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> |
| 27 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 28 | #include "aidl_language.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 29 | #include "ast_cpp.h" |
| 30 | #include "code_writer.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 31 | #include "logging.h" |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 32 | #include "os.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 33 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 35 | using std::string; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 36 | using std::unique_ptr; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 37 | using std::vector; |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 38 | using std::set; |
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 | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 45 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 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"; |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 48 | const char kAndroidParcelLiteral[] = "android::Parcel"; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 49 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 50 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 51 | const char kParcelHeader[] = "binder/Parcel.h"; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 52 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 53 | unique_ptr<AstNode> BreakOnStatusNotOk() { |
| 54 | IfStatement* ret = new IfStatement(new Comparison( |
| 55 | new LiteralExpression("status"), "!=", |
| 56 | new LiteralExpression("android::OK"))); |
| 57 | ret->OnTrue()->AddLiteral("break"); |
| 58 | return unique_ptr<AstNode>(ret); |
| 59 | } |
| 60 | |
| 61 | unique_ptr<AstNode> ReturnOnStatusNotOk() { |
| 62 | IfStatement* ret = new IfStatement(new Comparison( |
| 63 | new LiteralExpression("status"), "!=", |
| 64 | new LiteralExpression("android::OK"))); |
| 65 | ret->OnTrue()->AddLiteral("return status"); |
| 66 | return unique_ptr<AstNode>(ret); |
| 67 | } |
| 68 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 69 | string UpperCase(const std::string& s) { |
| 70 | string result = s; |
| 71 | for (char& c : result) |
| 72 | c = toupper(c); |
| 73 | return result; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 74 | } |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 75 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 76 | string BuildVarName(const AidlArgument& a) { |
| 77 | string prefix = "out_"; |
| 78 | if (a.GetDirection() & AidlArgument::IN_DIR) { |
| 79 | prefix = "in_"; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 80 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 81 | return prefix + a.GetName(); |
| 82 | } |
| 83 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 84 | ArgList BuildArgList(const TypeNamespace& types, |
| 85 | const AidlMethod& method, |
| 86 | bool for_declaration) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 87 | // Build up the argument list for the server method call. |
| 88 | vector<string> method_arguments; |
| 89 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 90 | string literal; |
| 91 | if (for_declaration) { |
| 92 | // Method declarations need types, pointers to out params, and variable |
| 93 | // names that match the .aidl specification. |
| 94 | const Type* type = types.Find(a->GetType().GetName()); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 95 | |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 96 | literal = type->CppType(a->GetType().IsArray()); |
| 97 | |
Christopher Wiley | b8e49a4 | 2015-10-27 12:55:18 -0700 | [diff] [blame^] | 98 | if (a->IsOut()) { |
| 99 | literal = literal + "*"; |
| 100 | } else { |
| 101 | // We pass in parameters that are not primitives by const reference. |
| 102 | // Arrays of primitives are not primitives. |
| 103 | if (!type->IsCppPrimitive() || a->GetType().IsArray()) { |
| 104 | literal = "const " + literal + "&"; |
| 105 | } |
| 106 | } |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 107 | |
| 108 | literal += " " + a->GetName(); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 109 | } else { |
| 110 | if (a->IsOut()) { literal = "&"; } |
| 111 | literal += BuildVarName(*a); |
| 112 | } |
| 113 | method_arguments.push_back(literal); |
| 114 | } |
| 115 | |
| 116 | const Type* return_type = types.Find(method.GetType().GetName()); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 117 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 118 | if (return_type != types.VoidType()) { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 119 | string literal; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 120 | if (for_declaration) { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 121 | literal = StringPrintf( |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 122 | "%s* %s", return_type->CppType(false /* not array */).c_str(), |
| 123 | kReturnVarName); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 124 | } else { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 125 | literal = string{"&"} + kReturnVarName; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 126 | } |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 127 | method_arguments.push_back(literal); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 130 | return ArgList(method_arguments); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 133 | unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 134 | const TypeNamespace& types, |
| 135 | bool for_interface) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 136 | uint32_t modifiers = 0; |
| 137 | if (for_interface) { |
| 138 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 139 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 140 | } else { |
| 141 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 142 | } |
| 143 | |
| 144 | return unique_ptr<Declaration>{ |
| 145 | new MethodDecl{kAndroidStatusLiteral, |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 146 | method.GetName(), |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 147 | BuildArgList(types, method, true /* for method decl */), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 148 | modifiers}}; |
| 149 | } |
| 150 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 151 | unique_ptr<CppNamespace> NestInNamespaces( |
| 152 | vector<unique_ptr<Declaration>> decls, |
| 153 | const vector<string>& package) { |
| 154 | if (package.empty()) { |
| 155 | // We should also be checking this before we get this far, but do it again |
| 156 | // for the sake of unit tests and meaningful errors. |
| 157 | LOG(FATAL) << "C++ generation requires a package declaration " |
| 158 | "for namespacing"; |
| 159 | } |
| 160 | auto it = package.crbegin(); // Iterate over the namespaces inner to outer |
| 161 | unique_ptr<CppNamespace> inner{new CppNamespace{*it, std::move(decls)}}; |
| 162 | ++it; |
| 163 | for (; it != package.crend(); ++it) { |
| 164 | inner.reset(new CppNamespace{*it, std::move(inner)}); |
| 165 | } |
| 166 | return inner; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 169 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl, |
| 170 | const vector<string>& package) { |
| 171 | vector<unique_ptr<Declaration>> decls; |
| 172 | decls.push_back(std::move(decl)); |
| 173 | return NestInNamespaces(std::move(decls), package); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 176 | bool DeclareLocalVariable(const TypeNamespace& types, const AidlArgument& a, |
| 177 | StatementBlock* b) { |
| 178 | const Type* cpp_type = types.Find(a.GetType().GetName()); |
| 179 | if (!cpp_type) { return false; } |
| 180 | |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 181 | string type = cpp_type->CppType(a.GetType().IsArray()); |
| 182 | |
| 183 | b->AddLiteral(type + " " + BuildVarName(a)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 187 | enum class ClassNames { BASE, CLIENT, SERVER, INTERFACE }; |
| 188 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 189 | string ClassName(const AidlInterface& interface, ClassNames type) { |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 190 | string c_name = interface.GetName(); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 191 | |
| 192 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 193 | c_name = c_name.substr(1); |
| 194 | |
| 195 | switch (type) { |
| 196 | case ClassNames::CLIENT: |
| 197 | c_name = "Bp" + c_name; |
| 198 | break; |
| 199 | case ClassNames::SERVER: |
| 200 | c_name = "Bn" + c_name; |
| 201 | break; |
| 202 | case ClassNames::INTERFACE: |
| 203 | c_name = "I" + c_name; |
| 204 | break; |
| 205 | case ClassNames::BASE: |
| 206 | break; |
| 207 | } |
| 208 | return c_name; |
| 209 | } |
| 210 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 211 | string HeaderFile(const AidlInterface& interface, |
| 212 | ClassNames class_type, |
| 213 | bool use_os_sep = true) { |
| 214 | string file_path = interface.GetPackage(); |
| 215 | for (char& c: file_path) { |
| 216 | if (c == '.') { |
| 217 | c = (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 218 | } |
| 219 | } |
| 220 | if (!file_path.empty()) { |
| 221 | file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 222 | } |
| 223 | file_path += ClassName(interface, class_type); |
| 224 | file_path += ".h"; |
| 225 | |
| 226 | return file_path; |
| 227 | } |
| 228 | |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 229 | string BuildHeaderGuard(const AidlInterface& interface, |
| 230 | ClassNames header_type) { |
| 231 | string class_name = ClassName(interface, header_type); |
| 232 | 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 | } |
| 238 | string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", |
| 239 | interface.GetPackage().c_str(), |
| 240 | class_name.c_str()); |
| 241 | for (char& c : ret) { |
| 242 | if (c == '.') { |
| 243 | c = '_'; |
| 244 | } |
| 245 | c = toupper(c); |
| 246 | } |
| 247 | return ret; |
| 248 | } |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 249 | |
| 250 | unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types, |
| 251 | const AidlInterface& interface, |
| 252 | const AidlMethod& method) { |
| 253 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 254 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| 255 | unique_ptr<MethodImpl> ret{new MethodImpl{ |
| 256 | kAndroidStatusLiteral, bp_name, method.GetName(), |
| 257 | ArgList{BuildArgList(types, method, true /* for method decl */)}}}; |
| 258 | StatementBlock* b = ret->GetStatementBlock(); |
| 259 | |
| 260 | // Declare parcels to hold our query and the response. |
| 261 | b->AddLiteral(StringPrintf("%s data", kAndroidParcelLiteral)); |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 262 | // Even if we're oneway, the transact method still takes a parcel. |
| 263 | b->AddLiteral(StringPrintf("%s reply", kAndroidParcelLiteral)); |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 264 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 265 | // And declare the status variable we need for error handling. |
| 266 | b->AddLiteral(StringPrintf("%s status", kAndroidStatusLiteral)); |
| 267 | |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 268 | // Add the name of the interface we're hoping to call. |
| 269 | b->AddStatement(new Assignment( |
| 270 | "status", |
| 271 | new MethodCall("data.writeInterfaceToken", "getInterfaceDescriptor()"))); |
| 272 | b->AddStatement(ReturnOnStatusNotOk()); |
| 273 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 274 | // Serialization looks roughly like: |
| 275 | // status = data.WriteInt32(in_param_name); |
| 276 | // if (status != android::OK) { return status; } |
| 277 | for (const AidlArgument* a : method.GetInArguments()) { |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 278 | string method = |
| 279 | types.Find(a->GetType().GetName()) |
| 280 | ->WriteToParcelMethod(a->GetType().IsArray()); |
| 281 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 282 | string var_name = ((a->IsOut()) ? "*" : "") + a->GetName(); |
| 283 | b->AddStatement(new Assignment( |
| 284 | "status", |
| 285 | new MethodCall("data." + method, ArgList(var_name)))); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 286 | b->AddStatement(ReturnOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Invoke the transaction on the remote binder and confirm status. |
| 290 | string transaction_code = StringPrintf( |
| 291 | "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str()); |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 292 | |
| 293 | vector<string> args = {transaction_code, "data", "&reply"}; |
| 294 | |
| 295 | if (interface.IsOneway() || method.IsOneway()) { |
| 296 | args.push_back("android::IBinder::FLAG_ONEWAY"); |
| 297 | } |
| 298 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 299 | b->AddStatement(new Assignment( |
| 300 | "status", |
| 301 | new MethodCall("remote()->transact", |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 302 | ArgList(args)))); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 303 | b->AddStatement(ReturnOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 304 | |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 305 | if (!interface.IsOneway() && !method.IsOneway()) { |
| 306 | // Strip off the exception header and fail if we see a remote exception. |
| 307 | // if (reply.readExceptionCode()) { |
| 308 | // status = android::FAILED_TRANSACTION; |
| 309 | // return status; |
| 310 | // } |
| 311 | IfStatement* exception_check = new IfStatement( |
| 312 | new LiteralExpression("reply.readExceptionCode()")); |
| 313 | b->AddStatement(exception_check); |
| 314 | exception_check->OnTrue()->AddStatement( |
| 315 | new Assignment("status", "android::FAILED_TRANSACTION")); |
| 316 | exception_check->OnTrue()->AddLiteral("return status"); |
| 317 | } |
| 318 | |
| 319 | // Type checking should guarantee that nothing below emits code until "return |
| 320 | // status" if we are a oneway method, so no more fear of accessing reply. |
Christopher Wiley | 2aaeda8 | 2015-10-19 15:16:49 -0700 | [diff] [blame] | 321 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 322 | // If the method is expected to return something, read it first by convention. |
| 323 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 324 | if (return_type != types.VoidType()) { |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 325 | string method = return_type->ReadFromParcelMethod(false); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 326 | b->AddStatement(new Assignment( |
| 327 | "status", |
| 328 | new MethodCall("reply." + method, ArgList(kReturnVarName)))); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 329 | b->AddStatement(ReturnOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 333 | // Deserialization looks roughly like: |
| 334 | // status = reply.ReadInt32(out_param_name); |
| 335 | // if (status != android::OK) { return status; } |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 336 | string method = |
| 337 | types.Find(a->GetType().GetName()) |
| 338 | ->ReadFromParcelMethod(a->GetType().IsArray()); |
| 339 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 340 | b->AddStatement(new Assignment( |
| 341 | "status", |
| 342 | new MethodCall("reply." + method, ArgList(a->GetName())))); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 343 | b->AddStatement(ReturnOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | b->AddLiteral("return status"); |
| 347 | |
| 348 | return unique_ptr<Declaration>(ret.release()); |
| 349 | } |
| 350 | |
| 351 | } // namespace |
| 352 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 353 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 354 | const AidlInterface& interface) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 355 | vector<string> include_list = { |
| 356 | HeaderFile(interface, ClassNames::CLIENT, false), |
| 357 | kParcelHeader |
| 358 | }; |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 359 | vector<unique_ptr<Declaration>> file_decls; |
| 360 | |
| 361 | // The constructor just passes the IBinder instance up to the super |
| 362 | // class. |
Christopher Wiley | 1db0348 | 2015-10-22 11:42:02 -0700 | [diff] [blame] | 363 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 364 | file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{ |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 365 | ClassName(interface, ClassNames::CLIENT), |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 366 | ArgList{"const android::sp<android::IBinder>& impl"}, |
Christopher Wiley | 1db0348 | 2015-10-22 11:42:02 -0700 | [diff] [blame] | 367 | { "BpInterface<" + i_name + ">(impl)" }}}); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 368 | |
| 369 | // Clients define a method per transaction. |
| 370 | for (const auto& method : interface.GetMethods()) { |
| 371 | unique_ptr<Declaration> m = DefineClientTransaction( |
| 372 | types, interface, *method); |
| 373 | if (!m) { return nullptr; } |
| 374 | file_decls.push_back(std::move(m)); |
| 375 | } |
| 376 | return unique_ptr<Document>{new CppSource{ |
| 377 | include_list, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 378 | NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 381 | namespace { |
| 382 | |
| 383 | bool HandleServerTransaction(const TypeNamespace& types, |
| 384 | const AidlMethod& method, |
| 385 | StatementBlock* b) { |
| 386 | // Declare all the parameters now. In the common case, we expect no errors |
| 387 | // in serialization. |
| 388 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 389 | if (!DeclareLocalVariable(types, *a, b)) { return false; } |
| 390 | } |
| 391 | |
| 392 | // Declare a variable to hold the return value. |
| 393 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 394 | if (return_type != types.VoidType()) { |
| 395 | b->AddLiteral(StringPrintf( |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 396 | "%s %s", return_type->CppType(false /* not array */).c_str(), |
| 397 | kReturnVarName)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 400 | // Check that the client is calling the correct interface. |
| 401 | IfStatement* interface_check = new IfStatement( |
| 402 | new MethodCall("data.checkInterface", "this"), |
| 403 | true /* invert the check */); |
| 404 | b->AddStatement(interface_check); |
| 405 | interface_check->OnTrue()->AddStatement( |
| 406 | new Assignment("status", "android::BAD_TYPE")); |
| 407 | interface_check->OnTrue()->AddLiteral("break"); |
| 408 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 409 | // Deserialize each "in" parameter to the transaction. |
| 410 | for (const AidlArgument* a : method.GetInArguments()) { |
| 411 | // Deserialization looks roughly like: |
| 412 | // status = data.ReadInt32(&in_param_name); |
| 413 | // if (status != android::OK) { break; } |
| 414 | const Type* type = types.Find(a->GetType().GetName()); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 415 | string readMethod = type->ReadFromParcelMethod(a->GetType().IsArray()); |
| 416 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 417 | b->AddStatement(new Assignment{ |
| 418 | "status", |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 419 | new MethodCall{"data." + readMethod, |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 420 | "&" + BuildVarName(*a)}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 421 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Call the actual method. This is implemented by the subclass. |
| 425 | b->AddStatement(new Assignment{ |
| 426 | "status", new MethodCall{ |
| 427 | method.GetName(), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 428 | BuildArgList(types, method, false /* not for method decl */)}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 429 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 430 | |
Christopher Wiley | 2aaeda8 | 2015-10-19 15:16:49 -0700 | [diff] [blame] | 431 | // Write that we encountered no exceptions during transaction handling. |
| 432 | b->AddStatement(new Assignment("status", "reply->writeNoException()")); |
| 433 | b->AddStatement(BreakOnStatusNotOk()); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 434 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 435 | // If we have a return value, write it first. |
| 436 | if (return_type != types.VoidType()) { |
Christopher Wiley | 2aaeda8 | 2015-10-19 15:16:49 -0700 | [diff] [blame] | 437 | string writeMethod = |
| 438 | "reply->" + |
| 439 | return_type->WriteToParcelMethod(method.GetType().IsArray()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 440 | b->AddStatement(new Assignment{ |
Christopher Wiley | 2aaeda8 | 2015-10-19 15:16:49 -0700 | [diff] [blame] | 441 | "status", new MethodCall{writeMethod, ArgList{kReturnVarName}}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 442 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 445 | // Write each out parameter to the reply parcel. |
| 446 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 447 | // Serialization looks roughly like: |
| 448 | // status = data.WriteInt32(out_param_name); |
| 449 | // if (status != android::OK) { break; } |
| 450 | const Type* type = types.Find(a->GetType().GetName()); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 451 | string writeMethod = type->WriteToParcelMethod(a->GetType().IsArray()); |
| 452 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 453 | b->AddStatement(new Assignment{ |
| 454 | "status", |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 455 | new MethodCall{"reply->" + writeMethod, |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 456 | BuildVarName(*a)}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 457 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | } // namespace |
| 464 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 465 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 466 | const AidlInterface& interface) { |
| 467 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
| 468 | vector<string> include_list{ |
| 469 | HeaderFile(interface, ClassNames::SERVER, false), |
| 470 | kParcelHeader |
| 471 | }; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 472 | unique_ptr<MethodImpl> on_transact{new MethodImpl{ |
| 473 | kAndroidStatusLiteral, bn_name, "onTransact", |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 474 | ArgList{{"uint32_t code", |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 475 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 476 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
| 477 | "uint32_t flags"}} |
| 478 | }}; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 479 | |
Christopher Wiley | 05f4f89 | 2015-10-14 13:30:43 -0700 | [diff] [blame] | 480 | // Declare the status variable |
| 481 | on_transact->GetStatementBlock()->AddLiteral( |
| 482 | StringPrintf("%s status", kAndroidStatusLiteral)); |
| 483 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 484 | // Add the all important switch statement, but retain a pointer to it. |
| 485 | SwitchStatement* s = new SwitchStatement{"code"}; |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 486 | on_transact->GetStatementBlock()->AddStatement(s); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 487 | |
| 488 | // The switch statement has a case statement for each transaction code. |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 489 | for (const auto& method : interface.GetMethods()) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 490 | StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName())); |
| 491 | if (!b) { return nullptr; } |
| 492 | |
| 493 | if (!HandleServerTransaction(types, *method, b)) { return nullptr; } |
| 494 | } |
| 495 | |
| 496 | // The switch statement has a default case which defers to the super class. |
| 497 | // The superclass handles a few pre-defined transactions. |
| 498 | StatementBlock* b = s->AddCase(""); |
| 499 | b->AddLiteral( |
| 500 | "status = android::BBinder::onTransact(code, data, reply, flags)"); |
| 501 | |
| 502 | // Finally, the server's onTransact method just returns a status code. |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 503 | on_transact->GetStatementBlock()->AddLiteral("return status"); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 504 | |
| 505 | return unique_ptr<Document>{new CppSource{ |
| 506 | include_list, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 507 | NestInNamespaces(std::move(on_transact), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 510 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& /* types */, |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 511 | const AidlInterface& interface) { |
| 512 | vector<string> include_list{ |
| 513 | HeaderFile(interface, ClassNames::INTERFACE, false), |
| 514 | HeaderFile(interface, ClassNames::CLIENT, false), |
| 515 | }; |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 516 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 517 | string fq_name = ClassName(interface, ClassNames::INTERFACE); |
| 518 | if (!interface.GetPackage().empty()) { |
| 519 | fq_name = interface.GetPackage() + "." + fq_name; |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | unique_ptr<ConstructorDecl> meta_if{new ConstructorDecl{ |
| 523 | "IMPLEMENT_META_INTERFACE", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 524 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 525 | '"' + fq_name + '"'}}}}; |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 526 | |
| 527 | return unique_ptr<Document>{new CppSource{ |
| 528 | include_list, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 529 | NestInNamespaces(std::move(meta_if), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 532 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 533 | const AidlInterface& interface) { |
| 534 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 535 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 536 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 537 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{ |
| 538 | bp_name, |
| 539 | ArgList{"const android::sp<android::IBinder>& impl"}, |
| 540 | ConstructorDecl::IS_EXPLICIT |
| 541 | }}; |
| 542 | unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{ |
| 543 | "~" + bp_name, |
| 544 | ArgList{}, |
| 545 | ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}}; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 546 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 547 | vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 548 | publics.push_back(std::move(constructor)); |
| 549 | publics.push_back(std::move(destructor)); |
| 550 | |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 551 | for (const auto& method: interface.GetMethods()) { |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 552 | publics.push_back(BuildMethodDecl(*method, types, false)); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 555 | unique_ptr<ClassDecl> bp_class{ |
| 556 | new ClassDecl{bp_name, |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 557 | "android::BpInterface<" + i_name + ">", |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 558 | std::move(publics), |
| 559 | {} |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 560 | }}; |
| 561 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 562 | return unique_ptr<Document>{new CppHeader{ |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 563 | BuildHeaderGuard(interface, ClassNames::CLIENT), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 564 | {kIBinderHeader, |
| 565 | kIInterfaceHeader, |
| 566 | "utils/Errors.h", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 567 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 568 | NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 571 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */, |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 572 | const AidlInterface& interface) { |
| 573 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 574 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 575 | |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 576 | unique_ptr<Declaration> on_transact{new MethodDecl{ |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 577 | kAndroidStatusLiteral, "onTransact", |
| 578 | ArgList{{"uint32_t code", |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 579 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 580 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 581 | "uint32_t flags = 0"}}, |
| 582 | MethodDecl::IS_OVERRIDE |
| 583 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 584 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 585 | std::vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 586 | publics.push_back(std::move(on_transact)); |
| 587 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 588 | unique_ptr<ClassDecl> bn_class{ |
| 589 | new ClassDecl{bn_name, |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 590 | "android::BnInterface<" + i_name + ">", |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 591 | std::move(publics), |
| 592 | {} |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 593 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 594 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 595 | return unique_ptr<Document>{new CppHeader{ |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 596 | BuildHeaderGuard(interface, ClassNames::SERVER), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 597 | {"binder/IInterface.h", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 598 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 599 | NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 602 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 603 | const AidlInterface& interface) { |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 604 | set<string> includes = { kIBinderHeader, kIInterfaceHeader }; |
| 605 | |
| 606 | for (const auto& method : interface.GetMethods()) { |
| 607 | for (const auto& argument : method->GetArguments()) { |
| 608 | const Type* type = types.Find(argument->GetType().GetName()); |
| 609 | const std::string& header = type->Header(); |
| 610 | if (! header.empty()) |
| 611 | includes.insert(header); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 612 | if (argument->GetType().IsArray()) |
| 613 | includes.insert("vector"); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | const Type* type = types.Find(method->GetType().GetName()); |
| 617 | const std::string& header = type->Header(); |
| 618 | if (! header.empty()) |
| 619 | includes.insert(header); |
| 620 | } |
| 621 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 622 | unique_ptr<ClassDecl> if_class{ |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 623 | new ClassDecl{ClassName(interface, ClassNames::INTERFACE), |
| 624 | "android::IInterface"}}; |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 625 | if_class->AddPublic(unique_ptr<Declaration>{new ConstructorDecl{ |
| 626 | "DECLARE_META_INTERFACE", |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 627 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}}); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 628 | |
| 629 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 630 | for (const auto& method : interface.GetMethods()) { |
| 631 | // Each method gets an enum entry and pure virtual declaration. |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 632 | if_class->AddPublic(BuildMethodDecl(*method, types, true)); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 633 | call_enum->AddValue( |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 634 | UpperCase(method->GetName()), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 635 | StringPrintf("android::IBinder::FIRST_CALL_TRANSACTION + %d", |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 636 | method->GetId())); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 637 | } |
| 638 | if_class->AddPublic(std::move(call_enum)); |
| 639 | |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 640 | return unique_ptr<Document>{new CppHeader{ |
| 641 | BuildHeaderGuard(interface, ClassNames::INTERFACE), |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 642 | vector<string>(includes.begin(), includes.end()), |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 643 | NestInNamespaces(std::move(if_class), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 646 | bool WriteHeader(const CppOptions& options, |
| 647 | const TypeNamespace& types, |
| 648 | const AidlInterface& interface, |
| 649 | const IoDelegate& io_delegate, |
| 650 | ClassNames header_type) { |
| 651 | unique_ptr<Document> header; |
| 652 | switch (header_type) { |
| 653 | case ClassNames::INTERFACE: |
| 654 | header = BuildInterfaceHeader(types, interface); |
| 655 | break; |
| 656 | case ClassNames::CLIENT: |
| 657 | header = BuildClientHeader(types, interface); |
| 658 | break; |
| 659 | case ClassNames::SERVER: |
| 660 | header = BuildServerHeader(types, interface); |
| 661 | break; |
| 662 | default: |
| 663 | LOG(FATAL) << "aidl internal error"; |
| 664 | } |
| 665 | if (!header) { |
| 666 | LOG(ERROR) << "aidl internal error: Failed to generate header."; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 667 | return false; |
| 668 | } |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 669 | |
| 670 | // TODO(wiley): b/25026025 error checking for file I/O |
| 671 | header->Write(io_delegate.GetCodeWriter( |
| 672 | options.OutputHeaderDir() + OS_PATH_SEPARATOR + |
| 673 | HeaderFile(interface, header_type)).get()); |
| 674 | |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 678 | } // namespace internals |
| 679 | |
| 680 | using namespace internals; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 681 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 682 | bool GenerateCpp(const CppOptions& options, |
| 683 | const TypeNamespace& types, |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 684 | const AidlInterface& interface, |
| 685 | const IoDelegate& io_delegate) { |
| 686 | auto interface_src = BuildInterfaceSource(types, interface); |
| 687 | auto client_src = BuildClientSource(types, interface); |
| 688 | auto server_src = BuildServerSource(types, interface); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 689 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 690 | if (!interface_src || !client_src || !server_src) { |
| 691 | return false; |
| 692 | } |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 693 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 694 | if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 695 | interface.GetSplitPackage())) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 696 | LOG(ERROR) << "Failed to create directory structure for headers."; |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | if (!WriteHeader(options, types, interface, io_delegate, |
| 701 | ClassNames::INTERFACE) || |
| 702 | !WriteHeader(options, types, interface, io_delegate, |
| 703 | ClassNames::CLIENT) || |
| 704 | !WriteHeader(options, types, interface, io_delegate, |
| 705 | ClassNames::SERVER)) { |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | // TODO(wiley): b/25026025 error checking for file I/O. |
| 710 | // If it fails, we should remove all the partial results. |
| 711 | unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter( |
| 712 | options.OutputCppFilePath()); |
| 713 | interface_src->Write(writer.get()); |
| 714 | client_src->Write(writer.get()); |
| 715 | server_src->Write(writer.get()); |
| 716 | |
| 717 | return true; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 720 | } // namespace cpp |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 721 | } // namespace aidl |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 722 | } // namespace android |