blob: 548e425b50f23714c6ae8d7af1672fca76793830 [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"
4
5#include <android-base/logging.h>
6
Andreas Huberc9410c72016-07-28 12:18:40 -07007namespace android {
8
9Type::Type() {}
10Type::~Type() {}
11
Andreas Huber5345ec22016-07-29 13:33:27 -070012bool Type::isScope() const {
13 return false;
14}
15
Andreas Hubera2723d22016-07-29 15:36:07 -070016bool Type::isInterface() const {
17 return false;
18}
19
Andreas Huber737080b2016-08-02 15:38:04 -070020const ScalarType *Type::resolveToScalarType() const {
21 return NULL;
22}
23
Andreas Huber881227d2016-08-02 14:20:21 -070024std::string Type::getCppType(StorageMode, std::string *) const {
25 CHECK(!"Should not be here");
26 return std::string();
27}
28
29void Type::emitReaderWriter(
30 Formatter &,
31 const std::string &,
32 const std::string &,
33 bool,
34 bool,
35 ErrorMode) const {
36 CHECK(!"Should not be here");
37}
38
39void Type::emitReaderWriterEmbedded(
40 Formatter &,
41 const std::string &,
42 bool,
43 const std::string &,
44 bool,
45 bool,
46 ErrorMode,
47 const std::string &,
48 const std::string &) const {
49 CHECK(!"Should not be here");
50}
51
52void Type::handleError(Formatter &out, ErrorMode mode) const {
53 switch (mode) {
54 case ErrorMode_Ignore:
55 {
56 out << "/* _aidl_err ignored! */\n\n";
57 break;
58 }
59
60 case ErrorMode_Goto:
61 {
62 out << "if (_aidl_err != ::android::OK) { goto _aidl_error; }\n\n";
63 break;
64 }
65
66 case ErrorMode_Break:
67 {
68 out << "if (_aidl_err != ::android::OK) { break; }\n\n";
69 break;
70 }
Andreas Huber737080b2016-08-02 15:38:04 -070071
72 case ErrorMode_Return:
73 {
74 out << "if (_aidl_err != ::android::OK) { return _aidl_err; }\n\n";
75 break;
76 }
Andreas Huber881227d2016-08-02 14:20:21 -070077 }
78}
79
80void Type::handleError2(Formatter &out, ErrorMode mode) const {
81 switch (mode) {
82 case ErrorMode_Goto:
83 {
84 out << "goto _aidl_error;\n";
85 break;
86 }
Andreas Huber737080b2016-08-02 15:38:04 -070087
Andreas Huber881227d2016-08-02 14:20:21 -070088 case ErrorMode_Break:
89 {
90 out << "break;\n";
91 break;
92 }
Andreas Huber737080b2016-08-02 15:38:04 -070093
Andreas Huber881227d2016-08-02 14:20:21 -070094 case ErrorMode_Ignore:
95 {
96 out << "/* ignoring _aidl_error! */";
97 break;
98 }
Andreas Huber737080b2016-08-02 15:38:04 -070099
100 case ErrorMode_Return:
101 {
102 out << "return _aidl_err;\n";
103 break;
104 }
Andreas Huber881227d2016-08-02 14:20:21 -0700105 }
106}
107
108void Type::emitReaderWriterEmbeddedForTypeName(
109 Formatter &out,
110 const std::string &name,
111 bool nameIsPointer,
112 const std::string &parcelObj,
113 bool parcelObjIsPointer,
114 bool isReader,
115 ErrorMode mode,
116 const std::string &parentName,
117 const std::string &offsetText,
118 const std::string &typeName,
119 const std::string &childName) const {
120 const std::string parcelObjDeref =
121 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
122
123 const std::string parcelObjPointer =
124 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
125
126 const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
127 const std::string namePointer = nameIsPointer ? name : ("&" + name);
128
129 out << "_aidl_err = ";
130
131 if (isReader) {
132 out << "const_cast<"
133 << typeName
134 << " *>("
135 << namePointer
136 << ")->readEmbeddedFromParcel(\n";
137 } else {
138 out << nameDeref
139 << "writeEmbeddedToParcel(\n";
140 }
141
142 out.indent();
143 out.indent();
144
145 out << (isReader ? parcelObjDeref : parcelObjPointer)
146 << ",\n"
147 << parentName
148 << ",\n"
149 << offsetText;
150
151 if (!childName.empty()) {
152 out << ", &"
153 << childName;
154 }
155
156 out << ");\n\n";
157
158 out.unindent();
159 out.unindent();
160
161 handleError(out, mode);
162}
163
164status_t Type::emitTypeDeclarations(Formatter &) const {
165 return OK;
166}
167
168status_t Type::emitTypeDefinitions(
169 Formatter &, const std::string) const {
170 return OK;
171}
172
173bool Type::needsEmbeddedReadWrite() const {
174 return false;
175}
176
177bool Type::resultNeedsDeref() const {
178 return false;
179}
180
181std::string Type::getCppType(std::string *extra) const {
182 return getCppType(StorageMode_Stack, extra);
183}
184
185std::string Type::getCppResultType(std::string *extra) const {
186 return getCppType(StorageMode_Result, extra);
187}
188
189std::string Type::getCppArgumentType(std::string *extra) const {
190 return getCppType(StorageMode_Argument, extra);
191}
192
Andreas Huberc9410c72016-07-28 12:18:40 -0700193} // namespace android
194