blob: bec465587d524043e83cde96b7e70459bf57053a [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Type.h"
2
Andreas Huber881227d2016-08-02 14:20:21 -07003#include "Formatter.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -07004#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -07005
6#include <android-base/logging.h>
7
Andreas Huberc9410c72016-07-28 12:18:40 -07008namespace android {
9
10Type::Type() {}
11Type::~Type() {}
12
Andreas Huber5345ec22016-07-29 13:33:27 -070013bool Type::isScope() const {
14 return false;
15}
16
Andreas Hubera2723d22016-07-29 15:36:07 -070017bool Type::isInterface() const {
18 return false;
19}
20
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070021bool Type::isEnum() const {
22 return false;
23}
24
25bool Type::isTypeDef() const {
26 return false;
27}
28
Andreas Huber737080b2016-08-02 15:38:04 -070029const ScalarType *Type::resolveToScalarType() const {
30 return NULL;
31}
32
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070033bool Type::isValidEnumStorageType() const {
34 const ScalarType *scalarType = resolveToScalarType();
35
36 if (scalarType == NULL) {
37 return false;
38 }
39
40 return scalarType->isValidEnumStorageType();
41}
42
Andreas Huber881227d2016-08-02 14:20:21 -070043std::string Type::getCppType(StorageMode, std::string *) const {
44 CHECK(!"Should not be here");
45 return std::string();
46}
47
48void Type::emitReaderWriter(
49 Formatter &,
50 const std::string &,
51 const std::string &,
52 bool,
53 bool,
54 ErrorMode) const {
55 CHECK(!"Should not be here");
56}
57
58void Type::emitReaderWriterEmbedded(
59 Formatter &,
60 const std::string &,
61 bool,
62 const std::string &,
63 bool,
64 bool,
65 ErrorMode,
66 const std::string &,
67 const std::string &) const {
68 CHECK(!"Should not be here");
69}
70
71void Type::handleError(Formatter &out, ErrorMode mode) const {
72 switch (mode) {
73 case ErrorMode_Ignore:
74 {
Iliyan Malchev549e2592016-08-10 08:59:12 -070075 out << "/* _hidl_err ignored! */\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070076 break;
77 }
78
79 case ErrorMode_Goto:
80 {
Iliyan Malchev549e2592016-08-10 08:59:12 -070081 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070082 break;
83 }
84
85 case ErrorMode_Break:
86 {
Iliyan Malchev549e2592016-08-10 08:59:12 -070087 out << "if (_hidl_err != ::android::OK) { break; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070088 break;
89 }
Andreas Huber737080b2016-08-02 15:38:04 -070090
91 case ErrorMode_Return:
92 {
Iliyan Malchev549e2592016-08-10 08:59:12 -070093 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
Andreas Huber737080b2016-08-02 15:38:04 -070094 break;
95 }
Andreas Huber881227d2016-08-02 14:20:21 -070096 }
97}
98
99void Type::handleError2(Formatter &out, ErrorMode mode) const {
100 switch (mode) {
101 case ErrorMode_Goto:
102 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700103 out << "goto _hidl_error;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700104 break;
105 }
Andreas Huber737080b2016-08-02 15:38:04 -0700106
Andreas Huber881227d2016-08-02 14:20:21 -0700107 case ErrorMode_Break:
108 {
109 out << "break;\n";
110 break;
111 }
Andreas Huber737080b2016-08-02 15:38:04 -0700112
Andreas Huber881227d2016-08-02 14:20:21 -0700113 case ErrorMode_Ignore:
114 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700115 out << "/* ignoring _hidl_error! */";
Andreas Huber881227d2016-08-02 14:20:21 -0700116 break;
117 }
Andreas Huber737080b2016-08-02 15:38:04 -0700118
119 case ErrorMode_Return:
120 {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700121 out << "return _hidl_err;\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700122 break;
123 }
Andreas Huber881227d2016-08-02 14:20:21 -0700124 }
125}
126
127void Type::emitReaderWriterEmbeddedForTypeName(
128 Formatter &out,
129 const std::string &name,
130 bool nameIsPointer,
131 const std::string &parcelObj,
132 bool parcelObjIsPointer,
133 bool isReader,
134 ErrorMode mode,
135 const std::string &parentName,
136 const std::string &offsetText,
137 const std::string &typeName,
138 const std::string &childName) const {
139 const std::string parcelObjDeref =
140 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
141
142 const std::string parcelObjPointer =
143 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
144
145 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
146 const std::string namePointer = nameIsPointer ? name : ("&" + name);
147
Iliyan Malchev549e2592016-08-10 08:59:12 -0700148 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700149
150 if (isReader) {
151 out << "const_cast<"
152 << typeName
153 << " *>("
154 << namePointer
155 << ")->readEmbeddedFromParcel(\n";
156 } else {
157 out << nameDeref
158 << "writeEmbeddedToParcel(\n";
159 }
160
161 out.indent();
162 out.indent();
163
164 out << (isReader ? parcelObjDeref : parcelObjPointer)
165 << ",\n"
166 << parentName
167 << ",\n"
168 << offsetText;
169
170 if (!childName.empty()) {
171 out << ", &"
172 << childName;
173 }
174
175 out << ");\n\n";
176
177 out.unindent();
178 out.unindent();
179
180 handleError(out, mode);
181}
182
183status_t Type::emitTypeDeclarations(Formatter &) const {
184 return OK;
185}
186
187status_t Type::emitTypeDefinitions(
188 Formatter &, const std::string) const {
189 return OK;
190}
191
192bool Type::needsEmbeddedReadWrite() const {
193 return false;
194}
195
196bool Type::resultNeedsDeref() const {
197 return false;
198}
199
200std::string Type::getCppType(std::string *extra) const {
201 return getCppType(StorageMode_Stack, extra);
202}
203
204std::string Type::getCppResultType(std::string *extra) const {
205 return getCppType(StorageMode_Result, extra);
206}
207
208std::string Type::getCppArgumentType(std::string *extra) const {
209 return getCppType(StorageMode_Argument, extra);
210}
211
Andreas Huberc9410c72016-07-28 12:18:40 -0700212} // namespace android
213