blob: f97af232795286b566c2f974f0e27a1b3f1c17e5 [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
Andreas Huber4c865b72016-09-14 15:26:27 -0700101std::string Type::getJavaType(
102 std::string *extra, bool /* forInitializer */) const {
103 CHECK(!"Should not be here");
104 extra->clear();
105 return std::string();
106}
107
Andreas Huber85eabdb2016-08-25 11:24:49 -0700108std::string Type::getJavaWrapperType() const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700109 std::string extra;
110 return getJavaType(&extra);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700111}
112
Andreas Huber2831d512016-08-15 09:33:47 -0700113std::string Type::getJavaSuffix() const {
114 CHECK(!"Should not be here");
115 return std::string();
116}
117
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700118std::string Type::getVtsType() const {
119 CHECK(!"Should not be here");
120 return std::string();
121}
122
Andreas Huber881227d2016-08-02 14:20:21 -0700123void Type::emitReaderWriter(
124 Formatter &,
125 const std::string &,
126 const std::string &,
127 bool,
128 bool,
129 ErrorMode) const {
130 CHECK(!"Should not be here");
131}
132
Yifan Hongbf459bc2016-08-23 16:50:37 -0700133void Type::emitResolveReferences(
134 Formatter &,
135 const std::string &,
136 bool,
137 const std::string &,
138 bool,
139 bool,
140 ErrorMode) const {
141 CHECK(!"Should not be here");
142}
143
144void Type::emitResolveReferencesEmbedded(
145 Formatter &,
146 size_t,
147 const std::string &,
148 const std::string &,
149 bool,
150 const std::string &,
151 bool,
152 bool,
153 ErrorMode,
154 const std::string &,
155 const std::string &) const {
156 CHECK(!"Should not be here");
157}
158
Yifan Hong00f47172016-09-30 14:40:45 -0700159bool Type::useParentInEmitResolveReferencesEmbedded() const {
160 return true;
161}
162
Andreas Huber881227d2016-08-02 14:20:21 -0700163void Type::emitReaderWriterEmbedded(
164 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700165 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700166 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700167 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700168 bool,
169 const std::string &,
170 bool,
171 bool,
172 ErrorMode,
173 const std::string &,
174 const std::string &) const {
175 CHECK(!"Should not be here");
176}
177
Andreas Huber2831d512016-08-15 09:33:47 -0700178void Type::emitJavaReaderWriter(
179 Formatter &out,
180 const std::string &parcelObj,
181 const std::string &argName,
182 bool isReader) const {
183 emitJavaReaderWriterWithSuffix(
184 out,
185 parcelObj,
186 argName,
187 isReader,
188 getJavaSuffix(),
189 "" /* extra */);
190}
191
Andreas Huber85eabdb2016-08-25 11:24:49 -0700192void Type::emitJavaFieldInitializer(
193 Formatter &out,
194 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700195 std::string extra;
196 out << getJavaType(&extra)
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,
280 const std::string &childName) const {
281 const std::string parcelObjDeref =
282 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
283
284 const std::string parcelObjPointer =
285 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
286
287 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
288 const std::string namePointer = nameIsPointer ? name : ("&" + name);
289
Iliyan Malchev549e2592016-08-10 08:59:12 -0700290 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700291
292 if (isReader) {
293 out << "const_cast<"
294 << typeName
295 << " *>("
296 << namePointer
297 << ")->readEmbeddedFromParcel(\n";
298 } else {
299 out << nameDeref
300 << "writeEmbeddedToParcel(\n";
301 }
302
303 out.indent();
304 out.indent();
305
306 out << (isReader ? parcelObjDeref : parcelObjPointer)
307 << ",\n"
308 << parentName
309 << ",\n"
310 << offsetText;
311
312 if (!childName.empty()) {
313 out << ", &"
314 << childName;
315 }
316
317 out << ");\n\n";
318
319 out.unindent();
320 out.unindent();
321
322 handleError(out, mode);
323}
324
325status_t Type::emitTypeDeclarations(Formatter &) const {
326 return OK;
327}
328
Andreas Hubere3f769a2016-10-10 10:54:44 -0700329status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
330 return OK;
331}
332
Andreas Huber881227d2016-08-02 14:20:21 -0700333status_t Type::emitTypeDefinitions(
334 Formatter &, const std::string) const {
335 return OK;
336}
337
Andreas Huber85eabdb2016-08-25 11:24:49 -0700338status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700339 return OK;
340}
341
Andreas Huber881227d2016-08-02 14:20:21 -0700342bool Type::needsEmbeddedReadWrite() const {
343 return false;
344}
345
Yifan Hongbf459bc2016-08-23 16:50:37 -0700346bool Type::needsResolveReferences() const {
347 return false;
348}
349
Andreas Huber881227d2016-08-02 14:20:21 -0700350bool Type::resultNeedsDeref() const {
351 return false;
352}
353
Yifan Hong3b320f82016-11-01 15:15:54 -0700354std::string Type::getCppStackType(bool specifyNamespaces) const {
355 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700356}
357
Yifan Hong3b320f82016-11-01 15:15:54 -0700358std::string Type::getCppResultType(bool specifyNamespaces) const {
359 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700360}
361
Yifan Hong3b320f82016-11-01 15:15:54 -0700362std::string Type::getCppArgumentType(bool specifyNamespaces) const {
363 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700364}
365
Andreas Huber2831d512016-08-15 09:33:47 -0700366void Type::emitJavaReaderWriterWithSuffix(
367 Formatter &out,
368 const std::string &parcelObj,
369 const std::string &argName,
370 bool isReader,
371 const std::string &suffix,
372 const std::string &extra) const {
373 out << parcelObj
374 << "."
375 << (isReader ? "read" : "write")
376 << suffix
377 << "(";
378
379 if (isReader) {
380 out << extra;
381 } else {
382 out << (extra.empty() ? "" : (extra + ", "));
383 out << argName;
384 }
385
386 out << ");\n";
387}
388
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700389status_t Type::emitVtsTypeDeclarations(Formatter &) const {
390 return OK;
391}
392
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700393status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700394 return emitVtsTypeDeclarations(out);
395}
396
Andreas Huber70a59e12016-08-16 12:57:01 -0700397bool Type::isJavaCompatible() const {
398 return true;
399}
400
Andreas Huber85eabdb2016-08-25 11:24:49 -0700401void Type::getAlignmentAndSize(size_t *, size_t *) const {
402 CHECK(!"Should not be here");
403}
404
Andreas Huber019d21d2016-10-03 12:59:47 -0700405void Type::appendToExportedTypesVector(
406 std::vector<const Type *> * /* exportedTypes */) const {
407}
408
Andreas Huber1c507272016-10-05 14:33:21 -0700409status_t Type::emitExportedHeader(
410 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700411 return OK;
412}
413
Yifan Hongbf459bc2016-08-23 16:50:37 -0700414////////////////////////////////////////
415
416TemplatedType::TemplatedType() : mElementType(nullptr) {
417}
418void TemplatedType::setElementType(Type *elementType) {
419 CHECK(mElementType == nullptr); // can only be set once.
420 mElementType = elementType;
421}
422
Andreas Huberc9410c72016-07-28 12:18:40 -0700423} // namespace android
424