blob: e54fdc50b3e26c4d1dbdccfb988c7f6fa5f43419 [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
123void Type::emitReaderWriterEmbedded(
124 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700125 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700126 const std::string &,
127 bool,
128 const std::string &,
129 bool,
130 bool,
131 ErrorMode,
132 const std::string &,
133 const std::string &) const {
134 CHECK(!"Should not be here");
135}
136
Andreas Huber2831d512016-08-15 09:33:47 -0700137void Type::emitJavaReaderWriter(
138 Formatter &out,
139 const std::string &parcelObj,
140 const std::string &argName,
141 bool isReader) const {
142 emitJavaReaderWriterWithSuffix(
143 out,
144 parcelObj,
145 argName,
146 isReader,
147 getJavaSuffix(),
148 "" /* extra */);
149}
150
Andreas Huber85eabdb2016-08-25 11:24:49 -0700151void Type::emitJavaFieldInitializer(
152 Formatter &out,
153 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700154 std::string extra;
155 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700156 << " "
157 << fieldName
158 << ";\n";
159}
160
161void Type::emitJavaFieldReaderWriter(
162 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700163 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700164 const std::string &,
165 const std::string &,
166 const std::string &,
Andreas Huber709b62d2016-09-19 11:21:18 -0700167 const std::string &,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700168 bool) const {
169 CHECK(!"Should not be here");
170}
171
Andreas Huber881227d2016-08-02 14:20:21 -0700172void Type::handleError(Formatter &out, ErrorMode mode) const {
173 switch (mode) {
174 case ErrorMode_Ignore:
175 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700176 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700177 break;
178 }
179
180 case ErrorMode_Goto:
181 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700182 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700183 break;
184 }
185
186 case ErrorMode_Break:
187 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700188 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700189 break;
190 }
Andreas Huber737080b2016-08-02 15:38:04 -0700191
192 case ErrorMode_Return:
193 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700194 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700195 break;
196 }
Andreas Huber881227d2016-08-02 14:20:21 -0700197 }
198}
199
200void Type::handleError2(Formatter &out, ErrorMode mode) const {
201 switch (mode) {
202 case ErrorMode_Goto:
203 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700204 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700205 break;
206 }
Andreas Huber737080b2016-08-02 15:38:04 -0700207
Andreas Huber881227d2016-08-02 14:20:21 -0700208 case ErrorMode_Break:
209 {
210 out << "break;\n";
211 break;
212 }
Andreas Huber737080b2016-08-02 15:38:04 -0700213
Andreas Huber881227d2016-08-02 14:20:21 -0700214 case ErrorMode_Ignore:
215 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700216 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700217 break;
218 }
Andreas Huber737080b2016-08-02 15:38:04 -0700219
220 case ErrorMode_Return:
221 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700222 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700223 break;
224 }
Andreas Huber881227d2016-08-02 14:20:21 -0700225 }
226}
227
228void Type::emitReaderWriterEmbeddedForTypeName(
229 Formatter &out,
230 const std::string &name,
231 bool nameIsPointer,
232 const std::string &parcelObj,
233 bool parcelObjIsPointer,
234 bool isReader,
235 ErrorMode mode,
236 const std::string &parentName,
237 const std::string &offsetText,
238 const std::string &typeName,
239 const std::string &childName) const {
240 const std::string parcelObjDeref =
241 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
242
243 const std::string parcelObjPointer =
244 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
245
246 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
247 const std::string namePointer = nameIsPointer ? name : ("&" + name);
248
Iliyan Malchev549e2592016-08-10 08:59:12 -0700249 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700250
251 if (isReader) {
252 out << "const_cast<"
253 << typeName
254 << " *>("
255 << namePointer
256 << ")->readEmbeddedFromParcel(\n";
257 } else {
258 out << nameDeref
259 << "writeEmbeddedToParcel(\n";
260 }
261
262 out.indent();
263 out.indent();
264
265 out << (isReader ? parcelObjDeref : parcelObjPointer)
266 << ",\n"
267 << parentName
268 << ",\n"
269 << offsetText;
270
271 if (!childName.empty()) {
272 out << ", &"
273 << childName;
274 }
275
276 out << ");\n\n";
277
278 out.unindent();
279 out.unindent();
280
281 handleError(out, mode);
282}
283
284status_t Type::emitTypeDeclarations(Formatter &) const {
285 return OK;
286}
287
288status_t Type::emitTypeDefinitions(
289 Formatter &, const std::string) const {
290 return OK;
291}
292
Andreas Huber85eabdb2016-08-25 11:24:49 -0700293status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700294 return OK;
295}
296
Andreas Huber881227d2016-08-02 14:20:21 -0700297bool Type::needsEmbeddedReadWrite() const {
298 return false;
299}
300
301bool Type::resultNeedsDeref() const {
302 return false;
303}
304
Steven Moreland979e0992016-09-07 09:18:08 -0700305std::string Type::getCppType(std::string *extra,
306 bool specifyNamespaces) const {
307 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700308}
309
Steven Moreland979e0992016-09-07 09:18:08 -0700310std::string Type::getCppResultType(std::string *extra,
311 bool specifyNamespaces) const {
312 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700313}
314
Steven Moreland979e0992016-09-07 09:18:08 -0700315std::string Type::getCppArgumentType(std::string *extra,
316 bool specifyNamespaces) const {
317 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700318}
319
Andreas Huber2831d512016-08-15 09:33:47 -0700320void Type::emitJavaReaderWriterWithSuffix(
321 Formatter &out,
322 const std::string &parcelObj,
323 const std::string &argName,
324 bool isReader,
325 const std::string &suffix,
326 const std::string &extra) const {
327 out << parcelObj
328 << "."
329 << (isReader ? "read" : "write")
330 << suffix
331 << "(";
332
333 if (isReader) {
334 out << extra;
335 } else {
336 out << (extra.empty() ? "" : (extra + ", "));
337 out << argName;
338 }
339
340 out << ");\n";
341}
342
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700343status_t Type::emitVtsTypeDeclarations(Formatter &) const {
344 return OK;
345}
346
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700347status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700348 return emitVtsTypeDeclarations(out);
349}
350
Andreas Huber70a59e12016-08-16 12:57:01 -0700351bool Type::isJavaCompatible() const {
352 return true;
353}
354
Andreas Huber85eabdb2016-08-25 11:24:49 -0700355void Type::getAlignmentAndSize(size_t *, size_t *) const {
356 CHECK(!"Should not be here");
357}
358
Andreas Huberc9410c72016-07-28 12:18:40 -0700359} // namespace android
360