blob: 1338ffbb271fa746d01859c6abfc286667e68875 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017#ifndef TYPE_H_
18
19#define TYPE_H_
20
21#include <android-base/macros.h>
Andreas Huber881227d2016-08-02 14:20:21 -070022#include <utils/Errors.h>
Steven Moreland979e0992016-09-07 09:18:08 -070023#include <set>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070024#include <string>
Timur Iskhakov33431e62017-08-21 17:31:23 -070025#include <unordered_set>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070026#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Timur Iskhakov505316c2017-08-05 03:38:59 +000028#include "Reference.h"
29
Andreas Huberc9410c72016-07-28 12:18:40 -070030namespace android {
31
Timur Iskhakov891a8662017-08-25 21:53:48 -070032struct ConstantExpression;
Andreas Huberc9410c72016-07-28 12:18:40 -070033struct Formatter;
Steven Moreland979e0992016-09-07 09:18:08 -070034struct FQName;
Timur Iskhakov891a8662017-08-25 21:53:48 -070035struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070036
37struct Type {
38 Type();
39 virtual ~Type();
40
Andreas Huber709b62d2016-09-19 11:21:18 -070041 virtual bool isArray() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010042 virtual bool isBinder() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080043 virtual bool isBitField() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010044 virtual bool isCompoundType() const;
45 virtual bool isEnum() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080046 virtual bool isHandle() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010047 virtual bool isInterface() const;
48 virtual bool isNamedType() const;
Steven Moreland397b5e12017-06-08 14:02:26 -070049 virtual bool isMemory() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010050 virtual bool isPointer() const;
51 virtual bool isScope() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080052 virtual bool isScalar() const;
53 virtual bool isString() const;
54 virtual bool isTemplatedType() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010055 virtual bool isTypeDef() const;
Andreas Huber709b62d2016-09-19 11:21:18 -070056 virtual bool isVector() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070057
Timur Iskhakov33431e62017-08-21 17:31:23 -070058 // All types defined in this type.
Timur Iskhakovb58f4182017-08-29 15:19:24 -070059 std::vector<Type*> getDefinedTypes();
60 virtual std::vector<const Type*> getDefinedTypes() const;
Timur Iskhakov33431e62017-08-21 17:31:23 -070061
62 // All types referenced in this type.
Timur Iskhakovb58f4182017-08-29 15:19:24 -070063 std::vector<Reference<Type>*> getReferences();
64 virtual std::vector<const Reference<Type>*> getReferences() const;
Timur Iskhakov33431e62017-08-21 17:31:23 -070065
Timur Iskhakov891a8662017-08-25 21:53:48 -070066 // All constant expressions referenced in this type.
Timur Iskhakovb58f4182017-08-29 15:19:24 -070067 std::vector<ConstantExpression*> getConstantExpressions();
68 virtual std::vector<const ConstantExpression*> getConstantExpressions() const;
Timur Iskhakov891a8662017-08-25 21:53:48 -070069
Timur Iskhakov40731af2017-08-24 14:18:35 -070070 // All types referenced in this type that must have completed
71 // definiton before being referenced.
Timur Iskhakovb58f4182017-08-29 15:19:24 -070072 std::vector<Reference<Type>*> getStrongReferences();
73 virtual std::vector<const Reference<Type>*> getStrongReferences() const;
Timur Iskhakov40731af2017-08-24 14:18:35 -070074
Timur Iskhakov33431e62017-08-21 17:31:23 -070075 // Proceeds recursive pass
76 // Makes sure to visit each node only once.
77 status_t recursivePass(const std::function<status_t(Type*)>& func,
78 std::unordered_set<const Type*>* visited);
79 status_t recursivePass(const std::function<status_t(const Type*)>& func,
80 std::unordered_set<const Type*>* visited) const;
81
Timur Iskhakovcec46c42017-08-09 00:22:02 -070082 // Recursive tree pass that completes type declarations
83 // that depend on super types
84 virtual status_t resolveInheritance();
85
Timur Iskhakovcec46c42017-08-09 00:22:02 -070086 // Recursive tree pass that validates all type-related
87 // syntax restrictions
88 virtual status_t validate() const;
89
Timur Iskhakov40731af2017-08-24 14:18:35 -070090 // Recursive tree pass checkAcyclic return type.
91 // Stores cycle end for nice error messages.
92 struct CheckAcyclicStatus {
93 CheckAcyclicStatus(status_t status, const Type* cycleEnd = nullptr);
94
95 status_t status;
96
97 // If a cycle is found, stores the end of cycle.
98 // While going back in recursion, this is used to stop printing the cycle.
99 const Type* cycleEnd;
100 };
101
102 // Recursive tree pass that ensures that type definitions and references
103 // are acyclic.
104 // If some cases allow using of incomplete types, these cases are to be
105 // declared in Type::getStrongReferences.
106 CheckAcyclicStatus checkAcyclic(std::unordered_set<const Type*>* visited,
107 std::unordered_set<const Type*>* stack) const;
108
Andreas Huber737080b2016-08-02 15:38:04 -0700109 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -0700110
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700111 virtual std::string typeName() const = 0;
Steven Moreland30bb6a82016-11-30 09:18:34 -0800112
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700113 bool isValidEnumStorageType() const;
Steven Moreland9df52442016-12-12 08:51:14 -0800114 virtual bool isElidableType() const;
Yifan Hongc6752dc2016-12-20 14:00:14 -0800115 virtual bool canCheckEquality() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700116
Timur Iskhakov35930c42017-08-28 18:49:54 -0700117 // Marks that package proceeding is completed
118 // Post parse passes must be proceeded during owner package parsing
119 void setPostParseCompleted();
120
Andreas Huber881227d2016-08-02 14:20:21 -0700121 enum StorageMode {
122 StorageMode_Stack,
123 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +0100124 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -0700125 };
Yifan Hong3b320f82016-11-01 15:15:54 -0700126
Steven Morelande30ee9b2017-05-09 13:31:01 -0700127 // specifyNamespaces: whether to specify namespaces for built-in types
Andreas Huber881227d2016-08-02 14:20:21 -0700128 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -0700129 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -0700130 bool specifyNamespaces) const;
131
132 std::string decorateCppName(
133 const std::string &name,
134 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700135 bool specifyNamespaces) const;
136
Yifan Hong3b320f82016-11-01 15:15:54 -0700137 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700138
Yifan Hong3b320f82016-11-01 15:15:54 -0700139 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700140
Yifan Hong3b320f82016-11-01 15:15:54 -0700141 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -0700142
Yifan Hong4ed13472016-11-02 10:44:11 -0700143 // For an array type, dimensionality information will be accumulated at the
144 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -0700145 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
146 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -0700147 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -0700148
Andreas Huber85eabdb2016-08-25 11:24:49 -0700149 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -0700150 virtual std::string getJavaSuffix() const;
151
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700152 virtual std::string getVtsType() const;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800153 virtual std::string getVtsValueName() const;
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700154
Andreas Huber881227d2016-08-02 14:20:21 -0700155 enum ErrorMode {
156 ErrorMode_Ignore,
157 ErrorMode_Goto,
158 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700159 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700160 };
161 virtual void emitReaderWriter(
162 Formatter &out,
163 const std::string &name,
164 const std::string &parcelObj,
165 bool parcelObjIsPointer,
166 bool isReader,
167 ErrorMode mode) const;
168
169 virtual void emitReaderWriterEmbedded(
170 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700171 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700172 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700173 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700174 bool nameIsPointer,
175 const std::string &parcelObj,
176 bool parcelObjIsPointer,
177 bool isReader,
178 ErrorMode mode,
179 const std::string &parentName,
180 const std::string &offsetText) const;
181
Yifan Hongbf459bc2016-08-23 16:50:37 -0700182 virtual void emitResolveReferences(
183 Formatter &out,
184 const std::string &name,
185 bool nameIsPointer,
186 const std::string &parcelObj,
187 bool parcelObjIsPointer,
188 bool isReader,
189 ErrorMode mode) const;
190
191 virtual void emitResolveReferencesEmbedded(
192 Formatter &out,
193 size_t depth,
194 const std::string &name,
195 const std::string &sanitizedName,
196 bool nameIsPointer,
197 const std::string &parcelObj,
198 bool parcelObjIsPointer,
199 bool isReader,
200 ErrorMode mode,
201 const std::string &parentName,
202 const std::string &offsetText) const;
203
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800204 virtual void emitDump(
205 Formatter &out,
206 const std::string &streamName,
207 const std::string &name) const;
208
Yifan Honge45b5302017-02-22 10:49:07 -0800209 virtual void emitJavaDump(
210 Formatter &out,
211 const std::string &streamName,
212 const std::string &name) const;
213
Yifan Hong00f47172016-09-30 14:40:45 -0700214 virtual bool useParentInEmitResolveReferencesEmbedded() const;
215
Yifan Hong244e82d2016-11-11 11:13:57 -0800216 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
217
Andreas Huber2831d512016-08-15 09:33:47 -0700218 virtual void emitJavaReaderWriter(
219 Formatter &out,
220 const std::string &parcelObj,
221 const std::string &argName,
222 bool isReader) const;
223
Andreas Huber85eabdb2016-08-25 11:24:49 -0700224 virtual void emitJavaFieldInitializer(
225 Formatter &out,
226 const std::string &fieldName) const;
227
228 virtual void emitJavaFieldReaderWriter(
229 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700230 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700231 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700232 const std::string &blobName,
233 const std::string &fieldName,
234 const std::string &offset,
235 bool isReader) const;
236
Andreas Huber881227d2016-08-02 14:20:21 -0700237 virtual status_t emitTypeDeclarations(Formatter &out) const;
238
Andreas Hubere3f769a2016-10-10 10:54:44 -0700239 // Emit any declarations pertaining to this type that have to be
240 // at global scope, i.e. enum class operators.
241 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
242
Yifan Hong244e82d2016-11-11 11:13:57 -0800243 // Emit any declarations pertaining to this type that have to be
244 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
245 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
246
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700247 virtual status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700248
Andreas Huber85eabdb2016-08-25 11:24:49 -0700249 virtual status_t emitJavaTypeDeclarations(
250 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700251
Andreas Huber881227d2016-08-02 14:20:21 -0700252 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700253 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700254 virtual bool resultNeedsDeref() const;
255
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700256 // Generates type declaration for vts proto file.
257 // TODO (b/30844146): make it a pure virtual method.
258 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700259 // Generates type declaration as attribute of method (return value or method
260 // argument) or attribute of compound type for vts proto file.
261 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700262
Andreas Huber70a59e12016-08-16 12:57:01 -0700263 // Returns true iff this type is supported through the Java backend.
264 virtual bool isJavaCompatible() const;
Andreas Huber60d3b222017-03-30 09:10:56 -0700265 virtual bool containsPointer() const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700266 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
267
Andreas Huber019d21d2016-10-03 12:59:47 -0700268 virtual void appendToExportedTypesVector(
269 std::vector<const Type *> *exportedTypes) const;
270
Andreas Huber1c507272016-10-05 14:33:21 -0700271 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700272
Andreas Huber881227d2016-08-02 14:20:21 -0700273protected:
274 void handleError(Formatter &out, ErrorMode mode) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700275
276 void emitReaderWriterEmbeddedForTypeName(
277 Formatter &out,
278 const std::string &name,
279 bool nameIsPointer,
280 const std::string &parcelObj,
281 bool parcelObjIsPointer,
282 bool isReader,
283 ErrorMode mode,
284 const std::string &parentName,
285 const std::string &offsetText,
286 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800287 const std::string &childName,
288 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700289
Andreas Huber2831d512016-08-15 09:33:47 -0700290 void emitJavaReaderWriterWithSuffix(
291 Formatter &out,
292 const std::string &parcelObj,
293 const std::string &argName,
294 bool isReader,
295 const std::string &suffix,
296 const std::string &extra) const;
297
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800298 void emitDumpWithMethod(
299 Formatter &out,
300 const std::string &streamName,
301 const std::string &methodName,
302 const std::string &name) const;
303
Timur Iskhakov35930c42017-08-28 18:49:54 -0700304 private:
305 bool mIsPostParseCompleted = false;
306
Andreas Huberc9410c72016-07-28 12:18:40 -0700307 DISALLOW_COPY_AND_ASSIGN(Type);
308};
309
Yifan Hongbf459bc2016-08-23 16:50:37 -0700310/* Base type for VectorType and RefType. */
311struct TemplatedType : public Type {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000312 void setElementType(const Reference<Type>& elementType);
313 Type* getElementType() const;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700314
Timur Iskhakov33431e62017-08-21 17:31:23 -0700315 bool isTemplatedType() const override;
316
317 virtual bool isCompatibleElementType(Type* elementType) const = 0;
318
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700319 std::vector<const Reference<Type>*> getReferences() const override;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700320
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700321 virtual status_t validate() const override;
322
Timur Iskhakov33431e62017-08-21 17:31:23 -0700323 status_t emitVtsTypeDeclarations(Formatter& out) const override;
324 status_t emitVtsAttributeType(Formatter& out) const override;
325
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700326 protected:
Yifan Hongbf459bc2016-08-23 16:50:37 -0700327 TemplatedType();
Timur Iskhakov505316c2017-08-05 03:38:59 +0000328 Reference<Type> mElementType;
329
330 private:
Yifan Hongbf459bc2016-08-23 16:50:37 -0700331 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
332};
333
Andreas Huberc9410c72016-07-28 12:18:40 -0700334} // namespace android
335
336#endif // TYPE_H_
337