blob: b9b2d1279751af8b5bdd22fdc4c7bb63ae686952 [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) {
Jooyung Han6991d922020-01-30 18:13:55 +0900145 literal = CppNameOf(method.GetType(), typenames) + "*";
146 if (!type_name_only) {
147 literal += " " + string(kReturnVarName);
148 }
Christopher Wileyad339272015-10-05 19:11:58 -0700149 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700150 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700151 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700152 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700153 }
154
Christopher Wileyade4b452015-10-10 11:06:03 -0700155 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700156}
157
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900158unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, const AidlTypenames& typenames,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700159 bool for_interface) {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700160 uint32_t modifiers = 0;
161 if (for_interface) {
162 modifiers |= MethodDecl::IS_VIRTUAL;
163 modifiers |= MethodDecl::IS_PURE_VIRTUAL;
164 } else {
165 modifiers |= MethodDecl::IS_OVERRIDE;
166 }
167
168 return unique_ptr<Declaration>{
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900169 new MethodDecl{kBinderStatusLiteral, method.GetName(),
170 BuildArgList(typenames, method, true /* for method decl */), modifiers}};
Christopher Wiley0c732db2015-09-29 14:36:44 -0700171}
172
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900173unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const AidlTypenames&,
Jiyong Park309668e2018-07-28 16:55:44 +0900174 const Options& options, bool for_interface) {
175 CHECK(!method.IsUserDefined());
176 if (method.GetName() == kGetInterfaceVersion && options.Version()) {
177 std::ostringstream code;
178 if (for_interface) {
179 code << "virtual ";
180 }
181 code << "int32_t " << kGetInterfaceVersion << "()";
182 if (for_interface) {
183 code << " = 0;\n";
184 } else {
185 code << " override;\n";
186 }
187 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
188 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900189 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
190 std::ostringstream code;
191 if (for_interface) {
192 code << "virtual ";
193 }
194 code << "std::string " << kGetInterfaceHash << "()";
195 if (for_interface) {
196 code << " = 0;\n";
197 } else {
198 code << " override;\n";
199 }
200 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
201 }
Jiyong Park309668e2018-07-28 16:55:44 +0900202 return nullptr;
203}
204
Steven Morelandf3da0892018-10-05 14:52:01 -0700205std::vector<unique_ptr<Declaration>> NestInNamespaces(vector<unique_ptr<Declaration>> decls,
206 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700207 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700208 for (; it != package.crend(); ++it) {
Steven Morelandf3da0892018-10-05 14:52:01 -0700209 vector<unique_ptr<Declaration>> inner;
210 inner.emplace_back(unique_ptr<Declaration>{new CppNamespace{*it, std::move(decls)}});
211
212 decls = std::move(inner);
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700213 }
Steven Morelandf3da0892018-10-05 14:52:01 -0700214 return decls;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700215}
216
Steven Morelandf3da0892018-10-05 14:52:01 -0700217std::vector<unique_ptr<Declaration>> NestInNamespaces(unique_ptr<Declaration> decl,
218 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700219 vector<unique_ptr<Declaration>> decls;
220 decls.push_back(std::move(decl));
221 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700222}
223
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900224bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b,
225 const AidlTypenames& typenamespaces) {
226 string type = CppNameOf(a.GetType(), typenamespaces);
Casey Dahlinb0966612015-10-19 16:35:26 -0700227
228 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700229 return true;
230}
231
Steven Moreland5557f1c2018-07-02 13:50:23 -0700232string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
233 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700234 for (size_t i = 1; i < class_name.size(); ++i) {
235 if (isupper(class_name[i])) {
236 class_name.insert(i, "_");
237 ++i;
238 }
239 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700240 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700241 class_name.c_str());
242 for (char& c : ret) {
243 if (c == '.') {
244 c = '_';
245 }
246 c = toupper(c);
247 }
248 return ret;
249}
Christopher Wiley36570f42015-10-08 17:20:11 -0700250
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900251unique_ptr<Declaration> DefineClientTransaction(const AidlTypenames& typenames,
Christopher Wiley36570f42015-10-08 17:20:11 -0700252 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900253 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700254 const string i_name = ClassName(interface, ClassNames::INTERFACE);
255 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900256 unique_ptr<MethodImpl> ret{
257 new MethodImpl{kBinderStatusLiteral, bp_name, method.GetName(),
258 ArgList{BuildArgList(typenames, method, true /* for method decl */)}}};
Christopher Wiley36570f42015-10-08 17:20:11 -0700259 StatementBlock* b = ret->GetStatementBlock();
260
261 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800262 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700263 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800264 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700265
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800266 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800267 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
268 kAndroidStatusVarName,
269 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800270 // We unconditionally return a Status object.
271 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700272
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900273 if (options.GenTraces()) {
Devin Mooreff1e4812020-04-16 11:22:55 -0700274 b->AddLiteral(StringPrintf("::android::ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
275 kTraceVarName, interface.GetName().c_str(),
276 method.GetName().c_str()));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100277 }
278
Jiyong Parkce50e262018-10-29 09:54:20 +0900279 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900280 b->AddLiteral(GenLogBeforeExecute(bp_name, method, false /* isServer */, false /* isNdk */),
281 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900282 }
283
Christopher Wiley8993cb52015-10-21 09:53:24 -0700284 // Add the name of the interface we're hoping to call.
285 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800286 kAndroidStatusVarName,
287 new MethodCall(StringPrintf("%s.writeInterfaceToken",
288 kDataVarName),
289 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800290 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700291
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700292 for (const auto& a: method.GetArguments()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700293 const string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700294
295 if (a->IsIn()) {
296 // Serialization looks roughly like:
297 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
298 // if (_aidl_ret_status != ::android::OK) { goto error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900299 const string& method = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Normanee8674f2019-09-20 16:07:00 -0700300 b->AddStatement(
301 new Assignment(kAndroidStatusVarName,
302 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
303 ParcelWriteCastOf(a->GetType(), typenames, var_name))));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700304 b->AddStatement(GotoErrorOnBadStatus());
305 } else if (a->IsOut() && a->GetType().IsArray()) {
306 // Special case, the length of the out array is written into the parcel.
307 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
308 // if (_aidl_ret_status != ::android::OK) { goto error; }
309 b->AddStatement(new Assignment(
310 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700311 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName), var_name)));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700312 b->AddStatement(GotoErrorOnBadStatus());
313 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700314 }
315
316 // Invoke the transaction on the remote binder and confirm status.
Jeongik Chab5d962f2018-11-17 09:12:28 +0900317 string transaction_code = GetTransactionIdFor(method);
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700318
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800319 vector<string> args = {transaction_code, kDataVarName,
320 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700321
Steven Morelandacd53472018-12-14 10:17:26 -0800322 if (method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800323 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700324 }
325
Christopher Wiley36570f42015-10-08 17:20:11 -0700326 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800327 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700328 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700329 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900330
331 // If the method is not implemented in the remote side, try to call the
332 // default implementation, if provided.
333 vector<string> arg_names;
334 for (const auto& a : method.GetArguments()) {
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900335 if (IsNonCopyableType(a->GetType(), typenames)) {
336 arg_names.emplace_back(StringPrintf("std::move(%s)", a->GetName().c_str()));
337 } else {
338 arg_names.emplace_back(a->GetName());
339 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900340 }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900341 if (method.GetType().GetName() != "void") {
Jiyong Park75e1a742018-07-04 12:31:23 +0900342 arg_names.emplace_back(kReturnVarName);
343 }
344 b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && "
345 "%s::getDefaultImpl())) {\n"
346 " return %s::getDefaultImpl()->%s(%s);\n"
347 "}\n",
348 i_name.c_str(), i_name.c_str(), method.GetName().c_str(),
349 Join(arg_names, ", ").c_str()),
350 false /* no semicolon */);
351
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800352 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700353
Steven Morelandacd53472018-12-14 10:17:26 -0800354 if (!method.IsOneway()) {
Christopher Wiley1227d612015-10-26 16:59:20 -0700355 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800356 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
357 // if (_aidl_ret_status != ::android::OK) { goto error; }
358 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800359 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800360 kAndroidStatusVarName,
361 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800362 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700363 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800364 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700365 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800366 exception_check->OnTrue()->AddLiteral(
367 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700368 }
369
370 // Type checking should guarantee that nothing below emits code until "return
371 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700372
Christopher Wiley36570f42015-10-08 17:20:11 -0700373 // If the method is expected to return something, read it first by convention.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900374 if (method.GetType().GetName() != "void") {
375 const string& method_call = ParcelReadMethodOf(method.GetType(), typenames);
Christopher Wiley36570f42015-10-08 17:20:11 -0700376 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800377 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700378 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method_call.c_str()),
379 ParcelReadCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800380 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700381 }
382
383 for (const AidlArgument* a : method.GetOutArguments()) {
384 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800385 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
386 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900387 string method = ParcelReadMethodOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700388
Daniel Norman85aed542019-08-21 12:01:14 -0700389 b->AddStatement(
390 new Assignment(kAndroidStatusVarName,
391 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method.c_str()),
392 ParcelReadCastOf(a->GetType(), typenames, a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800393 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700394 }
395
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800396 // If we've gotten to here, one of two things is true:
397 // 1) We've read some bad status_t
398 // 2) We've only read status_t == OK and there was no exception in the
399 // response.
400 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800401 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800402 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800403 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
404 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100405
Jiyong Parkce50e262018-10-29 09:54:20 +0900406 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900407 b->AddLiteral(GenLogAfterExecute(bp_name, interface, method, kStatusVarName, kReturnVarName,
408 false /* isServer */, false /* isNdk */),
Jeongik Cha46375122018-12-21 18:41:21 +0900409 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900410 }
411
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800412 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700413
414 return unique_ptr<Declaration>(ret.release());
415}
416
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900417unique_ptr<Declaration> DefineClientMetaTransaction(const AidlTypenames& /* typenames */,
Jiyong Park309668e2018-07-28 16:55:44 +0900418 const AidlInterface& interface,
419 const AidlMethod& method,
420 const Options& options) {
421 CHECK(!method.IsUserDefined());
422 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
423 const string iface = ClassName(interface, ClassNames::INTERFACE);
424 const string proxy = ClassName(interface, ClassNames::CLIENT);
425 // Note: race condition can happen here, but no locking is required
426 // because 1) writing an interger is atomic and 2) this transaction
427 // will always return the same value, i.e., competing threads will
428 // give write the same value to cached_version_.
429 std::ostringstream code;
430 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900431 << " if (cached_version_ == -1) {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900432 << " ::android::Parcel data;\n"
433 << " ::android::Parcel reply;\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900434 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
Jeongik Chab5d962f2018-11-17 09:12:28 +0900435 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
436 << ", data, &reply);\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900437 << " if (err == ::android::OK) {\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900438 << " ::android::binder::Status _aidl_status;\n"
439 << " err = _aidl_status.readFromParcel(reply);\n"
440 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
441 << " cached_version_ = reply.readInt32();\n"
442 << " }\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900443 << " }\n"
444 << " }\n"
445 << " return cached_version_;\n"
446 << "}\n";
447 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
448 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900449 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
450 const string iface = ClassName(interface, ClassNames::INTERFACE);
451 const string proxy = ClassName(interface, ClassNames::CLIENT);
452 std::ostringstream code;
453 code << "std::string " << proxy << "::" << kGetInterfaceHash << "() {\n"
454 << " std::lock_guard<std::mutex> lockGuard(cached_hash_mutex_);\n"
455 << " if (cached_hash_ == \"-1\") {\n"
456 << " ::android::Parcel data;\n"
457 << " ::android::Parcel reply;\n"
458 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
459 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
460 << ", data, &reply);\n"
461 << " if (err == ::android::OK) {\n"
462 << " ::android::binder::Status _aidl_status;\n"
463 << " err = _aidl_status.readFromParcel(reply);\n"
464 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
Jiyong Parkb0944822020-03-06 17:08:30 +0900465 << " reply.readUtf8FromUtf16(&cached_hash_);\n"
Paul Trautrimb77048c2020-01-21 16:39:32 +0900466 << " }\n"
467 << " }\n"
468 << " }\n"
469 << " return cached_hash_;\n"
470 << "}\n";
471 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
472 }
Jiyong Park309668e2018-07-28 16:55:44 +0900473 return nullptr;
474}
475
Christopher Wiley36570f42015-10-08 17:20:11 -0700476} // namespace
477
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900478unique_ptr<Document> BuildClientSource(const AidlTypenames& typenames,
479 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700480 vector<string> include_list = {
481 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900482 kParcelHeader,
483 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700484 };
Jiyong Parkce50e262018-10-29 09:54:20 +0900485 if (options.GenLog()) {
486 include_list.emplace_back("chrono");
487 include_list.emplace_back("functional");
488 include_list.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900489 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700490 vector<unique_ptr<Declaration>> file_decls;
491
492 // The constructor just passes the IBinder instance up to the super
493 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700494 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700495 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700496 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800497 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
498 kImplVarName)},
499 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700500
Jiyong Parkce50e262018-10-29 09:54:20 +0900501 if (options.GenLog()) {
502 string code;
503 ClassName(interface, ClassNames::CLIENT);
504 CodeWriterPtr writer = CodeWriter::ForString(&code);
505 (*writer) << "std::function<void(const Json::Value&)> "
506 << ClassName(interface, ClassNames::CLIENT) << "::logFunc;\n";
507 writer->Close();
508 file_decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
509 }
510
Christopher Wiley36570f42015-10-08 17:20:11 -0700511 // Clients define a method per transaction.
512 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900513 unique_ptr<Declaration> m;
514 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900515 m = DefineClientTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900516 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900517 m = DefineClientMetaTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900518 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700519 if (!m) { return nullptr; }
520 file_decls.push_back(std::move(m));
521 }
522 return unique_ptr<Document>{new CppSource{
523 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700524 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700525}
526
Christopher Wileyad339272015-10-05 19:11:58 -0700527namespace {
528
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900529bool HandleServerTransaction(const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900530 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700531 // Declare all the parameters now. In the common case, we expect no errors
532 // in serialization.
533 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900534 if (!DeclareLocalVariable(*a, b, typenames)) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700535 return false;
536 }
Christopher Wileyad339272015-10-05 19:11:58 -0700537 }
538
539 // Declare a variable to hold the return value.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900540 if (method.GetType().GetName() != "void") {
541 string type = CppNameOf(method.GetType(), typenames);
542 b->AddLiteral(StringPrintf("%s %s", type.c_str(), kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700543 }
544
Christopher Wiley8993cb52015-10-21 09:53:24 -0700545 // Check that the client is calling the correct interface.
546 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800547 new MethodCall(StringPrintf("%s.checkInterface",
548 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700549 true /* invert the check */);
550 b->AddStatement(interface_check);
551 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800552 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700553 interface_check->OnTrue()->AddLiteral("break");
554
Christopher Wileyad339272015-10-05 19:11:58 -0700555 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700556 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700557 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800558 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
559 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Normanee8674f2019-09-20 16:07:00 -0700560 const string& var_name = "&" + BuildVarName(*a);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700561 if (a->IsIn()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700562 const string& readMethod = ParcelReadMethodOf(a->GetType(), typenames);
563 b->AddStatement(
564 new Assignment{kAndroidStatusVarName,
Daniel Normanee8674f2019-09-20 16:07:00 -0700565 new MethodCall{string(kDataVarName) + "." + readMethod,
566 ParcelReadCastOf(a->GetType(), typenames, var_name)}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700567 b->AddStatement(BreakOnStatusNotOk());
568 } else if (a->IsOut() && a->GetType().IsArray()) {
569 // Special case, the length of the out array is written into the parcel.
570 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
571 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Norman85aed542019-08-21 12:01:14 -0700572 b->AddStatement(
573 new Assignment{kAndroidStatusVarName,
574 new MethodCall{string(kDataVarName) + ".resizeOutVector", var_name}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700575 b->AddStatement(BreakOnStatusNotOk());
576 }
Christopher Wileyad339272015-10-05 19:11:58 -0700577 }
578
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900579 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100580 b->AddStatement(new Statement(new MethodCall("atrace_begin",
581 ArgList{{"ATRACE_TAG_AIDL",
582 StringPrintf("\"%s::%s::cppServer\"",
583 interface.GetName().c_str(),
584 method.GetName().c_str())}})));
585 }
Jeongik Cha46375122018-12-21 18:41:21 +0900586 const string bn_name = ClassName(interface, ClassNames::SERVER);
587 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900588 b->AddLiteral(GenLogBeforeExecute(bn_name, method, true /* isServer */, false /* isNdk */),
589 false);
Jeongik Cha46375122018-12-21 18:41:21 +0900590 }
Christopher Wileyad339272015-10-05 19:11:58 -0700591 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800592 vector<unique_ptr<AstNode>> status_args;
593 status_args.emplace_back(new MethodCall(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900594 method.GetName(), BuildArgList(typenames, method, false /* not for method decl */)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800595 b->AddStatement(new Statement(new MethodCall(
596 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
597 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700598
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900599 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100600 b->AddStatement(new Statement(new MethodCall("atrace_end",
601 "ATRACE_TAG_AIDL")));
602 }
603
Jeongik Chab34800b2019-03-08 14:36:58 +0900604 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900605 b->AddLiteral(GenLogAfterExecute(bn_name, interface, method, kStatusVarName, kReturnVarName,
606 true /* isServer */, false /* isNdk */),
607 false);
Jeongik Chab34800b2019-03-08 14:36:58 +0900608 }
609
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800610 // Write exceptions during transaction handling to parcel.
611 if (!method.IsOneway()) {
612 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800613 kAndroidStatusVarName,
614 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800615 b->AddStatement(BreakOnStatusNotOk());
616 IfStatement* exception_check = new IfStatement(
617 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
618 b->AddStatement(exception_check);
619 exception_check->OnTrue()->AddLiteral("break");
620 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700621
Christopher Wiley36570f42015-10-08 17:20:11 -0700622 // If we have a return value, write it first.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900623 if (method.GetType().GetName() != "void") {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700624 string writeMethod =
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900625 string(kReplyVarName) + "->" + ParcelWriteMethodOf(method.GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700626 b->AddStatement(new Assignment(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900627 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700628 new MethodCall(writeMethod,
629 ParcelWriteCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700630 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700631 }
Christopher Wileyad339272015-10-05 19:11:58 -0700632 // Write each out parameter to the reply parcel.
633 for (const AidlArgument* a : method.GetOutArguments()) {
634 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800635 // _aidl_ret_status = data.WriteInt32(out_param_name);
636 // if (_aidl_ret_status != ::android::OK) { break; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900637 const string& writeMethod = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700638 b->AddStatement(new Assignment(
639 kAndroidStatusVarName,
640 new MethodCall(string(kReplyVarName) + "->" + writeMethod,
641 ParcelWriteCastOf(a->GetType(), typenames, BuildVarName(*a)))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700642 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700643 }
644
645 return true;
646}
647
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900648bool HandleServerMetaTransaction(const AidlTypenames&, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900649 const AidlMethod& method, const Options& options,
650 StatementBlock* b) {
651 CHECK(!method.IsUserDefined());
652
653 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
654 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900655 code << "_aidl_data.checkInterface(this);\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900656 << "_aidl_reply->writeNoException();\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900657 << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
Jiyong Park309668e2018-07-28 16:55:44 +0900658 << "::VERSION)";
659 b->AddLiteral(code.str());
660 return true;
661 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900662 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
663 std::ostringstream code;
664 code << "_aidl_data.checkInterface(this);\n"
665 << "_aidl_reply->writeNoException();\n"
Jiyong Parkb0944822020-03-06 17:08:30 +0900666 << "_aidl_reply->writeUtf8AsUtf16(" << ClassName(interface, ClassNames::INTERFACE)
667 << "::HASH)";
Paul Trautrimb77048c2020-01-21 16:39:32 +0900668 b->AddLiteral(code.str());
669 return true;
670 }
Jiyong Park309668e2018-07-28 16:55:44 +0900671 return false;
672}
673
Christopher Wileyad339272015-10-05 19:11:58 -0700674} // namespace
675
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900676unique_ptr<Document> BuildServerSource(const AidlTypenames& typenames,
677 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700678 const string bn_name = ClassName(interface, ClassNames::SERVER);
679 vector<string> include_list{
680 HeaderFile(interface, ClassNames::SERVER, false),
Steven Morelanda57d0a62019-07-30 09:41:14 -0700681 kParcelHeader,
682 kStabilityHeader,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700683 };
Jeongik Cha46375122018-12-21 18:41:21 +0900684 if (options.GenLog()) {
685 include_list.emplace_back("chrono");
686 include_list.emplace_back("functional");
687 include_list.emplace_back("json/value.h");
Jeongik Cha46375122018-12-21 18:41:21 +0900688 }
Steven Moreland800508d2019-07-30 10:45:31 -0700689
690 unique_ptr<ConstructorImpl> constructor{
691 new ConstructorImpl{ClassName(interface, ClassNames::SERVER), ArgList{}, {}}};
692
Steven Morelanda57d0a62019-07-30 09:41:14 -0700693 if (interface.IsVintfStability()) {
694 constructor->GetStatementBlock()->AddLiteral("::android::internal::Stability::markVintf(this)");
695 } else {
696 constructor->GetStatementBlock()->AddLiteral(
697 "::android::internal::Stability::markCompilationUnit(this)");
698 }
699
Christopher Wileyad339272015-10-05 19:11:58 -0700700 unique_ptr<MethodImpl> on_transact{new MethodImpl{
701 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800702 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
703 StringPrintf("const %s& %s", kAndroidParcelLiteral,
704 kDataVarName),
705 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
706 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700707 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700708
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800709 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700710 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800711 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
712 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700713
Christopher Wileyad339272015-10-05 19:11:58 -0700714 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800715 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700716 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700717
718 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700719 for (const auto& method : interface.GetMethods()) {
Jeongik Chab5d962f2018-11-17 09:12:28 +0900720 StatementBlock* b = s->AddCase(GetTransactionIdFor(*method));
Christopher Wileyad339272015-10-05 19:11:58 -0700721 if (!b) { return nullptr; }
722
Jiyong Park309668e2018-07-28 16:55:44 +0900723 bool success = false;
724 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900725 success = HandleServerTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900726 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900727 success = HandleServerMetaTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900728 }
729 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900730 return nullptr;
731 }
Christopher Wileyad339272015-10-05 19:11:58 -0700732 }
733
734 // The switch statement has a default case which defers to the super class.
735 // The superclass handles a few pre-defined transactions.
736 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800737 b->AddLiteral(StringPrintf(
738 "%s = ::android::BBinder::onTransact(%s, %s, "
739 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
740 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700741
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800742 // If we saw a null reference, we can map that to an appropriate exception.
743 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800744 new LiteralExpression(string(kAndroidStatusVarName) +
745 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800746 on_transact->GetStatementBlock()->AddStatement(null_check);
747 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800748 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800749 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800750 ".writeToParcel(%s)",
751 kBinderStatusLiteral, kBinderStatusLiteral,
752 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800753
Christopher Wileyad339272015-10-05 19:11:58 -0700754 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800755 on_transact->GetStatementBlock()->AddLiteral(
756 StringPrintf("return %s", kAndroidStatusVarName));
Jeongik Cha46375122018-12-21 18:41:21 +0900757 vector<unique_ptr<Declaration>> decls;
Steven Moreland800508d2019-07-30 10:45:31 -0700758 decls.push_back(std::move(constructor));
Jeongik Cha46375122018-12-21 18:41:21 +0900759 decls.push_back(std::move(on_transact));
Christopher Wileyad339272015-10-05 19:11:58 -0700760
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900761 if (options.Version() > 0) {
762 std::ostringstream code;
763 code << "int32_t " << bn_name << "::" << kGetInterfaceVersion << "() {\n"
764 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::VERSION;\n"
765 << "}\n";
766 decls.emplace_back(new LiteralDecl(code.str()));
767 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900768 if (!options.Hash().empty()) {
769 std::ostringstream code;
770 code << "std::string " << bn_name << "::" << kGetInterfaceHash << "() {\n"
771 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::HASH;\n"
772 << "}\n";
773 decls.emplace_back(new LiteralDecl(code.str()));
774 }
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900775
Jeongik Cha46375122018-12-21 18:41:21 +0900776 if (options.GenLog()) {
777 string code;
778 ClassName(interface, ClassNames::SERVER);
779 CodeWriterPtr writer = CodeWriter::ForString(&code);
780 (*writer) << "std::function<void(const Json::Value&)> "
781 << ClassName(interface, ClassNames::SERVER) << "::logFunc;\n";
782 writer->Close();
783 decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
784 }
785 return unique_ptr<Document>{
786 new CppSource{include_list, NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700787}
788
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900789unique_ptr<Document> BuildInterfaceSource(const AidlTypenames& typenames,
Jooyung Han7bee8e32020-01-30 17:25:21 +0900790 const AidlInterface& interface,
791 [[maybe_unused]] const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700792 vector<string> include_list{
Jiyong Park5b7e5322019-04-03 20:05:01 +0900793 HeaderFile(interface, ClassNames::RAW, false),
Christopher Wiley054afbd2015-10-16 17:08:43 -0700794 HeaderFile(interface, ClassNames::CLIENT, false),
795 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700796
Christopher Wiley054afbd2015-10-16 17:08:43 -0700797 string fq_name = ClassName(interface, ClassNames::INTERFACE);
798 if (!interface.GetPackage().empty()) {
799 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700800 }
801
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700802 vector<unique_ptr<Declaration>> decls;
803
Christopher Wiley11a9d792016-02-24 17:20:33 -0800804 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Ivan Lozanob3f4d622019-11-25 09:30:02 -0800805 "DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE",
806 ArgList{vector<string>{ClassName(interface, ClassNames::BASE), '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700807 decls.push_back(std::move(meta_if));
808
Steven Moreland693640b2018-07-19 13:46:27 -0700809 for (const auto& constant : interface.GetConstantDeclarations()) {
810 const AidlConstantValue& value = constant->GetValue();
811 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
812
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900813 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700814 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
815 ClassName(interface, ClassNames::INTERFACE),
816 constant->GetName(), {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700817 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700818 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700819 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700820 getter->GetStatementBlock()->AddLiteral("return value");
821 decls.push_back(std::move(getter));
822 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700823
824 return unique_ptr<Document>{new CppSource{
825 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700826 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700827}
828
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900829unique_ptr<Document> BuildClientHeader(const AidlTypenames& typenames,
830 const AidlInterface& interface, const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700831 const string i_name = ClassName(interface, ClassNames::INTERFACE);
832 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700833
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900834 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900835 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900836
Christopher Wileyb23149d2015-10-14 13:52:21 -0700837 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
838 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800839 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
840 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700841 ConstructorDecl::IS_EXPLICIT
842 }};
843 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
844 "~" + bp_name,
845 ArgList{},
846 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700847
Christopher Wileyf944e792015-09-29 10:00:46 -0700848 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700849 publics.push_back(std::move(constructor));
850 publics.push_back(std::move(destructor));
851
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700852 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900853 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900854 publics.push_back(BuildMethodDecl(*method, typenames, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900855 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900856 publics.push_back(BuildMetaMethodDecl(*method, typenames, options, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900857 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700858 }
859
Jiyong Parkce50e262018-10-29 09:54:20 +0900860 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900861 includes.emplace_back("chrono"); // for std::chrono::steady_clock
862 includes.emplace_back("functional"); // for std::function
863 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900864 publics.emplace_back(
865 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
866 }
867
Jiyong Park309668e2018-07-28 16:55:44 +0900868 vector<unique_ptr<Declaration>> privates;
869
870 if (options.Version() > 0) {
871 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
872 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900873 if (!options.Hash().empty()) {
874 privates.emplace_back(new LiteralDecl("std::string cached_hash_ = \"-1\";\n"));
875 privates.emplace_back(new LiteralDecl("std::mutex cached_hash_mutex_;\n"));
876 }
Jiyong Park309668e2018-07-28 16:55:44 +0900877
878 unique_ptr<ClassDecl> bp_class{new ClassDecl{
879 bp_name,
880 "::android::BpInterface<" + i_name + ">",
881 std::move(publics),
882 std::move(privates),
883 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700884
Jiyong Parkce50e262018-10-29 09:54:20 +0900885 return unique_ptr<Document>{
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900886 new CppHeader{BuildHeaderGuard(interface, ClassNames::CLIENT), includes,
Jiyong Parkce50e262018-10-29 09:54:20 +0900887 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700888}
889
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900890unique_ptr<Document> BuildServerHeader(const AidlTypenames& /* typenames */,
Jeongik Cha46375122018-12-21 18:41:21 +0900891 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700892 const string i_name = ClassName(interface, ClassNames::INTERFACE);
893 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700894
Steven Moreland800508d2019-07-30 10:45:31 -0700895 unique_ptr<ConstructorDecl> constructor{
896 new ConstructorDecl{bn_name, ArgList{}, ConstructorDecl::IS_EXPLICIT}};
897
Christopher Wileyfd51d602015-10-14 13:04:48 -0700898 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700899 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800900 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
901 StringPrintf("const %s& %s", kAndroidParcelLiteral,
902 kDataVarName),
903 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900904 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700905 MethodDecl::IS_OVERRIDE
906 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900907 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700908
Jeongik Cha46375122018-12-21 18:41:21 +0900909 vector<unique_ptr<Declaration>> publics;
Steven Moreland800508d2019-07-30 10:45:31 -0700910 publics.push_back(std::move(constructor));
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700911 publics.push_back(std::move(on_transact));
912
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900913 if (options.Version() > 0) {
914 std::ostringstream code;
915 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
916 publics.emplace_back(new LiteralDecl(code.str()));
917 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900918 if (!options.Hash().empty()) {
919 std::ostringstream code;
920 code << "std::string " << kGetInterfaceHash << "();\n";
921 publics.emplace_back(new LiteralDecl(code.str()));
922 }
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900923
Jeongik Cha46375122018-12-21 18:41:21 +0900924 if (options.GenLog()) {
925 includes.emplace_back("chrono"); // for std::chrono::steady_clock
926 includes.emplace_back("functional"); // for std::function
927 includes.emplace_back("json/value.h");
928 publics.emplace_back(
929 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
930 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700931 unique_ptr<ClassDecl> bn_class{
932 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800933 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700934 std::move(publics),
935 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700936 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700937
Jeongik Cha46375122018-12-21 18:41:21 +0900938 return unique_ptr<Document>{
939 new CppHeader{BuildHeaderGuard(interface, ClassNames::SERVER), includes,
940 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700941}
942
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900943unique_ptr<Document> BuildInterfaceHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900944 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900945 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700946
947 for (const auto& method : interface.GetMethods()) {
948 for (const auto& argument : method->GetArguments()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900949 AddHeaders(argument->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700950 }
951
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900952 AddHeaders(method->GetType(), typenames, includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700953 }
954
Jiyong Park75e1a742018-07-04 12:31:23 +0900955 const string i_name = ClassName(interface, ClassNames::INTERFACE);
956 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800957 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700958 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700959 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700960
Jiyong Park309668e2018-07-28 16:55:44 +0900961 if (options.Version() > 0) {
962 std::ostringstream code;
963 code << "const int32_t VERSION = " << options.Version() << ";\n";
964
965 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
966 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900967 if (!options.Hash().empty()) {
968 std::ostringstream code;
969 code << "const std::string HASH = \"" << options.Hash() << "\";\n";
970
971 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
972 }
Jiyong Park309668e2018-07-28 16:55:44 +0900973
Steven Moreland693640b2018-07-19 13:46:27 -0700974 std::vector<std::unique_ptr<Declaration>> string_constants;
Daniel Norman85aed542019-08-21 12:01:14 -0700975 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t", false}};
Steven Moreland693640b2018-07-19 13:46:27 -0700976 for (const auto& constant : interface.GetConstantDeclarations()) {
977 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800978
Steven Moreland693640b2018-07-19 13:46:27 -0700979 switch (value.GetType()) {
980 case AidlConstantValue::Type::STRING: {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900981 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700982 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
983 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700984 string_constants.push_back(std::move(getter));
985 break;
986 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700987 case AidlConstantValue::Type::BOOLEAN: // fall-through
988 case AidlConstantValue::Type::INT8: // fall-through
989 case AidlConstantValue::Type::INT32: {
Steven Moreland860b1942018-08-16 14:59:28 -0700990 int_constant_enum->AddValue(constant->GetName(),
991 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700992 break;
993 }
994 default: {
995 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
996 }
997 }
998 }
999 if (int_constant_enum->HasValues()) {
1000 if_class->AddPublic(std::move(int_constant_enum));
1001 }
1002 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -07001003 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -07001004
1005 for (auto& string_constant : string_constants) {
1006 if_class->AddPublic(std::move(string_constant));
1007 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -07001008 }
Martijn Coenenf1b50782018-02-21 21:06:23 +01001009
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001010 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +01001011 includes.insert(kTraceHeader);
1012 }
1013
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -07001014 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -07001015 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +09001016 if (method->IsUserDefined()) {
1017 // Each method gets an enum entry and pure virtual declaration.
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001018 if_class->AddPublic(BuildMethodDecl(*method, typenames, true));
Jiyong Park309668e2018-07-28 16:55:44 +09001019 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001020 if_class->AddPublic(BuildMetaMethodDecl(*method, typenames, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +09001021 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -07001022 }
Christopher Wiley0c732db2015-09-29 14:36:44 -07001023 }
Christopher Wiley0c732db2015-09-29 14:36:44 -07001024
Jooyung Han7bee8e32020-01-30 17:25:21 +09001025 // Implement the default impl class.
1026 vector<unique_ptr<Declaration>> method_decls;
1027 // onAsBinder returns nullptr as this interface is not associated with a
1028 // real binder.
1029 method_decls.emplace_back(
1030 new LiteralDecl("::android::IBinder* onAsBinder() override {\n"
1031 " return nullptr;\n"
1032 "}\n"));
1033 // Each interface method by default returns UNKNOWN_TRANSACTION with is
1034 // the same status that is returned by transact() when the method is
1035 // not implemented in the server side. In other words, these default
1036 // methods do nothing; they only exist to aid making a real default
1037 // impl class without having to override all methods in an interface.
Jiyong Park75e1a742018-07-04 12:31:23 +09001038 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +09001039 if (method->IsUserDefined()) {
Jooyung Han7bee8e32020-01-30 17:25:21 +09001040 std::ostringstream code;
1041 code << "::android::binder::Status " << method->GetName()
1042 << BuildArgList(typenames, *method, true, true).ToString() << " override {\n"
1043 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
1044 << "}\n";
1045 method_decls.emplace_back(new LiteralDecl(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +09001046 } else {
Jooyung Han7bee8e32020-01-30 17:25:21 +09001047 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
1048 std::ostringstream code;
1049 code << "int32_t " << kGetInterfaceVersion << "() override {\n"
1050 << " return 0;\n"
1051 << "}\n";
1052 method_decls.emplace_back(new LiteralDecl(code.str()));
1053 }
1054 if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) {
1055 std::ostringstream code;
1056 code << "std::string " << kGetInterfaceHash << "() override {\n"
1057 << " return \"\";\n"
1058 << "}\n";
1059 method_decls.emplace_back(new LiteralDecl(code.str()));
1060 }
Jiyong Park309668e2018-07-28 16:55:44 +09001061 }
Jiyong Park75e1a742018-07-04 12:31:23 +09001062 }
Jiyong Park309668e2018-07-28 16:55:44 +09001063
Jooyung Han7bee8e32020-01-30 17:25:21 +09001064 vector<unique_ptr<Declaration>> decls;
1065 decls.emplace_back(std::move(if_class));
1066 decls.emplace_back(new ClassDecl{
1067 ClassName(interface, ClassNames::DEFAULT_IMPL), i_name, std::move(method_decls), {}});
Jiyong Park75e1a742018-07-04 12:31:23 +09001068
1069 return unique_ptr<Document>{
1070 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
1071 vector<string>(includes.begin(), includes.end()),
1072 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001073}
1074
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001075std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001076 const AidlStructuredParcelable& parcel,
1077 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001078 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
1079
1080 set<string> includes = {kStatusHeader, kParcelHeader};
Jeongik Cha10f72b72019-09-04 21:28:13 +09001081 includes.insert("tuple");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001082 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001083 AddHeaders(variable->GetType(), typenames, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001084 }
1085
Jeongik Cha10f72b72019-09-04 21:28:13 +09001086 set<string> operators = {"<", ">", "==", ">=", "<=", "!="};
1087 for (const auto& op : operators) {
1088 std::ostringstream operator_code;
1089 std::vector<std::string> variable_name;
1090 std::vector<std::string> rhs_variable_name;
1091 for (const auto& variable : parcel.GetFields()) {
1092 variable_name.push_back(variable->GetName());
1093 rhs_variable_name.push_back("rhs." + variable->GetName());
1094 }
1095
1096 operator_code << "inline bool operator" << op << "(const " << parcel.GetName()
1097 << "& rhs) const {\n"
1098 << " return "
1099 << "std::tie(" << Join(variable_name, ", ") << ")" << op << "std::tie("
1100 << Join(rhs_variable_name, ", ") << ")"
1101 << ";\n"
1102 << "}\n";
1103
1104 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
1105 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001106 for (const auto& variable : parcel.GetFields()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001107
Steven Moreland9ea10e32018-07-19 15:26:09 -07001108 std::ostringstream out;
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001109 std::string cppType = CppNameOf(variable->GetType(), typenames);
1110 out << cppType.c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001111 if (variable->GetDefaultValue()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001112 out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
1113 << ")";
Jeongik Chadb561c92021-09-25 01:19:56 +09001114 } else if (auto type = typenames.TryGetDefinedType(variable->GetType().GetName()); type) {
1115 if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
1116 if (!variable->GetType().IsArray()) {
1117 // if an enum doesn't have explicit default value, do zero-initialization
1118 out << " = " << cppType << "(0)";
1119 }
1120 }
Jooyung Hana2dafff2022-03-25 22:03:56 +09001121 } else if (AidlTypenames::IsPrimitiveTypename(variable->GetType().GetName()) &&
1122 !variable->GetType().IsArray()) {
1123 out << " = {}";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001124 }
1125 out << ";\n";
1126
1127 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001128 }
1129
1130 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001131 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001132 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001133 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001134 unique_ptr<MethodDecl> write(new MethodDecl(
1135 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1136 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001137 parcel_class->AddPublic(std::move(write));
1138
1139 return unique_ptr<Document>{new CppHeader{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001140 BuildHeaderGuard(parcel, ClassNames::RAW), vector<string>(includes.begin(), includes.end()),
Steven Moreland5557f1c2018-07-02 13:50:23 -07001141 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1142}
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001143std::unique_ptr<Document> BuildParcelSource(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001144 const AidlStructuredParcelable& parcel,
1145 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001146 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1147 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001148 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001149 StatementBlock* read_block = read->GetStatementBlock();
1150 read_block->AddLiteral(
1151 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001152
1153 read_block->AddLiteral(
1154 "size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1155 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1156 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
1157 "size_t _aidl_parcelable_size = static_cast<size_t>(_aidl_parcelable_raw_size);\n");
1158
Steven Moreland5557f1c2018-07-02 13:50:23 -07001159 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001160 string method = ParcelReadMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001161
1162 read_block->AddStatement(new Assignment(
1163 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
Daniel Norman85aed542019-08-21 12:01:14 -07001164 ParcelReadCastOf(variable->GetType(), typenames,
1165 "&" + variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001166 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001167 read_block->AddLiteral(StringPrintf(
1168 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1169 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1170 " return %s;\n"
1171 "}",
1172 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001173 }
1174 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1175
Steven Morelandce39c532018-07-11 16:59:50 -07001176 unique_ptr<MethodImpl> write{
1177 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1178 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001179 StatementBlock* write_block = write->GetStatementBlock();
1180 write_block->AddLiteral(
1181 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001182
1183 write_block->AddLiteral(
1184 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1185 "_aidl_parcel->writeInt32(0);");
1186
Steven Moreland5557f1c2018-07-02 13:50:23 -07001187 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001188 string method = ParcelWriteMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001189 write_block->AddStatement(new Assignment(
Daniel Norman85aed542019-08-21 12:01:14 -07001190 kAndroidStatusVarName,
1191 new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1192 ParcelWriteCastOf(variable->GetType(), typenames, variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001193 write_block->AddStatement(ReturnOnStatusNotOk());
1194 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001195
1196 write_block->AddLiteral(
1197 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1198 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1199 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1200 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001201 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1202
1203 vector<unique_ptr<Declaration>> file_decls;
1204 file_decls.push_back(std::move(read));
1205 file_decls.push_back(std::move(write));
1206
1207 set<string> includes = {};
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001208 AddHeaders(parcel, includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001209
1210 return unique_ptr<Document>{
1211 new CppSource{vector<string>(includes.begin(), includes.end()),
1212 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1213}
1214
Daniel Norman0c1bd362019-11-12 23:05:31 -08001215std::string GenerateEnumToString(const AidlTypenames& typenames,
1216 const AidlEnumDeclaration& enum_decl) {
1217 std::ostringstream code;
1218 code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n";
1219 code << " switch(val) {\n";
1220 std::set<std::string> unique_cases;
1221 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1222 std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator);
1223 // Only add a case if its value has not yet been used in the switch
1224 // statement. C++ does not allow multiple cases with the same value, but
1225 // enums does allow this. In this scenario, the first declared
1226 // enumerator with the given value is printed.
1227 if (unique_cases.count(c) == 0) {
1228 unique_cases.insert(c);
1229 code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n";
1230 code << " return \"" << enumerator->GetName() << "\";\n";
1231 }
1232 }
1233 code << " default:\n";
1234 code << " return std::to_string(static_cast<"
1235 << CppNameOf(enum_decl.GetBackingType(), typenames) << ">(val));\n";
1236 code << " }\n";
1237 code << "}\n";
1238 return code.str();
1239}
1240
Daniel Norman85aed542019-08-21 12:01:14 -07001241std::unique_ptr<Document> BuildEnumHeader(const AidlTypenames& typenames,
1242 const AidlEnumDeclaration& enum_decl) {
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001243 std::unique_ptr<Enum> generated_enum{
Daniel Norman85aed542019-08-21 12:01:14 -07001244 new Enum{enum_decl.GetName(), CppNameOf(enum_decl.GetBackingType(), typenames), true}};
1245 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1246 generated_enum->AddValue(
1247 enumerator->GetName(),
1248 enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator));
1249 }
1250
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001251 std::set<std::string> includes = {
1252 "array",
1253 "binder/Enums.h",
1254 "string",
1255 };
Daniel Norman85aed542019-08-21 12:01:14 -07001256 AddHeaders(enum_decl.GetBackingType(), typenames, includes);
1257
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001258 std::vector<std::unique_ptr<Declaration>> decls1;
1259 decls1.push_back(std::move(generated_enum));
1260 decls1.push_back(std::make_unique<LiteralDecl>(GenerateEnumToString(typenames, enum_decl)));
1261
1262 std::vector<std::unique_ptr<Declaration>> decls2;
1263 decls2.push_back(std::make_unique<LiteralDecl>(GenerateEnumValues(enum_decl, {""})));
Daniel Norman0c1bd362019-11-12 23:05:31 -08001264
Daniel Norman85aed542019-08-21 12:01:14 -07001265 return unique_ptr<Document>{
Steven Morelandb8df37d2019-11-21 12:33:24 -08001266 new CppHeader{BuildHeaderGuard(enum_decl, ClassNames::RAW),
Daniel Norman85aed542019-08-21 12:01:14 -07001267 vector<string>(includes.begin(), includes.end()),
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001268 Append(NestInNamespaces(std::move(decls1), enum_decl.GetSplitPackage()),
1269 NestInNamespaces(std::move(decls2), {"android", "internal"}))}};
Daniel Norman85aed542019-08-21 12:01:14 -07001270}
1271
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001272bool WriteHeader(const Options& options, const AidlTypenames& typenames,
1273 const AidlInterface& interface, const IoDelegate& io_delegate,
1274 ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001275 unique_ptr<Document> header;
1276 switch (header_type) {
1277 case ClassNames::INTERFACE:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001278 header = BuildInterfaceHeader(typenames, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001279 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001280 break;
1281 case ClassNames::CLIENT:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001282 header = BuildClientHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001283 break;
1284 case ClassNames::SERVER:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001285 header = BuildServerHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001286 break;
1287 default:
1288 LOG(FATAL) << "aidl internal error";
1289 }
1290 if (!header) {
1291 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001292 return false;
1293 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001294
Jiyong Park05463732018-08-09 16:03:02 +09001295 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001296 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1297 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001298
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001299 const bool success = code_writer->Close();
1300 if (!success) {
1301 io_delegate.RemovePath(header_path);
1302 }
1303
1304 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001305}
1306
Casey Dahlina834dd42015-09-23 11:52:15 -07001307} // namespace internals
1308
1309using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001310
Jiyong Park74595c12018-07-23 15:22:50 +09001311bool GenerateCppInterface(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001312 const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Park74595c12018-07-23 15:22:50 +09001313 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001314 auto interface_src = BuildInterfaceSource(typenames, interface, options);
1315 auto client_src = BuildClientSource(typenames, interface, options);
1316 auto server_src = BuildServerSource(typenames, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001317
Christopher Wiley054afbd2015-10-16 17:08:43 -07001318 if (!interface_src || !client_src || !server_src) {
1319 return false;
1320 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001321
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001322 if (!WriteHeader(options, typenames, interface, io_delegate, ClassNames::INTERFACE) ||
1323 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::CLIENT) ||
1324 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::SERVER)) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001325 return false;
1326 }
1327
Jiyong Park74595c12018-07-23 15:22:50 +09001328 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001329 interface_src->Write(writer.get());
1330 client_src->Write(writer.get());
1331 server_src->Write(writer.get());
1332
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001333 const bool success = writer->Close();
1334 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001335 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001336 }
1337
1338 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001339}
1340
Jiyong Park74595c12018-07-23 15:22:50 +09001341bool GenerateCppParcel(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001342 const AidlTypenames& typenames, const AidlStructuredParcelable& parcelable,
Jiyong Park74595c12018-07-23 15:22:50 +09001343 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001344 auto header = BuildParcelHeader(typenames, parcelable, options);
1345 auto source = BuildParcelSource(typenames, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001346
1347 if (!header || !source) {
1348 return false;
1349 }
1350
Jiyong Park5b7e5322019-04-03 20:05:01 +09001351 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001352 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1353 header->Write(header_writer.get());
1354 CHECK(header_writer->Close());
1355
Steven Moreland81079f92018-07-06 16:15:53 -07001356 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001357 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001358 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1359 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1360 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001361 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001362 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1363 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1364 CHECK(bn_writer->Close());
1365
Jiyong Park74595c12018-07-23 15:22:50 +09001366 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001367 source->Write(source_writer.get());
1368 CHECK(source_writer->Close());
1369
1370 return true;
1371}
1372
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001373bool GenerateCppParcelDeclaration(const std::string& filename, const Options& options,
1374 const AidlParcelable& parcelable, const IoDelegate& io_delegate) {
1375 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1376 *source_writer
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001377 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001378 CHECK(source_writer->Close());
1379
1380 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1381 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
1382 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1383 header_writer->Write("#error TODO(b/111362593) parcelables do not have headers");
1384 CHECK(header_writer->Close());
1385 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
1386 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1387 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1388 CHECK(bp_writer->Close());
1389 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
1390 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1391 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1392 CHECK(bn_writer->Close());
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001393
1394 return true;
1395}
1396
Daniel Norman85aed542019-08-21 12:01:14 -07001397bool GenerateCppEnumDeclaration(const std::string& filename, const Options& options,
1398 const AidlTypenames& typenames,
1399 const AidlEnumDeclaration& enum_decl,
1400 const IoDelegate& io_delegate) {
1401 auto header = BuildEnumHeader(typenames, enum_decl);
1402 if (!header) return false;
1403
1404 const string header_path = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::RAW);
1405 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1406 header->Write(header_writer.get());
1407 CHECK(header_writer->Close());
1408
1409 // TODO(b/111362593): no unnecessary files just to have consistent output with interfaces
1410 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1411 *source_writer
1412 << "// This file is intentionally left blank as placeholder for enum declaration.\n";
1413 CHECK(source_writer->Close());
1414 const string bp_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::CLIENT);
1415 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1416 bp_writer->Write("#error TODO(b/111362593) enums do not have bp classes");
1417 CHECK(bp_writer->Close());
1418 const string bn_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::SERVER);
1419 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1420 bn_writer->Write("#error TODO(b/111362593) enums do not have bn classes");
1421 CHECK(bn_writer->Close());
1422
1423 return true;
1424}
1425
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001426bool GenerateCpp(const string& output_file, const Options& options, const AidlTypenames& typenames,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001427 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1428 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1429 if (parcelable != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001430 return GenerateCppParcel(output_file, options, typenames, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001431 }
1432
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001433 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1434 if (parcelable_decl != nullptr) {
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001435 return GenerateCppParcelDeclaration(output_file, options, *parcelable_decl, io_delegate);
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001436 }
1437
Daniel Norman85aed542019-08-21 12:01:14 -07001438 const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration();
1439 if (enum_decl != nullptr) {
1440 return GenerateCppEnumDeclaration(output_file, options, typenames, *enum_decl, io_delegate);
1441 }
1442
Steven Moreland5557f1c2018-07-02 13:50:23 -07001443 const AidlInterface* interface = defined_type.AsInterface();
1444 if (interface != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001445 return GenerateCppInterface(output_file, options, typenames, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001446 }
1447
1448 CHECK(false) << "Unrecognized type sent for cpp generation.";
1449 return false;
1450}
1451
Christopher Wileyf944e792015-09-29 10:00:46 -07001452} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001453} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001454} // namespace android