blob: b09f8d33013cf667e4d9824d1d45726b637892c2 [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 Huber4c865b72016-09-14 15:26:27 -070076std::string Type::getJavaType(
77 std::string *extra, bool /* forInitializer */) const {
78 CHECK(!"Should not be here");
79 extra->clear();
80 return std::string();
81}
82
Andreas Huber85eabdb2016-08-25 11:24:49 -070083std::string Type::getJavaWrapperType() const {
Andreas Huber4c865b72016-09-14 15:26:27 -070084 std::string extra;
85 return getJavaType(&extra);
Andreas Huber85eabdb2016-08-25 11:24:49 -070086}
87
Andreas Huber2831d512016-08-15 09:33:47 -070088std::string Type::getJavaSuffix() const {
89 CHECK(!"Should not be here");
90 return std::string();
91}
92
Andreas Huber881227d2016-08-02 14:20:21 -070093void Type::emitReaderWriter(
94 Formatter &,
95 const std::string &,
96 const std::string &,
97 bool,
98 bool,
99 ErrorMode) const {
100 CHECK(!"Should not be here");
101}
102
103void Type::emitReaderWriterEmbedded(
104 Formatter &,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700105 size_t,
Andreas Huber881227d2016-08-02 14:20:21 -0700106 const std::string &,
107 bool,
108 const std::string &,
109 bool,
110 bool,
111 ErrorMode,
112 const std::string &,
113 const std::string &) const {
114 CHECK(!"Should not be here");
115}
116
Andreas Huber2831d512016-08-15 09:33:47 -0700117void Type::emitJavaReaderWriter(
118 Formatter &out,
119 const std::string &parcelObj,
120 const std::string &argName,
121 bool isReader) const {
122 emitJavaReaderWriterWithSuffix(
123 out,
124 parcelObj,
125 argName,
126 isReader,
127 getJavaSuffix(),
128 "" /* extra */);
129}
130
Andreas Huber85eabdb2016-08-25 11:24:49 -0700131void Type::emitJavaFieldInitializer(
132 Formatter &out,
133 const std::string &fieldName) const {
Andreas Huber4c865b72016-09-14 15:26:27 -0700134 std::string extra;
135 out << getJavaType(&extra)
Andreas Huber85eabdb2016-08-25 11:24:49 -0700136 << " "
137 << fieldName
138 << ";\n";
139}
140
141void Type::emitJavaFieldReaderWriter(
142 Formatter &,
Andreas Huber4c865b72016-09-14 15:26:27 -0700143 size_t,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700144 const std::string &,
145 const std::string &,
146 const std::string &,
147 bool) const {
148 CHECK(!"Should not be here");
149}
150
Andreas Huber881227d2016-08-02 14:20:21 -0700151void Type::handleError(Formatter &out, ErrorMode mode) const {
152 switch (mode) {
153 case ErrorMode_Ignore:
154 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700155 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700156 break;
157 }
158
159 case ErrorMode_Goto:
160 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700161 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700162 break;
163 }
164
165 case ErrorMode_Break:
166 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700167 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700168 break;
169 }
Andreas Huber737080b2016-08-02 15:38:04 -0700170
171 case ErrorMode_Return:
172 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700173 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700174 break;
175 }
Andreas Huber881227d2016-08-02 14:20:21 -0700176 }
177}
178
179void Type::handleError2(Formatter &out, ErrorMode mode) const {
180 switch (mode) {
181 case ErrorMode_Goto:
182 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700183 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700184 break;
185 }
Andreas Huber737080b2016-08-02 15:38:04 -0700186
Andreas Huber881227d2016-08-02 14:20:21 -0700187 case ErrorMode_Break:
188 {
189 out << "break;\n";
190 break;
191 }
Andreas Huber737080b2016-08-02 15:38:04 -0700192
Andreas Huber881227d2016-08-02 14:20:21 -0700193 case ErrorMode_Ignore:
194 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700195 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700196 break;
197 }
Andreas Huber737080b2016-08-02 15:38:04 -0700198
199 case ErrorMode_Return:
200 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700201 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700202 break;
203 }
Andreas Huber881227d2016-08-02 14:20:21 -0700204 }
205}
206
207void Type::emitReaderWriterEmbeddedForTypeName(
208 Formatter &out,
209 const std::string &name,
210 bool nameIsPointer,
211 const std::string &parcelObj,
212 bool parcelObjIsPointer,
213 bool isReader,
214 ErrorMode mode,
215 const std::string &parentName,
216 const std::string &offsetText,
217 const std::string &typeName,
218 const std::string &childName) const {
219 const std::string parcelObjDeref =
220 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
221
222 const std::string parcelObjPointer =
223 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
224
225 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
226 const std::string namePointer = nameIsPointer ? name : ("&" + name);
227
Iliyan Malchev549e2592016-08-10 08:59:12 -0700228 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700229
230 if (isReader) {
231 out << "const_cast<"
232 << typeName
233 << " *>("
234 << namePointer
235 << ")->readEmbeddedFromParcel(\n";
236 } else {
237 out << nameDeref
238 << "writeEmbeddedToParcel(\n";
239 }
240
241 out.indent();
242 out.indent();
243
244 out << (isReader ? parcelObjDeref : parcelObjPointer)
245 << ",\n"
246 << parentName
247 << ",\n"
248 << offsetText;
249
250 if (!childName.empty()) {
251 out << ", &"
252 << childName;
253 }
254
255 out << ");\n\n";
256
257 out.unindent();
258 out.unindent();
259
260 handleError(out, mode);
261}
262
263status_t Type::emitTypeDeclarations(Formatter &) const {
264 return OK;
265}
266
267status_t Type::emitTypeDefinitions(
268 Formatter &, const std::string) const {
269 return OK;
270}
271
Andreas Huber85eabdb2016-08-25 11:24:49 -0700272status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700273 return OK;
274}
275
Andreas Huber881227d2016-08-02 14:20:21 -0700276bool Type::needsEmbeddedReadWrite() const {
277 return false;
278}
279
280bool Type::resultNeedsDeref() const {
281 return false;
282}
283
Steven Moreland979e0992016-09-07 09:18:08 -0700284std::string Type::getCppType(std::string *extra,
285 bool specifyNamespaces) const {
286 return getCppType(StorageMode_Stack, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700287}
288
Steven Moreland979e0992016-09-07 09:18:08 -0700289std::string Type::getCppResultType(std::string *extra,
290 bool specifyNamespaces) const {
291 return getCppType(StorageMode_Result, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700292}
293
Steven Moreland979e0992016-09-07 09:18:08 -0700294std::string Type::getCppArgumentType(std::string *extra,
295 bool specifyNamespaces) const {
296 return getCppType(StorageMode_Argument, extra, specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700297}
298
Andreas Huber2831d512016-08-15 09:33:47 -0700299void Type::emitJavaReaderWriterWithSuffix(
300 Formatter &out,
301 const std::string &parcelObj,
302 const std::string &argName,
303 bool isReader,
304 const std::string &suffix,
305 const std::string &extra) const {
306 out << parcelObj
307 << "."
308 << (isReader ? "read" : "write")
309 << suffix
310 << "(";
311
312 if (isReader) {
313 out << extra;
314 } else {
315 out << (extra.empty() ? "" : (extra + ", "));
316 out << argName;
317 }
318
319 out << ");\n";
320}
321
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700322status_t Type::emitVtsTypeDeclarations(Formatter &) const {
323 return OK;
324}
325
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700326status_t Type::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700327 return emitVtsTypeDeclarations(out);
328}
329
Andreas Huber70a59e12016-08-16 12:57:01 -0700330bool Type::isJavaCompatible() const {
331 return true;
332}
333
Andreas Huber85eabdb2016-08-25 11:24:49 -0700334void Type::getAlignmentAndSize(size_t *, size_t *) const {
335 CHECK(!"Should not be here");
336}
337
Andreas Huberc9410c72016-07-28 12:18:40 -0700338} // namespace android
339