blob: d81eeefa5f02f7b2cbca7e5738bfb6e4c1da9977 [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
Steven Moreland979e0992016-09-07 09:18:08 -070091std::string Type::getCppType(StorageMode, std::string *, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070092 CHECK(!"Should not be here");
93 return std::string();
94}
95
Andreas Huber4c865b72016-09-14 15:26:27 -070096std::string Type::getJavaType(
97 std::string *extra, bool /* forInitializer */) const {
98 CHECK(!"Should not be here");
99 extra->clear();
100 return std::string();
101}
102
Andreas Huber85eabdb2016-08-25 11:24:49 -0700103std::string Type::getJavaWrapperType() const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700104 std::string extra;
105 return getJavaType(&extra);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700106}
107
Andreas Huber2831d512016-08-15 09:33:47 -0700108std::string Type::getJavaSuffix() const {
109 CHECK(!"Should not be here");
110 return std::string();
111}
112
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700113std::string Type::getVtsType() const {
114 CHECK(!"Should not be here");
115 return std::string();
116}
117
Andreas Huber881227d2016-08-02 14:20:21 -0700118void Type::emitReaderWriter(
119 Formatter &,
120 const std::string &,
121 const std::string &,
122 bool,
123 bool,
124 ErrorMode) const {
125 CHECK(!"Should not be here");
126}
127
Yifan Hongbf459bc2016-08-23 16:50:37 -0700128void Type::emitResolveReferences(
129 Formatter &,
130 const std::string &,
131 bool,
132 const std::string &,
133 bool,
134 bool,
135 ErrorMode) const {
136 CHECK(!"Should not be here");
137}
138
139void Type::emitResolveReferencesEmbedded(
140 Formatter &,
141 size_t,
142 const std::string &,
143 const std::string &,
144 bool,
145 const std::string &,
146 bool,
147 bool,
148 ErrorMode,
149 const std::string &,
150 const std::string &) const {
151 CHECK(!"Should not be here");
152}
153
Yifan Hong00f47172016-09-30 14:40:45 -0700154bool Type::useParentInEmitResolveReferencesEmbedded() const {
155 return true;
156}
157
Andreas Huber881227d2016-08-02 14:20:21 -0700158void Type::emitReaderWriterEmbedded(
159 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700160 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700161 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700162 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700163 bool,
164 const std::string &,
165 bool,
166 bool,
167 ErrorMode,
168 const std::string &,
169 const std::string &) const {
170 CHECK(!"Should not be here");
171}
172
Andreas Huber2831d512016-08-15 09:33:47 -0700173void Type::emitJavaReaderWriter(
174 Formatter &out,
175 const std::string &parcelObj,
176 const std::string &argName,
177 bool isReader) const {
178 emitJavaReaderWriterWithSuffix(
179 out,
180 parcelObj,
181 argName,
182 isReader,
183 getJavaSuffix(),
184 "" /* extra */);
185}
186
Andreas Huber85eabdb2016-08-25 11:24:49 -0700187void Type::emitJavaFieldInitializer(
188 Formatter &out,
189 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700190 std::string extra;
191 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700192 << " "
193 << fieldName
194 << ";\n";
195}
196
197void Type::emitJavaFieldReaderWriter(
198 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700199 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700200 const std::string &,
201 const std::string &,
202 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700203 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700204 bool) const {
205 CHECK(!"Should not be here");
206}
207
Andreas Huber881227d2016-08-02 14:20:21 -0700208void Type::handleError(Formatter &out, ErrorMode mode) const {
209 switch (mode) {
210 case ErrorMode_Ignore:
211 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700212 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700213 break;
214 }
215
216 case ErrorMode_Goto:
217 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700218 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700219 break;
220 }
221
222 case ErrorMode_Break:
223 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700224 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700225 break;
226 }
Andreas Huber737080b2016-08-02 15:38:04 -0700227
228 case ErrorMode_Return:
229 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700230 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700231 break;
232 }
Andreas Huber881227d2016-08-02 14:20:21 -0700233 }
234}
235
236void Type::handleError2(Formatter &out, ErrorMode mode) const {
237 switch (mode) {
238 case ErrorMode_Goto:
239 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700240 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700241 break;
242 }
Andreas Huber737080b2016-08-02 15:38:04 -0700243
Andreas Huber881227d2016-08-02 14:20:21 -0700244 case ErrorMode_Break:
245 {
246 out << "break;\n";
247 break;
248 }
Andreas Huber737080b2016-08-02 15:38:04 -0700249
Andreas Huber881227d2016-08-02 14:20:21 -0700250 case ErrorMode_Ignore:
251 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700252 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700253 break;
254 }
Andreas Huber737080b2016-08-02 15:38:04 -0700255
256 case ErrorMode_Return:
257 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700258 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700259 break;
260 }
Andreas Huber881227d2016-08-02 14:20:21 -0700261 }
262}
263
264void Type::emitReaderWriterEmbeddedForTypeName(
265 Formatter &out,
266 const std::string &name,
267 bool nameIsPointer,
268 const std::string &parcelObj,
269 bool parcelObjIsPointer,
270 bool isReader,
271 ErrorMode mode,
272 const std::string &parentName,
273 const std::string &offsetText,
274 const std::string &typeName,
275 const std::string &childName) const {
276 const std::string parcelObjDeref =
277 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
278
279 const std::string parcelObjPointer =
280 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
281
282 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
283 const std::string namePointer = nameIsPointer ? name : ("&" + name);
284
Iliyan Malchev549e2592016-08-10 08:59:12 -0700285 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700286
287 if (isReader) {
288 out << "const_cast<"
289 << typeName
290 << " *>("
291 << namePointer
292 << ")->readEmbeddedFromParcel(\n";
293 } else {
294 out << nameDeref
295 << "writeEmbeddedToParcel(\n";
296 }
297
298 out.indent();
299 out.indent();
300
301 out << (isReader ? parcelObjDeref : parcelObjPointer)
302 << ",\n"
303 << parentName
304 << ",\n"
305 << offsetText;
306
307 if (!childName.empty()) {
308 out << ", &"
309 << childName;
310 }
311
312 out << ");\n\n";
313
314 out.unindent();
315 out.unindent();
316
317 handleError(out, mode);
318}
319
320status_t Type::emitTypeDeclarations(Formatter &) const {
321 return OK;
322}
323
324status_t Type::emitTypeDefinitions(
325 Formatter &, const std::string) const {
326 return OK;
327}
328
Andreas Huber85eabdb2016-08-25 11:24:49 -0700329status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700330 return OK;
331}
332
Andreas Huber881227d2016-08-02 14:20:21 -0700333bool Type::needsEmbeddedReadWrite() const {
334 return false;
335}
336
Yifan Hongbf459bc2016-08-23 16:50:37 -0700337bool Type::needsResolveReferences() const {
338 return false;
339}
340
Andreas Huber881227d2016-08-02 14:20:21 -0700341bool Type::resultNeedsDeref() const {
342 return false;
343}
344
Steven Moreland979e0992016-09-07 09:18:08 -0700345std::string Type::getCppType(std::string *extra,
346 bool specifyNamespaces) const {
347 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700348}
349
Steven Moreland979e0992016-09-07 09:18:08 -0700350std::string Type::getCppResultType(std::string *extra,
351 bool specifyNamespaces) const {
352 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700353}
354
Steven Moreland979e0992016-09-07 09:18:08 -0700355std::string Type::getCppArgumentType(std::string *extra,
356 bool specifyNamespaces) const {
357 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700358}
359
Andreas Huber2831d512016-08-15 09:33:47 -0700360void Type::emitJavaReaderWriterWithSuffix(
361 Formatter &out,
362 const std::string &parcelObj,
363 const std::string &argName,
364 bool isReader,
365 const std::string &suffix,
366 const std::string &extra) const {
367 out << parcelObj
368 << "."
369 << (isReader ? "read" : "write")
370 << suffix
371 << "(";
372
373 if (isReader) {
374 out << extra;
375 } else {
376 out << (extra.empty() ? "" : (extra + ", "));
377 out << argName;
378 }
379
380 out << ");\n";
381}
382
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700383status_t Type::emitVtsTypeDeclarations(Formatter &) const {
384 return OK;
385}
386
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700387status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700388 return emitVtsTypeDeclarations(out);
389}
390
Andreas Huber70a59e12016-08-16 12:57:01 -0700391bool Type::isJavaCompatible() const {
392 return true;
393}
394
Andreas Huber85eabdb2016-08-25 11:24:49 -0700395void Type::getAlignmentAndSize(size_t *, size_t *) const {
396 CHECK(!"Should not be here");
397}
398
Andreas Huber019d21d2016-10-03 12:59:47 -0700399void Type::appendToExportedTypesVector(
400 std::vector<const Type *> * /* exportedTypes */) const {
401}
402
403status_t Type::emitExportedHeader(Formatter & /* out */) const {
404 return OK;
405}
406
Yifan Hongbf459bc2016-08-23 16:50:37 -0700407////////////////////////////////////////
408
409TemplatedType::TemplatedType() : mElementType(nullptr) {
410}
411void TemplatedType::setElementType(Type *elementType) {
412 CHECK(mElementType == nullptr); // can only be set once.
413 mElementType = elementType;
414}
415
Andreas Huberc9410c72016-07-28 12:18:40 -0700416} // namespace android
417