blob: 47cea08b8e3ffd4c20bf663f0cfaebe192da42cc [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
Andreas Huber737080b2016-08-02 15:38:04 -070077const ScalarType *Type::resolveToScalarType() const {
78 return NULL;
79}
80
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070081bool Type::isValidEnumStorageType() const {
82 const ScalarType *scalarType = resolveToScalarType();
83
84 if (scalarType == NULL) {
85 return false;
86 }
87
88 return scalarType->isValidEnumStorageType();
89}
90
Yifan Hong3b320f82016-11-01 15:15:54 -070091std::string Type::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070092 CHECK(!"Should not be here");
93 return std::string();
94}
95
Yifan Hong3b320f82016-11-01 15:15:54 -070096std::string Type::decorateCppName(
97 const std::string &name, StorageMode mode, bool specifyNamespaces) const {
98 return getCppType(mode, specifyNamespaces) + " " + name;
99}
100
Yifan Hong4ed13472016-11-02 10:44:11 -0700101std::string Type::getJavaType(bool /* forInitializer */) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700102 CHECK(!"Should not be here");
Andreas Huber4c865b72016-09-14 15:26:27 -0700103 return std::string();
104}
105
Andreas Huber85eabdb2016-08-25 11:24:49 -0700106std::string Type::getJavaWrapperType() const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700107 return getJavaType();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700108}
109
Andreas Huber2831d512016-08-15 09:33:47 -0700110std::string Type::getJavaSuffix() const {
111 CHECK(!"Should not be here");
112 return std::string();
113}
114
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700115std::string Type::getVtsType() const {
116 CHECK(!"Should not be here");
117 return std::string();
118}
119
Andreas Huber881227d2016-08-02 14:20:21 -0700120void Type::emitReaderWriter(
121 Formatter &,
122 const std::string &,
123 const std::string &,
124 bool,
125 bool,
126 ErrorMode) const {
127 CHECK(!"Should not be here");
128}
129
Yifan Hongbf459bc2016-08-23 16:50:37 -0700130void Type::emitResolveReferences(
131 Formatter &,
132 const std::string &,
133 bool,
134 const std::string &,
135 bool,
136 bool,
137 ErrorMode) const {
138 CHECK(!"Should not be here");
139}
140
141void Type::emitResolveReferencesEmbedded(
142 Formatter &,
143 size_t,
144 const std::string &,
145 const std::string &,
146 bool,
147 const std::string &,
148 bool,
149 bool,
150 ErrorMode,
151 const std::string &,
152 const std::string &) const {
153 CHECK(!"Should not be here");
154}
155
Yifan Hong00f47172016-09-30 14:40:45 -0700156bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800157 return needsResolveReferences();
158}
159
160bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
161 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700162}
163
Andreas Huber881227d2016-08-02 14:20:21 -0700164void Type::emitReaderWriterEmbedded(
165 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700166 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700167 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700168 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700169 bool,
170 const std::string &,
171 bool,
172 bool,
173 ErrorMode,
174 const std::string &,
175 const std::string &) const {
176 CHECK(!"Should not be here");
177}
178
Andreas Huber2831d512016-08-15 09:33:47 -0700179void Type::emitJavaReaderWriter(
180 Formatter &out,
181 const std::string &parcelObj,
182 const std::string &argName,
183 bool isReader) const {
184 emitJavaReaderWriterWithSuffix(
185 out,
186 parcelObj,
187 argName,
188 isReader,
189 getJavaSuffix(),
190 "" /* extra */);
191}
192
Andreas Huber85eabdb2016-08-25 11:24:49 -0700193void Type::emitJavaFieldInitializer(
194 Formatter &out,
195 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700196 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700197 << " "
198 << fieldName
199 << ";\n";
200}
201
202void Type::emitJavaFieldReaderWriter(
203 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700204 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700205 const std::string &,
206 const std::string &,
207 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700208 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700209 bool) const {
210 CHECK(!"Should not be here");
211}
212
Andreas Huber881227d2016-08-02 14:20:21 -0700213void Type::handleError(Formatter &out, ErrorMode mode) const {
214 switch (mode) {
215 case ErrorMode_Ignore:
216 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700217 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700218 break;
219 }
220
221 case ErrorMode_Goto:
222 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700223 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700224 break;
225 }
226
227 case ErrorMode_Break:
228 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700229 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700230 break;
231 }
Andreas Huber737080b2016-08-02 15:38:04 -0700232
233 case ErrorMode_Return:
234 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700235 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700236 break;
237 }
Andreas Huber881227d2016-08-02 14:20:21 -0700238 }
239}
240
241void Type::handleError2(Formatter &out, ErrorMode mode) const {
242 switch (mode) {
243 case ErrorMode_Goto:
244 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700245 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700246 break;
247 }
Andreas Huber737080b2016-08-02 15:38:04 -0700248
Andreas Huber881227d2016-08-02 14:20:21 -0700249 case ErrorMode_Break:
250 {
251 out << "break;\n";
252 break;
253 }
Andreas Huber737080b2016-08-02 15:38:04 -0700254
Andreas Huber881227d2016-08-02 14:20:21 -0700255 case ErrorMode_Ignore:
256 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700257 out << "/* ignoring _hidl_error! */";
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 << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700264 break;
265 }
Andreas Huber881227d2016-08-02 14:20:21 -0700266 }
267}
268
269void Type::emitReaderWriterEmbeddedForTypeName(
270 Formatter &out,
271 const std::string &name,
272 bool nameIsPointer,
273 const std::string &parcelObj,
274 bool parcelObjIsPointer,
275 bool isReader,
276 ErrorMode mode,
277 const std::string &parentName,
278 const std::string &offsetText,
279 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800280 const std::string &childName,
281 const std::string &funcNamespace) const {
282
283 const std::string parcelObjDeref =
Andreas Huber881227d2016-08-02 14:20:21 -0700284 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
285
286 const std::string parcelObjPointer =
287 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
288
Yifan Hong244e82d2016-11-11 11:13:57 -0800289 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Andreas Huber881227d2016-08-02 14:20:21 -0700290 const std::string namePointer = nameIsPointer ? name : ("&" + name);
291
Iliyan Malchev549e2592016-08-10 08:59:12 -0700292 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700293
Yifan Hong244e82d2016-11-11 11:13:57 -0800294 if (!funcNamespace.empty()) {
295 out << funcNamespace << "::";
296 }
297
298 out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
299
300 out.indent();
301 out.indent();
302
Andreas Huber881227d2016-08-02 14:20:21 -0700303 if (isReader) {
304 out << "const_cast<"
305 << typeName
306 << " *>("
307 << namePointer
Yifan Hong244e82d2016-11-11 11:13:57 -0800308 << "),\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700309 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800310 out << nameDerefed
311 << ",\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700312 }
313
Andreas Huber881227d2016-08-02 14:20:21 -0700314 out << (isReader ? parcelObjDeref : parcelObjPointer)
315 << ",\n"
316 << parentName
317 << ",\n"
318 << offsetText;
319
320 if (!childName.empty()) {
321 out << ", &"
322 << childName;
323 }
324
325 out << ");\n\n";
326
327 out.unindent();
328 out.unindent();
329
330 handleError(out, mode);
331}
332
333status_t Type::emitTypeDeclarations(Formatter &) const {
334 return OK;
335}
336
Andreas Hubere3f769a2016-10-10 10:54:44 -0700337status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
338 return OK;
339}
340
Yifan Hong244e82d2016-11-11 11:13:57 -0800341status_t Type::emitGlobalHwDeclarations(Formatter &) const {
342 return OK;
343}
344
Andreas Huber881227d2016-08-02 14:20:21 -0700345status_t Type::emitTypeDefinitions(
346 Formatter &, const std::string) const {
347 return OK;
348}
349
Andreas Huber85eabdb2016-08-25 11:24:49 -0700350status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700351 return OK;
352}
353
Andreas Huber881227d2016-08-02 14:20:21 -0700354bool Type::needsEmbeddedReadWrite() const {
355 return false;
356}
357
Yifan Hongbf459bc2016-08-23 16:50:37 -0700358bool Type::needsResolveReferences() const {
359 return false;
360}
361
Andreas Huber881227d2016-08-02 14:20:21 -0700362bool Type::resultNeedsDeref() const {
363 return false;
364}
365
Yifan Hong3b320f82016-11-01 15:15:54 -0700366std::string Type::getCppStackType(bool specifyNamespaces) const {
367 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700368}
369
Yifan Hong3b320f82016-11-01 15:15:54 -0700370std::string Type::getCppResultType(bool specifyNamespaces) const {
371 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700372}
373
Yifan Hong3b320f82016-11-01 15:15:54 -0700374std::string Type::getCppArgumentType(bool specifyNamespaces) const {
375 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700376}
377
Andreas Huber2831d512016-08-15 09:33:47 -0700378void Type::emitJavaReaderWriterWithSuffix(
379 Formatter &out,
380 const std::string &parcelObj,
381 const std::string &argName,
382 bool isReader,
383 const std::string &suffix,
384 const std::string &extra) const {
385 out << parcelObj
386 << "."
387 << (isReader ? "read" : "write")
388 << suffix
389 << "(";
390
391 if (isReader) {
392 out << extra;
393 } else {
394 out << (extra.empty() ? "" : (extra + ", "));
395 out << argName;
396 }
397
398 out << ");\n";
399}
400
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700401status_t Type::emitVtsTypeDeclarations(Formatter &) const {
402 return OK;
403}
404
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700405status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700406 return emitVtsTypeDeclarations(out);
407}
408
Andreas Huber70a59e12016-08-16 12:57:01 -0700409bool Type::isJavaCompatible() const {
410 return true;
411}
412
Andreas Huber85eabdb2016-08-25 11:24:49 -0700413void Type::getAlignmentAndSize(size_t *, size_t *) const {
414 CHECK(!"Should not be here");
415}
416
Andreas Huber019d21d2016-10-03 12:59:47 -0700417void Type::appendToExportedTypesVector(
418 std::vector<const Type *> * /* exportedTypes */) const {
419}
420
Andreas Huber1c507272016-10-05 14:33:21 -0700421status_t Type::emitExportedHeader(
422 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700423 return OK;
424}
425
Yifan Hongbf459bc2016-08-23 16:50:37 -0700426////////////////////////////////////////
427
428TemplatedType::TemplatedType() : mElementType(nullptr) {
429}
430void TemplatedType::setElementType(Type *elementType) {
431 CHECK(mElementType == nullptr); // can only be set once.
432 mElementType = elementType;
433}
434
Andreas Huberc9410c72016-07-28 12:18:40 -0700435} // namespace android
436