blob: f51706ef8f415cd033bad29f18315d1233cc64dd [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"
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070030#include "ast_cpp.h"
31#include "code_writer.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070032#include "logging.h"
Christopher Wiley054afbd2015-10-16 17:08:43 -070033#include "os.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070034
Jiyong Park75e1a742018-07-04 12:31:23 +090035using android::base::Join;
Christopher Wileye3550c62015-09-29 13:26:10 -070036using android::base::StringPrintf;
Jiyong Park75e1a742018-07-04 12:31:23 +090037using std::set;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070038using std::string;
Casey Dahlin082f1d12015-09-21 14:06:25 -070039using std::unique_ptr;
Casey Dahlina834dd42015-09-23 11:52:15 -070040using std::vector;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070041
Christopher Wileyeb1acc12015-09-16 11:25:13 -070042namespace android {
43namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070044namespace cpp {
Casey Dahlina834dd42015-09-23 11:52:15 -070045namespace internals {
Christopher Wiley0c732db2015-09-29 14:36:44 -070046namespace {
Christopher Wiley36570f42015-10-08 17:20:11 -070047
Casey Dahlinb8d9e882015-11-24 10:57:23 -080048const char kAndroidStatusVarName[] = "_aidl_ret_status";
49const char kCodeVarName[] = "_aidl_code";
50const char kFlagsVarName[] = "_aidl_flags";
51const char kDataVarName[] = "_aidl_data";
52const char kErrorLabel[] = "_aidl_error";
53const char kImplVarName[] = "_aidl_impl";
54const char kReplyVarName[] = "_aidl_reply";
Christopher Wileyad339272015-10-05 19:11:58 -070055const char kReturnVarName[] = "_aidl_return";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080056const char kStatusVarName[] = "_aidl_status";
Martijn Coenenf1b50782018-02-21 21:06:23 +010057const char kTraceVarName[] = "_aidl_trace";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080058const char kAndroidParcelLiteral[] = "::android::Parcel";
59const char kAndroidStatusLiteral[] = "::android::status_t";
60const char kAndroidStatusOk[] = "::android::OK";
61const char kBinderStatusLiteral[] = "::android::binder::Status";
Christopher Wiley0c732db2015-09-29 14:36:44 -070062const char kIBinderHeader[] = "binder/IBinder.h";
63const char kIInterfaceHeader[] = "binder/IInterface.h";
Christopher Wileyad339272015-10-05 19:11:58 -070064const char kParcelHeader[] = "binder/Parcel.h";
Christopher Wiley433c8bb2015-11-12 14:20:46 -080065const char kStatusHeader[] = "binder/Status.h";
Christopher Wiley69b44cf2016-05-03 13:43:33 -070066const char kString16Header[] = "utils/String16.h";
Martijn Coenenf1b50782018-02-21 21:06:23 +010067const char kTraceHeader[] = "utils/Trace.h";
Casey Dahlin389781f2015-10-22 13:13:21 -070068const char kStrongPointerHeader[] = "utils/StrongPointer.h";
Jiyong Park75e1a742018-07-04 12:31:23 +090069const char kAndroidBaseMacrosHeader[] = "android-base/macros.h";
Casey Dahlin082f1d12015-09-21 14:06:25 -070070
Christopher Wiley0eb903e2015-10-20 17:07:08 -070071unique_ptr<AstNode> BreakOnStatusNotOk() {
72 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080073 new LiteralExpression(kAndroidStatusVarName), "!=",
74 new LiteralExpression(kAndroidStatusOk)));
Christopher Wiley0eb903e2015-10-20 17:07:08 -070075 ret->OnTrue()->AddLiteral("break");
76 return unique_ptr<AstNode>(ret);
77}
78
Christopher Wiley433c8bb2015-11-12 14:20:46 -080079unique_ptr<AstNode> GotoErrorOnBadStatus() {
80 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080081 new LiteralExpression(kAndroidStatusVarName), "!=",
82 new LiteralExpression(kAndroidStatusOk)));
83 ret->OnTrue()->AddLiteral(StringPrintf("goto %s", kErrorLabel));
Christopher Wiley433c8bb2015-11-12 14:20:46 -080084 return unique_ptr<AstNode>(ret);
85}
86
Steven Moreland5557f1c2018-07-02 13:50:23 -070087unique_ptr<AstNode> ReturnOnStatusNotOk() {
88 IfStatement* ret = new IfStatement(new Comparison(new LiteralExpression(kAndroidStatusVarName),
89 "!=", new LiteralExpression(kAndroidStatusOk)));
90 ret->OnTrue()->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
91 return unique_ptr<AstNode>(ret);
92}
93
Christopher Wiley0c732db2015-09-29 14:36:44 -070094string UpperCase(const std::string& s) {
95 string result = s;
96 for (char& c : result)
97 c = toupper(c);
98 return result;
Casey Dahlina834dd42015-09-23 11:52:15 -070099}
Casey Dahlin082f1d12015-09-21 14:06:25 -0700100
Christopher Wileyad339272015-10-05 19:11:58 -0700101string BuildVarName(const AidlArgument& a) {
102 string prefix = "out_";
103 if (a.GetDirection() & AidlArgument::IN_DIR) {
104 prefix = "in_";
Christopher Wileye3550c62015-09-29 13:26:10 -0700105 }
Christopher Wileyad339272015-10-05 19:11:58 -0700106 return prefix + a.GetName();
107}
108
Jiyong Park75e1a742018-07-04 12:31:23 +0900109ArgList BuildArgList(const TypeNamespace& types, const AidlMethod& method, bool for_declaration,
110 bool type_name_only = false) {
Christopher Wileyad339272015-10-05 19:11:58 -0700111 // Build up the argument list for the server method call.
112 vector<string> method_arguments;
113 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
114 string literal;
115 if (for_declaration) {
116 // Method declarations need types, pointers to out params, and variable
117 // names that match the .aidl specification.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800118 const Type* type = a->GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700119
Casey Dahlina2f77c42015-12-01 18:26:02 -0800120 literal = type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700121
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700122 if (a->IsOut()) {
123 literal = literal + "*";
124 } else {
125 // We pass in parameters that are not primitives by const reference.
126 // Arrays of primitives are not primitives.
127 if (!type->IsCppPrimitive() || a->GetType().IsArray()) {
128 literal = "const " + literal + "&";
129 }
130 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900131 if (!type_name_only) {
132 literal += " " + a->GetName();
133 }
Christopher Wileyad339272015-10-05 19:11:58 -0700134 } else {
135 if (a->IsOut()) { literal = "&"; }
136 literal += BuildVarName(*a);
137 }
138 method_arguments.push_back(literal);
139 }
140
Casey Dahlina2f77c42015-12-01 18:26:02 -0800141 const Type* return_type = method.GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700142
Christopher Wileyad339272015-10-05 19:11:58 -0700143 if (return_type != types.VoidType()) {
Christopher Wileyade4b452015-10-10 11:06:03 -0700144 string literal;
Christopher Wileyad339272015-10-05 19:11:58 -0700145 if (for_declaration) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900146 literal = StringPrintf("%s* %s", return_type->CppType().c_str(),
147 type_name_only ? "" : kReturnVarName);
Christopher Wileyad339272015-10-05 19:11:58 -0700148 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700149 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700150 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700151 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700152 }
153
Christopher Wileyade4b452015-10-10 11:06:03 -0700154 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700155}
156
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700157unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700158 const TypeNamespace& types,
159 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>{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800169 new MethodDecl{kBinderStatusLiteral,
Casey Dahlinf4a93112015-10-05 16:58:09 -0700170 method.GetName(),
Christopher Wileyad339272015-10-05 19:11:58 -0700171 BuildArgList(types, method, true /* for method decl */),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700172 modifiers}};
173}
174
Jiyong Park309668e2018-07-28 16:55:44 +0900175unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const TypeNamespace&,
176 const Options& options, bool for_interface) {
177 CHECK(!method.IsUserDefined());
178 if (method.GetName() == kGetInterfaceVersion && options.Version()) {
179 std::ostringstream code;
180 if (for_interface) {
181 code << "virtual ";
182 }
183 code << "int32_t " << kGetInterfaceVersion << "()";
184 if (for_interface) {
185 code << " = 0;\n";
186 } else {
187 code << " override;\n";
188 }
189 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
190 }
191 return nullptr;
192}
193
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700194unique_ptr<CppNamespace> NestInNamespaces(
195 vector<unique_ptr<Declaration>> decls,
196 const vector<string>& package) {
197 if (package.empty()) {
198 // We should also be checking this before we get this far, but do it again
199 // for the sake of unit tests and meaningful errors.
200 LOG(FATAL) << "C++ generation requires a package declaration "
201 "for namespacing";
202 }
203 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
204 unique_ptr<CppNamespace> inner{new CppNamespace{*it, std::move(decls)}};
205 ++it;
206 for (; it != package.crend(); ++it) {
207 inner.reset(new CppNamespace{*it, std::move(inner)});
208 }
209 return inner;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700210}
211
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700212unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl,
213 const vector<string>& package) {
214 vector<unique_ptr<Declaration>> decls;
215 decls.push_back(std::move(decl));
216 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700217}
218
Steven Moreland1c41e972018-07-09 16:07:00 -0700219bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800220 const Type* cpp_type = a.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700221 if (!cpp_type) { return false; }
222
Casey Dahlina2f77c42015-12-01 18:26:02 -0800223 string type = cpp_type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700224
225 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700226 return true;
227}
228
Steven Moreland5557f1c2018-07-02 13:50:23 -0700229string ClassName(const AidlDefinedType& defined_type, ClassNames type) {
230 string c_name = defined_type.GetName();
Christopher Wiley0c732db2015-09-29 14:36:44 -0700231
232 if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1]))
233 c_name = c_name.substr(1);
234
235 switch (type) {
236 case ClassNames::CLIENT:
237 c_name = "Bp" + c_name;
238 break;
239 case ClassNames::SERVER:
240 c_name = "Bn" + c_name;
241 break;
242 case ClassNames::INTERFACE:
243 c_name = "I" + c_name;
244 break;
Jiyong Park75e1a742018-07-04 12:31:23 +0900245 case ClassNames::DEFAULT_IMPL:
246 c_name = "I" + c_name + "Default";
247 break;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700248 case ClassNames::BASE:
249 break;
250 }
251 return c_name;
252}
253
Steven Moreland5557f1c2018-07-02 13:50:23 -0700254string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
255 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700256 for (size_t i = 1; i < class_name.size(); ++i) {
257 if (isupper(class_name[i])) {
258 class_name.insert(i, "_");
259 ++i;
260 }
261 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700262 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700263 class_name.c_str());
264 for (char& c : ret) {
265 if (c == '.') {
266 c = '_';
267 }
268 c = toupper(c);
269 }
270 return ret;
271}
Christopher Wiley36570f42015-10-08 17:20:11 -0700272
273unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types,
274 const AidlInterface& interface,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900275 const AidlMethod& method, const Options& options) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700276 const string i_name = ClassName(interface, ClassNames::INTERFACE);
277 const string bp_name = ClassName(interface, ClassNames::CLIENT);
278 unique_ptr<MethodImpl> ret{new MethodImpl{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800279 kBinderStatusLiteral, bp_name, method.GetName(),
Christopher Wiley36570f42015-10-08 17:20:11 -0700280 ArgList{BuildArgList(types, method, true /* for method decl */)}}};
281 StatementBlock* b = ret->GetStatementBlock();
282
283 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800284 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700285 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800286 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700287
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800288 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800289 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
290 kAndroidStatusVarName,
291 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800292 // We unconditionally return a Status object.
293 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700294
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900295 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100296 b->AddLiteral(
297 StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
298 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
299 }
300
Christopher Wiley8993cb52015-10-21 09:53:24 -0700301 // Add the name of the interface we're hoping to call.
302 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800303 kAndroidStatusVarName,
304 new MethodCall(StringPrintf("%s.writeInterfaceToken",
305 kDataVarName),
306 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800307 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700308
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700309 for (const auto& a: method.GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800310 const Type* type = a->GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700311 string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Casey Dahlin389781f2015-10-22 13:13:21 -0700312 var_name = type->WriteCast(var_name);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700313
314 if (a->IsIn()) {
315 // Serialization looks roughly like:
316 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
317 // if (_aidl_ret_status != ::android::OK) { goto error; }
318 const string& method = type->WriteToParcelMethod();
319 b->AddStatement(new Assignment(
320 kAndroidStatusVarName,
321 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
322 ArgList(var_name))));
323 b->AddStatement(GotoErrorOnBadStatus());
324 } else if (a->IsOut() && a->GetType().IsArray()) {
325 // Special case, the length of the out array is written into the parcel.
326 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
327 // if (_aidl_ret_status != ::android::OK) { goto error; }
328 b->AddStatement(new Assignment(
329 kAndroidStatusVarName,
330 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName),
331 ArgList(var_name))));
332 b->AddStatement(GotoErrorOnBadStatus());
333 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700334 }
335
336 // Invoke the transaction on the remote binder and confirm status.
337 string transaction_code = StringPrintf(
338 "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str());
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700339
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800340 vector<string> args = {transaction_code, kDataVarName,
341 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700342
343 if (interface.IsOneway() || method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800344 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700345 }
346
Christopher Wiley36570f42015-10-08 17:20:11 -0700347 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800348 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700349 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700350 ArgList(args))));
Jiyong Park75e1a742018-07-04 12:31:23 +0900351
352 // If the method is not implemented in the remote side, try to call the
353 // default implementation, if provided.
354 vector<string> arg_names;
355 for (const auto& a : method.GetArguments()) {
356 arg_names.emplace_back(a->GetName());
357 }
358 if (method.GetType().GetLanguageType<Type>() != types.VoidType()) {
359 arg_names.emplace_back(kReturnVarName);
360 }
361 b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && "
362 "%s::getDefaultImpl())) {\n"
363 " return %s::getDefaultImpl()->%s(%s);\n"
364 "}\n",
365 i_name.c_str(), i_name.c_str(), method.GetName().c_str(),
366 Join(arg_names, ", ").c_str()),
367 false /* no semicolon */);
368
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800369 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700370
Christopher Wiley1227d612015-10-26 16:59:20 -0700371 if (!interface.IsOneway() && !method.IsOneway()) {
372 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800373 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
374 // if (_aidl_ret_status != ::android::OK) { goto error; }
375 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800376 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800377 kAndroidStatusVarName,
378 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800379 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700380 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800381 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700382 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800383 exception_check->OnTrue()->AddLiteral(
384 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700385 }
386
387 // Type checking should guarantee that nothing below emits code until "return
388 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700389
Christopher Wiley36570f42015-10-08 17:20:11 -0700390 // If the method is expected to return something, read it first by convention.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800391 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700392 if (return_type != types.VoidType()) {
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700393 const string& method_call = return_type->ReadFromParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700394 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800395 kAndroidStatusVarName,
396 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
397 method_call.c_str()),
398 ArgList(kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800399 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700400 }
401
402 for (const AidlArgument* a : method.GetOutArguments()) {
403 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800404 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
405 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Casey Dahlinb0966612015-10-19 16:35:26 -0700406 string method =
Casey Dahlina2f77c42015-12-01 18:26:02 -0800407 a->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700408
Christopher Wiley36570f42015-10-08 17:20:11 -0700409 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800410 kAndroidStatusVarName,
411 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
412 method.c_str()),
413 ArgList(a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800414 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700415 }
416
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800417 // If we've gotten to here, one of two things is true:
418 // 1) We've read some bad status_t
419 // 2) We've only read status_t == OK and there was no exception in the
420 // response.
421 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800422 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800423 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800424 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
425 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100426
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800427 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700428
429 return unique_ptr<Declaration>(ret.release());
430}
431
Jiyong Park309668e2018-07-28 16:55:44 +0900432unique_ptr<Declaration> DefineClientMetaTransaction(const TypeNamespace&,
433 const AidlInterface& interface,
434 const AidlMethod& method,
435 const Options& options) {
436 CHECK(!method.IsUserDefined());
437 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
438 const string iface = ClassName(interface, ClassNames::INTERFACE);
439 const string proxy = ClassName(interface, ClassNames::CLIENT);
440 // Note: race condition can happen here, but no locking is required
441 // because 1) writing an interger is atomic and 2) this transaction
442 // will always return the same value, i.e., competing threads will
443 // give write the same value to cached_version_.
444 std::ostringstream code;
445 code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n"
446 << " if (cached_version_ != -1) {\n"
447 << " ::android::Parcel data;\n"
448 << " ::android::Parcel reply;\n"
449 << " ::android::status_t err = remote()->transact(" << iface
450 << "::" << UpperCase(kGetInterfaceVersion) << ", data, &reply);\n"
451 << " if (err == ::android::OK) {\n"
452 << " cached_version_ = reply.readInt32();\n"
453 << " }\n"
454 << " }\n"
455 << " return cached_version_;\n"
456 << "}\n";
457 return unique_ptr<Declaration>(new LiteralDecl(code.str()));
458 }
459 return nullptr;
460}
461
Christopher Wiley36570f42015-10-08 17:20:11 -0700462} // namespace
463
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900464unique_ptr<Document> BuildClientSource(const TypeNamespace& types, const AidlInterface& interface,
465 const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700466 vector<string> include_list = {
467 HeaderFile(interface, ClassNames::CLIENT, false),
Jiyong Park75e1a742018-07-04 12:31:23 +0900468 kParcelHeader,
469 kAndroidBaseMacrosHeader
Christopher Wiley054afbd2015-10-16 17:08:43 -0700470 };
Christopher Wiley36570f42015-10-08 17:20:11 -0700471 vector<unique_ptr<Declaration>> file_decls;
472
473 // The constructor just passes the IBinder instance up to the super
474 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700475 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700476 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700477 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800478 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
479 kImplVarName)},
480 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700481
482 // Clients define a method per transaction.
483 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900484 unique_ptr<Declaration> m;
485 if (method->IsUserDefined()) {
486 m = DefineClientTransaction(types, interface, *method, options);
487 } else {
488 m = DefineClientMetaTransaction(types, interface, *method, options);
489 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700490 if (!m) { return nullptr; }
491 file_decls.push_back(std::move(m));
492 }
493 return unique_ptr<Document>{new CppSource{
494 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700495 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700496}
497
Christopher Wileyad339272015-10-05 19:11:58 -0700498namespace {
499
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900500bool HandleServerTransaction(const TypeNamespace& types, const AidlInterface& interface,
501 const AidlMethod& method, const Options& options, StatementBlock* b) {
Christopher Wileyad339272015-10-05 19:11:58 -0700502 // Declare all the parameters now. In the common case, we expect no errors
503 // in serialization.
504 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700505 if (!DeclareLocalVariable(*a, b)) {
506 return false;
507 }
Christopher Wileyad339272015-10-05 19:11:58 -0700508 }
509
510 // Declare a variable to hold the return value.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800511 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700512 if (return_type != types.VoidType()) {
513 b->AddLiteral(StringPrintf(
Casey Dahlina2f77c42015-12-01 18:26:02 -0800514 "%s %s", return_type->CppType().c_str(),
Casey Dahlinb0966612015-10-19 16:35:26 -0700515 kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700516 }
517
Christopher Wiley8993cb52015-10-21 09:53:24 -0700518 // Check that the client is calling the correct interface.
519 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800520 new MethodCall(StringPrintf("%s.checkInterface",
521 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700522 true /* invert the check */);
523 b->AddStatement(interface_check);
524 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800525 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700526 interface_check->OnTrue()->AddLiteral("break");
527
Christopher Wileyad339272015-10-05 19:11:58 -0700528 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700529 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700530 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800531 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
532 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800533 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700534 const string& readMethod = type->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700535
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700536 if (a->IsIn()) {
537 b->AddStatement(new Assignment{
538 kAndroidStatusVarName,
539 new MethodCall{string(kDataVarName) + "." + readMethod,
540 "&" + BuildVarName(*a)}});
541 b->AddStatement(BreakOnStatusNotOk());
542 } else if (a->IsOut() && a->GetType().IsArray()) {
543 // Special case, the length of the out array is written into the parcel.
544 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
545 // if (_aidl_ret_status != ::android::OK) { break; }
546 b->AddStatement(new Assignment{
547 kAndroidStatusVarName,
548 new MethodCall{string(kDataVarName) + ".resizeOutVector",
549 "&" + BuildVarName(*a)}});
550 b->AddStatement(BreakOnStatusNotOk());
551 }
Christopher Wileyad339272015-10-05 19:11:58 -0700552 }
553
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900554 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100555 b->AddStatement(new Statement(new MethodCall("atrace_begin",
556 ArgList{{"ATRACE_TAG_AIDL",
557 StringPrintf("\"%s::%s::cppServer\"",
558 interface.GetName().c_str(),
559 method.GetName().c_str())}})));
560 }
561
Christopher Wileyad339272015-10-05 19:11:58 -0700562 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800563 vector<unique_ptr<AstNode>> status_args;
564 status_args.emplace_back(new MethodCall(
Christopher Wileyad339272015-10-05 19:11:58 -0700565 method.GetName(),
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800566 BuildArgList(types, method, false /* not for method decl */)));
567 b->AddStatement(new Statement(new MethodCall(
568 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
569 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700570
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900571 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100572 b->AddStatement(new Statement(new MethodCall("atrace_end",
573 "ATRACE_TAG_AIDL")));
574 }
575
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800576 // Write exceptions during transaction handling to parcel.
577 if (!method.IsOneway()) {
578 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800579 kAndroidStatusVarName,
580 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800581 b->AddStatement(BreakOnStatusNotOk());
582 IfStatement* exception_check = new IfStatement(
583 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
584 b->AddStatement(exception_check);
585 exception_check->OnTrue()->AddLiteral("break");
586 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700587
Christopher Wiley36570f42015-10-08 17:20:11 -0700588 // If we have a return value, write it first.
589 if (return_type != types.VoidType()) {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700590 string writeMethod =
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800591 string(kReplyVarName) + "->" +
Casey Dahlina2f77c42015-12-01 18:26:02 -0800592 return_type->WriteToParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700593 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800594 kAndroidStatusVarName, new MethodCall{writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700595 ArgList{return_type->WriteCast(kReturnVarName)}}});
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700596 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700597 }
598
Christopher Wileyad339272015-10-05 19:11:58 -0700599 // Write each out parameter to the reply parcel.
600 for (const AidlArgument* a : method.GetOutArguments()) {
601 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800602 // _aidl_ret_status = data.WriteInt32(out_param_name);
603 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800604 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700605 const string& writeMethod = type->WriteToParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700606
Christopher Wileyad339272015-10-05 19:11:58 -0700607 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800608 kAndroidStatusVarName,
609 new MethodCall{string(kReplyVarName) + "->" + writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700610 type->WriteCast(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
Jiyong Park309668e2018-07-28 16:55:44 +0900617bool HandleServerMetaTransaction(const TypeNamespace&, const AidlInterface& interface,
618 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;
624 code << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE)
625 << "::VERSION)";
626 b->AddLiteral(code.str());
627 return true;
628 }
629 return false;
630}
631
Christopher Wileyad339272015-10-05 19:11:58 -0700632} // namespace
633
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900634unique_ptr<Document> BuildServerSource(const TypeNamespace& types, const AidlInterface& interface,
635 const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700636 const string bn_name = ClassName(interface, ClassNames::SERVER);
637 vector<string> include_list{
638 HeaderFile(interface, ClassNames::SERVER, false),
639 kParcelHeader
640 };
Christopher Wileyad339272015-10-05 19:11:58 -0700641 unique_ptr<MethodImpl> on_transact{new MethodImpl{
642 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800643 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
644 StringPrintf("const %s& %s", kAndroidParcelLiteral,
645 kDataVarName),
646 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
647 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700648 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700649
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800650 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700651 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800652 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
653 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700654
Christopher Wileyad339272015-10-05 19:11:58 -0700655 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800656 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700657 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700658
659 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700660 for (const auto& method : interface.GetMethods()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700661 StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName()));
662 if (!b) { return nullptr; }
663
Jiyong Park309668e2018-07-28 16:55:44 +0900664 bool success = false;
665 if (method->IsUserDefined()) {
666 success = HandleServerTransaction(types, interface, *method, options, b);
667 } else {
668 success = HandleServerMetaTransaction(types, interface, *method, options, b);
669 }
670 if (!success) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900671 return nullptr;
672 }
Christopher Wileyad339272015-10-05 19:11:58 -0700673 }
674
675 // The switch statement has a default case which defers to the super class.
676 // The superclass handles a few pre-defined transactions.
677 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800678 b->AddLiteral(StringPrintf(
679 "%s = ::android::BBinder::onTransact(%s, %s, "
680 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
681 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700682
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800683 // If we saw a null reference, we can map that to an appropriate exception.
684 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800685 new LiteralExpression(string(kAndroidStatusVarName) +
686 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800687 on_transact->GetStatementBlock()->AddStatement(null_check);
688 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800689 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800690 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800691 ".writeToParcel(%s)",
692 kBinderStatusLiteral, kBinderStatusLiteral,
693 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800694
Christopher Wileyad339272015-10-05 19:11:58 -0700695 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800696 on_transact->GetStatementBlock()->AddLiteral(
697 StringPrintf("return %s", kAndroidStatusVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700698
699 return unique_ptr<Document>{new CppSource{
700 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700701 NestInNamespaces(std::move(on_transact), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700702}
703
Jiyong Park75e1a742018-07-04 12:31:23 +0900704unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& types,
Jiyong Park309668e2018-07-28 16:55:44 +0900705 const AidlInterface& interface, const Options& options) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700706 vector<string> include_list{
707 HeaderFile(interface, ClassNames::INTERFACE, false),
708 HeaderFile(interface, ClassNames::CLIENT, false),
709 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700710
Christopher Wiley054afbd2015-10-16 17:08:43 -0700711 string fq_name = ClassName(interface, ClassNames::INTERFACE);
712 if (!interface.GetPackage().empty()) {
713 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700714 }
715
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700716 vector<unique_ptr<Declaration>> decls;
717
Christopher Wiley11a9d792016-02-24 17:20:33 -0800718 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700719 "IMPLEMENT_META_INTERFACE",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700720 ArgList{vector<string>{ClassName(interface, ClassNames::BASE),
Christopher Wileyade4b452015-10-10 11:06:03 -0700721 '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700722 decls.push_back(std::move(meta_if));
723
Steven Moreland693640b2018-07-19 13:46:27 -0700724 for (const auto& constant : interface.GetConstantDeclarations()) {
725 const AidlConstantValue& value = constant->GetValue();
726 if (value.GetType() != AidlConstantValue::Type::STRING) continue;
727
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700728 unique_ptr<MethodImpl> getter(new MethodImpl(
729 "const ::android::String16&",
730 ClassName(interface, ClassNames::INTERFACE),
731 constant->GetName(),
732 {}));
733 getter->GetStatementBlock()->AddLiteral(
Steven Moreland693640b2018-07-19 13:46:27 -0700734 StringPrintf("static const ::android::String16 value(%s)", value.ToString().c_str()));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700735 getter->GetStatementBlock()->AddLiteral("return value");
736 decls.push_back(std::move(getter));
737 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700738
Jiyong Park75e1a742018-07-04 12:31:23 +0900739 // Implement the default impl class.
740 // onAsBinder returns nullptr as this interface is not associated with a
741 // real binder.
742 const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL));
743 decls.emplace_back(
744 new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n"
745 " return nullptr;\n"
746 "}\n",
747 default_impl.c_str())));
748 // Each interface method by default returns UNKNOWN_TRANSACTION with is
749 // the same status that is returned by transact() when the method is
750 // not implemented in the server side. In other words, these default
751 // methods do nothing; they only exist to aid making a real default
752 // impl class without having to override all methods in an interface.
753 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900754 if (method->IsUserDefined()) {
755 std::ostringstream code;
756 code << "::android::binder::Status " << default_impl << "::" << method->GetName()
757 << BuildArgList(types, *method, true, true).ToString() << " {\n"
758 << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n"
759 << "}\n";
760 decls.emplace_back(new LiteralDecl(code.str()));
761 } else {
762 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
763 std::ostringstream code;
764 code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n"
765 << " return 0;\n"
766 << "}\n";
767 decls.emplace_back(new LiteralDecl(code.str()));
768 }
769 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900770 }
771
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700772 return unique_ptr<Document>{new CppSource{
773 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700774 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700775}
776
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900777unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, const AidlInterface& interface,
Jiyong Park309668e2018-07-28 16:55:44 +0900778 const Options& options) {
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700779 const string i_name = ClassName(interface, ClassNames::INTERFACE);
780 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700781
Christopher Wileyb23149d2015-10-14 13:52:21 -0700782 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
783 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800784 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
785 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700786 ConstructorDecl::IS_EXPLICIT
787 }};
788 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
789 "~" + bp_name,
790 ArgList{},
791 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700792
Christopher Wileyf944e792015-09-29 10:00:46 -0700793 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700794 publics.push_back(std::move(constructor));
795 publics.push_back(std::move(destructor));
796
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700797 for (const auto& method: interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900798 if (method->IsUserDefined()) {
799 publics.push_back(BuildMethodDecl(*method, types, false));
800 } else {
801 publics.push_back(BuildMetaMethodDecl(*method, types, options, false));
802 }
Casey Dahlina834dd42015-09-23 11:52:15 -0700803 }
804
Jiyong Park309668e2018-07-28 16:55:44 +0900805 vector<unique_ptr<Declaration>> privates;
806
807 if (options.Version() > 0) {
808 privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n"));
809 }
810
811 unique_ptr<ClassDecl> bp_class{new ClassDecl{
812 bp_name,
813 "::android::BpInterface<" + i_name + ">",
814 std::move(publics),
815 std::move(privates),
816 }};
Casey Dahlina834dd42015-09-23 11:52:15 -0700817
Christopher Wiley0c732db2015-09-29 14:36:44 -0700818 return unique_ptr<Document>{new CppHeader{
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700819 BuildHeaderGuard(interface, ClassNames::CLIENT),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700820 {kIBinderHeader,
821 kIInterfaceHeader,
822 "utils/Errors.h",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700823 HeaderFile(interface, ClassNames::INTERFACE, false)},
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700824 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700825}
826
Christopher Wileyf59c4992015-10-08 13:12:44 -0700827unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900828 const AidlInterface& interface, const Options&) {
Christopher Wileyfd51d602015-10-14 13:04:48 -0700829 const string i_name = ClassName(interface, ClassNames::INTERFACE);
830 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700831
Christopher Wileyfd51d602015-10-14 13:04:48 -0700832 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700833 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800834 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
835 StringPrintf("const %s& %s", kAndroidParcelLiteral,
836 kDataVarName),
837 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
838 StringPrintf("uint32_t %s = 0", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700839 MethodDecl::IS_OVERRIDE
840 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700841
Christopher Wileyf944e792015-09-29 10:00:46 -0700842 std::vector<unique_ptr<Declaration>> publics;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700843 publics.push_back(std::move(on_transact));
844
Christopher Wileyf944e792015-09-29 10:00:46 -0700845 unique_ptr<ClassDecl> bn_class{
846 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800847 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700848 std::move(publics),
849 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700850 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700851
Christopher Wiley0c732db2015-09-29 14:36:44 -0700852 return unique_ptr<Document>{new CppHeader{
Christopher Wileyfd51d602015-10-14 13:04:48 -0700853 BuildHeaderGuard(interface, ClassNames::SERVER),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700854 {"binder/IInterface.h",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700855 HeaderFile(interface, ClassNames::INTERFACE, false)},
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700856 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700857}
858
Christopher Wileye3550c62015-09-29 13:26:10 -0700859unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900860 const AidlInterface& interface, const Options& options) {
Jiyong Park75e1a742018-07-04 12:31:23 +0900861 set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader};
Casey Dahlince776cf2015-10-15 18:45:54 -0700862
863 for (const auto& method : interface.GetMethods()) {
864 for (const auto& argument : method->GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800865 const Type* type = argument->GetType().GetLanguageType<Type>();
866 type->GetHeaders(&includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700867 }
868
Casey Dahlina2f77c42015-12-01 18:26:02 -0800869 const Type* return_type = method->GetType().GetLanguageType<Type>();
870 return_type->GetHeaders(&includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700871 }
872
Jiyong Park75e1a742018-07-04 12:31:23 +0900873 const string i_name = ClassName(interface, ClassNames::INTERFACE);
874 unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800875 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700876 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700877 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700878
Jiyong Park309668e2018-07-28 16:55:44 +0900879 if (options.Version() > 0) {
880 std::ostringstream code;
881 code << "const int32_t VERSION = " << options.Version() << ";\n";
882
883 if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str())));
884 }
885
Steven Moreland693640b2018-07-19 13:46:27 -0700886 std::vector<std::unique_ptr<Declaration>> string_constants;
887 unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t"}};
888 for (const auto& constant : interface.GetConstantDeclarations()) {
889 const AidlConstantValue& value = constant->GetValue();
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800890
Steven Moreland693640b2018-07-19 13:46:27 -0700891 switch (value.GetType()) {
892 case AidlConstantValue::Type::STRING: {
893 unique_ptr<Declaration> getter(new MethodDecl(
894 "const ::android::String16&", constant->GetName(), {}, MethodDecl::IS_STATIC));
895 string_constants.push_back(std::move(getter));
896 break;
897 }
898 case AidlConstantValue::Type::INTEGER: {
899 int_constant_enum->AddValue(constant->GetName(), value.ToString());
900 break;
901 }
902 default: {
903 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
904 }
905 }
906 }
907 if (int_constant_enum->HasValues()) {
908 if_class->AddPublic(std::move(int_constant_enum));
909 }
910 if (!string_constants.empty()) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700911 includes.insert(kString16Header);
Steven Moreland693640b2018-07-19 13:46:27 -0700912
913 for (auto& string_constant : string_constants) {
914 if_class->AddPublic(std::move(string_constant));
915 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700916 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100917
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900918 if (options.GenTraces()) {
Martijn Coenenf1b50782018-02-21 21:06:23 +0100919 includes.insert(kTraceHeader);
920 }
921
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700922 if (!interface.GetMethods().empty()) {
923 unique_ptr<Enum> call_enum{new Enum{"Call"}};
924 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900925 if (method->IsUserDefined()) {
926 // Each method gets an enum entry and pure virtual declaration.
927 if_class->AddPublic(BuildMethodDecl(*method, types, true));
928 call_enum->AddValue(
929 UpperCase(method->GetName()),
930 StringPrintf("::android::IBinder::FIRST_CALL_TRANSACTION + %d", method->GetId()));
931 } else {
932 if_class->AddPublic(BuildMetaMethodDecl(*method, types, options, true));
933 call_enum->AddValue(UpperCase(method->GetName()), std::to_string(method->GetId()));
934 }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700935 }
936 if_class->AddPublic(std::move(call_enum));
Christopher Wiley0c732db2015-09-29 14:36:44 -0700937 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700938
Jiyong Park75e1a742018-07-04 12:31:23 +0900939 vector<unique_ptr<Declaration>> decls;
940 decls.emplace_back(std::move(if_class));
941
942 // Base class for the default implementation.
943 vector<string> method_decls;
944 for (const auto& method : interface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900945 if (method->IsUserDefined()) {
946 method_decls.emplace_back(BuildMethodDecl(*method, types, false)->ToString());
947 } else {
948 method_decls.emplace_back(BuildMetaMethodDecl(*method, types, options, false)->ToString());
949 }
Jiyong Park75e1a742018-07-04 12:31:23 +0900950 }
Jiyong Park309668e2018-07-28 16:55:44 +0900951
Jiyong Park75e1a742018-07-04 12:31:23 +0900952 decls.emplace_back(new LiteralDecl(
953 android::base::StringPrintf("class %s : public %s {\n"
954 "public:\n"
955 " ::android::IBinder* onAsBinder() override;\n"
956 " %s\n"
957 "};\n",
958 ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(),
959 i_name.c_str(), Join(method_decls, " ").c_str())));
960
961 return unique_ptr<Document>{
962 new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE),
963 vector<string>(includes.begin(), includes.end()),
964 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700965}
966
Steven Moreland5557f1c2018-07-02 13:50:23 -0700967std::unique_ptr<Document> BuildParcelHeader(const TypeNamespace& /*types*/,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900968 const AidlStructuredParcelable& parcel,
969 const Options&) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700970 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
971
972 set<string> includes = {kStatusHeader, kParcelHeader};
973 for (const auto& variable : parcel.GetFields()) {
974 const Type* type = variable->GetType().GetLanguageType<Type>();
975 type->GetHeaders(&includes);
976 }
977
978 for (const auto& variable : parcel.GetFields()) {
979 const Type* type = variable->GetType().GetLanguageType<Type>();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700980 const AidlConstantValue* default_value = variable->GetDefaultValue();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700981
Steven Moreland9ea10e32018-07-19 15:26:09 -0700982 std::ostringstream out;
983 out << type->CppType().c_str() << " " << variable->GetName().c_str();
984 if (default_value) {
985 out << " = " << type->CppType().c_str() << "(" << default_value->ToString() << ")";
986 }
987 out << ";\n";
988
989 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str())));
Steven Moreland5557f1c2018-07-02 13:50:23 -0700990 }
991
992 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -0700993 ArgList("const ::android::Parcel* _aidl_parcel"),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700994 MethodDecl::IS_OVERRIDE));
995 parcel_class->AddPublic(std::move(read));
996 unique_ptr<MethodDecl> write(new MethodDecl(kAndroidStatusLiteral, "writeToParcel",
Steven Morelandce39c532018-07-11 16:59:50 -0700997 ArgList("::android::Parcel* _aidl_parcel"),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700998 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST));
999 parcel_class->AddPublic(std::move(write));
1000
1001 return unique_ptr<Document>{new CppHeader{
1002 BuildHeaderGuard(parcel, ClassNames::BASE), vector<string>(includes.begin(), includes.end()),
1003 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
1004}
Steven Moreland1c41e972018-07-09 16:07:00 -07001005std::unique_ptr<Document> BuildParcelSource(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<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
1009 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -07001010 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001011 StatementBlock* read_block = read->GetStatementBlock();
1012 read_block->AddLiteral(
1013 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
1014 for (const auto& variable : parcel.GetFields()) {
1015 string method = variable->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
1016
1017 read_block->AddStatement(new Assignment(
1018 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1019 ArgList("&" + variable->GetName()))));
1020 read_block->AddStatement(ReturnOnStatusNotOk());
1021 }
1022 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1023
Steven Morelandce39c532018-07-11 16:59:50 -07001024 unique_ptr<MethodImpl> write{
1025 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
1026 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -07001027 StatementBlock* write_block = write->GetStatementBlock();
1028 write_block->AddLiteral(
1029 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
1030 for (const auto& variable : parcel.GetFields()) {
1031 string method = variable->GetType().GetLanguageType<Type>()->WriteToParcelMethod();
1032
1033 write_block->AddStatement(new Assignment(
1034 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
1035 ArgList(variable->GetName()))));
1036 write_block->AddStatement(ReturnOnStatusNotOk());
1037 }
1038 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
1039
1040 vector<unique_ptr<Declaration>> file_decls;
1041 file_decls.push_back(std::move(read));
1042 file_decls.push_back(std::move(write));
1043
1044 set<string> includes = {};
1045 parcel.GetLanguageType<Type>()->GetHeaders(&includes);
1046
1047 return unique_ptr<Document>{
1048 new CppSource{vector<string>(includes.begin(), includes.end()),
1049 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
1050}
1051
Jiyong Park74595c12018-07-23 15:22:50 +09001052bool WriteHeader(const Options& options, const TypeNamespace& types, const AidlInterface& interface,
1053 const IoDelegate& io_delegate, ClassNames header_type) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001054 unique_ptr<Document> header;
1055 switch (header_type) {
1056 case ClassNames::INTERFACE:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001057 header = BuildInterfaceHeader(types, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001058 break;
1059 case ClassNames::CLIENT:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001060 header = BuildClientHeader(types, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001061 break;
1062 case ClassNames::SERVER:
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001063 header = BuildServerHeader(types, interface, options);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001064 break;
1065 default:
1066 LOG(FATAL) << "aidl internal error";
1067 }
1068 if (!header) {
1069 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001070 return false;
1071 }
Christopher Wiley054afbd2015-10-16 17:08:43 -07001072
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001073 const string header_path = options.OutputHeaderDir() + OS_PATH_SEPARATOR +
1074 HeaderFile(interface, header_type);
1075 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
1076 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -07001077
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001078 const bool success = code_writer->Close();
1079 if (!success) {
1080 io_delegate.RemovePath(header_path);
1081 }
1082
1083 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001084}
1085
Casey Dahlina834dd42015-09-23 11:52:15 -07001086} // namespace internals
1087
1088using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001089
Steven Moreland5557f1c2018-07-02 13:50:23 -07001090string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type, bool use_os_sep) {
1091 string file_path = defined_type.GetPackage();
Christopher Wiley3a9911c2016-01-19 12:59:09 -08001092 for (char& c: file_path) {
1093 if (c == '.') {
1094 c = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
1095 }
1096 }
1097 if (!file_path.empty()) {
1098 file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/';
1099 }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001100 file_path += ClassName(defined_type, class_type);
Christopher Wiley3a9911c2016-01-19 12:59:09 -08001101 file_path += ".h";
1102
1103 return file_path;
1104}
1105
Jiyong Park74595c12018-07-23 15:22:50 +09001106bool GenerateCppInterface(const string& output_file, const Options& options,
1107 const TypeNamespace& types, const AidlInterface& interface,
1108 const IoDelegate& io_delegate) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001109 auto interface_src = BuildInterfaceSource(types, interface, options);
1110 auto client_src = BuildClientSource(types, interface, options);
1111 auto server_src = BuildServerSource(types, interface, options);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001112
Christopher Wiley054afbd2015-10-16 17:08:43 -07001113 if (!interface_src || !client_src || !server_src) {
1114 return false;
1115 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -07001116
Christopher Wiley054afbd2015-10-16 17:08:43 -07001117 if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(),
Christopher Wileyb656a3b2015-10-16 11:11:09 -07001118 interface.GetSplitPackage())) {
Christopher Wiley054afbd2015-10-16 17:08:43 -07001119 LOG(ERROR) << "Failed to create directory structure for headers.";
1120 return false;
1121 }
1122
1123 if (!WriteHeader(options, types, interface, io_delegate,
1124 ClassNames::INTERFACE) ||
1125 !WriteHeader(options, types, interface, io_delegate,
1126 ClassNames::CLIENT) ||
1127 !WriteHeader(options, types, interface, io_delegate,
1128 ClassNames::SERVER)) {
1129 return false;
1130 }
1131
Jiyong Park74595c12018-07-23 15:22:50 +09001132 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file);
Christopher Wiley054afbd2015-10-16 17:08:43 -07001133 interface_src->Write(writer.get());
1134 client_src->Write(writer.get());
1135 server_src->Write(writer.get());
1136
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001137 const bool success = writer->Close();
1138 if (!success) {
Jiyong Park74595c12018-07-23 15:22:50 +09001139 io_delegate.RemovePath(options.OutputFile());
Christopher Wiley9d6e0b22015-11-13 12:18:16 -08001140 }
1141
1142 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001143}
1144
Jiyong Park74595c12018-07-23 15:22:50 +09001145bool GenerateCppParcel(const string& output_file, const Options& options,
1146 const cpp::TypeNamespace& types, const AidlStructuredParcelable& parcelable,
1147 const IoDelegate& io_delegate) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +09001148 auto header = BuildParcelHeader(types, parcelable, options);
1149 auto source = BuildParcelSource(types, parcelable, options);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001150
1151 if (!header || !source) {
1152 return false;
1153 }
1154
1155 if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), parcelable.GetSplitPackage())) {
1156 LOG(ERROR) << "Failed to create directory structure for headers.";
1157 }
1158
1159 const string header_path =
1160 options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::BASE);
1161 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
1162 header->Write(header_writer.get());
1163 CHECK(header_writer->Close());
1164
Steven Moreland81079f92018-07-06 16:15:53 -07001165 // TODO(b/111362593): no unecessary files just to have consistent output with interfaces
1166 const string bp_header =
1167 options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::CLIENT);
1168 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
1169 bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes");
1170 CHECK(bp_writer->Close());
1171 const string bn_header =
1172 options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::SERVER);
1173 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
1174 bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes");
1175 CHECK(bn_writer->Close());
1176
Jiyong Park74595c12018-07-23 15:22:50 +09001177 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001178 source->Write(source_writer.get());
1179 CHECK(source_writer->Close());
1180
1181 return true;
1182}
1183
Jiyong Park74595c12018-07-23 15:22:50 +09001184bool GenerateCpp(const string& output_file, const Options& options, const TypeNamespace& types,
Steven Moreland5557f1c2018-07-02 13:50:23 -07001185 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
1186 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
1187 if (parcelable != nullptr) {
Jiyong Park74595c12018-07-23 15:22:50 +09001188 return GenerateCppParcel(output_file, options, types, *parcelable, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001189 }
1190
1191 const AidlInterface* interface = defined_type.AsInterface();
1192 if (interface != nullptr) {
Jiyong Park74595c12018-07-23 15:22:50 +09001193 return GenerateCppInterface(output_file, options, types, *interface, io_delegate);
Steven Moreland5557f1c2018-07-02 13:50:23 -07001194 }
1195
1196 CHECK(false) << "Unrecognized type sent for cpp generation.";
1197 return false;
1198}
1199
Christopher Wileyf944e792015-09-29 10:00:46 -07001200} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -07001201} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -07001202} // namespace android