blob: d17d906ddcc61170a72b9619802e4ebbe195f0ce [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 Huber881227d2016-08-02 14:20:21 -070019#include "Formatter.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070020#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021
22#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 Huber737080b2016-08-02 15:38:04 -070053const ScalarType *Type::resolveToScalarType() const {
54 return NULL;
55}
56
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070057bool Type::isValidEnumStorageType() const {
58 const ScalarType *scalarType = resolveToScalarType();
59
60 if (scalarType == NULL) {
61 return false;
62 }
63
64 return scalarType->isValidEnumStorageType();
65}
66
Andreas Huber881227d2016-08-02 14:20:21 -070067std::string Type::getCppType(StorageMode, std::string *) const {
68 CHECK(!"Should not be here");
69 return std::string();
70}
71
Andreas Huber85eabdb2016-08-25 11:24:49 -070072std::string Type::getJavaWrapperType() const {
73 return getJavaType();
74}
75
Andreas Huber2831d512016-08-15 09:33:47 -070076std::string Type::getJavaSuffix() const {
77 CHECK(!"Should not be here");
78 return std::string();
79}
80
Andreas Huber881227d2016-08-02 14:20:21 -070081void Type::emitReaderWriter(
82 Formatter &,
83 const std::string &,
84 const std::string &,
85 bool,
86 bool,
87 ErrorMode) const {
88 CHECK(!"Should not be here");
89}
90
91void Type::emitReaderWriterEmbedded(
92 Formatter &,
93 const std::string &,
94 bool,
95 const std::string &,
96 bool,
97 bool,
98 ErrorMode,
99 const std::string &,
100 const std::string &) const {
101 CHECK(!"Should not be here");
102}
103
Andreas Huber2831d512016-08-15 09:33:47 -0700104void Type::emitJavaReaderWriter(
105 Formatter &out,
106 const std::string &parcelObj,
107 const std::string &argName,
108 bool isReader) const {
109 emitJavaReaderWriterWithSuffix(
110 out,
111 parcelObj,
112 argName,
113 isReader,
114 getJavaSuffix(),
115 "" /* extra */);
116}
117
Andreas Huber85eabdb2016-08-25 11:24:49 -0700118void Type::emitJavaFieldInitializer(
119 Formatter &out,
120 const std::string &fieldName) const {
121 out << getJavaType()
122 << " "
123 << fieldName
124 << ";\n";
125}
126
127void Type::emitJavaFieldReaderWriter(
128 Formatter &,
129 const std::string &,
130 const std::string &,
131 const std::string &,
132 bool) const {
133 CHECK(!"Should not be here");
134}
135
Andreas Huber881227d2016-08-02 14:20:21 -0700136void Type::handleError(Formatter &out, ErrorMode mode) const {
137 switch (mode) {
138 case ErrorMode_Ignore:
139 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700140 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700141 break;
142 }
143
144 case ErrorMode_Goto:
145 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700146 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700147 break;
148 }
149
150 case ErrorMode_Break:
151 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700152 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700153 break;
154 }
Andreas Huber737080b2016-08-02 15:38:04 -0700155
156 case ErrorMode_Return:
157 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700158 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700159 break;
160 }
Andreas Huber881227d2016-08-02 14:20:21 -0700161 }
162}
163
164void Type::handleError2(Formatter &out, ErrorMode mode) const {
165 switch (mode) {
166 case ErrorMode_Goto:
167 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700168 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700169 break;
170 }
Andreas Huber737080b2016-08-02 15:38:04 -0700171
Andreas Huber881227d2016-08-02 14:20:21 -0700172 case ErrorMode_Break:
173 {
174 out << "break;\n";
175 break;
176 }
Andreas Huber737080b2016-08-02 15:38:04 -0700177
Andreas Huber881227d2016-08-02 14:20:21 -0700178 case ErrorMode_Ignore:
179 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700180 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700181 break;
182 }
Andreas Huber737080b2016-08-02 15:38:04 -0700183
184 case ErrorMode_Return:
185 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700186 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700187 break;
188 }
Andreas Huber881227d2016-08-02 14:20:21 -0700189 }
190}
191
192void Type::emitReaderWriterEmbeddedForTypeName(
193 Formatter &out,
194 const std::string &name,
195 bool nameIsPointer,
196 const std::string &parcelObj,
197 bool parcelObjIsPointer,
198 bool isReader,
199 ErrorMode mode,
200 const std::string &parentName,
201 const std::string &offsetText,
202 const std::string &typeName,
203 const std::string &childName) const {
204 const std::string parcelObjDeref =
205 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
206
207 const std::string parcelObjPointer =
208 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
209
210 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
211 const std::string namePointer = nameIsPointer ? name : ("&" + name);
212
Iliyan Malchev549e2592016-08-10 08:59:12 -0700213 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700214
215 if (isReader) {
216 out << "const_cast<"
217 << typeName
218 << " *>("
219 << namePointer
220 << ")->readEmbeddedFromParcel(\n";
221 } else {
222 out << nameDeref
223 << "writeEmbeddedToParcel(\n";
224 }
225
226 out.indent();
227 out.indent();
228
229 out << (isReader ? parcelObjDeref : parcelObjPointer)
230 << ",\n"
231 << parentName
232 << ",\n"
233 << offsetText;
234
235 if (!childName.empty()) {
236 out << ", &"
237 << childName;
238 }
239
240 out << ");\n\n";
241
242 out.unindent();
243 out.unindent();
244
245 handleError(out, mode);
246}
247
248status_t Type::emitTypeDeclarations(Formatter &) const {
249 return OK;
250}
251
252status_t Type::emitTypeDefinitions(
253 Formatter &, const std::string) const {
254 return OK;
255}
256
Andreas Huber85eabdb2016-08-25 11:24:49 -0700257status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700258 return OK;
259}
260
Andreas Huber881227d2016-08-02 14:20:21 -0700261bool Type::needsEmbeddedReadWrite() const {
262 return false;
263}
264
265bool Type::resultNeedsDeref() const {
266 return false;
267}
268
269std::string Type::getCppType(std::string *extra) const {
270 return getCppType(StorageMode_Stack, extra);
271}
272
273std::string Type::getCppResultType(std::string *extra) const {
274 return getCppType(StorageMode_Result, extra);
275}
276
277std::string Type::getCppArgumentType(std::string *extra) const {
278 return getCppType(StorageMode_Argument, extra);
279}
280
Andreas Huber2831d512016-08-15 09:33:47 -0700281void Type::emitJavaReaderWriterWithSuffix(
282 Formatter &out,
283 const std::string &parcelObj,
284 const std::string &argName,
285 bool isReader,
286 const std::string &suffix,
287 const std::string &extra) const {
288 out << parcelObj
289 << "."
290 << (isReader ? "read" : "write")
291 << suffix
292 << "(";
293
294 if (isReader) {
295 out << extra;
296 } else {
297 out << (extra.empty() ? "" : (extra + ", "));
298 out << argName;
299 }
300
301 out << ");\n";
302}
303
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700304status_t Type::emitVtsTypeDeclarations(Formatter &) const {
305 return OK;
306}
307
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700308status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700309 return emitVtsTypeDeclarations(out);
310}
311
Andreas Huber70a59e12016-08-16 12:57:01 -0700312bool Type::isJavaCompatible() const {
313 return true;
314}
315
Andreas Huber85eabdb2016-08-25 11:24:49 -0700316void Type::getAlignmentAndSize(size_t *, size_t *) const {
317 CHECK(!"Should not be here");
318}
319
Andreas Huberc9410c72016-07-28 12:18:40 -0700320} // namespace android
321