Type.cpp revision 295ad30bf6212c16accc5095601b2a71d44b4c8b
1#include "Type.h" 2 3#include "Formatter.h" 4#include "ScalarType.h" 5 6#include <android-base/logging.h> 7 8namespace android { 9 10Type::Type() {} 11Type::~Type() {} 12 13bool Type::isScope() const { 14 return false; 15} 16 17bool Type::isInterface() const { 18 return false; 19} 20 21bool Type::isEnum() const { 22 return false; 23} 24 25bool Type::isTypeDef() const { 26 return false; 27} 28 29bool Type::isBinder() const { 30 return false; 31} 32 33const ScalarType *Type::resolveToScalarType() const { 34 return NULL; 35} 36 37bool Type::isValidEnumStorageType() const { 38 const ScalarType *scalarType = resolveToScalarType(); 39 40 if (scalarType == NULL) { 41 return false; 42 } 43 44 return scalarType->isValidEnumStorageType(); 45} 46 47std::string Type::getCppType(StorageMode, std::string *) const { 48 CHECK(!"Should not be here"); 49 return std::string(); 50} 51 52std::string Type::getJavaSuffix() const { 53 CHECK(!"Should not be here"); 54 return std::string(); 55} 56 57void Type::emitReaderWriter( 58 Formatter &, 59 const std::string &, 60 const std::string &, 61 bool, 62 bool, 63 ErrorMode) const { 64 CHECK(!"Should not be here"); 65} 66 67void Type::emitReaderWriterEmbedded( 68 Formatter &, 69 const std::string &, 70 bool, 71 const std::string &, 72 bool, 73 bool, 74 ErrorMode, 75 const std::string &, 76 const std::string &) const { 77 CHECK(!"Should not be here"); 78} 79 80void Type::emitJavaReaderWriter( 81 Formatter &out, 82 const std::string &parcelObj, 83 const std::string &argName, 84 bool isReader) const { 85 emitJavaReaderWriterWithSuffix( 86 out, 87 parcelObj, 88 argName, 89 isReader, 90 getJavaSuffix(), 91 "" /* extra */); 92} 93 94void Type::handleError(Formatter &out, ErrorMode mode) const { 95 switch (mode) { 96 case ErrorMode_Ignore: 97 { 98 out << "/* _hidl_err ignored! */\n\n"; 99 break; 100 } 101 102 case ErrorMode_Goto: 103 { 104 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; 105 break; 106 } 107 108 case ErrorMode_Break: 109 { 110 out << "if (_hidl_err != ::android::OK) { break; }\n\n"; 111 break; 112 } 113 114 case ErrorMode_Return: 115 { 116 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n"; 117 break; 118 } 119 } 120} 121 122void Type::handleError2(Formatter &out, ErrorMode mode) const { 123 switch (mode) { 124 case ErrorMode_Goto: 125 { 126 out << "goto _hidl_error;\n"; 127 break; 128 } 129 130 case ErrorMode_Break: 131 { 132 out << "break;\n"; 133 break; 134 } 135 136 case ErrorMode_Ignore: 137 { 138 out << "/* ignoring _hidl_error! */"; 139 break; 140 } 141 142 case ErrorMode_Return: 143 { 144 out << "return _hidl_err;\n"; 145 break; 146 } 147 } 148} 149 150void Type::emitReaderWriterEmbeddedForTypeName( 151 Formatter &out, 152 const std::string &name, 153 bool nameIsPointer, 154 const std::string &parcelObj, 155 bool parcelObjIsPointer, 156 bool isReader, 157 ErrorMode mode, 158 const std::string &parentName, 159 const std::string &offsetText, 160 const std::string &typeName, 161 const std::string &childName) const { 162 const std::string parcelObjDeref = 163 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj; 164 165 const std::string parcelObjPointer = 166 parcelObjIsPointer ? parcelObj : ("&" + parcelObj); 167 168 const std::string nameDeref = name + (nameIsPointer ? "->" : "."); 169 const std::string namePointer = nameIsPointer ? name : ("&" + name); 170 171 out << "_hidl_err = "; 172 173 if (isReader) { 174 out << "const_cast<" 175 << typeName 176 << " *>(" 177 << namePointer 178 << ")->readEmbeddedFromParcel(\n"; 179 } else { 180 out << nameDeref 181 << "writeEmbeddedToParcel(\n"; 182 } 183 184 out.indent(); 185 out.indent(); 186 187 out << (isReader ? parcelObjDeref : parcelObjPointer) 188 << ",\n" 189 << parentName 190 << ",\n" 191 << offsetText; 192 193 if (!childName.empty()) { 194 out << ", &" 195 << childName; 196 } 197 198 out << ");\n\n"; 199 200 out.unindent(); 201 out.unindent(); 202 203 handleError(out, mode); 204} 205 206status_t Type::emitTypeDeclarations(Formatter &) const { 207 return OK; 208} 209 210status_t Type::emitTypeDefinitions( 211 Formatter &, const std::string) const { 212 return OK; 213} 214 215status_t Type::emitJavaTypeDeclarations(Formatter &) const { 216 return OK; 217} 218 219bool Type::needsEmbeddedReadWrite() const { 220 return false; 221} 222 223bool Type::resultNeedsDeref() const { 224 return false; 225} 226 227std::string Type::getCppType(std::string *extra) const { 228 return getCppType(StorageMode_Stack, extra); 229} 230 231std::string Type::getCppResultType(std::string *extra) const { 232 return getCppType(StorageMode_Result, extra); 233} 234 235std::string Type::getCppArgumentType(std::string *extra) const { 236 return getCppType(StorageMode_Argument, extra); 237} 238 239void Type::emitJavaReaderWriterWithSuffix( 240 Formatter &out, 241 const std::string &parcelObj, 242 const std::string &argName, 243 bool isReader, 244 const std::string &suffix, 245 const std::string &extra) const { 246 out << parcelObj 247 << "." 248 << (isReader ? "read" : "write") 249 << suffix 250 << "("; 251 252 if (isReader) { 253 out << extra; 254 } else { 255 out << (extra.empty() ? "" : (extra + ", ")); 256 out << argName; 257 } 258 259 out << ");\n"; 260} 261 262status_t Type::emitVtsTypeDeclarations(Formatter &) const { 263 return OK; 264} 265 266status_t Type::emitVtsArgumentType(Formatter &out) const { 267 return emitVtsTypeDeclarations(out); 268} 269 270bool Type::isJavaCompatible() const { 271 return true; 272} 273 274} // namespace android 275 276