blob: e3825db40062048610e276562899e80889a24ae1 [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
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070049bool Type::isEnum() const {
50 return false;
51}
52
53bool Type::isTypeDef() const {
54 return false;
55}
56
Andreas Huber295ad302016-08-16 11:35:00 -070057bool Type::isBinder() const {
58 return false;
59}
60
Andreas Huber39fa7182016-08-19 14:27:33 -070061bool Type::isNamedType() const {
62 return false;
63}
64
Andreas Huberf630bc82016-09-09 14:52:25 -070065bool Type::isCompoundType() const {
66 return false;
67}
68
Andreas Huber709b62d2016-09-19 11:21:18 -070069bool Type::isArray() const {
70 return false;
71}
72
73bool Type::isVector() const {
74 return false;
75}
76
Martijn Coenen99e6beb2016-12-01 15:48:42 +010077bool Type::isPointer() const {
78 return false;
79}
80
Steven Moreland30bb6a82016-11-30 09:18:34 -080081std::string Type::typeName() const {
82 return "";
83}
84
Andreas Huber737080b2016-08-02 15:38:04 -070085const ScalarType *Type::resolveToScalarType() const {
86 return NULL;
87}
88
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070089bool Type::isValidEnumStorageType() const {
90 const ScalarType *scalarType = resolveToScalarType();
91
92 if (scalarType == NULL) {
93 return false;
94 }
95
96 return scalarType->isValidEnumStorageType();
97}
98
Yifan Hong3b320f82016-11-01 15:15:54 -070099std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700100 CHECK(!"Should not be here");
101 return std::string();
102}
103
Yifan Hong3b320f82016-11-01 15:15:54 -0700104std::string Type::decorateCppName(
105 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
106 return getCppType(mode, specifyNamespaces) + " " + name;
107}
108
Yifan Hong4ed13472016-11-02 10:44:11 -0700109std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700110 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700111 return std::string();
112}
113
Andreas Huber85eabdb2016-08-25 11:24:49 -0700114std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700115 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700116}
117
Andreas Huber2831d512016-08-15 09:33:47 -0700118std::string Type::getJavaSuffix() const {
119 CHECK(!"Should not be here");
120 return std::string();
121}
122
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700123std::string Type::getVtsType() const {
124 CHECK(!"Should not be here");
125 return std::string();
126}
127
Andreas Huber881227d2016-08-02 14:20:21 -0700128void Type::emitReaderWriter(
129 Formatter &,
130 const std::string &,
131 const std::string &,
132 bool,
133 bool,
134 ErrorMode) const {
135 CHECK(!"Should not be here");
136}
137
Yifan Hongbf459bc2016-08-23 16:50:37 -0700138void Type::emitResolveReferences(
139 Formatter &,
140 const std::string &,
141 bool,
142 const std::string &,
143 bool,
144 bool,
145 ErrorMode) const {
146 CHECK(!"Should not be here");
147}
148
149void Type::emitResolveReferencesEmbedded(
150 Formatter &,
151 size_t,
152 const std::string &,
153 const std::string &,
154 bool,
155 const std::string &,
156 bool,
157 bool,
158 ErrorMode,
159 const std::string &,
160 const std::string &) const {
161 CHECK(!"Should not be here");
162}
163
Yifan Hong00f47172016-09-30 14:40:45 -0700164bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800165 return needsResolveReferences();
166}
167
168bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
169 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700170}
171
Andreas Huber881227d2016-08-02 14:20:21 -0700172void Type::emitReaderWriterEmbedded(
173 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700174 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700175 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700176 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700177 bool,
178 const std::string &,
179 bool,
180 bool,
181 ErrorMode,
182 const std::string &,
183 const std::string &) const {
184 CHECK(!"Should not be here");
185}
186
Andreas Huber2831d512016-08-15 09:33:47 -0700187void Type::emitJavaReaderWriter(
188 Formatter &out,
189 const std::string &parcelObj,
190 const std::string &argName,
191 bool isReader) const {
192 emitJavaReaderWriterWithSuffix(
193 out,
194 parcelObj,
195 argName,
196 isReader,
197 getJavaSuffix(),
198 "" /* extra */);
199}
200
Andreas Huber85eabdb2016-08-25 11:24:49 -0700201void Type::emitJavaFieldInitializer(
202 Formatter &out,
203 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700204 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700205 << " "
206 << fieldName
207 << ";\n";
208}
209
210void Type::emitJavaFieldReaderWriter(
211 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700212 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700213 const std::string &,
214 const std::string &,
215 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700216 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700217 bool) const {
218 CHECK(!"Should not be here");
219}
220
Andreas Huber881227d2016-08-02 14:20:21 -0700221void Type::handleError(Formatter &out, ErrorMode mode) const {
222 switch (mode) {
223 case ErrorMode_Ignore:
224 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700225 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700226 break;
227 }
228
229 case ErrorMode_Goto:
230 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700231 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700232 break;
233 }
234
235 case ErrorMode_Break:
236 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700237 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700238 break;
239 }
Andreas Huber737080b2016-08-02 15:38:04 -0700240
241 case ErrorMode_Return:
242 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700243 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700244 break;
245 }
Andreas Huber881227d2016-08-02 14:20:21 -0700246 }
247}
248
249void Type::handleError2(Formatter &out, ErrorMode mode) const {
250 switch (mode) {
251 case ErrorMode_Goto:
252 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700253 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700254 break;
255 }
Andreas Huber737080b2016-08-02 15:38:04 -0700256
Andreas Huber881227d2016-08-02 14:20:21 -0700257 case ErrorMode_Break:
258 {
259 out << "break;\n";
260 break;
261 }
Andreas Huber737080b2016-08-02 15:38:04 -0700262
Andreas Huber881227d2016-08-02 14:20:21 -0700263 case ErrorMode_Ignore:
264 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700265 out << "/* ignoring _hidl_error! */";
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 << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700272 break;
273 }
Andreas Huber881227d2016-08-02 14:20:21 -0700274 }
275}
276
277void Type::emitReaderWriterEmbeddedForTypeName(
278 Formatter &out,
279 const std::string &name,
280 bool nameIsPointer,
281 const std::string &parcelObj,
282 bool parcelObjIsPointer,
283 bool isReader,
284 ErrorMode mode,
285 const std::string &parentName,
286 const std::string &offsetText,
287 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800288 const std::string &childName,
289 const std::string &funcNamespace) const {
290
291 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700292 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
293
294 const std::string parcelObjPointer =
295 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
296
Yifan Hong244e82d2016-11-11 11:13:57 -0800297 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700298 const std::string namePointer = nameIsPointer ? name : ("&" + name);
299
Iliyan Malchev549e2592016-08-10 08:59:12 -0700300 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700301
Yifan Hong244e82d2016-11-11 11:13:57 -0800302 if (!funcNamespace.empty()) {
303 out << funcNamespace << "::";
304 }
305
306 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
307
308 out.indent();
309 out.indent();
310
Andreas Huber881227d2016-08-02 14:20:21 -0700311 if (isReader) {
312 out << "const_cast<"
313 << typeName
314 << " *>("
315 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800316 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700317 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800318 out << nameDerefed
319 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700320 }
321
Andreas Huber881227d2016-08-02 14:20:21 -0700322 out << (isReader ? parcelObjDeref : parcelObjPointer)
323 << ",\n"
324 << parentName
325 << ",\n"
326 << offsetText;
327
328 if (!childName.empty()) {
329 out << ", &"
330 << childName;
331 }
332
333 out << ");\n\n";
334
335 out.unindent();
336 out.unindent();
337
338 handleError(out, mode);
339}
340
341status_t Type::emitTypeDeclarations(Formatter &) const {
342 return OK;
343}
344
Andreas Hubere3f769a2016-10-10 10:54:44 -0700345status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
346 return OK;
347}
348
Yifan Hong244e82d2016-11-11 11:13:57 -0800349status_t Type::emitGlobalHwDeclarations(Formatter &) const {
350 return OK;
351}
352
Andreas Huber881227d2016-08-02 14:20:21 -0700353status_t Type::emitTypeDefinitions(
354 Formatter &, const std::string) const {
355 return OK;
356}
357
Andreas Huber85eabdb2016-08-25 11:24:49 -0700358status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700359 return OK;
360}
361
Andreas Huber881227d2016-08-02 14:20:21 -0700362bool Type::needsEmbeddedReadWrite() const {
363 return false;
364}
365
Yifan Hongbf459bc2016-08-23 16:50:37 -0700366bool Type::needsResolveReferences() const {
367 return false;
368}
369
Andreas Huber881227d2016-08-02 14:20:21 -0700370bool Type::resultNeedsDeref() const {
371 return false;
372}
373
Yifan Hong3b320f82016-11-01 15:15:54 -0700374std::string Type::getCppStackType(bool specifyNamespaces) const {
375 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700376}
377
Yifan Hong3b320f82016-11-01 15:15:54 -0700378std::string Type::getCppResultType(bool specifyNamespaces) const {
379 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700380}
381
Yifan Hong3b320f82016-11-01 15:15:54 -0700382std::string Type::getCppArgumentType(bool specifyNamespaces) const {
383 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700384}
385
Andreas Huber2831d512016-08-15 09:33:47 -0700386void Type::emitJavaReaderWriterWithSuffix(
387 Formatter &out,
388 const std::string &parcelObj,
389 const std::string &argName,
390 bool isReader,
391 const std::string &suffix,
392 const std::string &extra) const {
393 out << parcelObj
394 << "."
395 << (isReader ? "read" : "write")
396 << suffix
397 << "(";
398
399 if (isReader) {
400 out << extra;
401 } else {
402 out << (extra.empty() ? "" : (extra + ", "));
403 out << argName;
404 }
405
406 out << ");\n";
407}
408
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700409status_t Type::emitVtsTypeDeclarations(Formatter &) const {
410 return OK;
411}
412
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700413status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700414 return emitVtsTypeDeclarations(out);
415}
416
Andreas Huber70a59e12016-08-16 12:57:01 -0700417bool Type::isJavaCompatible() const {
418 return true;
419}
420
Andreas Huber85eabdb2016-08-25 11:24:49 -0700421void Type::getAlignmentAndSize(size_t *, size_t *) const {
422 CHECK(!"Should not be here");
423}
424
Andreas Huber019d21d2016-10-03 12:59:47 -0700425void Type::appendToExportedTypesVector(
426 std::vector<const Type *> * /* exportedTypes */) const {
427}
428
Andreas Huber1c507272016-10-05 14:33:21 -0700429status_t Type::emitExportedHeader(
430 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700431 return OK;
432}
433
Yifan Hongbf459bc2016-08-23 16:50:37 -0700434////////////////////////////////////////
435
436TemplatedType::TemplatedType() : mElementType(nullptr) {
437}
Steven Moreland30bb6a82016-11-30 09:18:34 -0800438
Yifan Hongbf459bc2016-08-23 16:50:37 -0700439void TemplatedType::setElementType(Type *elementType) {
440 CHECK(mElementType == nullptr); // can only be set once.
Steven Moreland30bb6a82016-11-30 09:18:34 -0800441 CHECK(isCompatibleElementType(elementType));
Yifan Hongbf459bc2016-08-23 16:50:37 -0700442 mElementType = elementType;
443}
444
Andreas Huberc9410c72016-07-28 12:18:40 -0700445} // namespace android
446