blob: 7f53339816cbec44cd4eacf00d71e584cac945a8 [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;
59
Andreas Huber881227d2016-08-02 14:20:21 -070060 enum StorageMode {
61 StorageMode_Stack,
62 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +010063 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -070064 };
Yifan Hong3b320f82016-11-01 15:15:54 -070065
Andreas Huber881227d2016-08-02 14:20:21 -070066 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070067 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070068 bool specifyNamespaces) const;
69
70 std::string decorateCppName(
71 const std::string &name,
72 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070073 bool specifyNamespaces) const;
74
75 /* gets all hidl-defined types used when this item is
76 * printed using getCppType or getJavaType. Examples:
77 *
78 * vec<vec<vec<IFoo>>>: IFoo is added to the set
79 * (the hypothetical type pair)
80 * pair<IFoo, IBar>: IFoo and IBar are added to the set
81 * int32_t: nothing is added to the set
82 * string: nothing is added to the set
83 * IFoo: IFoo is added to the set
84 */
85 virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070086
Yifan Hong3b320f82016-11-01 15:15:54 -070087 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070088
Yifan Hong3b320f82016-11-01 15:15:54 -070089 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070090
Yifan Hong3b320f82016-11-01 15:15:54 -070091 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070092
Yifan Hong4ed13472016-11-02 10:44:11 -070093 // For an array type, dimensionality information will be accumulated at the
94 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070095 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
96 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070097 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070098
Andreas Huber85eabdb2016-08-25 11:24:49 -070099 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -0700100 virtual std::string getJavaSuffix() const;
101
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700102 virtual std::string getVtsType() const;
103
Andreas Huber881227d2016-08-02 14:20:21 -0700104 enum ErrorMode {
105 ErrorMode_Ignore,
106 ErrorMode_Goto,
107 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700108 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700109 };
110 virtual void emitReaderWriter(
111 Formatter &out,
112 const std::string &name,
113 const std::string &parcelObj,
114 bool parcelObjIsPointer,
115 bool isReader,
116 ErrorMode mode) const;
117
118 virtual void emitReaderWriterEmbedded(
119 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700120 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700121 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700122 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700123 bool nameIsPointer,
124 const std::string &parcelObj,
125 bool parcelObjIsPointer,
126 bool isReader,
127 ErrorMode mode,
128 const std::string &parentName,
129 const std::string &offsetText) const;
130
Yifan Hongbf459bc2016-08-23 16:50:37 -0700131 virtual void emitResolveReferences(
132 Formatter &out,
133 const std::string &name,
134 bool nameIsPointer,
135 const std::string &parcelObj,
136 bool parcelObjIsPointer,
137 bool isReader,
138 ErrorMode mode) const;
139
140 virtual void emitResolveReferencesEmbedded(
141 Formatter &out,
142 size_t depth,
143 const std::string &name,
144 const std::string &sanitizedName,
145 bool nameIsPointer,
146 const std::string &parcelObj,
147 bool parcelObjIsPointer,
148 bool isReader,
149 ErrorMode mode,
150 const std::string &parentName,
151 const std::string &offsetText) const;
152
Yifan Hong00f47172016-09-30 14:40:45 -0700153 virtual bool useParentInEmitResolveReferencesEmbedded() const;
154
Yifan Hong244e82d2016-11-11 11:13:57 -0800155 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
156
Andreas Huber2831d512016-08-15 09:33:47 -0700157 virtual void emitJavaReaderWriter(
158 Formatter &out,
159 const std::string &parcelObj,
160 const std::string &argName,
161 bool isReader) const;
162
Andreas Huber85eabdb2016-08-25 11:24:49 -0700163 virtual void emitJavaFieldInitializer(
164 Formatter &out,
165 const std::string &fieldName) const;
166
167 virtual void emitJavaFieldReaderWriter(
168 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700169 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700170 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700171 const std::string &blobName,
172 const std::string &fieldName,
173 const std::string &offset,
174 bool isReader) const;
175
Andreas Huber881227d2016-08-02 14:20:21 -0700176 virtual status_t emitTypeDeclarations(Formatter &out) const;
177
Andreas Hubere3f769a2016-10-10 10:54:44 -0700178 // Emit any declarations pertaining to this type that have to be
179 // at global scope, i.e. enum class operators.
180 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
181
Yifan Hong244e82d2016-11-11 11:13:57 -0800182 // Emit any declarations pertaining to this type that have to be
183 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
184 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
185
Andreas Huber881227d2016-08-02 14:20:21 -0700186 virtual status_t emitTypeDefinitions(
187 Formatter &out, const std::string prefix) const;
188
Andreas Huber85eabdb2016-08-25 11:24:49 -0700189 virtual status_t emitJavaTypeDeclarations(
190 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700191
Andreas Huber881227d2016-08-02 14:20:21 -0700192 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700193 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700194 virtual bool resultNeedsDeref() const;
195
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700196 // Generates type declaration for vts proto file.
197 // TODO (b/30844146): make it a pure virtual method.
198 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700199 // Generates type declaration as attribute of method (return value or method
200 // argument) or attribute of compound type for vts proto file.
201 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700202
Andreas Huber70a59e12016-08-16 12:57:01 -0700203 // Returns true iff this type is supported through the Java backend.
204 virtual bool isJavaCompatible() const;
205
Andreas Huber85eabdb2016-08-25 11:24:49 -0700206 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
207
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700208 void setAnnotations(std::vector<Annotation *> *annotations);
209 const std::vector<Annotation *> &annotations() const;
210
Andreas Huber019d21d2016-10-03 12:59:47 -0700211 virtual void appendToExportedTypesVector(
212 std::vector<const Type *> *exportedTypes) const;
213
Andreas Huber1c507272016-10-05 14:33:21 -0700214 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700215
Andreas Huber881227d2016-08-02 14:20:21 -0700216protected:
217 void handleError(Formatter &out, ErrorMode mode) const;
218 void handleError2(Formatter &out, ErrorMode mode) const;
219
220 void emitReaderWriterEmbeddedForTypeName(
221 Formatter &out,
222 const std::string &name,
223 bool nameIsPointer,
224 const std::string &parcelObj,
225 bool parcelObjIsPointer,
226 bool isReader,
227 ErrorMode mode,
228 const std::string &parentName,
229 const std::string &offsetText,
230 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800231 const std::string &childName,
232 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700233
Andreas Huber2831d512016-08-15 09:33:47 -0700234 void emitJavaReaderWriterWithSuffix(
235 Formatter &out,
236 const std::string &parcelObj,
237 const std::string &argName,
238 bool isReader,
239 const std::string &suffix,
240 const std::string &extra) const;
241
Andreas Huberc9410c72016-07-28 12:18:40 -0700242private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700243 std::vector<Annotation *> *mAnnotations;
244
Andreas Huberc9410c72016-07-28 12:18:40 -0700245 DISALLOW_COPY_AND_ASSIGN(Type);
246};
247
Yifan Hongbf459bc2016-08-23 16:50:37 -0700248/* Base type for VectorType and RefType. */
249struct TemplatedType : public Type {
250 void setElementType(Type *elementType);
Yifan Hongabf73ee2016-12-05 18:47:00 -0800251 Type *getElementType() const;
252 bool isTemplatedType() const override;
Steven Moreland30bb6a82016-11-30 09:18:34 -0800253 virtual bool isCompatibleElementType(Type *elementType) const = 0;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700254protected:
255 TemplatedType();
256 Type *mElementType;
257private:
258 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
259};
260
Andreas Huberc9410c72016-07-28 12:18:40 -0700261} // namespace android
262
263#endif // TYPE_H_
264