blob: 54f5956c42a3ffa5e85907b5473b7093cd69aeb8 [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 &,
157 bool,
158 const std::string &,
159 bool,
160 bool,
161 ErrorMode,
162 const std::string &,
163 const std::string &) const {
164 CHECK(!"Should not be here");
165}
166
Andreas Huber2831d512016-08-15 09:33:47 -0700167void Type::emitJavaReaderWriter(
168 Formatter &out,
169 const std::string &parcelObj,
170 const std::string &argName,
171 bool isReader) const {
172 emitJavaReaderWriterWithSuffix(
173 out,
174 parcelObj,
175 argName,
176 isReader,
177 getJavaSuffix(),
178 "" /* extra */);
179}
180
Andreas Huber85eabdb2016-08-25 11:24:49 -0700181void Type::emitJavaFieldInitializer(
182 Formatter &out,
183 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700184 std::string extra;
185 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700186 << " "
187 << fieldName
188 << ";\n";
189}
190
191void Type::emitJavaFieldReaderWriter(
192 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700193 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700194 const std::string &,
195 const std::string &,
196 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700197 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700198 bool) const {
199 CHECK(!"Should not be here");
200}
201
Andreas Huber881227d2016-08-02 14:20:21 -0700202void Type::handleError(Formatter &out, ErrorMode mode) const {
203 switch (mode) {
204 case ErrorMode_Ignore:
205 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700206 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700207 break;
208 }
209
210 case ErrorMode_Goto:
211 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700212 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700213 break;
214 }
215
216 case ErrorMode_Break:
217 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700218 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700219 break;
220 }
Andreas Huber737080b2016-08-02 15:38:04 -0700221
222 case ErrorMode_Return:
223 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700224 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700225 break;
226 }
Andreas Huber881227d2016-08-02 14:20:21 -0700227 }
228}
229
230void Type::handleError2(Formatter &out, ErrorMode mode) const {
231 switch (mode) {
232 case ErrorMode_Goto:
233 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700234 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700235 break;
236 }
Andreas Huber737080b2016-08-02 15:38:04 -0700237
Andreas Huber881227d2016-08-02 14:20:21 -0700238 case ErrorMode_Break:
239 {
240 out << "break;\n";
241 break;
242 }
Andreas Huber737080b2016-08-02 15:38:04 -0700243
Andreas Huber881227d2016-08-02 14:20:21 -0700244 case ErrorMode_Ignore:
245 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700246 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700247 break;
248 }
Andreas Huber737080b2016-08-02 15:38:04 -0700249
250 case ErrorMode_Return:
251 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700252 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700253 break;
254 }
Andreas Huber881227d2016-08-02 14:20:21 -0700255 }
256}
257
258void Type::emitReaderWriterEmbeddedForTypeName(
259 Formatter &out,
260 const std::string &name,
261 bool nameIsPointer,
262 const std::string &parcelObj,
263 bool parcelObjIsPointer,
264 bool isReader,
265 ErrorMode mode,
266 const std::string &parentName,
267 const std::string &offsetText,
268 const std::string &typeName,
269 const std::string &childName) const {
270 const std::string parcelObjDeref =
271 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
272
273 const std::string parcelObjPointer =
274 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
275
276 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
277 const std::string namePointer = nameIsPointer ? name : ("&" + name);
278
Iliyan Malchev549e2592016-08-10 08:59:12 -0700279 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700280
281 if (isReader) {
282 out << "const_cast<"
283 << typeName
284 << " *>("
285 << namePointer
286 << ")->readEmbeddedFromParcel(\n";
287 } else {
288 out << nameDeref
289 << "writeEmbeddedToParcel(\n";
290 }
291
292 out.indent();
293 out.indent();
294
295 out << (isReader ? parcelObjDeref : parcelObjPointer)
296 << ",\n"
297 << parentName
298 << ",\n"
299 << offsetText;
300
301 if (!childName.empty()) {
302 out << ", &"
303 << childName;
304 }
305
306 out << ");\n\n";
307
308 out.unindent();
309 out.unindent();
310
311 handleError(out, mode);
312}
313
314status_t Type::emitTypeDeclarations(Formatter &) const {
315 return OK;
316}
317
318status_t Type::emitTypeDefinitions(
319 Formatter &, const std::string) const {
320 return OK;
321}
322
Andreas Huber85eabdb2016-08-25 11:24:49 -0700323status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700324 return OK;
325}
326
Andreas Huber881227d2016-08-02 14:20:21 -0700327bool Type::needsEmbeddedReadWrite() const {
328 return false;
329}
330
Yifan Hongbf459bc2016-08-23 16:50:37 -0700331bool Type::needsResolveReferences() const {
332 return false;
333}
334
Andreas Huber881227d2016-08-02 14:20:21 -0700335bool Type::resultNeedsDeref() const {
336 return false;
337}
338
Steven Moreland979e0992016-09-07 09:18:08 -0700339std::string Type::getCppType(std::string *extra,
340 bool specifyNamespaces) const {
341 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700342}
343
Steven Moreland979e0992016-09-07 09:18:08 -0700344std::string Type::getCppResultType(std::string *extra,
345 bool specifyNamespaces) const {
346 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700347}
348
Steven Moreland979e0992016-09-07 09:18:08 -0700349std::string Type::getCppArgumentType(std::string *extra,
350 bool specifyNamespaces) const {
351 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700352}
353
Andreas Huber2831d512016-08-15 09:33:47 -0700354void Type::emitJavaReaderWriterWithSuffix(
355 Formatter &out,
356 const std::string &parcelObj,
357 const std::string &argName,
358 bool isReader,
359 const std::string &suffix,
360 const std::string &extra) const {
361 out << parcelObj
362 << "."
363 << (isReader ? "read" : "write")
364 << suffix
365 << "(";
366
367 if (isReader) {
368 out << extra;
369 } else {
370 out << (extra.empty() ? "" : (extra + ", "));
371 out << argName;
372 }
373
374 out << ");\n";
375}
376
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700377status_t Type::emitVtsTypeDeclarations(Formatter &) const {
378 return OK;
379}
380
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700381status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700382 return emitVtsTypeDeclarations(out);
383}
384
Andreas Huber70a59e12016-08-16 12:57:01 -0700385bool Type::isJavaCompatible() const {
386 return true;
387}
388
Andreas Huber85eabdb2016-08-25 11:24:49 -0700389void Type::getAlignmentAndSize(size_t *, size_t *) const {
390 CHECK(!"Should not be here");
391}
392
Yifan Hongbf459bc2016-08-23 16:50:37 -0700393////////////////////////////////////////
394
395TemplatedType::TemplatedType() : mElementType(nullptr) {
396}
397void TemplatedType::setElementType(Type *elementType) {
398 CHECK(mElementType == nullptr); // can only be set once.
399 mElementType = elementType;
400}
401
Andreas Huberc9410c72016-07-28 12:18:40 -0700402} // namespace android
403