| 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" |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 18 | |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 19 | #include <cctype> |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 20 | #include <cstring> |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 21 | #include <memory> |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 22 | #include <random> |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 23 | #include <set> |
| 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; |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 39 | using std::set; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 40 | |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace aidl { |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 43 | namespace cpp { |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 44 | namespace internals { |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 45 | namespace { |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 46 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 47 | const char kStatusOkOrBreakCheck[] = "if (status != android::OK) { break; }"; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 48 | const char kStatusOkOrReturnLiteral[] = |
| 49 | "if (status != android::OK) { return status; }"; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 50 | const char kReturnVarName[] = "_aidl_return"; |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 51 | const char kAndroidStatusLiteral[] = "android::status_t"; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 52 | const char kAndroidParcelLiteral[] = "android::Parcel"; |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 53 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 54 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 55 | const char kParcelHeader[] = "binder/Parcel.h"; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 56 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 57 | string UpperCase(const std::string& s) { |
| 58 | string result = s; |
| 59 | for (char& c : result) |
| 60 | c = toupper(c); |
| 61 | return result; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 62 | } |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 63 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 64 | string BuildVarName(const AidlArgument& a) { |
| 65 | string prefix = "out_"; |
| 66 | if (a.GetDirection() & AidlArgument::IN_DIR) { |
| 67 | prefix = "in_"; |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 68 | } |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 69 | return prefix + a.GetName(); |
| 70 | } |
| 71 | |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 72 | ArgList BuildArgList(const TypeNamespace& types, |
| 73 | const AidlMethod& method, |
| 74 | bool for_declaration) { |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 75 | // Build up the argument list for the server method call. |
| 76 | vector<string> method_arguments; |
| 77 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 78 | string literal; |
| 79 | if (for_declaration) { |
| 80 | // Method declarations need types, pointers to out params, and variable |
| 81 | // names that match the .aidl specification. |
| 82 | const Type* type = types.Find(a->GetType().GetName()); |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 83 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 84 | literal = StringPrintf( |
| 85 | "%s%s %s", type->CppType().c_str(), |
| 86 | (a->IsOut()) ? "*" : "", |
| 87 | a->GetName().c_str()); |
| 88 | } else { |
| 89 | if (a->IsOut()) { literal = "&"; } |
| 90 | literal += BuildVarName(*a); |
| 91 | } |
| 92 | method_arguments.push_back(literal); |
| 93 | } |
| 94 | |
| 95 | const Type* return_type = types.Find(method.GetType().GetName()); |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 96 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 97 | if (return_type != types.VoidType()) { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 98 | string literal; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 99 | if (for_declaration) { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 100 | literal = StringPrintf( |
| 101 | "%s* %s", return_type->CppType().c_str(), kReturnVarName); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 102 | } else { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 103 | literal = string{"&"} + kReturnVarName; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 104 | } |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 105 | method_arguments.push_back(literal); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 108 | return ArgList(method_arguments); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 111 | unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 112 | const TypeNamespace& types, |
| 113 | bool for_interface) { |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 114 | uint32_t modifiers = 0; |
| 115 | if (for_interface) { |
| 116 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 117 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 118 | } else { |
| 119 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 120 | } |
| 121 | |
| 122 | return unique_ptr<Declaration>{ |
| 123 | new MethodDecl{kAndroidStatusLiteral, |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 124 | method.GetName(), |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 125 | BuildArgList(types, method, true /* for method decl */), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 126 | modifiers}}; |
| 127 | } |
| 128 | |
| 129 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl) { |
| 130 | using N = CppNamespace; |
| 131 | using NPtr = unique_ptr<N>; |
| 132 | return NPtr{new N{"android", NPtr{new N{"generated", std::move(decl)}}}}; |
| 133 | } |
| 134 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 135 | unique_ptr<CppNamespace> NestInNamespaces( |
| 136 | vector<unique_ptr<Declaration>> decls) { |
| 137 | using N = CppNamespace; |
| 138 | using NPtr = unique_ptr<N>; |
| 139 | return NPtr{new N{"android", NPtr{new N{"generated", std::move(decls)}}}}; |
| 140 | } |
| 141 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 142 | bool DeclareLocalVariable(const TypeNamespace& types, const AidlArgument& a, |
| 143 | StatementBlock* b) { |
| 144 | const Type* cpp_type = types.Find(a.GetType().GetName()); |
| 145 | if (!cpp_type) { return false; } |
| 146 | |
| 147 | b->AddLiteral(cpp_type->CppType() + " " + BuildVarName(a)); |
| 148 | return true; |
| 149 | } |
| 150 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 151 | enum class ClassNames { BASE, CLIENT, SERVER, INTERFACE }; |
| 152 | |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 153 | string ClassName(const AidlInterface& interface, ClassNames type) { |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 154 | string c_name = interface.GetName(); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 155 | |
| 156 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 157 | c_name = c_name.substr(1); |
| 158 | |
| 159 | switch (type) { |
| 160 | case ClassNames::CLIENT: |
| 161 | c_name = "Bp" + c_name; |
| 162 | break; |
| 163 | case ClassNames::SERVER: |
| 164 | c_name = "Bn" + c_name; |
| 165 | break; |
| 166 | case ClassNames::INTERFACE: |
| 167 | c_name = "I" + c_name; |
| 168 | break; |
| 169 | case ClassNames::BASE: |
| 170 | break; |
| 171 | } |
| 172 | return c_name; |
| 173 | } |
| 174 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 175 | string BuildHeaderGuard(const AidlInterface& interface, |
| 176 | ClassNames header_type) { |
| 177 | string class_name = ClassName(interface, header_type); |
| 178 | for (size_t i = 1; i < class_name.size(); ++i) { |
| 179 | if (isupper(class_name[i])) { |
| 180 | class_name.insert(i, "_"); |
| 181 | ++i; |
| 182 | } |
| 183 | } |
| 184 | string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", |
| 185 | interface.GetPackage().c_str(), |
| 186 | class_name.c_str()); |
| 187 | for (char& c : ret) { |
| 188 | if (c == '.') { |
| 189 | c = '_'; |
| 190 | } |
| 191 | c = toupper(c); |
| 192 | } |
| 193 | return ret; |
| 194 | } |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 195 | |
| 196 | unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types, |
| 197 | const AidlInterface& interface, |
| 198 | const AidlMethod& method) { |
| 199 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 200 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| 201 | unique_ptr<MethodImpl> ret{new MethodImpl{ |
| 202 | kAndroidStatusLiteral, bp_name, method.GetName(), |
| 203 | ArgList{BuildArgList(types, method, true /* for method decl */)}}}; |
| 204 | StatementBlock* b = ret->GetStatementBlock(); |
| 205 | |
| 206 | // Declare parcels to hold our query and the response. |
| 207 | b->AddLiteral(StringPrintf("%s data", kAndroidParcelLiteral)); |
| 208 | b->AddLiteral(StringPrintf("%s reply", kAndroidParcelLiteral)); |
| 209 | // And declare the status variable we need for error handling. |
| 210 | b->AddLiteral(StringPrintf("%s status", kAndroidStatusLiteral)); |
| 211 | |
| 212 | // Serialization looks roughly like: |
| 213 | // status = data.WriteInt32(in_param_name); |
| 214 | // if (status != android::OK) { return status; } |
| 215 | for (const AidlArgument* a : method.GetInArguments()) { |
| 216 | string method = types.Find(a->GetType().GetName())->WriteToParcelMethod(); |
| 217 | string var_name = ((a->IsOut()) ? "*" : "") + a->GetName(); |
| 218 | b->AddStatement(new Assignment( |
| 219 | "status", |
| 220 | new MethodCall("data." + method, ArgList(var_name)))); |
| 221 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 222 | } |
| 223 | |
| 224 | // Invoke the transaction on the remote binder and confirm status. |
| 225 | string transaction_code = StringPrintf( |
| 226 | "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str()); |
| 227 | b->AddStatement(new Assignment( |
| 228 | "status", |
| 229 | new MethodCall("remote()->transact", |
| 230 | ArgList({transaction_code, "data", "&reply"})))); |
| 231 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 232 | |
| 233 | // If the method is expected to return something, read it first by convention. |
| 234 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 235 | if (return_type != types.VoidType()) { |
| 236 | string method = return_type->ReadFromParcelMethod(); |
| 237 | b->AddStatement(new Assignment( |
| 238 | "status", |
| 239 | new MethodCall("reply." + method, ArgList(kReturnVarName)))); |
| 240 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 241 | } |
| 242 | |
| 243 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 244 | // Deserialization looks roughly like: |
| 245 | // status = reply.ReadInt32(out_param_name); |
| 246 | // if (status != android::OK) { return status; } |
| 247 | string method = types.Find(a->GetType().GetName())->ReadFromParcelMethod(); |
| 248 | b->AddStatement(new Assignment( |
| 249 | "status", |
| 250 | new MethodCall("reply." + method, ArgList(a->GetName())))); |
| 251 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 252 | } |
| 253 | |
| 254 | b->AddLiteral("return status"); |
| 255 | |
| 256 | return unique_ptr<Declaration>(ret.release()); |
| 257 | } |
| 258 | |
| 259 | } // namespace |
| 260 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 261 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 262 | const AidlInterface& interface) { |
| 263 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| 264 | vector<string> include_list = { bp_name + ".h", kParcelHeader }; |
| 265 | vector<unique_ptr<Declaration>> file_decls; |
| 266 | |
| 267 | // The constructor just passes the IBinder instance up to the super |
| 268 | // class. |
| 269 | file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{ |
| 270 | bp_name, |
| 271 | ArgList{"const android::sp<android::IBinder>& impl"}, |
| 272 | { "BpInterface<IPingResponder>(impl)" }}}); |
| 273 | |
| 274 | // Clients define a method per transaction. |
| 275 | for (const auto& method : interface.GetMethods()) { |
| 276 | unique_ptr<Declaration> m = DefineClientTransaction( |
| 277 | types, interface, *method); |
| 278 | if (!m) { return nullptr; } |
| 279 | file_decls.push_back(std::move(m)); |
| 280 | } |
| 281 | return unique_ptr<Document>{new CppSource{ |
| 282 | include_list, |
| 283 | NestInNamespaces(std::move(file_decls))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 286 | namespace { |
| 287 | |
| 288 | bool HandleServerTransaction(const TypeNamespace& types, |
| 289 | const AidlMethod& method, |
| 290 | StatementBlock* b) { |
| 291 | // Declare all the parameters now. In the common case, we expect no errors |
| 292 | // in serialization. |
| 293 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 294 | if (!DeclareLocalVariable(types, *a, b)) { return false; } |
| 295 | } |
| 296 | |
| 297 | // Declare a variable to hold the return value. |
| 298 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 299 | if (return_type != types.VoidType()) { |
| 300 | b->AddLiteral(StringPrintf( |
| 301 | "%s %s", return_type->CppType().c_str(), kReturnVarName)); |
| 302 | } |
| 303 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 304 | // Deserialize each "in" parameter to the transaction. |
| 305 | for (const AidlArgument* a : method.GetInArguments()) { |
| 306 | // Deserialization looks roughly like: |
| 307 | // status = data.ReadInt32(&in_param_name); |
| 308 | // if (status != android::OK) { break; } |
| 309 | const Type* type = types.Find(a->GetType().GetName()); |
| 310 | b->AddStatement(new Assignment{ |
| 311 | "status", |
| 312 | new MethodCall{"data." + type->ReadFromParcelMethod(), |
| 313 | "&" + BuildVarName(*a)}}); |
| 314 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 315 | } |
| 316 | |
| 317 | // Call the actual method. This is implemented by the subclass. |
| 318 | b->AddStatement(new Assignment{ |
| 319 | "status", new MethodCall{ |
| 320 | method.GetName(), |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 321 | BuildArgList(types, method, false /* not for method decl */)}}); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 322 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 323 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 324 | // If we have a return value, write it first. |
| 325 | if (return_type != types.VoidType()) { |
| 326 | string method = "reply->" + return_type->WriteToParcelMethod(); |
| 327 | b->AddStatement(new Assignment{ |
| 328 | "status", new MethodCall{method, ArgList{kReturnVarName}}}); |
| 329 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 330 | } |
| 331 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 332 | // Write each out parameter to the reply parcel. |
| 333 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 334 | // Serialization looks roughly like: |
| 335 | // status = data.WriteInt32(out_param_name); |
| 336 | // if (status != android::OK) { break; } |
| 337 | const Type* type = types.Find(a->GetType().GetName()); |
| 338 | b->AddStatement(new Assignment{ |
| 339 | "status", |
| 340 | new MethodCall{"reply->" + type->WriteToParcelMethod(), |
| 341 | BuildVarName(*a)}}); |
| 342 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 343 | } |
| 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | } // namespace |
| 349 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 350 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 351 | const AidlInterface& parsed_doc) { |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 352 | const string bn_name = ClassName(parsed_doc, ClassNames::SERVER); |
| 353 | vector<string> include_list{bn_name + ".h", kParcelHeader}; |
| 354 | unique_ptr<MethodImpl> on_transact{new MethodImpl{ |
| 355 | kAndroidStatusLiteral, bn_name, "onTransact", |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 356 | ArgList{{"uint32_t code", |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 357 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 358 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
| 359 | "uint32_t flags"}} |
| 360 | }}; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 361 | |
| Christopher Wiley | 05f4f89 | 2015-10-14 13:30:43 -0700 | [diff] [blame] | 362 | // Declare the status variable |
| 363 | on_transact->GetStatementBlock()->AddLiteral( |
| 364 | StringPrintf("%s status", kAndroidStatusLiteral)); |
| 365 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 366 | // Add the all important switch statement, but retain a pointer to it. |
| 367 | SwitchStatement* s = new SwitchStatement{"code"}; |
| Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 368 | on_transact->GetStatementBlock()->AddStatement(s); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 369 | |
| 370 | // The switch statement has a case statement for each transaction code. |
| 371 | for (const auto& method : parsed_doc.GetMethods()) { |
| 372 | StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName())); |
| 373 | if (!b) { return nullptr; } |
| 374 | |
| 375 | if (!HandleServerTransaction(types, *method, b)) { return nullptr; } |
| 376 | } |
| 377 | |
| 378 | // The switch statement has a default case which defers to the super class. |
| 379 | // The superclass handles a few pre-defined transactions. |
| 380 | StatementBlock* b = s->AddCase(""); |
| 381 | b->AddLiteral( |
| 382 | "status = android::BBinder::onTransact(code, data, reply, flags)"); |
| 383 | |
| 384 | // Finally, the server's onTransact method just returns a status code. |
| Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 385 | on_transact->GetStatementBlock()->AddLiteral("return status"); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 386 | |
| 387 | return unique_ptr<Document>{new CppSource{ |
| 388 | include_list, |
| 389 | NestInNamespaces(std::move(on_transact))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 392 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& /* types */, |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 393 | const AidlInterface& parsed_doc) { |
| Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 394 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 395 | const string bp_name = ClassName(parsed_doc, ClassNames::CLIENT); |
| 396 | vector<string> include_list{i_name + ".h", bp_name + ".h"}; |
| 397 | |
| 398 | string fq_name = i_name; |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 399 | if (!parsed_doc.GetPackage().empty()) { |
| 400 | 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] | 401 | } |
| 402 | |
| 403 | unique_ptr<ConstructorDecl> meta_if{new ConstructorDecl{ |
| 404 | "IMPLEMENT_META_INTERFACE", |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 405 | ArgList{vector<string>{ClassName(parsed_doc, ClassNames::BASE), |
| 406 | '"' + fq_name + '"'}}}}; |
| Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 407 | |
| 408 | return unique_ptr<Document>{new CppSource{ |
| 409 | include_list, |
| 410 | NestInNamespaces(std::move(meta_if))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 413 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 414 | const AidlInterface& interface) { |
| 415 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 416 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 417 | |
| Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 418 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{ |
| 419 | bp_name, |
| 420 | ArgList{"const android::sp<android::IBinder>& impl"}, |
| 421 | ConstructorDecl::IS_EXPLICIT |
| 422 | }}; |
| 423 | unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{ |
| 424 | "~" + bp_name, |
| 425 | ArgList{}, |
| 426 | ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}}; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 427 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 428 | vector<unique_ptr<Declaration>> publics; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 429 | publics.push_back(std::move(constructor)); |
| 430 | publics.push_back(std::move(destructor)); |
| 431 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 432 | for (const auto& method: interface.GetMethods()) { |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 433 | publics.push_back(BuildMethodDecl(*method, types, false)); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 436 | unique_ptr<ClassDecl> bp_class{ |
| 437 | new ClassDecl{bp_name, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 438 | "android::BpInterface<" + i_name + ">", |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 439 | std::move(publics), |
| 440 | {} |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 441 | }}; |
| 442 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 443 | return unique_ptr<Document>{new CppHeader{ |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 444 | BuildHeaderGuard(interface, ClassNames::CLIENT), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 445 | {kIBinderHeader, |
| 446 | kIInterfaceHeader, |
| 447 | "utils/Errors.h", |
| 448 | i_name + ".h"}, |
| 449 | NestInNamespaces(std::move(bp_class))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 452 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */, |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 453 | const AidlInterface& interface) { |
| 454 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 455 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 456 | |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 457 | unique_ptr<Declaration> on_transact{new MethodDecl{ |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 458 | kAndroidStatusLiteral, "onTransact", |
| 459 | ArgList{{"uint32_t code", |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 460 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 461 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 462 | "uint32_t flags = 0"}}, |
| 463 | MethodDecl::IS_OVERRIDE |
| 464 | }}; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 465 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 466 | std::vector<unique_ptr<Declaration>> publics; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 467 | publics.push_back(std::move(on_transact)); |
| 468 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 469 | unique_ptr<ClassDecl> bn_class{ |
| 470 | new ClassDecl{bn_name, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 471 | "android::BnInterface<" + i_name + ">", |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 472 | std::move(publics), |
| 473 | {} |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 474 | }}; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 475 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 476 | return unique_ptr<Document>{new CppHeader{ |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 477 | BuildHeaderGuard(interface, ClassNames::SERVER), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 478 | {"binder/IInterface.h", |
| 479 | i_name + ".h"}, |
| 480 | NestInNamespaces(std::move(bn_class))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 483 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 484 | const AidlInterface& interface) { |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 485 | set<string> includes = { kIBinderHeader, kIInterfaceHeader }; |
| 486 | |
| 487 | for (const auto& method : interface.GetMethods()) { |
| 488 | for (const auto& argument : method->GetArguments()) { |
| 489 | const Type* type = types.Find(argument->GetType().GetName()); |
| 490 | const std::string& header = type->Header(); |
| 491 | if (! header.empty()) |
| 492 | includes.insert(header); |
| 493 | } |
| 494 | |
| 495 | const Type* type = types.Find(method->GetType().GetName()); |
| 496 | const std::string& header = type->Header(); |
| 497 | if (! header.empty()) |
| 498 | includes.insert(header); |
| 499 | } |
| 500 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 501 | unique_ptr<ClassDecl> if_class{ |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 502 | new ClassDecl{ClassName(interface, ClassNames::INTERFACE), |
| 503 | "android::IInterface"}}; |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 504 | if_class->AddPublic(unique_ptr<Declaration>{new ConstructorDecl{ |
| 505 | "DECLARE_META_INTERFACE", |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 506 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}}); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 507 | |
| 508 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 509 | for (const auto& method : interface.GetMethods()) { |
| 510 | // Each method gets an enum entry and pure virtual declaration. |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 511 | if_class->AddPublic(BuildMethodDecl(*method, types, true)); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 512 | call_enum->AddValue( |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 513 | UpperCase(method->GetName()), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 514 | StringPrintf("android::IBinder::FIRST_CALL_TRANSACTION + %d", |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 515 | method->GetId())); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 516 | } |
| 517 | if_class->AddPublic(std::move(call_enum)); |
| 518 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 519 | return unique_ptr<Document>{new CppHeader{ |
| 520 | BuildHeaderGuard(interface, ClassNames::INTERFACE), |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 521 | vector<string>(includes.begin(), includes.end()), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 522 | NestInNamespaces(std::move(if_class))}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 525 | bool GenerateCppForFile(const std::string& name, unique_ptr<Document> doc) { |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 526 | if (!doc) { |
| 527 | return false; |
| 528 | } |
| 529 | unique_ptr<CodeWriter> writer = GetFileWriter(name); |
| 530 | doc->Write(writer.get()); |
| 531 | return true; |
| 532 | } |
| 533 | |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 534 | } // namespace internals |
| 535 | |
| 536 | using namespace internals; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 537 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 538 | bool GenerateCpp(const CppOptions& options, |
| 539 | const TypeNamespace& types, |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 540 | const AidlInterface& parsed_doc) { |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 541 | bool success = true; |
| 542 | |
| 543 | success &= GenerateCppForFile(options.ClientCppFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 544 | BuildClientSource(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 545 | success &= GenerateCppForFile(options.ClientHeaderFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 546 | BuildClientHeader(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 547 | success &= GenerateCppForFile(options.ServerCppFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 548 | BuildServerSource(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 549 | success &= GenerateCppForFile(options.ServerHeaderFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 550 | BuildServerHeader(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 551 | success &= GenerateCppForFile(options.InterfaceCppFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 552 | BuildInterfaceSource(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 553 | success &= GenerateCppForFile(options.InterfaceHeaderFileName(), |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 554 | BuildInterfaceHeader(types, parsed_doc)); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 555 | |
| 556 | return success; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 559 | } // namespace cpp |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 560 | } // namespace aidl |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 561 | } // namespace android |