blob: 9d2a1fb6d77af36a0cf47ada8899518389c92cfc [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";
Christopher Wiley433c8bb2015-11-12 14:20:46 -080066const char kStatusHeader[] = "binder/Status.h";
Christopher Wiley69b44cf2016-05-03 13:43:33 -070067const char kString16Header[] = "utils/String16.h";
Martijn Coenenf1b50782018-02-21 21:06:23 +010068const char kTraceHeader[] = "utils/Trace.h";
Casey Dahlin389781f2015-10-22 13:13:21 -070069const char kStrongPointerHeader[] = "utils/StrongPointer.h";
Jiyong Park75e1a742018-07-04 12:31:23 +090070const char kAndroidBaseMacrosHeader[] = "android-base/macros.h";
Casey Dahlin082f1d12015-09-21 14:06:25 -070071
Christopher Wiley0eb903e2015-10-20 17:07:08 -070072unique_ptr<AstNode> BreakOnStatusNotOk() {
73 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080074 new LiteralExpression(kAndroidStatusVarName), "!=",
75 new LiteralExpression(kAndroidStatusOk)));
Christopher Wiley0eb903e2015-10-20 17:07:08 -070076 ret->OnTrue()->AddLiteral("break");
77 return unique_ptr<AstNode>(ret);
78}
79
Christopher Wiley433c8bb2015-11-12 14:20:46 -080080unique_ptr<AstNode> GotoErrorOnBadStatus() {
81 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080082 new LiteralExpression(kAndroidStatusVarName), "!=",
83 new LiteralExpression(kAndroidStatusOk)));
84 ret->OnTrue()->AddLiteral(StringPrintf("goto %s", kErrorLabel));
Christopher Wiley433c8bb2015-11-12 14:20:46 -080085 return unique_ptr<AstNode>(ret);
86}
87
Steven Moreland5557f1c2018-07-02 13:50:23 -070088unique_ptr<AstNode> ReturnOnStatusNotOk() {
89 IfStatement* ret = new IfStatement(new Comparison(new LiteralExpression(kAndroidStatusVarName),
90 "!=", new LiteralExpression(kAndroidStatusOk)));
91 ret->OnTrue()->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
92 return unique_ptr<AstNode>(ret);
93}
94
Jiyong Park75e1a742018-07-04 12:31:23 +090095ArgList BuildArgList(const TypeNamespace& types, const AidlMethod& method, bool for_declaration,
96 bool type_name_only = false) {
Christopher Wileyad339272015-10-05 19:11:58 -070097 // Build up the argument list for the server method call.
98 vector<string> method_arguments;
99 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
100 string literal;
101 if (for_declaration) {
102 // Method declarations need types, pointers to out params, and variable
103 // names that match the .aidl specification.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800104 const Type* type = a->GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700105
Casey Dahlina2f77c42015-12-01 18:26:02 -0800106 literal = type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700107
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700108 if (a->IsOut()) {
109 literal = literal + "*";
110 } else {
111 // We pass in parameters that are not primitives by const reference.
112 // Arrays of primitives are not primitives.
113 if (!type->IsCppPrimitive() || a->GetType().IsArray()) {
114 literal = "const " + literal + "&";
115 }
116 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900117 if (!type_name_only) {
118 literal += " " + a->GetName();
119 }
Christopher Wileyad339272015-10-05 19:11:58 -0700120 } else {
121 if (a->IsOut()) { literal = "&"; }
122 literal += BuildVarName(*a);
123 }
124 method_arguments.push_back(literal);
125 }
126
Casey Dahlina2f77c42015-12-01 18:26:02 -0800127 const Type* return_type = method.GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700128
Christopher Wileyad339272015-10-05 19:11:58 -0700129 if (return_type != types.VoidType()) {
Christopher Wileyade4b452015-10-10 11:06:03 -0700130 string literal;
Christopher Wileyad339272015-10-05 19:11:58 -0700131 if (for_declaration) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900132 literal = StringPrintf("%s* %s", return_type->CppType().c_str(),
133 type_name_only ? "" : kReturnVarName);
Christopher Wileyad339272015-10-05 19:11:58 -0700134 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700135 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700136 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700137 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700138 }
139
Christopher Wileyade4b452015-10-10 11:06:03 -0700140 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700141}
142
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700143unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700144 const TypeNamespace& types,
145 bool for_interface) {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700146 uint32_t modifiers = 0;
147 if (for_interface) {
148 modifiers |= MethodDecl::IS_VIRTUAL;
149 modifiers |= MethodDecl::IS_PURE_VIRTUAL;
150 } else {
151 modifiers |= MethodDecl::IS_OVERRIDE;
152 }
153
154 return unique_ptr<Declaration>{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800155 new MethodDecl{kBinderStatusLiteral,
Casey Dahlinf4a93112015-10-05 16:58:09 -0700156 method.GetName(),
Christopher Wileyad339272015-10-05 19:11:58 -0700157 BuildArgList(types, method, true /* for method decl */),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700158 modifiers}};
159}
160
Jiyong Park309668e2018-07-28 16:55:44 +0900161unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const TypeNamespace&,
162 const Options& options, bool for_interface) {
163 CHECK(!method.IsUserDefined());
164 if (method.GetName() == kGetInterfaceVersion && options.Version()) {
165 std::ostringstream code;
166 if (for_interface) {
167 code << "virtual ";
168 }
169 code << "int32_t " << kGetInterfaceVersion << "()";
170 if (for_interface) {
171 code << " = 0;\n";
172 } else {
173 code << " override;\n";
174 }
175 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
176 }
177 return nullptr;
178}
179
Steven Morelandf3da0892018-10-05 14:52:01 -0700180std::vector<unique_ptr<Declaration>> NestInNamespaces(vector<unique_ptr<Declaration>> decls,
181 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700182 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700183 for (; it != package.crend(); ++it) {
Steven Morelandf3da0892018-10-05 14:52:01 -0700184 vector<unique_ptr<Declaration>> inner;
185 inner.emplace_back(unique_ptr<Declaration>{new CppNamespace{*it, std::move(decls)}});
186
187 decls = std::move(inner);
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700188 }
Steven Morelandf3da0892018-10-05 14:52:01 -0700189 return decls;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700190}
191
Steven Morelandf3da0892018-10-05 14:52:01 -0700192std::vector<unique_ptr<Declaration>> NestInNamespaces(unique_ptr<Declaration> decl,
193 const vector<string>& package) {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700194 vector<unique_ptr<Declaration>> decls;
195 decls.push_back(std::move(decl));
196 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700197}
198
Steven Moreland1c41e972018-07-09 16:07:00 -0700199bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800200 const Type* cpp_type = a.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700201 if (!cpp_type) { return false; }
202
Casey Dahlina2f77c42015-12-01 18:26:02 -0800203 string type = cpp_type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700204
205 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700206 return true;
207}
208
Steven Moreland5557f1c2018-07-02 13:50:23 -0700209string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
210 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700211 for (size_t i = 1; i < class_name.size(); ++i) {
212 if (isupper(class_name[i])) {
213 class_name.insert(i, "_");
214 ++i;
215 }
216 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700217 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700218 class_name.c_str());
219 for (char& c : ret) {
220 if (c == '.') {
221 c = '_';
222 }
223 c = toupper(c);
224 }
225 return ret;
226}
Christopher Wiley36570f42015-10-08 17:20:11 -0700227
228unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types,
229 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900230 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700231 const string i_name = ClassName(interface, ClassNames::INTERFACE);
232 const string bp_name = ClassName(interface, ClassNames::CLIENT);
233 unique_ptr<MethodImpl> ret{new MethodImpl{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800234 kBinderStatusLiteral, bp_name, method.GetName(),
Christopher Wiley36570f42015-10-08 17:20:11 -0700235 ArgList{BuildArgList(types, method, true /* for method decl */)}}};
236 StatementBlock* b = ret->GetStatementBlock();
237
238 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800239 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700240 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800241 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700242
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800243 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800244 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
245 kAndroidStatusVarName,
246 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800247 // We unconditionally return a Status object.
248 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700249
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900250 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100251 b->AddLiteral(
252 StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
253 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
254 }
255
Jiyong Parkce50e262018-10-29 09:54:20 +0900256 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900257 b->AddLiteral(GenLogBeforeExecute(bp_name, method, false /* isServer */, false /* isNdk */),
258 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900259 }
260
Christopher Wiley8993cb52015-10-21 09:53:24 -0700261 // Add the name of the interface we're hoping to call.
262 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800263 kAndroidStatusVarName,
264 new MethodCall(StringPrintf("%s.writeInterfaceToken",
265 kDataVarName),
266 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800267 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700268
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700269 for (const auto& a: method.GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800270 const Type* type = a->GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700271 string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Casey Dahlin389781f2015-10-22 13:13:21 -0700272 var_name = type->WriteCast(var_name);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700273
274 if (a->IsIn()) {
275 // Serialization looks roughly like:
276 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
277 // if (_aidl_ret_status != ::android::OK) { goto error; }
278 const string& method = type->WriteToParcelMethod();
279 b->AddStatement(new Assignment(
280 kAndroidStatusVarName,
281 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
282 ArgList(var_name))));
283 b->AddStatement(GotoErrorOnBadStatus());
284 } else if (a->IsOut() && a->GetType().IsArray()) {
285 // Special case, the length of the out array is written into the parcel.
286 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
287 // if (_aidl_ret_status != ::android::OK) { goto error; }
288 b->AddStatement(new Assignment(
289 kAndroidStatusVarName,
290 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName),
291 ArgList(var_name))));
292 b->AddStatement(GotoErrorOnBadStatus());
293 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700294 }
295
296 // Invoke the transaction on the remote binder and confirm status.
Jeongik Chab5d962f2018-11-17 09:12:28 +0900297 string transaction_code = GetTransactionIdFor(method);
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700298
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800299 vector<string> args = {transaction_code, kDataVarName,
300 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700301
Steven Morelandacd53472018-12-14 10:17:26 -0800302 if (method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800303 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700304 }
305
Christopher Wiley36570f42015-10-08 17:20:11 -0700306 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800307 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700308 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700309 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900310
311 // If the method is not implemented in the remote side, try to call the
312 // default implementation, if provided.
313 vector<string> arg_names;
314 for (const auto& a : method.GetArguments()) {
315 arg_names.emplace_back(a->GetName());
316 }
317 if (method.GetType().GetLanguageType<Type>() != types.VoidType()) {
318 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.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800350 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700351 if (return_type != types.VoidType()) {
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700352 const string& method_call = return_type->ReadFromParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700353 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800354 kAndroidStatusVarName,
355 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
356 method_call.c_str()),
357 ArgList(kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800358 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700359 }
360
361 for (const AidlArgument* a : method.GetOutArguments()) {
362 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800363 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
364 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Casey Dahlinb0966612015-10-19 16:35:26 -0700365 string method =
Casey Dahlina2f77c42015-12-01 18:26:02 -0800366 a->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700367
Christopher Wiley36570f42015-10-08 17:20:11 -0700368 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800369 kAndroidStatusVarName,
370 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
371 method.c_str()),
372 ArgList(a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800373 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700374 }
375
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800376 // If we've gotten to here, one of two things is true:
377 // 1) We've read some bad status_t
378 // 2) We've only read status_t == OK and there was no exception in the
379 // response.
380 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800381 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800382 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800383 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
384 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100385
Jiyong Parkce50e262018-10-29 09:54:20 +0900386 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900387 b->AddLiteral(GenLogAfterExecute(bp_name, interface, method, kStatusVarName, kReturnVarName,
388 false /* isServer */, false /* isNdk */),
Jeongik Cha46375122018-12-21 18:41:21 +0900389 false /* no semicolon */);
Jiyong Parkce50e262018-10-29 09:54:20 +0900390 }
391
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800392 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700393
394 return unique_ptr<Declaration>(ret.release());
395}
396
Jiyong Park309668e2018-07-28 16:55:44 +0900397unique_ptr<Declaration> DefineClientMetaTransaction(const TypeNamespace&,
398 const AidlInterface& interface,
399 const AidlMethod& method,
400 const Options& options) {
401 CHECK(!method.IsUserDefined());
402 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
403 const string iface = ClassName(interface, ClassNames::INTERFACE);
404 const string proxy = ClassName(interface, ClassNames::CLIENT);
405 // Note: race condition can happen here, but no locking is required
406 // because 1) writing an interger is atomic and 2) this transaction
407 // will always return the same value, i.e., competing threads will
408 // give write the same value to cached_version_.
409 std::ostringstream code;
410 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900411 << " if (cached_version_ == -1) {\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900412 << " ::android::Parcel data;\n"
413 << " ::android::Parcel reply;\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900414 << " data.writeInterfaceToken(getInterfaceDescriptor());\n"
Jeongik Chab5d962f2018-11-17 09:12:28 +0900415 << " ::android::status_t err = remote()->transact(" << GetTransactionIdFor(method)
416 << ", data, &reply);\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900417 << " if (err == ::android::OK) {\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900418 << " ::android::binder::Status _aidl_status;\n"
419 << " err = _aidl_status.readFromParcel(reply);\n"
420 << " if (err == ::android::OK && _aidl_status.isOk()) {\n"
421 << " cached_version_ = reply.readInt32();\n"
422 << " }\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900423 << " }\n"
424 << " }\n"
425 << " return cached_version_;\n"
426 << "}\n";
427 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
428 }
429 return nullptr;
430}
431
Christopher Wiley36570f42015-10-08 17:20:11 -0700432} // namespace
433
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900434unique_ptr<Document> BuildClientSource(const TypeNamespace& types, const AidlInterface& interface,
435 const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700436 vector<string> include_list = {
437 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900438 kParcelHeader,
439 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700440 };
Jiyong Parkce50e262018-10-29 09:54:20 +0900441 if (options.GenLog()) {
442 include_list.emplace_back("chrono");
443 include_list.emplace_back("functional");
444 include_list.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900445 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700446 vector<unique_ptr<Declaration>> file_decls;
447
448 // The constructor just passes the IBinder instance up to the super
449 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700450 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700451 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700452 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800453 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
454 kImplVarName)},
455 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700456
Jiyong Parkce50e262018-10-29 09:54:20 +0900457 if (options.GenLog()) {
458 string code;
459 ClassName(interface, ClassNames::CLIENT);
460 CodeWriterPtr writer = CodeWriter::ForString(&code);
461 (*writer) << "std::function<void(const Json::Value&)> "
462 << ClassName(interface, ClassNames::CLIENT) << "::logFunc;\n";
463 writer->Close();
464 file_decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
465 }
466
Christopher Wiley36570f42015-10-08 17:20:11 -0700467 // Clients define a method per transaction.
468 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900469 unique_ptr<Declaration> m;
470 if (method->IsUserDefined()) {
471 m = DefineClientTransaction(types, interface, *method, options);
472 } else {
473 m = DefineClientMetaTransaction(types, interface, *method, options);
474 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700475 if (!m) { return nullptr; }
476 file_decls.push_back(std::move(m));
477 }
478 return unique_ptr<Document>{new CppSource{
479 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700480 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700481}
482
Christopher Wileyad339272015-10-05 19:11:58 -0700483namespace {
484
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900485bool HandleServerTransaction(const TypeNamespace& types, const AidlInterface& interface,
486 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700487 // Declare all the parameters now. In the common case, we expect no errors
488 // in serialization.
489 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700490 if (!DeclareLocalVariable(*a, b)) {
491 return false;
492 }
Christopher Wileyad339272015-10-05 19:11:58 -0700493 }
494
495 // Declare a variable to hold the return value.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800496 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700497 if (return_type != types.VoidType()) {
498 b->AddLiteral(StringPrintf(
Casey Dahlina2f77c42015-12-01 18:26:02 -0800499 "%s %s", return_type->CppType().c_str(),
Casey Dahlinb0966612015-10-19 16:35:26 -0700500 kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700501 }
502
Christopher Wiley8993cb52015-10-21 09:53:24 -0700503 // Check that the client is calling the correct interface.
504 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800505 new MethodCall(StringPrintf("%s.checkInterface",
506 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700507 true /* invert the check */);
508 b->AddStatement(interface_check);
509 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800510 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700511 interface_check->OnTrue()->AddLiteral("break");
512
Christopher Wileyad339272015-10-05 19:11:58 -0700513 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700514 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700515 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800516 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
517 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800518 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700519 const string& readMethod = type->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700520
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700521 if (a->IsIn()) {
522 b->AddStatement(new Assignment{
523 kAndroidStatusVarName,
524 new MethodCall{string(kDataVarName) + "." + readMethod,
525 "&" + BuildVarName(*a)}});
526 b->AddStatement(BreakOnStatusNotOk());
527 } else if (a->IsOut() && a->GetType().IsArray()) {
528 // Special case, the length of the out array is written into the parcel.
529 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
530 // if (_aidl_ret_status != ::android::OK) { break; }
531 b->AddStatement(new Assignment{
532 kAndroidStatusVarName,
533 new MethodCall{string(kDataVarName) + ".resizeOutVector",
534 "&" + BuildVarName(*a)}});
535 b->AddStatement(BreakOnStatusNotOk());
536 }
Christopher Wileyad339272015-10-05 19:11:58 -0700537 }
538
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900539 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100540 b->AddStatement(new Statement(new MethodCall("atrace_begin",
541 ArgList{{"ATRACE_TAG_AIDL",
542 StringPrintf("\"%s::%s::cppServer\"",
543 interface.GetName().c_str(),
544 method.GetName().c_str())}})));
545 }
Jeongik Cha46375122018-12-21 18:41:21 +0900546 const string bn_name = ClassName(interface, ClassNames::SERVER);
547 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900548 b->AddLiteral(GenLogBeforeExecute(bn_name, method, true /* isServer */, false /* isNdk */),
549 false);
Jeongik Cha46375122018-12-21 18:41:21 +0900550 }
Christopher Wileyad339272015-10-05 19:11:58 -0700551 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800552 vector<unique_ptr<AstNode>> status_args;
553 status_args.emplace_back(new MethodCall(
Christopher Wileyad339272015-10-05 19:11:58 -0700554 method.GetName(),
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800555 BuildArgList(types, method, false /* not for method decl */)));
556 b->AddStatement(new Statement(new MethodCall(
557 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
558 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700559
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900560 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100561 b->AddStatement(new Statement(new MethodCall("atrace_end",
562 "ATRACE_TAG_AIDL")));
563 }
564
Jeongik Chab34800b2019-03-08 14:36:58 +0900565 if (options.GenLog()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900566 b->AddLiteral(GenLogAfterExecute(bn_name, interface, method, kStatusVarName, kReturnVarName,
567 true /* isServer */, false /* isNdk */),
568 false);
Jeongik Chab34800b2019-03-08 14:36:58 +0900569 }
570
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800571 // Write exceptions during transaction handling to parcel.
572 if (!method.IsOneway()) {
573 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800574 kAndroidStatusVarName,
575 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800576 b->AddStatement(BreakOnStatusNotOk());
577 IfStatement* exception_check = new IfStatement(
578 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
579 b->AddStatement(exception_check);
580 exception_check->OnTrue()->AddLiteral("break");
581 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700582
Christopher Wiley36570f42015-10-08 17:20:11 -0700583 // If we have a return value, write it first.
584 if (return_type != types.VoidType()) {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700585 string writeMethod =
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800586 string(kReplyVarName) + "->" +
Casey Dahlina2f77c42015-12-01 18:26:02 -0800587 return_type->WriteToParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700588 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800589 kAndroidStatusVarName, new MethodCall{writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700590 ArgList{return_type->WriteCast(kReturnVarName)}}});
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700591 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700592 }
Christopher Wileyad339272015-10-05 19:11:58 -0700593 // Write each out parameter to the reply parcel.
594 for (const AidlArgument* a : method.GetOutArguments()) {
595 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800596 // _aidl_ret_status = data.WriteInt32(out_param_name);
597 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800598 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700599 const string& writeMethod = type->WriteToParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700600
Christopher Wileyad339272015-10-05 19:11:58 -0700601 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800602 kAndroidStatusVarName,
603 new MethodCall{string(kReplyVarName) + "->" + writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700604 type->WriteCast(BuildVarName(*a))}});
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700605 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700606 }
607
608 return true;
609}
610
Jiyong Park309668e2018-07-28 16:55:44 +0900611bool HandleServerMetaTransaction(const TypeNamespace&, const AidlInterface& interface,
612 const AidlMethod& method, const Options& options,
613 StatementBlock* b) {
614 CHECK(!method.IsUserDefined());
615
616 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
617 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900618 code << "_aidl_data.checkInterface(this);\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900619 << "_aidl_reply->writeNoException();\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900620 << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
Jiyong Park309668e2018-07-28 16:55:44 +0900621 << "::VERSION)";
622 b->AddLiteral(code.str());
623 return true;
624 }
625 return false;
626}
627
Christopher Wileyad339272015-10-05 19:11:58 -0700628} // namespace
629
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900630unique_ptr<Document> BuildServerSource(const TypeNamespace& types, const AidlInterface& interface,
631 const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700632 const string bn_name = ClassName(interface, ClassNames::SERVER);
633 vector<string> include_list{
634 HeaderFile(interface, ClassNames::SERVER, false),
635 kParcelHeader
636 };
Jeongik Cha46375122018-12-21 18:41:21 +0900637 if (options.GenLog()) {
638 include_list.emplace_back("chrono");
639 include_list.emplace_back("functional");
640 include_list.emplace_back("json/value.h");
Jeongik Cha46375122018-12-21 18:41:21 +0900641 }
Christopher Wileyad339272015-10-05 19:11:58 -0700642 unique_ptr<MethodImpl> on_transact{new MethodImpl{
643 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800644 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
645 StringPrintf("const %s& %s", kAndroidParcelLiteral,
646 kDataVarName),
647 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
648 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700649 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700650
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800651 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700652 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800653 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
654 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700655
Christopher Wileyad339272015-10-05 19:11:58 -0700656 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800657 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700658 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700659
660 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700661 for (const auto& method : interface.GetMethods()) {
Jeongik Chab5d962f2018-11-17 09:12:28 +0900662 StatementBlock* b = s->AddCase(GetTransactionIdFor(*method));
Christopher Wileyad339272015-10-05 19:11:58 -0700663 if (!b) { return nullptr; }
664
Jiyong Park309668e2018-07-28 16:55:44 +0900665 bool success = false;
666 if (method->IsUserDefined()) {
667 success = HandleServerTransaction(types, interface, *method, options, b);
668 } else {
669 success = HandleServerMetaTransaction(types, interface, *method, options, b);
670 }
671 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900672 return nullptr;
673 }
Christopher Wileyad339272015-10-05 19:11:58 -0700674 }
675
676 // The switch statement has a default case which defers to the super class.
677 // The superclass handles a few pre-defined transactions.
678 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800679 b->AddLiteral(StringPrintf(
680 "%s = ::android::BBinder::onTransact(%s, %s, "
681 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
682 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700683
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800684 // If we saw a null reference, we can map that to an appropriate exception.
685 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800686 new LiteralExpression(string(kAndroidStatusVarName) +
687 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800688 on_transact->GetStatementBlock()->AddStatement(null_check);
689 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800690 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800691 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800692 ".writeToParcel(%s)",
693 kBinderStatusLiteral, kBinderStatusLiteral,
694 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800695
Christopher Wileyad339272015-10-05 19:11:58 -0700696 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800697 on_transact->GetStatementBlock()->AddLiteral(
698 StringPrintf("return %s", kAndroidStatusVarName));
Jeongik Cha46375122018-12-21 18:41:21 +0900699 vector<unique_ptr<Declaration>> decls;
700 decls.push_back(std::move(on_transact));
Christopher Wileyad339272015-10-05 19:11:58 -0700701
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900702 if (options.Version() > 0) {
703 std::ostringstream code;
704 code << "int32_t " << bn_name << "::" << kGetInterfaceVersion << "() {\n"
705 << " return " << ClassName(interface, ClassNames::INTERFACE) << "::VERSION;\n"
706 << "}\n";
707 decls.emplace_back(new LiteralDecl(code.str()));
708 }
709
Jeongik Cha46375122018-12-21 18:41:21 +0900710 if (options.GenLog()) {
711 string code;
712 ClassName(interface, ClassNames::SERVER);
713 CodeWriterPtr writer = CodeWriter::ForString(&code);
714 (*writer) << "std::function<void(const Json::Value&)> "
715 << ClassName(interface, ClassNames::SERVER) << "::logFunc;\n";
716 writer->Close();
717 decls.push_back(unique_ptr<Declaration>(new LiteralDecl(code)));
718 }
719 return unique_ptr<Document>{
720 new CppSource{include_list, NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700721}
722
Jiyong Park75e1a742018-07-04 12:31:23 +0900723unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& types,
Jiyong Park309668e2018-07-28 16:55:44 +0900724 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700725 vector<string> include_list{
Jiyong Park5b7e5322019-04-03 20:05:01 +0900726 HeaderFile(interface, ClassNames::RAW, false),
Christopher Wiley054afbd2015-10-16 17:08:43 -0700727 HeaderFile(interface, ClassNames::CLIENT, false),
728 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700729
Christopher Wiley054afbd2015-10-16 17:08:43 -0700730 string fq_name = ClassName(interface, ClassNames::INTERFACE);
731 if (!interface.GetPackage().empty()) {
732 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700733 }
734
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700735 vector<unique_ptr<Declaration>> decls;
736
Christopher Wiley11a9d792016-02-24 17:20:33 -0800737 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700738 "IMPLEMENT_META_INTERFACE",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700739 ArgList{vector<string>{ClassName(interface, ClassNames::BASE),
Christopher Wileyade4b452015-10-10 11:06:03 -0700740 '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700741 decls.push_back(std::move(meta_if));
742
Steven Moreland693640b2018-07-19 13:46:27 -0700743 for (const auto& constant : interface.GetConstantDeclarations()) {
744 const AidlConstantValue& value = constant->GetValue();
745 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
746
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700747 std::string cppType = constant->GetType().GetLanguageType<Type>()->CppType();
748
749 unique_ptr<MethodImpl> getter(new MethodImpl("const " + cppType + "&",
750 ClassName(interface, ClassNames::INTERFACE),
751 constant->GetName(), {}));
Steven Moreland860b1942018-08-16 14:59:28 -0700752 getter->GetStatementBlock()->AddLiteral(
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700753 StringPrintf("static const %s value(%s)", cppType.c_str(),
Steven Moreland860b1942018-08-16 14:59:28 -0700754 constant->ValueString(ConstantValueDecorator).c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700755 getter->GetStatementBlock()->AddLiteral("return value");
756 decls.push_back(std::move(getter));
757 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700758
Jiyong Park75e1a742018-07-04 12:31:23 +0900759 // Implement the default impl class.
760 // onAsBinder returns nullptr as this interface is not associated with a
761 // real binder.
762 const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL));
763 decls.emplace_back(
764 new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n"
765 " return nullptr;\n"
766 "}\n",
767 default_impl.c_str())));
768 // Each interface method by default returns UNKNOWN_TRANSACTION with is
769 // the same status that is returned by transact() when the method is
770 // not implemented in the server side. In other words, these default
771 // methods do nothing; they only exist to aid making a real default
772 // impl class without having to override all methods in an interface.
773 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900774 if (method->IsUserDefined()) {
775 std::ostringstream code;
776 code << "::android::binder::Status " << default_impl << "::" << method->GetName()
777 << BuildArgList(types, *method, true, true).ToString() << " {\n"
778 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
779 << "}\n";
780 decls.emplace_back(new LiteralDecl(code.str()));
781 } else {
782 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
783 std::ostringstream code;
784 code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n"
785 << " return 0;\n"
786 << "}\n";
787 decls.emplace_back(new LiteralDecl(code.str()));
788 }
789 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900790 }
791
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700792 return unique_ptr<Document>{new CppSource{
793 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700794 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700795}
796
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900797unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900798 const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700799 const string i_name = ClassName(interface, ClassNames::INTERFACE);
800 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700801
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900802 vector<string> includes = {kIBinderHeader, kIInterfaceHeader, "utils/Errors.h",
Jiyong Park5b7e5322019-04-03 20:05:01 +0900803 HeaderFile(interface, ClassNames::RAW, false)};
Jiyong Parkce50e262018-10-29 09:54:20 +0900804
Christopher Wileyb23149d2015-10-14 13:52:21 -0700805 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
806 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800807 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
808 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700809 ConstructorDecl::IS_EXPLICIT
810 }};
811 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
812 "~" + bp_name,
813 ArgList{},
814 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700815
Christopher Wileyf944e792015-09-29 10:00:46 -0700816 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700817 publics.push_back(std::move(constructor));
818 publics.push_back(std::move(destructor));
819
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700820 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900821 if (method->IsUserDefined()) {
822 publics.push_back(BuildMethodDecl(*method, types, false));
823 } else {
824 publics.push_back(BuildMetaMethodDecl(*method, types, options, false));
825 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700826 }
827
Jiyong Parkce50e262018-10-29 09:54:20 +0900828 if (options.GenLog()) {
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900829 includes.emplace_back("chrono"); // for std::chrono::steady_clock
830 includes.emplace_back("functional"); // for std::function
831 includes.emplace_back("json/value.h");
Jiyong Parkce50e262018-10-29 09:54:20 +0900832 publics.emplace_back(
833 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
834 }
835
Jiyong Park309668e2018-07-28 16:55:44 +0900836 vector<unique_ptr<Declaration>> privates;
837
838 if (options.Version() > 0) {
839 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
840 }
841
842 unique_ptr<ClassDecl> bp_class{new ClassDecl{
843 bp_name,
844 "::android::BpInterface<" + i_name + ">",
845 std::move(publics),
846 std::move(privates),
847 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700848
Jiyong Parkce50e262018-10-29 09:54:20 +0900849 return unique_ptr<Document>{
Jiyong Parkb064cbb2018-11-06 02:47:18 +0900850 new CppHeader{BuildHeaderGuard(interface, ClassNames::CLIENT), includes,
Jiyong Parkce50e262018-10-29 09:54:20 +0900851 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700852}
853
Christopher Wileyf59c4992015-10-08 13:12:44 -0700854unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */,
Jeongik Cha46375122018-12-21 18:41:21 +0900855 const AidlInterface& interface, const Options& options) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700856 const string i_name = ClassName(interface, ClassNames::INTERFACE);
857 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700858
Christopher Wileyfd51d602015-10-14 13:04:48 -0700859 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700860 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800861 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
862 StringPrintf("const %s& %s", kAndroidParcelLiteral,
863 kDataVarName),
864 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
Jiyong Park8533bd02018-10-29 21:31:18 +0900865 StringPrintf("uint32_t %s", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700866 MethodDecl::IS_OVERRIDE
867 }};
Jiyong Park5b7e5322019-04-03 20:05:01 +0900868 vector<string> includes = {"binder/IInterface.h", HeaderFile(interface, ClassNames::RAW, false)};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700869
Jeongik Cha46375122018-12-21 18:41:21 +0900870 vector<unique_ptr<Declaration>> publics;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700871 publics.push_back(std::move(on_transact));
872
Jiyong Park22d4cfc2019-04-09 14:23:41 +0900873 if (options.Version() > 0) {
874 std::ostringstream code;
875 code << "int32_t " << kGetInterfaceVersion << "() final override;\n";
876 publics.emplace_back(new LiteralDecl(code.str()));
877 }
878
Jeongik Cha46375122018-12-21 18:41:21 +0900879 if (options.GenLog()) {
880 includes.emplace_back("chrono"); // for std::chrono::steady_clock
881 includes.emplace_back("functional"); // for std::function
882 includes.emplace_back("json/value.h");
883 publics.emplace_back(
884 new LiteralDecl{"static std::function<void(const Json::Value&)> logFunc;\n"});
885 }
Christopher Wileyf944e792015-09-29 10:00:46 -0700886 unique_ptr<ClassDecl> bn_class{
887 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800888 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700889 std::move(publics),
890 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700891 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700892
Jeongik Cha46375122018-12-21 18:41:21 +0900893 return unique_ptr<Document>{
894 new CppHeader{BuildHeaderGuard(interface, ClassNames::SERVER), includes,
895 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700896}
897
Christopher Wileye3550c62015-09-29 13:26:10 -0700898unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900899 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900900 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700901
902 for (const auto& method : interface.GetMethods()) {
903 for (const auto& argument : method->GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800904 const Type* type = argument->GetType().GetLanguageType<Type>();
905 type->GetHeaders(&includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700906 }
907
Casey Dahlina2f77c42015-12-01 18:26:02 -0800908 const Type* return_type = method->GetType().GetLanguageType<Type>();
Jiyong Parkb034bf02018-07-30 17:44:33 +0900909 if (return_type != nullptr) {
910 return_type->GetHeaders(&includes);
911 }
Casey Dahlince776cf2015-10-15 18:45:54 -0700912 }
913
Jiyong Park75e1a742018-07-04 12:31:23 +0900914 const string i_name = ClassName(interface, ClassNames::INTERFACE);
915 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800916 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700917 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700918 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700919
Jiyong Park309668e2018-07-28 16:55:44 +0900920 if (options.Version() > 0) {
921 std::ostringstream code;
922 code << "const int32_t VERSION = " << options.Version() << ";\n";
923
924 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
925 }
926
Steven Moreland693640b2018-07-19 13:46:27 -0700927 std::vector<std::unique_ptr<Declaration>> string_constants;
928 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t"}};
929 for (const auto& constant : interface.GetConstantDeclarations()) {
930 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800931
Steven Moreland693640b2018-07-19 13:46:27 -0700932 switch (value.GetType()) {
933 case AidlConstantValue::Type::STRING: {
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700934 std::string cppType = constant->GetType().GetLanguageType<Type>()->CppType();
935 unique_ptr<Declaration> getter(new MethodDecl("const " + cppType + "&", constant->GetName(),
936 {}, MethodDecl::IS_STATIC));
Steven Moreland693640b2018-07-19 13:46:27 -0700937 string_constants.push_back(std::move(getter));
938 break;
939 }
Steven Moreland25294322018-08-07 18:13:55 -0700940 case AidlConstantValue::Type::INTEGRAL:
941 case AidlConstantValue::Type::HEXIDECIMAL: {
Steven Moreland860b1942018-08-16 14:59:28 -0700942 int_constant_enum->AddValue(constant->GetName(),
943 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -0700944 break;
945 }
946 default: {
947 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
948 }
949 }
950 }
951 if (int_constant_enum->HasValues()) {
952 if_class->AddPublic(std::move(int_constant_enum));
953 }
954 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700955 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700956
957 for (auto& string_constant : string_constants) {
958 if_class->AddPublic(std::move(string_constant));
959 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700960 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100961
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900962 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100963 includes.insert(kTraceHeader);
964 }
965
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700966 if (!interface.GetMethods().empty()) {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700967 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900968 if (method->IsUserDefined()) {
969 // Each method gets an enum entry and pure virtual declaration.
970 if_class->AddPublic(BuildMethodDecl(*method, types, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900971 } else {
972 if_class->AddPublic(BuildMetaMethodDecl(*method, types, options, true));
Jiyong Park309668e2018-07-28 16:55:44 +0900973 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700974 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700975 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700976
Jiyong Park75e1a742018-07-04 12:31:23 +0900977 vector<unique_ptr<Declaration>> decls;
978 decls.emplace_back(std::move(if_class));
979
980 // Base class for the default implementation.
981 vector<string> method_decls;
982 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900983 if (method->IsUserDefined()) {
984 method_decls.emplace_back(BuildMethodDecl(*method, types, false)->ToString());
985 } else {
986 method_decls.emplace_back(BuildMetaMethodDecl(*method, types, options, false)->ToString());
987 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900988 }
Jiyong Park309668e2018-07-28 16:55:44 +0900989
Jiyong Park75e1a742018-07-04 12:31:23 +0900990 decls.emplace_back(new LiteralDecl(
991 android::base::StringPrintf("class %s : public %s {\n"
992 "public:\n"
993 " ::android::IBinder* onAsBinder() override;\n"
994 " %s\n"
995 "};\n",
996 ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(),
997 i_name.c_str(), Join(method_decls, " ").c_str())));
998
999 return unique_ptr<Document>{
1000 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
1001 vector<string>(includes.begin(), includes.end()),
1002 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001003}
1004
Steven Moreland5557f1c2018-07-02 13:50:23 -07001005std::unique_ptr<Document> BuildParcelHeader(const TypeNamespace& /*types*/,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001006 const AidlStructuredParcelable& parcel,
1007 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001008 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
1009
1010 set<string> includes = {kStatusHeader, kParcelHeader};
1011 for (const auto& variable : parcel.GetFields()) {
1012 const Type* type = variable->GetType().GetLanguageType<Type>();
1013 type->GetHeaders(&includes);
1014 }
1015
1016 for (const auto& variable : parcel.GetFields()) {
1017 const Type* type = variable->GetType().GetLanguageType<Type>();
1018
Steven Moreland9ea10e32018-07-19 15:26:09 -07001019 std::ostringstream out;
1020 out << type->CppType().c_str() << " " << variable->GetName().c_str();
Steven Moreland25294322018-08-07 18:13:55 -07001021 if (variable->GetDefaultValue()) {
Steven Moreland860b1942018-08-16 14:59:28 -07001022 out << " = " << type->CppType().c_str() << "("
1023 << variable->ValueString(ConstantValueDecorator) << ")";
Steven Moreland9ea10e32018-07-19 15:26:09 -07001024 }
1025 out << ";\n";
1026
1027 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001028 }
1029
1030 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001031 ArgList("const ::android::Parcel* _aidl_parcel"),
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001032 MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001033 parcel_class->AddPublic(std::move(read));
Jeongik Chaa2ada0c2018-11-17 15:11:45 +09001034 unique_ptr<MethodDecl> write(new MethodDecl(
1035 kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
1036 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001037 parcel_class->AddPublic(std::move(write));
1038
1039 return unique_ptr<Document>{new CppHeader{
1040 BuildHeaderGuard(parcel, ClassNames::BASE), vector<string>(includes.begin(), includes.end()),
1041 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1042}
Steven Moreland1c41e972018-07-09 16:07:00 -07001043std::unique_ptr<Document> BuildParcelSource(const TypeNamespace& /*types*/,
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001044 const AidlStructuredParcelable& parcel,
1045 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001046 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1047 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001048 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001049 StatementBlock* read_block = read->GetStatementBlock();
1050 read_block->AddLiteral(
1051 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001052
1053 read_block->AddLiteral(
1054 "size_t _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1055 "int32_t _aidl_parcelable_raw_size = _aidl_parcel->readInt32();\n"
1056 "if (_aidl_parcelable_raw_size < 0) return ::android::BAD_VALUE;\n"
1057 "size_t _aidl_parcelable_size = static_cast<size_t>(_aidl_parcelable_raw_size);\n");
1058
Steven Moreland5557f1c2018-07-02 13:50:23 -07001059 for (const auto& variable : parcel.GetFields()) {
1060 string method = variable->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
1061
1062 read_block->AddStatement(new Assignment(
1063 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1064 ArgList("&" + variable->GetName()))));
1065 read_block->AddStatement(ReturnOnStatusNotOk());
Jeongik Cha95eba572018-11-22 09:14:52 +09001066 read_block->AddLiteral(StringPrintf(
1067 "if (_aidl_parcel->dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) {\n"
1068 " _aidl_parcel->setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
1069 " return %s;\n"
1070 "}",
1071 kAndroidStatusVarName));
Steven Moreland5557f1c2018-07-02 13:50:23 -07001072 }
1073 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1074
Steven Morelandce39c532018-07-11 16:59:50 -07001075 unique_ptr<MethodImpl> write{
1076 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1077 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001078 StatementBlock* write_block = write->GetStatementBlock();
1079 write_block->AddLiteral(
1080 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
Jeongik Cha95eba572018-11-22 09:14:52 +09001081
1082 write_block->AddLiteral(
1083 "auto _aidl_start_pos = _aidl_parcel->dataPosition();\n"
1084 "_aidl_parcel->writeInt32(0);");
1085
Steven Moreland5557f1c2018-07-02 13:50:23 -07001086 for (const auto& variable : parcel.GetFields()) {
1087 string method = variable->GetType().GetLanguageType<Type>()->WriteToParcelMethod();
1088
1089 write_block->AddStatement(new Assignment(
1090 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1091 ArgList(variable->GetName()))));
1092 write_block->AddStatement(ReturnOnStatusNotOk());
1093 }
Jeongik Cha95eba572018-11-22 09:14:52 +09001094
1095 write_block->AddLiteral(
1096 "auto _aidl_end_pos = _aidl_parcel->dataPosition();\n"
1097 "_aidl_parcel->setDataPosition(_aidl_start_pos);\n"
1098 "_aidl_parcel->writeInt32(_aidl_end_pos - _aidl_start_pos);\n"
1099 "_aidl_parcel->setDataPosition(_aidl_end_pos);");
Steven Moreland5557f1c2018-07-02 13:50:23 -07001100 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1101
1102 vector<unique_ptr<Declaration>> file_decls;
1103 file_decls.push_back(std::move(read));
1104 file_decls.push_back(std::move(write));
1105
1106 set<string> includes = {};
1107 parcel.GetLanguageType<Type>()->GetHeaders(&includes);
1108
1109 return unique_ptr<Document>{
1110 new CppSource{vector<string>(includes.begin(), includes.end()),
1111 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1112}
1113
Jiyong Park74595c12018-07-23 15:22:50 +09001114bool WriteHeader(const Options& options, const TypeNamespace& types, const AidlInterface& interface,
1115 const IoDelegate& io_delegate, ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001116 unique_ptr<Document> header;
1117 switch (header_type) {
1118 case ClassNames::INTERFACE:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001119 header = BuildInterfaceHeader(types, interface, options);
Jiyong Park5b7e5322019-04-03 20:05:01 +09001120 header_type = ClassNames::RAW;
Christopher Wiley054afbd2015-10-16 17:08:43 -07001121 break;
1122 case ClassNames::CLIENT:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001123 header = BuildClientHeader(types, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001124 break;
1125 case ClassNames::SERVER:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001126 header = BuildServerHeader(types, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001127 break;
1128 default:
1129 LOG(FATAL) << "aidl internal error";
1130 }
1131 if (!header) {
1132 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001133 return false;
1134 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001135
Jiyong Park05463732018-08-09 16:03:02 +09001136 const string header_path = options.OutputHeaderDir() + HeaderFile(interface, header_type);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001137 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1138 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001139
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001140 const bool success = code_writer->Close();
1141 if (!success) {
1142 io_delegate.RemovePath(header_path);
1143 }
1144
1145 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001146}
1147
Casey Dahlina834dd42015-09-23 11:52:15 -07001148} // namespace internals
1149
1150using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001151
Jiyong Park74595c12018-07-23 15:22:50 +09001152bool GenerateCppInterface(const string& output_file, const Options& options,
1153 const TypeNamespace& types, const AidlInterface& interface,
1154 const IoDelegate& io_delegate) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001155 auto interface_src = BuildInterfaceSource(types, interface, options);
1156 auto client_src = BuildClientSource(types, interface, options);
1157 auto server_src = BuildServerSource(types, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001158
Christopher Wiley054afbd2015-10-16 17:08:43 -07001159 if (!interface_src || !client_src || !server_src) {
1160 return false;
1161 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001162
Christopher Wiley054afbd2015-10-16 17:08:43 -07001163 if (!WriteHeader(options, types, interface, io_delegate,
1164 ClassNames::INTERFACE) ||
1165 !WriteHeader(options, types, interface, io_delegate,
1166 ClassNames::CLIENT) ||
1167 !WriteHeader(options, types, interface, io_delegate,
1168 ClassNames::SERVER)) {
1169 return false;
1170 }
1171
Jiyong Park74595c12018-07-23 15:22:50 +09001172 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001173 interface_src->Write(writer.get());
1174 client_src->Write(writer.get());
1175 server_src->Write(writer.get());
1176
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001177 const bool success = writer->Close();
1178 if (!success) {
Steven Morelandc209cab2018-08-27 01:25:21 -07001179 io_delegate.RemovePath(output_file);
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001180 }
1181
1182 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001183}
1184
Jiyong Park74595c12018-07-23 15:22:50 +09001185bool GenerateCppParcel(const string& output_file, const Options& options,
1186 const cpp::TypeNamespace& types, const AidlStructuredParcelable& parcelable,
1187 const IoDelegate& io_delegate) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001188 auto header = BuildParcelHeader(types, parcelable, options);
1189 auto source = BuildParcelSource(types, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001190
1191 if (!header || !source) {
1192 return false;
1193 }
1194
Jiyong Park5b7e5322019-04-03 20:05:01 +09001195 const string header_path = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::RAW);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001196 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1197 header->Write(header_writer.get());
1198 CHECK(header_writer->Close());
1199
Steven Moreland81079f92018-07-06 16:15:53 -07001200 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
Jiyong Park05463732018-08-09 16:03:02 +09001201 const string bp_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::CLIENT);
Steven Moreland81079f92018-07-06 16:15:53 -07001202 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1203 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1204 CHECK(bp_writer->Close());
Jiyong Park05463732018-08-09 16:03:02 +09001205 const string bn_header = options.OutputHeaderDir() + HeaderFile(parcelable, ClassNames::SERVER);
Steven Moreland81079f92018-07-06 16:15:53 -07001206 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1207 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1208 CHECK(bn_writer->Close());
1209
Jiyong Park74595c12018-07-23 15:22:50 +09001210 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001211 source->Write(source_writer.get());
1212 CHECK(source_writer->Close());
1213
1214 return true;
1215}
1216
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001217bool GenerateCppParcelDeclaration(const std::string& filename, const IoDelegate& io_delegate) {
1218 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
1219 *code_writer
1220 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
1221
1222 return true;
1223}
1224
Jiyong Park74595c12018-07-23 15:22:50 +09001225bool GenerateCpp(const string& output_file, const Options& options, const TypeNamespace& types,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001226 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1227 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1228 if (parcelable != nullptr) {
Jiyong Park74595c12018-07-23 15:22:50 +09001229 return GenerateCppParcel(output_file, options, types, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001230 }
1231
Steven Moreland2a9a7d62019-02-05 16:11:54 -08001232 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
1233 if (parcelable_decl != nullptr) {
1234 return GenerateCppParcelDeclaration(output_file, io_delegate);
1235 }
1236
Steven Moreland5557f1c2018-07-02 13:50:23 -07001237 const AidlInterface* interface = defined_type.AsInterface();
1238 if (interface != nullptr) {
Jiyong Park74595c12018-07-23 15:22:50 +09001239 return GenerateCppInterface(output_file, options, types, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001240 }
1241
1242 CHECK(false) << "Unrecognized type sent for cpp generation.";
1243 return false;
1244}
1245
Christopher Wileyf944e792015-09-29 10:00:46 -07001246} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001247} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001248} // namespace android