blob: 632220714dbbf38ab94739fadd0f87000e7cef07 [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>
Steven Moreland979e0992016-09-07 09:18:08 -070024#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070025
26namespace android {
27
28struct Formatter;
Andreas Huber737080b2016-08-02 15:38:04 -070029struct ScalarType;
Steven Moreland979e0992016-09-07 09:18:08 -070030struct FQName;
Andreas Huberc9410c72016-07-28 12:18:40 -070031
32struct Type {
33 Type();
34 virtual ~Type();
35
Andreas Huberfd4afab2016-08-03 13:02:57 -070036 Type *ref() { return this; }
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
Andreas Huber881227d2016-08-02 14:20:21 -070093 enum ErrorMode {
94 ErrorMode_Ignore,
95 ErrorMode_Goto,
96 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -070097 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -070098 };
99 virtual void emitReaderWriter(
100 Formatter &out,
101 const std::string &name,
102 const std::string &parcelObj,
103 bool parcelObjIsPointer,
104 bool isReader,
105 ErrorMode mode) const;
106
107 virtual void emitReaderWriterEmbedded(
108 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700109 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700110 const std::string &name,
111 bool nameIsPointer,
112 const std::string &parcelObj,
113 bool parcelObjIsPointer,
114 bool isReader,
115 ErrorMode mode,
116 const std::string &parentName,
117 const std::string &offsetText) const;
118
Andreas Huber2831d512016-08-15 09:33:47 -0700119 virtual void emitJavaReaderWriter(
120 Formatter &out,
121 const std::string &parcelObj,
122 const std::string &argName,
123 bool isReader) const;
124
Andreas Huber85eabdb2016-08-25 11:24:49 -0700125 virtual void emitJavaFieldInitializer(
126 Formatter &out,
127 const std::string &fieldName) const;
128
129 virtual void emitJavaFieldReaderWriter(
130 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700131 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700132 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700133 const std::string &blobName,
134 const std::string &fieldName,
135 const std::string &offset,
136 bool isReader) const;
137
Andreas Huber881227d2016-08-02 14:20:21 -0700138 virtual status_t emitTypeDeclarations(Formatter &out) const;
139
140 virtual status_t emitTypeDefinitions(
141 Formatter &out, const std::string prefix) const;
142
Andreas Huber85eabdb2016-08-25 11:24:49 -0700143 virtual status_t emitJavaTypeDeclarations(
144 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700145
Andreas Huber881227d2016-08-02 14:20:21 -0700146 virtual bool needsEmbeddedReadWrite() const;
147 virtual bool resultNeedsDeref() const;
148
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700149 // Generates type declaration for vts proto file.
150 // TODO (b/30844146): make it a pure virtual method.
151 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700152 // Generates type declaration as attribute of method (return value or method
153 // argument) or attribute of compound type for vts proto file.
154 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700155
Andreas Huber70a59e12016-08-16 12:57:01 -0700156 // Returns true iff this type is supported through the Java backend.
157 virtual bool isJavaCompatible() const;
158
Andreas Huber85eabdb2016-08-25 11:24:49 -0700159 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
160
Andreas Huber881227d2016-08-02 14:20:21 -0700161protected:
162 void handleError(Formatter &out, ErrorMode mode) const;
163 void handleError2(Formatter &out, ErrorMode mode) const;
164
165 void emitReaderWriterEmbeddedForTypeName(
166 Formatter &out,
167 const std::string &name,
168 bool nameIsPointer,
169 const std::string &parcelObj,
170 bool parcelObjIsPointer,
171 bool isReader,
172 ErrorMode mode,
173 const std::string &parentName,
174 const std::string &offsetText,
175 const std::string &typeName,
176 const std::string &childName) const;
177
Andreas Huber2831d512016-08-15 09:33:47 -0700178 void emitJavaReaderWriterWithSuffix(
179 Formatter &out,
180 const std::string &parcelObj,
181 const std::string &argName,
182 bool isReader,
183 const std::string &suffix,
184 const std::string &extra) const;
185
Andreas Huberc9410c72016-07-28 12:18:40 -0700186private:
187 DISALLOW_COPY_AND_ASSIGN(Type);
188};
189
190} // namespace android
191
192#endif // TYPE_H_
193