blob: 9fea80fe19305b0bb802378ff6c8f8e1502be44b [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 Huber8d3ac0c2016-08-04 14:49:23 -070019#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070020
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070021#include <hidl-util/Formatter.h>
Andreas Huber881227d2016-08-02 14:20:21 -070022#include <android-base/logging.h>
23
Andreas Huberc9410c72016-07-28 12:18:40 -070024namespace android {
25
26Type::Type() {}
27Type::~Type() {}
28
Andreas Huber5345ec22016-07-29 13:33:27 -070029bool Type::isScope() const {
30 return false;
31}
32
Andreas Hubera2723d22016-07-29 15:36:07 -070033bool Type::isInterface() const {
34 return false;
35}
36
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070037bool Type::isEnum() const {
38 return false;
39}
40
41bool Type::isTypeDef() const {
42 return false;
43}
44
Andreas Huber295ad302016-08-16 11:35:00 -070045bool Type::isBinder() const {
46 return false;
47}
48
Andreas Huber39fa7182016-08-19 14:27:33 -070049bool Type::isNamedType() const {
50 return false;
51}
52
Andreas Huberf630bc82016-09-09 14:52:25 -070053bool Type::isCompoundType() const {
54 return false;
55}
56
Andreas Huber737080b2016-08-02 15:38:04 -070057const ScalarType *Type::resolveToScalarType() const {
58 return NULL;
59}
60
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070061bool Type::isValidEnumStorageType() const {
62 const ScalarType *scalarType = resolveToScalarType();
63
64 if (scalarType == NULL) {
65 return false;
66 }
67
68 return scalarType->isValidEnumStorageType();
69}
70
Steven Moreland979e0992016-09-07 09:18:08 -070071std::string Type::getCppType(StorageMode, std::string *, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070072 CHECK(!"Should not be here");
73 return std::string();
74}
75
Andreas Huber85eabdb2016-08-25 11:24:49 -070076std::string Type::getJavaWrapperType() const {
77 return getJavaType();
78}
79
Andreas Huber2831d512016-08-15 09:33:47 -070080std::string Type::getJavaSuffix() const {
81 CHECK(!"Should not be here");
82 return std::string();
83}
84
Andreas Huber881227d2016-08-02 14:20:21 -070085void Type::emitReaderWriter(
86 Formatter &,
87 const std::string &,
88 const std::string &,
89 bool,
90 bool,
91 ErrorMode) const {
92 CHECK(!"Should not be here");
93}
94
95void Type::emitReaderWriterEmbedded(
96 Formatter &,
97 const std::string &,
98 bool,
99 const std::string &,
100 bool,
101 bool,
102 ErrorMode,
103 const std::string &,
104 const std::string &) const {
105 CHECK(!"Should not be here");
106}
107
Andreas Huber2831d512016-08-15 09:33:47 -0700108void Type::emitJavaReaderWriter(
109 Formatter &out,
110 const std::string &parcelObj,
111 const std::string &argName,
112 bool isReader) const {
113 emitJavaReaderWriterWithSuffix(
114 out,
115 parcelObj,
116 argName,
117 isReader,
118 getJavaSuffix(),
119 "" /* extra */);
120}
121
Andreas Huber85eabdb2016-08-25 11:24:49 -0700122void Type::emitJavaFieldInitializer(
123 Formatter &out,
124 const std::string &fieldName) const {
125 out << getJavaType()
126 << " "
127 << fieldName
128 << ";\n";
129}
130
131void Type::emitJavaFieldReaderWriter(
132 Formatter &,
133 const std::string &,
134 const std::string &,
135 const std::string &,
136 bool) const {
137 CHECK(!"Should not be here");
138}
139
Andreas Huber881227d2016-08-02 14:20:21 -0700140void Type::handleError(Formatter &out, ErrorMode mode) const {
141 switch (mode) {
142 case ErrorMode_Ignore:
143 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700144 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700145 break;
146 }
147
148 case ErrorMode_Goto:
149 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700150 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700151 break;
152 }
153
154 case ErrorMode_Break:
155 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700156 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700157 break;
158 }
Andreas Huber737080b2016-08-02 15:38:04 -0700159
160 case ErrorMode_Return:
161 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700162 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700163 break;
164 }
Andreas Huber881227d2016-08-02 14:20:21 -0700165 }
166}
167
168void Type::handleError2(Formatter &out, ErrorMode mode) const {
169 switch (mode) {
170 case ErrorMode_Goto:
171 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700172 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700173 break;
174 }
Andreas Huber737080b2016-08-02 15:38:04 -0700175
Andreas Huber881227d2016-08-02 14:20:21 -0700176 case ErrorMode_Break:
177 {
178 out << "break;\n";
179 break;
180 }
Andreas Huber737080b2016-08-02 15:38:04 -0700181
Andreas Huber881227d2016-08-02 14:20:21 -0700182 case ErrorMode_Ignore:
183 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700184 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700185 break;
186 }
Andreas Huber737080b2016-08-02 15:38:04 -0700187
188 case ErrorMode_Return:
189 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700190 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700191 break;
192 }
Andreas Huber881227d2016-08-02 14:20:21 -0700193 }
194}
195
196void Type::emitReaderWriterEmbeddedForTypeName(
197 Formatter &out,
198 const std::string &name,
199 bool nameIsPointer,
200 const std::string &parcelObj,
201 bool parcelObjIsPointer,
202 bool isReader,
203 ErrorMode mode,
204 const std::string &parentName,
205 const std::string &offsetText,
206 const std::string &typeName,
207 const std::string &childName) const {
208 const std::string parcelObjDeref =
209 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
210
211 const std::string parcelObjPointer =
212 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
213
214 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
215 const std::string namePointer = nameIsPointer ? name : ("&" + name);
216
Iliyan Malchev549e2592016-08-10 08:59:12 -0700217 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700218
219 if (isReader) {
220 out << "const_cast<"
221 << typeName
222 << " *>("
223 << namePointer
224 << ")->readEmbeddedFromParcel(\n";
225 } else {
226 out << nameDeref
227 << "writeEmbeddedToParcel(\n";
228 }
229
230 out.indent();
231 out.indent();
232
233 out << (isReader ? parcelObjDeref : parcelObjPointer)
234 << ",\n"
235 << parentName
236 << ",\n"
237 << offsetText;
238
239 if (!childName.empty()) {
240 out << ", &"
241 << childName;
242 }
243
244 out << ");\n\n";
245
246 out.unindent();
247 out.unindent();
248
249 handleError(out, mode);
250}
251
252status_t Type::emitTypeDeclarations(Formatter &) const {
253 return OK;
254}
255
256status_t Type::emitTypeDefinitions(
257 Formatter &, const std::string) const {
258 return OK;
259}
260
Andreas Huber85eabdb2016-08-25 11:24:49 -0700261status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700262 return OK;
263}
264
Andreas Huber881227d2016-08-02 14:20:21 -0700265bool Type::needsEmbeddedReadWrite() const {
266 return false;
267}
268
269bool Type::resultNeedsDeref() const {
270 return false;
271}
272
Steven Moreland979e0992016-09-07 09:18:08 -0700273std::string Type::getCppType(std::string *extra,
274 bool specifyNamespaces) const {
275 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700276}
277
Steven Moreland979e0992016-09-07 09:18:08 -0700278std::string Type::getCppResultType(std::string *extra,
279 bool specifyNamespaces) const {
280 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700281}
282
Steven Moreland979e0992016-09-07 09:18:08 -0700283std::string Type::getCppArgumentType(std::string *extra,
284 bool specifyNamespaces) const {
285 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700286}
287
Andreas Huber2831d512016-08-15 09:33:47 -0700288void Type::emitJavaReaderWriterWithSuffix(
289 Formatter &out,
290 const std::string &parcelObj,
291 const std::string &argName,
292 bool isReader,
293 const std::string &suffix,
294 const std::string &extra) const {
295 out << parcelObj
296 << "."
297 << (isReader ? "read" : "write")
298 << suffix
299 << "(";
300
301 if (isReader) {
302 out << extra;
303 } else {
304 out << (extra.empty() ? "" : (extra + ", "));
305 out << argName;
306 }
307
308 out << ");\n";
309}
310
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700311status_t Type::emitVtsTypeDeclarations(Formatter &) const {
312 return OK;
313}
314
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700315status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700316 return emitVtsTypeDeclarations(out);
317}
318
Andreas Huber70a59e12016-08-16 12:57:01 -0700319bool Type::isJavaCompatible() const {
320 return true;
321}
322
Andreas Huber85eabdb2016-08-25 11:24:49 -0700323void Type::getAlignmentAndSize(size_t *, size_t *) const {
324 CHECK(!"Should not be here");
325}
326
Andreas Huberc9410c72016-07-28 12:18:40 -0700327} // namespace android
328