Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 1 | /* |
| 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" |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 18 | #include "parse_helpers.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 19 | |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 20 | #include <cctype> |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 21 | #include <cstring> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 22 | #include <memory> |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 23 | #include <random> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 26 | #include <base/stringprintf.h> |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 27 | #include <base/strings.h> |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 28 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 29 | #include "aidl_language.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 30 | #include "ast_cpp.h" |
| 31 | #include "code_writer.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 32 | #include "logging.h" |
| 33 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 35 | using android::base::Join; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 36 | using std::string; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 37 | using std::unique_ptr; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 38 | using std::vector; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 39 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 42 | namespace cpp { |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 43 | namespace internals { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 44 | namespace { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 45 | const char kStatusOkOrBreakCheck[] = "if (status != android::OK) { break; }"; |
| 46 | const char kReturnVarName[] = "_aidl_return"; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 47 | const char kAndroidStatusLiteral[] = "android::status_t"; |
| 48 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 49 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 50 | const char kParcelHeader[] = "binder/Parcel.h"; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 51 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 52 | string UpperCase(const std::string& s) { |
| 53 | string result = s; |
| 54 | for (char& c : result) |
| 55 | c = toupper(c); |
| 56 | return result; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 57 | } |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 58 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 59 | string BuildVarName(const AidlArgument& a) { |
| 60 | string prefix = "out_"; |
| 61 | if (a.GetDirection() & AidlArgument::IN_DIR) { |
| 62 | prefix = "in_"; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 63 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 64 | return prefix + a.GetName(); |
| 65 | } |
| 66 | |
| 67 | vector<string> BuildArgList(const TypeNamespace& types, |
| 68 | const AidlMethod& method, |
| 69 | bool for_declaration) { |
| 70 | // Build up the argument list for the server method call. |
| 71 | vector<string> method_arguments; |
| 72 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 73 | string literal; |
| 74 | if (for_declaration) { |
| 75 | // Method declarations need types, pointers to out params, and variable |
| 76 | // names that match the .aidl specification. |
| 77 | const Type* type = types.Find(a->GetType().GetName()); |
| 78 | literal = StringPrintf( |
| 79 | "%s%s %s", type->CppType().c_str(), |
| 80 | (a->IsOut()) ? "*" : "", |
| 81 | a->GetName().c_str()); |
| 82 | } else { |
| 83 | if (a->IsOut()) { literal = "&"; } |
| 84 | literal += BuildVarName(*a); |
| 85 | } |
| 86 | method_arguments.push_back(literal); |
| 87 | } |
| 88 | |
| 89 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 90 | if (return_type != types.VoidType()) { |
| 91 | if (for_declaration) { |
| 92 | method_arguments.push_back( |
| 93 | StringPrintf("%s* %s", return_type->CppType().c_str(), |
| 94 | kReturnVarName)); |
| 95 | } else { |
| 96 | method_arguments.push_back(string{"&"} + kReturnVarName); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return method_arguments; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 103 | unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 104 | const TypeNamespace& types, |
| 105 | bool for_interface) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 106 | uint32_t modifiers = 0; |
| 107 | if (for_interface) { |
| 108 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 109 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 110 | } else { |
| 111 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 112 | } |
| 113 | |
| 114 | return unique_ptr<Declaration>{ |
| 115 | new MethodDecl{kAndroidStatusLiteral, |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 116 | method.GetName(), |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 117 | BuildArgList(types, method, true /* for method decl */), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 118 | modifiers}}; |
| 119 | } |
| 120 | |
| 121 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl) { |
| 122 | using N = CppNamespace; |
| 123 | using NPtr = unique_ptr<N>; |
| 124 | return NPtr{new N{"android", NPtr{new N{"generated", std::move(decl)}}}}; |
| 125 | } |
| 126 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 127 | bool DeclareLocalVariable(const TypeNamespace& types, const AidlArgument& a, |
| 128 | StatementBlock* b) { |
| 129 | const Type* cpp_type = types.Find(a.GetType().GetName()); |
| 130 | if (!cpp_type) { return false; } |
| 131 | |
| 132 | b->AddLiteral(cpp_type->CppType() + " " + BuildVarName(a)); |
| 133 | return true; |
| 134 | } |
| 135 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 136 | } // namespace |
| 137 | |
| 138 | enum class ClassNames { BASE, CLIENT, SERVER, INTERFACE }; |
| 139 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 140 | string ClassName(const AidlInterface& interface, ClassNames type) { |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 141 | string c_name = interface.GetName(); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 142 | |
| 143 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 144 | c_name = c_name.substr(1); |
| 145 | |
| 146 | switch (type) { |
| 147 | case ClassNames::CLIENT: |
| 148 | c_name = "Bp" + c_name; |
| 149 | break; |
| 150 | case ClassNames::SERVER: |
| 151 | c_name = "Bn" + c_name; |
| 152 | break; |
| 153 | case ClassNames::INTERFACE: |
| 154 | c_name = "I" + c_name; |
| 155 | break; |
| 156 | case ClassNames::BASE: |
| 157 | break; |
| 158 | } |
| 159 | return c_name; |
| 160 | } |
| 161 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 162 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 163 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 164 | unique_ptr<CppNamespace> ns{new CppNamespace{"android"}}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 165 | return unique_ptr<Document>{new CppSource{ {}, std::move(ns)}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 168 | namespace { |
| 169 | |
| 170 | bool HandleServerTransaction(const TypeNamespace& types, |
| 171 | const AidlMethod& method, |
| 172 | StatementBlock* b) { |
| 173 | // Declare all the parameters now. In the common case, we expect no errors |
| 174 | // in serialization. |
| 175 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 176 | if (!DeclareLocalVariable(types, *a, b)) { return false; } |
| 177 | } |
| 178 | |
| 179 | // Declare a variable to hold the return value. |
| 180 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 181 | if (return_type != types.VoidType()) { |
| 182 | b->AddLiteral(StringPrintf( |
| 183 | "%s %s", return_type->CppType().c_str(), kReturnVarName)); |
| 184 | } |
| 185 | |
| 186 | // Declare the status variable |
| 187 | b->AddLiteral(StringPrintf("%s status", kAndroidStatusLiteral)); |
| 188 | |
| 189 | // Deserialize each "in" parameter to the transaction. |
| 190 | for (const AidlArgument* a : method.GetInArguments()) { |
| 191 | // Deserialization looks roughly like: |
| 192 | // status = data.ReadInt32(&in_param_name); |
| 193 | // if (status != android::OK) { break; } |
| 194 | const Type* type = types.Find(a->GetType().GetName()); |
| 195 | b->AddStatement(new Assignment{ |
| 196 | "status", |
| 197 | new MethodCall{"data." + type->ReadFromParcelMethod(), |
| 198 | "&" + BuildVarName(*a)}}); |
| 199 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 200 | } |
| 201 | |
| 202 | // Call the actual method. This is implemented by the subclass. |
| 203 | b->AddStatement(new Assignment{ |
| 204 | "status", new MethodCall{ |
| 205 | method.GetName(), |
| 206 | new ArgList{BuildArgList(types, method, |
| 207 | false /* not for method decl */)}}}); |
| 208 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 209 | |
| 210 | // Write each out parameter to the reply parcel. |
| 211 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 212 | // Serialization looks roughly like: |
| 213 | // status = data.WriteInt32(out_param_name); |
| 214 | // if (status != android::OK) { break; } |
| 215 | const Type* type = types.Find(a->GetType().GetName()); |
| 216 | b->AddStatement(new Assignment{ |
| 217 | "status", |
| 218 | new MethodCall{"reply->" + type->WriteToParcelMethod(), |
| 219 | BuildVarName(*a)}}); |
| 220 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 221 | } |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | } // namespace |
| 227 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 228 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 229 | const AidlInterface& parsed_doc) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame^] | 230 | const string bn_name = ClassName(parsed_doc, ClassNames::SERVER); |
| 231 | vector<string> include_list{bn_name + ".h", kParcelHeader}; |
| 232 | unique_ptr<MethodImpl> on_transact{new MethodImpl{ |
| 233 | kAndroidStatusLiteral, bn_name, "onTransact", |
| 234 | {"uint32_t code", |
| 235 | "const android::Parcel& data", |
| 236 | "android::Parcel* reply", |
| 237 | "uint32_t flags"} |
| 238 | }}; |
| 239 | |
| 240 | // Add the all important switch statement, but retain a pointer to it. |
| 241 | SwitchStatement* s = new SwitchStatement{"code"}; |
| 242 | on_transact->AddStatement(unique_ptr<AstNode>{s}); |
| 243 | |
| 244 | // The switch statement has a case statement for each transaction code. |
| 245 | for (const auto& method : parsed_doc.GetMethods()) { |
| 246 | StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName())); |
| 247 | if (!b) { return nullptr; } |
| 248 | |
| 249 | if (!HandleServerTransaction(types, *method, b)) { return nullptr; } |
| 250 | } |
| 251 | |
| 252 | // The switch statement has a default case which defers to the super class. |
| 253 | // The superclass handles a few pre-defined transactions. |
| 254 | StatementBlock* b = s->AddCase(""); |
| 255 | b->AddLiteral( |
| 256 | "status = android::BBinder::onTransact(code, data, reply, flags)"); |
| 257 | |
| 258 | // Finally, the server's onTransact method just returns a status code. |
| 259 | on_transact->AddStatement(unique_ptr<AstNode>{ |
| 260 | new LiteralStatement{"return status"}}); |
| 261 | |
| 262 | return unique_ptr<Document>{new CppSource{ |
| 263 | include_list, |
| 264 | NestInNamespaces(std::move(on_transact))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 267 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 268 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 269 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 270 | const string bp_name = ClassName(parsed_doc, ClassNames::CLIENT); |
| 271 | vector<string> include_list{i_name + ".h", bp_name + ".h"}; |
| 272 | |
| 273 | string fq_name = i_name; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 274 | if (!parsed_doc.GetPackage().empty()) { |
| 275 | fq_name = StringPrintf("%s.%s", parsed_doc.GetPackage().c_str(), i_name.c_str()); |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | unique_ptr<ConstructorDecl> meta_if{new ConstructorDecl{ |
| 279 | "IMPLEMENT_META_INTERFACE", |
| 280 | {ClassName(parsed_doc, ClassNames::BASE), '"' + fq_name + '"'} |
| 281 | }}; |
| 282 | |
| 283 | return unique_ptr<Document>{new CppSource{ |
| 284 | include_list, |
| 285 | NestInNamespaces(std::move(meta_if))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 288 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 289 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 290 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 291 | const string bp_name = ClassName(parsed_doc, ClassNames::CLIENT); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 292 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 293 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl(bp_name, {})}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 294 | unique_ptr<ConstructorDecl> destructor{ |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 295 | new ConstructorDecl("~" + bp_name, {})}; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 296 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 297 | vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 298 | publics.push_back(std::move(constructor)); |
| 299 | publics.push_back(std::move(destructor)); |
| 300 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 301 | for (const auto& item : parsed_doc.GetMethods()) { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 302 | publics.push_back(BuildMethodDecl(*item, types, false)); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 305 | unique_ptr<ClassDecl> bp_class{ |
| 306 | new ClassDecl{bp_name, |
| 307 | "public android::BpInterface<" + i_name + ">", |
| 308 | std::move(publics), |
| 309 | {} |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 310 | }}; |
| 311 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 312 | return unique_ptr<Document>{new CppHeader{ |
| 313 | bp_name + "_H", |
| 314 | {kIBinderHeader, |
| 315 | kIInterfaceHeader, |
| 316 | "utils/Errors.h", |
| 317 | i_name + ".h"}, |
| 318 | NestInNamespaces(std::move(bp_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 321 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 322 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 323 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 324 | const string bn_name = ClassName(parsed_doc, ClassNames::SERVER); |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 325 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 326 | unique_ptr<Declaration> on_transact{ |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 327 | new MethodDecl(kAndroidStatusLiteral, "onTransact", |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 328 | { "uint32_t code", |
| 329 | "const android::Parcel& data", |
| 330 | "android::Parcel* reply", |
| 331 | "uint32_t flags = 0" |
| 332 | })}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 333 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 334 | std::vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 335 | publics.push_back(std::move(on_transact)); |
| 336 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 337 | unique_ptr<ClassDecl> bn_class{ |
| 338 | new ClassDecl{bn_name, |
| 339 | "public android::BnInterface<" + i_name + ">", |
| 340 | std::move(publics), |
| 341 | {} |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 342 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 343 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 344 | return unique_ptr<Document>{new CppHeader{ |
| 345 | bn_name + "_H", |
| 346 | {"binder/IInterface.h", |
| 347 | i_name + ".h"}, |
| 348 | NestInNamespaces(std::move(bn_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 351 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 352 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 353 | unique_ptr<ClassDecl> if_class{ |
| 354 | new ClassDecl{ClassName(parsed_doc, ClassNames::INTERFACE), |
| 355 | "public android::IInterface"}}; |
| 356 | if_class->AddPublic(unique_ptr<Declaration>{ |
| 357 | new ConstructorDecl{ |
| 358 | "DECLARE_META_INTERFACE", |
| 359 | {ClassName(parsed_doc, ClassNames::BASE)}}}); |
| 360 | |
| 361 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 362 | for (const auto& method : parsed_doc.GetMethods()) { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 363 | if_class->AddPublic(BuildMethodDecl(*method, types, true)); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 364 | call_enum->AddValue( |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 365 | UpperCase(method->GetName()), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 366 | StringPrintf("android::IBinder::FIRST_CALL_TRANSACTION + %d", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 367 | method->GetId())); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 368 | } |
| 369 | if_class->AddPublic(std::move(call_enum)); |
| 370 | |
| 371 | return unique_ptr<Document>{new CppSource{ |
| 372 | {kIBinderHeader, |
| 373 | kIInterfaceHeader}, |
| 374 | NestInNamespaces(std::move(if_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 377 | bool GenerateCppForFile(const std::string& name, unique_ptr<Document> doc) { |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 378 | if (!doc) { |
| 379 | return false; |
| 380 | } |
| 381 | unique_ptr<CodeWriter> writer = GetFileWriter(name); |
| 382 | doc->Write(writer.get()); |
| 383 | return true; |
| 384 | } |
| 385 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 386 | } // namespace internals |
| 387 | |
| 388 | using namespace internals; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 389 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 390 | bool GenerateCpp(const CppOptions& options, |
| 391 | const TypeNamespace& types, |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 392 | const AidlInterface& parsed_doc) { |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 393 | bool success = true; |
| 394 | |
| 395 | success &= GenerateCppForFile(options.ClientCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 396 | BuildClientSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 397 | success &= GenerateCppForFile(options.ClientHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 398 | BuildClientHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 399 | success &= GenerateCppForFile(options.ServerCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 400 | BuildServerSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 401 | success &= GenerateCppForFile(options.ServerHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 402 | BuildServerHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 403 | success &= GenerateCppForFile(options.InterfaceCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 404 | BuildInterfaceSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 405 | success &= GenerateCppForFile(options.InterfaceHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 406 | BuildInterfaceHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 407 | |
| 408 | return success; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 411 | } // namespace cpp |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 412 | } // namespace aidl |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 413 | } // namespace android |