blob: e137043d50bdb14872bf4f9d77fa5774110d7ee5 [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,
56 StorageMode_Compound // when stored in a compound type
Andreas Huber881227d2016-08-02 14:20:21 -070057 };
Yifan Hong3b320f82016-11-01 15:15:54 -070058
Andreas Huber881227d2016-08-02 14:20:21 -070059 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070060 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070061 bool specifyNamespaces) const;
62
63 std::string decorateCppName(
64 const std::string &name,
65 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070066 bool specifyNamespaces) const;
67
68 /* gets all hidl-defined types used when this item is
69 * printed using getCppType or getJavaType. Examples:
70 *
71 * vec<vec<vec<IFoo>>>: IFoo is added to the set
72 * (the hypothetical type pair)
73 * pair<IFoo, IBar>: IFoo and IBar are added to the set
74 * int32_t: nothing is added to the set
75 * string: nothing is added to the set
76 * IFoo: IFoo is added to the set
77 */
78 virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070079
Yifan Hong3b320f82016-11-01 15:15:54 -070080 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Yifan Hong3b320f82016-11-01 15:15:54 -070082 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070083
Yifan Hong3b320f82016-11-01 15:15:54 -070084 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070085
Martijn Coenenac587892016-11-17 15:14:19 +010086 std::string getCppCompoundType(bool specifyNamespaces = true) const;
87
Yifan Hong4ed13472016-11-02 10:44:11 -070088 // For an array type, dimensionality information will be accumulated at the
89 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070090 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
91 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070092 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070093
Andreas Huber85eabdb2016-08-25 11:24:49 -070094 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070095 virtual std::string getJavaSuffix() const;
96
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070097 virtual std::string getVtsType() const;
98
Andreas Huber881227d2016-08-02 14:20:21 -070099 enum ErrorMode {
100 ErrorMode_Ignore,
101 ErrorMode_Goto,
102 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700103 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700104 };
105 virtual void emitReaderWriter(
106 Formatter &out,
107 const std::string &name,
108 const std::string &parcelObj,
109 bool parcelObjIsPointer,
110 bool isReader,
111 ErrorMode mode) const;
112
113 virtual void emitReaderWriterEmbedded(
114 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700115 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700116 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700117 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700118 bool nameIsPointer,
119 const std::string &parcelObj,
120 bool parcelObjIsPointer,
121 bool isReader,
122 ErrorMode mode,
123 const std::string &parentName,
124 const std::string &offsetText) const;
125
Yifan Hongbf459bc2016-08-23 16:50:37 -0700126 virtual void emitResolveReferences(
127 Formatter &out,
128 const std::string &name,
129 bool nameIsPointer,
130 const std::string &parcelObj,
131 bool parcelObjIsPointer,
132 bool isReader,
133 ErrorMode mode) const;
134
135 virtual void emitResolveReferencesEmbedded(
136 Formatter &out,
137 size_t depth,
138 const std::string &name,
139 const std::string &sanitizedName,
140 bool nameIsPointer,
141 const std::string &parcelObj,
142 bool parcelObjIsPointer,
143 bool isReader,
144 ErrorMode mode,
145 const std::string &parentName,
146 const std::string &offsetText) const;
147
Yifan Hong00f47172016-09-30 14:40:45 -0700148 virtual bool useParentInEmitResolveReferencesEmbedded() const;
149
Yifan Hong244e82d2016-11-11 11:13:57 -0800150 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
151
Andreas Huber2831d512016-08-15 09:33:47 -0700152 virtual void emitJavaReaderWriter(
153 Formatter &out,
154 const std::string &parcelObj,
155 const std::string &argName,
156 bool isReader) const;
157
Andreas Huber85eabdb2016-08-25 11:24:49 -0700158 virtual void emitJavaFieldInitializer(
159 Formatter &out,
160 const std::string &fieldName) const;
161
162 virtual void emitJavaFieldReaderWriter(
163 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700164 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700165 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700166 const std::string &blobName,
167 const std::string &fieldName,
168 const std::string &offset,
169 bool isReader) const;
170
Andreas Huber881227d2016-08-02 14:20:21 -0700171 virtual status_t emitTypeDeclarations(Formatter &out) const;
172
Andreas Hubere3f769a2016-10-10 10:54:44 -0700173 // Emit any declarations pertaining to this type that have to be
174 // at global scope, i.e. enum class operators.
175 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
176
Yifan Hong244e82d2016-11-11 11:13:57 -0800177 // Emit any declarations pertaining to this type that have to be
178 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
179 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
180
Andreas Huber881227d2016-08-02 14:20:21 -0700181 virtual status_t emitTypeDefinitions(
182 Formatter &out, const std::string prefix) const;
183
Andreas Huber85eabdb2016-08-25 11:24:49 -0700184 virtual status_t emitJavaTypeDeclarations(
185 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700186
Andreas Huber881227d2016-08-02 14:20:21 -0700187 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700188 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700189 virtual bool resultNeedsDeref() const;
190
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700191 // Generates type declaration for vts proto file.
192 // TODO (b/30844146): make it a pure virtual method.
193 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700194 // Generates type declaration as attribute of method (return value or method
195 // argument) or attribute of compound type for vts proto file.
196 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700197
Andreas Huber70a59e12016-08-16 12:57:01 -0700198 // Returns true iff this type is supported through the Java backend.
199 virtual bool isJavaCompatible() const;
200
Andreas Huber85eabdb2016-08-25 11:24:49 -0700201 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
202
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700203 void setAnnotations(std::vector<Annotation *> *annotations);
204 const std::vector<Annotation *> &annotations() const;
205
Andreas Huber019d21d2016-10-03 12:59:47 -0700206 virtual void appendToExportedTypesVector(
207 std::vector<const Type *> *exportedTypes) const;
208
Andreas Huber1c507272016-10-05 14:33:21 -0700209 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700210
Andreas Huber881227d2016-08-02 14:20:21 -0700211protected:
212 void handleError(Formatter &out, ErrorMode mode) const;
213 void handleError2(Formatter &out, ErrorMode mode) const;
214
215 void emitReaderWriterEmbeddedForTypeName(
216 Formatter &out,
217 const std::string &name,
218 bool nameIsPointer,
219 const std::string &parcelObj,
220 bool parcelObjIsPointer,
221 bool isReader,
222 ErrorMode mode,
223 const std::string &parentName,
224 const std::string &offsetText,
225 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800226 const std::string &childName,
227 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700228
Andreas Huber2831d512016-08-15 09:33:47 -0700229 void emitJavaReaderWriterWithSuffix(
230 Formatter &out,
231 const std::string &parcelObj,
232 const std::string &argName,
233 bool isReader,
234 const std::string &suffix,
235 const std::string &extra) const;
236
Andreas Huberc9410c72016-07-28 12:18:40 -0700237private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700238 std::vector<Annotation *> *mAnnotations;
239
Andreas Huberc9410c72016-07-28 12:18:40 -0700240 DISALLOW_COPY_AND_ASSIGN(Type);
241};
242
Yifan Hongbf459bc2016-08-23 16:50:37 -0700243/* Base type for VectorType and RefType. */
244struct TemplatedType : public Type {
245 void setElementType(Type *elementType);
246protected:
247 TemplatedType();
248 Type *mElementType;
249private:
250 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
251};
252
Andreas Huberc9410c72016-07-28 12:18:40 -0700253} // namespace android
254
255#endif // TYPE_H_
256