blob: 912bc253ccf9fd96b4f408dcbcb988c11de8debf [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 Huber737080b2016-08-02 15:38:04 -070033const ScalarType *Type::resolveToScalarType() const {
34 return NULL;
35}
36
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070037bool Type::isValidEnumStorageType() const {
38 const ScalarType *scalarType = resolveToScalarType();
39
40 if (scalarType == NULL) {
41 return false;
42 }
43
44 return scalarType->isValidEnumStorageType();
45}
46
Andreas Huber881227d2016-08-02 14:20:21 -070047std::string Type::getCppType(StorageMode, std::string *) const {
48 CHECK(!"Should not be here");
49 return std::string();
50}
51
Andreas Huber2831d512016-08-15 09:33:47 -070052std::string Type::getJavaSuffix() const {
53 CHECK(!"Should not be here");
54 return std::string();
55}
56
Andreas Huber881227d2016-08-02 14:20:21 -070057void Type::emitReaderWriter(
58 Formatter &,
59 const std::string &,
60 const std::string &,
61 bool,
62 bool,
63 ErrorMode) const {
64 CHECK(!"Should not be here");
65}
66
67void Type::emitReaderWriterEmbedded(
68 Formatter &,
69 const std::string &,
70 bool,
71 const std::string &,
72 bool,
73 bool,
74 ErrorMode,
75 const std::string &,
76 const std::string &) const {
77 CHECK(!"Should not be here");
78}
79
Andreas Huber2831d512016-08-15 09:33:47 -070080void Type::emitJavaReaderWriter(
81 Formatter &out,
82 const std::string &parcelObj,
83 const std::string &argName,
84 bool isReader) const {
85 emitJavaReaderWriterWithSuffix(
86 out,
87 parcelObj,
88 argName,
89 isReader,
90 getJavaSuffix(),
91 "" /* extra */);
92}
93
Andreas Huber881227d2016-08-02 14:20:21 -070094void Type::handleError(Formatter &out, ErrorMode mode) const {
95 switch (mode) {
96 case ErrorMode_Ignore:
97 {
Iliyan Malchev549e2592016-08-10 08:59:12 -070098 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070099 break;
100 }
101
102 case ErrorMode_Goto:
103 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700104 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700105 break;
106 }
107
108 case ErrorMode_Break:
109 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700110 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700111 break;
112 }
Andreas Huber737080b2016-08-02 15:38:04 -0700113
114 case ErrorMode_Return:
115 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700116 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700117 break;
118 }
Andreas Huber881227d2016-08-02 14:20:21 -0700119 }
120}
121
122void Type::handleError2(Formatter &out, ErrorMode mode) const {
123 switch (mode) {
124 case ErrorMode_Goto:
125 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700126 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700127 break;
128 }
Andreas Huber737080b2016-08-02 15:38:04 -0700129
Andreas Huber881227d2016-08-02 14:20:21 -0700130 case ErrorMode_Break:
131 {
132 out << "break;\n";
133 break;
134 }
Andreas Huber737080b2016-08-02 15:38:04 -0700135
Andreas Huber881227d2016-08-02 14:20:21 -0700136 case ErrorMode_Ignore:
137 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700138 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700139 break;
140 }
Andreas Huber737080b2016-08-02 15:38:04 -0700141
142 case ErrorMode_Return:
143 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700144 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700145 break;
146 }
Andreas Huber881227d2016-08-02 14:20:21 -0700147 }
148}
149
150void Type::emitReaderWriterEmbeddedForTypeName(
151 Formatter &out,
152 const std::string &name,
153 bool nameIsPointer,
154 const std::string &parcelObj,
155 bool parcelObjIsPointer,
156 bool isReader,
157 ErrorMode mode,
158 const std::string &parentName,
159 const std::string &offsetText,
160 const std::string &typeName,
161 const std::string &childName) const {
162 const std::string parcelObjDeref =
163 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
164
165 const std::string parcelObjPointer =
166 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
167
168 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
169 const std::string namePointer = nameIsPointer ? name : ("&" + name);
170
Iliyan Malchev549e2592016-08-10 08:59:12 -0700171 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700172
173 if (isReader) {
174 out << "const_cast<"
175 << typeName
176 << " *>("
177 << namePointer
178 << ")->readEmbeddedFromParcel(\n";
179 } else {
180 out << nameDeref
181 << "writeEmbeddedToParcel(\n";
182 }
183
184 out.indent();
185 out.indent();
186
187 out << (isReader ? parcelObjDeref : parcelObjPointer)
188 << ",\n"
189 << parentName
190 << ",\n"
191 << offsetText;
192
193 if (!childName.empty()) {
194 out << ", &"
195 << childName;
196 }
197
198 out << ");\n\n";
199
200 out.unindent();
201 out.unindent();
202
203 handleError(out, mode);
204}
205
206status_t Type::emitTypeDeclarations(Formatter &) const {
207 return OK;
208}
209
210status_t Type::emitTypeDefinitions(
211 Formatter &, const std::string) const {
212 return OK;
213}
214
Andreas Huber2831d512016-08-15 09:33:47 -0700215status_t Type::emitJavaTypeDeclarations(Formatter &) const {
216 return OK;
217}
218
Andreas Huber881227d2016-08-02 14:20:21 -0700219bool Type::needsEmbeddedReadWrite() const {
220 return false;
221}
222
223bool Type::resultNeedsDeref() const {
224 return false;
225}
226
227std::string Type::getCppType(std::string *extra) const {
228 return getCppType(StorageMode_Stack, extra);
229}
230
231std::string Type::getCppResultType(std::string *extra) const {
232 return getCppType(StorageMode_Result, extra);
233}
234
235std::string Type::getCppArgumentType(std::string *extra) const {
236 return getCppType(StorageMode_Argument, extra);
237}
238
Andreas Huber2831d512016-08-15 09:33:47 -0700239void Type::emitJavaReaderWriterWithSuffix(
240 Formatter &out,
241 const std::string &parcelObj,
242 const std::string &argName,
243 bool isReader,
244 const std::string &suffix,
245 const std::string &extra) const {
246 out << parcelObj
247 << "."
248 << (isReader ? "read" : "write")
249 << suffix
250 << "(";
251
252 if (isReader) {
253 out << extra;
254 } else {
255 out << (extra.empty() ? "" : (extra + ", "));
256 out << argName;
257 }
258
259 out << ");\n";
260}
261
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700262status_t Type::emitVtsTypeDeclarations(Formatter &) const {
263 return OK;
264}
265
266status_t Type::emitVtsArgumentType(Formatter &out) const {
267 return emitVtsTypeDeclarations(out);
268}
269
Andreas Huber70a59e12016-08-16 12:57:01 -0700270bool Type::isJavaCompatible() const {
271 return true;
272}
273
Andreas Huberc9410c72016-07-28 12:18:40 -0700274} // namespace android
275