blob: 924caedba13e7a112299ef0bdc41bca265a915f7 [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 Huber7c5ddfb2016-09-29 13:45:22 -070019#include "Annotation.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070020#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070022#include <hidl-util/Formatter.h>
Andreas Huber881227d2016-08-02 14:20:21 -070023#include <android-base/logging.h>
24
Andreas Huberc9410c72016-07-28 12:18:40 -070025namespace android {
26
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070027Type::Type()
28 : mAnnotations(nullptr) {
29}
30
Andreas Huberc9410c72016-07-28 12:18:40 -070031Type::~Type() {}
32
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070033void Type::setAnnotations(std::vector<Annotation *> *annotations) {
34 mAnnotations = annotations;
35}
36
37const std::vector<Annotation *> &Type::annotations() const {
38 return *mAnnotations;
39}
40
Andreas Huber5345ec22016-07-29 13:33:27 -070041bool Type::isScope() const {
42 return false;
43}
44
Andreas Hubera2723d22016-07-29 15:36:07 -070045bool Type::isInterface() const {
46 return false;
47}
48
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070049bool Type::isEnum() const {
50 return false;
51}
52
53bool Type::isTypeDef() const {
54 return false;
55}
56
Andreas Huber295ad302016-08-16 11:35:00 -070057bool Type::isBinder() const {
58 return false;
59}
60
Andreas Huber39fa7182016-08-19 14:27:33 -070061bool Type::isNamedType() const {
62 return false;
63}
64
Andreas Huberf630bc82016-09-09 14:52:25 -070065bool Type::isCompoundType() const {
66 return false;
67}
68
Andreas Huber709b62d2016-09-19 11:21:18 -070069bool Type::isArray() const {
70 return false;
71}
72
73bool Type::isVector() const {
74 return false;
75}
76
Martijn Coenen99e6beb2016-12-01 15:48:42 +010077bool Type::isPointer() const {
78 return false;
79}
80
Andreas Huber737080b2016-08-02 15:38:04 -070081const ScalarType *Type::resolveToScalarType() const {
82 return NULL;
83}
84
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070085bool Type::isValidEnumStorageType() const {
86 const ScalarType *scalarType = resolveToScalarType();
87
88 if (scalarType == NULL) {
89 return false;
90 }
91
92 return scalarType->isValidEnumStorageType();
93}
94
Yifan Hong3b320f82016-11-01 15:15:54 -070095std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070096 CHECK(!"Should not be here");
97 return std::string();
98}
99
Yifan Hong3b320f82016-11-01 15:15:54 -0700100std::string Type::decorateCppName(
101 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
102 return getCppType(mode, specifyNamespaces) + " " + name;
103}
104
Yifan Hong4ed13472016-11-02 10:44:11 -0700105std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700106 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700107 return std::string();
108}
109
Andreas Huber85eabdb2016-08-25 11:24:49 -0700110std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700111 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700112}
113
Andreas Huber2831d512016-08-15 09:33:47 -0700114std::string Type::getJavaSuffix() const {
115 CHECK(!"Should not be here");
116 return std::string();
117}
118
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700119std::string Type::getVtsType() const {
120 CHECK(!"Should not be here");
121 return std::string();
122}
123
Andreas Huber881227d2016-08-02 14:20:21 -0700124void Type::emitReaderWriter(
125 Formatter &,
126 const std::string &,
127 const std::string &,
128 bool,
129 bool,
130 ErrorMode) const {
131 CHECK(!"Should not be here");
132}
133
Yifan Hongbf459bc2016-08-23 16:50:37 -0700134void Type::emitResolveReferences(
135 Formatter &,
136 const std::string &,
137 bool,
138 const std::string &,
139 bool,
140 bool,
141 ErrorMode) const {
142 CHECK(!"Should not be here");
143}
144
145void Type::emitResolveReferencesEmbedded(
146 Formatter &,
147 size_t,
148 const std::string &,
149 const std::string &,
150 bool,
151 const std::string &,
152 bool,
153 bool,
154 ErrorMode,
155 const std::string &,
156 const std::string &) const {
157 CHECK(!"Should not be here");
158}
159
Yifan Hong00f47172016-09-30 14:40:45 -0700160bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800161 return needsResolveReferences();
162}
163
164bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
165 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700166}
167
Andreas Huber881227d2016-08-02 14:20:21 -0700168void Type::emitReaderWriterEmbedded(
169 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700170 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700171 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700172 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700173 bool,
174 const std::string &,
175 bool,
176 bool,
177 ErrorMode,
178 const std::string &,
179 const std::string &) const {
180 CHECK(!"Should not be here");
181}
182
Andreas Huber2831d512016-08-15 09:33:47 -0700183void Type::emitJavaReaderWriter(
184 Formatter &out,
185 const std::string &parcelObj,
186 const std::string &argName,
187 bool isReader) const {
188 emitJavaReaderWriterWithSuffix(
189 out,
190 parcelObj,
191 argName,
192 isReader,
193 getJavaSuffix(),
194 "" /* extra */);
195}
196
Andreas Huber85eabdb2016-08-25 11:24:49 -0700197void Type::emitJavaFieldInitializer(
198 Formatter &out,
199 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700200 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700201 << " "
202 << fieldName
203 << ";\n";
204}
205
206void Type::emitJavaFieldReaderWriter(
207 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700208 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700209 const std::string &,
210 const std::string &,
211 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700212 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700213 bool) const {
214 CHECK(!"Should not be here");
215}
216
Andreas Huber881227d2016-08-02 14:20:21 -0700217void Type::handleError(Formatter &out, ErrorMode mode) const {
218 switch (mode) {
219 case ErrorMode_Ignore:
220 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700221 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700222 break;
223 }
224
225 case ErrorMode_Goto:
226 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700227 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700228 break;
229 }
230
231 case ErrorMode_Break:
232 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700233 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700234 break;
235 }
Andreas Huber737080b2016-08-02 15:38:04 -0700236
237 case ErrorMode_Return:
238 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700239 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700240 break;
241 }
Andreas Huber881227d2016-08-02 14:20:21 -0700242 }
243}
244
245void Type::handleError2(Formatter &out, ErrorMode mode) const {
246 switch (mode) {
247 case ErrorMode_Goto:
248 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700249 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700250 break;
251 }
Andreas Huber737080b2016-08-02 15:38:04 -0700252
Andreas Huber881227d2016-08-02 14:20:21 -0700253 case ErrorMode_Break:
254 {
255 out << "break;\n";
256 break;
257 }
Andreas Huber737080b2016-08-02 15:38:04 -0700258
Andreas Huber881227d2016-08-02 14:20:21 -0700259 case ErrorMode_Ignore:
260 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700261 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700262 break;
263 }
Andreas Huber737080b2016-08-02 15:38:04 -0700264
265 case ErrorMode_Return:
266 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700267 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700268 break;
269 }
Andreas Huber881227d2016-08-02 14:20:21 -0700270 }
271}
272
273void Type::emitReaderWriterEmbeddedForTypeName(
274 Formatter &out,
275 const std::string &name,
276 bool nameIsPointer,
277 const std::string &parcelObj,
278 bool parcelObjIsPointer,
279 bool isReader,
280 ErrorMode mode,
281 const std::string &parentName,
282 const std::string &offsetText,
283 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800284 const std::string &childName,
285 const std::string &funcNamespace) const {
286
287 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700288 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
289
290 const std::string parcelObjPointer =
291 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
292
Yifan Hong244e82d2016-11-11 11:13:57 -0800293 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700294 const std::string namePointer = nameIsPointer ? name : ("&" + name);
295
Iliyan Malchev549e2592016-08-10 08:59:12 -0700296 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700297
Yifan Hong244e82d2016-11-11 11:13:57 -0800298 if (!funcNamespace.empty()) {
299 out << funcNamespace << "::";
300 }
301
302 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
303
304 out.indent();
305 out.indent();
306
Andreas Huber881227d2016-08-02 14:20:21 -0700307 if (isReader) {
308 out << "const_cast<"
309 << typeName
310 << " *>("
311 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800312 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700313 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800314 out << nameDerefed
315 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700316 }
317
Andreas Huber881227d2016-08-02 14:20:21 -0700318 out << (isReader ? parcelObjDeref : parcelObjPointer)
319 << ",\n"
320 << parentName
321 << ",\n"
322 << offsetText;
323
324 if (!childName.empty()) {
325 out << ", &"
326 << childName;
327 }
328
329 out << ");\n\n";
330
331 out.unindent();
332 out.unindent();
333
334 handleError(out, mode);
335}
336
337status_t Type::emitTypeDeclarations(Formatter &) const {
338 return OK;
339}
340
Andreas Hubere3f769a2016-10-10 10:54:44 -0700341status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
342 return OK;
343}
344
Yifan Hong244e82d2016-11-11 11:13:57 -0800345status_t Type::emitGlobalHwDeclarations(Formatter &) const {
346 return OK;
347}
348
Andreas Huber881227d2016-08-02 14:20:21 -0700349status_t Type::emitTypeDefinitions(
350 Formatter &, const std::string) const {
351 return OK;
352}
353
Andreas Huber85eabdb2016-08-25 11:24:49 -0700354status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700355 return OK;
356}
357
Andreas Huber881227d2016-08-02 14:20:21 -0700358bool Type::needsEmbeddedReadWrite() const {
359 return false;
360}
361
Yifan Hongbf459bc2016-08-23 16:50:37 -0700362bool Type::needsResolveReferences() const {
363 return false;
364}
365
Andreas Huber881227d2016-08-02 14:20:21 -0700366bool Type::resultNeedsDeref() const {
367 return false;
368}
369
Yifan Hong3b320f82016-11-01 15:15:54 -0700370std::string Type::getCppStackType(bool specifyNamespaces) const {
371 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700372}
373
Yifan Hong3b320f82016-11-01 15:15:54 -0700374std::string Type::getCppResultType(bool specifyNamespaces) const {
375 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700376}
377
Yifan Hong3b320f82016-11-01 15:15:54 -0700378std::string Type::getCppArgumentType(bool specifyNamespaces) const {
379 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700380}
381
Andreas Huber2831d512016-08-15 09:33:47 -0700382void Type::emitJavaReaderWriterWithSuffix(
383 Formatter &out,
384 const std::string &parcelObj,
385 const std::string &argName,
386 bool isReader,
387 const std::string &suffix,
388 const std::string &extra) const {
389 out << parcelObj
390 << "."
391 << (isReader ? "read" : "write")
392 << suffix
393 << "(";
394
395 if (isReader) {
396 out << extra;
397 } else {
398 out << (extra.empty() ? "" : (extra + ", "));
399 out << argName;
400 }
401
402 out << ");\n";
403}
404
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700405status_t Type::emitVtsTypeDeclarations(Formatter &) const {
406 return OK;
407}
408
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700409status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700410 return emitVtsTypeDeclarations(out);
411}
412
Andreas Huber70a59e12016-08-16 12:57:01 -0700413bool Type::isJavaCompatible() const {
414 return true;
415}
416
Andreas Huber85eabdb2016-08-25 11:24:49 -0700417void Type::getAlignmentAndSize(size_t *, size_t *) const {
418 CHECK(!"Should not be here");
419}
420
Andreas Huber019d21d2016-10-03 12:59:47 -0700421void Type::appendToExportedTypesVector(
422 std::vector<const Type *> * /* exportedTypes */) const {
423}
424
Andreas Huber1c507272016-10-05 14:33:21 -0700425status_t Type::emitExportedHeader(
426 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700427 return OK;
428}
429
Yifan Hongbf459bc2016-08-23 16:50:37 -0700430////////////////////////////////////////
431
432TemplatedType::TemplatedType() : mElementType(nullptr) {
433}
434void TemplatedType::setElementType(Type *elementType) {
435 CHECK(mElementType == nullptr); // can only be set once.
436 mElementType = elementType;
437}
438
Andreas Huberc9410c72016-07-28 12:18:40 -0700439} // namespace android
440