blob: 81c92cf9bce64e9cea49bcfc9a0f6bdc9f2ac1d7 [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{
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700734 "IMPLEMENT_META_INTERFACE",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700735 ArgList{vector<string>{ClassName(interface, ClassNames::BASE),
Christopher Wileyade4b452015-10-10 11:06:03 -0700736 '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700737 decls.push_back(std::move(meta_if));
738
Steven Moreland693640b2018-07-19 13:46:27 -0700739 for (const auto& constant : interface.GetConstantDeclarations()) {
740 const AidlConstantValue& value = constant->GetValue();
741 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
742
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900743 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700744 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
745 ClassName(interface, ClassNames::INTERFACE),
746 constant->GetName(), {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700747 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700748 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700749 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700750 getter->GetStatementBlock()->AddLiteral("return value");
751 decls.push_back(std::move(getter));
752 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700753
Jiyong Park75e1a742018-07-04 12:31:23 +0900754 // Implement the default impl class.
755 // onAsBinder returns nullptr as this interface is not associated with a
756 // real binder.
757 const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL));
758 decls.emplace_back(
759 new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n"
760 " return nullptr;\n"
761 "}\n",
762 default_impl.c_str())));
763 // Each interface method by default returns UNKNOWN_TRANSACTION with is
764 // the same status that is returned by transact() when the method is
765 // not implemented in the server side. In other words, these default
766 // methods do nothing; they only exist to aid making a real default
767 // impl class without having to override all methods in an interface.
768 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900769 if (method->IsUserDefined()) {
770 std::ostringstream code;
771 code << "::android::binder::Status " << default_impl << "::" << method->GetName()
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900772 << BuildArgList(typenames, *method, true, true).ToString() << " {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900773 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
774 << "}\n";
775 decls.emplace_back(new LiteralDecl(code.str()));
776 } else {
777 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
778 std::ostringstream code;
779 code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n"
780 << " return 0;\n"
781 << "}\n";
782 decls.emplace_back(new LiteralDecl(code.str()));
783 }
784 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900785 }
786
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700787 return unique_ptr<Document>{new CppSource{
788 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700789 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700790}
791
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900792unique_ptr<Document> BuildClientHeader(const AidlTypenames& typenames,
793 const AidlInterface& interface, const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700794 const string i_name = ClassName(interface, ClassNames::INTERFACE);
795 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700796
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900797 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900798 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900799
Christopher Wileyb23149d2015-10-14 13:52:21 -0700800 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
801 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800802 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
803 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700804 ConstructorDecl::IS_EXPLICIT
805 }};
806 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
807 "~" + bp_name,
808 ArgList{},
809 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700810
Christopher Wileyf944e792015-09-29 10:00:46 -0700811 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700812 publics.push_back(std::move(constructor));
813 publics.push_back(std::move(destructor));
814
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700815 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900816 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900817 publics.push_back(BuildMethodDecl(*method, typenames, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900818 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900819 publics.push_back(BuildMetaMethodDecl(*method, typenames, options, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900820 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700821 }
822
Jiyong Parkce50e262018-10-29 09:54:20 +0900823 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900824 includes.emplace_back("chrono"); // for std::chrono::steady_clock
825 includes.emplace_back("functional"); // for std::function
826 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900827 publics.emplace_back(
828 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
829 }
830
Jiyong Park309668e2018-07-28 16:55:44 +0900831 vector<unique_ptr<Declaration>> privates;
832
833 if (options.Version() > 0) {
834 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
835 }
836
837 unique_ptr<ClassDecl> bp_class{new ClassDecl{
838 bp_name,
839 "::android::BpInterface<" + i_name + ">",
840 std::move(publics),
841 std::move(privates),
842 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700843
Jiyong Parkce50e262018-10-29 09:54:20 +0900844 return unique_ptr<Document>{
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900845 new CppHeader{BuildHeaderGuard(interface, ClassNames::CLIENT), includes,
Jiyong Parkce50e262018-10-29 09:54:20 +0900846 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700847}
848
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900849unique_ptr<Document> BuildServerHeader(const AidlTypenames& /* typenames */,
Jeongik Cha46375122018-12-21 18:41:21 +0900850 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700851 const string i_name = ClassName(interface, ClassNames::INTERFACE);
852 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700853
Steven Moreland800508d2019-07-30 10:45:31 -0700854 unique_ptr<ConstructorDecl> constructor{
855 new ConstructorDecl{bn_name, ArgList{}, ConstructorDecl::IS_EXPLICIT}};
856
Christopher Wileyfd51d602015-10-14 13:04:48 -0700857 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700858 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800859 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
860 StringPrintf("const %s& %s", kAndroidParcelLiteral,
861 kDataVarName),
862 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900863 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700864 MethodDecl::IS_OVERRIDE
865 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900866 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700867
Jeongik Cha46375122018-12-21 18:41:21 +0900868 vector<unique_ptr<Declaration>> publics;
Steven Moreland800508d2019-07-30 10:45:31 -0700869 publics.push_back(std::move(constructor));
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700870 publics.push_back(std::move(on_transact));
871
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900872 if (options.Version() > 0) {
873 std::ostringstream code;
874 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
875 publics.emplace_back(new LiteralDecl(code.str()));
876 }
877
Jeongik Cha46375122018-12-21 18:41:21 +0900878 if (options.GenLog()) {
879 includes.emplace_back("chrono"); // for std::chrono::steady_clock
880 includes.emplace_back("functional"); // for std::function
881 includes.emplace_back("json/value.h");
882 publics.emplace_back(
883 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
884 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700885 unique_ptr<ClassDecl> bn_class{
886 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800887 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700888 std::move(publics),
889 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700890 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700891
Jeongik Cha46375122018-12-21 18:41:21 +0900892 return unique_ptr<Document>{
893 new CppHeader{BuildHeaderGuard(interface, ClassNames::SERVER), includes,
894 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700895}
896
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900897unique_ptr<Document> BuildInterfaceHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900898 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900899 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700900
901 for (const auto& method : interface.GetMethods()) {
902 for (const auto& argument : method->GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900903 AddHeaders(argument->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700904 }
905
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900906 AddHeaders(method->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700907 }
908
Jiyong Park75e1a742018-07-04 12:31:23 +0900909 const string i_name = ClassName(interface, ClassNames::INTERFACE);
910 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800911 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700912 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700913 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700914
Jiyong Park309668e2018-07-28 16:55:44 +0900915 if (options.Version() > 0) {
916 std::ostringstream code;
917 code << "const int32_t VERSION = " << options.Version() << ";\n";
918
919 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
920 }
921
Steven Moreland693640b2018-07-19 13:46:27 -0700922 std::vector<std::unique_ptr<Declaration>> string_constants;
Daniel Norman85aed542019-08-21 12:01:14 -0700923 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t", false}};
Steven Moreland693640b2018-07-19 13:46:27 -0700924 for (const auto& constant : interface.GetConstantDeclarations()) {
925 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800926
Steven Moreland693640b2018-07-19 13:46:27 -0700927 switch (value.GetType()) {
928 case AidlConstantValue::Type::STRING: {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900929 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700930 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
931 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700932 string_constants.push_back(std::move(getter));
933 break;
934 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700935 case AidlConstantValue::Type::BOOLEAN: // fall-through
936 case AidlConstantValue::Type::INT8: // fall-through
937 case AidlConstantValue::Type::INT32: {
Steven Moreland860b1942018-08-16 14:59:28 -0700938 int_constant_enum->AddValue(constant->GetName(),
939 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700940 break;
941 }
942 default: {
943 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
944 }
945 }
946 }
947 if (int_constant_enum->HasValues()) {
948 if_class->AddPublic(std::move(int_constant_enum));
949 }
950 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700951 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700952
953 for (auto& string_constant : string_constants) {
954 if_class->AddPublic(std::move(string_constant));
955 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700956 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100957
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900958 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100959 includes.insert(kTraceHeader);
960 }
961
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700962 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700963 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900964 if (method->IsUserDefined()) {
965 // Each method gets an enum entry and pure virtual declaration.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900966 if_class->AddPublic(BuildMethodDecl(*method, typenames, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900967 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900968 if_class->AddPublic(BuildMetaMethodDecl(*method, typenames, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900969 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700970 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700971 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700972
Jiyong Park75e1a742018-07-04 12:31:23 +0900973 vector<unique_ptr<Declaration>> decls;
974 decls.emplace_back(std::move(if_class));
975
976 // Base class for the default implementation.
977 vector<string> method_decls;
978 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900979 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900980 method_decls.emplace_back(BuildMethodDecl(*method, typenames, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900981 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900982 method_decls.emplace_back(
983 BuildMetaMethodDecl(*method, typenames, options, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900984 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900985 }
Jiyong Park309668e2018-07-28 16:55:44 +0900986
Jiyong Park75e1a742018-07-04 12:31:23 +0900987 decls.emplace_back(new LiteralDecl(
988 android::base::StringPrintf("class %s : public %s {\n"
989 "public:\n"
990 " ::android::IBinder* onAsBinder() override;\n"
991 " %s\n"
992 "};\n",
993 ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(),
994 i_name.c_str(), Join(method_decls, " ").c_str())));
995
996 return unique_ptr<Document>{
997 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
998 vector<string>(includes.begin(), includes.end()),
999 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001000}
1001
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001002std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001003 const AidlStructuredParcelable& parcel,
1004 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001005 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
1006
1007 set<string> includes = {kStatusHeader, kParcelHeader};
Jeongik Cha10f72b72019-09-04 21:28:13 +09001008 includes.insert("tuple");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001009 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001010 AddHeaders(variable->GetType(), typenames, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001011 }
1012
Jeongik Cha10f72b72019-09-04 21:28:13 +09001013 set<string> operators = {"<", ">", "==", ">=", "<=", "!="};
1014 for (const auto& op : operators) {
1015 std::ostringstream operator_code;
1016 std::vector<std::string> variable_name;
1017 std::vector<std::string> rhs_variable_name;
1018 for (const auto& variable : parcel.GetFields()) {
1019 variable_name.push_back(variable->GetName());
1020 rhs_variable_name.push_back("rhs." + variable->GetName());
1021 }
1022
1023 operator_code << "inline bool operator" << op << "(const " << parcel.GetName()
1024 << "& rhs) const {\n"
1025 << " return "
1026 << "std::tie(" << Join(variable_name, ", ") << ")" << op << "std::tie("
1027 << Join(rhs_variable_name, ", ") << ")"
1028 << ";\n"
1029 << "}\n";
1030
1031 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
1032 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001033 for (const auto& variable : parcel.GetFields()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001034
Steven Moreland9ea10e32018-07-19 15:26:09 -07001035 std::ostringstream out;
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001036 std::string cppType = CppNameOf(variable->GetType(), typenames);
1037 out << cppType.c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001038 if (variable->GetDefaultValue()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001039 out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
1040 << ")";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001041 }
1042 out << ";\n";
1043
1044 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001045 }
1046
1047 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001048 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001049 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001050 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001051 unique_ptr<MethodDecl> write(new MethodDecl(
1052 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1053 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001054 parcel_class->AddPublic(std::move(write));
1055
1056 return unique_ptr<Document>{new CppHeader{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001057 BuildHeaderGuard(parcel, ClassNames::RAW), vector<string>(includes.begin(), includes.end()),
Steven Moreland5557f1c2018-07-02 13:50:23 -07001058 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1059}
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001060std::unique_ptr<Document> BuildParcelSource(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001061 const AidlStructuredParcelable& parcel,
1062 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001063 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1064 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001065 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001066 StatementBlock* read_block = read->GetStatementBlock();
1067 read_block->AddLiteral(
1068 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001069
1070 read_block->AddLiteral(
1071 "size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1072 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1073 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
1074 "size_t _aidl_parcelable_size = static_cast<size_t>(_aidl_parcelable_raw_size);\n");
1075
Steven Moreland5557f1c2018-07-02 13:50:23 -07001076 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001077 string method = ParcelReadMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001078
1079 read_block->AddStatement(new Assignment(
1080 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
Daniel Norman85aed542019-08-21 12:01:14 -07001081 ParcelReadCastOf(variable->GetType(), typenames,
1082 "&" + variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001083 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001084 read_block->AddLiteral(StringPrintf(
1085 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1086 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1087 " return %s;\n"
1088 "}",
1089 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001090 }
1091 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1092
Steven Morelandce39c532018-07-11 16:59:50 -07001093 unique_ptr<MethodImpl> write{
1094 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1095 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001096 StatementBlock* write_block = write->GetStatementBlock();
1097 write_block->AddLiteral(
1098 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001099
1100 write_block->AddLiteral(
1101 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1102 "_aidl_parcel->writeInt32(0);");
1103
Steven Moreland5557f1c2018-07-02 13:50:23 -07001104 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001105 string method = ParcelWriteMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001106 write_block->AddStatement(new Assignment(
Daniel Norman85aed542019-08-21 12:01:14 -07001107 kAndroidStatusVarName,
1108 new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1109 ParcelWriteCastOf(variable->GetType(), typenames, variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001110 write_block->AddStatement(ReturnOnStatusNotOk());
1111 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001112
1113 write_block->AddLiteral(
1114 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1115 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1116 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1117 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001118 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1119
1120 vector<unique_ptr<Declaration>> file_decls;
1121 file_decls.push_back(std::move(read));
1122 file_decls.push_back(std::move(write));
1123
1124 set<string> includes = {};
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001125 AddHeaders(parcel, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001126
1127 return unique_ptr<Document>{
1128 new CppSource{vector<string>(includes.begin(), includes.end()),
1129 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1130}
1131
Daniel Norman0c1bd362019-11-12 23:05:31 -08001132std::string GenerateEnumToString(const AidlTypenames& typenames,
1133 const AidlEnumDeclaration& enum_decl) {
1134 std::ostringstream code;
1135 code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n";
1136 code << " switch(val) {\n";
1137 std::set<std::string> unique_cases;
1138 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1139 std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator);
1140 // Only add a case if its value has not yet been used in the switch
1141 // statement. C++ does not allow multiple cases with the same value, but
1142 // enums does allow this. In this scenario, the first declared
1143 // enumerator with the given value is printed.
1144 if (unique_cases.count(c) == 0) {
1145 unique_cases.insert(c);
1146 code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n";
1147 code << " return \"" << enumerator->GetName() << "\";\n";
1148 }
1149 }
1150 code << " default:\n";
1151 code << " return std::to_string(static_cast<"
1152 << CppNameOf(enum_decl.GetBackingType(), typenames) << ">(val));\n";
1153 code << " }\n";
1154 code << "}\n";
1155 return code.str();
1156}
1157
Daniel Norman85aed542019-08-21 12:01:14 -07001158std::unique_ptr<Document> BuildEnumHeader(const AidlTypenames& typenames,
1159 const AidlEnumDeclaration& enum_decl) {
1160 unique_ptr<Enum> generated_enum{
1161 new Enum{enum_decl.GetName(), CppNameOf(enum_decl.GetBackingType(), typenames), true}};
1162 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1163 generated_enum->AddValue(
1164 enumerator->GetName(),
1165 enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator));
1166 }
1167
Daniel Norman0c1bd362019-11-12 23:05:31 -08001168 set<string> includes = {"string"};
Daniel Norman85aed542019-08-21 12:01:14 -07001169 AddHeaders(enum_decl.GetBackingType(), typenames, includes);
1170
Daniel Norman0c1bd362019-11-12 23:05:31 -08001171 vector<unique_ptr<Declaration>> decls;
1172 decls.emplace_back(std::move(generated_enum));
1173 decls.emplace_back(
1174 unique_ptr<Declaration>(new LiteralDecl(GenerateEnumToString(typenames, enum_decl))));
1175
Daniel Norman85aed542019-08-21 12:01:14 -07001176 return unique_ptr<Document>{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001177 new CppHeader{BuildHeaderGuard(enum_decl, ClassNames::RAW),
Daniel Norman85aed542019-08-21 12:01:14 -07001178 vector<string>(includes.begin(), includes.end()),
Daniel Norman0c1bd362019-11-12 23:05:31 -08001179 NestInNamespaces(std::move(decls), enum_decl.GetSplitPackage())}};
Daniel Norman85aed542019-08-21 12:01:14 -07001180}
1181
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001182bool WriteHeader(const Options& options, const AidlTypenames& typenames,
1183 const AidlInterface& interface, const IoDelegate& io_delegate,
1184 ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001185 unique_ptr<Document> header;
1186 switch (header_type) {
1187 case ClassNames::INTERFACE:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001188 header = BuildInterfaceHeader(typenames, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001189 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001190 break;
1191 case ClassNames::CLIENT:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001192 header = BuildClientHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001193 break;
1194 case ClassNames::SERVER:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001195 header = BuildServerHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001196 break;
1197 default:
1198 LOG(FATAL) << "aidl internal error";
1199 }
1200 if (!header) {
1201 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001202 return false;
1203 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001204
Jiyong Park05463732018-08-09 16:03:02 +09001205 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001206 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1207 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001208
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001209 const bool success = code_writer->Close();
1210 if (!success) {
1211 io_delegate.RemovePath(header_path);
1212 }
1213
1214 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001215}
1216
Casey Dahlina834dd42015-09-23 11:52:15 -07001217} // namespace internals
1218
1219using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001220
Jiyong Park74595c12018-07-23 15:22:50 +09001221bool GenerateCppInterface(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001222 const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Park74595c12018-07-23 15:22:50 +09001223 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001224 auto interface_src = BuildInterfaceSource(typenames, interface, options);
1225 auto client_src = BuildClientSource(typenames, interface, options);
1226 auto server_src = BuildServerSource(typenames, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001227
Christopher Wiley054afbd2015-10-16 17:08:43 -07001228 if (!interface_src || !client_src || !server_src) {
1229 return false;
1230 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001231
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001232 if (!WriteHeader(options, typenames, interface, io_delegate, ClassNames::INTERFACE) ||
1233 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::CLIENT) ||
1234 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::SERVER)) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001235 return false;
1236 }
1237
Jiyong Park74595c12018-07-23 15:22:50 +09001238 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001239 interface_src->Write(writer.get());
1240 client_src->Write(writer.get());
1241 server_src->Write(writer.get());
1242
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001243 const bool success = writer->Close();
1244 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001245 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001246 }
1247
1248 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001249}
1250
Jiyong Park74595c12018-07-23 15:22:50 +09001251bool GenerateCppParcel(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001252 const AidlTypenames& typenames, const AidlStructuredParcelable& parcelable,
Jiyong Park74595c12018-07-23 15:22:50 +09001253 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001254 auto header = BuildParcelHeader(typenames, parcelable, options);
1255 auto source = BuildParcelSource(typenames, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001256
1257 if (!header || !source) {
1258 return false;
1259 }
1260
Jiyong Park5b7e5322019-04-03 20:05:01 +09001261 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001262 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1263 header->Write(header_writer.get());
1264 CHECK(header_writer->Close());
1265
Steven Moreland81079f92018-07-06 16:15:53 -07001266 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001267 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001268 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1269 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1270 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001271 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001272 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1273 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1274 CHECK(bn_writer->Close());
1275
Jiyong Park74595c12018-07-23 15:22:50 +09001276 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001277 source->Write(source_writer.get());
1278 CHECK(source_writer->Close());
1279
1280 return true;
1281}
1282
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001283bool GenerateCppParcelDeclaration(const std::string& filename, const Options& options,
1284 const AidlParcelable& parcelable, const IoDelegate& io_delegate) {
1285 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1286 *source_writer
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001287 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001288 CHECK(source_writer->Close());
1289
1290 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1291 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
1292 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1293 header_writer->Write("#error TODO(b/111362593) parcelables do not have headers");
1294 CHECK(header_writer->Close());
1295 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
1296 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1297 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1298 CHECK(bp_writer->Close());
1299 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
1300 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1301 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1302 CHECK(bn_writer->Close());
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001303
1304 return true;
1305}
1306
Daniel Norman85aed542019-08-21 12:01:14 -07001307bool GenerateCppEnumDeclaration(const std::string& filename, const Options& options,
1308 const AidlTypenames& typenames,
1309 const AidlEnumDeclaration& enum_decl,
1310 const IoDelegate& io_delegate) {
1311 auto header = BuildEnumHeader(typenames, enum_decl);
1312 if (!header) return false;
1313
1314 const string header_path = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::RAW);
1315 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1316 header->Write(header_writer.get());
1317 CHECK(header_writer->Close());
1318
1319 // TODO(b/111362593): no unnecessary files just to have consistent output with interfaces
1320 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1321 *source_writer
1322 << "// This file is intentionally left blank as placeholder for enum declaration.\n";
1323 CHECK(source_writer->Close());
1324 const string bp_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::CLIENT);
1325 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1326 bp_writer->Write("#error TODO(b/111362593) enums do not have bp classes");
1327 CHECK(bp_writer->Close());
1328 const string bn_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::SERVER);
1329 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1330 bn_writer->Write("#error TODO(b/111362593) enums do not have bn classes");
1331 CHECK(bn_writer->Close());
1332
1333 return true;
1334}
1335
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001336bool GenerateCpp(const string& output_file, const Options& options, const AidlTypenames& typenames,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001337 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1338 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1339 if (parcelable != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001340 return GenerateCppParcel(output_file, options, typenames, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001341 }
1342
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001343 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1344 if (parcelable_decl != nullptr) {
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001345 return GenerateCppParcelDeclaration(output_file, options, *parcelable_decl, io_delegate);
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001346 }
1347
Daniel Norman85aed542019-08-21 12:01:14 -07001348 const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration();
1349 if (enum_decl != nullptr) {
1350 return GenerateCppEnumDeclaration(output_file, options, typenames, *enum_decl, io_delegate);
1351 }
1352
Steven Moreland5557f1c2018-07-02 13:50:23 -07001353 const AidlInterface* interface = defined_type.AsInterface();
1354 if (interface != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001355 return GenerateCppInterface(output_file, options, typenames, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001356 }
1357
1358 CHECK(false) << "Unrecognized type sent for cpp generation.";
1359 return false;
1360}
1361
Christopher Wileyf944e792015-09-29 10:00:46 -07001362} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001363} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001364} // namespace android