blob: 80401a102a86ac34a1db75015722922c8ef32021 [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
Andreas Huber881227d2016-08-02 14:20:21 -070068 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070069 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070070 bool specifyNamespaces) const;
71
72 std::string decorateCppName(
73 const std::string &name,
74 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070075 bool specifyNamespaces) const;
76
77 /* gets all hidl-defined types used when this item is
78 * printed using getCppType or getJavaType. Examples:
79 *
80 * vec<vec<vec<IFoo>>>: IFoo is added to the set
81 * (the hypothetical type pair)
82 * pair<IFoo, IBar>: IFoo and IBar are added to the set
83 * int32_t: nothing is added to the set
84 * string: nothing is added to the set
85 * IFoo: IFoo is added to the set
86 */
87 virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070088
Yifan Hong3b320f82016-11-01 15:15:54 -070089 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070090
Yifan Hong3b320f82016-11-01 15:15:54 -070091 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070092
Yifan Hong3b320f82016-11-01 15:15:54 -070093 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070094
Yifan Hong4ed13472016-11-02 10:44:11 -070095 // For an array type, dimensionality information will be accumulated at the
96 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070097 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
98 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070099 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -0700100
Andreas Huber85eabdb2016-08-25 11:24:49 -0700101 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -0700102 virtual std::string getJavaSuffix() const;
103
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700104 virtual std::string getVtsType() const;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800105 virtual std::string getVtsValueName() const;
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700106
Andreas Huber881227d2016-08-02 14:20:21 -0700107 enum ErrorMode {
108 ErrorMode_Ignore,
109 ErrorMode_Goto,
110 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700111 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700112 };
113 virtual void emitReaderWriter(
114 Formatter &out,
115 const std::string &name,
116 const std::string &parcelObj,
117 bool parcelObjIsPointer,
118 bool isReader,
119 ErrorMode mode) const;
120
121 virtual void emitReaderWriterEmbedded(
122 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700123 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700124 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700125 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700126 bool nameIsPointer,
127 const std::string &parcelObj,
128 bool parcelObjIsPointer,
129 bool isReader,
130 ErrorMode mode,
131 const std::string &parentName,
132 const std::string &offsetText) const;
133
Yifan Hongbf459bc2016-08-23 16:50:37 -0700134 virtual void emitResolveReferences(
135 Formatter &out,
136 const std::string &name,
137 bool nameIsPointer,
138 const std::string &parcelObj,
139 bool parcelObjIsPointer,
140 bool isReader,
141 ErrorMode mode) const;
142
143 virtual void emitResolveReferencesEmbedded(
144 Formatter &out,
145 size_t depth,
146 const std::string &name,
147 const std::string &sanitizedName,
148 bool nameIsPointer,
149 const std::string &parcelObj,
150 bool parcelObjIsPointer,
151 bool isReader,
152 ErrorMode mode,
153 const std::string &parentName,
154 const std::string &offsetText) const;
155
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800156 virtual void emitDump(
157 Formatter &out,
158 const std::string &streamName,
159 const std::string &name) const;
160
Yifan Honge45b5302017-02-22 10:49:07 -0800161 virtual void emitJavaDump(
162 Formatter &out,
163 const std::string &streamName,
164 const std::string &name) const;
165
Yifan Hong00f47172016-09-30 14:40:45 -0700166 virtual bool useParentInEmitResolveReferencesEmbedded() const;
167
Yifan Hong244e82d2016-11-11 11:13:57 -0800168 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
169
Andreas Huber2831d512016-08-15 09:33:47 -0700170 virtual void emitJavaReaderWriter(
171 Formatter &out,
172 const std::string &parcelObj,
173 const std::string &argName,
174 bool isReader) const;
175
Andreas Huber85eabdb2016-08-25 11:24:49 -0700176 virtual void emitJavaFieldInitializer(
177 Formatter &out,
178 const std::string &fieldName) const;
179
180 virtual void emitJavaFieldReaderWriter(
181 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700182 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700183 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700184 const std::string &blobName,
185 const std::string &fieldName,
186 const std::string &offset,
187 bool isReader) const;
188
Andreas Huber881227d2016-08-02 14:20:21 -0700189 virtual status_t emitTypeDeclarations(Formatter &out) const;
190
Andreas Hubere3f769a2016-10-10 10:54:44 -0700191 // Emit any declarations pertaining to this type that have to be
192 // at global scope, i.e. enum class operators.
193 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
194
Yifan Hong244e82d2016-11-11 11:13:57 -0800195 // Emit any declarations pertaining to this type that have to be
196 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
197 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
198
Andreas Huber881227d2016-08-02 14:20:21 -0700199 virtual status_t emitTypeDefinitions(
200 Formatter &out, const std::string prefix) const;
201
Andreas Huber85eabdb2016-08-25 11:24:49 -0700202 virtual status_t emitJavaTypeDeclarations(
203 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700204
Andreas Huber881227d2016-08-02 14:20:21 -0700205 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700206 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700207 virtual bool resultNeedsDeref() const;
208
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700209 // Generates type declaration for vts proto file.
210 // TODO (b/30844146): make it a pure virtual method.
211 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700212 // Generates type declaration as attribute of method (return value or method
213 // argument) or attribute of compound type for vts proto file.
214 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700215
Andreas Huber70a59e12016-08-16 12:57:01 -0700216 // Returns true iff this type is supported through the Java backend.
217 virtual bool isJavaCompatible() const;
Andreas Huber2639fa32017-03-30 09:10:56 -0700218 virtual bool containsPointer() const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700219 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
220
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700221 void setAnnotations(std::vector<Annotation *> *annotations);
222 const std::vector<Annotation *> &annotations() const;
223
Andreas Huber019d21d2016-10-03 12:59:47 -0700224 virtual void appendToExportedTypesVector(
225 std::vector<const Type *> *exportedTypes) const;
226
Andreas Huber1c507272016-10-05 14:33:21 -0700227 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700228
Andreas Huber881227d2016-08-02 14:20:21 -0700229protected:
230 void handleError(Formatter &out, ErrorMode mode) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700231
232 void emitReaderWriterEmbeddedForTypeName(
233 Formatter &out,
234 const std::string &name,
235 bool nameIsPointer,
236 const std::string &parcelObj,
237 bool parcelObjIsPointer,
238 bool isReader,
239 ErrorMode mode,
240 const std::string &parentName,
241 const std::string &offsetText,
242 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800243 const std::string &childName,
244 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700245
Andreas Huber2831d512016-08-15 09:33:47 -0700246 void emitJavaReaderWriterWithSuffix(
247 Formatter &out,
248 const std::string &parcelObj,
249 const std::string &argName,
250 bool isReader,
251 const std::string &suffix,
252 const std::string &extra) const;
253
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800254 void emitDumpWithMethod(
255 Formatter &out,
256 const std::string &streamName,
257 const std::string &methodName,
258 const std::string &name) const;
259
Andreas Huberc9410c72016-07-28 12:18:40 -0700260private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700261 std::vector<Annotation *> *mAnnotations;
262
Andreas Huberc9410c72016-07-28 12:18:40 -0700263 DISALLOW_COPY_AND_ASSIGN(Type);
264};
265
Yifan Hongbf459bc2016-08-23 16:50:37 -0700266/* Base type for VectorType and RefType. */
267struct TemplatedType : public Type {
268 void setElementType(Type *elementType);
Yifan Hongabf73ee2016-12-05 18:47:00 -0800269 Type *getElementType() const;
270 bool isTemplatedType() const override;
Steven Moreland30bb6a82016-11-30 09:18:34 -0800271 virtual bool isCompatibleElementType(Type *elementType) const = 0;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800272 status_t emitVtsTypeDeclarations(Formatter &out) const override;
273 status_t emitVtsAttributeType(Formatter &out) const override;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700274protected:
275 TemplatedType();
276 Type *mElementType;
277private:
278 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
279};
280
Andreas Huberc9410c72016-07-28 12:18:40 -0700281} // namespace android
282
283#endif // TYPE_H_
284