blob: d7ae31a92184a5388c378853c29ccff174b41d2b [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 {
157 return true;
158}
159
Andreas Huber881227d2016-08-02 14:20:21 -0700160void Type::emitReaderWriterEmbedded(
161 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700162 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700163 const std::string &,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700164 const std::string &,
Andreas Huber881227d2016-08-02 14:20:21 -0700165 bool,
166 const std::string &,
167 bool,
168 bool,
169 ErrorMode,
170 const std::string &,
171 const std::string &) const {
172 CHECK(!"Should not be here");
173}
174
Andreas Huber2831d512016-08-15 09:33:47 -0700175void Type::emitJavaReaderWriter(
176 Formatter &out,
177 const std::string &parcelObj,
178 const std::string &argName,
179 bool isReader) const {
180 emitJavaReaderWriterWithSuffix(
181 out,
182 parcelObj,
183 argName,
184 isReader,
185 getJavaSuffix(),
186 "" /* extra */);
187}
188
Andreas Huber85eabdb2016-08-25 11:24:49 -0700189void Type::emitJavaFieldInitializer(
190 Formatter &out,
191 const std::string &fieldName) const {
Yifan Hong4ed13472016-11-02 10:44:11 -0700192 out << getJavaType()
Andreas Huber85eabdb2016-08-25 11:24:49 -0700193 << " "
194 << fieldName
195 << ";\n";
196}
197
198void Type::emitJavaFieldReaderWriter(
199 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700200 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700201 const std::string &,
202 const std::string &,
203 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700204 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700205 bool) const {
206 CHECK(!"Should not be here");
207}
208
Andreas Huber881227d2016-08-02 14:20:21 -0700209void Type::handleError(Formatter &out, ErrorMode mode) const {
210 switch (mode) {
211 case ErrorMode_Ignore:
212 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700213 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700214 break;
215 }
216
217 case ErrorMode_Goto:
218 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700219 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700220 break;
221 }
222
223 case ErrorMode_Break:
224 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700225 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700226 break;
227 }
Andreas Huber737080b2016-08-02 15:38:04 -0700228
229 case ErrorMode_Return:
230 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700231 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700232 break;
233 }
Andreas Huber881227d2016-08-02 14:20:21 -0700234 }
235}
236
237void Type::handleError2(Formatter &out, ErrorMode mode) const {
238 switch (mode) {
239 case ErrorMode_Goto:
240 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700241 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700242 break;
243 }
Andreas Huber737080b2016-08-02 15:38:04 -0700244
Andreas Huber881227d2016-08-02 14:20:21 -0700245 case ErrorMode_Break:
246 {
247 out << "break;\n";
248 break;
249 }
Andreas Huber737080b2016-08-02 15:38:04 -0700250
Andreas Huber881227d2016-08-02 14:20:21 -0700251 case ErrorMode_Ignore:
252 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700253 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700254 break;
255 }
Andreas Huber737080b2016-08-02 15:38:04 -0700256
257 case ErrorMode_Return:
258 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700259 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700260 break;
261 }
Andreas Huber881227d2016-08-02 14:20:21 -0700262 }
263}
264
265void Type::emitReaderWriterEmbeddedForTypeName(
266 Formatter &out,
267 const std::string &name,
268 bool nameIsPointer,
269 const std::string &parcelObj,
270 bool parcelObjIsPointer,
271 bool isReader,
272 ErrorMode mode,
273 const std::string &parentName,
274 const std::string &offsetText,
275 const std::string &typeName,
276 const std::string &childName) const {
277 const std::string parcelObjDeref =
278 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
279
280 const std::string parcelObjPointer =
281 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
282
283 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
284 const std::string namePointer = nameIsPointer ? name : ("&" + name);
285
Iliyan Malchev549e2592016-08-10 08:59:12 -0700286 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700287
288 if (isReader) {
289 out << "const_cast<"
290 << typeName
291 << " *>("
292 << namePointer
293 << ")->readEmbeddedFromParcel(\n";
294 } else {
295 out << nameDeref
296 << "writeEmbeddedToParcel(\n";
297 }
298
299 out.indent();
300 out.indent();
301
302 out << (isReader ? parcelObjDeref : parcelObjPointer)
303 << ",\n"
304 << parentName
305 << ",\n"
306 << offsetText;
307
308 if (!childName.empty()) {
309 out << ", &"
310 << childName;
311 }
312
313 out << ");\n\n";
314
315 out.unindent();
316 out.unindent();
317
318 handleError(out, mode);
319}
320
321status_t Type::emitTypeDeclarations(Formatter &) const {
322 return OK;
323}
324
Andreas Hubere3f769a2016-10-10 10:54:44 -0700325status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
326 return OK;
327}
328
Andreas Huber881227d2016-08-02 14:20:21 -0700329status_t Type::emitTypeDefinitions(
330 Formatter &, const std::string) const {
331 return OK;
332}
333
Andreas Huber85eabdb2016-08-25 11:24:49 -0700334status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700335 return OK;
336}
337
Andreas Huber881227d2016-08-02 14:20:21 -0700338bool Type::needsEmbeddedReadWrite() const {
339 return false;
340}
341
Yifan Hongbf459bc2016-08-23 16:50:37 -0700342bool Type::needsResolveReferences() const {
343 return false;
344}
345
Andreas Huber881227d2016-08-02 14:20:21 -0700346bool Type::resultNeedsDeref() const {
347 return false;
348}
349
Yifan Hong3b320f82016-11-01 15:15:54 -0700350std::string Type::getCppStackType(bool specifyNamespaces) const {
351 return getCppType(StorageMode_Stack, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700352}
353
Yifan Hong3b320f82016-11-01 15:15:54 -0700354std::string Type::getCppResultType(bool specifyNamespaces) const {
355 return getCppType(StorageMode_Result, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700356}
357
Yifan Hong3b320f82016-11-01 15:15:54 -0700358std::string Type::getCppArgumentType(bool specifyNamespaces) const {
359 return getCppType(StorageMode_Argument, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700360}
361
Andreas Huber2831d512016-08-15 09:33:47 -0700362void Type::emitJavaReaderWriterWithSuffix(
363 Formatter &out,
364 const std::string &parcelObj,
365 const std::string &argName,
366 bool isReader,
367 const std::string &suffix,
368 const std::string &extra) const {
369 out << parcelObj
370 << "."
371 << (isReader ? "read" : "write")
372 << suffix
373 << "(";
374
375 if (isReader) {
376 out << extra;
377 } else {
378 out << (extra.empty() ? "" : (extra + ", "));
379 out << argName;
380 }
381
382 out << ");\n";
383}
384
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700385status_t Type::emitVtsTypeDeclarations(Formatter &) const {
386 return OK;
387}
388
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700389status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700390 return emitVtsTypeDeclarations(out);
391}
392
Andreas Huber70a59e12016-08-16 12:57:01 -0700393bool Type::isJavaCompatible() const {
394 return true;
395}
396
Andreas Huber85eabdb2016-08-25 11:24:49 -0700397void Type::getAlignmentAndSize(size_t *, size_t *) const {
398 CHECK(!"Should not be here");
399}
400
Andreas Huber019d21d2016-10-03 12:59:47 -0700401void Type::appendToExportedTypesVector(
402 std::vector<const Type *> * /* exportedTypes */) const {
403}
404
Andreas Huber1c507272016-10-05 14:33:21 -0700405status_t Type::emitExportedHeader(
406 Formatter & /* out */, bool /* forJava */) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700407 return OK;
408}
409
Yifan Hongbf459bc2016-08-23 16:50:37 -0700410////////////////////////////////////////
411
412TemplatedType::TemplatedType() : mElementType(nullptr) {
413}
414void TemplatedType::setElementType(Type *elementType) {
415 CHECK(mElementType == nullptr); // can only be set once.
416 mElementType = elementType;
417}
418
Andreas Huberc9410c72016-07-28 12:18:40 -0700419} // namespace android
420