blob: 0b2b7153d20325edf66b924f29690a44b8d2c45c [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
Andreas Huber881227d2016-08-02 14:20:21 -0700149void Type::emitReaderWriterEmbedded(
150 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700151 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700152 const std::string &,
153 bool,
154 const std::string &,
155 bool,
156 bool,
157 ErrorMode,
158 const std::string &,
159 const std::string &) const {
160 CHECK(!"Should not be here");
161}
162
Andreas Huber2831d512016-08-15 09:33:47 -0700163void Type::emitJavaReaderWriter(
164 Formatter &out,
165 const std::string &parcelObj,
166 const std::string &argName,
167 bool isReader) const {
168 emitJavaReaderWriterWithSuffix(
169 out,
170 parcelObj,
171 argName,
172 isReader,
173 getJavaSuffix(),
174 "" /* extra */);
175}
176
Andreas Huber85eabdb2016-08-25 11:24:49 -0700177void Type::emitJavaFieldInitializer(
178 Formatter &out,
179 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700180 std::string extra;
181 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700182 << " "
183 << fieldName
184 << ";\n";
185}
186
187void Type::emitJavaFieldReaderWriter(
188 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700189 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700190 const std::string &,
191 const std::string &,
192 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700193 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700194 bool) const {
195 CHECK(!"Should not be here");
196}
197
Andreas Huber881227d2016-08-02 14:20:21 -0700198void Type::handleError(Formatter &out, ErrorMode mode) const {
199 switch (mode) {
200 case ErrorMode_Ignore:
201 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700202 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700203 break;
204 }
205
206 case ErrorMode_Goto:
207 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700208 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700209 break;
210 }
211
212 case ErrorMode_Break:
213 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700214 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700215 break;
216 }
Andreas Huber737080b2016-08-02 15:38:04 -0700217
218 case ErrorMode_Return:
219 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700220 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700221 break;
222 }
Andreas Huber881227d2016-08-02 14:20:21 -0700223 }
224}
225
226void Type::handleError2(Formatter &out, ErrorMode mode) const {
227 switch (mode) {
228 case ErrorMode_Goto:
229 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700230 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700231 break;
232 }
Andreas Huber737080b2016-08-02 15:38:04 -0700233
Andreas Huber881227d2016-08-02 14:20:21 -0700234 case ErrorMode_Break:
235 {
236 out << "break;\n";
237 break;
238 }
Andreas Huber737080b2016-08-02 15:38:04 -0700239
Andreas Huber881227d2016-08-02 14:20:21 -0700240 case ErrorMode_Ignore:
241 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700242 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700243 break;
244 }
Andreas Huber737080b2016-08-02 15:38:04 -0700245
246 case ErrorMode_Return:
247 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700248 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700249 break;
250 }
Andreas Huber881227d2016-08-02 14:20:21 -0700251 }
252}
253
254void Type::emitReaderWriterEmbeddedForTypeName(
255 Formatter &out,
256 const std::string &name,
257 bool nameIsPointer,
258 const std::string &parcelObj,
259 bool parcelObjIsPointer,
260 bool isReader,
261 ErrorMode mode,
262 const std::string &parentName,
263 const std::string &offsetText,
264 const std::string &typeName,
265 const std::string &childName) const {
266 const std::string parcelObjDeref =
267 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
268
269 const std::string parcelObjPointer =
270 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
271
272 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
273 const std::string namePointer = nameIsPointer ? name : ("&" + name);
274
Iliyan Malchev549e2592016-08-10 08:59:12 -0700275 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700276
277 if (isReader) {
278 out << "const_cast<"
279 << typeName
280 << " *>("
281 << namePointer
282 << ")->readEmbeddedFromParcel(\n";
283 } else {
284 out << nameDeref
285 << "writeEmbeddedToParcel(\n";
286 }
287
288 out.indent();
289 out.indent();
290
291 out << (isReader ? parcelObjDeref : parcelObjPointer)
292 << ",\n"
293 << parentName
294 << ",\n"
295 << offsetText;
296
297 if (!childName.empty()) {
298 out << ", &"
299 << childName;
300 }
301
302 out << ");\n\n";
303
304 out.unindent();
305 out.unindent();
306
307 handleError(out, mode);
308}
309
310status_t Type::emitTypeDeclarations(Formatter &) const {
311 return OK;
312}
313
314status_t Type::emitTypeDefinitions(
315 Formatter &, const std::string) const {
316 return OK;
317}
318
Andreas Huber85eabdb2016-08-25 11:24:49 -0700319status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700320 return OK;
321}
322
Andreas Huber881227d2016-08-02 14:20:21 -0700323bool Type::needsEmbeddedReadWrite() const {
324 return false;
325}
326
Yifan Hongbf459bc2016-08-23 16:50:37 -0700327bool Type::needsResolveReferences() const {
328 return false;
329}
330
Andreas Huber881227d2016-08-02 14:20:21 -0700331bool Type::resultNeedsDeref() const {
332 return false;
333}
334
Steven Moreland979e0992016-09-07 09:18:08 -0700335std::string Type::getCppType(std::string *extra,
336 bool specifyNamespaces) const {
337 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700338}
339
Steven Moreland979e0992016-09-07 09:18:08 -0700340std::string Type::getCppResultType(std::string *extra,
341 bool specifyNamespaces) const {
342 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700343}
344
Steven Moreland979e0992016-09-07 09:18:08 -0700345std::string Type::getCppArgumentType(std::string *extra,
346 bool specifyNamespaces) const {
347 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700348}
349
Andreas Huber2831d512016-08-15 09:33:47 -0700350void Type::emitJavaReaderWriterWithSuffix(
351 Formatter &out,
352 const std::string &parcelObj,
353 const std::string &argName,
354 bool isReader,
355 const std::string &suffix,
356 const std::string &extra) const {
357 out << parcelObj
358 << "."
359 << (isReader ? "read" : "write")
360 << suffix
361 << "(";
362
363 if (isReader) {
364 out << extra;
365 } else {
366 out << (extra.empty() ? "" : (extra + ", "));
367 out << argName;
368 }
369
370 out << ");\n";
371}
372
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700373status_t Type::emitVtsTypeDeclarations(Formatter &) const {
374 return OK;
375}
376
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700377status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700378 return emitVtsTypeDeclarations(out);
379}
380
Andreas Huber70a59e12016-08-16 12:57:01 -0700381bool Type::isJavaCompatible() const {
382 return true;
383}
384
Andreas Huber85eabdb2016-08-25 11:24:49 -0700385void Type::getAlignmentAndSize(size_t *, size_t *) const {
386 CHECK(!"Should not be here");
387}
388
Yifan Hongbf459bc2016-08-23 16:50:37 -0700389////////////////////////////////////////
390
391TemplatedType::TemplatedType() : mElementType(nullptr) {
392}
393void TemplatedType::setElementType(Type *elementType) {
394 CHECK(mElementType == nullptr); // can only be set once.
395 mElementType = elementType;
396}
397
Andreas Huberc9410c72016-07-28 12:18:40 -0700398} // namespace android
399