blob: 35374c734d3ffa96f1958507cb16feb92f8fcd9f [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 Huber5345ec22016-07-29 13:33:27 -070038 virtual bool isScope() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070039 virtual bool isInterface() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070040 virtual bool isEnum() const;
41 virtual bool isTypeDef() const;
Andreas Huber295ad302016-08-16 11:35:00 -070042 virtual bool isBinder() const;
Andreas Huber39fa7182016-08-19 14:27:33 -070043 virtual bool isNamedType() const;
Andreas Huberf630bc82016-09-09 14:52:25 -070044 virtual bool isCompoundType() const;
Andreas Huber709b62d2016-09-19 11:21:18 -070045 virtual bool isArray() const;
46 virtual bool isVector() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070047
Andreas Huber737080b2016-08-02 15:38:04 -070048 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070049
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070050 bool isValidEnumStorageType() const;
51
Andreas Huber881227d2016-08-02 14:20:21 -070052 enum StorageMode {
53 StorageMode_Stack,
54 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +010055 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -070056 };
Yifan Hong3b320f82016-11-01 15:15:54 -070057
Andreas Huber881227d2016-08-02 14:20:21 -070058 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070059 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070060 bool specifyNamespaces) const;
61
62 std::string decorateCppName(
63 const std::string &name,
64 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070065 bool specifyNamespaces) const;
66
67 /* gets all hidl-defined types used when this item is
68 * printed using getCppType or getJavaType. Examples:
69 *
70 * vec<vec<vec<IFoo>>>: IFoo is added to the set
71 * (the hypothetical type pair)
72 * pair<IFoo, IBar>: IFoo and IBar are added to the set
73 * int32_t: nothing is added to the set
74 * string: nothing is added to the set
75 * IFoo: IFoo is added to the set
76 */
77 virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070078
Yifan Hong3b320f82016-11-01 15:15:54 -070079 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070080
Yifan Hong3b320f82016-11-01 15:15:54 -070081 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070082
Yifan Hong3b320f82016-11-01 15:15:54 -070083 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070084
Yifan Hong4ed13472016-11-02 10:44:11 -070085 // For an array type, dimensionality information will be accumulated at the
86 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070087 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
88 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070089 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070090
Andreas Huber85eabdb2016-08-25 11:24:49 -070091 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070092 virtual std::string getJavaSuffix() const;
93
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070094 virtual std::string getVtsType() const;
95
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 Hong00f47172016-09-30 14:40:45 -0700145 virtual bool useParentInEmitResolveReferencesEmbedded() const;
146
Yifan Hong244e82d2016-11-11 11:13:57 -0800147 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
148
Andreas Huber2831d512016-08-15 09:33:47 -0700149 virtual void emitJavaReaderWriter(
150 Formatter &out,
151 const std::string &parcelObj,
152 const std::string &argName,
153 bool isReader) const;
154
Andreas Huber85eabdb2016-08-25 11:24:49 -0700155 virtual void emitJavaFieldInitializer(
156 Formatter &out,
157 const std::string &fieldName) const;
158
159 virtual void emitJavaFieldReaderWriter(
160 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700161 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700162 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700163 const std::string &blobName,
164 const std::string &fieldName,
165 const std::string &offset,
166 bool isReader) const;
167
Andreas Huber881227d2016-08-02 14:20:21 -0700168 virtual status_t emitTypeDeclarations(Formatter &out) const;
169
Andreas Hubere3f769a2016-10-10 10:54:44 -0700170 // Emit any declarations pertaining to this type that have to be
171 // at global scope, i.e. enum class operators.
172 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
173
Yifan Hong244e82d2016-11-11 11:13:57 -0800174 // Emit any declarations pertaining to this type that have to be
175 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
176 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
177
Andreas Huber881227d2016-08-02 14:20:21 -0700178 virtual status_t emitTypeDefinitions(
179 Formatter &out, const std::string prefix) const;
180
Andreas Huber85eabdb2016-08-25 11:24:49 -0700181 virtual status_t emitJavaTypeDeclarations(
182 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700183
Andreas Huber881227d2016-08-02 14:20:21 -0700184 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700185 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700186 virtual bool resultNeedsDeref() const;
187
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700188 // Generates type declaration for vts proto file.
189 // TODO (b/30844146): make it a pure virtual method.
190 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700191 // Generates type declaration as attribute of method (return value or method
192 // argument) or attribute of compound type for vts proto file.
193 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700194
Andreas Huber70a59e12016-08-16 12:57:01 -0700195 // Returns true iff this type is supported through the Java backend.
196 virtual bool isJavaCompatible() const;
197
Andreas Huber85eabdb2016-08-25 11:24:49 -0700198 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
199
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700200 void setAnnotations(std::vector<Annotation *> *annotations);
201 const std::vector<Annotation *> &annotations() const;
202
Andreas Huber019d21d2016-10-03 12:59:47 -0700203 virtual void appendToExportedTypesVector(
204 std::vector<const Type *> *exportedTypes) const;
205
Andreas Huber1c507272016-10-05 14:33:21 -0700206 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700207
Andreas Huber881227d2016-08-02 14:20:21 -0700208protected:
209 void handleError(Formatter &out, ErrorMode mode) const;
210 void handleError2(Formatter &out, ErrorMode mode) const;
211
212 void emitReaderWriterEmbeddedForTypeName(
213 Formatter &out,
214 const std::string &name,
215 bool nameIsPointer,
216 const std::string &parcelObj,
217 bool parcelObjIsPointer,
218 bool isReader,
219 ErrorMode mode,
220 const std::string &parentName,
221 const std::string &offsetText,
222 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800223 const std::string &childName,
224 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700225
Andreas Huber2831d512016-08-15 09:33:47 -0700226 void emitJavaReaderWriterWithSuffix(
227 Formatter &out,
228 const std::string &parcelObj,
229 const std::string &argName,
230 bool isReader,
231 const std::string &suffix,
232 const std::string &extra) const;
233
Andreas Huberc9410c72016-07-28 12:18:40 -0700234private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700235 std::vector<Annotation *> *mAnnotations;
236
Andreas Huberc9410c72016-07-28 12:18:40 -0700237 DISALLOW_COPY_AND_ASSIGN(Type);
238};
239
Yifan Hongbf459bc2016-08-23 16:50:37 -0700240/* Base type for VectorType and RefType. */
241struct TemplatedType : public Type {
242 void setElementType(Type *elementType);
243protected:
244 TemplatedType();
245 Type *mElementType;
246private:
247 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
248};
249
Andreas Huberc9410c72016-07-28 12:18:40 -0700250} // namespace android
251
252#endif // TYPE_H_
253