blob: 54135e09ae6196470ff152adc62829c1aeac4faf [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
Andreas Huber881227d2016-08-02 14:20:21 -0700156void Type::emitReaderWriter(
157 Formatter &,
158 const std::string &,
159 const std::string &,
160 bool,
161 bool,
162 ErrorMode) const {
163 CHECK(!"Should not be here");
164}
165
Yifan Hongbf459bc2016-08-23 16:50:37 -0700166void Type::emitResolveReferences(
167 Formatter &,
168 const std::string &,
169 bool,
170 const std::string &,
171 bool,
172 bool,
173 ErrorMode) const {
174 CHECK(!"Should not be here");
175}
176
177void Type::emitResolveReferencesEmbedded(
178 Formatter &,
179 size_t,
180 const std::string &,
181 const std::string &,
182 bool,
183 const std::string &,
184 bool,
185 bool,
186 ErrorMode,
187 const std::string &,
188 const std::string &) const {
189 CHECK(!"Should not be here");
190}
191
Yifan Hong00f47172016-09-30 14:40:45 -0700192bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800193 return needsResolveReferences();
194}
195
196bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
197 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700198}
199
Andreas Huber881227d2016-08-02 14:20:21 -0700200void Type::emitReaderWriterEmbedded(
201 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700202 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700203 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700204 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700205 bool,
206 const std::string &,
207 bool,
208 bool,
209 ErrorMode,
210 const std::string &,
211 const std::string &) const {
212 CHECK(!"Should not be here");
213}
214
Andreas Huber2831d512016-08-15 09:33:47 -0700215void Type::emitJavaReaderWriter(
216 Formatter &out,
217 const std::string &parcelObj,
218 const std::string &argName,
219 bool isReader) const {
220 emitJavaReaderWriterWithSuffix(
221 out,
222 parcelObj,
223 argName,
224 isReader,
225 getJavaSuffix(),
226 "" /* extra */);
227}
228
Andreas Huber85eabdb2016-08-25 11:24:49 -0700229void Type::emitJavaFieldInitializer(
230 Formatter &out,
231 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700232 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700233 << " "
234 << fieldName
235 << ";\n";
236}
237
238void Type::emitJavaFieldReaderWriter(
239 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700240 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700241 const std::string &,
242 const std::string &,
243 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700244 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700245 bool) const {
246 CHECK(!"Should not be here");
247}
248
Andreas Huber881227d2016-08-02 14:20:21 -0700249void Type::handleError(Formatter &out, ErrorMode mode) const {
250 switch (mode) {
251 case ErrorMode_Ignore:
252 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700253 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700254 break;
255 }
256
257 case ErrorMode_Goto:
258 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700259 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700260 break;
261 }
262
263 case ErrorMode_Break:
264 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700265 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700266 break;
267 }
Andreas Huber737080b2016-08-02 15:38:04 -0700268
269 case ErrorMode_Return:
270 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700271 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700272 break;
273 }
Andreas Huber881227d2016-08-02 14:20:21 -0700274 }
275}
276
277void Type::handleError2(Formatter &out, ErrorMode mode) const {
278 switch (mode) {
279 case ErrorMode_Goto:
280 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700281 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700282 break;
283 }
Andreas Huber737080b2016-08-02 15:38:04 -0700284
Andreas Huber881227d2016-08-02 14:20:21 -0700285 case ErrorMode_Break:
286 {
287 out << "break;\n";
288 break;
289 }
Andreas Huber737080b2016-08-02 15:38:04 -0700290
Andreas Huber881227d2016-08-02 14:20:21 -0700291 case ErrorMode_Ignore:
292 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700293 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700294 break;
295 }
Andreas Huber737080b2016-08-02 15:38:04 -0700296
297 case ErrorMode_Return:
298 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700299 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700300 break;
301 }
Andreas Huber881227d2016-08-02 14:20:21 -0700302 }
303}
304
305void Type::emitReaderWriterEmbeddedForTypeName(
306 Formatter &out,
307 const std::string &name,
308 bool nameIsPointer,
309 const std::string &parcelObj,
310 bool parcelObjIsPointer,
311 bool isReader,
312 ErrorMode mode,
313 const std::string &parentName,
314 const std::string &offsetText,
315 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800316 const std::string &childName,
317 const std::string &funcNamespace) const {
318
319 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700320 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
321
322 const std::string parcelObjPointer =
323 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
324
Yifan Hong244e82d2016-11-11 11:13:57 -0800325 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700326 const std::string namePointer = nameIsPointer ? name : ("&" + name);
327
Iliyan Malchev549e2592016-08-10 08:59:12 -0700328 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700329
Yifan Hong244e82d2016-11-11 11:13:57 -0800330 if (!funcNamespace.empty()) {
331 out << funcNamespace << "::";
332 }
333
334 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
335
336 out.indent();
337 out.indent();
338
Andreas Huber881227d2016-08-02 14:20:21 -0700339 if (isReader) {
340 out << "const_cast<"
341 << typeName
342 << " *>("
343 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800344 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700345 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800346 out << nameDerefed
347 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700348 }
349
Andreas Huber881227d2016-08-02 14:20:21 -0700350 out << (isReader ? parcelObjDeref : parcelObjPointer)
351 << ",\n"
352 << parentName
353 << ",\n"
354 << offsetText;
355
356 if (!childName.empty()) {
357 out << ", &"
358 << childName;
359 }
360
361 out << ");\n\n";
362
363 out.unindent();
364 out.unindent();
365
366 handleError(out, mode);
367}
368
369status_t Type::emitTypeDeclarations(Formatter &) const {
370 return OK;
371}
372
Andreas Hubere3f769a2016-10-10 10:54:44 -0700373status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
374 return OK;
375}
376
Yifan Hong244e82d2016-11-11 11:13:57 -0800377status_t Type::emitGlobalHwDeclarations(Formatter &) const {
378 return OK;
379}
380
Andreas Huber881227d2016-08-02 14:20:21 -0700381status_t Type::emitTypeDefinitions(
382 Formatter &, const std::string) const {
383 return OK;
384}
385
Andreas Huber85eabdb2016-08-25 11:24:49 -0700386status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700387 return OK;
388}
389
Andreas Huber881227d2016-08-02 14:20:21 -0700390bool Type::needsEmbeddedReadWrite() const {
391 return false;
392}
393
Yifan Hongbf459bc2016-08-23 16:50:37 -0700394bool Type::needsResolveReferences() const {
395 return false;
396}
397
Andreas Huber881227d2016-08-02 14:20:21 -0700398bool Type::resultNeedsDeref() const {
399 return false;
400}
401
Yifan Hong3b320f82016-11-01 15:15:54 -0700402std::string Type::getCppStackType(bool specifyNamespaces) const {
403 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700404}
405
Yifan Hong3b320f82016-11-01 15:15:54 -0700406std::string Type::getCppResultType(bool specifyNamespaces) const {
407 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700408}
409
Yifan Hong3b320f82016-11-01 15:15:54 -0700410std::string Type::getCppArgumentType(bool specifyNamespaces) const {
411 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700412}
413
Andreas Huber2831d512016-08-15 09:33:47 -0700414void Type::emitJavaReaderWriterWithSuffix(
415 Formatter &out,
416 const std::string &parcelObj,
417 const std::string &argName,
418 bool isReader,
419 const std::string &suffix,
420 const std::string &extra) const {
421 out << parcelObj
422 << "."
423 << (isReader ? "read" : "write")
424 << suffix
425 << "(";
426
427 if (isReader) {
428 out << extra;
429 } else {
430 out << (extra.empty() ? "" : (extra + ", "));
431 out << argName;
432 }
433
434 out << ");\n";
435}
436
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700437status_t Type::emitVtsTypeDeclarations(Formatter &) const {
438 return OK;
439}
440
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700441status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700442 return emitVtsTypeDeclarations(out);
443}
444
Andreas Huber70a59e12016-08-16 12:57:01 -0700445bool Type::isJavaCompatible() const {
446 return true;
447}
448
Andreas Huber85eabdb2016-08-25 11:24:49 -0700449void Type::getAlignmentAndSize(size_t *, size_t *) const {
450 CHECK(!"Should not be here");
451}
452
Andreas Huber019d21d2016-10-03 12:59:47 -0700453void Type::appendToExportedTypesVector(
454 std::vector<const Type *> * /* exportedTypes */) const {
455}
456
Andreas Huber1c507272016-10-05 14:33:21 -0700457status_t Type::emitExportedHeader(
458 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700459 return OK;
460}
461
Yifan Hongbf459bc2016-08-23 16:50:37 -0700462////////////////////////////////////////
463
464TemplatedType::TemplatedType() : mElementType(nullptr) {
465}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800466
Yifan Hongbf459bc2016-08-23 16:50:37 -0700467void TemplatedType::setElementType(Type *elementType) {
468 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800469 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700470 mElementType = elementType;
471}
472
Yifan Hongabf73ee2016-12-05 18:47:00 -0800473Type *TemplatedType::getElementType() const {
474 return mElementType;
475}
476
477bool TemplatedType::isTemplatedType() const {
478 return true;
479}
480
Andreas Huberc9410c72016-07-28 12:18:40 -0700481} // namespace android
482