blob: f155816ffccc6ca4fc077acd34bb9d442415e3dd [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 Hongf5cc2f72017-01-04 18:02:34 -0800192void Type::emitDump(
193 Formatter &out,
194 const std::string &streamName,
195 const std::string &name) const {
196 emitDumpWithMethod(out, streamName, "::android::hardware::details::toString", name);
197}
198
199void Type::emitDumpWithMethod(
200 Formatter &out,
201 const std::string &streamName,
202 const std::string &methodName,
203 const std::string &name) const {
204 out << streamName
205 << " += "
206 << methodName
207 << "("
208 << name
209 << ");\n";
210}
211
Yifan Hong00f47172016-09-30 14:40:45 -0700212bool Type::useParentInEmitResolveReferencesEmbedded() const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800213 return needsResolveReferences();
214}
215
216bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
217 return needsEmbeddedReadWrite();
Yifan Hong00f47172016-09-30 14:40:45 -0700218}
219
Andreas Huber881227d2016-08-02 14:20:21 -0700220void Type::emitReaderWriterEmbedded(
221 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700222 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700223 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700224 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700225 bool,
226 const std::string &,
227 bool,
228 bool,
229 ErrorMode,
230 const std::string &,
231 const std::string &) const {
232 CHECK(!"Should not be here");
233}
234
Andreas Huber2831d512016-08-15 09:33:47 -0700235void Type::emitJavaReaderWriter(
236 Formatter &out,
237 const std::string &parcelObj,
238 const std::string &argName,
239 bool isReader) const {
240 emitJavaReaderWriterWithSuffix(
241 out,
242 parcelObj,
243 argName,
244 isReader,
245 getJavaSuffix(),
246 "" /* extra */);
247}
248
Andreas Huber85eabdb2016-08-25 11:24:49 -0700249void Type::emitJavaFieldInitializer(
250 Formatter &out,
251 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700252 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700253 << " "
254 << fieldName
255 << ";\n";
256}
257
258void Type::emitJavaFieldReaderWriter(
259 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700260 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700261 const std::string &,
262 const std::string &,
263 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700264 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700265 bool) const {
266 CHECK(!"Should not be here");
267}
268
Andreas Huber881227d2016-08-02 14:20:21 -0700269void Type::handleError(Formatter &out, ErrorMode mode) const {
270 switch (mode) {
271 case ErrorMode_Ignore:
272 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700273 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700274 break;
275 }
276
277 case ErrorMode_Goto:
278 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700279 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700280 break;
281 }
282
283 case ErrorMode_Break:
284 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700285 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
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 << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700292 break;
293 }
Andreas Huber881227d2016-08-02 14:20:21 -0700294 }
295}
296
Andreas Huber881227d2016-08-02 14:20:21 -0700297void 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