blob: 907a0c2df167ddc678709206642578249c9c8c03 [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 Huber2831d512016-08-15 09:33:47 -070056std::string Type::getJavaSuffix() const {
57 CHECK(!"Should not be here");
58 return std::string();
59}
60
Andreas Huber881227d2016-08-02 14:20:21 -070061void Type::emitReaderWriter(
62 Formatter &,
63 const std::string &,
64 const std::string &,
65 bool,
66 bool,
67 ErrorMode) const {
68 CHECK(!"Should not be here");
69}
70
71void Type::emitReaderWriterEmbedded(
72 Formatter &,
73 const std::string &,
74 bool,
75 const std::string &,
76 bool,
77 bool,
78 ErrorMode,
79 const std::string &,
80 const std::string &) const {
81 CHECK(!"Should not be here");
82}
83
Andreas Huber2831d512016-08-15 09:33:47 -070084void Type::emitJavaReaderWriter(
85 Formatter &out,
86 const std::string &parcelObj,
87 const std::string &argName,
88 bool isReader) const {
89 emitJavaReaderWriterWithSuffix(
90 out,
91 parcelObj,
92 argName,
93 isReader,
94 getJavaSuffix(),
95 "" /* extra */);
96}
97
Andreas Huber881227d2016-08-02 14:20:21 -070098void Type::handleError(Formatter &out, ErrorMode mode) const {
99 switch (mode) {
100 case ErrorMode_Ignore:
101 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700102 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700103 break;
104 }
105
106 case ErrorMode_Goto:
107 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700108 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700109 break;
110 }
111
112 case ErrorMode_Break:
113 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700114 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700115 break;
116 }
Andreas Huber737080b2016-08-02 15:38:04 -0700117
118 case ErrorMode_Return:
119 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700120 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700121 break;
122 }
Andreas Huber881227d2016-08-02 14:20:21 -0700123 }
124}
125
126void Type::handleError2(Formatter &out, ErrorMode mode) const {
127 switch (mode) {
128 case ErrorMode_Goto:
129 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700130 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700131 break;
132 }
Andreas Huber737080b2016-08-02 15:38:04 -0700133
Andreas Huber881227d2016-08-02 14:20:21 -0700134 case ErrorMode_Break:
135 {
136 out << "break;\n";
137 break;
138 }
Andreas Huber737080b2016-08-02 15:38:04 -0700139
Andreas Huber881227d2016-08-02 14:20:21 -0700140 case ErrorMode_Ignore:
141 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700142 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700143 break;
144 }
Andreas Huber737080b2016-08-02 15:38:04 -0700145
146 case ErrorMode_Return:
147 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700148 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700149 break;
150 }
Andreas Huber881227d2016-08-02 14:20:21 -0700151 }
152}
153
154void Type::emitReaderWriterEmbeddedForTypeName(
155 Formatter &out,
156 const std::string &name,
157 bool nameIsPointer,
158 const std::string &parcelObj,
159 bool parcelObjIsPointer,
160 bool isReader,
161 ErrorMode mode,
162 const std::string &parentName,
163 const std::string &offsetText,
164 const std::string &typeName,
165 const std::string &childName) const {
166 const std::string parcelObjDeref =
167 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
168
169 const std::string parcelObjPointer =
170 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
171
172 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
173 const std::string namePointer = nameIsPointer ? name : ("&" + name);
174
Iliyan Malchev549e2592016-08-10 08:59:12 -0700175 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700176
177 if (isReader) {
178 out << "const_cast<"
179 << typeName
180 << " *>("
181 << namePointer
182 << ")->readEmbeddedFromParcel(\n";
183 } else {
184 out << nameDeref
185 << "writeEmbeddedToParcel(\n";
186 }
187
188 out.indent();
189 out.indent();
190
191 out << (isReader ? parcelObjDeref : parcelObjPointer)
192 << ",\n"
193 << parentName
194 << ",\n"
195 << offsetText;
196
197 if (!childName.empty()) {
198 out << ", &"
199 << childName;
200 }
201
202 out << ");\n\n";
203
204 out.unindent();
205 out.unindent();
206
207 handleError(out, mode);
208}
209
210status_t Type::emitTypeDeclarations(Formatter &) const {
211 return OK;
212}
213
214status_t Type::emitTypeDefinitions(
215 Formatter &, const std::string) const {
216 return OK;
217}
218
Andreas Huber2831d512016-08-15 09:33:47 -0700219status_t Type::emitJavaTypeDeclarations(Formatter &) const {
220 return OK;
221}
222
Andreas Huber881227d2016-08-02 14:20:21 -0700223bool Type::needsEmbeddedReadWrite() const {
224 return false;
225}
226
227bool Type::resultNeedsDeref() const {
228 return false;
229}
230
231std::string Type::getCppType(std::string *extra) const {
232 return getCppType(StorageMode_Stack, extra);
233}
234
235std::string Type::getCppResultType(std::string *extra) const {
236 return getCppType(StorageMode_Result, extra);
237}
238
239std::string Type::getCppArgumentType(std::string *extra) const {
240 return getCppType(StorageMode_Argument, extra);
241}
242
Andreas Huber2831d512016-08-15 09:33:47 -0700243void Type::emitJavaReaderWriterWithSuffix(
244 Formatter &out,
245 const std::string &parcelObj,
246 const std::string &argName,
247 bool isReader,
248 const std::string &suffix,
249 const std::string &extra) const {
250 out << parcelObj
251 << "."
252 << (isReader ? "read" : "write")
253 << suffix
254 << "(";
255
256 if (isReader) {
257 out << extra;
258 } else {
259 out << (extra.empty() ? "" : (extra + ", "));
260 out << argName;
261 }
262
263 out << ");\n";
264}
265
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700266status_t Type::emitVtsTypeDeclarations(Formatter &) const {
267 return OK;
268}
269
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700270status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700271 return emitVtsTypeDeclarations(out);
272}
273
Andreas Huber70a59e12016-08-16 12:57:01 -0700274bool Type::isJavaCompatible() const {
275 return true;
276}
277
Andreas Huberc9410c72016-07-28 12:18:40 -0700278} // namespace android
279