blob: 585afc9ce9a26f0d573d1d5b26026fe7978086bd [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 {
Hridya Valsaraju9ab1e9e2017-03-10 07:52:23 -0800201 emitDumpWithMethod(out, streamName, "::android::hardware::toString", name);
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800202}
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 Honge45b5302017-02-22 10:49:07 -0800217void Type::emitJavaDump(
218 Formatter &out,
219 const std::string &streamName,
220 const std::string &name) const {
221 out << streamName << ".append(" << name << ");\n";
222}
223
Yifan Hong00f47172016-09-30 14:40:45 -0700224bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800225 return needsResolveReferences();
226}
227
228bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
229 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700230}
231
Andreas Huber881227d2016-08-02 14:20:21 -0700232void Type::emitReaderWriterEmbedded(
233 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700234 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700235 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700236 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700237 bool,
238 const std::string &,
239 bool,
240 bool,
241 ErrorMode,
242 const std::string &,
243 const std::string &) const {
244 CHECK(!"Should not be here");
245}
246
Andreas Huber2831d512016-08-15 09:33:47 -0700247void Type::emitJavaReaderWriter(
248 Formatter &out,
249 const std::string &parcelObj,
250 const std::string &argName,
251 bool isReader) const {
252 emitJavaReaderWriterWithSuffix(
253 out,
254 parcelObj,
255 argName,
256 isReader,
257 getJavaSuffix(),
258 "" /* extra */);
259}
260
Andreas Huber85eabdb2016-08-25 11:24:49 -0700261void Type::emitJavaFieldInitializer(
262 Formatter &out,
263 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700264 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700265 << " "
266 << fieldName
267 << ";\n";
268}
269
270void Type::emitJavaFieldReaderWriter(
271 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700272 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700273 const std::string &,
274 const std::string &,
275 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700276 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700277 bool) const {
278 CHECK(!"Should not be here");
279}
280
Andreas Huber881227d2016-08-02 14:20:21 -0700281void Type::handleError(Formatter &out, ErrorMode mode) const {
282 switch (mode) {
283 case ErrorMode_Ignore:
284 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700285 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700286 break;
287 }
288
289 case ErrorMode_Goto:
290 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700291 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700292 break;
293 }
294
295 case ErrorMode_Break:
296 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700297 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700298 break;
299 }
Andreas Huber737080b2016-08-02 15:38:04 -0700300
301 case ErrorMode_Return:
302 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700303 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700304 break;
305 }
Andreas Huber881227d2016-08-02 14:20:21 -0700306 }
307}
308
Andreas Huber881227d2016-08-02 14:20:21 -0700309void Type::emitReaderWriterEmbeddedForTypeName(
310 Formatter &out,
311 const std::string &name,
312 bool nameIsPointer,
313 const std::string &parcelObj,
314 bool parcelObjIsPointer,
315 bool isReader,
316 ErrorMode mode,
317 const std::string &parentName,
318 const std::string &offsetText,
319 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800320 const std::string &childName,
321 const std::string &funcNamespace) const {
322
323 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700324 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
325
326 const std::string parcelObjPointer =
327 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
328
Yifan Hong244e82d2016-11-11 11:13:57 -0800329 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700330 const std::string namePointer = nameIsPointer ? name : ("&" + name);
331
Iliyan Malchev549e2592016-08-10 08:59:12 -0700332 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700333
Yifan Hong244e82d2016-11-11 11:13:57 -0800334 if (!funcNamespace.empty()) {
335 out << funcNamespace << "::";
336 }
337
338 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
339
340 out.indent();
341 out.indent();
342
Andreas Huber881227d2016-08-02 14:20:21 -0700343 if (isReader) {
344 out << "const_cast<"
345 << typeName
346 << " *>("
347 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800348 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700349 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800350 out << nameDerefed
351 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700352 }
353
Andreas Huber881227d2016-08-02 14:20:21 -0700354 out << (isReader ? parcelObjDeref : parcelObjPointer)
355 << ",\n"
356 << parentName
357 << ",\n"
358 << offsetText;
359
360 if (!childName.empty()) {
361 out << ", &"
362 << childName;
363 }
364
365 out << ");\n\n";
366
367 out.unindent();
368 out.unindent();
369
370 handleError(out, mode);
371}
372
373status_t Type::emitTypeDeclarations(Formatter &) const {
374 return OK;
375}
376
Andreas Hubere3f769a2016-10-10 10:54:44 -0700377status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
378 return OK;
379}
380
Yifan Hong244e82d2016-11-11 11:13:57 -0800381status_t Type::emitGlobalHwDeclarations(Formatter &) const {
382 return OK;
383}
384
Andreas Huber881227d2016-08-02 14:20:21 -0700385status_t Type::emitTypeDefinitions(
386 Formatter &, const std::string) const {
387 return OK;
388}
389
Andreas Huber85eabdb2016-08-25 11:24:49 -0700390status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700391 return OK;
392}
393
Andreas Huber881227d2016-08-02 14:20:21 -0700394bool Type::needsEmbeddedReadWrite() const {
395 return false;
396}
397
Yifan Hongbf459bc2016-08-23 16:50:37 -0700398bool Type::needsResolveReferences() const {
399 return false;
400}
401
Andreas Huber881227d2016-08-02 14:20:21 -0700402bool Type::resultNeedsDeref() const {
403 return false;
404}
405
Yifan Hong3b320f82016-11-01 15:15:54 -0700406std::string Type::getCppStackType(bool specifyNamespaces) const {
407 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700408}
409
Yifan Hong3b320f82016-11-01 15:15:54 -0700410std::string Type::getCppResultType(bool specifyNamespaces) const {
411 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700412}
413
Yifan Hong3b320f82016-11-01 15:15:54 -0700414std::string Type::getCppArgumentType(bool specifyNamespaces) const {
415 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700416}
417
Andreas Huber2831d512016-08-15 09:33:47 -0700418void Type::emitJavaReaderWriterWithSuffix(
419 Formatter &out,
420 const std::string &parcelObj,
421 const std::string &argName,
422 bool isReader,
423 const std::string &suffix,
424 const std::string &extra) const {
425 out << parcelObj
426 << "."
427 << (isReader ? "read" : "write")
428 << suffix
429 << "(";
430
431 if (isReader) {
432 out << extra;
433 } else {
434 out << (extra.empty() ? "" : (extra + ", "));
435 out << argName;
436 }
437
438 out << ");\n";
439}
440
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700441status_t Type::emitVtsTypeDeclarations(Formatter &) const {
442 return OK;
443}
444
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700445status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700446 return emitVtsTypeDeclarations(out);
447}
448
Andreas Huber70a59e12016-08-16 12:57:01 -0700449bool Type::isJavaCompatible() const {
450 return true;
451}
452
Andreas Huber85eabdb2016-08-25 11:24:49 -0700453void Type::getAlignmentAndSize(size_t *, size_t *) const {
454 CHECK(!"Should not be here");
455}
456
Andreas Huber019d21d2016-10-03 12:59:47 -0700457void Type::appendToExportedTypesVector(
458 std::vector<const Type *> * /* exportedTypes */) const {
459}
460
Andreas Huber1c507272016-10-05 14:33:21 -0700461status_t Type::emitExportedHeader(
462 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700463 return OK;
464}
465
Yifan Hongbf459bc2016-08-23 16:50:37 -0700466////////////////////////////////////////
467
468TemplatedType::TemplatedType() : mElementType(nullptr) {
469}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800470
Yifan Hongbf459bc2016-08-23 16:50:37 -0700471void TemplatedType::setElementType(Type *elementType) {
472 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800473 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700474 mElementType = elementType;
475}
476
Yifan Hongabf73ee2016-12-05 18:47:00 -0800477Type *TemplatedType::getElementType() const {
478 return mElementType;
479}
480
481bool TemplatedType::isTemplatedType() const {
482 return true;
483}
484
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800485status_t TemplatedType::emitVtsTypeDeclarations(Formatter &out) const {
486 out << "type: " << getVtsType() << "\n";
487 out << getVtsValueName() << ": {\n";
488 out.indent();
489 status_t err = mElementType->emitVtsTypeDeclarations(out);
490 if (err != OK) {
491 return err;
492 }
493 out.unindent();
494 out << "}\n";
495 return OK;
496}
497
498status_t TemplatedType::emitVtsAttributeType(Formatter &out) const {
499 out << "type: " << getVtsType() << "\n";
500 out << getVtsValueName() << ": {\n";
501 out.indent();
502 status_t status = mElementType->emitVtsAttributeType(out);
503 if (status != OK) {
504 return status;
505 }
506 out.unindent();
507 out << "}\n";
508 return OK;
509}
Andreas Huberc9410c72016-07-28 12:18:40 -0700510} // namespace android
511