blob: c5e55a9b36c995651d32fe31db09392e84bbc043 [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
Yifan Hong3b320f82016-11-01 15:15:54 -0700119std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700120 CHECK(!"Should not be here");
121 return std::string();
122}
123
Yifan Hong3b320f82016-11-01 15:15:54 -0700124std::string Type::decorateCppName(
125 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
126 return getCppType(mode, specifyNamespaces) + " " + name;
127}
128
Yifan Hong4ed13472016-11-02 10:44:11 -0700129std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700130 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700131 return std::string();
132}
133
Andreas Huber85eabdb2016-08-25 11:24:49 -0700134std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700135 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700136}
137
Andreas Huber2831d512016-08-15 09:33:47 -0700138std::string Type::getJavaSuffix() const {
139 CHECK(!"Should not be here");
140 return std::string();
141}
142
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700143std::string Type::getVtsType() const {
144 CHECK(!"Should not be here");
145 return std::string();
146}
147
Andreas Huber881227d2016-08-02 14:20:21 -0700148void Type::emitReaderWriter(
149 Formatter &,
150 const std::string &,
151 const std::string &,
152 bool,
153 bool,
154 ErrorMode) const {
155 CHECK(!"Should not be here");
156}
157
Yifan Hongbf459bc2016-08-23 16:50:37 -0700158void Type::emitResolveReferences(
159 Formatter &,
160 const std::string &,
161 bool,
162 const std::string &,
163 bool,
164 bool,
165 ErrorMode) const {
166 CHECK(!"Should not be here");
167}
168
169void Type::emitResolveReferencesEmbedded(
170 Formatter &,
171 size_t,
172 const std::string &,
173 const std::string &,
174 bool,
175 const std::string &,
176 bool,
177 bool,
178 ErrorMode,
179 const std::string &,
180 const std::string &) const {
181 CHECK(!"Should not be here");
182}
183
Yifan Hong00f47172016-09-30 14:40:45 -0700184bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800185 return needsResolveReferences();
186}
187
188bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
189 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700190}
191
Andreas Huber881227d2016-08-02 14:20:21 -0700192void Type::emitReaderWriterEmbedded(
193 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700194 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700195 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700196 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700197 bool,
198 const std::string &,
199 bool,
200 bool,
201 ErrorMode,
202 const std::string &,
203 const std::string &) const {
204 CHECK(!"Should not be here");
205}
206
Andreas Huber2831d512016-08-15 09:33:47 -0700207void Type::emitJavaReaderWriter(
208 Formatter &out,
209 const std::string &parcelObj,
210 const std::string &argName,
211 bool isReader) const {
212 emitJavaReaderWriterWithSuffix(
213 out,
214 parcelObj,
215 argName,
216 isReader,
217 getJavaSuffix(),
218 "" /* extra */);
219}
220
Andreas Huber85eabdb2016-08-25 11:24:49 -0700221void Type::emitJavaFieldInitializer(
222 Formatter &out,
223 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700224 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700225 << " "
226 << fieldName
227 << ";\n";
228}
229
230void Type::emitJavaFieldReaderWriter(
231 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700232 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700233 const std::string &,
234 const std::string &,
235 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700236 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700237 bool) const {
238 CHECK(!"Should not be here");
239}
240
Andreas Huber881227d2016-08-02 14:20:21 -0700241void Type::handleError(Formatter &out, ErrorMode mode) const {
242 switch (mode) {
243 case ErrorMode_Ignore:
244 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700245 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700246 break;
247 }
248
249 case ErrorMode_Goto:
250 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700251 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700252 break;
253 }
254
255 case ErrorMode_Break:
256 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700257 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700258 break;
259 }
Andreas Huber737080b2016-08-02 15:38:04 -0700260
261 case ErrorMode_Return:
262 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700263 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700264 break;
265 }
Andreas Huber881227d2016-08-02 14:20:21 -0700266 }
267}
268
269void Type::handleError2(Formatter &out, ErrorMode mode) const {
270 switch (mode) {
271 case ErrorMode_Goto:
272 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700273 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700274 break;
275 }
Andreas Huber737080b2016-08-02 15:38:04 -0700276
Andreas Huber881227d2016-08-02 14:20:21 -0700277 case ErrorMode_Break:
278 {
279 out << "break;\n";
280 break;
281 }
Andreas Huber737080b2016-08-02 15:38:04 -0700282
Andreas Huber881227d2016-08-02 14:20:21 -0700283 case ErrorMode_Ignore:
284 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700285 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700286 break;
287 }
Andreas Huber737080b2016-08-02 15:38:04 -0700288
289 case ErrorMode_Return:
290 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700291 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700292 break;
293 }
Andreas Huber881227d2016-08-02 14:20:21 -0700294 }
295}
296
297void Type::emitReaderWriterEmbeddedForTypeName(
298 Formatter &out,
299 const std::string &name,
300 bool nameIsPointer,
301 const std::string &parcelObj,
302 bool parcelObjIsPointer,
303 bool isReader,
304 ErrorMode mode,
305 const std::string &parentName,
306 const std::string &offsetText,
307 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800308 const std::string &childName,
309 const std::string &funcNamespace) const {
310
311 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700312 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
313
314 const std::string parcelObjPointer =
315 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
316
Yifan Hong244e82d2016-11-11 11:13:57 -0800317 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700318 const std::string namePointer = nameIsPointer ? name : ("&" + name);
319
Iliyan Malchev549e2592016-08-10 08:59:12 -0700320 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700321
Yifan Hong244e82d2016-11-11 11:13:57 -0800322 if (!funcNamespace.empty()) {
323 out << funcNamespace << "::";
324 }
325
326 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
327
328 out.indent();
329 out.indent();
330
Andreas Huber881227d2016-08-02 14:20:21 -0700331 if (isReader) {
332 out << "const_cast<"
333 << typeName
334 << " *>("
335 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800336 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700337 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800338 out << nameDerefed
339 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700340 }
341
Andreas Huber881227d2016-08-02 14:20:21 -0700342 out << (isReader ? parcelObjDeref : parcelObjPointer)
343 << ",\n"
344 << parentName
345 << ",\n"
346 << offsetText;
347
348 if (!childName.empty()) {
349 out << ", &"
350 << childName;
351 }
352
353 out << ");\n\n";
354
355 out.unindent();
356 out.unindent();
357
358 handleError(out, mode);
359}
360
361status_t Type::emitTypeDeclarations(Formatter &) const {
362 return OK;
363}
364
Andreas Hubere3f769a2016-10-10 10:54:44 -0700365status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
366 return OK;
367}
368
Yifan Hong244e82d2016-11-11 11:13:57 -0800369status_t Type::emitGlobalHwDeclarations(Formatter &) const {
370 return OK;
371}
372
Andreas Huber881227d2016-08-02 14:20:21 -0700373status_t Type::emitTypeDefinitions(
374 Formatter &, const std::string) const {
375 return OK;
376}
377
Andreas Huber85eabdb2016-08-25 11:24:49 -0700378status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700379 return OK;
380}
381
Andreas Huber881227d2016-08-02 14:20:21 -0700382bool Type::needsEmbeddedReadWrite() const {
383 return false;
384}
385
Yifan Hongbf459bc2016-08-23 16:50:37 -0700386bool Type::needsResolveReferences() const {
387 return false;
388}
389
Andreas Huber881227d2016-08-02 14:20:21 -0700390bool Type::resultNeedsDeref() const {
391 return false;
392}
393
Yifan Hong3b320f82016-11-01 15:15:54 -0700394std::string Type::getCppStackType(bool specifyNamespaces) const {
395 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700396}
397
Yifan Hong3b320f82016-11-01 15:15:54 -0700398std::string Type::getCppResultType(bool specifyNamespaces) const {
399 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700400}
401
Yifan Hong3b320f82016-11-01 15:15:54 -0700402std::string Type::getCppArgumentType(bool specifyNamespaces) const {
403 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700404}
405
Andreas Huber2831d512016-08-15 09:33:47 -0700406void Type::emitJavaReaderWriterWithSuffix(
407 Formatter &out,
408 const std::string &parcelObj,
409 const std::string &argName,
410 bool isReader,
411 const std::string &suffix,
412 const std::string &extra) const {
413 out << parcelObj
414 << "."
415 << (isReader ? "read" : "write")
416 << suffix
417 << "(";
418
419 if (isReader) {
420 out << extra;
421 } else {
422 out << (extra.empty() ? "" : (extra + ", "));
423 out << argName;
424 }
425
426 out << ");\n";
427}
428
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700429status_t Type::emitVtsTypeDeclarations(Formatter &) const {
430 return OK;
431}
432
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700433status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700434 return emitVtsTypeDeclarations(out);
435}
436
Andreas Huber70a59e12016-08-16 12:57:01 -0700437bool Type::isJavaCompatible() const {
438 return true;
439}
440
Andreas Huber85eabdb2016-08-25 11:24:49 -0700441void Type::getAlignmentAndSize(size_t *, size_t *) const {
442 CHECK(!"Should not be here");
443}
444
Andreas Huber019d21d2016-10-03 12:59:47 -0700445void Type::appendToExportedTypesVector(
446 std::vector<const Type *> * /* exportedTypes */) const {
447}
448
Andreas Huber1c507272016-10-05 14:33:21 -0700449status_t Type::emitExportedHeader(
450 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700451 return OK;
452}
453
Yifan Hongbf459bc2016-08-23 16:50:37 -0700454////////////////////////////////////////
455
456TemplatedType::TemplatedType() : mElementType(nullptr) {
457}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800458
Yifan Hongbf459bc2016-08-23 16:50:37 -0700459void TemplatedType::setElementType(Type *elementType) {
460 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800461 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700462 mElementType = elementType;
463}
464
Yifan Hongabf73ee2016-12-05 18:47:00 -0800465Type *TemplatedType::getElementType() const {
466 return mElementType;
467}
468
469bool TemplatedType::isTemplatedType() const {
470 return true;
471}
472
Andreas Huberc9410c72016-07-28 12:18:40 -0700473} // namespace android
474