blob: e42f0721a6cac4b52056a229826155bbba1619ae [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#include "Type.h"
18
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070019#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070020
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070021#include <hidl-util/Formatter.h>
Andreas Huber881227d2016-08-02 14:20:21 -070022#include <android-base/logging.h>
23
Andreas Huberc9410c72016-07-28 12:18:40 -070024namespace android {
25
26Type::Type() {}
27Type::~Type() {}
28
Andreas Huber5345ec22016-07-29 13:33:27 -070029bool Type::isScope() const {
30 return false;
31}
32
Andreas Hubera2723d22016-07-29 15:36:07 -070033bool Type::isInterface() const {
34 return false;
35}
36
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070037bool Type::isEnum() const {
38 return false;
39}
40
41bool Type::isTypeDef() const {
42 return false;
43}
44
Andreas Huber295ad302016-08-16 11:35:00 -070045bool Type::isBinder() const {
46 return false;
47}
48
Andreas Huber39fa7182016-08-19 14:27:33 -070049bool Type::isNamedType() const {
50 return false;
51}
52
Andreas Huberf630bc82016-09-09 14:52:25 -070053bool Type::isCompoundType() const {
54 return false;
55}
56
Andreas Huber709b62d2016-09-19 11:21:18 -070057bool Type::isArray() const {
58 return false;
59}
60
61bool Type::isVector() const {
62 return false;
63}
64
Andreas Huber737080b2016-08-02 15:38:04 -070065const ScalarType *Type::resolveToScalarType() const {
66 return NULL;
67}
68
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070069bool Type::isValidEnumStorageType() const {
70 const ScalarType *scalarType = resolveToScalarType();
71
72 if (scalarType == NULL) {
73 return false;
74 }
75
76 return scalarType->isValidEnumStorageType();
77}
78
Steven Moreland979e0992016-09-07 09:18:08 -070079std::string Type::getCppType(StorageMode, std::string *, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070080 CHECK(!"Should not be here");
81 return std::string();
82}
83
Andreas Huber4c865b72016-09-14 15:26:27 -070084std::string Type::getJavaType(
85 std::string *extra, bool /* forInitializer */) const {
86 CHECK(!"Should not be here");
87 extra->clear();
88 return std::string();
89}
90
Andreas Huber85eabdb2016-08-25 11:24:49 -070091std::string Type::getJavaWrapperType() const {
Andreas Huber4c865b72016-09-14 15:26:27 -070092 std::string extra;
93 return getJavaType(&extra);
Andreas Huber85eabdb2016-08-25 11:24:49 -070094}
95
Andreas Huber2831d512016-08-15 09:33:47 -070096std::string Type::getJavaSuffix() const {
97 CHECK(!"Should not be here");
98 return std::string();
99}
100
Andreas Huber881227d2016-08-02 14:20:21 -0700101void Type::emitReaderWriter(
102 Formatter &,
103 const std::string &,
104 const std::string &,
105 bool,
106 bool,
107 ErrorMode) const {
108 CHECK(!"Should not be here");
109}
110
111void Type::emitReaderWriterEmbedded(
112 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700113 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700114 const std::string &,
115 bool,
116 const std::string &,
117 bool,
118 bool,
119 ErrorMode,
120 const std::string &,
121 const std::string &) const {
122 CHECK(!"Should not be here");
123}
124
Andreas Huber2831d512016-08-15 09:33:47 -0700125void Type::emitJavaReaderWriter(
126 Formatter &out,
127 const std::string &parcelObj,
128 const std::string &argName,
129 bool isReader) const {
130 emitJavaReaderWriterWithSuffix(
131 out,
132 parcelObj,
133 argName,
134 isReader,
135 getJavaSuffix(),
136 "" /* extra */);
137}
138
Andreas Huber85eabdb2016-08-25 11:24:49 -0700139void Type::emitJavaFieldInitializer(
140 Formatter &out,
141 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700142 std::string extra;
143 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700144 << " "
145 << fieldName
146 << ";\n";
147}
148
149void Type::emitJavaFieldReaderWriter(
150 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700151 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700152 const std::string &,
153 const std::string &,
154 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700155 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700156 bool) const {
157 CHECK(!"Should not be here");
158}
159
Andreas Huber881227d2016-08-02 14:20:21 -0700160void Type::handleError(Formatter &out, ErrorMode mode) const {
161 switch (mode) {
162 case ErrorMode_Ignore:
163 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700164 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700165 break;
166 }
167
168 case ErrorMode_Goto:
169 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700170 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700171 break;
172 }
173
174 case ErrorMode_Break:
175 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700176 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700177 break;
178 }
Andreas Huber737080b2016-08-02 15:38:04 -0700179
180 case ErrorMode_Return:
181 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700182 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700183 break;
184 }
Andreas Huber881227d2016-08-02 14:20:21 -0700185 }
186}
187
188void Type::handleError2(Formatter &out, ErrorMode mode) const {
189 switch (mode) {
190 case ErrorMode_Goto:
191 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700192 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700193 break;
194 }
Andreas Huber737080b2016-08-02 15:38:04 -0700195
Andreas Huber881227d2016-08-02 14:20:21 -0700196 case ErrorMode_Break:
197 {
198 out << "break;\n";
199 break;
200 }
Andreas Huber737080b2016-08-02 15:38:04 -0700201
Andreas Huber881227d2016-08-02 14:20:21 -0700202 case ErrorMode_Ignore:
203 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700204 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700205 break;
206 }
Andreas Huber737080b2016-08-02 15:38:04 -0700207
208 case ErrorMode_Return:
209 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700210 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700211 break;
212 }
Andreas Huber881227d2016-08-02 14:20:21 -0700213 }
214}
215
216void Type::emitReaderWriterEmbeddedForTypeName(
217 Formatter &out,
218 const std::string &name,
219 bool nameIsPointer,
220 const std::string &parcelObj,
221 bool parcelObjIsPointer,
222 bool isReader,
223 ErrorMode mode,
224 const std::string &parentName,
225 const std::string &offsetText,
226 const std::string &typeName,
227 const std::string &childName) const {
228 const std::string parcelObjDeref =
229 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
230
231 const std::string parcelObjPointer =
232 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
233
234 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
235 const std::string namePointer = nameIsPointer ? name : ("&" + name);
236
Iliyan Malchev549e2592016-08-10 08:59:12 -0700237 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700238
239 if (isReader) {
240 out << "const_cast<"
241 << typeName
242 << " *>("
243 << namePointer
244 << ")->readEmbeddedFromParcel(\n";
245 } else {
246 out << nameDeref
247 << "writeEmbeddedToParcel(\n";
248 }
249
250 out.indent();
251 out.indent();
252
253 out << (isReader ? parcelObjDeref : parcelObjPointer)
254 << ",\n"
255 << parentName
256 << ",\n"
257 << offsetText;
258
259 if (!childName.empty()) {
260 out << ", &"
261 << childName;
262 }
263
264 out << ");\n\n";
265
266 out.unindent();
267 out.unindent();
268
269 handleError(out, mode);
270}
271
272status_t Type::emitTypeDeclarations(Formatter &) const {
273 return OK;
274}
275
276status_t Type::emitTypeDefinitions(
277 Formatter &, const std::string) const {
278 return OK;
279}
280
Andreas Huber85eabdb2016-08-25 11:24:49 -0700281status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700282 return OK;
283}
284
Andreas Huber881227d2016-08-02 14:20:21 -0700285bool Type::needsEmbeddedReadWrite() const {
286 return false;
287}
288
289bool Type::resultNeedsDeref() const {
290 return false;
291}
292
Steven Moreland979e0992016-09-07 09:18:08 -0700293std::string Type::getCppType(std::string *extra,
294 bool specifyNamespaces) const {
295 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700296}
297
Steven Moreland979e0992016-09-07 09:18:08 -0700298std::string Type::getCppResultType(std::string *extra,
299 bool specifyNamespaces) const {
300 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700301}
302
Steven Moreland979e0992016-09-07 09:18:08 -0700303std::string Type::getCppArgumentType(std::string *extra,
304 bool specifyNamespaces) const {
305 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700306}
307
Andreas Huber2831d512016-08-15 09:33:47 -0700308void Type::emitJavaReaderWriterWithSuffix(
309 Formatter &out,
310 const std::string &parcelObj,
311 const std::string &argName,
312 bool isReader,
313 const std::string &suffix,
314 const std::string &extra) const {
315 out << parcelObj
316 << "."
317 << (isReader ? "read" : "write")
318 << suffix
319 << "(";
320
321 if (isReader) {
322 out << extra;
323 } else {
324 out << (extra.empty() ? "" : (extra + ", "));
325 out << argName;
326 }
327
328 out << ");\n";
329}
330
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700331status_t Type::emitVtsTypeDeclarations(Formatter &) const {
332 return OK;
333}
334
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700335status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700336 return emitVtsTypeDeclarations(out);
337}
338
Andreas Huber70a59e12016-08-16 12:57:01 -0700339bool Type::isJavaCompatible() const {
340 return true;
341}
342
Andreas Huber85eabdb2016-08-25 11:24:49 -0700343void Type::getAlignmentAndSize(size_t *, size_t *) const {
344 CHECK(!"Should not be here");
345}
346
Andreas Huberc9410c72016-07-28 12:18:40 -0700347} // namespace android
348