blob: 8abffd604d8f55610683041841c18ff0553045f7 [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 &,
Andreas Huberf9d49f12016-09-12 14:58:36 -070097 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -070098 const std::string &,
99 bool,
100 const std::string &,
101 bool,
102 bool,
103 ErrorMode,
104 const std::string &,
105 const std::string &) const {
106 CHECK(!"Should not be here");
107}
108
Andreas Huber2831d512016-08-15 09:33:47 -0700109void Type::emitJavaReaderWriter(
110 Formatter &out,
111 const std::string &parcelObj,
112 const std::string &argName,
113 bool isReader) const {
114 emitJavaReaderWriterWithSuffix(
115 out,
116 parcelObj,
117 argName,
118 isReader,
119 getJavaSuffix(),
120 "" /* extra */);
121}
122
Andreas Huber85eabdb2016-08-25 11:24:49 -0700123void Type::emitJavaFieldInitializer(
124 Formatter &out,
125 const std::string &fieldName) const {
126 out << getJavaType()
127 << " "
128 << fieldName
129 << ";\n";
130}
131
132void Type::emitJavaFieldReaderWriter(
133 Formatter &,
134 const std::string &,
135 const std::string &,
136 const std::string &,
137 bool) const {
138 CHECK(!"Should not be here");
139}
140
Andreas Huber881227d2016-08-02 14:20:21 -0700141void Type::handleError(Formatter &out, ErrorMode mode) const {
142 switch (mode) {
143 case ErrorMode_Ignore:
144 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700145 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700146 break;
147 }
148
149 case ErrorMode_Goto:
150 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700151 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700152 break;
153 }
154
155 case ErrorMode_Break:
156 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700157 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700158 break;
159 }
Andreas Huber737080b2016-08-02 15:38:04 -0700160
161 case ErrorMode_Return:
162 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700163 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700164 break;
165 }
Andreas Huber881227d2016-08-02 14:20:21 -0700166 }
167}
168
169void Type::handleError2(Formatter &out, ErrorMode mode) const {
170 switch (mode) {
171 case ErrorMode_Goto:
172 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700173 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700174 break;
175 }
Andreas Huber737080b2016-08-02 15:38:04 -0700176
Andreas Huber881227d2016-08-02 14:20:21 -0700177 case ErrorMode_Break:
178 {
179 out << "break;\n";
180 break;
181 }
Andreas Huber737080b2016-08-02 15:38:04 -0700182
Andreas Huber881227d2016-08-02 14:20:21 -0700183 case ErrorMode_Ignore:
184 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700185 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700186 break;
187 }
Andreas Huber737080b2016-08-02 15:38:04 -0700188
189 case ErrorMode_Return:
190 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700191 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700192 break;
193 }
Andreas Huber881227d2016-08-02 14:20:21 -0700194 }
195}
196
197void Type::emitReaderWriterEmbeddedForTypeName(
198 Formatter &out,
199 const std::string &name,
200 bool nameIsPointer,
201 const std::string &parcelObj,
202 bool parcelObjIsPointer,
203 bool isReader,
204 ErrorMode mode,
205 const std::string &parentName,
206 const std::string &offsetText,
207 const std::string &typeName,
208 const std::string &childName) const {
209 const std::string parcelObjDeref =
210 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
211
212 const std::string parcelObjPointer =
213 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
214
215 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
216 const std::string namePointer = nameIsPointer ? name : ("&" + name);
217
Iliyan Malchev549e2592016-08-10 08:59:12 -0700218 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700219
220 if (isReader) {
221 out << "const_cast<"
222 << typeName
223 << " *>("
224 << namePointer
225 << ")->readEmbeddedFromParcel(\n";
226 } else {
227 out << nameDeref
228 << "writeEmbeddedToParcel(\n";
229 }
230
231 out.indent();
232 out.indent();
233
234 out << (isReader ? parcelObjDeref : parcelObjPointer)
235 << ",\n"
236 << parentName
237 << ",\n"
238 << offsetText;
239
240 if (!childName.empty()) {
241 out << ", &"
242 << childName;
243 }
244
245 out << ");\n\n";
246
247 out.unindent();
248 out.unindent();
249
250 handleError(out, mode);
251}
252
253status_t Type::emitTypeDeclarations(Formatter &) const {
254 return OK;
255}
256
257status_t Type::emitTypeDefinitions(
258 Formatter &, const std::string) const {
259 return OK;
260}
261
Andreas Huber85eabdb2016-08-25 11:24:49 -0700262status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700263 return OK;
264}
265
Andreas Huber881227d2016-08-02 14:20:21 -0700266bool Type::needsEmbeddedReadWrite() const {
267 return false;
268}
269
270bool Type::resultNeedsDeref() const {
271 return false;
272}
273
Steven Moreland979e0992016-09-07 09:18:08 -0700274std::string Type::getCppType(std::string *extra,
275 bool specifyNamespaces) const {
276 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700277}
278
Steven Moreland979e0992016-09-07 09:18:08 -0700279std::string Type::getCppResultType(std::string *extra,
280 bool specifyNamespaces) const {
281 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700282}
283
Steven Moreland979e0992016-09-07 09:18:08 -0700284std::string Type::getCppArgumentType(std::string *extra,
285 bool specifyNamespaces) const {
286 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700287}
288
Andreas Huber2831d512016-08-15 09:33:47 -0700289void Type::emitJavaReaderWriterWithSuffix(
290 Formatter &out,
291 const std::string &parcelObj,
292 const std::string &argName,
293 bool isReader,
294 const std::string &suffix,
295 const std::string &extra) const {
296 out << parcelObj
297 << "."
298 << (isReader ? "read" : "write")
299 << suffix
300 << "(";
301
302 if (isReader) {
303 out << extra;
304 } else {
305 out << (extra.empty() ? "" : (extra + ", "));
306 out << argName;
307 }
308
309 out << ");\n";
310}
311
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700312status_t Type::emitVtsTypeDeclarations(Formatter &) const {
313 return OK;
314}
315
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700316status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700317 return emitVtsTypeDeclarations(out);
318}
319
Andreas Huber70a59e12016-08-16 12:57:01 -0700320bool Type::isJavaCompatible() const {
321 return true;
322}
323
Andreas Huber85eabdb2016-08-25 11:24:49 -0700324void Type::getAlignmentAndSize(size_t *, size_t *) const {
325 CHECK(!"Should not be here");
326}
327
Andreas Huberc9410c72016-07-28 12:18:40 -0700328} // namespace android
329