blob: 6c11d35cd1d2bdef9ffe150d4c6f50dbfce44561 [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 +0900224unique_ptr<Declaration> DefineClientTransaction(const AidlTypenames& typenames,
Christopher Wiley36570f42015-10-08 17:20:11 -0700225 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900226 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700227 const string i_name = ClassName(interface, ClassNames::INTERFACE);
228 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900229 unique_ptr<MethodImpl> ret{
Devin Moore53fc99c2020-08-12 08:07:52 -0700230 new MethodImpl{kBinderStatusLiteral,
231 bp_name,
232 method.GetName(),
233 {},
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900234 ArgList{BuildArgList(typenames, method, true /* for method decl */)}}};
Christopher Wiley36570f42015-10-08 17:20:11 -0700235 StatementBlock* b = ret->GetStatementBlock();
236
237 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800238 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700239 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800240 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700241
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800242 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800243 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
244 kAndroidStatusVarName,
245 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800246 // We unconditionally return a Status object.
247 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700248
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900249 if (options.GenTraces()) {
Devin Mooref7600872020-05-13 15:47:50 -0700250 b->AddLiteral(
251 StringPrintf("::android::ScopedTrace %s(ATRACE_TAG_AIDL, \"AIDL::cpp::%s::%s::cppClient\")",
252 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100253 }
254
Jiyong Parkce50e262018-10-29 09:54:20 +0900255 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900256 b->AddLiteral(GenLogBeforeExecute(bp_name, method, false /* isServer */, false /* isNdk */),
257 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900258 }
259
Christopher Wiley8993cb52015-10-21 09:53:24 -0700260 // Add the name of the interface we're hoping to call.
261 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800262 kAndroidStatusVarName,
263 new MethodCall(StringPrintf("%s.writeInterfaceToken",
264 kDataVarName),
265 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800266 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700267
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700268 for (const auto& a: method.GetArguments()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700269 const string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700270
271 if (a->IsIn()) {
272 // Serialization looks roughly like:
273 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
274 // if (_aidl_ret_status != ::android::OK) { goto error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900275 const string& method = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Normanee8674f2019-09-20 16:07:00 -0700276 b->AddStatement(
277 new Assignment(kAndroidStatusVarName,
278 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
279 ParcelWriteCastOf(a->GetType(), typenames, var_name))));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700280 b->AddStatement(GotoErrorOnBadStatus());
281 } else if (a->IsOut() && a->GetType().IsArray()) {
282 // Special case, the length of the out array is written into the parcel.
283 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
284 // if (_aidl_ret_status != ::android::OK) { goto error; }
285 b->AddStatement(new Assignment(
286 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700287 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName), var_name)));
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700288 b->AddStatement(GotoErrorOnBadStatus());
289 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700290 }
291
292 // Invoke the transaction on the remote binder and confirm status.
Jeongik Chab5d962f2018-11-17 09:12:28 +0900293 string transaction_code = GetTransactionIdFor(method);
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700294
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800295 vector<string> args = {transaction_code, kDataVarName,
296 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700297
Steven Morelandacd53472018-12-14 10:17:26 -0800298 if (method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800299 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700300 }
301
Christopher Wiley36570f42015-10-08 17:20:11 -0700302 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800303 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700304 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700305 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900306
307 // If the method is not implemented in the remote side, try to call the
308 // default implementation, if provided.
309 vector<string> arg_names;
310 for (const auto& a : method.GetArguments()) {
Jiyong Park4f3e8c02019-11-22 14:28:47 +0900311 if (IsNonCopyableType(a->GetType(), typenames)) {
312 arg_names.emplace_back(StringPrintf("std::move(%s)", a->GetName().c_str()));
313 } else {
314 arg_names.emplace_back(a->GetName());
315 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900316 }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900317 if (method.GetType().GetName() != "void") {
Jiyong Park75e1a742018-07-04 12:31:23 +0900318 arg_names.emplace_back(kReturnVarName);
319 }
320 b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && "
321 "%s::getDefaultImpl())) {\n"
322 " return %s::getDefaultImpl()->%s(%s);\n"
323 "}\n",
324 i_name.c_str(), i_name.c_str(), method.GetName().c_str(),
325 Join(arg_names, ", ").c_str()),
326 false /* no semicolon */);
327
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800328 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700329
Steven Morelandacd53472018-12-14 10:17:26 -0800330 if (!method.IsOneway()) {
Christopher Wiley1227d612015-10-26 16:59:20 -0700331 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800332 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
333 // if (_aidl_ret_status != ::android::OK) { goto error; }
334 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800335 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800336 kAndroidStatusVarName,
337 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800338 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700339 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800340 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700341 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800342 exception_check->OnTrue()->AddLiteral(
343 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700344 }
345
346 // Type checking should guarantee that nothing below emits code until "return
347 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700348
Christopher Wiley36570f42015-10-08 17:20:11 -0700349 // If the method is expected to return something, read it first by convention.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900350 if (method.GetType().GetName() != "void") {
351 const string& method_call = ParcelReadMethodOf(method.GetType(), typenames);
Christopher Wiley36570f42015-10-08 17:20:11 -0700352 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800353 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700354 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method_call.c_str()),
355 ParcelReadCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800356 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700357 }
358
359 for (const AidlArgument* a : method.GetOutArguments()) {
360 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800361 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
362 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900363 string method = ParcelReadMethodOf(a->GetType(), typenames);
Casey Dahlinb0966612015-10-19 16:35:26 -0700364
Daniel Norman85aed542019-08-21 12:01:14 -0700365 b->AddStatement(
366 new Assignment(kAndroidStatusVarName,
367 new MethodCall(StringPrintf("%s.%s", kReplyVarName, method.c_str()),
368 ParcelReadCastOf(a->GetType(), typenames, a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800369 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700370 }
371
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800372 // If we've gotten to here, one of two things is true:
373 // 1) We've read some bad status_t
374 // 2) We've only read status_t == OK and there was no exception in the
375 // response.
376 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800377 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800378 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800379 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
380 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100381
Jiyong Parkce50e262018-10-29 09:54:20 +0900382 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900383 b->AddLiteral(GenLogAfterExecute(bp_name, interface, method, kStatusVarName, kReturnVarName,
384 false /* isServer */, false /* isNdk */),
Jeongik Cha46375122018-12-21 18:41:21 +0900385 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900386 }
387
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800388 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700389
390 return unique_ptr<Declaration>(ret.release());
391}
392
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900393unique_ptr<Declaration> DefineClientMetaTransaction(const AidlTypenames& /* typenames */,
Jiyong Park309668e2018-07-28 16:55:44 +0900394 const AidlInterface& interface,
395 const AidlMethod& method,
396 const Options& options) {
397 CHECK(!method.IsUserDefined());
398 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
399 const string iface = ClassName(interface, ClassNames::INTERFACE);
400 const string proxy = ClassName(interface, ClassNames::CLIENT);
401 // Note: race condition can happen here, but no locking is required
402 // because 1) writing an interger is atomic and 2) this transaction
403 // will always return the same value, i.e., competing threads will
404 // give write the same value to cached_version_.
405 std::ostringstream code;
406 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900407 << " if (cached_version_ == -1) {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900408 << " ::android::Parcel data;\n"
409 << " ::android::Parcel reply;\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900410 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
Jeongik Chab5d962f2018-11-17 09:12:28 +0900411 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
412 << ", data, &reply);\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900413 << " if (err == ::android::OK) {\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900414 << " ::android::binder::Status _aidl_status;\n"
415 << " err = _aidl_status.readFromParcel(reply);\n"
416 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
417 << " cached_version_ = reply.readInt32();\n"
418 << " }\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900419 << " }\n"
420 << " }\n"
421 << " return cached_version_;\n"
422 << "}\n";
423 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
424 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900425 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
426 const string iface = ClassName(interface, ClassNames::INTERFACE);
427 const string proxy = ClassName(interface, ClassNames::CLIENT);
428 std::ostringstream code;
429 code << "std::string " << proxy << "::" << kGetInterfaceHash << "() {\n"
430 << " std::lock_guard<std::mutex> lockGuard(cached_hash_mutex_);\n"
431 << " if (cached_hash_ == \"-1\") {\n"
432 << " ::android::Parcel data;\n"
433 << " ::android::Parcel reply;\n"
434 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
435 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
436 << ", data, &reply);\n"
437 << " if (err == ::android::OK) {\n"
438 << " ::android::binder::Status _aidl_status;\n"
439 << " err = _aidl_status.readFromParcel(reply);\n"
440 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
Jiyong Park193f5212020-03-06 17:08:30 +0900441 << " reply.readUtf8FromUtf16(&cached_hash_);\n"
Paul Trautrimb77048c2020-01-21 16:39:32 +0900442 << " }\n"
443 << " }\n"
444 << " }\n"
445 << " return cached_hash_;\n"
446 << "}\n";
447 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
448 }
Jiyong Park309668e2018-07-28 16:55:44 +0900449 return nullptr;
450}
451
Christopher Wiley36570f42015-10-08 17:20:11 -0700452} // namespace
453
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900454unique_ptr<Document> BuildClientSource(const AidlTypenames& typenames,
455 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700456 vector<string> include_list = {
457 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900458 kParcelHeader,
459 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700460 };
Jiyong Parkce50e262018-10-29 09:54:20 +0900461 if (options.GenLog()) {
462 include_list.emplace_back("chrono");
463 include_list.emplace_back("functional");
464 include_list.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900465 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700466 vector<unique_ptr<Declaration>> file_decls;
467
468 // The constructor just passes the IBinder instance up to the super
469 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700470 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700471 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700472 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800473 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
474 kImplVarName)},
475 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700476
Jiyong Parkce50e262018-10-29 09:54:20 +0900477 if (options.GenLog()) {
478 string code;
479 ClassName(interface, ClassNames::CLIENT);
480 CodeWriterPtr writer = CodeWriter::ForString(&code);
481 (*writer) << "std::function<void(const Json::Value&)> "
482 << ClassName(interface, ClassNames::CLIENT) << "::logFunc;\n";
483 writer->Close();
484 file_decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
485 }
486
Christopher Wiley36570f42015-10-08 17:20:11 -0700487 // Clients define a method per transaction.
488 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900489 unique_ptr<Declaration> m;
490 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900491 m = DefineClientTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900492 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900493 m = DefineClientMetaTransaction(typenames, interface, *method, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900494 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700495 if (!m) { return nullptr; }
496 file_decls.push_back(std::move(m));
497 }
498 return unique_ptr<Document>{new CppSource{
499 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700500 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700501}
502
Christopher Wileyad339272015-10-05 19:11:58 -0700503namespace {
504
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900505bool HandleServerTransaction(const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900506 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700507 // Declare all the parameters now. In the common case, we expect no errors
508 // in serialization.
509 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Devin Moore61dee6e2020-08-26 14:46:26 -0700510 b->AddLiteral(StringPrintf("%s %s", CppNameOf(a->GetType(), typenames).c_str(),
511 BuildVarName(*a).c_str()));
Christopher Wileyad339272015-10-05 19:11:58 -0700512 }
513
514 // Declare a variable to hold the return value.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900515 if (method.GetType().GetName() != "void") {
516 string type = CppNameOf(method.GetType(), typenames);
517 b->AddLiteral(StringPrintf("%s %s", type.c_str(), kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700518 }
519
Christopher Wiley8993cb52015-10-21 09:53:24 -0700520 // Check that the client is calling the correct interface.
521 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800522 new MethodCall(StringPrintf("%s.checkInterface",
523 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700524 true /* invert the check */);
525 b->AddStatement(interface_check);
526 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800527 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700528 interface_check->OnTrue()->AddLiteral("break");
529
Devin Mooref7600872020-05-13 15:47:50 -0700530 if (options.GenTraces()) {
531 b->AddLiteral(
532 StringPrintf("::android::ScopedTrace %s(ATRACE_TAG_AIDL, \"AIDL::cpp::%s::%s::cppServer\")",
533 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
534 }
535
Christopher Wileyad339272015-10-05 19:11:58 -0700536 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700537 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700538 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800539 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
540 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Normanee8674f2019-09-20 16:07:00 -0700541 const string& var_name = "&" + BuildVarName(*a);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700542 if (a->IsIn()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700543 const string& readMethod = ParcelReadMethodOf(a->GetType(), typenames);
544 b->AddStatement(
545 new Assignment{kAndroidStatusVarName,
Daniel Normanee8674f2019-09-20 16:07:00 -0700546 new MethodCall{string(kDataVarName) + "." + readMethod,
547 ParcelReadCastOf(a->GetType(), typenames, var_name)}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700548 b->AddStatement(BreakOnStatusNotOk());
549 } else if (a->IsOut() && a->GetType().IsArray()) {
550 // Special case, the length of the out array is written into the parcel.
551 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
552 // if (_aidl_ret_status != ::android::OK) { break; }
Daniel Norman85aed542019-08-21 12:01:14 -0700553 b->AddStatement(
554 new Assignment{kAndroidStatusVarName,
555 new MethodCall{string(kDataVarName) + ".resizeOutVector", var_name}});
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700556 b->AddStatement(BreakOnStatusNotOk());
557 }
Christopher Wileyad339272015-10-05 19:11:58 -0700558 }
559
Jeongik Cha46375122018-12-21 18:41:21 +0900560 const string bn_name = ClassName(interface, ClassNames::SERVER);
561 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900562 b->AddLiteral(GenLogBeforeExecute(bn_name, method, true /* isServer */, false /* isNdk */),
563 false);
Jeongik Cha46375122018-12-21 18:41:21 +0900564 }
Christopher Wileyad339272015-10-05 19:11:58 -0700565 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800566 vector<unique_ptr<AstNode>> status_args;
567 status_args.emplace_back(new MethodCall(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900568 method.GetName(), BuildArgList(typenames, method, false /* not for method decl */)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800569 b->AddStatement(new Statement(new MethodCall(
570 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
571 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700572
Jeongik Chab34800b2019-03-08 14:36:58 +0900573 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900574 b->AddLiteral(GenLogAfterExecute(bn_name, interface, method, kStatusVarName, kReturnVarName,
575 true /* isServer */, false /* isNdk */),
576 false);
Jeongik Chab34800b2019-03-08 14:36:58 +0900577 }
578
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800579 // Write exceptions during transaction handling to parcel.
580 if (!method.IsOneway()) {
581 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800582 kAndroidStatusVarName,
583 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800584 b->AddStatement(BreakOnStatusNotOk());
585 IfStatement* exception_check = new IfStatement(
586 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
587 b->AddStatement(exception_check);
588 exception_check->OnTrue()->AddLiteral("break");
589 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700590
Christopher Wiley36570f42015-10-08 17:20:11 -0700591 // If we have a return value, write it first.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900592 if (method.GetType().GetName() != "void") {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700593 string writeMethod =
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900594 string(kReplyVarName) + "->" + ParcelWriteMethodOf(method.GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700595 b->AddStatement(new Assignment(
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900596 kAndroidStatusVarName,
Daniel Norman85aed542019-08-21 12:01:14 -0700597 new MethodCall(writeMethod,
598 ParcelWriteCastOf(method.GetType(), typenames, kReturnVarName))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700599 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700600 }
Christopher Wileyad339272015-10-05 19:11:58 -0700601 // Write each out parameter to the reply parcel.
602 for (const AidlArgument* a : method.GetOutArguments()) {
603 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800604 // _aidl_ret_status = data.WriteInt32(out_param_name);
605 // if (_aidl_ret_status != ::android::OK) { break; }
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900606 const string& writeMethod = ParcelWriteMethodOf(a->GetType(), typenames);
Daniel Norman85aed542019-08-21 12:01:14 -0700607 b->AddStatement(new Assignment(
608 kAndroidStatusVarName,
609 new MethodCall(string(kReplyVarName) + "->" + writeMethod,
610 ParcelWriteCastOf(a->GetType(), typenames, BuildVarName(*a)))));
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700611 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700612 }
613
614 return true;
615}
616
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900617bool HandleServerMetaTransaction(const AidlTypenames&, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900618 const AidlMethod& method, const Options& options,
619 StatementBlock* b) {
620 CHECK(!method.IsUserDefined());
621
622 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
623 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900624 code << "_aidl_data.checkInterface(this);\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900625 << "_aidl_reply->writeNoException();\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900626 << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
Jiyong Park309668e2018-07-28 16:55:44 +0900627 << "::VERSION)";
628 b->AddLiteral(code.str());
629 return true;
630 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900631 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
632 std::ostringstream code;
633 code << "_aidl_data.checkInterface(this);\n"
634 << "_aidl_reply->writeNoException();\n"
Jiyong Park193f5212020-03-06 17:08:30 +0900635 << "_aidl_reply->writeUtf8AsUtf16(" << ClassName(interface, ClassNames::INTERFACE)
636 << "::HASH)";
Paul Trautrimb77048c2020-01-21 16:39:32 +0900637 b->AddLiteral(code.str());
638 return true;
639 }
Jiyong Park309668e2018-07-28 16:55:44 +0900640 return false;
641}
642
Christopher Wileyad339272015-10-05 19:11:58 -0700643} // namespace
644
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900645unique_ptr<Document> BuildServerSource(const AidlTypenames& typenames,
646 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700647 const string bn_name = ClassName(interface, ClassNames::SERVER);
648 vector<string> include_list{
649 HeaderFile(interface, ClassNames::SERVER, false),
Steven Morelanda57d0a62019-07-30 09:41:14 -0700650 kParcelHeader,
651 kStabilityHeader,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700652 };
Jeongik Cha46375122018-12-21 18:41:21 +0900653 if (options.GenLog()) {
654 include_list.emplace_back("chrono");
655 include_list.emplace_back("functional");
656 include_list.emplace_back("json/value.h");
Jeongik Cha46375122018-12-21 18:41:21 +0900657 }
Steven Moreland800508d2019-07-30 10:45:31 -0700658
659 unique_ptr<ConstructorImpl> constructor{
660 new ConstructorImpl{ClassName(interface, ClassNames::SERVER), ArgList{}, {}}};
661
Steven Morelanda57d0a62019-07-30 09:41:14 -0700662 if (interface.IsVintfStability()) {
663 constructor->GetStatementBlock()->AddLiteral("::android::internal::Stability::markVintf(this)");
664 } else {
665 constructor->GetStatementBlock()->AddLiteral(
666 "::android::internal::Stability::markCompilationUnit(this)");
667 }
668
Devin Moore53fc99c2020-08-12 08:07:52 -0700669 unique_ptr<MethodImpl> on_transact{
670 new MethodImpl{kAndroidStatusLiteral,
671 bn_name,
672 "onTransact",
673 {},
674 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
675 StringPrintf("const %s& %s", kAndroidParcelLiteral, kDataVarName),
676 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
677 StringPrintf("uint32_t %s", kFlagsVarName)}}}};
Christopher Wileyad339272015-10-05 19:11:58 -0700678
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800679 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700680 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800681 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
682 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700683
Christopher Wileyad339272015-10-05 19:11:58 -0700684 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800685 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700686 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700687
688 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700689 for (const auto& method : interface.GetMethods()) {
Jeongik Chab5d962f2018-11-17 09:12:28 +0900690 StatementBlock* b = s->AddCase(GetTransactionIdFor(*method));
Christopher Wileyad339272015-10-05 19:11:58 -0700691 if (!b) { return nullptr; }
692
Jiyong Park309668e2018-07-28 16:55:44 +0900693 bool success = false;
694 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900695 success = HandleServerTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900696 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900697 success = HandleServerMetaTransaction(typenames, interface, *method, options, b);
Jiyong Park309668e2018-07-28 16:55:44 +0900698 }
699 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900700 return nullptr;
701 }
Christopher Wileyad339272015-10-05 19:11:58 -0700702 }
703
704 // The switch statement has a default case which defers to the super class.
705 // The superclass handles a few pre-defined transactions.
706 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800707 b->AddLiteral(StringPrintf(
708 "%s = ::android::BBinder::onTransact(%s, %s, "
709 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
710 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700711
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800712 // If we saw a null reference, we can map that to an appropriate exception.
713 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800714 new LiteralExpression(string(kAndroidStatusVarName) +
715 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800716 on_transact->GetStatementBlock()->AddStatement(null_check);
717 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800718 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800719 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800720 ".writeToParcel(%s)",
721 kBinderStatusLiteral, kBinderStatusLiteral,
722 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800723
Christopher Wileyad339272015-10-05 19:11:58 -0700724 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800725 on_transact->GetStatementBlock()->AddLiteral(
726 StringPrintf("return %s", kAndroidStatusVarName));
Jeongik Cha46375122018-12-21 18:41:21 +0900727 vector<unique_ptr<Declaration>> decls;
Steven Moreland800508d2019-07-30 10:45:31 -0700728 decls.push_back(std::move(constructor));
Jeongik Cha46375122018-12-21 18:41:21 +0900729 decls.push_back(std::move(on_transact));
Christopher Wileyad339272015-10-05 19:11:58 -0700730
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900731 if (options.Version() > 0) {
732 std::ostringstream code;
733 code << "int32_t " << bn_name << "::" << kGetInterfaceVersion << "() {\n"
734 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::VERSION;\n"
735 << "}\n";
736 decls.emplace_back(new LiteralDecl(code.str()));
737 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900738 if (!options.Hash().empty()) {
739 std::ostringstream code;
740 code << "std::string " << bn_name << "::" << kGetInterfaceHash << "() {\n"
741 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::HASH;\n"
742 << "}\n";
743 decls.emplace_back(new LiteralDecl(code.str()));
744 }
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900745
Jeongik Cha46375122018-12-21 18:41:21 +0900746 if (options.GenLog()) {
747 string code;
748 ClassName(interface, ClassNames::SERVER);
749 CodeWriterPtr writer = CodeWriter::ForString(&code);
750 (*writer) << "std::function<void(const Json::Value&)> "
751 << ClassName(interface, ClassNames::SERVER) << "::logFunc;\n";
752 writer->Close();
753 decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
754 }
755 return unique_ptr<Document>{
756 new CppSource{include_list, NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700757}
758
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900759unique_ptr<Document> BuildInterfaceSource(const AidlTypenames& typenames,
Jooyung Han7bee8e32020-01-30 17:25:21 +0900760 const AidlInterface& interface,
761 [[maybe_unused]] const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700762 vector<string> include_list{
Jiyong Park5b7e5322019-04-03 20:05:01 +0900763 HeaderFile(interface, ClassNames::RAW, false),
Christopher Wiley054afbd2015-10-16 17:08:43 -0700764 HeaderFile(interface, ClassNames::CLIENT, false),
765 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700766
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700767 vector<unique_ptr<Declaration>> decls;
768
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900769 unique_ptr<MacroDecl> meta_if{
770 new MacroDecl{"DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE",
771 ArgList{vector<string>{ClassName(interface, ClassNames::BASE),
772 '"' + interface.GetDescriptor() + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700773 decls.push_back(std::move(meta_if));
774
Steven Moreland693640b2018-07-19 13:46:27 -0700775 for (const auto& constant : interface.GetConstantDeclarations()) {
776 const AidlConstantValue& value = constant->GetValue();
777 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
778
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900779 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700780 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
781 ClassName(interface, ClassNames::INTERFACE),
Devin Moore53fc99c2020-08-12 08:07:52 -0700782 constant->GetName(), {}, {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700783 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700784 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700785 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700786 getter->GetStatementBlock()->AddLiteral("return value");
787 decls.push_back(std::move(getter));
788 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700789
790 return unique_ptr<Document>{new CppSource{
791 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700792 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700793}
794
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900795unique_ptr<Document> BuildClientHeader(const AidlTypenames& typenames,
796 const AidlInterface& interface, const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700797 const string i_name = ClassName(interface, ClassNames::INTERFACE);
798 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700799
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900800 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900801 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900802
Christopher Wileyb23149d2015-10-14 13:52:21 -0700803 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
804 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800805 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
806 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700807 ConstructorDecl::IS_EXPLICIT
808 }};
809 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
810 "~" + bp_name,
811 ArgList{},
812 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700813
Christopher Wileyf944e792015-09-29 10:00:46 -0700814 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700815 publics.push_back(std::move(constructor));
816 publics.push_back(std::move(destructor));
817
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700818 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900819 if (method->IsUserDefined()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900820 publics.push_back(BuildMethodDecl(*method, typenames, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900821 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900822 publics.push_back(BuildMetaMethodDecl(*method, typenames, options, false));
Jiyong Park309668e2018-07-28 16:55:44 +0900823 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700824 }
825
Jiyong Parkce50e262018-10-29 09:54:20 +0900826 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900827 includes.emplace_back("chrono"); // for std::chrono::steady_clock
828 includes.emplace_back("functional"); // for std::function
829 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900830 publics.emplace_back(
831 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
832 }
833
Jiyong Park309668e2018-07-28 16:55:44 +0900834 vector<unique_ptr<Declaration>> privates;
835
836 if (options.Version() > 0) {
837 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
838 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900839 if (!options.Hash().empty()) {
840 privates.emplace_back(new LiteralDecl("std::string cached_hash_ = \"-1\";\n"));
841 privates.emplace_back(new LiteralDecl("std::mutex cached_hash_mutex_;\n"));
842 }
Jiyong Park309668e2018-07-28 16:55:44 +0900843
844 unique_ptr<ClassDecl> bp_class{new ClassDecl{
845 bp_name,
846 "::android::BpInterface<" + i_name + ">",
Devin Moore53fc99c2020-08-12 08:07:52 -0700847 {},
Jiyong Park309668e2018-07-28 16:55:44 +0900848 std::move(publics),
849 std::move(privates),
850 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700851
Jiyong Parkce50e262018-10-29 09:54:20 +0900852 return unique_ptr<Document>{
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700853 new CppHeader{includes, NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700854}
855
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900856unique_ptr<Document> BuildServerHeader(const AidlTypenames& /* typenames */,
Jeongik Cha46375122018-12-21 18:41:21 +0900857 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700858 const string i_name = ClassName(interface, ClassNames::INTERFACE);
859 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700860
Steven Moreland800508d2019-07-30 10:45:31 -0700861 unique_ptr<ConstructorDecl> constructor{
862 new ConstructorDecl{bn_name, ArgList{}, ConstructorDecl::IS_EXPLICIT}};
863
Christopher Wileyfd51d602015-10-14 13:04:48 -0700864 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700865 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800866 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
867 StringPrintf("const %s& %s", kAndroidParcelLiteral,
868 kDataVarName),
869 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900870 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700871 MethodDecl::IS_OVERRIDE
872 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900873 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700874
Jeongik Cha46375122018-12-21 18:41:21 +0900875 vector<unique_ptr<Declaration>> publics;
Steven Moreland800508d2019-07-30 10:45:31 -0700876 publics.push_back(std::move(constructor));
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700877 publics.push_back(std::move(on_transact));
878
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900879 if (options.Version() > 0) {
880 std::ostringstream code;
881 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
882 publics.emplace_back(new LiteralDecl(code.str()));
883 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900884 if (!options.Hash().empty()) {
885 std::ostringstream code;
886 code << "std::string " << kGetInterfaceHash << "();\n";
887 publics.emplace_back(new LiteralDecl(code.str()));
888 }
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900889
Jeongik Cha46375122018-12-21 18:41:21 +0900890 if (options.GenLog()) {
891 includes.emplace_back("chrono"); // for std::chrono::steady_clock
892 includes.emplace_back("functional"); // for std::function
893 includes.emplace_back("json/value.h");
894 publics.emplace_back(
895 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
896 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700897 unique_ptr<ClassDecl> bn_class{
Devin Moore53fc99c2020-08-12 08:07:52 -0700898 new ClassDecl{bn_name, "::android::BnInterface<" + i_name + ">", {}, std::move(publics), {}}};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700899
Jeongik Cha46375122018-12-21 18:41:21 +0900900 return unique_ptr<Document>{
Devin Moore7aaa9cb2020-08-13 14:53:01 -0700901 new CppHeader{includes, NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700902}
903
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900904unique_ptr<Document> BuildInterfaceHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900905 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900906 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700907
908 for (const auto& method : interface.GetMethods()) {
909 for (const auto& argument : method->GetArguments()) {
Devin Moore2f2077a2020-08-28 11:27:53 -0700910 AddHeaders(argument->GetType(), typenames, &includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700911 }
912
Devin Moore2f2077a2020-08-28 11:27:53 -0700913 AddHeaders(method->GetType(), typenames, &includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700914 }
915
Jiyong Park75e1a742018-07-04 12:31:23 +0900916 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Devin Moore53fc99c2020-08-12 08:07:52 -0700917 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface", {}}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800918 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700919 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700920 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700921
Jiyong Park309668e2018-07-28 16:55:44 +0900922 if (options.Version() > 0) {
923 std::ostringstream code;
924 code << "const int32_t VERSION = " << options.Version() << ";\n";
925
926 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
927 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900928 if (!options.Hash().empty()) {
929 std::ostringstream code;
930 code << "const std::string HASH = \"" << options.Hash() << "\";\n";
931
932 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
933 }
Jiyong Park309668e2018-07-28 16:55:44 +0900934
Steven Moreland693640b2018-07-19 13:46:27 -0700935 std::vector<std::unique_ptr<Declaration>> string_constants;
Daniel Norman85aed542019-08-21 12:01:14 -0700936 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t", false}};
Steven Moreland693640b2018-07-19 13:46:27 -0700937 for (const auto& constant : interface.GetConstantDeclarations()) {
938 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800939
Steven Moreland693640b2018-07-19 13:46:27 -0700940 switch (value.GetType()) {
941 case AidlConstantValue::Type::STRING: {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900942 std::string cppType = CppNameOf(constant->GetType(), typenames);
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700943 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
944 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700945 string_constants.push_back(std::move(getter));
946 break;
947 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700948 case AidlConstantValue::Type::BOOLEAN: // fall-through
949 case AidlConstantValue::Type::INT8: // fall-through
950 case AidlConstantValue::Type::INT32: {
Steven Moreland860b1942018-08-16 14:59:28 -0700951 int_constant_enum->AddValue(constant->GetName(),
952 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700953 break;
954 }
955 default: {
956 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
957 }
958 }
959 }
960 if (int_constant_enum->HasValues()) {
961 if_class->AddPublic(std::move(int_constant_enum));
962 }
963 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700964 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700965
966 for (auto& string_constant : string_constants) {
967 if_class->AddPublic(std::move(string_constant));
968 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700969 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100970
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900971 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100972 includes.insert(kTraceHeader);
973 }
974
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700975 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700976 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900977 if (method->IsUserDefined()) {
978 // Each method gets an enum entry and pure virtual declaration.
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900979 if_class->AddPublic(BuildMethodDecl(*method, typenames, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900980 } else {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900981 if_class->AddPublic(BuildMetaMethodDecl(*method, typenames, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900982 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700983 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700984 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700985
Jooyung Han7bee8e32020-01-30 17:25:21 +0900986 // Implement the default impl class.
987 vector<unique_ptr<Declaration>> method_decls;
988 // onAsBinder returns nullptr as this interface is not associated with a
989 // real binder.
990 method_decls.emplace_back(
991 new LiteralDecl("::android::IBinder* onAsBinder() override {\n"
992 " return nullptr;\n"
993 "}\n"));
994 // Each interface method by default returns UNKNOWN_TRANSACTION with is
995 // the same status that is returned by transact() when the method is
996 // not implemented in the server side. In other words, these default
997 // methods do nothing; they only exist to aid making a real default
998 // impl class without having to override all methods in an interface.
Jiyong Park75e1a742018-07-04 12:31:23 +0900999 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +09001000 if (method->IsUserDefined()) {
Jooyung Han7bee8e32020-01-30 17:25:21 +09001001 std::ostringstream code;
1002 code << "::android::binder::Status " << method->GetName()
1003 << BuildArgList(typenames, *method, true, true).ToString() << " override {\n"
1004 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
1005 << "}\n";
1006 method_decls.emplace_back(new LiteralDecl(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +09001007 } else {
Jooyung Han7bee8e32020-01-30 17:25:21 +09001008 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
1009 std::ostringstream code;
1010 code << "int32_t " << kGetInterfaceVersion << "() override {\n"
1011 << " return 0;\n"
1012 << "}\n";
1013 method_decls.emplace_back(new LiteralDecl(code.str()));
1014 }
1015 if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) {
1016 std::ostringstream code;
1017 code << "std::string " << kGetInterfaceHash << "() override {\n"
1018 << " return \"\";\n"
1019 << "}\n";
1020 method_decls.emplace_back(new LiteralDecl(code.str()));
1021 }
Jiyong Park309668e2018-07-28 16:55:44 +09001022 }
Jiyong Park75e1a742018-07-04 12:31:23 +09001023 }
Jiyong Park309668e2018-07-28 16:55:44 +09001024
Jooyung Han7bee8e32020-01-30 17:25:21 +09001025 vector<unique_ptr<Declaration>> decls;
1026 decls.emplace_back(std::move(if_class));
1027 decls.emplace_back(new ClassDecl{
Devin Moore53fc99c2020-08-12 08:07:52 -07001028 ClassName(interface, ClassNames::DEFAULT_IMPL), i_name, {}, std::move(method_decls), {}});
Jiyong Park75e1a742018-07-04 12:31:23 +09001029
1030 return unique_ptr<Document>{
Devin Moore7aaa9cb2020-08-13 14:53:01 -07001031 new CppHeader{vector<string>(includes.begin(), includes.end()),
Jiyong Park75e1a742018-07-04 12:31:23 +09001032 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001033}
1034
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001035std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001036 const AidlStructuredParcelable& parcel,
1037 const Options&) {
Devin Moore53fc99c2020-08-12 08:07:52 -07001038 const std::vector<std::string>& type_params =
1039 parcel.IsGeneric() ? parcel.GetTypeParameters() : std::vector<std::string>();
1040 unique_ptr<ClassDecl> parcel_class{
1041 new ClassDecl{parcel.GetName(), "::android::Parcelable", type_params}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001042
1043 set<string> includes = {kStatusHeader, kParcelHeader};
Jeongik Cha10f72b72019-09-04 21:28:13 +09001044 includes.insert("tuple");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001045 for (const auto& variable : parcel.GetFields()) {
Devin Moore2f2077a2020-08-28 11:27:53 -07001046 AddHeaders(variable->GetType(), typenames, &includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001047 }
1048
Jeongik Cha10f72b72019-09-04 21:28:13 +09001049 set<string> operators = {"<", ">", "==", ">=", "<=", "!="};
1050 for (const auto& op : operators) {
1051 std::ostringstream operator_code;
1052 std::vector<std::string> variable_name;
1053 std::vector<std::string> rhs_variable_name;
1054 for (const auto& variable : parcel.GetFields()) {
1055 variable_name.push_back(variable->GetName());
1056 rhs_variable_name.push_back("rhs." + variable->GetName());
1057 }
1058
Jeongik Cha969f11a2020-08-19 13:08:06 +09001059 operator_code << "inline bool operator" << op << "([[maybe_unused]] const " << parcel.GetName()
Jeongik Cha10f72b72019-09-04 21:28:13 +09001060 << "& rhs) const {\n"
1061 << " return "
1062 << "std::tie(" << Join(variable_name, ", ") << ")" << op << "std::tie("
1063 << Join(rhs_variable_name, ", ") << ")"
1064 << ";\n"
1065 << "}\n";
1066
1067 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(operator_code.str())));
1068 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001069 for (const auto& variable : parcel.GetFields()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001070
Steven Moreland9ea10e32018-07-19 15:26:09 -07001071 std::ostringstream out;
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001072 std::string cppType = CppNameOf(variable->GetType(), typenames);
1073 out << cppType.c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001074 if (variable->GetDefaultValue()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001075 out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
1076 << ")";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001077 }
1078 out << ";\n";
1079
1080 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001081 }
1082
Jeongik Cha92d33d02020-05-14 00:53:57 +09001083 if (parcel.IsVintfStability()) {
1084 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(
Steven Moreland4768c962020-07-23 01:21:16 +00001085 new LiteralDecl("::android::Parcelable::Stability getStability() const override { return "
1086 "::android::Parcelable::Stability::STABILITY_VINTF; }\n")));
Jeongik Cha92d33d02020-05-14 00:53:57 +09001087 }
1088
Steven Moreland5557f1c2018-07-02 13:50:23 -07001089 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001090 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001091 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001092 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001093 unique_ptr<MethodDecl> write(new MethodDecl(
1094 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1095 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001096 parcel_class->AddPublic(std::move(write));
1097
Devin Moore7aaa9cb2020-08-13 14:53:01 -07001098 return unique_ptr<Document>{
1099 new CppHeader{vector<string>(includes.begin(), includes.end()),
1100 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001101}
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001102std::unique_ptr<Document> BuildParcelSource(const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001103 const AidlStructuredParcelable& parcel,
1104 const Options&) {
Devin Moore53fc99c2020-08-12 08:07:52 -07001105 const std::vector<std::string>& type_params =
1106 parcel.IsGeneric() ? parcel.GetTypeParameters() : std::vector<std::string>();
Steven Moreland5557f1c2018-07-02 13:50:23 -07001107 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
Devin Moore53fc99c2020-08-12 08:07:52 -07001108 "readFromParcel", type_params,
Steven Morelandce39c532018-07-11 16:59:50 -07001109 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001110 StatementBlock* read_block = read->GetStatementBlock();
1111 read_block->AddLiteral(
1112 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001113
1114 read_block->AddLiteral(
Jeongik Cha969f11a2020-08-19 13:08:06 +09001115 "[[maybe_unused]] size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
Jeongik Cha95eba572018-11-22 09:14:52 +09001116 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1117 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
Jeongik Cha969f11a2020-08-19 13:08:06 +09001118 "[[maybe_unused]] size_t _aidl_parcelable_size = "
Jeongik Cha8b329982020-09-01 20:59:36 +09001119 "static_cast<size_t>(_aidl_parcelable_raw_size);\n"
1120 "if (_aidl_start_pos > SIZE_MAX - _aidl_parcelable_size) return ::android::BAD_VALUE;\n");
Jeongik Cha95eba572018-11-22 09:14:52 +09001121
Steven Moreland5557f1c2018-07-02 13:50:23 -07001122 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001123 string method = ParcelReadMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001124
1125 read_block->AddStatement(new Assignment(
1126 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
Daniel Norman85aed542019-08-21 12:01:14 -07001127 ParcelReadCastOf(variable->GetType(), typenames,
1128 "&" + variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001129 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001130 read_block->AddLiteral(StringPrintf(
1131 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1132 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1133 " return %s;\n"
1134 "}",
1135 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001136 }
1137 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1138
Steven Morelandce39c532018-07-11 16:59:50 -07001139 unique_ptr<MethodImpl> write{
Devin Moore53fc99c2020-08-12 08:07:52 -07001140 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel", type_params,
Steven Morelandce39c532018-07-11 16:59:50 -07001141 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001142 StatementBlock* write_block = write->GetStatementBlock();
1143 write_block->AddLiteral(
1144 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001145
1146 write_block->AddLiteral(
1147 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1148 "_aidl_parcel->writeInt32(0);");
1149
Steven Moreland5557f1c2018-07-02 13:50:23 -07001150 for (const auto& variable : parcel.GetFields()) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001151 string method = ParcelWriteMethodOf(variable->GetType(), typenames);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001152 write_block->AddStatement(new Assignment(
Daniel Norman85aed542019-08-21 12:01:14 -07001153 kAndroidStatusVarName,
1154 new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1155 ParcelWriteCastOf(variable->GetType(), typenames, variable->GetName()))));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001156 write_block->AddStatement(ReturnOnStatusNotOk());
1157 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001158
1159 write_block->AddLiteral(
1160 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1161 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1162 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1163 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001164 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1165
1166 vector<unique_ptr<Declaration>> file_decls;
1167 file_decls.push_back(std::move(read));
1168 file_decls.push_back(std::move(write));
1169
1170 set<string> includes = {};
Devin Moore2f2077a2020-08-28 11:27:53 -07001171 AddHeaders(parcel, &includes);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001172
1173 return unique_ptr<Document>{
1174 new CppSource{vector<string>(includes.begin(), includes.end()),
1175 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1176}
1177
Daniel Norman0c1bd362019-11-12 23:05:31 -08001178std::string GenerateEnumToString(const AidlTypenames& typenames,
1179 const AidlEnumDeclaration& enum_decl) {
1180 std::ostringstream code;
1181 code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n";
1182 code << " switch(val) {\n";
1183 std::set<std::string> unique_cases;
1184 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1185 std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator);
1186 // Only add a case if its value has not yet been used in the switch
1187 // statement. C++ does not allow multiple cases with the same value, but
1188 // enums does allow this. In this scenario, the first declared
1189 // enumerator with the given value is printed.
1190 if (unique_cases.count(c) == 0) {
1191 unique_cases.insert(c);
1192 code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n";
1193 code << " return \"" << enumerator->GetName() << "\";\n";
1194 }
1195 }
1196 code << " default:\n";
1197 code << " return std::to_string(static_cast<"
1198 << CppNameOf(enum_decl.GetBackingType(), typenames) << ">(val));\n";
1199 code << " }\n";
1200 code << "}\n";
1201 return code.str();
1202}
1203
Daniel Norman85aed542019-08-21 12:01:14 -07001204std::unique_ptr<Document> BuildEnumHeader(const AidlTypenames& typenames,
1205 const AidlEnumDeclaration& enum_decl) {
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001206 std::unique_ptr<Enum> generated_enum{
Daniel Norman85aed542019-08-21 12:01:14 -07001207 new Enum{enum_decl.GetName(), CppNameOf(enum_decl.GetBackingType(), typenames), true}};
1208 for (const auto& enumerator : enum_decl.GetEnumerators()) {
1209 generated_enum->AddValue(
1210 enumerator->GetName(),
1211 enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator));
1212 }
1213
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001214 std::set<std::string> includes = {
1215 "array",
1216 "binder/Enums.h",
1217 "string",
1218 };
Devin Moore2f2077a2020-08-28 11:27:53 -07001219 AddHeaders(enum_decl.GetBackingType(), typenames, &includes);
Daniel Norman85aed542019-08-21 12:01:14 -07001220
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001221 std::vector<std::unique_ptr<Declaration>> decls1;
1222 decls1.push_back(std::move(generated_enum));
1223 decls1.push_back(std::make_unique<LiteralDecl>(GenerateEnumToString(typenames, enum_decl)));
1224
1225 std::vector<std::unique_ptr<Declaration>> decls2;
1226 decls2.push_back(std::make_unique<LiteralDecl>(GenerateEnumValues(enum_decl, {""})));
Daniel Norman0c1bd362019-11-12 23:05:31 -08001227
Daniel Norman85aed542019-08-21 12:01:14 -07001228 return unique_ptr<Document>{
Devin Moore7aaa9cb2020-08-13 14:53:01 -07001229 new CppHeader{vector<string>(includes.begin(), includes.end()),
Jooyung Han7a9aceb2019-12-17 14:18:15 +00001230 Append(NestInNamespaces(std::move(decls1), enum_decl.GetSplitPackage()),
1231 NestInNamespaces(std::move(decls2), {"android", "internal"}))}};
Daniel Norman85aed542019-08-21 12:01:14 -07001232}
1233
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001234bool WriteHeader(const Options& options, const AidlTypenames& typenames,
1235 const AidlInterface& interface, const IoDelegate& io_delegate,
1236 ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001237 unique_ptr<Document> header;
1238 switch (header_type) {
1239 case ClassNames::INTERFACE:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001240 header = BuildInterfaceHeader(typenames, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001241 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001242 break;
1243 case ClassNames::CLIENT:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001244 header = BuildClientHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001245 break;
1246 case ClassNames::SERVER:
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001247 header = BuildServerHeader(typenames, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001248 break;
1249 default:
1250 LOG(FATAL) << "aidl internal error";
1251 }
1252 if (!header) {
1253 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001254 return false;
1255 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001256
Jiyong Park05463732018-08-09 16:03:02 +09001257 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001258 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1259 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001260
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001261 const bool success = code_writer->Close();
1262 if (!success) {
1263 io_delegate.RemovePath(header_path);
1264 }
1265
1266 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001267}
1268
Casey Dahlina834dd42015-09-23 11:52:15 -07001269} // namespace internals
1270
1271using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001272
Jiyong Park74595c12018-07-23 15:22:50 +09001273bool GenerateCppInterface(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001274 const AidlTypenames& typenames, const AidlInterface& interface,
Jiyong Park74595c12018-07-23 15:22:50 +09001275 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001276 auto interface_src = BuildInterfaceSource(typenames, interface, options);
1277 auto client_src = BuildClientSource(typenames, interface, options);
1278 auto server_src = BuildServerSource(typenames, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001279
Christopher Wiley054afbd2015-10-16 17:08:43 -07001280 if (!interface_src || !client_src || !server_src) {
1281 return false;
1282 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001283
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001284 if (!WriteHeader(options, typenames, interface, io_delegate, ClassNames::INTERFACE) ||
1285 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::CLIENT) ||
1286 !WriteHeader(options, typenames, interface, io_delegate, ClassNames::SERVER)) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001287 return false;
1288 }
1289
Jiyong Park74595c12018-07-23 15:22:50 +09001290 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001291 interface_src->Write(writer.get());
1292 client_src->Write(writer.get());
1293 server_src->Write(writer.get());
1294
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001295 const bool success = writer->Close();
1296 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001297 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001298 }
1299
1300 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001301}
1302
Jiyong Park74595c12018-07-23 15:22:50 +09001303bool GenerateCppParcel(const string& output_file, const Options& options,
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001304 const AidlTypenames& typenames, const AidlStructuredParcelable& parcelable,
Jiyong Park74595c12018-07-23 15:22:50 +09001305 const IoDelegate& io_delegate) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001306 auto header = BuildParcelHeader(typenames, parcelable, options);
1307 auto source = BuildParcelSource(typenames, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001308
1309 if (!header || !source) {
1310 return false;
1311 }
1312
Jiyong Park5b7e5322019-04-03 20:05:01 +09001313 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001314 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1315 header->Write(header_writer.get());
Devin Moore53fc99c2020-08-12 08:07:52 -07001316 if (parcelable.IsGeneric()) {
1317 // Need to write all of the source in the header file, not cpp file.
1318 source->Write(header_writer.get());
1319 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001320 CHECK(header_writer->Close());
1321
Steven Moreland81079f92018-07-06 16:15:53 -07001322 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001323 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001324 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1325 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1326 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001327 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001328 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1329 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1330 CHECK(bn_writer->Close());
1331
Jiyong Park74595c12018-07-23 15:22:50 +09001332 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Devin Moore53fc99c2020-08-12 08:07:52 -07001333 if (parcelable.IsGeneric()) {
1334 // Since the type is generic, the source is written in the header file
1335 auto empty_source = unique_ptr<Document>{new CppSource{{}, {}}};
1336 empty_source->Write(source_writer.get());
1337 } else {
1338 source->Write(source_writer.get());
1339 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001340 CHECK(source_writer->Close());
1341
1342 return true;
1343}
1344
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001345bool GenerateCppParcelDeclaration(const std::string& filename, const Options& options,
1346 const AidlParcelable& parcelable, const IoDelegate& io_delegate) {
1347 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1348 *source_writer
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001349 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001350 CHECK(source_writer->Close());
1351
1352 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1353 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
1354 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1355 header_writer->Write("#error TODO(b/111362593) parcelables do not have headers");
1356 CHECK(header_writer->Close());
1357 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
1358 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());
1361 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
1362 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());
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001365
1366 return true;
1367}
1368
Daniel Norman85aed542019-08-21 12:01:14 -07001369bool GenerateCppEnumDeclaration(const std::string& filename, const Options& options,
1370 const AidlTypenames& typenames,
1371 const AidlEnumDeclaration& enum_decl,
1372 const IoDelegate& io_delegate) {
1373 auto header = BuildEnumHeader(typenames, enum_decl);
1374 if (!header) return false;
1375
1376 const string header_path = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::RAW);
1377 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1378 header->Write(header_writer.get());
1379 CHECK(header_writer->Close());
1380
1381 // TODO(b/111362593): no unnecessary files just to have consistent output with interfaces
1382 CodeWriterPtr source_writer = io_delegate.GetCodeWriter(filename);
1383 *source_writer
1384 << "// This file is intentionally left blank as placeholder for enum declaration.\n";
1385 CHECK(source_writer->Close());
1386 const string bp_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::CLIENT);
1387 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1388 bp_writer->Write("#error TODO(b/111362593) enums do not have bp classes");
1389 CHECK(bp_writer->Close());
1390 const string bn_header = options.OutputHeaderDir() + HeaderFile(enum_decl, ClassNames::SERVER);
1391 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1392 bn_writer->Write("#error TODO(b/111362593) enums do not have bn classes");
1393 CHECK(bn_writer->Close());
1394
1395 return true;
1396}
1397
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001398bool GenerateCpp(const string& output_file, const Options& options, const AidlTypenames& typenames,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001399 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1400 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1401 if (parcelable != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001402 return GenerateCppParcel(output_file, options, typenames, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001403 }
1404
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001405 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1406 if (parcelable_decl != nullptr) {
Dan Willemsenc46b65e2019-06-06 10:55:58 -07001407 return GenerateCppParcelDeclaration(output_file, options, *parcelable_decl, io_delegate);
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001408 }
1409
Daniel Norman85aed542019-08-21 12:01:14 -07001410 const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration();
1411 if (enum_decl != nullptr) {
1412 return GenerateCppEnumDeclaration(output_file, options, typenames, *enum_decl, io_delegate);
1413 }
1414
Steven Moreland5557f1c2018-07-02 13:50:23 -07001415 const AidlInterface* interface = defined_type.AsInterface();
1416 if (interface != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +09001417 return GenerateCppInterface(output_file, options, typenames, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001418 }
1419
1420 CHECK(false) << "Unrecognized type sent for cpp generation.";
1421 return false;
1422}
1423
Christopher Wileyf944e792015-09-29 10:00:46 -07001424} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001425} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001426} // namespace android