blob: 107e502121e118b5e6628e961dbfd15fd8f1cd03 [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 Hong3b320f82016-11-01 15:15:54 -0700123std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700124 CHECK(!"Should not be here");
125 return std::string();
126}
127
Yifan Hong3b320f82016-11-01 15:15:54 -0700128std::string Type::decorateCppName(
129 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
130 return getCppType(mode, specifyNamespaces) + " " + name;
131}
132
Yifan Hong4ed13472016-11-02 10:44:11 -0700133std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700134 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700135 return std::string();
136}
137
Andreas Huber85eabdb2016-08-25 11:24:49 -0700138std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700139 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700140}
141
Andreas Huber2831d512016-08-15 09:33:47 -0700142std::string Type::getJavaSuffix() const {
143 CHECK(!"Should not be here");
144 return std::string();
145}
146
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700147std::string Type::getVtsType() const {
148 CHECK(!"Should not be here");
149 return std::string();
150}
151
Andreas Huber881227d2016-08-02 14:20:21 -0700152void Type::emitReaderWriter(
153 Formatter &,
154 const std::string &,
155 const std::string &,
156 bool,
157 bool,
158 ErrorMode) const {
159 CHECK(!"Should not be here");
160}
161
Yifan Hongbf459bc2016-08-23 16:50:37 -0700162void Type::emitResolveReferences(
163 Formatter &,
164 const std::string &,
165 bool,
166 const std::string &,
167 bool,
168 bool,
169 ErrorMode) const {
170 CHECK(!"Should not be here");
171}
172
173void Type::emitResolveReferencesEmbedded(
174 Formatter &,
175 size_t,
176 const std::string &,
177 const std::string &,
178 bool,
179 const std::string &,
180 bool,
181 bool,
182 ErrorMode,
183 const std::string &,
184 const std::string &) const {
185 CHECK(!"Should not be here");
186}
187
Yifan Hong00f47172016-09-30 14:40:45 -0700188bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800189 return needsResolveReferences();
190}
191
192bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
193 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700194}
195
Andreas Huber881227d2016-08-02 14:20:21 -0700196void Type::emitReaderWriterEmbedded(
197 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700198 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700199 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700200 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700201 bool,
202 const std::string &,
203 bool,
204 bool,
205 ErrorMode,
206 const std::string &,
207 const std::string &) const {
208 CHECK(!"Should not be here");
209}
210
Andreas Huber2831d512016-08-15 09:33:47 -0700211void Type::emitJavaReaderWriter(
212 Formatter &out,
213 const std::string &parcelObj,
214 const std::string &argName,
215 bool isReader) const {
216 emitJavaReaderWriterWithSuffix(
217 out,
218 parcelObj,
219 argName,
220 isReader,
221 getJavaSuffix(),
222 "" /* extra */);
223}
224
Andreas Huber85eabdb2016-08-25 11:24:49 -0700225void Type::emitJavaFieldInitializer(
226 Formatter &out,
227 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700228 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700229 << " "
230 << fieldName
231 << ";\n";
232}
233
234void Type::emitJavaFieldReaderWriter(
235 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700236 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700237 const std::string &,
238 const std::string &,
239 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700240 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700241 bool) const {
242 CHECK(!"Should not be here");
243}
244
Andreas Huber881227d2016-08-02 14:20:21 -0700245void Type::handleError(Formatter &out, ErrorMode mode) const {
246 switch (mode) {
247 case ErrorMode_Ignore:
248 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700249 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700250 break;
251 }
252
253 case ErrorMode_Goto:
254 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700255 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700256 break;
257 }
258
259 case ErrorMode_Break:
260 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700261 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
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 << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700268 break;
269 }
Andreas Huber881227d2016-08-02 14:20:21 -0700270 }
271}
272
273void Type::handleError2(Formatter &out, ErrorMode mode) const {
274 switch (mode) {
275 case ErrorMode_Goto:
276 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700277 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700278 break;
279 }
Andreas Huber737080b2016-08-02 15:38:04 -0700280
Andreas Huber881227d2016-08-02 14:20:21 -0700281 case ErrorMode_Break:
282 {
283 out << "break;\n";
284 break;
285 }
Andreas Huber737080b2016-08-02 15:38:04 -0700286
Andreas Huber881227d2016-08-02 14:20:21 -0700287 case ErrorMode_Ignore:
288 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700289 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700290 break;
291 }
Andreas Huber737080b2016-08-02 15:38:04 -0700292
293 case ErrorMode_Return:
294 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700295 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700296 break;
297 }
Andreas Huber881227d2016-08-02 14:20:21 -0700298 }
299}
300
301void Type::emitReaderWriterEmbeddedForTypeName(
302 Formatter &out,
303 const std::string &name,
304 bool nameIsPointer,
305 const std::string &parcelObj,
306 bool parcelObjIsPointer,
307 bool isReader,
308 ErrorMode mode,
309 const std::string &parentName,
310 const std::string &offsetText,
311 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800312 const std::string &childName,
313 const std::string &funcNamespace) const {
314
315 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700316 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
317
318 const std::string parcelObjPointer =
319 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
320
Yifan Hong244e82d2016-11-11 11:13:57 -0800321 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700322 const std::string namePointer = nameIsPointer ? name : ("&" + name);
323
Iliyan Malchev549e2592016-08-10 08:59:12 -0700324 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700325
Yifan Hong244e82d2016-11-11 11:13:57 -0800326 if (!funcNamespace.empty()) {
327 out << funcNamespace << "::";
328 }
329
330 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
331
332 out.indent();
333 out.indent();
334
Andreas Huber881227d2016-08-02 14:20:21 -0700335 if (isReader) {
336 out << "const_cast<"
337 << typeName
338 << " *>("
339 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800340 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700341 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800342 out << nameDerefed
343 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700344 }
345
Andreas Huber881227d2016-08-02 14:20:21 -0700346 out << (isReader ? parcelObjDeref : parcelObjPointer)
347 << ",\n"
348 << parentName
349 << ",\n"
350 << offsetText;
351
352 if (!childName.empty()) {
353 out << ", &"
354 << childName;
355 }
356
357 out << ");\n\n";
358
359 out.unindent();
360 out.unindent();
361
362 handleError(out, mode);
363}
364
365status_t Type::emitTypeDeclarations(Formatter &) const {
366 return OK;
367}
368
Andreas Hubere3f769a2016-10-10 10:54:44 -0700369status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
370 return OK;
371}
372
Yifan Hong244e82d2016-11-11 11:13:57 -0800373status_t Type::emitGlobalHwDeclarations(Formatter &) const {
374 return OK;
375}
376
Andreas Huber881227d2016-08-02 14:20:21 -0700377status_t Type::emitTypeDefinitions(
378 Formatter &, const std::string) const {
379 return OK;
380}
381
Andreas Huber85eabdb2016-08-25 11:24:49 -0700382status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700383 return OK;
384}
385
Andreas Huber881227d2016-08-02 14:20:21 -0700386bool Type::needsEmbeddedReadWrite() const {
387 return false;
388}
389
Yifan Hongbf459bc2016-08-23 16:50:37 -0700390bool Type::needsResolveReferences() const {
391 return false;
392}
393
Andreas Huber881227d2016-08-02 14:20:21 -0700394bool Type::resultNeedsDeref() const {
395 return false;
396}
397
Yifan Hong3b320f82016-11-01 15:15:54 -0700398std::string Type::getCppStackType(bool specifyNamespaces) const {
399 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700400}
401
Yifan Hong3b320f82016-11-01 15:15:54 -0700402std::string Type::getCppResultType(bool specifyNamespaces) const {
403 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700404}
405
Yifan Hong3b320f82016-11-01 15:15:54 -0700406std::string Type::getCppArgumentType(bool specifyNamespaces) const {
407 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700408}
409
Andreas Huber2831d512016-08-15 09:33:47 -0700410void Type::emitJavaReaderWriterWithSuffix(
411 Formatter &out,
412 const std::string &parcelObj,
413 const std::string &argName,
414 bool isReader,
415 const std::string &suffix,
416 const std::string &extra) const {
417 out << parcelObj
418 << "."
419 << (isReader ? "read" : "write")
420 << suffix
421 << "(";
422
423 if (isReader) {
424 out << extra;
425 } else {
426 out << (extra.empty() ? "" : (extra + ", "));
427 out << argName;
428 }
429
430 out << ");\n";
431}
432
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700433status_t Type::emitVtsTypeDeclarations(Formatter &) const {
434 return OK;
435}
436
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700437status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700438 return emitVtsTypeDeclarations(out);
439}
440
Andreas Huber70a59e12016-08-16 12:57:01 -0700441bool Type::isJavaCompatible() const {
442 return true;
443}
444
Andreas Huber85eabdb2016-08-25 11:24:49 -0700445void Type::getAlignmentAndSize(size_t *, size_t *) const {
446 CHECK(!"Should not be here");
447}
448
Andreas Huber019d21d2016-10-03 12:59:47 -0700449void Type::appendToExportedTypesVector(
450 std::vector<const Type *> * /* exportedTypes */) const {
451}
452
Andreas Huber1c507272016-10-05 14:33:21 -0700453status_t Type::emitExportedHeader(
454 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700455 return OK;
456}
457
Yifan Hongbf459bc2016-08-23 16:50:37 -0700458////////////////////////////////////////
459
460TemplatedType::TemplatedType() : mElementType(nullptr) {
461}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800462
Yifan Hongbf459bc2016-08-23 16:50:37 -0700463void TemplatedType::setElementType(Type *elementType) {
464 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800465 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700466 mElementType = elementType;
467}
468
Yifan Hongabf73ee2016-12-05 18:47:00 -0800469Type *TemplatedType::getElementType() const {
470 return mElementType;
471}
472
473bool TemplatedType::isTemplatedType() const {
474 return true;
475}
476
Andreas Huberc9410c72016-07-28 12:18:40 -0700477} // namespace android
478