blob: e632df1547447d607ed6dc49b8565990e62c8454 [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;
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900102 // b/144943748: CppNameOf FileDescriptor is unique_fd. Don't pass it by
103 // const reference but by value to make it easier for the user to keep
104 // it beyond the scope of the call. unique_fd is a thin wrapper for an
105 // int (fd) so passing by value is not expensive.
106 const bool nonCopyable = IsNonCopyableType(a->GetType(), typenames);
Christopher Wileyad339272015-10-05 19:11:58 -0700107 if (for_declaration) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900108 // Method declarations need typenames, pointers to out params, and variable
Christopher Wileyad339272015-10-05 19:11:58 -0700109 // names that match the .aidl specification.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900110 literal = CppNameOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700111
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700112 if (a->IsOut()) {
113 literal = literal + "*";
114 } else {
Steven Morelandddec09d2019-10-07 16:10:41 -0700115 const auto definedType = typenames.TryGetDefinedType(a->GetType().GetName());
116
117 const bool isEnum = definedType && definedType->AsEnumDeclaration() != nullptr;
118 const bool isPrimitive = AidlTypenames::IsPrimitiveTypename(a->GetType().GetName());
119
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700120 // We pass in parameters that are not primitives by const reference.
121 // Arrays of primitives are not primitives.
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900122 if (!(isPrimitive || isEnum || nonCopyable) || a->GetType().IsArray()) {
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700123 literal = "const " + literal + "&";
124 }
125 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900126 if (!type_name_only) {
127 literal += " " + a->GetName();
128 }
Christopher Wileyad339272015-10-05 19:11:58 -0700129 } else {
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900130 std::string varName = BuildVarName(*a);
131 if (a->IsOut()) {
132 literal = "&" + varName;
133 } else if (nonCopyable) {
134 literal = "std::move(" + varName + ")";
135 } else {
136 literal = varName;
137 }
Christopher Wileyad339272015-10-05 19:11:58 -0700138 }
139 method_arguments.push_back(literal);
140 }
141
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900142 if (method.GetType().GetName() != "void") {
Christopher Wileyade4b452015-10-10 11:06:03 -0700143 string literal;
Christopher Wileyad339272015-10-05 19:11:58 -0700144 if (for_declaration) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900145 literal = StringPrintf("%s* %s", CppNameOf(method.GetType(), typenames).c_str(),
Jiyong Park75e1a742018-07-04 12:31:23 +0900146 type_name_only ? "" : kReturnVarName);
Christopher Wileyad339272015-10-05 19:11:58 -0700147 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700148 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700149 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700150 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700151 }
152
Christopher Wileyade4b452015-10-10 11:06:03 -0700153 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700154}
155
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900156unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, const AidlTypenames& typenames,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700157 bool for_interface) {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700158 uint32_t modifiers = 0;
159 if (for_interface) {
160 modifiers |= MethodDecl::IS_VIRTUAL;
161 modifiers |= MethodDecl::IS_PURE_VIRTUAL;
162 } else {
163 modifiers |= MethodDecl::IS_OVERRIDE;
164 }
165
166 return unique_ptr<Declaration>{
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900167 new MethodDecl{kBinderStatusLiteral, method.GetName(),
168 BuildArgList(typenames, method, true /* for method decl */), modifiers}};
Christopher Wiley0c732db2015-09-29 14:36:44 -0700169}
170
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900171unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const AidlTypenames&,
Jiyong Park309668e2018-07-28 16:55:44 +0900172 const Options& options, bool for_interface) {
173 CHECK(!method.IsUserDefined());
174 if (method.GetName() == kGetInterfaceVersion && options.Version()) {
175 std::ostringstream code;
176 if (for_interface) {
177 code << "virtual ";
178 }
179 code << "int32_t " << kGetInterfaceVersion << "()";
180 if (for_interface) {
181 code << " = 0;\n";
182 } else {
183 code << " override;\n";
184 }
185 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
186 }
187 return nullptr;
188}
189
Steven Morelandf3da0892018-10-05 14:52:01 -0700190std::vector<unique_ptr<Declaration>> NestInNamespaces(vector<unique_ptr<Declaration>> decls,
191 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700192 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700193 for (; it != package.crend(); ++it) {
Steven Morelandf3da0892018-10-05 14:52:01 -0700194 vector<unique_ptr<Declaration>> inner;
195 inner.emplace_back(unique_ptr<Declaration>{new CppNamespace{*it, std::move(decls)}});
196
197 decls = std::move(inner);
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700198 }
Steven Morelandf3da0892018-10-05 14:52:01 -0700199 return decls;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700200}
201
Steven Morelandf3da0892018-10-05 14:52:01 -0700202std::vector<unique_ptr<Declaration>> NestInNamespaces(unique_ptr<Declaration> decl,
203 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700204 vector<unique_ptr<Declaration>> decls;
205 decls.push_back(std::move(decl));
206 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700207}
208
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900209bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b,
210 const AidlTypenames& typenamespaces) {
211 string type = CppNameOf(a.GetType(), typenamespaces);
Casey Dahlinb0966612015-10-19 16:35:26 -0700212
213 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700214 return true;
215}
216
Steven Moreland5557f1c2018-07-02 13:50:23 -0700217string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
218 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700219 for (size_t i = 1; i < class_name.size(); ++i) {
220 if (isupper(class_name[i])) {
221 class_name.insert(i, "_");
222 ++i;
223 }
224 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700225 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700226 class_name.c_str());
227 for (char& c : ret) {
228 if (c == '.') {
229 c = '_';
230 }
231 c = toupper(c);
232 }
233 return ret;
234}
Christopher Wiley36570f42015-10-08 17:20:11 -0700235
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900236unique_ptr<Declaration> DefineClientTransaction(const AidlTypenames& typenames,
Christopher Wiley36570f42015-10-08 17:20:11 -0700237 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900238 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700239 const string i_name = ClassName(interface, ClassNames::INTERFACE);
240 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900241 unique_ptr<MethodImpl> ret{
242 new MethodImpl{kBinderStatusLiteral, bp_name, method.GetName(),
243 ArgList{BuildArgList(typenames, method, true /* for method decl */)}}};
Christopher Wiley36570f42015-10-08 17:20:11 -0700244 StatementBlock* b = ret->GetStatementBlock();
245
246 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800247 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700248 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800249 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700250
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800251 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800252 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
253 kAndroidStatusVarName,
254 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800255 // We unconditionally return a Status object.
256 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700257
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900258 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100259 b->AddLiteral(
260 StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
261 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
262 }
263
Jiyong Parkce50e262018-10-29 09:54:20 +0900264 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900265 b->AddLiteral(GenLogBeforeExecute(bp_name, method, false /* isServer */, false /* isNdk */),
266 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900267 }
268
Christopher Wiley8993cb52015-10-21 09:53:24 -0700269 // Add the name of the interface we're hoping to call.
270 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800271 kAndroidStatusVarName,
272 new MethodCall(StringPrintf("%s.writeInterfaceToken",
273 kDataVarName),
274 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800275 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700276
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700277 for (const auto& a: method.GetArguments()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700278 const string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700279
280 if (a->IsIn()) {
281 // Serialization looks roughly like:
282 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
283 // if (_aidl_ret_status != ::android::OK) { goto error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900284 const string& method = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Normanee8674f2019-09-20 16:07:00 -0700285 b->AddStatement(
286 new Assignment(kAndroidStatusVarName,
287 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
288 ParcelWriteCastOf(a->GetType(), typenames, var_name))));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700289 b->AddStatement(GotoErrorOnBadStatus());
290 } else if (a->IsOut() && a->GetType().IsArray()) {
291 // Special case, the length of the out array is written into the parcel.
292 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
293 // if (_aidl_ret_status != ::android::OK) { goto error; }
294 b->AddStatement(new Assignment(
295 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700296 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName), var_name)));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700297 b->AddStatement(GotoErrorOnBadStatus());
298 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700299 }
300
301 // Invoke the transaction on the remote binder and confirm status.
Jeongik Chab5d962f2018-11-17 09:12:28 +0900302 string transaction_code = GetTransactionIdFor(method);
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700303
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800304 vector<string> args = {transaction_code, kDataVarName,
305 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700306
Steven Morelandacd53472018-12-14 10:17:26 -0800307 if (method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800308 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700309 }
310
Christopher Wiley36570f42015-10-08 17:20:11 -0700311 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800312 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700313 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700314 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900315
316 // If the method is not implemented in the remote side, try to call the
317 // default implementation, if provided.
318 vector<string> arg_names;
319 for (const auto& a : method.GetArguments()) {
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900320 if (IsNonCopyableType(a->GetType(), typenames)) {
321 arg_names.emplace_back(StringPrintf("std::move(%s)", a->GetName().c_str()));
322 } else {
323 arg_names.emplace_back(a->GetName());
324 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900325 }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900326 if (method.GetType().GetName() != "void") {
Jiyong Park75e1a742018-07-04 12:31:23 +0900327 arg_names.emplace_back(kReturnVarName);
328 }
329 b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && "
330 "%s::getDefaultImpl())) {\n"
331 " return %s::getDefaultImpl()->%s(%s);\n"
332 "}\n",
333 i_name.c_str(), i_name.c_str(), method.GetName().c_str(),
334 Join(arg_names, ", ").c_str()),
335 false /* no semicolon */);
336
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800337 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700338
Steven Morelandacd53472018-12-14 10:17:26 -0800339 if (!method.IsOneway()) {
Christopher Wiley1227d612015-10-26 16:59:20 -0700340 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800341 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
342 // if (_aidl_ret_status != ::android::OK) { goto error; }
343 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800344 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800345 kAndroidStatusVarName,
346 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800347 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700348 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800349 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700350 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800351 exception_check->OnTrue()->AddLiteral(
352 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700353 }
354
355 // Type checking should guarantee that nothing below emits code until "return
356 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700357
Christopher Wiley36570f42015-10-08 17:20:11 -0700358 // If the method is expected to return something, read it first by convention.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900359 if (method.GetType().GetName() != "void") {
360 const string& method_call = ParcelReadMethodOf(method.GetType(), typenames);
Christopher Wiley36570f42015-10-08 17:20:11 -0700361 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800362 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700363 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method_call.c_str()),
364 ParcelReadCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800365 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700366 }
367
368 for (const AidlArgument* a : method.GetOutArguments()) {
369 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800370 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
371 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900372 string method = ParcelReadMethodOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700373
Daniel Norman85aed542019-08-21 12:01:14 -0700374 b->AddStatement(
375 new Assignment(kAndroidStatusVarName,
376 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method.c_str()),
377 ParcelReadCastOf(a->GetType(), typenames, a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800378 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700379 }
380
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800381 // If we've gotten to here, one of two things is true:
382 // 1) We've read some bad status_t
383 // 2) We've only read status_t == OK and there was no exception in the
384 // response.
385 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800386 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800387 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800388 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
389 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100390
Jiyong Parkce50e262018-10-29 09:54:20 +0900391 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900392 b->AddLiteral(GenLogAfterExecute(bp_name, interface, method, kStatusVarName, kReturnVarName,
393 false /* isServer */, false /* isNdk */),
Jeongik Cha46375122018-12-21 18:41:21 +0900394 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900395 }
396
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800397 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700398
399 return unique_ptr<Declaration>(ret.release());
400}
401
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900402unique_ptr<Declaration> DefineClientMetaTransaction(const AidlTypenames& /* typenames */,
Jiyong Park309668e2018-07-28 16:55:44 +0900403 const AidlInterface& interface,
404 const AidlMethod& method,
405 const Options& options) {
406 CHECK(!method.IsUserDefined());
407 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
408 const string iface = ClassName(interface, ClassNames::INTERFACE);
409 const string proxy = ClassName(interface, ClassNames::CLIENT);
410 // Note: race condition can happen here, but no locking is required
411 // because 1) writing an interger is atomic and 2) this transaction
412 // will always return the same value, i.e., competing threads will
413 // give write the same value to cached_version_.
414 std::ostringstream code;
415 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900416 << " if (cached_version_ == -1) {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900417 << " ::android::Parcel data;\n"
418 << " ::android::Parcel reply;\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900419 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
Jeongik Chab5d962f2018-11-17 09:12:28 +0900420 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
421 << ", data, &reply);\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900422 << " if (err == ::android::OK) {\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900423 << " ::android::binder::Status _aidl_status;\n"
424 << " err = _aidl_status.readFromParcel(reply);\n"
425 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
426 << " cached_version_ = reply.readInt32();\n"
427 << " }\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900428 << " }\n"
429 << " }\n"
430 << " return cached_version_;\n"
431 << "}\n";
432 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
433 }
434 return nullptr;
435}
436
Christopher Wiley36570f42015-10-08 17:20:11 -0700437} // namespace
438
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900439unique_ptr<Document> BuildClientSource(const AidlTypenames& typenames,
440 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700441 vector<string> include_list = {
442 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900443 kParcelHeader,
444 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700445 };
Jiyong Parkce50e262018-10-29 09:54:20 +0900446 if (options.GenLog()) {
447 include_list.emplace_back("chrono");
448 include_list.emplace_back("functional");
449 include_list.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900450 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700451 vector<unique_ptr<Declaration>> file_decls;
452
453 // The constructor just passes the IBinder instance up to the super
454 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700455 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700456 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700457 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800458 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
459 kImplVarName)},
460 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700461
Jiyong Parkce50e262018-10-29 09:54:20 +0900462 if (options.GenLog()) {
463 string code;
464 ClassName(interface, ClassNames::CLIENT);
465 CodeWriterPtr writer = CodeWriter::ForString(&code);
466 (*writer) << "std::function<void(const Json::Value&)> "
467 << ClassName(interface, ClassNames::CLIENT) << "::logFunc;\n";
468 writer->Close();
469 file_decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
470 }
471
Christopher Wiley36570f42015-10-08 17:20:11 -0700472 // Clients define a method per transaction.
473 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900474 unique_ptr<Declaration> m;
475 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900476 m = DefineClientTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900477 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900478 m = DefineClientMetaTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900479 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700480 if (!m) { return nullptr; }
481 file_decls.push_back(std::move(m));
482 }
483 return unique_ptr<Document>{new CppSource{
484 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700485 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700486}
487
Christopher Wileyad339272015-10-05 19:11:58 -0700488namespace {
489
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900490bool HandleServerTransaction(const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900491 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700492 // Declare all the parameters now. In the common case, we expect no errors
493 // in serialization.
494 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900495 if (!DeclareLocalVariable(*a, b, typenames)) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700496 return false;
497 }
Christopher Wileyad339272015-10-05 19:11:58 -0700498 }
499
500 // Declare a variable to hold the return value.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900501 if (method.GetType().GetName() != "void") {
502 string type = CppNameOf(method.GetType(), typenames);
503 b->AddLiteral(StringPrintf("%s %s", type.c_str(), kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700504 }
505
Christopher Wiley8993cb52015-10-21 09:53:24 -0700506 // Check that the client is calling the correct interface.
507 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800508 new MethodCall(StringPrintf("%s.checkInterface",
509 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700510 true /* invert the check */);
511 b->AddStatement(interface_check);
512 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800513 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700514 interface_check->OnTrue()->AddLiteral("break");
515
Christopher Wileyad339272015-10-05 19:11:58 -0700516 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700517 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700518 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800519 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
520 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Normanee8674f2019-09-20 16:07:00 -0700521 const string& var_name = "&" + BuildVarName(*a);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700522 if (a->IsIn()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700523 const string& readMethod = ParcelReadMethodOf(a->GetType(), typenames);
524 b->AddStatement(
525 new Assignment{kAndroidStatusVarName,
Daniel Normanee8674f2019-09-20 16:07:00 -0700526 new MethodCall{string(kDataVarName) + "." + readMethod,
527 ParcelReadCastOf(a->GetType(), typenames, var_name)}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700528 b->AddStatement(BreakOnStatusNotOk());
529 } else if (a->IsOut() && a->GetType().IsArray()) {
530 // Special case, the length of the out array is written into the parcel.
531 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
532 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Norman85aed542019-08-21 12:01:14 -0700533 b->AddStatement(
534 new Assignment{kAndroidStatusVarName,
535 new MethodCall{string(kDataVarName) + ".resizeOutVector", var_name}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700536 b->AddStatement(BreakOnStatusNotOk());
537 }
Christopher Wileyad339272015-10-05 19:11:58 -0700538 }
539
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900540 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100541 b->AddStatement(new Statement(new MethodCall("atrace_begin",
542 ArgList{{"ATRACE_TAG_AIDL",
543 StringPrintf("\"%s::%s::cppServer\"",
544 interface.GetName().c_str(),
545 method.GetName().c_str())}})));
546 }
Jeongik Cha46375122018-12-21 18:41:21 +0900547 const string bn_name = ClassName(interface, ClassNames::SERVER);
548 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900549 b->AddLiteral(GenLogBeforeExecute(bn_name, method, true /* isServer */, false /* isNdk */),
550 false);
Jeongik Cha46375122018-12-21 18:41:21 +0900551 }
Christopher Wileyad339272015-10-05 19:11:58 -0700552 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800553 vector<unique_ptr<AstNode>> status_args;
554 status_args.emplace_back(new MethodCall(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900555 method.GetName(), BuildArgList(typenames, method, false /* not for method decl */)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800556 b->AddStatement(new Statement(new MethodCall(
557 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
558 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700559
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900560 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100561 b->AddStatement(new Statement(new MethodCall("atrace_end",
562 "ATRACE_TAG_AIDL")));
563 }
564
Jeongik Chab34800b2019-03-08 14:36:58 +0900565 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900566 b->AddLiteral(GenLogAfterExecute(bn_name, interface, method, kStatusVarName, kReturnVarName,
567 true /* isServer */, false /* isNdk */),
568 false);
Jeongik Chab34800b2019-03-08 14:36:58 +0900569 }
570
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800571 // Write exceptions during transaction handling to parcel.
572 if (!method.IsOneway()) {
573 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800574 kAndroidStatusVarName,
575 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800576 b->AddStatement(BreakOnStatusNotOk());
577 IfStatement* exception_check = new IfStatement(
578 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
579 b->AddStatement(exception_check);
580 exception_check->OnTrue()->AddLiteral("break");
581 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700582
Christopher Wiley36570f42015-10-08 17:20:11 -0700583 // If we have a return value, write it first.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900584 if (method.GetType().GetName() != "void") {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700585 string writeMethod =
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900586 string(kReplyVarName) + "->" + ParcelWriteMethodOf(method.GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700587 b->AddStatement(new Assignment(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900588 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700589 new MethodCall(writeMethod,
590 ParcelWriteCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700591 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700592 }
Christopher Wileyad339272015-10-05 19:11:58 -0700593 // Write each out parameter to the reply parcel.
594 for (const AidlArgument* a : method.GetOutArguments()) {
595 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800596 // _aidl_ret_status = data.WriteInt32(out_param_name);
597 // if (_aidl_ret_status != ::android::OK) { break; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900598 const string& writeMethod = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700599 b->AddStatement(new Assignment(
600 kAndroidStatusVarName,
601 new MethodCall(string(kReplyVarName) + "->" + writeMethod,
602 ParcelWriteCastOf(a->GetType(), typenames, BuildVarName(*a)))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700603 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700604 }
605
606 return true;
607}
608
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900609bool HandleServerMetaTransaction(const AidlTypenames&, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900610 const AidlMethod& method, const Options& options,
611 StatementBlock* b) {
612 CHECK(!method.IsUserDefined());
613
614 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
615 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900616 code << "_aidl_data.checkInterface(this);\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900617 << "_aidl_reply->writeNoException();\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900618 << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
Jiyong Park309668e2018-07-28 16:55:44 +0900619 << "::VERSION)";
620 b->AddLiteral(code.str());
621 return true;
622 }
623 return false;
624}
625
Christopher Wileyad339272015-10-05 19:11:58 -0700626} // namespace
627
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900628unique_ptr<Document> BuildServerSource(const AidlTypenames& typenames,
629 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700630 const string bn_name = ClassName(interface, ClassNames::SERVER);
631 vector<string> include_list{
632 HeaderFile(interface, ClassNames::SERVER, false),
Steven Morelanda57d0a62019-07-30 09:41:14 -0700633 kParcelHeader,
634 kStabilityHeader,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700635 };
Jeongik Cha46375122018-12-21 18:41:21 +0900636 if (options.GenLog()) {
637 include_list.emplace_back("chrono");
638 include_list.emplace_back("functional");
639 include_list.emplace_back("json/value.h");
Jeongik Cha46375122018-12-21 18:41:21 +0900640 }
Steven Moreland800508d2019-07-30 10:45:31 -0700641
642 unique_ptr<ConstructorImpl> constructor{
643 new ConstructorImpl{ClassName(interface, ClassNames::SERVER), ArgList{}, {}}};
644
Steven Morelanda57d0a62019-07-30 09:41:14 -0700645 if (interface.IsVintfStability()) {
646 constructor->GetStatementBlock()->AddLiteral("::android::internal::Stability::markVintf(this)");
647 } else {
648 constructor->GetStatementBlock()->AddLiteral(
649 "::android::internal::Stability::markCompilationUnit(this)");
650 }
651
Christopher Wileyad339272015-10-05 19:11:58 -0700652 unique_ptr<MethodImpl> on_transact{new MethodImpl{
653 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800654 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
655 StringPrintf("const %s& %s", kAndroidParcelLiteral,
656 kDataVarName),
657 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
658 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700659 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700660
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800661 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700662 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800663 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
664 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700665
Christopher Wileyad339272015-10-05 19:11:58 -0700666 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800667 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700668 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700669
670 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700671 for (const auto& method : interface.GetMethods()) {
Jeongik Chab5d962f2018-11-17 09:12:28 +0900672 StatementBlock* b = s->AddCase(GetTransactionIdFor(*method));
Christopher Wileyad339272015-10-05 19:11:58 -0700673 if (!b) { return nullptr; }
674
Jiyong Park309668e2018-07-28 16:55:44 +0900675 bool success = false;
676 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900677 success = HandleServerTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900678 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900679 success = HandleServerMetaTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900680 }
681 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900682 return nullptr;
683 }
Christopher Wileyad339272015-10-05 19:11:58 -0700684 }
685
686 // The switch statement has a default case which defers to the super class.
687 // The superclass handles a few pre-defined transactions.
688 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800689 b->AddLiteral(StringPrintf(
690 "%s = ::android::BBinder::onTransact(%s, %s, "
691 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
692 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700693
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800694 // If we saw a null reference, we can map that to an appropriate exception.
695 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800696 new LiteralExpression(string(kAndroidStatusVarName) +
697 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800698 on_transact->GetStatementBlock()->AddStatement(null_check);
699 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800700 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800701 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800702 ".writeToParcel(%s)",
703 kBinderStatusLiteral, kBinderStatusLiteral,
704 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800705
Christopher Wileyad339272015-10-05 19:11:58 -0700706 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800707 on_transact->GetStatementBlock()->AddLiteral(
708 StringPrintf("return %s", kAndroidStatusVarName));
Jeongik Cha46375122018-12-21 18:41:21 +0900709 vector<unique_ptr<Declaration>> decls;
Steven Moreland800508d2019-07-30 10:45:31 -0700710 decls.push_back(std::move(constructor));
Jeongik Cha46375122018-12-21 18:41:21 +0900711 decls.push_back(std::move(on_transact));
Christopher Wileyad339272015-10-05 19:11:58 -0700712
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900713 if (options.Version() > 0) {
714 std::ostringstream code;
715 code << "int32_t " << bn_name << "::" << kGetInterfaceVersion << "() {\n"
716 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::VERSION;\n"
717 << "}\n";
718 decls.emplace_back(new LiteralDecl(code.str()));
719 }
720
Jeongik Cha46375122018-12-21 18:41:21 +0900721 if (options.GenLog()) {
722 string code;
723 ClassName(interface, ClassNames::SERVER);
724 CodeWriterPtr writer = CodeWriter::ForString(&code);
725 (*writer) << "std::function<void(const Json::Value&)> "
726 << ClassName(interface, ClassNames::SERVER) << "::logFunc;\n";
727 writer->Close();
728 decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
729 }
730 return unique_ptr<Document>{
731 new CppSource{include_list, NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700732}
733
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900734unique_ptr<Document> BuildInterfaceSource(const AidlTypenames& typenames,
Jiyong Park309668e2018-07-28 16:55:44 +0900735 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700736 vector<string> include_list{
Jiyong Park5b7e5322019-04-03 20:05:01 +0900737 HeaderFile(interface, ClassNames::RAW, false),
Christopher Wiley054afbd2015-10-16 17:08:43 -0700738 HeaderFile(interface, ClassNames::CLIENT, false),
739 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700740
Christopher Wiley054afbd2015-10-16 17:08:43 -0700741 string fq_name = ClassName(interface, ClassNames::INTERFACE);
742 if (!interface.GetPackage().empty()) {
743 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700744 }
745
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700746 vector<unique_ptr<Declaration>> decls;
747
Christopher Wiley11a9d792016-02-24 17:20:33 -0800748 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Ivan Lozanob3f4d622019-11-25 09:30:02 -0800749 "DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE",
750 ArgList{vector<string>{ClassName(interface, ClassNames::BASE), '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700751 decls.push_back(std::move(meta_if));
752
Steven Moreland693640b2018-07-19 13:46:27 -0700753 for (const auto& constant : interface.GetConstantDeclarations()) {
754 const AidlConstantValue& value = constant->GetValue();
755 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
756
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900757 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700758 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
759 ClassName(interface, ClassNames::INTERFACE),
760 constant->GetName(), {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700761 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700762 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700763 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700764 getter->GetStatementBlock()->AddLiteral("return value");
765 decls.push_back(std::move(getter));
766 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700767
Jiyong Park75e1a742018-07-04 12:31:23 +0900768 // Implement the default impl class.
769 // onAsBinder returns nullptr as this interface is not associated with a
770 // real binder.
771 const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL));
772 decls.emplace_back(
773 new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n"
774 " return nullptr;\n"
775 "}\n",
776 default_impl.c_str())));
777 // Each interface method by default returns UNKNOWN_TRANSACTION with is
778 // the same status that is returned by transact() when the method is
779 // not implemented in the server side. In other words, these default
780 // methods do nothing; they only exist to aid making a real default
781 // impl class without having to override all methods in an interface.
782 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900783 if (method->IsUserDefined()) {
784 std::ostringstream code;
785 code << "::android::binder::Status " << default_impl << "::" << method->GetName()
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900786 << BuildArgList(typenames, *method, true, true).ToString() << " {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900787 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
788 << "}\n";
789 decls.emplace_back(new LiteralDecl(code.str()));
790 } else {
791 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
792 std::ostringstream code;
793 code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n"
794 << " return 0;\n"
795 << "}\n";
796 decls.emplace_back(new LiteralDecl(code.str()));
797 }
798 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900799 }
800
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700801 return unique_ptr<Document>{new CppSource{
802 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700803 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700804}
805
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900806unique_ptr<Document> BuildClientHeader(const AidlTypenames& typenames,
807 const AidlInterface& interface, const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700808 const string i_name = ClassName(interface, ClassNames::INTERFACE);
809 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700810
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900811 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900812 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900813
Christopher Wileyb23149d2015-10-14 13:52:21 -0700814 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
815 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800816 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
817 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700818 ConstructorDecl::IS_EXPLICIT
819 }};
820 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
821 "~" + bp_name,
822 ArgList{},
823 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700824
Christopher Wileyf944e792015-09-29 10:00:46 -0700825 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700826 publics.push_back(std::move(constructor));
827 publics.push_back(std::move(destructor));
828
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700829 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900830 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900831 publics.push_back(BuildMethodDecl(*method, typenames, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900832 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900833 publics.push_back(BuildMetaMethodDecl(*method, typenames, options, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900834 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700835 }
836
Jiyong Parkce50e262018-10-29 09:54:20 +0900837 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900838 includes.emplace_back("chrono"); // for std::chrono::steady_clock
839 includes.emplace_back("functional"); // for std::function
840 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900841 publics.emplace_back(
842 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
843 }
844
Jiyong Park309668e2018-07-28 16:55:44 +0900845 vector<unique_ptr<Declaration>> privates;
846
847 if (options.Version() > 0) {
848 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
849 }
850
851 unique_ptr<ClassDecl> bp_class{new ClassDecl{
852 bp_name,
853 "::android::BpInterface<" + i_name + ">",
854 std::move(publics),
855 std::move(privates),
856 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700857
Jiyong Parkce50e262018-10-29 09:54:20 +0900858 return unique_ptr<Document>{
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900859 new CppHeader{BuildHeaderGuard(interface, ClassNames::CLIENT), includes,
Jiyong Parkce50e262018-10-29 09:54:20 +0900860 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700861}
862
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900863unique_ptr<Document> BuildServerHeader(const AidlTypenames& /* typenames */,
Jeongik Cha46375122018-12-21 18:41:21 +0900864 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700865 const string i_name = ClassName(interface, ClassNames::INTERFACE);
866 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700867
Steven Moreland800508d2019-07-30 10:45:31 -0700868 unique_ptr<ConstructorDecl> constructor{
869 new ConstructorDecl{bn_name, ArgList{}, ConstructorDecl::IS_EXPLICIT}};
870
Christopher Wileyfd51d602015-10-14 13:04:48 -0700871 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700872 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800873 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
874 StringPrintf("const %s& %s", kAndroidParcelLiteral,
875 kDataVarName),
876 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900877 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700878 MethodDecl::IS_OVERRIDE
879 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900880 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700881
Jeongik Cha46375122018-12-21 18:41:21 +0900882 vector<unique_ptr<Declaration>> publics;
Steven Moreland800508d2019-07-30 10:45:31 -0700883 publics.push_back(std::move(constructor));
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700884 publics.push_back(std::move(on_transact));
885
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900886 if (options.Version() > 0) {
887 std::ostringstream code;
888 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
889 publics.emplace_back(new LiteralDecl(code.str()));
890 }
891
Jeongik Cha46375122018-12-21 18:41:21 +0900892 if (options.GenLog()) {
893 includes.emplace_back("chrono"); // for std::chrono::steady_clock
894 includes.emplace_back("functional"); // for std::function
895 includes.emplace_back("json/value.h");
896 publics.emplace_back(
897 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
898 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700899 unique_ptr<ClassDecl> bn_class{
900 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800901 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700902 std::move(publics),
903 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700904 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700905
Jeongik Cha46375122018-12-21 18:41:21 +0900906 return unique_ptr<Document>{
907 new CppHeader{BuildHeaderGuard(interface, ClassNames::SERVER), includes,
908 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700909}
910
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900911unique_ptr<Document> BuildInterfaceHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900912 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900913 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700914
915 for (const auto& method : interface.GetMethods()) {
916 for (const auto& argument : method->GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900917 AddHeaders(argument->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700918 }
919
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900920 AddHeaders(method->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700921 }
922
Jiyong Park75e1a742018-07-04 12:31:23 +0900923 const string i_name = ClassName(interface, ClassNames::INTERFACE);
924 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800925 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700926 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700927 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700928
Jiyong Park309668e2018-07-28 16:55:44 +0900929 if (options.Version() > 0) {
930 std::ostringstream code;
931 code << "const int32_t VERSION = " << options.Version() << ";\n";
932
933 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
934 }
935
Steven Moreland693640b2018-07-19 13:46:27 -0700936 std::vector<std::unique_ptr<Declaration>> string_constants;
Daniel Norman85aed542019-08-21 12:01:14 -0700937 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t", false}};
Steven Moreland693640b2018-07-19 13:46:27 -0700938 for (const auto& constant : interface.GetConstantDeclarations()) {
939 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800940
Steven Moreland693640b2018-07-19 13:46:27 -0700941 switch (value.GetType()) {
942 case AidlConstantValue::Type::STRING: {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900943 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700944 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
945 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700946 string_constants.push_back(std::move(getter));
947 break;
948 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700949 case AidlConstantValue::Type::BOOLEAN: // fall-through
950 case AidlConstantValue::Type::INT8: // fall-through
951 case AidlConstantValue::Type::INT32: {
Steven Moreland860b1942018-08-16 14:59:28 -0700952 int_constant_enum->AddValue(constant->GetName(),
953 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700954 break;
955 }
956 default: {
957 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
958 }
959 }
960 }
961 if (int_constant_enum->HasValues()) {
962 if_class->AddPublic(std::move(int_constant_enum));
963 }
964 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700965 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700966
967 for (auto& string_constant : string_constants) {
968 if_class->AddPublic(std::move(string_constant));
969 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700970 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100971
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900972 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100973 includes.insert(kTraceHeader);
974 }
975
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700976 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700977 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900978 if (method->IsUserDefined()) {
979 // Each method gets an enum entry and pure virtual declaration.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900980 if_class->AddPublic(BuildMethodDecl(*method, typenames, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900981 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900982 if_class->AddPublic(BuildMetaMethodDecl(*method, typenames, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900983 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700984 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700985 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700986
Jiyong Park75e1a742018-07-04 12:31:23 +0900987 vector<unique_ptr<Declaration>> decls;
988 decls.emplace_back(std::move(if_class));
989
990 // Base class for the default implementation.
991 vector<string> method_decls;
992 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900993 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900994 method_decls.emplace_back(BuildMethodDecl(*method, typenames, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900995 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900996 method_decls.emplace_back(
997 BuildMetaMethodDecl(*method, typenames, options, false)->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900998 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900999 }
Jiyong Park309668e2018-07-28 16:55:44 +09001000
Jiyong Park75e1a742018-07-04 12:31:23 +09001001 decls.emplace_back(new LiteralDecl(
1002 android::base::StringPrintf("class %s : public %s {\n"
1003 "public:\n"
1004 " ::android::IBinder* onAsBinder() override;\n"
1005 " %s\n"
1006 "};\n",
1007 ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(),
1008 i_name.c_str(), Join(method_decls, " ").c_str())));
1009
1010 return unique_ptr<Document>{
1011 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
1012 vector<string>(includes.begin(), includes.end()),
1013 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001014}
1015
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001016std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001017 const AidlStructuredParcelable& parcel,
1018 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001019 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
1020
1021 set<string> includes = {kStatusHeader, kParcelHeader};
Jeongik Cha10f72b72019-09-04 21:28:13 +09001022 includes.insert("tuple");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001023 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001024 AddHeaders(variable->GetType(), typenames, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001025 }
1026
Jeongik Cha10f72b72019-09-04 21:28:13 +09001027 set<string> operators = {"<", ">", "==", ">=", "<=", "!="};
1028 for (const auto& op : operators) {
1029 std::ostringstream operator_code;
1030 std::vector<std::string> variable_name;
1031 std::vector<std::string> rhs_variable_name;
1032 for (const auto& variable : parcel.GetFields()) {
1033 variable_name.push_back(variable->GetName());
1034 rhs_variable_name.push_back("rhs." + variable->GetName());
1035 }
1036
1037 operator_code << "inline bool operator" << op << "(const " << parcel.GetName()
1038 << "& rhs) const {\n"
1039 << " return "
1040 << "std::tie(" << Join(variable_name, ", ") << ")" << op << "std::tie("
1041 << Join(rhs_variable_name, ", ") << ")"
1042 << ";\n"
1043 << "}\n";
1044
1045 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
1046 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001047 for (const auto& variable : parcel.GetFields()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001048
Steven Moreland9ea10e32018-07-19 15:26:09 -07001049 std::ostringstream out;
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001050 std::string cppType = CppNameOf(variable->GetType(), typenames);
1051 out << cppType.c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001052 if (variable->GetDefaultValue()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001053 out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
1054 << ")";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001055 }
1056 out << ";\n";
1057
1058 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001059 }
1060
1061 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001062 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001063 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001064 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001065 unique_ptr<MethodDecl> write(new MethodDecl(
1066 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1067 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001068 parcel_class->AddPublic(std::move(write));
1069
1070 return unique_ptr<Document>{new CppHeader{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001071 BuildHeaderGuard(parcel, ClassNames::RAW), vector<string>(includes.begin(), includes.end()),
Steven Moreland5557f1c2018-07-02 13:50:23 -07001072 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1073}
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001074std::unique_ptr<Document> BuildParcelSource(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001075 const AidlStructuredParcelable& parcel,
1076 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001077 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1078 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001079 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001080 StatementBlock* read_block = read->GetStatementBlock();
1081 read_block->AddLiteral(
1082 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001083
1084 read_block->AddLiteral(
1085 "size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1086 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1087 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
1088 "size_t _aidl_parcelable_size = static_cast<size_t>(_aidl_parcelable_raw_size);\n");
1089
Steven Moreland5557f1c2018-07-02 13:50:23 -07001090 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001091 string method = ParcelReadMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001092
1093 read_block->AddStatement(new Assignment(
1094 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
Daniel Norman85aed542019-08-21 12:01:14 -07001095 ParcelReadCastOf(variable->GetType(), typenames,
1096 "&" + variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001097 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001098 read_block->AddLiteral(StringPrintf(
1099 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1100 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1101 " return %s;\n"
1102 "}",
1103 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001104 }
1105 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1106
Steven Morelandce39c532018-07-11 16:59:50 -07001107 unique_ptr<MethodImpl> write{
1108 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1109 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001110 StatementBlock* write_block = write->GetStatementBlock();
1111 write_block->AddLiteral(
1112 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001113
1114 write_block->AddLiteral(
1115 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1116 "_aidl_parcel->writeInt32(0);");
1117
Steven Moreland5557f1c2018-07-02 13:50:23 -07001118 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001119 string method = ParcelWriteMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001120 write_block->AddStatement(new Assignment(
Daniel Norman85aed542019-08-21 12:01:14 -07001121 kAndroidStatusVarName,
1122 new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1123 ParcelWriteCastOf(variable->GetType(), typenames, variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001124 write_block->AddStatement(ReturnOnStatusNotOk());
1125 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001126
1127 write_block->AddLiteral(
1128 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1129 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1130 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1131 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001132 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1133
1134 vector<unique_ptr<Declaration>> file_decls;
1135 file_decls.push_back(std::move(read));
1136 file_decls.push_back(std::move(write));
1137
1138 set<string> includes = {};
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001139 AddHeaders(parcel, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001140
1141 return unique_ptr<Document>{
1142 new CppSource{vector<string>(includes.begin(), includes.end()),
1143 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1144}
1145
Daniel Norman0c1bd362019-11-12 23:05:31 -08001146std::string GenerateEnumToString(const AidlTypenames& typenames,
1147 const AidlEnumDeclaration& enum_decl) {
1148 std::ostringstream code;
1149 code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n";
1150 code << " switch(val) {\n";
1151 std::set<std::string> unique_cases;
1152 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1153 std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator);
1154 // Only add a case if its value has not yet been used in the switch
1155 // statement. C++ does not allow multiple cases with the same value, but
1156 // enums does allow this. In this scenario, the first declared
1157 // enumerator with the given value is printed.
1158 if (unique_cases.count(c) == 0) {
1159 unique_cases.insert(c);
1160 code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n";
1161 code << " return \"" << enumerator->GetName() << "\";\n";
1162 }
1163 }
1164 code << " default:\n";
1165 code << " return std::to_string(static_cast<"
1166 << CppNameOf(enum_decl.GetBackingType(), typenames) << ">(val));\n";
1167 code << " }\n";
1168 code << "}\n";
1169 return code.str();
1170}
1171
Daniel Norman85aed542019-08-21 12:01:14 -07001172std::unique_ptr<Document> BuildEnumHeader(const AidlTypenames& typenames,
1173 const AidlEnumDeclaration& enum_decl) {
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001174 std::unique_ptr<Enum> generated_enum{
Daniel Norman85aed542019-08-21 12:01:14 -07001175 new Enum{enum_decl.GetName(), CppNameOf(enum_decl.GetBackingType(), typenames), true}};
1176 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1177 generated_enum->AddValue(
1178 enumerator->GetName(),
1179 enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator));
1180 }
1181
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001182 std::set<std::string> includes = {
1183 "array",
1184 "binder/Enums.h",
1185 "string",
1186 };
Daniel Norman85aed542019-08-21 12:01:14 -07001187 AddHeaders(enum_decl.GetBackingType(), typenames, includes);
1188
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001189 std::vector<std::unique_ptr<Declaration>> decls1;
1190 decls1.push_back(std::move(generated_enum));
1191 decls1.push_back(std::make_unique<LiteralDecl>(GenerateEnumToString(typenames, enum_decl)));
1192
1193 std::vector<std::unique_ptr<Declaration>> decls2;
1194 decls2.push_back(std::make_unique<LiteralDecl>(GenerateEnumValues(enum_decl, {""})));
Daniel Norman0c1bd362019-11-12 23:05:31 -08001195
Daniel Norman85aed542019-08-21 12:01:14 -07001196 return unique_ptr<Document>{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001197 new CppHeader{BuildHeaderGuard(enum_decl, ClassNames::RAW),
Daniel Norman85aed542019-08-21 12:01:14 -07001198 vector<string>(includes.begin(), includes.end()),
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001199 Append(NestInNamespaces(std::move(decls1), enum_decl.GetSplitPackage()),
1200 NestInNamespaces(std::move(decls2), {"android", "internal"}))}};
Daniel Norman85aed542019-08-21 12:01:14 -07001201}
1202
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001203bool WriteHeader(const Options& options, const AidlTypenames& typenames,
1204 const AidlInterface& interface, const IoDelegate& io_delegate,
1205 ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001206 unique_ptr<Document> header;
1207 switch (header_type) {
1208 case ClassNames::INTERFACE:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001209 header = BuildInterfaceHeader(typenames, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001210 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001211 break;
1212 case ClassNames::CLIENT:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001213 header = BuildClientHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001214 break;
1215 case ClassNames::SERVER:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001216 header = BuildServerHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001217 break;
1218 default:
1219 LOG(FATAL) << "aidl internal error";
1220 }
1221 if (!header) {
1222 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001223 return false;
1224 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001225
Jiyong Park05463732018-08-09 16:03:02 +09001226 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001227 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1228 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001229
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001230 const bool success = code_writer->Close();
1231 if (!success) {
1232 io_delegate.RemovePath(header_path);
1233 }
1234
1235 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001236}
1237
Casey Dahlina834dd42015-09-23 11:52:15 -07001238} // namespace internals
1239
1240using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001241
Jiyong Park74595c12018-07-23 15:22:50 +09001242bool GenerateCppInterface(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001243 const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Park74595c12018-07-23 15:22:50 +09001244 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001245 auto interface_src = BuildInterfaceSource(typenames, interface, options);
1246 auto client_src = BuildClientSource(typenames, interface, options);
1247 auto server_src = BuildServerSource(typenames, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001248
Christopher Wiley054afbd2015-10-16 17:08:43 -07001249 if (!interface_src || !client_src || !server_src) {
1250 return false;
1251 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001252
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001253 if (!WriteHeader(options, typenames, interface, io_delegate, ClassNames::INTERFACE) ||
1254 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::CLIENT) ||
1255 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::SERVER)) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001256 return false;
1257 }
1258
Jiyong Park74595c12018-07-23 15:22:50 +09001259 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001260 interface_src->Write(writer.get());
1261 client_src->Write(writer.get());
1262 server_src->Write(writer.get());
1263
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001264 const bool success = writer->Close();
1265 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001266 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001267 }
1268
1269 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001270}
1271
Jiyong Park74595c12018-07-23 15:22:50 +09001272bool GenerateCppParcel(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001273 const AidlTypenames& typenames, const AidlStructuredParcelable& parcelable,
Jiyong Park74595c12018-07-23 15:22:50 +09001274 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001275 auto header = BuildParcelHeader(typenames, parcelable, options);
1276 auto source = BuildParcelSource(typenames, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001277
1278 if (!header || !source) {
1279 return false;
1280 }
1281
Jiyong Park5b7e5322019-04-03 20:05:01 +09001282 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001283 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1284 header->Write(header_writer.get());
1285 CHECK(header_writer->Close());
1286
Steven Moreland81079f92018-07-06 16:15:53 -07001287 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001288 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001289 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1290 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1291 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001292 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001293 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1294 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1295 CHECK(bn_writer->Close());
1296
Jiyong Park74595c12018-07-23 15:22:50 +09001297 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001298 source->Write(source_writer.get());
1299 CHECK(source_writer->Close());
1300
1301 return true;
1302}
1303
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001304bool GenerateCppParcelDeclaration(const std::string& filename, const Options& options,
1305 const AidlParcelable& parcelable, const IoDelegate& io_delegate) {
1306 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1307 *source_writer
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001308 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001309 CHECK(source_writer->Close());
1310
1311 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1312 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
1313 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1314 header_writer->Write("#error TODO(b/111362593) parcelables do not have headers");
1315 CHECK(header_writer->Close());
1316 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
1317 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1318 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1319 CHECK(bp_writer->Close());
1320 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
1321 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1322 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1323 CHECK(bn_writer->Close());
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001324
1325 return true;
1326}
1327
Daniel Norman85aed542019-08-21 12:01:14 -07001328bool GenerateCppEnumDeclaration(const std::string& filename, const Options& options,
1329 const AidlTypenames& typenames,
1330 const AidlEnumDeclaration& enum_decl,
1331 const IoDelegate& io_delegate) {
1332 auto header = BuildEnumHeader(typenames, enum_decl);
1333 if (!header) return false;
1334
1335 const string header_path = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::RAW);
1336 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1337 header->Write(header_writer.get());
1338 CHECK(header_writer->Close());
1339
1340 // TODO(b/111362593): no unnecessary files just to have consistent output with interfaces
1341 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1342 *source_writer
1343 << "// This file is intentionally left blank as placeholder for enum declaration.\n";
1344 CHECK(source_writer->Close());
1345 const string bp_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::CLIENT);
1346 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1347 bp_writer->Write("#error TODO(b/111362593) enums do not have bp classes");
1348 CHECK(bp_writer->Close());
1349 const string bn_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::SERVER);
1350 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1351 bn_writer->Write("#error TODO(b/111362593) enums do not have bn classes");
1352 CHECK(bn_writer->Close());
1353
1354 return true;
1355}
1356
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001357bool GenerateCpp(const string& output_file, const Options& options, const AidlTypenames& typenames,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001358 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1359 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1360 if (parcelable != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001361 return GenerateCppParcel(output_file, options, typenames, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001362 }
1363
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001364 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1365 if (parcelable_decl != nullptr) {
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001366 return GenerateCppParcelDeclaration(output_file, options, *parcelable_decl, io_delegate);
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001367 }
1368
Daniel Norman85aed542019-08-21 12:01:14 -07001369 const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration();
1370 if (enum_decl != nullptr) {
1371 return GenerateCppEnumDeclaration(output_file, options, typenames, *enum_decl, io_delegate);
1372 }
1373
Steven Moreland5557f1c2018-07-02 13:50:23 -07001374 const AidlInterface* interface = defined_type.AsInterface();
1375 if (interface != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001376 return GenerateCppInterface(output_file, options, typenames, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001377 }
1378
1379 CHECK(false) << "Unrecognized type sent for cpp generation.";
1380 return false;
1381}
1382
Christopher Wileyf944e792015-09-29 10:00:46 -07001383} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001384} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001385} // namespace android