blob: db877d4554276d137c4d415f3dbdc000fb09920b [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
Andreas Huber881227d2016-08-02 14:20:21 -0700113void Type::emitReaderWriter(
114 Formatter &,
115 const std::string &,
116 const std::string &,
117 bool,
118 bool,
119 ErrorMode) const {
120 CHECK(!"Should not be here");
121}
122
Yifan Hongbf459bc2016-08-23 16:50:37 -0700123void Type::emitResolveReferences(
124 Formatter &,
125 const std::string &,
126 bool,
127 const std::string &,
128 bool,
129 bool,
130 ErrorMode) const {
131 CHECK(!"Should not be here");
132}
133
134void Type::emitResolveReferencesEmbedded(
135 Formatter &,
136 size_t,
137 const std::string &,
138 const std::string &,
139 bool,
140 const std::string &,
141 bool,
142 bool,
143 ErrorMode,
144 const std::string &,
145 const std::string &) const {
146 CHECK(!"Should not be here");
147}
148
Yifan Hong00f47172016-09-30 14:40:45 -0700149bool Type::useParentInEmitResolveReferencesEmbedded() const {
150 return true;
151}
152
Andreas Huber881227d2016-08-02 14:20:21 -0700153void Type::emitReaderWriterEmbedded(
154 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700155 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700156 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700157 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700158 bool,
159 const std::string &,
160 bool,
161 bool,
162 ErrorMode,
163 const std::string &,
164 const std::string &) const {
165 CHECK(!"Should not be here");
166}
167
Andreas Huber2831d512016-08-15 09:33:47 -0700168void Type::emitJavaReaderWriter(
169 Formatter &out,
170 const std::string &parcelObj,
171 const std::string &argName,
172 bool isReader) const {
173 emitJavaReaderWriterWithSuffix(
174 out,
175 parcelObj,
176 argName,
177 isReader,
178 getJavaSuffix(),
179 "" /* extra */);
180}
181
Andreas Huber85eabdb2016-08-25 11:24:49 -0700182void Type::emitJavaFieldInitializer(
183 Formatter &out,
184 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700185 std::string extra;
186 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700187 << " "
188 << fieldName
189 << ";\n";
190}
191
192void Type::emitJavaFieldReaderWriter(
193 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700194 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700195 const std::string &,
196 const std::string &,
197 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700198 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700199 bool) const {
200 CHECK(!"Should not be here");
201}
202
Andreas Huber881227d2016-08-02 14:20:21 -0700203void Type::handleError(Formatter &out, ErrorMode mode) const {
204 switch (mode) {
205 case ErrorMode_Ignore:
206 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700207 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700208 break;
209 }
210
211 case ErrorMode_Goto:
212 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700213 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700214 break;
215 }
216
217 case ErrorMode_Break:
218 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700219 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700220 break;
221 }
Andreas Huber737080b2016-08-02 15:38:04 -0700222
223 case ErrorMode_Return:
224 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700225 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700226 break;
227 }
Andreas Huber881227d2016-08-02 14:20:21 -0700228 }
229}
230
231void Type::handleError2(Formatter &out, ErrorMode mode) const {
232 switch (mode) {
233 case ErrorMode_Goto:
234 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700235 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700236 break;
237 }
Andreas Huber737080b2016-08-02 15:38:04 -0700238
Andreas Huber881227d2016-08-02 14:20:21 -0700239 case ErrorMode_Break:
240 {
241 out << "break;\n";
242 break;
243 }
Andreas Huber737080b2016-08-02 15:38:04 -0700244
Andreas Huber881227d2016-08-02 14:20:21 -0700245 case ErrorMode_Ignore:
246 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700247 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700248 break;
249 }
Andreas Huber737080b2016-08-02 15:38:04 -0700250
251 case ErrorMode_Return:
252 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700253 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700254 break;
255 }
Andreas Huber881227d2016-08-02 14:20:21 -0700256 }
257}
258
259void Type::emitReaderWriterEmbeddedForTypeName(
260 Formatter &out,
261 const std::string &name,
262 bool nameIsPointer,
263 const std::string &parcelObj,
264 bool parcelObjIsPointer,
265 bool isReader,
266 ErrorMode mode,
267 const std::string &parentName,
268 const std::string &offsetText,
269 const std::string &typeName,
270 const std::string &childName) const {
271 const std::string parcelObjDeref =
272 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
273
274 const std::string parcelObjPointer =
275 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
276
277 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
278 const std::string namePointer = nameIsPointer ? name : ("&" + name);
279
Iliyan Malchev549e2592016-08-10 08:59:12 -0700280 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700281
282 if (isReader) {
283 out << "const_cast<"
284 << typeName
285 << " *>("
286 << namePointer
287 << ")->readEmbeddedFromParcel(\n";
288 } else {
289 out << nameDeref
290 << "writeEmbeddedToParcel(\n";
291 }
292
293 out.indent();
294 out.indent();
295
296 out << (isReader ? parcelObjDeref : parcelObjPointer)
297 << ",\n"
298 << parentName
299 << ",\n"
300 << offsetText;
301
302 if (!childName.empty()) {
303 out << ", &"
304 << childName;
305 }
306
307 out << ");\n\n";
308
309 out.unindent();
310 out.unindent();
311
312 handleError(out, mode);
313}
314
315status_t Type::emitTypeDeclarations(Formatter &) const {
316 return OK;
317}
318
319status_t Type::emitTypeDefinitions(
320 Formatter &, const std::string) const {
321 return OK;
322}
323
Andreas Huber85eabdb2016-08-25 11:24:49 -0700324status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700325 return OK;
326}
327
Andreas Huber881227d2016-08-02 14:20:21 -0700328bool Type::needsEmbeddedReadWrite() const {
329 return false;
330}
331
Yifan Hongbf459bc2016-08-23 16:50:37 -0700332bool Type::needsResolveReferences() const {
333 return false;
334}
335
Andreas Huber881227d2016-08-02 14:20:21 -0700336bool Type::resultNeedsDeref() const {
337 return false;
338}
339
Steven Moreland979e0992016-09-07 09:18:08 -0700340std::string Type::getCppType(std::string *extra,
341 bool specifyNamespaces) const {
342 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700343}
344
Steven Moreland979e0992016-09-07 09:18:08 -0700345std::string Type::getCppResultType(std::string *extra,
346 bool specifyNamespaces) const {
347 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700348}
349
Steven Moreland979e0992016-09-07 09:18:08 -0700350std::string Type::getCppArgumentType(std::string *extra,
351 bool specifyNamespaces) const {
352 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700353}
354
Andreas Huber2831d512016-08-15 09:33:47 -0700355void Type::emitJavaReaderWriterWithSuffix(
356 Formatter &out,
357 const std::string &parcelObj,
358 const std::string &argName,
359 bool isReader,
360 const std::string &suffix,
361 const std::string &extra) const {
362 out << parcelObj
363 << "."
364 << (isReader ? "read" : "write")
365 << suffix
366 << "(";
367
368 if (isReader) {
369 out << extra;
370 } else {
371 out << (extra.empty() ? "" : (extra + ", "));
372 out << argName;
373 }
374
375 out << ");\n";
376}
377
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700378status_t Type::emitVtsTypeDeclarations(Formatter &) const {
379 return OK;
380}
381
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700382status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700383 return emitVtsTypeDeclarations(out);
384}
385
Andreas Huber70a59e12016-08-16 12:57:01 -0700386bool Type::isJavaCompatible() const {
387 return true;
388}
389
Andreas Huber85eabdb2016-08-25 11:24:49 -0700390void Type::getAlignmentAndSize(size_t *, size_t *) const {
391 CHECK(!"Should not be here");
392}
393
Andreas Huber019d21d2016-10-03 12:59:47 -0700394void Type::appendToExportedTypesVector(
395 std::vector<const Type *> * /* exportedTypes */) const {
396}
397
398status_t Type::emitExportedHeader(Formatter & /* out */) const {
399 return OK;
400}
401
Yifan Hongbf459bc2016-08-23 16:50:37 -0700402////////////////////////////////////////
403
404TemplatedType::TemplatedType() : mElementType(nullptr) {
405}
406void TemplatedType::setElementType(Type *elementType) {
407 CHECK(mElementType == nullptr); // can only be set once.
408 mElementType = elementType;
409}
410
Andreas Huberc9410c72016-07-28 12:18:40 -0700411} // namespace android
412