blob: 8fc12db9faa6b7f9d43af2efc9cc60bf70189ae0 [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"
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070018
Casey Dahlin082f1d12015-09-21 14:06:25 -070019#include <cctype>
Christopher Wileyad339272015-10-05 19:11:58 -070020#include <cstring>
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070021#include <memory>
Casey Dahlin082f1d12015-09-21 14:06:25 -070022#include <random>
Casey Dahlince776cf2015-10-15 18:45:54 -070023#include <set>
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070024#include <string>
25
Elliott Hughes0a620672015-12-04 13:53:18 -080026#include <android-base/stringprintf.h>
Christopher Wileye3550c62015-09-29 13:26:10 -070027
Casey Dahlina834dd42015-09-23 11:52:15 -070028#include "aidl_language.h"
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070029#include "ast_cpp.h"
30#include "code_writer.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070031#include "logging.h"
Christopher Wiley054afbd2015-10-16 17:08:43 -070032#include "os.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070033
Christopher Wileye3550c62015-09-29 13:26:10 -070034using android::base::StringPrintf;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070035using std::string;
Casey Dahlin082f1d12015-09-21 14:06:25 -070036using std::unique_ptr;
Casey Dahlina834dd42015-09-23 11:52:15 -070037using std::vector;
Casey Dahlince776cf2015-10-15 18:45:54 -070038using std::set;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -070039
Christopher Wileyeb1acc12015-09-16 11:25:13 -070040namespace android {
41namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070042namespace cpp {
Casey Dahlina834dd42015-09-23 11:52:15 -070043namespace internals {
Christopher Wiley0c732db2015-09-29 14:36:44 -070044namespace {
Christopher Wiley36570f42015-10-08 17:20:11 -070045
Casey Dahlinb8d9e882015-11-24 10:57:23 -080046const char kAndroidStatusVarName[] = "_aidl_ret_status";
47const char kCodeVarName[] = "_aidl_code";
48const char kFlagsVarName[] = "_aidl_flags";
49const char kDataVarName[] = "_aidl_data";
50const char kErrorLabel[] = "_aidl_error";
51const char kImplVarName[] = "_aidl_impl";
52const char kReplyVarName[] = "_aidl_reply";
Christopher Wileyad339272015-10-05 19:11:58 -070053const char kReturnVarName[] = "_aidl_return";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080054const char kStatusVarName[] = "_aidl_status";
Martijn Coenenf1b50782018-02-21 21:06:23 +010055const char kTraceVarName[] = "_aidl_trace";
Casey Dahlinb8d9e882015-11-24 10:57:23 -080056const char kAndroidParcelLiteral[] = "::android::Parcel";
57const char kAndroidStatusLiteral[] = "::android::status_t";
58const char kAndroidStatusOk[] = "::android::OK";
59const char kBinderStatusLiteral[] = "::android::binder::Status";
Christopher Wiley0c732db2015-09-29 14:36:44 -070060const char kIBinderHeader[] = "binder/IBinder.h";
61const char kIInterfaceHeader[] = "binder/IInterface.h";
Christopher Wileyad339272015-10-05 19:11:58 -070062const char kParcelHeader[] = "binder/Parcel.h";
Christopher Wiley433c8bb2015-11-12 14:20:46 -080063const char kStatusHeader[] = "binder/Status.h";
Christopher Wiley69b44cf2016-05-03 13:43:33 -070064const char kString16Header[] = "utils/String16.h";
Martijn Coenenf1b50782018-02-21 21:06:23 +010065const char kTraceHeader[] = "utils/Trace.h";
Casey Dahlin389781f2015-10-22 13:13:21 -070066const char kStrongPointerHeader[] = "utils/StrongPointer.h";
Casey Dahlin082f1d12015-09-21 14:06:25 -070067
Christopher Wiley0eb903e2015-10-20 17:07:08 -070068unique_ptr<AstNode> BreakOnStatusNotOk() {
69 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080070 new LiteralExpression(kAndroidStatusVarName), "!=",
71 new LiteralExpression(kAndroidStatusOk)));
Christopher Wiley0eb903e2015-10-20 17:07:08 -070072 ret->OnTrue()->AddLiteral("break");
73 return unique_ptr<AstNode>(ret);
74}
75
Christopher Wiley433c8bb2015-11-12 14:20:46 -080076unique_ptr<AstNode> GotoErrorOnBadStatus() {
77 IfStatement* ret = new IfStatement(new Comparison(
Casey Dahlinb8d9e882015-11-24 10:57:23 -080078 new LiteralExpression(kAndroidStatusVarName), "!=",
79 new LiteralExpression(kAndroidStatusOk)));
80 ret->OnTrue()->AddLiteral(StringPrintf("goto %s", kErrorLabel));
Christopher Wiley433c8bb2015-11-12 14:20:46 -080081 return unique_ptr<AstNode>(ret);
82}
83
Steven Moreland5557f1c2018-07-02 13:50:23 -070084unique_ptr<AstNode> ReturnOnStatusNotOk() {
85 IfStatement* ret = new IfStatement(new Comparison(new LiteralExpression(kAndroidStatusVarName),
86 "!=", new LiteralExpression(kAndroidStatusOk)));
87 ret->OnTrue()->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
88 return unique_ptr<AstNode>(ret);
89}
90
Christopher Wiley0c732db2015-09-29 14:36:44 -070091string UpperCase(const std::string& s) {
92 string result = s;
93 for (char& c : result)
94 c = toupper(c);
95 return result;
Casey Dahlina834dd42015-09-23 11:52:15 -070096}
Casey Dahlin082f1d12015-09-21 14:06:25 -070097
Christopher Wileyad339272015-10-05 19:11:58 -070098string BuildVarName(const AidlArgument& a) {
99 string prefix = "out_";
100 if (a.GetDirection() & AidlArgument::IN_DIR) {
101 prefix = "in_";
Christopher Wileye3550c62015-09-29 13:26:10 -0700102 }
Christopher Wileyad339272015-10-05 19:11:58 -0700103 return prefix + a.GetName();
104}
105
Christopher Wileyade4b452015-10-10 11:06:03 -0700106ArgList BuildArgList(const TypeNamespace& types,
107 const AidlMethod& method,
108 bool for_declaration) {
Christopher Wileyad339272015-10-05 19:11:58 -0700109 // Build up the argument list for the server method call.
110 vector<string> method_arguments;
111 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
112 string literal;
113 if (for_declaration) {
114 // Method declarations need types, pointers to out params, and variable
115 // names that match the .aidl specification.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800116 const Type* type = a->GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700117
Casey Dahlina2f77c42015-12-01 18:26:02 -0800118 literal = type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700119
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700120 if (a->IsOut()) {
121 literal = literal + "*";
122 } else {
123 // We pass in parameters that are not primitives by const reference.
124 // Arrays of primitives are not primitives.
125 if (!type->IsCppPrimitive() || a->GetType().IsArray()) {
126 literal = "const " + literal + "&";
127 }
128 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700129
130 literal += " " + a->GetName();
Christopher Wileyad339272015-10-05 19:11:58 -0700131 } else {
132 if (a->IsOut()) { literal = "&"; }
133 literal += BuildVarName(*a);
134 }
135 method_arguments.push_back(literal);
136 }
137
Casey Dahlina2f77c42015-12-01 18:26:02 -0800138 const Type* return_type = method.GetType().GetLanguageType<Type>();
Casey Dahlince776cf2015-10-15 18:45:54 -0700139
Christopher Wileyad339272015-10-05 19:11:58 -0700140 if (return_type != types.VoidType()) {
Christopher Wileyade4b452015-10-10 11:06:03 -0700141 string literal;
Christopher Wileyad339272015-10-05 19:11:58 -0700142 if (for_declaration) {
Christopher Wileyade4b452015-10-10 11:06:03 -0700143 literal = StringPrintf(
Casey Dahlina2f77c42015-12-01 18:26:02 -0800144 "%s* %s", return_type->CppType().c_str(),
Casey Dahlinb0966612015-10-19 16:35:26 -0700145 kReturnVarName);
Christopher Wileyad339272015-10-05 19:11:58 -0700146 } else {
Christopher Wileyade4b452015-10-10 11:06:03 -0700147 literal = string{"&"} + kReturnVarName;
Christopher Wileyad339272015-10-05 19:11:58 -0700148 }
Christopher Wileyade4b452015-10-10 11:06:03 -0700149 method_arguments.push_back(literal);
Christopher Wileyad339272015-10-05 19:11:58 -0700150 }
151
Christopher Wileyade4b452015-10-10 11:06:03 -0700152 return ArgList(method_arguments);
Casey Dahlina834dd42015-09-23 11:52:15 -0700153}
154
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700155unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method,
Christopher Wiley0c732db2015-09-29 14:36:44 -0700156 const TypeNamespace& types,
157 bool for_interface) {
Christopher Wiley0c732db2015-09-29 14:36:44 -0700158 uint32_t modifiers = 0;
159 if (for_interface) {
160 modifiers |= MethodDecl::IS_VIRTUAL;
161 modifiers |= MethodDecl::IS_PURE_VIRTUAL;
162 } else {
163 modifiers |= MethodDecl::IS_OVERRIDE;
164 }
165
166 return unique_ptr<Declaration>{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800167 new MethodDecl{kBinderStatusLiteral,
Casey Dahlinf4a93112015-10-05 16:58:09 -0700168 method.GetName(),
Christopher Wileyad339272015-10-05 19:11:58 -0700169 BuildArgList(types, method, true /* for method decl */),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700170 modifiers}};
171}
172
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700173unique_ptr<CppNamespace> NestInNamespaces(
174 vector<unique_ptr<Declaration>> decls,
175 const vector<string>& package) {
176 if (package.empty()) {
177 // We should also be checking this before we get this far, but do it again
178 // for the sake of unit tests and meaningful errors.
179 LOG(FATAL) << "C++ generation requires a package declaration "
180 "for namespacing";
181 }
182 auto it = package.crbegin(); // Iterate over the namespaces inner to outer
183 unique_ptr<CppNamespace> inner{new CppNamespace{*it, std::move(decls)}};
184 ++it;
185 for (; it != package.crend(); ++it) {
186 inner.reset(new CppNamespace{*it, std::move(inner)});
187 }
188 return inner;
Christopher Wiley0c732db2015-09-29 14:36:44 -0700189}
190
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700191unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl,
192 const vector<string>& package) {
193 vector<unique_ptr<Declaration>> decls;
194 decls.push_back(std::move(decl));
195 return NestInNamespaces(std::move(decls), package);
Christopher Wiley36570f42015-10-08 17:20:11 -0700196}
197
Steven Moreland1c41e972018-07-09 16:07:00 -0700198bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800199 const Type* cpp_type = a.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700200 if (!cpp_type) { return false; }
201
Casey Dahlina2f77c42015-12-01 18:26:02 -0800202 string type = cpp_type->CppType();
Casey Dahlinb0966612015-10-19 16:35:26 -0700203
204 b->AddLiteral(type + " " + BuildVarName(a));
Christopher Wileyad339272015-10-05 19:11:58 -0700205 return true;
206}
207
Steven Moreland5557f1c2018-07-02 13:50:23 -0700208string ClassName(const AidlDefinedType& defined_type, ClassNames type) {
209 string c_name = defined_type.GetName();
Christopher Wiley0c732db2015-09-29 14:36:44 -0700210
211 if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1]))
212 c_name = c_name.substr(1);
213
214 switch (type) {
215 case ClassNames::CLIENT:
216 c_name = "Bp" + c_name;
217 break;
218 case ClassNames::SERVER:
219 c_name = "Bn" + c_name;
220 break;
221 case ClassNames::INTERFACE:
222 c_name = "I" + c_name;
223 break;
224 case ClassNames::BASE:
225 break;
226 }
227 return c_name;
228}
229
Steven Moreland5557f1c2018-07-02 13:50:23 -0700230string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) {
231 string class_name = ClassName(defined_type, header_type);
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700232 for (size_t i = 1; i < class_name.size(); ++i) {
233 if (isupper(class_name[i])) {
234 class_name.insert(i, "_");
235 ++i;
236 }
237 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700238 string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(),
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700239 class_name.c_str());
240 for (char& c : ret) {
241 if (c == '.') {
242 c = '_';
243 }
244 c = toupper(c);
245 }
246 return ret;
247}
Christopher Wiley36570f42015-10-08 17:20:11 -0700248
249unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types,
250 const AidlInterface& interface,
251 const AidlMethod& method) {
252 const string i_name = ClassName(interface, ClassNames::INTERFACE);
253 const string bp_name = ClassName(interface, ClassNames::CLIENT);
254 unique_ptr<MethodImpl> ret{new MethodImpl{
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800255 kBinderStatusLiteral, bp_name, method.GetName(),
Christopher Wiley36570f42015-10-08 17:20:11 -0700256 ArgList{BuildArgList(types, method, true /* for method decl */)}}};
257 StatementBlock* b = ret->GetStatementBlock();
258
259 // Declare parcels to hold our query and the response.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800260 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700261 // Even if we're oneway, the transact method still takes a parcel.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800262 b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName));
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700263
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800264 // Declare the status_t variable we need for error handling.
Christopher Wiley10957122015-12-04 14:35:38 -0800265 b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral,
266 kAndroidStatusVarName,
267 kAndroidStatusOk));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800268 // We unconditionally return a Status object.
269 b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700270
Martijn Coenenf1b50782018-02-21 21:06:23 +0100271 if (interface.ShouldGenerateTraces()) {
272 b->AddLiteral(
273 StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")",
274 kTraceVarName, interface.GetName().c_str(), method.GetName().c_str()));
275 }
276
Christopher Wiley8993cb52015-10-21 09:53:24 -0700277 // Add the name of the interface we're hoping to call.
278 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800279 kAndroidStatusVarName,
280 new MethodCall(StringPrintf("%s.writeInterfaceToken",
281 kDataVarName),
282 "getInterfaceDescriptor()")));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800283 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley8993cb52015-10-21 09:53:24 -0700284
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700285 for (const auto& a: method.GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800286 const Type* type = a->GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700287 string var_name = ((a->IsOut()) ? "*" : "") + a->GetName();
Casey Dahlin389781f2015-10-22 13:13:21 -0700288 var_name = type->WriteCast(var_name);
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700289
290 if (a->IsIn()) {
291 // Serialization looks roughly like:
292 // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name);
293 // if (_aidl_ret_status != ::android::OK) { goto error; }
294 const string& method = type->WriteToParcelMethod();
295 b->AddStatement(new Assignment(
296 kAndroidStatusVarName,
297 new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()),
298 ArgList(var_name))));
299 b->AddStatement(GotoErrorOnBadStatus());
300 } else if (a->IsOut() && a->GetType().IsArray()) {
301 // Special case, the length of the out array is written into the parcel.
302 // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name);
303 // if (_aidl_ret_status != ::android::OK) { goto error; }
304 b->AddStatement(new Assignment(
305 kAndroidStatusVarName,
306 new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName),
307 ArgList(var_name))));
308 b->AddStatement(GotoErrorOnBadStatus());
309 }
Christopher Wiley36570f42015-10-08 17:20:11 -0700310 }
311
312 // Invoke the transaction on the remote binder and confirm status.
313 string transaction_code = StringPrintf(
314 "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str());
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700315
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800316 vector<string> args = {transaction_code, kDataVarName,
317 StringPrintf("&%s", kReplyVarName)};
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700318
319 if (interface.IsOneway() || method.IsOneway()) {
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800320 args.push_back("::android::IBinder::FLAG_ONEWAY");
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700321 }
322
Christopher Wiley36570f42015-10-08 17:20:11 -0700323 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800324 kAndroidStatusVarName,
Christopher Wiley36570f42015-10-08 17:20:11 -0700325 new MethodCall("remote()->transact",
Casey Dahlin0dd08af2015-10-20 18:45:50 -0700326 ArgList(args))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800327 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700328
Christopher Wiley1227d612015-10-26 16:59:20 -0700329 if (!interface.IsOneway() && !method.IsOneway()) {
330 // Strip off the exception header and fail if we see a remote exception.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800331 // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
332 // if (_aidl_ret_status != ::android::OK) { goto error; }
333 // if (!_aidl_status.isOk()) { return _aidl_ret_status; }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800334 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800335 kAndroidStatusVarName,
336 StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800337 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley1227d612015-10-26 16:59:20 -0700338 IfStatement* exception_check = new IfStatement(
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800339 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
Christopher Wiley1227d612015-10-26 16:59:20 -0700340 b->AddStatement(exception_check);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800341 exception_check->OnTrue()->AddLiteral(
342 StringPrintf("return %s", kStatusVarName));
Christopher Wiley1227d612015-10-26 16:59:20 -0700343 }
344
345 // Type checking should guarantee that nothing below emits code until "return
346 // status" if we are a oneway method, so no more fear of accessing reply.
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700347
Christopher Wiley36570f42015-10-08 17:20:11 -0700348 // If the method is expected to return something, read it first by convention.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800349 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wiley36570f42015-10-08 17:20:11 -0700350 if (return_type != types.VoidType()) {
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700351 const string& method_call = return_type->ReadFromParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700352 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800353 kAndroidStatusVarName,
354 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
355 method_call.c_str()),
356 ArgList(kReturnVarName))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800357 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700358 }
359
360 for (const AidlArgument* a : method.GetOutArguments()) {
361 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800362 // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name);
363 // if (_aidl_status != ::android::OK) { goto _aidl_error; }
Casey Dahlinb0966612015-10-19 16:35:26 -0700364 string method =
Casey Dahlina2f77c42015-12-01 18:26:02 -0800365 a->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700366
Christopher Wiley36570f42015-10-08 17:20:11 -0700367 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800368 kAndroidStatusVarName,
369 new MethodCall(StringPrintf("%s.%s", kReplyVarName,
370 method.c_str()),
371 ArgList(a->GetName()))));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800372 b->AddStatement(GotoErrorOnBadStatus());
Christopher Wiley36570f42015-10-08 17:20:11 -0700373 }
374
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800375 // If we've gotten to here, one of two things is true:
376 // 1) We've read some bad status_t
377 // 2) We've only read status_t == OK and there was no exception in the
378 // response.
379 // In both cases, we're free to set Status from the status_t and return.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800380 b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800381 b->AddLiteral(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800382 StringPrintf("%s.setFromStatusT(%s)", kStatusVarName,
383 kAndroidStatusVarName));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100384
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800385 b->AddLiteral(StringPrintf("return %s", kStatusVarName));
Christopher Wiley36570f42015-10-08 17:20:11 -0700386
387 return unique_ptr<Declaration>(ret.release());
388}
389
390} // namespace
391
Christopher Wileye3550c62015-09-29 13:26:10 -0700392unique_ptr<Document> BuildClientSource(const TypeNamespace& types,
Christopher Wiley36570f42015-10-08 17:20:11 -0700393 const AidlInterface& interface) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700394 vector<string> include_list = {
395 HeaderFile(interface, ClassNames::CLIENT, false),
396 kParcelHeader
397 };
Christopher Wiley36570f42015-10-08 17:20:11 -0700398 vector<unique_ptr<Declaration>> file_decls;
399
400 // The constructor just passes the IBinder instance up to the super
401 // class.
Christopher Wiley1db03482015-10-22 11:42:02 -0700402 const string i_name = ClassName(interface, ClassNames::INTERFACE);
Christopher Wiley36570f42015-10-08 17:20:11 -0700403 file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{
Christopher Wiley054afbd2015-10-16 17:08:43 -0700404 ClassName(interface, ClassNames::CLIENT),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800405 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
406 kImplVarName)},
407 { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}});
Christopher Wiley36570f42015-10-08 17:20:11 -0700408
409 // Clients define a method per transaction.
410 for (const auto& method : interface.GetMethods()) {
411 unique_ptr<Declaration> m = DefineClientTransaction(
412 types, interface, *method);
413 if (!m) { return nullptr; }
414 file_decls.push_back(std::move(m));
415 }
416 return unique_ptr<Document>{new CppSource{
417 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700418 NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700419}
420
Christopher Wileyad339272015-10-05 19:11:58 -0700421namespace {
422
423bool HandleServerTransaction(const TypeNamespace& types,
Martijn Coenenf1b50782018-02-21 21:06:23 +0100424 const AidlInterface& interface,
Christopher Wileyad339272015-10-05 19:11:58 -0700425 const AidlMethod& method,
426 StatementBlock* b) {
427 // Declare all the parameters now. In the common case, we expect no errors
428 // in serialization.
429 for (const unique_ptr<AidlArgument>& a : method.GetArguments()) {
Steven Moreland1c41e972018-07-09 16:07:00 -0700430 if (!DeclareLocalVariable(*a, b)) {
431 return false;
432 }
Christopher Wileyad339272015-10-05 19:11:58 -0700433 }
434
435 // Declare a variable to hold the return value.
Casey Dahlina2f77c42015-12-01 18:26:02 -0800436 const Type* return_type = method.GetType().GetLanguageType<Type>();
Christopher Wileyad339272015-10-05 19:11:58 -0700437 if (return_type != types.VoidType()) {
438 b->AddLiteral(StringPrintf(
Casey Dahlina2f77c42015-12-01 18:26:02 -0800439 "%s %s", return_type->CppType().c_str(),
Casey Dahlinb0966612015-10-19 16:35:26 -0700440 kReturnVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700441 }
442
Christopher Wiley8993cb52015-10-21 09:53:24 -0700443 // Check that the client is calling the correct interface.
444 IfStatement* interface_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800445 new MethodCall(StringPrintf("%s.checkInterface",
446 kDataVarName), "this"),
Christopher Wiley8993cb52015-10-21 09:53:24 -0700447 true /* invert the check */);
448 b->AddStatement(interface_check);
449 interface_check->OnTrue()->AddStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800450 new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE"));
Christopher Wiley8993cb52015-10-21 09:53:24 -0700451 interface_check->OnTrue()->AddLiteral("break");
452
Christopher Wileyad339272015-10-05 19:11:58 -0700453 // Deserialize each "in" parameter to the transaction.
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700454 for (const auto& a: method.GetArguments()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700455 // Deserialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800456 // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name);
457 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800458 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700459 const string& readMethod = type->ReadFromParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700460
Christopher Wiley74b7bf12016-08-19 11:06:32 -0700461 if (a->IsIn()) {
462 b->AddStatement(new Assignment{
463 kAndroidStatusVarName,
464 new MethodCall{string(kDataVarName) + "." + readMethod,
465 "&" + BuildVarName(*a)}});
466 b->AddStatement(BreakOnStatusNotOk());
467 } else if (a->IsOut() && a->GetType().IsArray()) {
468 // Special case, the length of the out array is written into the parcel.
469 // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name);
470 // if (_aidl_ret_status != ::android::OK) { break; }
471 b->AddStatement(new Assignment{
472 kAndroidStatusVarName,
473 new MethodCall{string(kDataVarName) + ".resizeOutVector",
474 "&" + BuildVarName(*a)}});
475 b->AddStatement(BreakOnStatusNotOk());
476 }
Christopher Wileyad339272015-10-05 19:11:58 -0700477 }
478
Martijn Coenenf1b50782018-02-21 21:06:23 +0100479 if (interface.ShouldGenerateTraces()) {
480 b->AddStatement(new Statement(new MethodCall("atrace_begin",
481 ArgList{{"ATRACE_TAG_AIDL",
482 StringPrintf("\"%s::%s::cppServer\"",
483 interface.GetName().c_str(),
484 method.GetName().c_str())}})));
485 }
486
Christopher Wileyad339272015-10-05 19:11:58 -0700487 // Call the actual method. This is implemented by the subclass.
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800488 vector<unique_ptr<AstNode>> status_args;
489 status_args.emplace_back(new MethodCall(
Christopher Wileyad339272015-10-05 19:11:58 -0700490 method.GetName(),
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800491 BuildArgList(types, method, false /* not for method decl */)));
492 b->AddStatement(new Statement(new MethodCall(
493 StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName),
494 ArgList(std::move(status_args)))));
Christopher Wileyad339272015-10-05 19:11:58 -0700495
Martijn Coenenf1b50782018-02-21 21:06:23 +0100496 if (interface.ShouldGenerateTraces()) {
497 b->AddStatement(new Statement(new MethodCall("atrace_end",
498 "ATRACE_TAG_AIDL")));
499 }
500
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800501 // Write exceptions during transaction handling to parcel.
502 if (!method.IsOneway()) {
503 b->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800504 kAndroidStatusVarName,
505 StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800506 b->AddStatement(BreakOnStatusNotOk());
507 IfStatement* exception_check = new IfStatement(
508 new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName)));
509 b->AddStatement(exception_check);
510 exception_check->OnTrue()->AddLiteral("break");
511 }
Casey Dahlinb0966612015-10-19 16:35:26 -0700512
Christopher Wiley36570f42015-10-08 17:20:11 -0700513 // If we have a return value, write it first.
514 if (return_type != types.VoidType()) {
Christopher Wiley2aaeda82015-10-19 15:16:49 -0700515 string writeMethod =
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800516 string(kReplyVarName) + "->" +
Casey Dahlina2f77c42015-12-01 18:26:02 -0800517 return_type->WriteToParcelMethod();
Christopher Wiley36570f42015-10-08 17:20:11 -0700518 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800519 kAndroidStatusVarName, new MethodCall{writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700520 ArgList{return_type->WriteCast(kReturnVarName)}}});
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700521 b->AddStatement(BreakOnStatusNotOk());
Christopher Wiley36570f42015-10-08 17:20:11 -0700522 }
523
Christopher Wileyad339272015-10-05 19:11:58 -0700524 // Write each out parameter to the reply parcel.
525 for (const AidlArgument* a : method.GetOutArguments()) {
526 // Serialization looks roughly like:
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800527 // _aidl_ret_status = data.WriteInt32(out_param_name);
528 // if (_aidl_ret_status != ::android::OK) { break; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800529 const Type* type = a->GetType().GetLanguageType<Type>();
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700530 const string& writeMethod = type->WriteToParcelMethod();
Casey Dahlinb0966612015-10-19 16:35:26 -0700531
Christopher Wileyad339272015-10-05 19:11:58 -0700532 b->AddStatement(new Assignment{
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800533 kAndroidStatusVarName,
534 new MethodCall{string(kReplyVarName) + "->" + writeMethod,
Casey Dahlin389781f2015-10-22 13:13:21 -0700535 type->WriteCast(BuildVarName(*a))}});
Christopher Wiley0eb903e2015-10-20 17:07:08 -0700536 b->AddStatement(BreakOnStatusNotOk());
Christopher Wileyad339272015-10-05 19:11:58 -0700537 }
538
539 return true;
540}
541
542} // namespace
543
Christopher Wileye3550c62015-09-29 13:26:10 -0700544unique_ptr<Document> BuildServerSource(const TypeNamespace& types,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700545 const AidlInterface& interface) {
546 const string bn_name = ClassName(interface, ClassNames::SERVER);
547 vector<string> include_list{
548 HeaderFile(interface, ClassNames::SERVER, false),
549 kParcelHeader
550 };
Christopher Wileyad339272015-10-05 19:11:58 -0700551 unique_ptr<MethodImpl> on_transact{new MethodImpl{
552 kAndroidStatusLiteral, bn_name, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800553 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
554 StringPrintf("const %s& %s", kAndroidParcelLiteral,
555 kDataVarName),
556 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
557 StringPrintf("uint32_t %s", kFlagsVarName)}}
Christopher Wiley36570f42015-10-08 17:20:11 -0700558 }};
Christopher Wileyad339272015-10-05 19:11:58 -0700559
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800560 // Declare the status_t variable
Christopher Wiley05f4f892015-10-14 13:30:43 -0700561 on_transact->GetStatementBlock()->AddLiteral(
Christopher Wiley10957122015-12-04 14:35:38 -0800562 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName,
563 kAndroidStatusOk));
Christopher Wiley05f4f892015-10-14 13:30:43 -0700564
Christopher Wileyad339272015-10-05 19:11:58 -0700565 // Add the all important switch statement, but retain a pointer to it.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800566 SwitchStatement* s = new SwitchStatement{kCodeVarName};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700567 on_transact->GetStatementBlock()->AddStatement(s);
Christopher Wileyad339272015-10-05 19:11:58 -0700568
569 // The switch statement has a case statement for each transaction code.
Christopher Wiley054afbd2015-10-16 17:08:43 -0700570 for (const auto& method : interface.GetMethods()) {
Christopher Wileyad339272015-10-05 19:11:58 -0700571 StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName()));
572 if (!b) { return nullptr; }
573
Martijn Coenenf1b50782018-02-21 21:06:23 +0100574 if (!HandleServerTransaction(types, interface, *method, b)) { return nullptr; }
Christopher Wileyad339272015-10-05 19:11:58 -0700575 }
576
577 // The switch statement has a default case which defers to the super class.
578 // The superclass handles a few pre-defined transactions.
579 StatementBlock* b = s->AddCase("");
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800580 b->AddLiteral(StringPrintf(
581 "%s = ::android::BBinder::onTransact(%s, %s, "
582 "%s, %s)", kAndroidStatusVarName, kCodeVarName,
583 kDataVarName, kReplyVarName, kFlagsVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700584
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800585 // If we saw a null reference, we can map that to an appropriate exception.
586 IfStatement* null_check = new IfStatement(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800587 new LiteralExpression(string(kAndroidStatusVarName) +
588 " == ::android::UNEXPECTED_NULL"));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800589 on_transact->GetStatementBlock()->AddStatement(null_check);
590 null_check->OnTrue()->AddStatement(new Assignment(
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800591 kAndroidStatusVarName,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800592 StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)"
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800593 ".writeToParcel(%s)",
594 kBinderStatusLiteral, kBinderStatusLiteral,
595 kReplyVarName)));
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800596
Christopher Wileyad339272015-10-05 19:11:58 -0700597 // Finally, the server's onTransact method just returns a status code.
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800598 on_transact->GetStatementBlock()->AddLiteral(
599 StringPrintf("return %s", kAndroidStatusVarName));
Christopher Wileyad339272015-10-05 19:11:58 -0700600
601 return unique_ptr<Document>{new CppSource{
602 include_list,
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700603 NestInNamespaces(std::move(on_transact), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700604}
605
Christopher Wileyf59c4992015-10-08 13:12:44 -0700606unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& /* types */,
Christopher Wiley054afbd2015-10-16 17:08:43 -0700607 const AidlInterface& interface) {
608 vector<string> include_list{
609 HeaderFile(interface, ClassNames::INTERFACE, false),
610 HeaderFile(interface, ClassNames::CLIENT, false),
611 };
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700612
Christopher Wiley054afbd2015-10-16 17:08:43 -0700613 string fq_name = ClassName(interface, ClassNames::INTERFACE);
614 if (!interface.GetPackage().empty()) {
615 fq_name = interface.GetPackage() + "." + fq_name;
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700616 }
617
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700618 vector<unique_ptr<Declaration>> decls;
619
Christopher Wiley11a9d792016-02-24 17:20:33 -0800620 unique_ptr<MacroDecl> meta_if{new MacroDecl{
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700621 "IMPLEMENT_META_INTERFACE",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700622 ArgList{vector<string>{ClassName(interface, ClassNames::BASE),
Christopher Wileyade4b452015-10-10 11:06:03 -0700623 '"' + fq_name + '"'}}}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700624 decls.push_back(std::move(meta_if));
625
626 for (const auto& constant: interface.GetStringConstants()) {
627 unique_ptr<MethodImpl> getter(new MethodImpl(
628 "const ::android::String16&",
629 ClassName(interface, ClassNames::INTERFACE),
630 constant->GetName(),
631 {}));
632 getter->GetStatementBlock()->AddLiteral(
633 StringPrintf("static const ::android::String16 value(%s)",
634 constant->GetValue().c_str()));
635 getter->GetStatementBlock()->AddLiteral("return value");
636 decls.push_back(std::move(getter));
637 }
Christopher Wiley1dd458d2015-09-30 11:05:52 -0700638
639 return unique_ptr<Document>{new CppSource{
640 include_list,
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700641 NestInNamespaces(std::move(decls), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700642}
643
Christopher Wileye3550c62015-09-29 13:26:10 -0700644unique_ptr<Document> BuildClientHeader(const TypeNamespace& types,
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700645 const AidlInterface& interface) {
646 const string i_name = ClassName(interface, ClassNames::INTERFACE);
647 const string bp_name = ClassName(interface, ClassNames::CLIENT);
Casey Dahlina834dd42015-09-23 11:52:15 -0700648
Christopher Wileyb23149d2015-10-14 13:52:21 -0700649 unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{
650 bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800651 ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s",
652 kImplVarName)},
Christopher Wileyb23149d2015-10-14 13:52:21 -0700653 ConstructorDecl::IS_EXPLICIT
654 }};
655 unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{
656 "~" + bp_name,
657 ArgList{},
658 ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}};
Casey Dahlina834dd42015-09-23 11:52:15 -0700659
Christopher Wileyf944e792015-09-29 10:00:46 -0700660 vector<unique_ptr<Declaration>> publics;
Casey Dahlina834dd42015-09-23 11:52:15 -0700661 publics.push_back(std::move(constructor));
662 publics.push_back(std::move(destructor));
663
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700664 for (const auto& method: interface.GetMethods()) {
Christopher Wiley36570f42015-10-08 17:20:11 -0700665 publics.push_back(BuildMethodDecl(*method, types, false));
Casey Dahlina834dd42015-09-23 11:52:15 -0700666 }
667
Christopher Wileyf944e792015-09-29 10:00:46 -0700668 unique_ptr<ClassDecl> bp_class{
669 new ClassDecl{bp_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800670 "::android::BpInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700671 std::move(publics),
672 {}
Casey Dahlina834dd42015-09-23 11:52:15 -0700673 }};
674
Christopher Wiley0c732db2015-09-29 14:36:44 -0700675 return unique_ptr<Document>{new CppHeader{
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700676 BuildHeaderGuard(interface, ClassNames::CLIENT),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700677 {kIBinderHeader,
678 kIInterfaceHeader,
679 "utils/Errors.h",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700680 HeaderFile(interface, ClassNames::INTERFACE, false)},
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700681 NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700682}
683
Christopher Wileyf59c4992015-10-08 13:12:44 -0700684unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */,
Christopher Wileyfd51d602015-10-14 13:04:48 -0700685 const AidlInterface& interface) {
686 const string i_name = ClassName(interface, ClassNames::INTERFACE);
687 const string bn_name = ClassName(interface, ClassNames::SERVER);
Casey Dahlin082f1d12015-09-21 14:06:25 -0700688
Christopher Wileyfd51d602015-10-14 13:04:48 -0700689 unique_ptr<Declaration> on_transact{new MethodDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700690 kAndroidStatusLiteral, "onTransact",
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800691 ArgList{{StringPrintf("uint32_t %s", kCodeVarName),
692 StringPrintf("const %s& %s", kAndroidParcelLiteral,
693 kDataVarName),
694 StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName),
695 StringPrintf("uint32_t %s = 0", kFlagsVarName)}},
Christopher Wileyfd51d602015-10-14 13:04:48 -0700696 MethodDecl::IS_OVERRIDE
697 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700698
Christopher Wileyf944e792015-09-29 10:00:46 -0700699 std::vector<unique_ptr<Declaration>> publics;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700700 publics.push_back(std::move(on_transact));
701
Christopher Wileyf944e792015-09-29 10:00:46 -0700702 unique_ptr<ClassDecl> bn_class{
703 new ClassDecl{bn_name,
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800704 "::android::BnInterface<" + i_name + ">",
Christopher Wileyf944e792015-09-29 10:00:46 -0700705 std::move(publics),
706 {}
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700707 }};
Casey Dahlin082f1d12015-09-21 14:06:25 -0700708
Christopher Wiley0c732db2015-09-29 14:36:44 -0700709 return unique_ptr<Document>{new CppHeader{
Christopher Wileyfd51d602015-10-14 13:04:48 -0700710 BuildHeaderGuard(interface, ClassNames::SERVER),
Christopher Wiley0c732db2015-09-29 14:36:44 -0700711 {"binder/IInterface.h",
Christopher Wiley054afbd2015-10-16 17:08:43 -0700712 HeaderFile(interface, ClassNames::INTERFACE, false)},
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700713 NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700714}
715
Christopher Wileye3550c62015-09-29 13:26:10 -0700716unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types,
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700717 const AidlInterface& interface) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700718 set<string> includes = { kIBinderHeader, kIInterfaceHeader,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800719 kStatusHeader, kStrongPointerHeader };
Casey Dahlince776cf2015-10-15 18:45:54 -0700720
721 for (const auto& method : interface.GetMethods()) {
722 for (const auto& argument : method->GetArguments()) {
Casey Dahlina2f77c42015-12-01 18:26:02 -0800723 const Type* type = argument->GetType().GetLanguageType<Type>();
724 type->GetHeaders(&includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700725 }
726
Casey Dahlina2f77c42015-12-01 18:26:02 -0800727 const Type* return_type = method->GetType().GetLanguageType<Type>();
728 return_type->GetHeaders(&includes);
Casey Dahlince776cf2015-10-15 18:45:54 -0700729 }
730
Christopher Wiley0c732db2015-09-29 14:36:44 -0700731 unique_ptr<ClassDecl> if_class{
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700732 new ClassDecl{ClassName(interface, ClassNames::INTERFACE),
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800733 "::android::IInterface"}};
Christopher Wiley11a9d792016-02-24 17:20:33 -0800734 if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{
Christopher Wileyade4b452015-10-10 11:06:03 -0700735 "DECLARE_META_INTERFACE",
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700736 ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}});
Christopher Wiley0c732db2015-09-29 14:36:44 -0700737
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800738 unique_ptr<Enum> constant_enum{new Enum{"", "int32_t"}};
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700739 for (const auto& constant : interface.GetIntConstants()) {
Christopher Wileyfd7dc032016-02-02 17:58:39 -0800740 constant_enum->AddValue(
741 constant->GetName(), std::to_string(constant->GetValue()));
742 }
743 if (constant_enum->HasValues()) {
744 if_class->AddPublic(std::move(constant_enum));
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800745 }
746
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700747 if (!interface.GetStringConstants().empty()) {
748 includes.insert(kString16Header);
749 }
Martijn Coenenf1b50782018-02-21 21:06:23 +0100750
751 if (interface.ShouldGenerateTraces()) {
752 includes.insert(kTraceHeader);
753 }
754
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700755 for (const auto& constant : interface.GetStringConstants()) {
756 unique_ptr<MethodDecl> getter(new MethodDecl(
757 "const ::android::String16&", constant->GetName(),
758 {}, MethodDecl::IS_STATIC));
759 if_class->AddPublic(std::move(getter));
760 }
761
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700762 if (!interface.GetMethods().empty()) {
763 unique_ptr<Enum> call_enum{new Enum{"Call"}};
764 for (const auto& method : interface.GetMethods()) {
765 // Each method gets an enum entry and pure virtual declaration.
766 if_class->AddPublic(BuildMethodDecl(*method, types, true));
767 call_enum->AddValue(
768 UpperCase(method->GetName()),
769 StringPrintf("::android::IBinder::FIRST_CALL_TRANSACTION + %d",
770 method->GetId()));
771 }
772 if_class->AddPublic(std::move(call_enum));
Christopher Wiley0c732db2015-09-29 14:36:44 -0700773 }
Christopher Wiley0c732db2015-09-29 14:36:44 -0700774
Christopher Wiley3bb6bc12015-10-14 10:58:27 -0700775 return unique_ptr<Document>{new CppHeader{
776 BuildHeaderGuard(interface, ClassNames::INTERFACE),
Casey Dahlince776cf2015-10-15 18:45:54 -0700777 vector<string>(includes.begin(), includes.end()),
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700778 NestInNamespaces(std::move(if_class), interface.GetSplitPackage())}};
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700779}
780
Steven Moreland5557f1c2018-07-02 13:50:23 -0700781std::unique_ptr<Document> BuildParcelHeader(const TypeNamespace& /*types*/,
782 const AidlStructuredParcelable& parcel) {
783 unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}};
784
785 set<string> includes = {kStatusHeader, kParcelHeader};
786 for (const auto& variable : parcel.GetFields()) {
787 const Type* type = variable->GetType().GetLanguageType<Type>();
788 type->GetHeaders(&includes);
789 }
790
791 for (const auto& variable : parcel.GetFields()) {
792 const Type* type = variable->GetType().GetLanguageType<Type>();
793
794 parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(
795 StringPrintf("%s %s;\n", type->CppType().c_str(), variable->GetName().c_str()))));
796 }
797
798 unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -0700799 ArgList("const ::android::Parcel* _aidl_parcel"),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700800 MethodDecl::IS_OVERRIDE));
801 parcel_class->AddPublic(std::move(read));
802 unique_ptr<MethodDecl> write(new MethodDecl(kAndroidStatusLiteral, "writeToParcel",
Steven Morelandce39c532018-07-11 16:59:50 -0700803 ArgList("::android::Parcel* _aidl_parcel"),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700804 MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST));
805 parcel_class->AddPublic(std::move(write));
806
807 return unique_ptr<Document>{new CppHeader{
808 BuildHeaderGuard(parcel, ClassNames::BASE), vector<string>(includes.begin(), includes.end()),
809 NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}};
810}
Steven Moreland1c41e972018-07-09 16:07:00 -0700811std::unique_ptr<Document> BuildParcelSource(const TypeNamespace& /*types*/,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700812 const AidlStructuredParcelable& parcel) {
813 unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(),
814 "readFromParcel",
Steven Morelandce39c532018-07-11 16:59:50 -0700815 ArgList("const ::android::Parcel* _aidl_parcel")}};
Steven Moreland5557f1c2018-07-02 13:50:23 -0700816 StatementBlock* read_block = read->GetStatementBlock();
817 read_block->AddLiteral(
818 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
819 for (const auto& variable : parcel.GetFields()) {
820 string method = variable->GetType().GetLanguageType<Type>()->ReadFromParcelMethod();
821
822 read_block->AddStatement(new Assignment(
823 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
824 ArgList("&" + variable->GetName()))));
825 read_block->AddStatement(ReturnOnStatusNotOk());
826 }
827 read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
828
Steven Morelandce39c532018-07-11 16:59:50 -0700829 unique_ptr<MethodImpl> write{
830 new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel",
831 ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}};
Steven Moreland5557f1c2018-07-02 13:50:23 -0700832 StatementBlock* write_block = write->GetStatementBlock();
833 write_block->AddLiteral(
834 StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk));
835 for (const auto& variable : parcel.GetFields()) {
836 string method = variable->GetType().GetLanguageType<Type>()->WriteToParcelMethod();
837
838 write_block->AddStatement(new Assignment(
839 kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()),
840 ArgList(variable->GetName()))));
841 write_block->AddStatement(ReturnOnStatusNotOk());
842 }
843 write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName));
844
845 vector<unique_ptr<Declaration>> file_decls;
846 file_decls.push_back(std::move(read));
847 file_decls.push_back(std::move(write));
848
849 set<string> includes = {};
850 parcel.GetLanguageType<Type>()->GetHeaders(&includes);
851
852 return unique_ptr<Document>{
853 new CppSource{vector<string>(includes.begin(), includes.end()),
854 NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}};
855}
856
Christopher Wiley054afbd2015-10-16 17:08:43 -0700857bool WriteHeader(const CppOptions& options,
858 const TypeNamespace& types,
859 const AidlInterface& interface,
860 const IoDelegate& io_delegate,
861 ClassNames header_type) {
862 unique_ptr<Document> header;
863 switch (header_type) {
864 case ClassNames::INTERFACE:
865 header = BuildInterfaceHeader(types, interface);
866 break;
867 case ClassNames::CLIENT:
868 header = BuildClientHeader(types, interface);
869 break;
870 case ClassNames::SERVER:
871 header = BuildServerHeader(types, interface);
872 break;
873 default:
874 LOG(FATAL) << "aidl internal error";
875 }
876 if (!header) {
877 LOG(ERROR) << "aidl internal error: Failed to generate header.";
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700878 return false;
879 }
Christopher Wiley054afbd2015-10-16 17:08:43 -0700880
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800881 const string header_path = options.OutputHeaderDir() + OS_PATH_SEPARATOR +
882 HeaderFile(interface, header_type);
883 unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path));
884 header->Write(code_writer.get());
Christopher Wiley054afbd2015-10-16 17:08:43 -0700885
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800886 const bool success = code_writer->Close();
887 if (!success) {
888 io_delegate.RemovePath(header_path);
889 }
890
891 return success;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700892}
893
Casey Dahlina834dd42015-09-23 11:52:15 -0700894} // namespace internals
895
896using namespace internals;
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700897
Steven Moreland5557f1c2018-07-02 13:50:23 -0700898string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type, bool use_os_sep) {
899 string file_path = defined_type.GetPackage();
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800900 for (char& c: file_path) {
901 if (c == '.') {
902 c = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
903 }
904 }
905 if (!file_path.empty()) {
906 file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/';
907 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700908 file_path += ClassName(defined_type, class_type);
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800909 file_path += ".h";
910
911 return file_path;
912}
913
Steven Moreland5557f1c2018-07-02 13:50:23 -0700914bool GenerateCppInterface(const CppOptions& options, const TypeNamespace& types,
915 const AidlInterface& interface, const IoDelegate& io_delegate) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700916 auto interface_src = BuildInterfaceSource(types, interface);
917 auto client_src = BuildClientSource(types, interface);
918 auto server_src = BuildServerSource(types, interface);
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700919
Christopher Wiley054afbd2015-10-16 17:08:43 -0700920 if (!interface_src || !client_src || !server_src) {
921 return false;
922 }
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700923
Christopher Wiley054afbd2015-10-16 17:08:43 -0700924 if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(),
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700925 interface.GetSplitPackage())) {
Christopher Wiley054afbd2015-10-16 17:08:43 -0700926 LOG(ERROR) << "Failed to create directory structure for headers.";
927 return false;
928 }
929
930 if (!WriteHeader(options, types, interface, io_delegate,
931 ClassNames::INTERFACE) ||
932 !WriteHeader(options, types, interface, io_delegate,
933 ClassNames::CLIENT) ||
934 !WriteHeader(options, types, interface, io_delegate,
935 ClassNames::SERVER)) {
936 return false;
937 }
938
Christopher Wiley054afbd2015-10-16 17:08:43 -0700939 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(
940 options.OutputCppFilePath());
941 interface_src->Write(writer.get());
942 client_src->Write(writer.get());
943 server_src->Write(writer.get());
944
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800945 const bool success = writer->Close();
946 if (!success) {
947 io_delegate.RemovePath(options.OutputCppFilePath());
948 }
949
950 return success;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700951}
952
Steven Moreland5557f1c2018-07-02 13:50:23 -0700953bool GenerateCppParcel(const CppOptions& options, const cpp::TypeNamespace& types,
954 const AidlStructuredParcelable& parcelable, const IoDelegate& io_delegate) {
955 auto header = BuildParcelHeader(types, parcelable);
956 auto source = BuildParcelSource(types, parcelable);
957
958 if (!header || !source) {
959 return false;
960 }
961
962 if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), parcelable.GetSplitPackage())) {
963 LOG(ERROR) << "Failed to create directory structure for headers.";
964 }
965
966 const string header_path =
967 options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::BASE);
968 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
969 header->Write(header_writer.get());
970 CHECK(header_writer->Close());
971
972 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(options.OutputCppFilePath());
973 source->Write(source_writer.get());
974 CHECK(source_writer->Close());
975
976 return true;
977}
978
979bool GenerateCpp(const CppOptions& options, const TypeNamespace& types,
980 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
981 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
982 if (parcelable != nullptr) {
983 return GenerateCppParcel(options, types, *parcelable, io_delegate);
984 }
985
986 const AidlInterface* interface = defined_type.AsInterface();
987 if (interface != nullptr) {
988 return GenerateCppInterface(options, types, *interface, io_delegate);
989 }
990
991 CHECK(false) << "Unrecognized type sent for cpp generation.";
992 return false;
993}
994
Christopher Wileyf944e792015-09-29 10:00:46 -0700995} // namespace cpp
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700996} // namespace aidl
Christopher Wileyf944e792015-09-29 10:00:46 -0700997} // namespace android