1 /*
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 
17 #ifndef TYPE_H_
18 
19 #define TYPE_H_
20 
21 #include <android-base/macros.h>
22 #include <string>
23 #include <utils/Errors.h>
24 #include <vector>
25 #include <set>
26 
27 namespace android {
28 
29 struct Annotation;
30 struct Formatter;
31 struct ScalarType;
32 struct FQName;
33 
34 struct Type {
35     Type();
36     virtual ~Type();
37 
38     virtual bool isArray() const;
39     virtual bool isBinder() const;
40     virtual bool isBitField() const;
41     virtual bool isCompoundType() const;
42     virtual bool isEnum() const;
43     virtual bool isHandle() const;
44     virtual bool isInterface() const;
45     virtual bool isNamedType() const;
46     virtual bool isMemory() const;
47     virtual bool isPointer() const;
48     virtual bool isScope() const;
49     virtual bool isScalar() const;
50     virtual bool isString() const;
51     virtual bool isTemplatedType() const;
52     virtual bool isTypeDef() const;
53     virtual bool isVector() const;
54 
55     virtual const ScalarType *resolveToScalarType() const;
56 
57     virtual std::string typeName() const;
58 
59     bool isValidEnumStorageType() const;
60     virtual bool isElidableType() const;
61     virtual bool canCheckEquality() const;
62 
63     enum StorageMode {
64         StorageMode_Stack,
65         StorageMode_Argument,
66         StorageMode_Result,
67     };
68 
69     virtual std::string getCppType(
70             StorageMode mode,
71             bool specifyNamespaces) const;
72 
73     std::string decorateCppName(
74             const std::string &name,
75             StorageMode mode,
76             bool specifyNamespaces) const;
77 
78     /* gets all hidl-defined types used when this item is
79      * printed using getCppType or getJavaType. Examples:
80      *
81      * vec<vec<vec<IFoo>>>: IFoo is added to the set
82      * (the hypothetical type pair)
83      * pair<IFoo, IBar>: IFoo and IBar are added to the set
84      * int32_t: nothing is added to the set
85      * string: nothing is added to the set
86      * IFoo: IFoo is added to the set
87      */
88     virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
89 
90     std::string getCppStackType(bool specifyNamespaces = true) const;
91 
92     std::string getCppResultType(bool specifyNamespaces = true) const;
93 
94     std::string getCppArgumentType(bool specifyNamespaces = true) const;
95 
96     // For an array type, dimensionality information will be accumulated at the
97     // end of the returned string.
98     // if forInitializer == true, actual dimensions are included, i.e. [3][5],
99     // otherwise (and by default), they are omitted, i.e. [][].
100     virtual std::string getJavaType(bool forInitializer = false) const;
101 
102     virtual std::string getJavaWrapperType() const;
103     virtual std::string getJavaSuffix() const;
104 
105     virtual std::string getVtsType() const;
106     virtual std::string getVtsValueName() const;
107 
108     enum ErrorMode {
109         ErrorMode_Ignore,
110         ErrorMode_Goto,
111         ErrorMode_Break,
112         ErrorMode_Return,
113     };
114     virtual void emitReaderWriter(
115             Formatter &out,
116             const std::string &name,
117             const std::string &parcelObj,
118             bool parcelObjIsPointer,
119             bool isReader,
120             ErrorMode mode) const;
121 
122     virtual void emitReaderWriterEmbedded(
123             Formatter &out,
124             size_t depth,
125             const std::string &name,
126             const std::string &sanitizedName,
127             bool nameIsPointer,
128             const std::string &parcelObj,
129             bool parcelObjIsPointer,
130             bool isReader,
131             ErrorMode mode,
132             const std::string &parentName,
133             const std::string &offsetText) const;
134 
135     virtual void emitResolveReferences(
136             Formatter &out,
137             const std::string &name,
138             bool nameIsPointer,
139             const std::string &parcelObj,
140             bool parcelObjIsPointer,
141             bool isReader,
142             ErrorMode mode) const;
143 
144     virtual void emitResolveReferencesEmbedded(
145             Formatter &out,
146             size_t depth,
147             const std::string &name,
148             const std::string &sanitizedName,
149             bool nameIsPointer,
150             const std::string &parcelObj,
151             bool parcelObjIsPointer,
152             bool isReader,
153             ErrorMode mode,
154             const std::string &parentName,
155             const std::string &offsetText) const;
156 
157     virtual void emitDump(
158             Formatter &out,
159             const std::string &streamName,
160             const std::string &name) const;
161 
162     virtual void emitJavaDump(
163             Formatter &out,
164             const std::string &streamName,
165             const std::string &name) const;
166 
167     virtual bool useParentInEmitResolveReferencesEmbedded() const;
168 
169     virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
170 
171     virtual void emitJavaReaderWriter(
172             Formatter &out,
173             const std::string &parcelObj,
174             const std::string &argName,
175             bool isReader) const;
176 
177     virtual void emitJavaFieldInitializer(
178             Formatter &out,
179             const std::string &fieldName) const;
180 
181     virtual void emitJavaFieldReaderWriter(
182             Formatter &out,
183             size_t depth,
184             const std::string &parcelName,
185             const std::string &blobName,
186             const std::string &fieldName,
187             const std::string &offset,
188             bool isReader) const;
189 
190     virtual status_t emitTypeDeclarations(Formatter &out) const;
191 
192     // Emit any declarations pertaining to this type that have to be
193     // at global scope, i.e. enum class operators.
194     virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
195 
196     // Emit any declarations pertaining to this type that have to be
197     // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
198     virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
199 
200     virtual status_t emitTypeDefinitions(
201             Formatter &out, const std::string prefix) const;
202 
203     virtual status_t emitJavaTypeDeclarations(
204             Formatter &out, bool atTopLevel) const;
205 
206     virtual bool needsEmbeddedReadWrite() const;
207     virtual bool needsResolveReferences() const;
208     virtual bool resultNeedsDeref() const;
209 
210     // Generates type declaration for vts proto file.
211     // TODO (b/30844146): make it a pure virtual method.
212     virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
213     // Generates type declaration as attribute of method (return value or method
214     // argument) or attribute of compound type for vts proto file.
215     virtual status_t emitVtsAttributeType(Formatter &out) const;
216 
217     // Returns true iff this type is supported through the Java backend.
218     virtual bool isJavaCompatible() const;
219     virtual bool containsPointer() const;
220     virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
221 
222     void setAnnotations(std::vector<Annotation *> *annotations);
223     const std::vector<Annotation *> &annotations() const;
224 
225     virtual void appendToExportedTypesVector(
226             std::vector<const Type *> *exportedTypes) const;
227 
228     virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
229 
230 protected:
231     void handleError(Formatter &out, ErrorMode mode) const;
232 
233     void emitReaderWriterEmbeddedForTypeName(
234             Formatter &out,
235             const std::string &name,
236             bool nameIsPointer,
237             const std::string &parcelObj,
238             bool parcelObjIsPointer,
239             bool isReader,
240             ErrorMode mode,
241             const std::string &parentName,
242             const std::string &offsetText,
243             const std::string &typeName,
244             const std::string &childName,
245             const std::string &funcNamespace) const;
246 
247     void emitJavaReaderWriterWithSuffix(
248             Formatter &out,
249             const std::string &parcelObj,
250             const std::string &argName,
251             bool isReader,
252             const std::string &suffix,
253             const std::string &extra) const;
254 
255     void emitDumpWithMethod(
256             Formatter &out,
257             const std::string &streamName,
258             const std::string &methodName,
259             const std::string &name) const;
260 
261 private:
262     std::vector<Annotation *> *mAnnotations;
263 
264     DISALLOW_COPY_AND_ASSIGN(Type);
265 };
266 
267 /* Base type for VectorType and RefType. */
268 struct TemplatedType : public Type {
269     void setElementType(Type *elementType);
270     Type *getElementType() const;
271     bool isTemplatedType() const override;
272     virtual bool isCompatibleElementType(Type *elementType) const = 0;
273     status_t emitVtsTypeDeclarations(Formatter &out) const override;
274     status_t emitVtsAttributeType(Formatter &out) const override;
275 protected:
276     TemplatedType();
277     Type *mElementType;
278 private:
279     DISALLOW_COPY_AND_ASSIGN(TemplatedType);
280 };
281 
282 }  // namespace android
283 
284 #endif  // TYPE_H_
285 
286