blob: c8b3649efe02001805c310df1f1c91c36ad18709 [file] [log] [blame]
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001/*
2 * Copyright (C) 2015, 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
17#include "generate_cpp.h"
Jiyong Park309668e2018-07-28 16:55:44 +090018#include "aidl.h"
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070019
Casey Dahlin082f1d12015-09-21 14:06:25 -070020#include <cctype>
Christopher Wileyad339272015-10-05 19:11:58 -070021#include <cstring>
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070022#include <memory>
Casey Dahlin082f1d12015-09-21 14:06:25 -070023#include <random>
Casey Dahlince776cf2015-10-15 18:45:54 -070024#include <set>
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070025#include <string>
26
Elliott Hughes0a620672015-12-04 13:53:18 -080027#include <android-base/stringprintf.h>
Christopher Wileye3550c62015-09-29 13:26:10 -070028
Casey Dahlina834dd42015-09-23 11:52:15 -070029#include "aidl_language.h"
Jiyong Parkce50e262018-10-29 09:54:20 +090030#include "aidl_to_cpp.h"
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070031#include "ast_cpp.h"
32#include "code_writer.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070033#include "logging.h"
Christopher Wiley054afbd2015-10-16 17:08:43 -070034#include "os.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070035
Jiyong Park75e1a742018-07-04 12:31:23 +090036using android::base::Join;
Christopher Wileye3550c62015-09-29 13:26:10 -070037using android::base::StringPrintf;
Jiyong Park75e1a742018-07-04 12:31:23 +090038using std::set;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070039using std::string;
Casey Dahlin082f1d12015-09-21 14:06:25 -070040using std::unique_ptr;
Casey Dahlina834dd42015-09-23 11:52:15 -070041using std::vector;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070042
Christopher Wileyeb1acc12015-09-16 11:25:13 -070043namespace android {
44namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070045namespace cpp {
Casey Dahlina834dd42015-09-23 11:52:15 -070046namespace internals {
Christopher Wiley0c732db2015-09-29 14:36:44 -070047namespace {
Christopher Wiley36570f42015-10-08 17:20:11 -070048
Casey Dahlinb8d9e882015-11-24 10:57:23 -080049const char kAndroidStatusVarName[] = "_aidl_ret_status";
50const char kCodeVarName[] = "_aidl_code";
51const char kFlagsVarName[] = "_aidl_flags";
52const char kDataVarName[] = "_aidl_data";
53const char kErrorLabel[] = "_aidl_error";
54const char kImplVarName[] = "_aidl_impl";
55const char kReplyVarName[] = "_aidl_reply";
Christopher Wileyad339272015-10-05 19:11:58 -070056const char kReturnVarName[] = "_aidl_return";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080057const char kStatusVarName[] = "_aidl_status";
Martijn Coenenf1b50782018-02-21 21:06:23 +010058const char kTraceVarName[] = "_aidl_trace";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080059const char kAndroidParcelLiteral[] = "::android::Parcel";
60const char kAndroidStatusLiteral[] = "::android::status_t";
61const char kAndroidStatusOk[] = "::android::OK";
62const char kBinderStatusLiteral[] = "::android::binder::Status";
Christopher Wiley0c732db2015-09-29 14:36:44 -070063const char kIBinderHeader[] = "binder/IBinder.h";
64const char kIInterfaceHeader[] = "binder/IInterface.h";
Christopher Wileyad339272015-10-05 19:11:58 -070065const char kParcelHeader[] = "binder/Parcel.h";
Steven Morelanda57d0a62019-07-30 09:41:14 -070066const char kStabilityHeader[] = "binder/Stability.h";
Christopher Wiley433c8bb2015-11-12 14:20:46 -080067const char kStatusHeader[] = "binder/Status.h";
Christopher Wiley69b44cf2016-05-03 13:43:33 -070068const char kString16Header[] = "utils/String16.h";
Martijn Coenenf1b50782018-02-21 21:06:23 +010069const char kTraceHeader[] = "utils/Trace.h";
Casey Dahlin389781f2015-10-22 13:13:21 -070070const char kStrongPointerHeader[] = "utils/StrongPointer.h";
Jiyong Park75e1a742018-07-04 12:31:23 +090071const char kAndroidBaseMacrosHeader[] = "android-base/macros.h";
Casey Dahlin082f1d12015-09-21 14:06:25 -070072
Christopher Wiley0eb903e2015-10-20 17:07:08 -070073unique_ptr<AstNode> BreakOnStatusNotOk() {
74 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080075 new LiteralExpression(kAndroidStatusVarName), "!=",
76 new LiteralExpression(kAndroidStatusOk)));
Christopher Wiley0eb903e2015-10-20 17:07:08 -070077 ret->OnTrue()->AddLiteral("break");
78 return unique_ptr<AstNode>(ret);
79}
80
Christopher Wiley433c8bb2015-11-12 14:20:46 -080081unique_ptr<AstNode> GotoErrorOnBadStatus() {
82 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080083 new LiteralExpression(kAndroidStatusVarName), "!=",
84 new LiteralExpression(kAndroidStatusOk)));
85 ret->OnTrue()->AddLiteral(StringPrintf("goto %s", kErrorLabel));
Christopher Wiley433c8bb2015-11-12 14:20:46 -080086 return unique_ptr<AstNode>(ret);
87}
88
Steven Moreland5557f1c2018-07-02 13:50:23 -070089unique_ptr<AstNode> ReturnOnStatusNotOk() {
90 IfStatement* ret = new IfStatement(new Comparison(new LiteralExpression(kAndroidStatusVarName),
91 "!=", new LiteralExpression(kAndroidStatusOk)));
92 ret->OnTrue()->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
93 return unique_ptr<AstNode>(ret);
94}
95
Jeongik Cha1a7ab642019-07-29 17:31:02 +090096ArgList BuildArgList(const AidlTypenames& typenames, const AidlMethod& method, bool for_declaration,
Jiyong Park75e1a742018-07-04 12:31:23 +090097 bool type_name_only = false) {
Christopher Wileyad339272015-10-05 19:11:58 -070098 // Build up the argument list for the server method call.
99 vector<string> method_arguments;
100 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
101 string literal;
102 if (for_declaration) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900103 // Method declarations need typenames, pointers to out params, and variable
Christopher Wileyad339272015-10-05 19:11:58 -0700104 // names that match the .aidl specification.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900105 literal = CppNameOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700106
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700107 if (a->IsOut()) {
108 literal = literal + "*";
109 } else {
Steven Morelandddec09d2019-10-07 16:10:41 -0700110 const auto definedType = typenames.TryGetDefinedType(a->GetType().GetName());
111
112 const bool isEnum = definedType && definedType->AsEnumDeclaration() != nullptr;
113 const bool isPrimitive = AidlTypenames::IsPrimitiveTypename(a->GetType().GetName());
114
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700115 // We pass in parameters that are not primitives by const reference.
116 // Arrays of primitives are not primitives.
Steven Morelandddec09d2019-10-07 16:10:41 -0700117 if (!(isPrimitive || isEnum) || a->GetType().IsArray()) {
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700118 literal = "const " + literal + "&";
119 }
120 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900121 if (!type_name_only) {
122 literal += " " + a->GetName();
123 }
Christopher Wileyad339272015-10-05 19:11:58 -0700124 } else {
125 if (a->IsOut()) { literal = "&"; }
126 literal += BuildVarName(*a);
127 }
128 method_arguments.push_back(literal);
129 }
130
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900131 if (method.GetType().GetName() != "void") {
Christopher Wileyade4b452015-10-10 11:06:03 -0700132 string literal;
Christopher Wileyad339272015-10-05 19:11:58 -0700133 if (for_declaration) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900134 literal = StringPrintf("%s* %s", CppNameOf(method.GetType(), typenames).c_str(),
Jiyong Park75e1a742018-07-04 12:31:23 +0900135 type_name_only ? "" : kReturnVarName);
Christopher Wileyad339272015-10-05 19:11:58 -0700136 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700137 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700138 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700139 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700140 }
141
Christopher Wileyade4b452015-10-10 11:06:03 -0700142 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700143}
144
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900145unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, const AidlTypenames& typenames,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700146 bool for_interface) {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700147 uint32_t modifiers = 0;
148 if (for_interface) {
149 modifiers |= MethodDecl::IS_VIRTUAL;
150 modifiers |= MethodDecl::IS_PURE_VIRTUAL;
151 } else {
152 modifiers |= MethodDecl::IS_OVERRIDE;
153 }
154
155 return unique_ptr<Declaration>{
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900156 new MethodDecl{kBinderStatusLiteral, method.GetName(),
157 BuildArgList(typenames, method, true /* for method decl */), modifiers}};
Christopher Wiley0c732db2015-09-29 14:36:44 -0700158}
159
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900160unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const AidlTypenames&,
Jiyong Park309668e2018-07-28 16:55:44 +0900161 const Options& options, bool for_interface) {
162 CHECK(!method.IsUserDefined());
163 if (method.GetName() == kGetInterfaceVersion && options.Version()) {
164 std::ostringstream code;
165 if (for_interface) {
166 code << "virtual ";
167 }
168 code << "int32_t " << kGetInterfaceVersion << "()";
169 if (for_interface) {
170 code << " = 0;\n";
171 } else {
172 code << " override;\n";
173 }
174 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
175 }
176 return nullptr;
177}
178
Steven Morelandf3da0892018-10-05 14:52:01 -0700179std::vector<unique_ptr<Declaration>> NestInNamespaces(vector<unique_ptr<Declaration>> decls,
180 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700181 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700182 for (; it != package.crend(); ++it) {
Steven Morelandf3da0892018-10-05 14:52:01 -0700183 vector<unique_ptr<Declaration>> inner;
184 inner.emplace_back(unique_ptr<Declaration>{new CppNamespace{*it, std::move(decls)}});
185
186 decls = std::move(inner);
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700187 }
Steven Morelandf3da0892018-10-05 14:52:01 -0700188 return decls;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700189}
190
Steven Morelandf3da0892018-10-05 14:52:01 -0700191std::vector<unique_ptr<Declaration>> NestInNamespaces(unique_ptr<Declaration> decl,
192 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700193 vector<unique_ptr<Declaration>> decls;
194 decls.push_back(std::move(decl));
195 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700196}
197
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900198bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b,
199 const AidlTypenames& typenamespaces) {
200 string type = CppNameOf(a.GetType(), typenamespaces);
Casey Dahlinb0966612015-10-19 16:35:26 -0700201
202 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700203 return true;
204}
205
Steven Moreland5557f1c2018-07-02 13:50:23 -0700206string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
207 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700208 for (size_t i = 1; i < class_name.size(); ++i) {
209 if (isupper(class_name[i])) {
210 class_name.insert(i, "_");
211 ++i;
212 }
213 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700214 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700215 class_name.c_str());
216 for (char& c : ret) {
217 if (c == '.') {
218 c = '_';
219 }
220 c = toupper(c);
221 }
222 return ret;
223}
Christopher Wiley36570f42015-10-08 17:20:11 -0700224
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900225unique_ptr<Declaration> DefineClientTransaction(const AidlTypenames& typenames,
Christopher Wiley36570f42015-10-08 17:20:11 -0700226 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900227 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700228 const string i_name = ClassName(interface, ClassNames::INTERFACE);
229 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900230 unique_ptr<MethodImpl> ret{
231 new MethodImpl{kBinderStatusLiteral, bp_name, method.GetName(),
232 ArgList{BuildArgList(typenames, method, true /* for method decl */)}}};
Christopher Wiley36570f42015-10-08 17:20:11 -0700233 StatementBlock* b = ret->GetStatementBlock();
234
235 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800236 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700237 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800238 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700239
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800240 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800241 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
242 kAndroidStatusVarName,
243 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800244 // We unconditionally return a Status object.
245 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700246
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900247 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100248 b->AddLiteral(
249 StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
250 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
251 }
252
Jiyong Parkce50e262018-10-29 09:54:20 +0900253 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900254 b->AddLiteral(GenLogBeforeExecute(bp_name, method, false /* isServer */, false /* isNdk */),
255 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900256 }
257
Christopher Wiley8993cb52015-10-21 09:53:24 -0700258 // Add the name of the interface we're hoping to call.
259 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800260 kAndroidStatusVarName,
261 new MethodCall(StringPrintf("%s.writeInterfaceToken",
262 kDataVarName),
263 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800264 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700265
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700266 for (const auto& a: method.GetArguments()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700267 const string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700268
269 if (a->IsIn()) {
270 // Serialization looks roughly like:
271 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
272 // if (_aidl_ret_status != ::android::OK) { goto error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900273 const string& method = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Normanee8674f2019-09-20 16:07:00 -0700274 b->AddStatement(
275 new Assignment(kAndroidStatusVarName,
276 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
277 ParcelWriteCastOf(a->GetType(), typenames, var_name))));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700278 b->AddStatement(GotoErrorOnBadStatus());
279 } else if (a->IsOut() && a->GetType().IsArray()) {
280 // Special case, the length of the out array is written into the parcel.
281 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
282 // if (_aidl_ret_status != ::android::OK) { goto error; }
283 b->AddStatement(new Assignment(
284 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700285 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName), var_name)));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700286 b->AddStatement(GotoErrorOnBadStatus());
287 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700288 }
289
290 // Invoke the transaction on the remote binder and confirm status.
Jeongik Chab5d962f2018-11-17 09:12:28 +0900291 string transaction_code = GetTransactionIdFor(method);
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700292
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800293 vector<string> args = {transaction_code, kDataVarName,
294 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700295
Steven Morelandacd53472018-12-14 10:17:26 -0800296 if (method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800297 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700298 }
299
Christopher Wiley36570f42015-10-08 17:20:11 -0700300 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800301 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700302 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700303 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900304
305 // If the method is not implemented in the remote side, try to call the
306 // default implementation, if provided.
307 vector<string> arg_names;
308 for (const auto& a : method.GetArguments()) {
309 arg_names.emplace_back(a->GetName());
310 }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900311 if (method.GetType().GetName() != "void") {
Jiyong Park75e1a742018-07-04 12:31:23 +0900312 arg_names.emplace_back(kReturnVarName);
313 }
314 b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && "
315 "%s::getDefaultImpl())) {\n"
316 " return %s::getDefaultImpl()->%s(%s);\n"
317 "}\n",
318 i_name.c_str(), i_name.c_str(), method.GetName().c_str(),
319 Join(arg_names, ", ").c_str()),
320 false /* no semicolon */);
321
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800322 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700323
Steven Morelandacd53472018-12-14 10:17:26 -0800324 if (!method.IsOneway()) {
Christopher Wiley1227d612015-10-26 16:59:20 -0700325 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800326 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
327 // if (_aidl_ret_status != ::android::OK) { goto error; }
328 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800329 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800330 kAndroidStatusVarName,
331 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800332 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700333 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800334 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700335 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800336 exception_check->OnTrue()->AddLiteral(
337 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700338 }
339
340 // Type checking should guarantee that nothing below emits code until "return
341 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700342
Christopher Wiley36570f42015-10-08 17:20:11 -0700343 // If the method is expected to return something, read it first by convention.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900344 if (method.GetType().GetName() != "void") {
345 const string& method_call = ParcelReadMethodOf(method.GetType(), typenames);
Christopher Wiley36570f42015-10-08 17:20:11 -0700346 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800347 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700348 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method_call.c_str()),
349 ParcelReadCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800350 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700351 }
352
353 for (const AidlArgument* a : method.GetOutArguments()) {
354 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800355 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
356 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900357 string method = ParcelReadMethodOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700358
Daniel Norman85aed542019-08-21 12:01:14 -0700359 b->AddStatement(
360 new Assignment(kAndroidStatusVarName,
361 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method.c_str()),
362 ParcelReadCastOf(a->GetType(), typenames, a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800363 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700364 }
365
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800366 // If we've gotten to here, one of two things is true:
367 // 1) We've read some bad status_t
368 // 2) We've only read status_t == OK and there was no exception in the
369 // response.
370 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800371 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800372 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800373 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
374 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100375
Jiyong Parkce50e262018-10-29 09:54:20 +0900376 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900377 b->AddLiteral(GenLogAfterExecute(bp_name, interface, method, kStatusVarName, kReturnVarName,
378 false /* isServer */, false /* isNdk */),
Jeongik Cha46375122018-12-21 18:41:21 +0900379 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900380 }
381
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800382 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700383
384 return unique_ptr<Declaration>(ret.release());
385}
386
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900387unique_ptr<Declaration> DefineClientMetaTransaction(const AidlTypenames& /* typenames */,
Jiyong Park309668e2018-07-28 16:55:44 +0900388 const AidlInterface& interface,
389 const AidlMethod& method,
390 const Options& options) {
391 CHECK(!method.IsUserDefined());
392 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
393 const string iface = ClassName(interface, ClassNames::INTERFACE);
394 const string proxy = ClassName(interface, ClassNames::CLIENT);
395 // Note: race condition can happen here, but no locking is required
396 // because 1) writing an interger is atomic and 2) this transaction
397 // will always return the same value, i.e., competing threads will
398 // give write the same value to cached_version_.
399 std::ostringstream code;
400 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900401 << " if (cached_version_ == -1) {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900402 << " ::android::Parcel data;\n"
403 << " ::android::Parcel reply;\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900404 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
Jeongik Chab5d962f2018-11-17 09:12:28 +0900405 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
406 << ", data, &reply);\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900407 << " if (err == ::android::OK) {\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900408 << " ::android::binder::Status _aidl_status;\n"
409 << " err = _aidl_status.readFromParcel(reply);\n"
410 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
411 << " cached_version_ = reply.readInt32();\n"
412 << " }\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900413 << " }\n"
414 << " }\n"
415 << " return cached_version_;\n"
416 << "}\n";
417 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
418 }
419 return nullptr;
420}
421
Christopher Wiley36570f42015-10-08 17:20:11 -0700422} // namespace
423
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900424unique_ptr<Document> BuildClientSource(const AidlTypenames& typenames,
425 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700426 vector<string> include_list = {
427 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900428 kParcelHeader,
429 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700430 };
Jiyong Parkce50e262018-10-29 09:54:20 +0900431 if (options.GenLog()) {
432 include_list.emplace_back("chrono");
433 include_list.emplace_back("functional");
434 include_list.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900435 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700436 vector<unique_ptr<Declaration>> file_decls;
437
438 // The constructor just passes the IBinder instance up to the super
439 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700440 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700441 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700442 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800443 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
444 kImplVarName)},
445 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700446
Jiyong Parkce50e262018-10-29 09:54:20 +0900447 if (options.GenLog()) {
448 string code;
449 ClassName(interface, ClassNames::CLIENT);
450 CodeWriterPtr writer = CodeWriter::ForString(&code);
451 (*writer) << "std::function<void(const Json::Value&)> "
452 << ClassName(interface, ClassNames::CLIENT) << "::logFunc;\n";
453 writer->Close();
454 file_decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
455 }
456
Christopher Wiley36570f42015-10-08 17:20:11 -0700457 // Clients define a method per transaction.
458 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900459 unique_ptr<Declaration> m;
460 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900461 m = DefineClientTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900462 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900463 m = DefineClientMetaTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900464 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700465 if (!m) { return nullptr; }
466 file_decls.push_back(std::move(m));
467 }
468 return unique_ptr<Document>{new CppSource{
469 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700470 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700471}
472
Christopher Wileyad339272015-10-05 19:11:58 -0700473namespace {
474
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900475bool HandleServerTransaction(const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900476 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700477 // Declare all the parameters now. In the common case, we expect no errors
478 // in serialization.
479 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900480 if (!DeclareLocalVariable(*a, b, typenames)) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700481 return false;
482 }
Christopher Wileyad339272015-10-05 19:11:58 -0700483 }
484
485 // Declare a variable to hold the return value.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900486 if (method.GetType().GetName() != "void") {
487 string type = CppNameOf(method.GetType(), typenames);
488 b->AddLiteral(StringPrintf("%s %s", type.c_str(), kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700489 }
490
Christopher Wiley8993cb52015-10-21 09:53:24 -0700491 // Check that the client is calling the correct interface.
492 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800493 new MethodCall(StringPrintf("%s.checkInterface",
494 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700495 true /* invert the check */);
496 b->AddStatement(interface_check);
497 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800498 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700499 interface_check->OnTrue()->AddLiteral("break");
500
Christopher Wileyad339272015-10-05 19:11:58 -0700501 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700502 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700503 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800504 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
505 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Normanee8674f2019-09-20 16:07:00 -0700506 const string& var_name = "&" + BuildVarName(*a);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700507 if (a->IsIn()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700508 const string& readMethod = ParcelReadMethodOf(a->GetType(), typenames);
509 b->AddStatement(
510 new Assignment{kAndroidStatusVarName,
Daniel Normanee8674f2019-09-20 16:07:00 -0700511 new MethodCall{string(kDataVarName) + "." + readMethod,
512 ParcelReadCastOf(a->GetType(), typenames, var_name)}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700513 b->AddStatement(BreakOnStatusNotOk());
514 } else if (a->IsOut() && a->GetType().IsArray()) {
515 // Special case, the length of the out array is written into the parcel.
516 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
517 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Norman85aed542019-08-21 12:01:14 -0700518 b->AddStatement(
519 new Assignment{kAndroidStatusVarName,
520 new MethodCall{string(kDataVarName) + ".resizeOutVector", var_name}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700521 b->AddStatement(BreakOnStatusNotOk());
522 }
Christopher Wileyad339272015-10-05 19:11:58 -0700523 }
524
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900525 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100526 b->AddStatement(new Statement(new MethodCall("atrace_begin",
527 ArgList{{"ATRACE_TAG_AIDL",
528 StringPrintf("\"%s::%s::cppServer\"",
529 interface.GetName().c_str(),
530 method.GetName().c_str())}})));
531 }
Jeongik Cha46375122018-12-21 18:41:21 +0900532 const string bn_name = ClassName(interface, ClassNames::SERVER);
533 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900534 b->AddLiteral(GenLogBeforeExecute(bn_name, method, true /* isServer */, false /* isNdk */),
535 false);
Jeongik Cha46375122018-12-21 18:41:21 +0900536 }
Christopher Wileyad339272015-10-05 19:11:58 -0700537 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800538 vector<unique_ptr<AstNode>> status_args;
539 status_args.emplace_back(new MethodCall(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900540 method.GetName(), BuildArgList(typenames, method, false /* not for method decl */)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800541 b->AddStatement(new Statement(new MethodCall(
542 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
543 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700544
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900545 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100546 b->AddStatement(new Statement(new MethodCall("atrace_end",
547 "ATRACE_TAG_AIDL")));
548 }
549
Jeongik Chab34800b2019-03-08 14:36:58 +0900550 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900551 b->AddLiteral(GenLogAfterExecute(bn_name, interface, method, kStatusVarName, kReturnVarName,
552 true /* isServer */, false /* isNdk */),
553 false);
Jeongik Chab34800b2019-03-08 14:36:58 +0900554 }
555
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800556 // Write exceptions during transaction handling to parcel.
557 if (!method.IsOneway()) {
558 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800559 kAndroidStatusVarName,
560 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800561 b->AddStatement(BreakOnStatusNotOk());
562 IfStatement* exception_check = new IfStatement(
563 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
564 b->AddStatement(exception_check);
565 exception_check->OnTrue()->AddLiteral("break");
566 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700567
Christopher Wiley36570f42015-10-08 17:20:11 -0700568 // If we have a return value, write it first.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900569 if (method.GetType().GetName() != "void") {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700570 string writeMethod =
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900571 string(kReplyVarName) + "->" + ParcelWriteMethodOf(method.GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700572 b->AddStatement(new Assignment(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900573 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700574 new MethodCall(writeMethod,
575 ParcelWriteCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700576 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700577 }
Christopher Wileyad339272015-10-05 19:11:58 -0700578 // Write each out parameter to the reply parcel.
579 for (const AidlArgument* a : method.GetOutArguments()) {
580 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800581 // _aidl_ret_status = data.WriteInt32(out_param_name);
582 // if (_aidl_ret_status != ::android::OK) { break; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900583 const string& writeMethod = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700584 b->AddStatement(new Assignment(
585 kAndroidStatusVarName,
586 new MethodCall(string(kReplyVarName) + "->" + writeMethod,
587 ParcelWriteCastOf(a->GetType(), typenames, BuildVarName(*a)))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700588 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700589 }
590
591 return true;
592}
593
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900594bool HandleServerMetaTransaction(const AidlTypenames&, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900595 const AidlMethod& method, const Options& options,
596 StatementBlock* b) {
597 CHECK(!method.IsUserDefined());
598
599 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
600 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900601 code << "_aidl_data.checkInterface(this);\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900602 << "_aidl_reply->writeNoException();\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900603 << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
Jiyong Park309668e2018-07-28 16:55:44 +0900604 << "::VERSION)";
605 b->AddLiteral(code.str());
606 return true;
607 }
608 return false;
609}
610
Christopher Wileyad339272015-10-05 19:11:58 -0700611} // namespace
612
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900613unique_ptr<Document> BuildServerSource(const AidlTypenames& typenames,
614 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700615 const string bn_name = ClassName(interface, ClassNames::SERVER);
616 vector<string> include_list{
617 HeaderFile(interface, ClassNames::SERVER, false),
Steven Morelanda57d0a62019-07-30 09:41:14 -0700618 kParcelHeader,
619 kStabilityHeader,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700620 };
Jeongik Cha46375122018-12-21 18:41:21 +0900621 if (options.GenLog()) {
622 include_list.emplace_back("chrono");
623 include_list.emplace_back("functional");
624 include_list.emplace_back("json/value.h");
Jeongik Cha46375122018-12-21 18:41:21 +0900625 }
Steven Moreland800508d2019-07-30 10:45:31 -0700626
627 unique_ptr<ConstructorImpl> constructor{
628 new ConstructorImpl{ClassName(interface, ClassNames::SERVER), ArgList{}, {}}};
629
Steven Morelanda57d0a62019-07-30 09:41:14 -0700630 if (interface.IsVintfStability()) {
631 constructor->GetStatementBlock()->AddLiteral("::android::internal::Stability::markVintf(this)");
632 } else {
633 constructor->GetStatementBlock()->AddLiteral(
634 "::android::internal::Stability::markCompilationUnit(this)");
635 }
636
Christopher Wileyad339272015-10-05 19:11:58 -0700637 unique_ptr<MethodImpl> on_transact{new MethodImpl{
638 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800639 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
640 StringPrintf("const %s& %s", kAndroidParcelLiteral,
641 kDataVarName),
642 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
643 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700644 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700645
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800646 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700647 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800648 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
649 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700650
Christopher Wileyad339272015-10-05 19:11:58 -0700651 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800652 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700653 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700654
655 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700656 for (const auto& method : interface.GetMethods()) {
Jeongik Chab5d962f2018-11-17 09:12:28 +0900657 StatementBlock* b = s->AddCase(GetTransactionIdFor(*method));
Christopher Wileyad339272015-10-05 19:11:58 -0700658 if (!b) { return nullptr; }
659
Jiyong Park309668e2018-07-28 16:55:44 +0900660 bool success = false;
661 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900662 success = HandleServerTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900663 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900664 success = HandleServerMetaTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900665 }
666 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900667 return nullptr;
668 }
Christopher Wileyad339272015-10-05 19:11:58 -0700669 }
670
671 // The switch statement has a default case which defers to the super class.
672 // The superclass handles a few pre-defined transactions.
673 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800674 b->AddLiteral(StringPrintf(
675 "%s = ::android::BBinder::onTransact(%s, %s, "
676 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
677 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700678
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800679 // If we saw a null reference, we can map that to an appropriate exception.
680 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800681 new LiteralExpression(string(kAndroidStatusVarName) +
682 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800683 on_transact->GetStatementBlock()->AddStatement(null_check);
684 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800685 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800686 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800687 ".writeToParcel(%s)",
688 kBinderStatusLiteral, kBinderStatusLiteral,
689 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800690
Christopher Wileyad339272015-10-05 19:11:58 -0700691 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800692 on_transact->GetStatementBlock()->AddLiteral(
693 StringPrintf("return %s", kAndroidStatusVarName));
Jeongik Cha46375122018-12-21 18:41:21 +0900694 vector<unique_ptr<Declaration>> decls;
Steven Moreland800508d2019-07-30 10:45:31 -0700695 decls.push_back(std::move(constructor));
Jeongik Cha46375122018-12-21 18:41:21 +0900696 decls.push_back(std::move(on_transact));
Christopher Wileyad339272015-10-05 19:11:58 -0700697
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900698 if (options.Version() > 0) {
699 std::ostringstream code;
700 code << "int32_t " << bn_name << "::" << kGetInterfaceVersion << "() {\n"
701 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::VERSION;\n"
702 << "}\n";
703 decls.emplace_back(new LiteralDecl(code.str()));
704 }
705
Jeongik Cha46375122018-12-21 18:41:21 +0900706 if (options.GenLog()) {
707 string code;
708 ClassName(interface, ClassNames::SERVER);
709 CodeWriterPtr writer = CodeWriter::ForString(&code);
710 (*writer) << "std::function<void(const Json::Value&)> "
711 << ClassName(interface, ClassNames::SERVER) << "::logFunc;\n";
712 writer->Close();
713 decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
714 }
715 return unique_ptr<Document>{
716 new CppSource{include_list, NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700717}
718
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900719unique_ptr<Document> BuildInterfaceSource(const AidlTypenames& typenames,
Jiyong Park309668e2018-07-28 16:55:44 +0900720 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700721 vector<string> include_list{
Jiyong Park5b7e5322019-04-03 20:05:01 +0900722 HeaderFile(interface, ClassNames::RAW, false),
Christopher Wiley054afbd2015-10-16 17:08:43 -0700723 HeaderFile(interface, ClassNames::CLIENT, false),
724 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700725
Christopher Wiley054afbd2015-10-16 17:08:43 -0700726 string fq_name = ClassName(interface, ClassNames::INTERFACE);
727 if (!interface.GetPackage().empty()) {
728 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700729 }
730
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700731 vector<unique_ptr<Declaration>> decls;
732
Christopher Wiley11a9d792016-02-24 17:20:33 -0800733 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Ivan Lozanob3f4d622019-11-25 09:30:02 -0800734 "DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE",
735 ArgList{vector<string>{ClassName(interface, ClassNames::BASE), '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700736 decls.push_back(std::move(meta_if));
737
Steven Moreland693640b2018-07-19 13:46:27 -0700738 for (const auto& constant : interface.GetConstantDeclarations()) {
739 const AidlConstantValue& value = constant->GetValue();
740 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
741
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900742 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700743 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
744 ClassName(interface, ClassNames::INTERFACE),
745 constant->GetName(), {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700746 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700747 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700748 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700749 getter->GetStatementBlock()->AddLiteral("return value");
750 decls.push_back(std::move(getter));
751 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700752
Jiyong Park75e1a742018-07-04 12:31:23 +0900753 // Implement the default impl class.
754 // onAsBinder returns nullptr as this interface is not associated with a
755 // real binder.
756 const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL));
757 decls.emplace_back(
758 new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n"
759 " return nullptr;\n"
760 "}\n",
761 default_impl.c_str())));
762 // Each interface method by default returns UNKNOWN_TRANSACTION with is
763 // the same status that is returned by transact() when the method is
764 // not implemented in the server side. In other words, these default
765 // methods do nothing; they only exist to aid making a real default
766 // impl class without having to override all methods in an interface.
767 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900768 if (method->IsUserDefined()) {
769 std::ostringstream code;
770 code << "::android::binder::Status " << default_impl << "::" << method->GetName()
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900771 << BuildArgList(typenames, *method, true, true).ToString() << " {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900772 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
773 << "}\n";
774 decls.emplace_back(new LiteralDecl(code.str()));
775 } else {
776 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
777 std::ostringstream code;
778 code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n"
779 << " return 0;\n"
780 << "}\n";
781 decls.emplace_back(new LiteralDecl(code.str()));
782 }
783 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900784 }
785
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700786 return unique_ptr<Document>{new CppSource{
787 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700788 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700789}
790
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900791unique_ptr<Document> BuildClientHeader(const AidlTypenames& typenames,
792 const AidlInterface& interface, const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700793 const string i_name = ClassName(interface, ClassNames::INTERFACE);
794 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700795
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900796 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900797 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900798
Christopher Wileyb23149d2015-10-14 13:52:21 -0700799 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
800 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800801 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
802 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700803 ConstructorDecl::IS_EXPLICIT
804 }};
805 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
806 "~" + bp_name,
807 ArgList{},
808 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700809
Christopher Wileyf944e792015-09-29 10:00:46 -0700810 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700811 publics.push_back(std::move(constructor));
812 publics.push_back(std::move(destructor));
813
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700814 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900815 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900816 publics.push_back(BuildMethodDecl(*method, typenames, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900817 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900818 publics.push_back(BuildMetaMethodDecl(*method, typenames, options, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900819 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700820 }
821
Jiyong Parkce50e262018-10-29 09:54:20 +0900822 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900823 includes.emplace_back("chrono"); // for std::chrono::steady_clock
824 includes.emplace_back("functional"); // for std::function
825 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900826 publics.emplace_back(
827 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
828 }
829
Jiyong Park309668e2018-07-28 16:55:44 +0900830 vector<unique_ptr<Declaration>> privates;
831
832 if (options.Version() > 0) {
833 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
834 }
835
836 unique_ptr<ClassDecl> bp_class{new ClassDecl{
837 bp_name,
838 "::android::BpInterface<" + i_name + ">",
839 std::move(publics),
840 std::move(privates),
841 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700842
Jiyong Parkce50e262018-10-29 09:54:20 +0900843 return unique_ptr<Document>{
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900844 new CppHeader{BuildHeaderGuard(interface, ClassNames::CLIENT), includes,
Jiyong Parkce50e262018-10-29 09:54:20 +0900845 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700846}
847
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900848unique_ptr<Document> BuildServerHeader(const AidlTypenames& /* typenames */,
Jeongik Cha46375122018-12-21 18:41:21 +0900849 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700850 const string i_name = ClassName(interface, ClassNames::INTERFACE);
851 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700852
Steven Moreland800508d2019-07-30 10:45:31 -0700853 unique_ptr<ConstructorDecl> constructor{
854 new ConstructorDecl{bn_name, ArgList{}, ConstructorDecl::IS_EXPLICIT}};
855
Christopher Wileyfd51d602015-10-14 13:04:48 -0700856 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700857 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800858 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
859 StringPrintf("const %s& %s", kAndroidParcelLiteral,
860 kDataVarName),
861 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900862 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700863 MethodDecl::IS_OVERRIDE
864 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900865 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700866
Jeongik Cha46375122018-12-21 18:41:21 +0900867 vector<unique_ptr<Declaration>> publics;
Steven Moreland800508d2019-07-30 10:45:31 -0700868 publics.push_back(std::move(constructor));
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700869 publics.push_back(std::move(on_transact));
870
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900871 if (options.Version() > 0) {
872 std::ostringstream code;
873 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
874 publics.emplace_back(new LiteralDecl(code.str()));
875 }
876
Jeongik Cha46375122018-12-21 18:41:21 +0900877 if (options.GenLog()) {
878 includes.emplace_back("chrono"); // for std::chrono::steady_clock
879 includes.emplace_back("functional"); // for std::function
880 includes.emplace_back("json/value.h");
881 publics.emplace_back(
882 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
883 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700884 unique_ptr<ClassDecl> bn_class{
885 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800886 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700887 std::move(publics),
888 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700889 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700890
Jeongik Cha46375122018-12-21 18:41:21 +0900891 return unique_ptr<Document>{
892 new CppHeader{BuildHeaderGuard(interface, ClassNames::SERVER), includes,
893 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700894}
895
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900896unique_ptr<Document> BuildInterfaceHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900897 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900898 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700899
900 for (const auto& method : interface.GetMethods()) {
901 for (const auto& argument : method->GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900902 AddHeaders(argument->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700903 }
904
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900905 AddHeaders(method->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700906 }
907
Jiyong Park75e1a742018-07-04 12:31:23 +0900908 const string i_name = ClassName(interface, ClassNames::INTERFACE);
909 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800910 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700911 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700912 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700913
Jiyong Park309668e2018-07-28 16:55:44 +0900914 if (options.Version() > 0) {
915 std::ostringstream code;
916 code << "const int32_t VERSION = " << options.Version() << ";\n";
917
918 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
919 }
920
Steven Moreland693640b2018-07-19 13:46:27 -0700921 std::vector<std::unique_ptr<Declaration>> string_constants;
Daniel Norman85aed542019-08-21 12:01:14 -0700922 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t", false}};
Steven Moreland693640b2018-07-19 13:46:27 -0700923 for (const auto& constant : interface.GetConstantDeclarations()) {
924 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800925
Steven Moreland693640b2018-07-19 13:46:27 -0700926 switch (value.GetType()) {
927 case AidlConstantValue::Type::STRING: {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900928 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700929 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
930 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700931 string_constants.push_back(std::move(getter));
932 break;
933 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700934 case AidlConstantValue::Type::BOOLEAN: // fall-through
935 case AidlConstantValue::Type::INT8: // fall-through
936 case AidlConstantValue::Type::INT32: {
Steven Moreland860b1942018-08-16 14:59:28 -0700937 int_constant_enum->AddValue(constant->GetName(),
938 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700939 break;
940 }
941 default: {
942 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
943 }
944 }
945 }
946 if (int_constant_enum->HasValues()) {
947 if_class->AddPublic(std::move(int_constant_enum));
948 }
949 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700950 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700951
952 for (auto& string_constant : string_constants) {
953 if_class->AddPublic(std::move(string_constant));
954 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700955 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100956
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900957 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100958 includes.insert(kTraceHeader);
959 }
960
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700961 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700962 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900963 if (method->IsUserDefined()) {
964 // Each method gets an enum entry and pure virtual declaration.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900965 if_class->AddPublic(BuildMethodDecl(*method, typenames, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900966 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900967 if_class->AddPublic(BuildMetaMethodDecl(*method, typenames, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900968 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700969 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700970 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700971
Jiyong Park75e1a742018-07-04 12:31:23 +0900972 vector<unique_ptr<Declaration>> decls;
973 decls.emplace_back(std::move(if_class));
974
975 // Base class for the default implementation.
976 vector<string> method_decls;
977 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900978 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900979 method_decls.emplace_back(BuildMethodDecl(*method, typenames, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900980 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900981 method_decls.emplace_back(
982 BuildMetaMethodDecl(*method, typenames, options, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900983 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900984 }
Jiyong Park309668e2018-07-28 16:55:44 +0900985
Jiyong Park75e1a742018-07-04 12:31:23 +0900986 decls.emplace_back(new LiteralDecl(
987 android::base::StringPrintf("class %s : public %s {\n"
988 "public:\n"
989 " ::android::IBinder* onAsBinder() override;\n"
990 " %s\n"
991 "};\n",
992 ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(),
993 i_name.c_str(), Join(method_decls, " ").c_str())));
994
995 return unique_ptr<Document>{
996 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
997 vector<string>(includes.begin(), includes.end()),
998 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700999}
1000
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001001std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001002 const AidlStructuredParcelable& parcel,
1003 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001004 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
1005
1006 set<string> includes = {kStatusHeader, kParcelHeader};
Jeongik Cha10f72b72019-09-04 21:28:13 +09001007 includes.insert("tuple");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001008 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001009 AddHeaders(variable->GetType(), typenames, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001010 }
1011
Jeongik Cha10f72b72019-09-04 21:28:13 +09001012 set<string> operators = {"<", ">", "==", ">=", "<=", "!="};
1013 for (const auto& op : operators) {
1014 std::ostringstream operator_code;
1015 std::vector<std::string> variable_name;
1016 std::vector<std::string> rhs_variable_name;
1017 for (const auto& variable : parcel.GetFields()) {
1018 variable_name.push_back(variable->GetName());
1019 rhs_variable_name.push_back("rhs." + variable->GetName());
1020 }
1021
1022 operator_code << "inline bool operator" << op << "(const " << parcel.GetName()
1023 << "& rhs) const {\n"
1024 << " return "
1025 << "std::tie(" << Join(variable_name, ", ") << ")" << op << "std::tie("
1026 << Join(rhs_variable_name, ", ") << ")"
1027 << ";\n"
1028 << "}\n";
1029
1030 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
1031 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001032 for (const auto& variable : parcel.GetFields()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001033
Steven Moreland9ea10e32018-07-19 15:26:09 -07001034 std::ostringstream out;
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001035 std::string cppType = CppNameOf(variable->GetType(), typenames);
1036 out << cppType.c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001037 if (variable->GetDefaultValue()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001038 out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
1039 << ")";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001040 }
1041 out << ";\n";
1042
1043 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001044 }
1045
1046 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001047 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001048 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001049 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001050 unique_ptr<MethodDecl> write(new MethodDecl(
1051 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1052 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001053 parcel_class->AddPublic(std::move(write));
1054
1055 return unique_ptr<Document>{new CppHeader{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001056 BuildHeaderGuard(parcel, ClassNames::RAW), vector<string>(includes.begin(), includes.end()),
Steven Moreland5557f1c2018-07-02 13:50:23 -07001057 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1058}
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001059std::unique_ptr<Document> BuildParcelSource(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001060 const AidlStructuredParcelable& parcel,
1061 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001062 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1063 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001064 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001065 StatementBlock* read_block = read->GetStatementBlock();
1066 read_block->AddLiteral(
1067 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001068
1069 read_block->AddLiteral(
1070 "size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1071 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1072 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
1073 "size_t _aidl_parcelable_size = static_cast<size_t>(_aidl_parcelable_raw_size);\n");
1074
Steven Moreland5557f1c2018-07-02 13:50:23 -07001075 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001076 string method = ParcelReadMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001077
1078 read_block->AddStatement(new Assignment(
1079 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
Daniel Norman85aed542019-08-21 12:01:14 -07001080 ParcelReadCastOf(variable->GetType(), typenames,
1081 "&" + variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001082 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001083 read_block->AddLiteral(StringPrintf(
1084 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1085 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1086 " return %s;\n"
1087 "}",
1088 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001089 }
1090 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1091
Steven Morelandce39c532018-07-11 16:59:50 -07001092 unique_ptr<MethodImpl> write{
1093 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1094 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001095 StatementBlock* write_block = write->GetStatementBlock();
1096 write_block->AddLiteral(
1097 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001098
1099 write_block->AddLiteral(
1100 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1101 "_aidl_parcel->writeInt32(0);");
1102
Steven Moreland5557f1c2018-07-02 13:50:23 -07001103 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001104 string method = ParcelWriteMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001105 write_block->AddStatement(new Assignment(
Daniel Norman85aed542019-08-21 12:01:14 -07001106 kAndroidStatusVarName,
1107 new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1108 ParcelWriteCastOf(variable->GetType(), typenames, variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001109 write_block->AddStatement(ReturnOnStatusNotOk());
1110 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001111
1112 write_block->AddLiteral(
1113 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1114 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1115 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1116 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001117 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1118
1119 vector<unique_ptr<Declaration>> file_decls;
1120 file_decls.push_back(std::move(read));
1121 file_decls.push_back(std::move(write));
1122
1123 set<string> includes = {};
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001124 AddHeaders(parcel, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001125
1126 return unique_ptr<Document>{
1127 new CppSource{vector<string>(includes.begin(), includes.end()),
1128 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1129}
1130
Daniel Norman0c1bd362019-11-12 23:05:31 -08001131std::string GenerateEnumToString(const AidlTypenames& typenames,
1132 const AidlEnumDeclaration& enum_decl) {
1133 std::ostringstream code;
1134 code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n";
1135 code << " switch(val) {\n";
1136 std::set<std::string> unique_cases;
1137 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1138 std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator);
1139 // Only add a case if its value has not yet been used in the switch
1140 // statement. C++ does not allow multiple cases with the same value, but
1141 // enums does allow this. In this scenario, the first declared
1142 // enumerator with the given value is printed.
1143 if (unique_cases.count(c) == 0) {
1144 unique_cases.insert(c);
1145 code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n";
1146 code << " return \"" << enumerator->GetName() << "\";\n";
1147 }
1148 }
1149 code << " default:\n";
1150 code << " return std::to_string(static_cast<"
1151 << CppNameOf(enum_decl.GetBackingType(), typenames) << ">(val));\n";
1152 code << " }\n";
1153 code << "}\n";
1154 return code.str();
1155}
1156
Daniel Norman85aed542019-08-21 12:01:14 -07001157std::unique_ptr<Document> BuildEnumHeader(const AidlTypenames& typenames,
1158 const AidlEnumDeclaration& enum_decl) {
1159 unique_ptr<Enum> generated_enum{
1160 new Enum{enum_decl.GetName(), CppNameOf(enum_decl.GetBackingType(), typenames), true}};
1161 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1162 generated_enum->AddValue(
1163 enumerator->GetName(),
1164 enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator));
1165 }
1166
Daniel Norman0c1bd362019-11-12 23:05:31 -08001167 set<string> includes = {"string"};
Daniel Norman85aed542019-08-21 12:01:14 -07001168 AddHeaders(enum_decl.GetBackingType(), typenames, includes);
1169
Daniel Norman0c1bd362019-11-12 23:05:31 -08001170 vector<unique_ptr<Declaration>> decls;
1171 decls.emplace_back(std::move(generated_enum));
1172 decls.emplace_back(
1173 unique_ptr<Declaration>(new LiteralDecl(GenerateEnumToString(typenames, enum_decl))));
1174
Daniel Norman85aed542019-08-21 12:01:14 -07001175 return unique_ptr<Document>{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001176 new CppHeader{BuildHeaderGuard(enum_decl, ClassNames::RAW),
Daniel Norman85aed542019-08-21 12:01:14 -07001177 vector<string>(includes.begin(), includes.end()),
Daniel Norman0c1bd362019-11-12 23:05:31 -08001178 NestInNamespaces(std::move(decls), enum_decl.GetSplitPackage())}};
Daniel Norman85aed542019-08-21 12:01:14 -07001179}
1180
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001181bool WriteHeader(const Options& options, const AidlTypenames& typenames,
1182 const AidlInterface& interface, const IoDelegate& io_delegate,
1183 ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001184 unique_ptr<Document> header;
1185 switch (header_type) {
1186 case ClassNames::INTERFACE:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001187 header = BuildInterfaceHeader(typenames, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001188 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001189 break;
1190 case ClassNames::CLIENT:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001191 header = BuildClientHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001192 break;
1193 case ClassNames::SERVER:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001194 header = BuildServerHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001195 break;
1196 default:
1197 LOG(FATAL) << "aidl internal error";
1198 }
1199 if (!header) {
1200 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001201 return false;
1202 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001203
Jiyong Park05463732018-08-09 16:03:02 +09001204 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001205 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1206 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001207
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001208 const bool success = code_writer->Close();
1209 if (!success) {
1210 io_delegate.RemovePath(header_path);
1211 }
1212
1213 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001214}
1215
Casey Dahlina834dd42015-09-23 11:52:15 -07001216} // namespace internals
1217
1218using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001219
Jiyong Park74595c12018-07-23 15:22:50 +09001220bool GenerateCppInterface(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001221 const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Park74595c12018-07-23 15:22:50 +09001222 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001223 auto interface_src = BuildInterfaceSource(typenames, interface, options);
1224 auto client_src = BuildClientSource(typenames, interface, options);
1225 auto server_src = BuildServerSource(typenames, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001226
Christopher Wiley054afbd2015-10-16 17:08:43 -07001227 if (!interface_src || !client_src || !server_src) {
1228 return false;
1229 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001230
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001231 if (!WriteHeader(options, typenames, interface, io_delegate, ClassNames::INTERFACE) ||
1232 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::CLIENT) ||
1233 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::SERVER)) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001234 return false;
1235 }
1236
Jiyong Park74595c12018-07-23 15:22:50 +09001237 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001238 interface_src->Write(writer.get());
1239 client_src->Write(writer.get());
1240 server_src->Write(writer.get());
1241
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001242 const bool success = writer->Close();
1243 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001244 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001245 }
1246
1247 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001248}
1249
Jiyong Park74595c12018-07-23 15:22:50 +09001250bool GenerateCppParcel(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001251 const AidlTypenames& typenames, const AidlStructuredParcelable& parcelable,
Jiyong Park74595c12018-07-23 15:22:50 +09001252 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001253 auto header = BuildParcelHeader(typenames, parcelable, options);
1254 auto source = BuildParcelSource(typenames, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001255
1256 if (!header || !source) {
1257 return false;
1258 }
1259
Jiyong Park5b7e5322019-04-03 20:05:01 +09001260 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001261 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1262 header->Write(header_writer.get());
1263 CHECK(header_writer->Close());
1264
Steven Moreland81079f92018-07-06 16:15:53 -07001265 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001266 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001267 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1268 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1269 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001270 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001271 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1272 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1273 CHECK(bn_writer->Close());
1274
Jiyong Park74595c12018-07-23 15:22:50 +09001275 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001276 source->Write(source_writer.get());
1277 CHECK(source_writer->Close());
1278
1279 return true;
1280}
1281
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001282bool GenerateCppParcelDeclaration(const std::string& filename, const Options& options,
1283 const AidlParcelable& parcelable, const IoDelegate& io_delegate) {
1284 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1285 *source_writer
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001286 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001287 CHECK(source_writer->Close());
1288
1289 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1290 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
1291 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1292 header_writer->Write("#error TODO(b/111362593) parcelables do not have headers");
1293 CHECK(header_writer->Close());
1294 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
1295 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1296 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1297 CHECK(bp_writer->Close());
1298 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
1299 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1300 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1301 CHECK(bn_writer->Close());
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001302
1303 return true;
1304}
1305
Daniel Norman85aed542019-08-21 12:01:14 -07001306bool GenerateCppEnumDeclaration(const std::string& filename, const Options& options,
1307 const AidlTypenames& typenames,
1308 const AidlEnumDeclaration& enum_decl,
1309 const IoDelegate& io_delegate) {
1310 auto header = BuildEnumHeader(typenames, enum_decl);
1311 if (!header) return false;
1312
1313 const string header_path = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::RAW);
1314 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1315 header->Write(header_writer.get());
1316 CHECK(header_writer->Close());
1317
1318 // TODO(b/111362593): no unnecessary files just to have consistent output with interfaces
1319 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1320 *source_writer
1321 << "// This file is intentionally left blank as placeholder for enum declaration.\n";
1322 CHECK(source_writer->Close());
1323 const string bp_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::CLIENT);
1324 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1325 bp_writer->Write("#error TODO(b/111362593) enums do not have bp classes");
1326 CHECK(bp_writer->Close());
1327 const string bn_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::SERVER);
1328 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1329 bn_writer->Write("#error TODO(b/111362593) enums do not have bn classes");
1330 CHECK(bn_writer->Close());
1331
1332 return true;
1333}
1334
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001335bool GenerateCpp(const string& output_file, const Options& options, const AidlTypenames& typenames,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001336 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1337 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1338 if (parcelable != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001339 return GenerateCppParcel(output_file, options, typenames, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001340 }
1341
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001342 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1343 if (parcelable_decl != nullptr) {
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001344 return GenerateCppParcelDeclaration(output_file, options, *parcelable_decl, io_delegate);
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001345 }
1346
Daniel Norman85aed542019-08-21 12:01:14 -07001347 const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration();
1348 if (enum_decl != nullptr) {
1349 return GenerateCppEnumDeclaration(output_file, options, typenames, *enum_decl, io_delegate);
1350 }
1351
Steven Moreland5557f1c2018-07-02 13:50:23 -07001352 const AidlInterface* interface = defined_type.AsInterface();
1353 if (interface != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001354 return GenerateCppInterface(output_file, options, typenames, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001355 }
1356
1357 CHECK(false) << "Unrecognized type sent for cpp generation.";
1358 return false;
1359}
1360
Christopher Wileyf944e792015-09-29 10:00:46 -07001361} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001362} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001363} // namespace android