blob: 26eeab0b3d35e171893ebc7c2c89985f4f2152c2 [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 <string>
23#include <utils/Errors.h>
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070024#include <vector>
Steven Moreland979e0992016-09-07 09:18:08 -070025#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
27namespace android {
28
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070029struct Annotation;
Andreas Huberc9410c72016-07-28 12:18:40 -070030struct Formatter;
Andreas Huber737080b2016-08-02 15:38:04 -070031struct ScalarType;
Steven Moreland979e0992016-09-07 09:18:08 -070032struct FQName;
Andreas Huberc9410c72016-07-28 12:18:40 -070033
34struct Type {
35 Type();
36 virtual ~Type();
37
Andreas Huber709b62d2016-09-19 11:21:18 -070038 virtual bool isArray() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010039 virtual bool isBinder() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080040 virtual bool isBitField() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010041 virtual bool isCompoundType() const;
42 virtual bool isEnum() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080043 virtual bool isHandle() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010044 virtual bool isInterface() const;
45 virtual bool isNamedType() const;
46 virtual bool isPointer() const;
47 virtual bool isScope() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080048 virtual bool isScalar() const;
49 virtual bool isString() const;
50 virtual bool isTemplatedType() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010051 virtual bool isTypeDef() const;
Andreas Huber709b62d2016-09-19 11:21:18 -070052 virtual bool isVector() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070053
Andreas Huber737080b2016-08-02 15:38:04 -070054 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Steven Moreland30bb6a82016-11-30 09:18:34 -080056 virtual std::string typeName() const;
57
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070058 bool isValidEnumStorageType() const;
Steven Moreland9df52442016-12-12 08:51:14 -080059 virtual bool isElidableType() const;
Yifan Hongc6752dc2016-12-20 14:00:14 -080060 virtual bool canCheckEquality() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070061
Andreas Huber881227d2016-08-02 14:20:21 -070062 enum StorageMode {
63 StorageMode_Stack,
64 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +010065 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -070066 };
Yifan Hong3b320f82016-11-01 15:15:54 -070067
Steven Morelande30ee9b2017-05-09 13:31:01 -070068 // specifyNamespaces: whether to specify namespaces for built-in types
Andreas Huber881227d2016-08-02 14:20:21 -070069 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070070 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070071 bool specifyNamespaces) const;
72
73 std::string decorateCppName(
74 const std::string &name,
75 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070076 bool specifyNamespaces) const;
77
Yifan Hong3b320f82016-11-01 15:15:54 -070078 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070079
Yifan Hong3b320f82016-11-01 15:15:54 -070080 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Yifan Hong3b320f82016-11-01 15:15:54 -070082 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070083
Yifan Hong4ed13472016-11-02 10:44:11 -070084 // For an array type, dimensionality information will be accumulated at the
85 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070086 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
87 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070088 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070089
Andreas Huber85eabdb2016-08-25 11:24:49 -070090 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070091 virtual std::string getJavaSuffix() const;
92
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070093 virtual std::string getVtsType() const;
Zhuoyao Zhange9667842017-01-19 12:35:32 -080094 virtual std::string getVtsValueName() const;
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070095
Andreas Huber881227d2016-08-02 14:20:21 -070096 enum ErrorMode {
97 ErrorMode_Ignore,
98 ErrorMode_Goto,
99 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700100 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700101 };
102 virtual void emitReaderWriter(
103 Formatter &out,
104 const std::string &name,
105 const std::string &parcelObj,
106 bool parcelObjIsPointer,
107 bool isReader,
108 ErrorMode mode) const;
109
110 virtual void emitReaderWriterEmbedded(
111 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700112 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700113 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700114 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700115 bool nameIsPointer,
116 const std::string &parcelObj,
117 bool parcelObjIsPointer,
118 bool isReader,
119 ErrorMode mode,
120 const std::string &parentName,
121 const std::string &offsetText) const;
122
Yifan Hongbf459bc2016-08-23 16:50:37 -0700123 virtual void emitResolveReferences(
124 Formatter &out,
125 const std::string &name,
126 bool nameIsPointer,
127 const std::string &parcelObj,
128 bool parcelObjIsPointer,
129 bool isReader,
130 ErrorMode mode) const;
131
132 virtual void emitResolveReferencesEmbedded(
133 Formatter &out,
134 size_t depth,
135 const std::string &name,
136 const std::string &sanitizedName,
137 bool nameIsPointer,
138 const std::string &parcelObj,
139 bool parcelObjIsPointer,
140 bool isReader,
141 ErrorMode mode,
142 const std::string &parentName,
143 const std::string &offsetText) const;
144
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800145 virtual void emitDump(
146 Formatter &out,
147 const std::string &streamName,
148 const std::string &name) const;
149
Yifan Honge45b5302017-02-22 10:49:07 -0800150 virtual void emitJavaDump(
151 Formatter &out,
152 const std::string &streamName,
153 const std::string &name) const;
154
Yifan Hong00f47172016-09-30 14:40:45 -0700155 virtual bool useParentInEmitResolveReferencesEmbedded() const;
156
Yifan Hong244e82d2016-11-11 11:13:57 -0800157 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
158
Andreas Huber2831d512016-08-15 09:33:47 -0700159 virtual void emitJavaReaderWriter(
160 Formatter &out,
161 const std::string &parcelObj,
162 const std::string &argName,
163 bool isReader) const;
164
Andreas Huber85eabdb2016-08-25 11:24:49 -0700165 virtual void emitJavaFieldInitializer(
166 Formatter &out,
167 const std::string &fieldName) const;
168
169 virtual void emitJavaFieldReaderWriter(
170 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700171 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700172 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700173 const std::string &blobName,
174 const std::string &fieldName,
175 const std::string &offset,
176 bool isReader) const;
177
Andreas Huber881227d2016-08-02 14:20:21 -0700178 virtual status_t emitTypeDeclarations(Formatter &out) const;
179
Andreas Hubere3f769a2016-10-10 10:54:44 -0700180 // Emit any declarations pertaining to this type that have to be
181 // at global scope, i.e. enum class operators.
182 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
183
Yifan Hong244e82d2016-11-11 11:13:57 -0800184 // Emit any declarations pertaining to this type that have to be
185 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
186 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
187
Andreas Huber881227d2016-08-02 14:20:21 -0700188 virtual status_t emitTypeDefinitions(
189 Formatter &out, const std::string prefix) const;
190
Andreas Huber85eabdb2016-08-25 11:24:49 -0700191 virtual status_t emitJavaTypeDeclarations(
192 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700193
Andreas Huber881227d2016-08-02 14:20:21 -0700194 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700195 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700196 virtual bool resultNeedsDeref() const;
197
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700198 // Generates type declaration for vts proto file.
199 // TODO (b/30844146): make it a pure virtual method.
200 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700201 // Generates type declaration as attribute of method (return value or method
202 // argument) or attribute of compound type for vts proto file.
203 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700204
Andreas Huber70a59e12016-08-16 12:57:01 -0700205 // Returns true iff this type is supported through the Java backend.
206 virtual bool isJavaCompatible() const;
Andreas Huber60d3b222017-03-30 09:10:56 -0700207 virtual bool containsPointer() const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700208 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
209
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700210 void setAnnotations(std::vector<Annotation *> *annotations);
211 const std::vector<Annotation *> &annotations() const;
212
Andreas Huber019d21d2016-10-03 12:59:47 -0700213 virtual void appendToExportedTypesVector(
214 std::vector<const Type *> *exportedTypes) const;
215
Andreas Huber1c507272016-10-05 14:33:21 -0700216 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700217
Andreas Huber881227d2016-08-02 14:20:21 -0700218protected:
219 void handleError(Formatter &out, ErrorMode mode) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700220
221 void emitReaderWriterEmbeddedForTypeName(
222 Formatter &out,
223 const std::string &name,
224 bool nameIsPointer,
225 const std::string &parcelObj,
226 bool parcelObjIsPointer,
227 bool isReader,
228 ErrorMode mode,
229 const std::string &parentName,
230 const std::string &offsetText,
231 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800232 const std::string &childName,
233 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700234
Andreas Huber2831d512016-08-15 09:33:47 -0700235 void emitJavaReaderWriterWithSuffix(
236 Formatter &out,
237 const std::string &parcelObj,
238 const std::string &argName,
239 bool isReader,
240 const std::string &suffix,
241 const std::string &extra) const;
242
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800243 void emitDumpWithMethod(
244 Formatter &out,
245 const std::string &streamName,
246 const std::string &methodName,
247 const std::string &name) const;
248
Andreas Huberc9410c72016-07-28 12:18:40 -0700249private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700250 std::vector<Annotation *> *mAnnotations;
251
Andreas Huberc9410c72016-07-28 12:18:40 -0700252 DISALLOW_COPY_AND_ASSIGN(Type);
253};
254
Yifan Hongbf459bc2016-08-23 16:50:37 -0700255/* Base type for VectorType and RefType. */
256struct TemplatedType : public Type {
257 void setElementType(Type *elementType);
Yifan Hongabf73ee2016-12-05 18:47:00 -0800258 Type *getElementType() const;
259 bool isTemplatedType() const override;
Steven Moreland30bb6a82016-11-30 09:18:34 -0800260 virtual bool isCompatibleElementType(Type *elementType) const = 0;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800261 status_t emitVtsTypeDeclarations(Formatter &out) const override;
262 status_t emitVtsAttributeType(Formatter &out) const override;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700263protected:
264 TemplatedType();
265 Type *mElementType;
266private:
267 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
268};
269
Andreas Huberc9410c72016-07-28 12:18:40 -0700270} // namespace android
271
272#endif // TYPE_H_
273