1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RefType.h"
#include "ArrayType.h"
#include "CompoundType.h"
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
namespace android {
RefType::RefType(Scope* parent) : TemplatedType(parent) {}
std::string RefType::templatedTypeName() const {
return "ref";
}
std::vector<const Reference<Type>*> RefType::getStrongReferences() const {
return {};
}
std::string RefType::getVtsType() const {
return "TYPE_REF";
}
std::string RefType::getVtsValueName() const {
return "ref_value";
}
bool RefType::isCompatibleElementType(const Type* elementType) const {
if (elementType->isScalar()) {
return true;
}
if (elementType->isString()) {
return true;
}
if (elementType->isEnum()) {
return true;
}
if (elementType->isBitField()) {
return true;
}
if (elementType->isCompoundType() &&
static_cast<const CompoundType*>(elementType)->style() == CompoundType::STYLE_STRUCT) {
return true;
}
if (elementType->isTemplatedType()) {
return this->isCompatibleElementType(
static_cast<const TemplatedType*>(elementType)->getElementType());
}
if (elementType->isArray()) {
return this->isCompatibleElementType(
static_cast<const ArrayType*>(elementType)->getElementType());
}
return false;
}
/* return something like "T const *".
* The reason we don't return "const T *" is to handle cases like
* ref<ref<ref<T>>> t_3ptr;
* in this case the const's will get stacked on the left (const const const T *** t_3ptr)
* but in this implementation it would be clearer (T const* const* const* t_3ptr) */
std::string RefType::getCppType(StorageMode /*mode*/, bool specifyNamespaces) const {
return mElementType->getCppStackType(specifyNamespaces)
+ " const*";
}
void RefType::emitReaderWriter(
Formatter &,
const std::string &,
const std::string &,
bool,
bool,
ErrorMode) const {
// RefType doesn't get read / written at this stage.
return;
}
void RefType::emitResolveReferences(
Formatter &out,
const std::string &name,
bool nameIsPointer,
const std::string &parcelObj,
bool parcelObjIsPointer,
bool isReader,
ErrorMode mode) const {
emitResolveReferencesEmbedded(
out,
0 /* depth */,
name,
name /* sanitizedName */,
nameIsPointer,
parcelObj,
parcelObjIsPointer,
isReader,
mode,
"", // parentName
""); // offsetText
}
void RefType::emitResolveReferencesEmbedded(
Formatter &out,
size_t /* depth */,
const std::string &name,
const std::string &sanitizedName,
bool /*nameIsPointer*/,
const std::string &parcelObj,
bool parcelObjIsPointer,
bool isReader,
ErrorMode mode,
const std::string &parentName,
const std::string &offsetText) const {
std::string elementType = mElementType->getCppStackType();
std::string baseType = getCppStackType();
const std::string parcelObjDeref =
parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
const std::string parcelObjPointer =
parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
// as if nameIsPointer is false. Pointers are always pass by values,
// so name is always the pointer value itself. Hence nameIsPointer = false.
const std::string namePointer = "&" + name;
const std::string handleName = "_hidl_" + sanitizedName + "__ref_handle";
const std::string resolveBufName = "_hidl_" + sanitizedName + "__ref_resolve_buf";
bool isEmbedded = (!parentName.empty() && !offsetText.empty());
out << "size_t " << handleName << ";\n"
<< "bool " << resolveBufName << ";\n\n";
out << "_hidl_err = ";
if (isReader) {
out << "::android::hardware::read"
<< (isEmbedded ? "Embedded" : "")
<< "ReferenceFromParcel<"
<< elementType
<< ">(const_cast<"
<< baseType
<< " *>("
<< namePointer
<< "),";
} else {
out << "::android::hardware::write"
<< (isEmbedded ? "Embedded" : "")
<< "ReferenceToParcel<"
<< elementType
<< ">("
<< name
<< ",";
}
out.indent();
out.indent();
out << (isReader ? parcelObjDeref : parcelObjPointer);
if(isEmbedded)
out << ",\n"
<< parentName
<< ",\n"
<< offsetText;
out << ",\n&" + handleName;
out << ",\n&" + resolveBufName;
out << ");\n\n";
out.unindent();
out.unindent();
handleError(out, mode);
if(!mElementType->needsResolveReferences() && !mElementType->needsEmbeddedReadWrite())
return; // no need to deal with element type recursively.
out << "if(" << resolveBufName << ") {\n";
out.indent();
if(mElementType->needsEmbeddedReadWrite()) {
mElementType->emitReaderWriterEmbedded(
out,
0 /* depth */,
name,
sanitizedName,
true /* nameIsPointer */, // for element type, name is a pointer.
parcelObj,
parcelObjIsPointer,
isReader,
mode,
handleName,
"0 /* parentOffset */");
}
if(mElementType->needsResolveReferences()) {
mElementType->emitResolveReferencesEmbedded(
out,
0 /* depth */,
"(*" + name + ")",
sanitizedName + "_deref",
false /* nameIsPointer */,
// must deref it and say false here, otherwise pointer to pointers don't work
parcelObj,
parcelObjIsPointer,
isReader,
mode,
handleName,
"0 /* parentOffset */");
}
out.unindent();
out << "}\n\n";
}
bool RefType::deepNeedsResolveReferences(std::unordered_set<const Type*>* /* visited */) const {
return true;
}
bool RefType::needsEmbeddedReadWrite() const {
return false;
}
bool RefType::resultNeedsDeref() const {
return false;
}
bool RefType::deepIsJavaCompatible(std::unordered_set<const Type*>* /* visited */) const {
return false;
}
bool RefType::deepContainsPointer(std::unordered_set<const Type*>* /* visited */) const {
return true;
}
} // namespace android
|