blob: 71279c007b53d7330877b04147fb41bfadff1ffb [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
Yifan Hongabf73ee2016-12-05 18:47:00 -080049bool Type::isScalar() const {
50 return false;
51}
52
53bool Type::isString() const {
54 return false;
55}
56
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070057bool Type::isEnum() const {
58 return false;
59}
60
Yifan Hongabf73ee2016-12-05 18:47:00 -080061bool Type::isBitField() const {
62 return false;
63}
64
65bool Type::isHandle() const {
66 return false;
67}
68
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070069bool Type::isTypeDef() const {
70 return false;
71}
72
Andreas Huber295ad302016-08-16 11:35:00 -070073bool Type::isBinder() const {
74 return false;
75}
76
Andreas Huber39fa7182016-08-19 14:27:33 -070077bool Type::isNamedType() const {
78 return false;
79}
80
Andreas Huberf630bc82016-09-09 14:52:25 -070081bool Type::isCompoundType() const {
82 return false;
83}
84
Andreas Huber709b62d2016-09-19 11:21:18 -070085bool Type::isArray() const {
86 return false;
87}
88
89bool Type::isVector() const {
90 return false;
91}
92
Yifan Hongabf73ee2016-12-05 18:47:00 -080093bool Type::isTemplatedType() const {
94 return false;
95}
96
Martijn Coenen99e6beb2016-12-01 15:48:42 +010097bool Type::isPointer() const {
98 return false;
99}
100
Steven Moreland30bb6a82016-11-30 09:18:34 -0800101std::string Type::typeName() const {
102 return "";
103}
104
Andreas Huber737080b2016-08-02 15:38:04 -0700105const ScalarType *Type::resolveToScalarType() const {
106 return NULL;
107}
108
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700109bool Type::isValidEnumStorageType() const {
110 const ScalarType *scalarType = resolveToScalarType();
111
112 if (scalarType == NULL) {
113 return false;
114 }
115
116 return scalarType->isValidEnumStorageType();
117}
118
Steven Moreland9df52442016-12-12 08:51:14 -0800119bool Type::isElidableType() const {
120 return false;
121}
122
Yifan Hongc6752dc2016-12-20 14:00:14 -0800123bool Type::canCheckEquality() const {
124 return false;
125}
126
Yifan Hong3b320f82016-11-01 15:15:54 -0700127std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700128 CHECK(!"Should not be here");
129 return std::string();
130}
131
Yifan Hong3b320f82016-11-01 15:15:54 -0700132std::string Type::decorateCppName(
133 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
134 return getCppType(mode, specifyNamespaces) + " " + name;
135}
136
Yifan Hong4ed13472016-11-02 10:44:11 -0700137std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700138 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700139 return std::string();
140}
141
Andreas Huber85eabdb2016-08-25 11:24:49 -0700142std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700143 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700144}
145
Andreas Huber2831d512016-08-15 09:33:47 -0700146std::string Type::getJavaSuffix() const {
147 CHECK(!"Should not be here");
148 return std::string();
149}
150
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700151std::string Type::getVtsType() const {
152 CHECK(!"Should not be here");
153 return std::string();
154}
155
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800156std::string Type::getVtsValueName() const {
157 CHECK(!"Should not be here");
158 return std::string();
159}
160
Andreas Huber881227d2016-08-02 14:20:21 -0700161void Type::emitReaderWriter(
162 Formatter &,
163 const std::string &,
164 const std::string &,
165 bool,
166 bool,
167 ErrorMode) const {
168 CHECK(!"Should not be here");
169}
170
Yifan Hongbf459bc2016-08-23 16:50:37 -0700171void Type::emitResolveReferences(
172 Formatter &,
173 const std::string &,
174 bool,
175 const std::string &,
176 bool,
177 bool,
178 ErrorMode) const {
179 CHECK(!"Should not be here");
180}
181
182void Type::emitResolveReferencesEmbedded(
183 Formatter &,
184 size_t,
185 const std::string &,
186 const std::string &,
187 bool,
188 const std::string &,
189 bool,
190 bool,
191 ErrorMode,
192 const std::string &,
193 const std::string &) const {
194 CHECK(!"Should not be here");
195}
196
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800197void Type::emitDump(
198 Formatter &out,
199 const std::string &streamName,
200 const std::string &name) const {
201 emitDumpWithMethod(out, streamName, "::android::hardware::details::toString", name);
202}
203
204void Type::emitDumpWithMethod(
205 Formatter &out,
206 const std::string &streamName,
207 const std::string &methodName,
208 const std::string &name) const {
209 out << streamName
210 << " += "
211 << methodName
212 << "("
213 << name
214 << ");\n";
215}
216
Yifan Hong00f47172016-09-30 14:40:45 -0700217bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800218 return needsResolveReferences();
219}
220
221bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
222 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700223}
224
Andreas Huber881227d2016-08-02 14:20:21 -0700225void Type::emitReaderWriterEmbedded(
226 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700227 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700228 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700229 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700230 bool,
231 const std::string &,
232 bool,
233 bool,
234 ErrorMode,
235 const std::string &,
236 const std::string &) const {
237 CHECK(!"Should not be here");
238}
239
Andreas Huber2831d512016-08-15 09:33:47 -0700240void Type::emitJavaReaderWriter(
241 Formatter &out,
242 const std::string &parcelObj,
243 const std::string &argName,
244 bool isReader) const {
245 emitJavaReaderWriterWithSuffix(
246 out,
247 parcelObj,
248 argName,
249 isReader,
250 getJavaSuffix(),
251 "" /* extra */);
252}
253
Andreas Huber85eabdb2016-08-25 11:24:49 -0700254void Type::emitJavaFieldInitializer(
255 Formatter &out,
256 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700257 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700258 << " "
259 << fieldName
260 << ";\n";
261}
262
263void Type::emitJavaFieldReaderWriter(
264 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700265 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700266 const std::string &,
267 const std::string &,
268 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700269 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700270 bool) const {
271 CHECK(!"Should not be here");
272}
273
Andreas Huber881227d2016-08-02 14:20:21 -0700274void Type::handleError(Formatter &out, ErrorMode mode) const {
275 switch (mode) {
276 case ErrorMode_Ignore:
277 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700278 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700279 break;
280 }
281
282 case ErrorMode_Goto:
283 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700284 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700285 break;
286 }
287
288 case ErrorMode_Break:
289 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700290 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700291 break;
292 }
Andreas Huber737080b2016-08-02 15:38:04 -0700293
294 case ErrorMode_Return:
295 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700296 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700297 break;
298 }
Andreas Huber881227d2016-08-02 14:20:21 -0700299 }
300}
301
Andreas Huber881227d2016-08-02 14:20:21 -0700302void Type::emitReaderWriterEmbeddedForTypeName(
303 Formatter &out,
304 const std::string &name,
305 bool nameIsPointer,
306 const std::string &parcelObj,
307 bool parcelObjIsPointer,
308 bool isReader,
309 ErrorMode mode,
310 const std::string &parentName,
311 const std::string &offsetText,
312 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800313 const std::string &childName,
314 const std::string &funcNamespace) const {
315
316 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700317 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
318
319 const std::string parcelObjPointer =
320 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
321
Yifan Hong244e82d2016-11-11 11:13:57 -0800322 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700323 const std::string namePointer = nameIsPointer ? name : ("&" + name);
324
Iliyan Malchev549e2592016-08-10 08:59:12 -0700325 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700326
Yifan Hong244e82d2016-11-11 11:13:57 -0800327 if (!funcNamespace.empty()) {
328 out << funcNamespace << "::";
329 }
330
331 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
332
333 out.indent();
334 out.indent();
335
Andreas Huber881227d2016-08-02 14:20:21 -0700336 if (isReader) {
337 out << "const_cast<"
338 << typeName
339 << " *>("
340 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800341 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700342 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800343 out << nameDerefed
344 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700345 }
346
Andreas Huber881227d2016-08-02 14:20:21 -0700347 out << (isReader ? parcelObjDeref : parcelObjPointer)
348 << ",\n"
349 << parentName
350 << ",\n"
351 << offsetText;
352
353 if (!childName.empty()) {
354 out << ", &"
355 << childName;
356 }
357
358 out << ");\n\n";
359
360 out.unindent();
361 out.unindent();
362
363 handleError(out, mode);
364}
365
366status_t Type::emitTypeDeclarations(Formatter &) const {
367 return OK;
368}
369
Andreas Hubere3f769a2016-10-10 10:54:44 -0700370status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
371 return OK;
372}
373
Yifan Hong244e82d2016-11-11 11:13:57 -0800374status_t Type::emitGlobalHwDeclarations(Formatter &) const {
375 return OK;
376}
377
Andreas Huber881227d2016-08-02 14:20:21 -0700378status_t Type::emitTypeDefinitions(
379 Formatter &, const std::string) const {
380 return OK;
381}
382
Andreas Huber85eabdb2016-08-25 11:24:49 -0700383status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700384 return OK;
385}
386
Andreas Huber881227d2016-08-02 14:20:21 -0700387bool Type::needsEmbeddedReadWrite() const {
388 return false;
389}
390
Yifan Hongbf459bc2016-08-23 16:50:37 -0700391bool Type::needsResolveReferences() const {
392 return false;
393}
394
Andreas Huber881227d2016-08-02 14:20:21 -0700395bool Type::resultNeedsDeref() const {
396 return false;
397}
398
Yifan Hong3b320f82016-11-01 15:15:54 -0700399std::string Type::getCppStackType(bool specifyNamespaces) const {
400 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700401}
402
Yifan Hong3b320f82016-11-01 15:15:54 -0700403std::string Type::getCppResultType(bool specifyNamespaces) const {
404 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700405}
406
Yifan Hong3b320f82016-11-01 15:15:54 -0700407std::string Type::getCppArgumentType(bool specifyNamespaces) const {
408 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700409}
410
Andreas Huber2831d512016-08-15 09:33:47 -0700411void Type::emitJavaReaderWriterWithSuffix(
412 Formatter &out,
413 const std::string &parcelObj,
414 const std::string &argName,
415 bool isReader,
416 const std::string &suffix,
417 const std::string &extra) const {
418 out << parcelObj
419 << "."
420 << (isReader ? "read" : "write")
421 << suffix
422 << "(";
423
424 if (isReader) {
425 out << extra;
426 } else {
427 out << (extra.empty() ? "" : (extra + ", "));
428 out << argName;
429 }
430
431 out << ");\n";
432}
433
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700434status_t Type::emitVtsTypeDeclarations(Formatter &) const {
435 return OK;
436}
437
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700438status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700439 return emitVtsTypeDeclarations(out);
440}
441
Andreas Huber70a59e12016-08-16 12:57:01 -0700442bool Type::isJavaCompatible() const {
443 return true;
444}
445
Andreas Huber85eabdb2016-08-25 11:24:49 -0700446void Type::getAlignmentAndSize(size_t *, size_t *) const {
447 CHECK(!"Should not be here");
448}
449
Andreas Huber019d21d2016-10-03 12:59:47 -0700450void Type::appendToExportedTypesVector(
451 std::vector<const Type *> * /* exportedTypes */) const {
452}
453
Andreas Huber1c507272016-10-05 14:33:21 -0700454status_t Type::emitExportedHeader(
455 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700456 return OK;
457}
458
Yifan Hongbf459bc2016-08-23 16:50:37 -0700459////////////////////////////////////////
460
461TemplatedType::TemplatedType() : mElementType(nullptr) {
462}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800463
Yifan Hongbf459bc2016-08-23 16:50:37 -0700464void TemplatedType::setElementType(Type *elementType) {
465 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800466 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700467 mElementType = elementType;
468}
469
Yifan Hongabf73ee2016-12-05 18:47:00 -0800470Type *TemplatedType::getElementType() const {
471 return mElementType;
472}
473
474bool TemplatedType::isTemplatedType() const {
475 return true;
476}
477
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800478status_t TemplatedType::emitVtsTypeDeclarations(Formatter &out) const {
479 out << "type: " << getVtsType() << "\n";
480 out << getVtsValueName() << ": {\n";
481 out.indent();
482 status_t err = mElementType->emitVtsTypeDeclarations(out);
483 if (err != OK) {
484 return err;
485 }
486 out.unindent();
487 out << "}\n";
488 return OK;
489}
490
491status_t TemplatedType::emitVtsAttributeType(Formatter &out) const {
492 out << "type: " << getVtsType() << "\n";
493 out << getVtsValueName() << ": {\n";
494 out.indent();
495 status_t status = mElementType->emitVtsAttributeType(out);
496 if (status != OK) {
497 return status;
498 }
499 out.unindent();
500 out << "}\n";
501 return OK;
502}
Andreas Huberc9410c72016-07-28 12:18:40 -0700503} // namespace android
504