Type.cpp revision 2831d5145675ead9f2fb767bf5fe4ae56b88349f
1#include "Type.h"
2
3#include "Formatter.h"
4#include "ScalarType.h"
5
6#include <android-base/logging.h>
7
8namespace android {
9
10Type::Type() {}
11Type::~Type() {}
12
13bool Type::isScope() const {
14    return false;
15}
16
17bool Type::isInterface() const {
18    return false;
19}
20
21bool Type::isEnum() const {
22    return false;
23}
24
25bool Type::isTypeDef() const {
26    return false;
27}
28
29const ScalarType *Type::resolveToScalarType() const {
30    return NULL;
31}
32
33bool Type::isValidEnumStorageType() const {
34    const ScalarType *scalarType = resolveToScalarType();
35
36    if (scalarType == NULL) {
37        return false;
38    }
39
40    return scalarType->isValidEnumStorageType();
41}
42
43std::string Type::getCppType(StorageMode, std::string *) const {
44    CHECK(!"Should not be here");
45    return std::string();
46}
47
48std::string Type::getJavaSuffix() const {
49    CHECK(!"Should not be here");
50    return std::string();
51}
52
53void Type::emitReaderWriter(
54        Formatter &,
55        const std::string &,
56        const std::string &,
57        bool,
58        bool,
59        ErrorMode) const {
60    CHECK(!"Should not be here");
61}
62
63void Type::emitReaderWriterEmbedded(
64        Formatter &,
65        const std::string &,
66        bool,
67        const std::string &,
68        bool,
69        bool,
70        ErrorMode,
71        const std::string &,
72        const std::string &) const {
73    CHECK(!"Should not be here");
74}
75
76void Type::emitJavaReaderWriter(
77        Formatter &out,
78        const std::string &parcelObj,
79        const std::string &argName,
80        bool isReader) const {
81    emitJavaReaderWriterWithSuffix(
82            out,
83            parcelObj,
84            argName,
85            isReader,
86            getJavaSuffix(),
87            "" /* extra */);
88}
89
90void Type::handleError(Formatter &out, ErrorMode mode) const {
91    switch (mode) {
92        case ErrorMode_Ignore:
93        {
94            out << "/* _hidl_err ignored! */\n\n";
95            break;
96        }
97
98        case ErrorMode_Goto:
99        {
100            out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
101            break;
102        }
103
104        case ErrorMode_Break:
105        {
106            out << "if (_hidl_err != ::android::OK) { break; }\n\n";
107            break;
108        }
109
110        case ErrorMode_Return:
111        {
112            out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
113            break;
114        }
115    }
116}
117
118void Type::handleError2(Formatter &out, ErrorMode mode) const {
119    switch (mode) {
120        case ErrorMode_Goto:
121        {
122            out << "goto _hidl_error;\n";
123            break;
124        }
125
126        case ErrorMode_Break:
127        {
128            out << "break;\n";
129            break;
130        }
131
132        case ErrorMode_Ignore:
133        {
134            out << "/* ignoring _hidl_error! */";
135            break;
136        }
137
138        case ErrorMode_Return:
139        {
140            out << "return _hidl_err;\n";
141            break;
142        }
143    }
144}
145
146void Type::emitReaderWriterEmbeddedForTypeName(
147        Formatter &out,
148        const std::string &name,
149        bool nameIsPointer,
150        const std::string &parcelObj,
151        bool parcelObjIsPointer,
152        bool isReader,
153        ErrorMode mode,
154        const std::string &parentName,
155        const std::string &offsetText,
156        const std::string &typeName,
157        const std::string &childName) const {
158    const std::string parcelObjDeref =
159        parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
160
161    const std::string parcelObjPointer =
162        parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
163
164    const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
165    const std::string namePointer = nameIsPointer ? name : ("&" + name);
166
167    out << "_hidl_err = ";
168
169    if (isReader) {
170        out << "const_cast<"
171            << typeName
172            << " *>("
173            << namePointer
174            << ")->readEmbeddedFromParcel(\n";
175    } else {
176        out << nameDeref
177            << "writeEmbeddedToParcel(\n";
178    }
179
180    out.indent();
181    out.indent();
182
183    out << (isReader ? parcelObjDeref : parcelObjPointer)
184        << ",\n"
185        << parentName
186        << ",\n"
187        << offsetText;
188
189    if (!childName.empty()) {
190        out << ", &"
191            << childName;
192    }
193
194    out << ");\n\n";
195
196    out.unindent();
197    out.unindent();
198
199    handleError(out, mode);
200}
201
202status_t Type::emitTypeDeclarations(Formatter &) const {
203    return OK;
204}
205
206status_t Type::emitTypeDefinitions(
207        Formatter &, const std::string) const {
208    return OK;
209}
210
211status_t Type::emitJavaTypeDeclarations(Formatter &) const {
212    return OK;
213}
214
215bool Type::needsEmbeddedReadWrite() const {
216    return false;
217}
218
219bool Type::resultNeedsDeref() const {
220    return false;
221}
222
223std::string Type::getCppType(std::string *extra) const {
224    return getCppType(StorageMode_Stack, extra);
225}
226
227std::string Type::getCppResultType(std::string *extra) const {
228    return getCppType(StorageMode_Result, extra);
229}
230
231std::string Type::getCppArgumentType(std::string *extra) const {
232    return getCppType(StorageMode_Argument, extra);
233}
234
235void Type::emitJavaReaderWriterWithSuffix(
236        Formatter &out,
237        const std::string &parcelObj,
238        const std::string &argName,
239        bool isReader,
240        const std::string &suffix,
241        const std::string &extra) const {
242    out << parcelObj
243        << "."
244        << (isReader ? "read" : "write")
245        << suffix
246        << "(";
247
248    if (isReader) {
249        out << extra;
250    } else {
251        out << (extra.empty() ? "" : (extra + ", "));
252        out << argName;
253    }
254
255    out << ");\n";
256}
257
258}  // namespace android
259
260