blob: 6ffe7a20df0e6ce233e1f092bde7ac3e6389f46a [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Type.h"
2
Andreas Huber881227d2016-08-02 14:20:21 -07003#include "Formatter.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -07004#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -07005
6#include <android-base/logging.h>
7
Andreas Huberc9410c72016-07-28 12:18:40 -07008namespace android {
9
10Type::Type() {}
11Type::~Type() {}
12
Andreas Huber5345ec22016-07-29 13:33:27 -070013bool Type::isScope() const {
14 return false;
15}
16
Andreas Hubera2723d22016-07-29 15:36:07 -070017bool Type::isInterface() const {
18 return false;
19}
20
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070021bool Type::isEnum() const {
22 return false;
23}
24
25bool Type::isTypeDef() const {
26 return false;
27}
28
Andreas Huber295ad302016-08-16 11:35:00 -070029bool Type::isBinder() const {
30 return false;
31}
32
Andreas Huber39fa7182016-08-19 14:27:33 -070033bool Type::isNamedType() const {
34 return false;
35}
36
Andreas Huber737080b2016-08-02 15:38:04 -070037const ScalarType *Type::resolveToScalarType() const {
38 return NULL;
39}
40
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070041bool Type::isValidEnumStorageType() const {
42 const ScalarType *scalarType = resolveToScalarType();
43
44 if (scalarType == NULL) {
45 return false;
46 }
47
48 return scalarType->isValidEnumStorageType();
49}
50
Andreas Huber881227d2016-08-02 14:20:21 -070051std::string Type::getCppType(StorageMode, std::string *) const {
52 CHECK(!"Should not be here");
53 return std::string();
54}
55
Andreas Huber85eabdb2016-08-25 11:24:49 -070056std::string Type::getJavaWrapperType() const {
57 return getJavaType();
58}
59
Andreas Huber2831d512016-08-15 09:33:47 -070060std::string Type::getJavaSuffix() const {
61 CHECK(!"Should not be here");
62 return std::string();
63}
64
Andreas Huber881227d2016-08-02 14:20:21 -070065void Type::emitReaderWriter(
66 Formatter &,
67 const std::string &,
68 const std::string &,
69 bool,
70 bool,
71 ErrorMode) const {
72 CHECK(!"Should not be here");
73}
74
75void Type::emitReaderWriterEmbedded(
76 Formatter &,
77 const std::string &,
78 bool,
79 const std::string &,
80 bool,
81 bool,
82 ErrorMode,
83 const std::string &,
84 const std::string &) const {
85 CHECK(!"Should not be here");
86}
87
Andreas Huber2831d512016-08-15 09:33:47 -070088void Type::emitJavaReaderWriter(
89 Formatter &out,
90 const std::string &parcelObj,
91 const std::string &argName,
92 bool isReader) const {
93 emitJavaReaderWriterWithSuffix(
94 out,
95 parcelObj,
96 argName,
97 isReader,
98 getJavaSuffix(),
99 "" /* extra */);
100}
101
Andreas Huber85eabdb2016-08-25 11:24:49 -0700102void Type::emitJavaFieldInitializer(
103 Formatter &out,
104 const std::string &fieldName) const {
105 out << getJavaType()
106 << " "
107 << fieldName
108 << ";\n";
109}
110
111void Type::emitJavaFieldReaderWriter(
112 Formatter &,
113 const std::string &,
114 const std::string &,
115 const std::string &,
116 bool) const {
117 CHECK(!"Should not be here");
118}
119
Andreas Huber881227d2016-08-02 14:20:21 -0700120void Type::handleError(Formatter &out, ErrorMode mode) const {
121 switch (mode) {
122 case ErrorMode_Ignore:
123 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700124 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700125 break;
126 }
127
128 case ErrorMode_Goto:
129 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700130 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700131 break;
132 }
133
134 case ErrorMode_Break:
135 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700136 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700137 break;
138 }
Andreas Huber737080b2016-08-02 15:38:04 -0700139
140 case ErrorMode_Return:
141 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700142 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700143 break;
144 }
Andreas Huber881227d2016-08-02 14:20:21 -0700145 }
146}
147
148void Type::handleError2(Formatter &out, ErrorMode mode) const {
149 switch (mode) {
150 case ErrorMode_Goto:
151 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700152 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700153 break;
154 }
Andreas Huber737080b2016-08-02 15:38:04 -0700155
Andreas Huber881227d2016-08-02 14:20:21 -0700156 case ErrorMode_Break:
157 {
158 out << "break;\n";
159 break;
160 }
Andreas Huber737080b2016-08-02 15:38:04 -0700161
Andreas Huber881227d2016-08-02 14:20:21 -0700162 case ErrorMode_Ignore:
163 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700164 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700165 break;
166 }
Andreas Huber737080b2016-08-02 15:38:04 -0700167
168 case ErrorMode_Return:
169 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700170 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700171 break;
172 }
Andreas Huber881227d2016-08-02 14:20:21 -0700173 }
174}
175
176void Type::emitReaderWriterEmbeddedForTypeName(
177 Formatter &out,
178 const std::string &name,
179 bool nameIsPointer,
180 const std::string &parcelObj,
181 bool parcelObjIsPointer,
182 bool isReader,
183 ErrorMode mode,
184 const std::string &parentName,
185 const std::string &offsetText,
186 const std::string &typeName,
187 const std::string &childName) const {
188 const std::string parcelObjDeref =
189 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
190
191 const std::string parcelObjPointer =
192 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
193
194 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
195 const std::string namePointer = nameIsPointer ? name : ("&" + name);
196
Iliyan Malchev549e2592016-08-10 08:59:12 -0700197 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700198
199 if (isReader) {
200 out << "const_cast<"
201 << typeName
202 << " *>("
203 << namePointer
204 << ")->readEmbeddedFromParcel(\n";
205 } else {
206 out << nameDeref
207 << "writeEmbeddedToParcel(\n";
208 }
209
210 out.indent();
211 out.indent();
212
213 out << (isReader ? parcelObjDeref : parcelObjPointer)
214 << ",\n"
215 << parentName
216 << ",\n"
217 << offsetText;
218
219 if (!childName.empty()) {
220 out << ", &"
221 << childName;
222 }
223
224 out << ");\n\n";
225
226 out.unindent();
227 out.unindent();
228
229 handleError(out, mode);
230}
231
232status_t Type::emitTypeDeclarations(Formatter &) const {
233 return OK;
234}
235
236status_t Type::emitTypeDefinitions(
237 Formatter &, const std::string) const {
238 return OK;
239}
240
Andreas Huber85eabdb2016-08-25 11:24:49 -0700241status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700242 return OK;
243}
244
Andreas Huber881227d2016-08-02 14:20:21 -0700245bool Type::needsEmbeddedReadWrite() const {
246 return false;
247}
248
249bool Type::resultNeedsDeref() const {
250 return false;
251}
252
253std::string Type::getCppType(std::string *extra) const {
254 return getCppType(StorageMode_Stack, extra);
255}
256
257std::string Type::getCppResultType(std::string *extra) const {
258 return getCppType(StorageMode_Result, extra);
259}
260
261std::string Type::getCppArgumentType(std::string *extra) const {
262 return getCppType(StorageMode_Argument, extra);
263}
264
Andreas Huber2831d512016-08-15 09:33:47 -0700265void Type::emitJavaReaderWriterWithSuffix(
266 Formatter &out,
267 const std::string &parcelObj,
268 const std::string &argName,
269 bool isReader,
270 const std::string &suffix,
271 const std::string &extra) const {
272 out << parcelObj
273 << "."
274 << (isReader ? "read" : "write")
275 << suffix
276 << "(";
277
278 if (isReader) {
279 out << extra;
280 } else {
281 out << (extra.empty() ? "" : (extra + ", "));
282 out << argName;
283 }
284
285 out << ");\n";
286}
287
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700288status_t Type::emitVtsTypeDeclarations(Formatter &) const {
289 return OK;
290}
291
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700292status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700293 return emitVtsTypeDeclarations(out);
294}
295
Andreas Huber70a59e12016-08-16 12:57:01 -0700296bool Type::isJavaCompatible() const {
297 return true;
298}
299
Andreas Huber85eabdb2016-08-25 11:24:49 -0700300void Type::getAlignmentAndSize(size_t *, size_t *) const {
301 CHECK(!"Should not be here");
302}
303
Andreas Huberc9410c72016-07-28 12:18:40 -0700304} // namespace android
305