blob: b65ce4b8157b3690ac37fd5ba7c97ed5d3514c3f [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,
55 StorageMode_Result
56 };
57 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070058 StorageMode mode,
59 std::string *extra,
60 bool specifyNamespaces) const;
61
62 /* gets all hidl-defined types used when this item is
63 * printed using getCppType or getJavaType. Examples:
64 *
65 * vec<vec<vec<IFoo>>>: IFoo is added to the set
66 * (the hypothetical type pair)
67 * pair<IFoo, IBar>: IFoo and IBar are added to the set
68 * int32_t: nothing is added to the set
69 * string: nothing is added to the set
70 * IFoo: IFoo is added to the set
71 */
72 virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070073
74 // Convenience, gets StorageMode_Stack type.
Andreas Huber4c865b72016-09-14 15:26:27 -070075 std::string getCppType(
76 std::string *extra, bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070077
Andreas Huber4c865b72016-09-14 15:26:27 -070078 std::string getCppResultType(
79 std::string *extra, bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070080
Andreas Huber4c865b72016-09-14 15:26:27 -070081 std::string getCppArgumentType(
82 std::string *extra, bool specifyNamespaces = true) const;
83
84 // For an array type, "extra" accumulates dimensionality information,
85 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
86 // otherwise (and by default), they are omitted, i.e. [][].
87 virtual std::string getJavaType(
88 std::string *extra, bool forInitializer = false) const;
89
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;
94
Andreas Huber881227d2016-08-02 14:20:21 -070095 enum ErrorMode {
96 ErrorMode_Ignore,
97 ErrorMode_Goto,
98 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -070099 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700100 };
101 virtual void emitReaderWriter(
102 Formatter &out,
103 const std::string &name,
104 const std::string &parcelObj,
105 bool parcelObjIsPointer,
106 bool isReader,
107 ErrorMode mode) const;
108
109 virtual void emitReaderWriterEmbedded(
110 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700111 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700112 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700113 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700114 bool nameIsPointer,
115 const std::string &parcelObj,
116 bool parcelObjIsPointer,
117 bool isReader,
118 ErrorMode mode,
119 const std::string &parentName,
120 const std::string &offsetText) const;
121
Yifan Hongbf459bc2016-08-23 16:50:37 -0700122 virtual void emitResolveReferences(
123 Formatter &out,
124 const std::string &name,
125 bool nameIsPointer,
126 const std::string &parcelObj,
127 bool parcelObjIsPointer,
128 bool isReader,
129 ErrorMode mode) const;
130
131 virtual void emitResolveReferencesEmbedded(
132 Formatter &out,
133 size_t depth,
134 const std::string &name,
135 const std::string &sanitizedName,
136 bool nameIsPointer,
137 const std::string &parcelObj,
138 bool parcelObjIsPointer,
139 bool isReader,
140 ErrorMode mode,
141 const std::string &parentName,
142 const std::string &offsetText) const;
143
Yifan Hong00f47172016-09-30 14:40:45 -0700144 virtual bool useParentInEmitResolveReferencesEmbedded() const;
145
Andreas Huber2831d512016-08-15 09:33:47 -0700146 virtual void emitJavaReaderWriter(
147 Formatter &out,
148 const std::string &parcelObj,
149 const std::string &argName,
150 bool isReader) const;
151
Andreas Huber85eabdb2016-08-25 11:24:49 -0700152 virtual void emitJavaFieldInitializer(
153 Formatter &out,
154 const std::string &fieldName) const;
155
156 virtual void emitJavaFieldReaderWriter(
157 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700158 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700159 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700160 const std::string &blobName,
161 const std::string &fieldName,
162 const std::string &offset,
163 bool isReader) const;
164
Andreas Huber881227d2016-08-02 14:20:21 -0700165 virtual status_t emitTypeDeclarations(Formatter &out) const;
166
Andreas Hubere3f769a2016-10-10 10:54:44 -0700167 // Emit any declarations pertaining to this type that have to be
168 // at global scope, i.e. enum class operators.
169 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
170
Andreas Huber881227d2016-08-02 14:20:21 -0700171 virtual status_t emitTypeDefinitions(
172 Formatter &out, const std::string prefix) const;
173
Andreas Huber85eabdb2016-08-25 11:24:49 -0700174 virtual status_t emitJavaTypeDeclarations(
175 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700176
Andreas Huber881227d2016-08-02 14:20:21 -0700177 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700178 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700179 virtual bool resultNeedsDeref() const;
180
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700181 // Generates type declaration for vts proto file.
182 // TODO (b/30844146): make it a pure virtual method.
183 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700184 // Generates type declaration as attribute of method (return value or method
185 // argument) or attribute of compound type for vts proto file.
186 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700187
Andreas Huber70a59e12016-08-16 12:57:01 -0700188 // Returns true iff this type is supported through the Java backend.
189 virtual bool isJavaCompatible() const;
190
Andreas Huber85eabdb2016-08-25 11:24:49 -0700191 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
192
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700193 void setAnnotations(std::vector<Annotation *> *annotations);
194 const std::vector<Annotation *> &annotations() const;
195
Andreas Huber019d21d2016-10-03 12:59:47 -0700196 virtual void appendToExportedTypesVector(
197 std::vector<const Type *> *exportedTypes) const;
198
Andreas Huber1c507272016-10-05 14:33:21 -0700199 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700200
Andreas Huber881227d2016-08-02 14:20:21 -0700201protected:
202 void handleError(Formatter &out, ErrorMode mode) const;
203 void handleError2(Formatter &out, ErrorMode mode) const;
204
205 void emitReaderWriterEmbeddedForTypeName(
206 Formatter &out,
207 const std::string &name,
208 bool nameIsPointer,
209 const std::string &parcelObj,
210 bool parcelObjIsPointer,
211 bool isReader,
212 ErrorMode mode,
213 const std::string &parentName,
214 const std::string &offsetText,
215 const std::string &typeName,
216 const std::string &childName) const;
217
Andreas Huber2831d512016-08-15 09:33:47 -0700218 void emitJavaReaderWriterWithSuffix(
219 Formatter &out,
220 const std::string &parcelObj,
221 const std::string &argName,
222 bool isReader,
223 const std::string &suffix,
224 const std::string &extra) const;
225
Andreas Huberc9410c72016-07-28 12:18:40 -0700226private:
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700227 std::vector<Annotation *> *mAnnotations;
228
Andreas Huberc9410c72016-07-28 12:18:40 -0700229 DISALLOW_COPY_AND_ASSIGN(Type);
230};
231
Yifan Hongbf459bc2016-08-23 16:50:37 -0700232/* Base type for VectorType and RefType. */
233struct TemplatedType : public Type {
234 void setElementType(Type *elementType);
235protected:
236 TemplatedType();
237 Type *mElementType;
238private:
239 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
240};
241
Andreas Huberc9410c72016-07-28 12:18:40 -0700242} // namespace android
243
244#endif // TYPE_H_
245