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