Type.cpp revision f630bc8736003dcf4aac3dfe47167beb6beb6c6a
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "Type.h" 18 19#include "Formatter.h" 20#include "ScalarType.h" 21 22#include <android-base/logging.h> 23 24namespace android { 25 26Type::Type() {} 27Type::~Type() {} 28 29bool Type::isScope() const { 30 return false; 31} 32 33bool Type::isInterface() const { 34 return false; 35} 36 37bool Type::isEnum() const { 38 return false; 39} 40 41bool Type::isTypeDef() const { 42 return false; 43} 44 45bool Type::isBinder() const { 46 return false; 47} 48 49bool Type::isNamedType() const { 50 return false; 51} 52 53bool Type::isCompoundType() const { 54 return false; 55} 56 57const ScalarType *Type::resolveToScalarType() const { 58 return NULL; 59} 60 61bool Type::isValidEnumStorageType() const { 62 const ScalarType *scalarType = resolveToScalarType(); 63 64 if (scalarType == NULL) { 65 return false; 66 } 67 68 return scalarType->isValidEnumStorageType(); 69} 70 71std::string Type::getCppType(StorageMode, std::string *, bool) const { 72 CHECK(!"Should not be here"); 73 return std::string(); 74} 75 76std::string Type::getJavaWrapperType() const { 77 return getJavaType(); 78} 79 80std::string Type::getJavaSuffix() const { 81 CHECK(!"Should not be here"); 82 return std::string(); 83} 84 85void Type::emitReaderWriter( 86 Formatter &, 87 const std::string &, 88 const std::string &, 89 bool, 90 bool, 91 ErrorMode) const { 92 CHECK(!"Should not be here"); 93} 94 95void Type::emitReaderWriterEmbedded( 96 Formatter &, 97 const std::string &, 98 bool, 99 const std::string &, 100 bool, 101 bool, 102 ErrorMode, 103 const std::string &, 104 const std::string &) const { 105 CHECK(!"Should not be here"); 106} 107 108void Type::emitJavaReaderWriter( 109 Formatter &out, 110 const std::string &parcelObj, 111 const std::string &argName, 112 bool isReader) const { 113 emitJavaReaderWriterWithSuffix( 114 out, 115 parcelObj, 116 argName, 117 isReader, 118 getJavaSuffix(), 119 "" /* extra */); 120} 121 122void Type::emitJavaFieldInitializer( 123 Formatter &out, 124 const std::string &fieldName) const { 125 out << getJavaType() 126 << " " 127 << fieldName 128 << ";\n"; 129} 130 131void Type::emitJavaFieldReaderWriter( 132 Formatter &, 133 const std::string &, 134 const std::string &, 135 const std::string &, 136 bool) const { 137 CHECK(!"Should not be here"); 138} 139 140void Type::handleError(Formatter &out, ErrorMode mode) const { 141 switch (mode) { 142 case ErrorMode_Ignore: 143 { 144 out << "/* _hidl_err ignored! */\n\n"; 145 break; 146 } 147 148 case ErrorMode_Goto: 149 { 150 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; 151 break; 152 } 153 154 case ErrorMode_Break: 155 { 156 out << "if (_hidl_err != ::android::OK) { break; }\n\n"; 157 break; 158 } 159 160 case ErrorMode_Return: 161 { 162 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n"; 163 break; 164 } 165 } 166} 167 168void Type::handleError2(Formatter &out, ErrorMode mode) const { 169 switch (mode) { 170 case ErrorMode_Goto: 171 { 172 out << "goto _hidl_error;\n"; 173 break; 174 } 175 176 case ErrorMode_Break: 177 { 178 out << "break;\n"; 179 break; 180 } 181 182 case ErrorMode_Ignore: 183 { 184 out << "/* ignoring _hidl_error! */"; 185 break; 186 } 187 188 case ErrorMode_Return: 189 { 190 out << "return _hidl_err;\n"; 191 break; 192 } 193 } 194} 195 196void Type::emitReaderWriterEmbeddedForTypeName( 197 Formatter &out, 198 const std::string &name, 199 bool nameIsPointer, 200 const std::string &parcelObj, 201 bool parcelObjIsPointer, 202 bool isReader, 203 ErrorMode mode, 204 const std::string &parentName, 205 const std::string &offsetText, 206 const std::string &typeName, 207 const std::string &childName) const { 208 const std::string parcelObjDeref = 209 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj; 210 211 const std::string parcelObjPointer = 212 parcelObjIsPointer ? parcelObj : ("&" + parcelObj); 213 214 const std::string nameDeref = name + (nameIsPointer ? "->" : "."); 215 const std::string namePointer = nameIsPointer ? name : ("&" + name); 216 217 out << "_hidl_err = "; 218 219 if (isReader) { 220 out << "const_cast<" 221 << typeName 222 << " *>(" 223 << namePointer 224 << ")->readEmbeddedFromParcel(\n"; 225 } else { 226 out << nameDeref 227 << "writeEmbeddedToParcel(\n"; 228 } 229 230 out.indent(); 231 out.indent(); 232 233 out << (isReader ? parcelObjDeref : parcelObjPointer) 234 << ",\n" 235 << parentName 236 << ",\n" 237 << offsetText; 238 239 if (!childName.empty()) { 240 out << ", &" 241 << childName; 242 } 243 244 out << ");\n\n"; 245 246 out.unindent(); 247 out.unindent(); 248 249 handleError(out, mode); 250} 251 252status_t Type::emitTypeDeclarations(Formatter &) const { 253 return OK; 254} 255 256status_t Type::emitTypeDefinitions( 257 Formatter &, const std::string) const { 258 return OK; 259} 260 261status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const { 262 return OK; 263} 264 265bool Type::needsEmbeddedReadWrite() const { 266 return false; 267} 268 269bool Type::resultNeedsDeref() const { 270 return false; 271} 272 273std::string Type::getCppType(std::string *extra, 274 bool specifyNamespaces) const { 275 return getCppType(StorageMode_Stack, extra, specifyNamespaces); 276} 277 278std::string Type::getCppResultType(std::string *extra, 279 bool specifyNamespaces) const { 280 return getCppType(StorageMode_Result, extra, specifyNamespaces); 281} 282 283std::string Type::getCppArgumentType(std::string *extra, 284 bool specifyNamespaces) const { 285 return getCppType(StorageMode_Argument, extra, specifyNamespaces); 286} 287 288void Type::emitJavaReaderWriterWithSuffix( 289 Formatter &out, 290 const std::string &parcelObj, 291 const std::string &argName, 292 bool isReader, 293 const std::string &suffix, 294 const std::string &extra) const { 295 out << parcelObj 296 << "." 297 << (isReader ? "read" : "write") 298 << suffix 299 << "("; 300 301 if (isReader) { 302 out << extra; 303 } else { 304 out << (extra.empty() ? "" : (extra + ", ")); 305 out << argName; 306 } 307 308 out << ");\n"; 309} 310 311status_t Type::emitVtsTypeDeclarations(Formatter &) const { 312 return OK; 313} 314 315status_t Type::emitVtsAttributeType(Formatter &out) const { 316 return emitVtsTypeDeclarations(out); 317} 318 319bool Type::isJavaCompatible() const { 320 return true; 321} 322 323void Type::getAlignmentAndSize(size_t *, size_t *) const { 324 CHECK(!"Should not be here"); 325} 326 327} // namespace android 328 329