blob: ddea9bd65642e041fea033f45d38cd574e382265 [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.
59 virtual std::vector<Type*> getDefinedTypes() const;
60
61 // All types referenced in this type.
62 virtual std::vector<Reference<Type>> getReferences() const;
63
Timur Iskhakov891a8662017-08-25 21:53:48 -070064 // All constant expressions referenced in this type.
65 virtual std::vector<ConstantExpression*> getConstantExpressions() const;
66
Timur Iskhakov40731af2017-08-24 14:18:35 -070067 // All types referenced in this type that must have completed
68 // definiton before being referenced.
69 virtual std::vector<Reference<Type>> getStrongReferences() const;
70
Timur Iskhakov33431e62017-08-21 17:31:23 -070071 // Proceeds recursive pass
72 // Makes sure to visit each node only once.
73 status_t recursivePass(const std::function<status_t(Type*)>& func,
74 std::unordered_set<const Type*>* visited);
75 status_t recursivePass(const std::function<status_t(const Type*)>& func,
76 std::unordered_set<const Type*>* visited) const;
77
Timur Iskhakovcec46c42017-08-09 00:22:02 -070078 // Recursive tree pass that completes type declarations
79 // that depend on super types
80 virtual status_t resolveInheritance();
81
Timur Iskhakovcec46c42017-08-09 00:22:02 -070082 // Recursive tree pass that validates all type-related
83 // syntax restrictions
84 virtual status_t validate() const;
85
Timur Iskhakov40731af2017-08-24 14:18:35 -070086 // Recursive tree pass checkAcyclic return type.
87 // Stores cycle end for nice error messages.
88 struct CheckAcyclicStatus {
89 CheckAcyclicStatus(status_t status, const Type* cycleEnd = nullptr);
90
91 status_t status;
92
93 // If a cycle is found, stores the end of cycle.
94 // While going back in recursion, this is used to stop printing the cycle.
95 const Type* cycleEnd;
96 };
97
98 // Recursive tree pass that ensures that type definitions and references
99 // are acyclic.
100 // If some cases allow using of incomplete types, these cases are to be
101 // declared in Type::getStrongReferences.
102 CheckAcyclicStatus checkAcyclic(std::unordered_set<const Type*>* visited,
103 std::unordered_set<const Type*>* stack) const;
104
Andreas Huber737080b2016-08-02 15:38:04 -0700105 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -0700106
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700107 virtual std::string typeName() const = 0;
Steven Moreland30bb6a82016-11-30 09:18:34 -0800108
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700109 bool isValidEnumStorageType() const;
Steven Moreland9df52442016-12-12 08:51:14 -0800110 virtual bool isElidableType() const;
Yifan Hongc6752dc2016-12-20 14:00:14 -0800111 virtual bool canCheckEquality() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700112
Timur Iskhakov35930c42017-08-28 18:49:54 -0700113 // Marks that package proceeding is completed
114 // Post parse passes must be proceeded during owner package parsing
115 void setPostParseCompleted();
116
Andreas Huber881227d2016-08-02 14:20:21 -0700117 enum StorageMode {
118 StorageMode_Stack,
119 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +0100120 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -0700121 };
Yifan Hong3b320f82016-11-01 15:15:54 -0700122
Steven Morelande30ee9b2017-05-09 13:31:01 -0700123 // specifyNamespaces: whether to specify namespaces for built-in types
Andreas Huber881227d2016-08-02 14:20:21 -0700124 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -0700125 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -0700126 bool specifyNamespaces) const;
127
128 std::string decorateCppName(
129 const std::string &name,
130 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700131 bool specifyNamespaces) const;
132
Yifan Hong3b320f82016-11-01 15:15:54 -0700133 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700134
Yifan Hong3b320f82016-11-01 15:15:54 -0700135 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700136
Yifan Hong3b320f82016-11-01 15:15:54 -0700137 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -0700138
Yifan Hong4ed13472016-11-02 10:44:11 -0700139 // For an array type, dimensionality information will be accumulated at the
140 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -0700141 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
142 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -0700143 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -0700144
Andreas Huber85eabdb2016-08-25 11:24:49 -0700145 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -0700146 virtual std::string getJavaSuffix() const;
147
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700148 virtual std::string getVtsType() const;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800149 virtual std::string getVtsValueName() const;
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700150
Andreas Huber881227d2016-08-02 14:20:21 -0700151 enum ErrorMode {
152 ErrorMode_Ignore,
153 ErrorMode_Goto,
154 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700155 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700156 };
157 virtual void emitReaderWriter(
158 Formatter &out,
159 const std::string &name,
160 const std::string &parcelObj,
161 bool parcelObjIsPointer,
162 bool isReader,
163 ErrorMode mode) const;
164
165 virtual void emitReaderWriterEmbedded(
166 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700167 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700168 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700169 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700170 bool nameIsPointer,
171 const std::string &parcelObj,
172 bool parcelObjIsPointer,
173 bool isReader,
174 ErrorMode mode,
175 const std::string &parentName,
176 const std::string &offsetText) const;
177
Yifan Hongbf459bc2016-08-23 16:50:37 -0700178 virtual void emitResolveReferences(
179 Formatter &out,
180 const std::string &name,
181 bool nameIsPointer,
182 const std::string &parcelObj,
183 bool parcelObjIsPointer,
184 bool isReader,
185 ErrorMode mode) const;
186
187 virtual void emitResolveReferencesEmbedded(
188 Formatter &out,
189 size_t depth,
190 const std::string &name,
191 const std::string &sanitizedName,
192 bool nameIsPointer,
193 const std::string &parcelObj,
194 bool parcelObjIsPointer,
195 bool isReader,
196 ErrorMode mode,
197 const std::string &parentName,
198 const std::string &offsetText) const;
199
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800200 virtual void emitDump(
201 Formatter &out,
202 const std::string &streamName,
203 const std::string &name) const;
204
Yifan Honge45b5302017-02-22 10:49:07 -0800205 virtual void emitJavaDump(
206 Formatter &out,
207 const std::string &streamName,
208 const std::string &name) const;
209
Yifan Hong00f47172016-09-30 14:40:45 -0700210 virtual bool useParentInEmitResolveReferencesEmbedded() const;
211
Yifan Hong244e82d2016-11-11 11:13:57 -0800212 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
213
Andreas Huber2831d512016-08-15 09:33:47 -0700214 virtual void emitJavaReaderWriter(
215 Formatter &out,
216 const std::string &parcelObj,
217 const std::string &argName,
218 bool isReader) const;
219
Andreas Huber85eabdb2016-08-25 11:24:49 -0700220 virtual void emitJavaFieldInitializer(
221 Formatter &out,
222 const std::string &fieldName) const;
223
224 virtual void emitJavaFieldReaderWriter(
225 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700226 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700227 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700228 const std::string &blobName,
229 const std::string &fieldName,
230 const std::string &offset,
231 bool isReader) const;
232
Andreas Huber881227d2016-08-02 14:20:21 -0700233 virtual status_t emitTypeDeclarations(Formatter &out) const;
234
Andreas Hubere3f769a2016-10-10 10:54:44 -0700235 // Emit any declarations pertaining to this type that have to be
236 // at global scope, i.e. enum class operators.
237 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
238
Yifan Hong244e82d2016-11-11 11:13:57 -0800239 // Emit any declarations pertaining to this type that have to be
240 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
241 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
242
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700243 virtual status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700244
Andreas Huber85eabdb2016-08-25 11:24:49 -0700245 virtual status_t emitJavaTypeDeclarations(
246 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700247
Andreas Huber881227d2016-08-02 14:20:21 -0700248 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700249 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700250 virtual bool resultNeedsDeref() const;
251
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700252 // Generates type declaration for vts proto file.
253 // TODO (b/30844146): make it a pure virtual method.
254 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700255 // Generates type declaration as attribute of method (return value or method
256 // argument) or attribute of compound type for vts proto file.
257 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700258
Andreas Huber70a59e12016-08-16 12:57:01 -0700259 // Returns true iff this type is supported through the Java backend.
260 virtual bool isJavaCompatible() const;
Andreas Huber60d3b222017-03-30 09:10:56 -0700261 virtual bool containsPointer() const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700262 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
263
Andreas Huber019d21d2016-10-03 12:59:47 -0700264 virtual void appendToExportedTypesVector(
265 std::vector<const Type *> *exportedTypes) const;
266
Andreas Huber1c507272016-10-05 14:33:21 -0700267 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700268
Andreas Huber881227d2016-08-02 14:20:21 -0700269protected:
270 void handleError(Formatter &out, ErrorMode mode) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700271
272 void emitReaderWriterEmbeddedForTypeName(
273 Formatter &out,
274 const std::string &name,
275 bool nameIsPointer,
276 const std::string &parcelObj,
277 bool parcelObjIsPointer,
278 bool isReader,
279 ErrorMode mode,
280 const std::string &parentName,
281 const std::string &offsetText,
282 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800283 const std::string &childName,
284 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700285
Andreas Huber2831d512016-08-15 09:33:47 -0700286 void emitJavaReaderWriterWithSuffix(
287 Formatter &out,
288 const std::string &parcelObj,
289 const std::string &argName,
290 bool isReader,
291 const std::string &suffix,
292 const std::string &extra) const;
293
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800294 void emitDumpWithMethod(
295 Formatter &out,
296 const std::string &streamName,
297 const std::string &methodName,
298 const std::string &name) const;
299
Timur Iskhakov35930c42017-08-28 18:49:54 -0700300 private:
301 bool mIsPostParseCompleted = false;
302
Andreas Huberc9410c72016-07-28 12:18:40 -0700303 DISALLOW_COPY_AND_ASSIGN(Type);
304};
305
Yifan Hongbf459bc2016-08-23 16:50:37 -0700306/* Base type for VectorType and RefType. */
307struct TemplatedType : public Type {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000308 void setElementType(const Reference<Type>& elementType);
309 Type* getElementType() const;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700310
Timur Iskhakov33431e62017-08-21 17:31:23 -0700311 bool isTemplatedType() const override;
312
313 virtual bool isCompatibleElementType(Type* elementType) const = 0;
314
315 std::vector<Reference<Type>> getReferences() const override;
316
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700317 virtual status_t validate() const override;
318
Timur Iskhakov33431e62017-08-21 17:31:23 -0700319 status_t emitVtsTypeDeclarations(Formatter& out) const override;
320 status_t emitVtsAttributeType(Formatter& out) const override;
321
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700322 protected:
Yifan Hongbf459bc2016-08-23 16:50:37 -0700323 TemplatedType();
Timur Iskhakov505316c2017-08-05 03:38:59 +0000324 Reference<Type> mElementType;
325
326 private:
Yifan Hongbf459bc2016-08-23 16:50:37 -0700327 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
328};
329
Andreas Huberc9410c72016-07-28 12:18:40 -0700330} // namespace android
331
332#endif // TYPE_H_
333