Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #ifndef TYPE_H_ |
| 18 | |
| 19 | #define TYPE_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <utils/Errors.h> |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 24 | #include <vector> |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 25 | #include <set> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 29 | struct Annotation; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | struct Formatter; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 31 | struct ScalarType; |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 32 | struct FQName; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | |
| 34 | struct Type { |
| 35 | Type(); |
| 36 | virtual ~Type(); |
| 37 | |
Andreas Huber | 709b62d | 2016-09-19 11:21:18 -0700 | [diff] [blame] | 38 | virtual bool isArray() const; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 39 | virtual bool isBinder() const; |
Yifan Hong | abf73ee | 2016-12-05 18:47:00 -0800 | [diff] [blame] | 40 | virtual bool isBitField() const; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 41 | virtual bool isCompoundType() const; |
| 42 | virtual bool isEnum() const; |
Yifan Hong | abf73ee | 2016-12-05 18:47:00 -0800 | [diff] [blame] | 43 | virtual bool isHandle() const; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 44 | virtual bool isInterface() const; |
| 45 | virtual bool isNamedType() const; |
| 46 | virtual bool isPointer() const; |
| 47 | virtual bool isScope() const; |
Yifan Hong | abf73ee | 2016-12-05 18:47:00 -0800 | [diff] [blame] | 48 | virtual bool isScalar() const; |
| 49 | virtual bool isString() const; |
| 50 | virtual bool isTemplatedType() const; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 51 | virtual bool isTypeDef() const; |
Andreas Huber | 709b62d | 2016-09-19 11:21:18 -0700 | [diff] [blame] | 52 | virtual bool isVector() const; |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 53 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 54 | virtual const ScalarType *resolveToScalarType() const; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 56 | virtual std::string typeName() const; |
| 57 | |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 58 | bool isValidEnumStorageType() const; |
| 59 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 60 | enum StorageMode { |
| 61 | StorageMode_Stack, |
| 62 | StorageMode_Argument, |
Martijn Coenen | ac58789 | 2016-11-17 15:14:19 +0100 | [diff] [blame] | 63 | StorageMode_Result, |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 64 | }; |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 65 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 66 | virtual std::string getCppType( |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 67 | StorageMode mode, |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 68 | bool specifyNamespaces) const; |
| 69 | |
| 70 | std::string decorateCppName( |
| 71 | const std::string &name, |
| 72 | StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 73 | bool specifyNamespaces) const; |
| 74 | |
| 75 | /* gets all hidl-defined types used when this item is |
| 76 | * printed using getCppType or getJavaType. Examples: |
| 77 | * |
| 78 | * vec<vec<vec<IFoo>>>: IFoo is added to the set |
| 79 | * (the hypothetical type pair) |
| 80 | * pair<IFoo, IBar>: IFoo and IBar are added to the set |
| 81 | * int32_t: nothing is added to the set |
| 82 | * string: nothing is added to the set |
| 83 | * IFoo: IFoo is added to the set |
| 84 | */ |
| 85 | virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 86 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 87 | std::string getCppStackType(bool specifyNamespaces = true) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 88 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 89 | std::string getCppResultType(bool specifyNamespaces = true) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 90 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 91 | std::string getCppArgumentType(bool specifyNamespaces = true) const; |
Andreas Huber | 4c865b7 | 2016-09-14 15:26:27 -0700 | [diff] [blame] | 92 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 93 | // For an array type, dimensionality information will be accumulated at the |
| 94 | // end of the returned string. |
Andreas Huber | 4c865b7 | 2016-09-14 15:26:27 -0700 | [diff] [blame] | 95 | // if forInitializer == true, actual dimensions are included, i.e. [3][5], |
| 96 | // otherwise (and by default), they are omitted, i.e. [][]. |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 97 | virtual std::string getJavaType(bool forInitializer = false) const; |
Andreas Huber | 4c865b7 | 2016-09-14 15:26:27 -0700 | [diff] [blame] | 98 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 99 | virtual std::string getJavaWrapperType() const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 100 | virtual std::string getJavaSuffix() const; |
| 101 | |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 102 | virtual std::string getVtsType() const; |
| 103 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 104 | enum ErrorMode { |
| 105 | ErrorMode_Ignore, |
| 106 | ErrorMode_Goto, |
| 107 | ErrorMode_Break, |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 108 | ErrorMode_Return, |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 109 | }; |
| 110 | virtual void emitReaderWriter( |
| 111 | Formatter &out, |
| 112 | const std::string &name, |
| 113 | const std::string &parcelObj, |
| 114 | bool parcelObjIsPointer, |
| 115 | bool isReader, |
| 116 | ErrorMode mode) const; |
| 117 | |
| 118 | virtual void emitReaderWriterEmbedded( |
| 119 | Formatter &out, |
Andreas Huber | f9d49f1 | 2016-09-12 14:58:36 -0700 | [diff] [blame] | 120 | size_t depth, |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 121 | const std::string &name, |
Yifan Hong | be2a373 | 2016-10-05 13:33:41 -0700 | [diff] [blame] | 122 | const std::string &sanitizedName, |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 123 | bool nameIsPointer, |
| 124 | const std::string &parcelObj, |
| 125 | bool parcelObjIsPointer, |
| 126 | bool isReader, |
| 127 | ErrorMode mode, |
| 128 | const std::string &parentName, |
| 129 | const std::string &offsetText) const; |
| 130 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 131 | virtual void emitResolveReferences( |
| 132 | Formatter &out, |
| 133 | const std::string &name, |
| 134 | bool nameIsPointer, |
| 135 | const std::string &parcelObj, |
| 136 | bool parcelObjIsPointer, |
| 137 | bool isReader, |
| 138 | ErrorMode mode) const; |
| 139 | |
| 140 | virtual void emitResolveReferencesEmbedded( |
| 141 | Formatter &out, |
| 142 | size_t depth, |
| 143 | const std::string &name, |
| 144 | const std::string &sanitizedName, |
| 145 | bool nameIsPointer, |
| 146 | const std::string &parcelObj, |
| 147 | bool parcelObjIsPointer, |
| 148 | bool isReader, |
| 149 | ErrorMode mode, |
| 150 | const std::string &parentName, |
| 151 | const std::string &offsetText) const; |
| 152 | |
Yifan Hong | 00f4717 | 2016-09-30 14:40:45 -0700 | [diff] [blame] | 153 | virtual bool useParentInEmitResolveReferencesEmbedded() const; |
| 154 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 155 | virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const; |
| 156 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 157 | virtual void emitJavaReaderWriter( |
| 158 | Formatter &out, |
| 159 | const std::string &parcelObj, |
| 160 | const std::string &argName, |
| 161 | bool isReader) const; |
| 162 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 163 | virtual void emitJavaFieldInitializer( |
| 164 | Formatter &out, |
| 165 | const std::string &fieldName) const; |
| 166 | |
| 167 | virtual void emitJavaFieldReaderWriter( |
| 168 | Formatter &out, |
Andreas Huber | 4c865b7 | 2016-09-14 15:26:27 -0700 | [diff] [blame] | 169 | size_t depth, |
Andreas Huber | 709b62d | 2016-09-19 11:21:18 -0700 | [diff] [blame] | 170 | const std::string &parcelName, |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 171 | const std::string &blobName, |
| 172 | const std::string &fieldName, |
| 173 | const std::string &offset, |
| 174 | bool isReader) const; |
| 175 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 176 | virtual status_t emitTypeDeclarations(Formatter &out) const; |
| 177 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 178 | // Emit any declarations pertaining to this type that have to be |
| 179 | // at global scope, i.e. enum class operators. |
| 180 | virtual status_t emitGlobalTypeDeclarations(Formatter &out) const; |
| 181 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 182 | // Emit any declarations pertaining to this type that have to be |
| 183 | // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel |
| 184 | virtual status_t emitGlobalHwDeclarations(Formatter &out) const; |
| 185 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 186 | virtual status_t emitTypeDefinitions( |
| 187 | Formatter &out, const std::string prefix) const; |
| 188 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 189 | virtual status_t emitJavaTypeDeclarations( |
| 190 | Formatter &out, bool atTopLevel) const; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 191 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 192 | virtual bool needsEmbeddedReadWrite() const; |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 193 | virtual bool needsResolveReferences() const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 194 | virtual bool resultNeedsDeref() const; |
| 195 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 196 | // Generates type declaration for vts proto file. |
| 197 | // TODO (b/30844146): make it a pure virtual method. |
| 198 | virtual status_t emitVtsTypeDeclarations(Formatter &out) const; |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 199 | // Generates type declaration as attribute of method (return value or method |
| 200 | // argument) or attribute of compound type for vts proto file. |
| 201 | virtual status_t emitVtsAttributeType(Formatter &out) const; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 202 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 203 | // Returns true iff this type is supported through the Java backend. |
| 204 | virtual bool isJavaCompatible() const; |
| 205 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 206 | virtual void getAlignmentAndSize(size_t *align, size_t *size) const; |
| 207 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 208 | void setAnnotations(std::vector<Annotation *> *annotations); |
| 209 | const std::vector<Annotation *> &annotations() const; |
| 210 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 211 | virtual void appendToExportedTypesVector( |
| 212 | std::vector<const Type *> *exportedTypes) const; |
| 213 | |
Andreas Huber | 1c50727 | 2016-10-05 14:33:21 -0700 | [diff] [blame] | 214 | virtual status_t emitExportedHeader(Formatter &out, bool forJava) const; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 215 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 216 | protected: |
| 217 | void handleError(Formatter &out, ErrorMode mode) const; |
| 218 | void handleError2(Formatter &out, ErrorMode mode) const; |
| 219 | |
| 220 | void emitReaderWriterEmbeddedForTypeName( |
| 221 | Formatter &out, |
| 222 | const std::string &name, |
| 223 | bool nameIsPointer, |
| 224 | const std::string &parcelObj, |
| 225 | bool parcelObjIsPointer, |
| 226 | bool isReader, |
| 227 | ErrorMode mode, |
| 228 | const std::string &parentName, |
| 229 | const std::string &offsetText, |
| 230 | const std::string &typeName, |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 231 | const std::string &childName, |
| 232 | const std::string &funcNamespace) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 233 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 234 | void emitJavaReaderWriterWithSuffix( |
| 235 | Formatter &out, |
| 236 | const std::string &parcelObj, |
| 237 | const std::string &argName, |
| 238 | bool isReader, |
| 239 | const std::string &suffix, |
| 240 | const std::string &extra) const; |
| 241 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 242 | private: |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 243 | std::vector<Annotation *> *mAnnotations; |
| 244 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 245 | DISALLOW_COPY_AND_ASSIGN(Type); |
| 246 | }; |
| 247 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 248 | /* Base type for VectorType and RefType. */ |
| 249 | struct TemplatedType : public Type { |
| 250 | void setElementType(Type *elementType); |
Yifan Hong | abf73ee | 2016-12-05 18:47:00 -0800 | [diff] [blame] | 251 | Type *getElementType() const; |
| 252 | bool isTemplatedType() const override; |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 253 | virtual bool isCompatibleElementType(Type *elementType) const = 0; |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 254 | protected: |
| 255 | TemplatedType(); |
| 256 | Type *mElementType; |
| 257 | private: |
| 258 | DISALLOW_COPY_AND_ASSIGN(TemplatedType); |
| 259 | }; |
| 260 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 261 | } // namespace android |
| 262 | |
| 263 | #endif // TYPE_H_ |
| 264 | |