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" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 18 | #include "aidl.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 19 | |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 20 | #include <cctype> |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 21 | #include <cstring> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 22 | #include <memory> |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 23 | #include <random> |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 24 | #include <set> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 28 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 29 | #include "aidl_language.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 30 | #include "ast_cpp.h" |
| 31 | #include "code_writer.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 32 | #include "logging.h" |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 33 | #include "os.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 34 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 35 | using android::base::Join; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 36 | using android::base::StringPrintf; |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 37 | using std::set; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 38 | using std::string; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 39 | using std::unique_ptr; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 40 | using std::vector; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 41 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 44 | namespace cpp { |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 45 | namespace internals { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 46 | namespace { |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 47 | |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 48 | const char kAndroidStatusVarName[] = "_aidl_ret_status"; |
| 49 | const char kCodeVarName[] = "_aidl_code"; |
| 50 | const char kFlagsVarName[] = "_aidl_flags"; |
| 51 | const char kDataVarName[] = "_aidl_data"; |
| 52 | const char kErrorLabel[] = "_aidl_error"; |
| 53 | const char kImplVarName[] = "_aidl_impl"; |
| 54 | const char kReplyVarName[] = "_aidl_reply"; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 55 | const char kReturnVarName[] = "_aidl_return"; |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 56 | const char kStatusVarName[] = "_aidl_status"; |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 57 | const char kTraceVarName[] = "_aidl_trace"; |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 58 | const char kAndroidParcelLiteral[] = "::android::Parcel"; |
| 59 | const char kAndroidStatusLiteral[] = "::android::status_t"; |
| 60 | const char kAndroidStatusOk[] = "::android::OK"; |
| 61 | const char kBinderStatusLiteral[] = "::android::binder::Status"; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 62 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 63 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 64 | const char kParcelHeader[] = "binder/Parcel.h"; |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 65 | const char kStatusHeader[] = "binder/Status.h"; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 66 | const char kString16Header[] = "utils/String16.h"; |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 67 | const char kTraceHeader[] = "utils/Trace.h"; |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 68 | const char kStrongPointerHeader[] = "utils/StrongPointer.h"; |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 69 | const char kAndroidBaseMacrosHeader[] = "android-base/macros.h"; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 70 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 71 | unique_ptr<AstNode> BreakOnStatusNotOk() { |
| 72 | IfStatement* ret = new IfStatement(new Comparison( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 73 | new LiteralExpression(kAndroidStatusVarName), "!=", |
| 74 | new LiteralExpression(kAndroidStatusOk))); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 75 | ret->OnTrue()->AddLiteral("break"); |
| 76 | return unique_ptr<AstNode>(ret); |
| 77 | } |
| 78 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 79 | unique_ptr<AstNode> GotoErrorOnBadStatus() { |
| 80 | IfStatement* ret = new IfStatement(new Comparison( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 81 | new LiteralExpression(kAndroidStatusVarName), "!=", |
| 82 | new LiteralExpression(kAndroidStatusOk))); |
| 83 | ret->OnTrue()->AddLiteral(StringPrintf("goto %s", kErrorLabel)); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 84 | return unique_ptr<AstNode>(ret); |
| 85 | } |
| 86 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 87 | unique_ptr<AstNode> ReturnOnStatusNotOk() { |
| 88 | IfStatement* ret = new IfStatement(new Comparison(new LiteralExpression(kAndroidStatusVarName), |
| 89 | "!=", new LiteralExpression(kAndroidStatusOk))); |
| 90 | ret->OnTrue()->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName)); |
| 91 | return unique_ptr<AstNode>(ret); |
| 92 | } |
| 93 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 94 | string UpperCase(const std::string& s) { |
| 95 | string result = s; |
| 96 | for (char& c : result) |
| 97 | c = toupper(c); |
| 98 | return result; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 99 | } |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 100 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 101 | string BuildVarName(const AidlArgument& a) { |
| 102 | string prefix = "out_"; |
| 103 | if (a.GetDirection() & AidlArgument::IN_DIR) { |
| 104 | prefix = "in_"; |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 105 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 106 | return prefix + a.GetName(); |
| 107 | } |
| 108 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 109 | ArgList BuildArgList(const TypeNamespace& types, const AidlMethod& method, bool for_declaration, |
| 110 | bool type_name_only = false) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 111 | // Build up the argument list for the server method call. |
| 112 | vector<string> method_arguments; |
| 113 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 114 | string literal; |
| 115 | if (for_declaration) { |
| 116 | // Method declarations need types, pointers to out params, and variable |
| 117 | // names that match the .aidl specification. |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 118 | const Type* type = a->GetType().GetLanguageType<Type>(); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 119 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 120 | literal = type->CppType(); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 121 | |
Christopher Wiley | b8e49a4 | 2015-10-27 12:55:18 -0700 | [diff] [blame] | 122 | if (a->IsOut()) { |
| 123 | literal = literal + "*"; |
| 124 | } else { |
| 125 | // We pass in parameters that are not primitives by const reference. |
| 126 | // Arrays of primitives are not primitives. |
| 127 | if (!type->IsCppPrimitive() || a->GetType().IsArray()) { |
| 128 | literal = "const " + literal + "&"; |
| 129 | } |
| 130 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 131 | if (!type_name_only) { |
| 132 | literal += " " + a->GetName(); |
| 133 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 134 | } else { |
| 135 | if (a->IsOut()) { literal = "&"; } |
| 136 | literal += BuildVarName(*a); |
| 137 | } |
| 138 | method_arguments.push_back(literal); |
| 139 | } |
| 140 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 141 | const Type* return_type = method.GetType().GetLanguageType<Type>(); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 142 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 143 | if (return_type != types.VoidType()) { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 144 | string literal; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 145 | if (for_declaration) { |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 146 | literal = StringPrintf("%s* %s", return_type->CppType().c_str(), |
| 147 | type_name_only ? "" : kReturnVarName); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 148 | } else { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 149 | literal = string{"&"} + kReturnVarName; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 150 | } |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 151 | method_arguments.push_back(literal); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 154 | return ArgList(method_arguments); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 157 | unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 158 | const TypeNamespace& types, |
| 159 | bool for_interface) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 160 | uint32_t modifiers = 0; |
| 161 | if (for_interface) { |
| 162 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 163 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 164 | } else { |
| 165 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 166 | } |
| 167 | |
| 168 | return unique_ptr<Declaration>{ |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 169 | new MethodDecl{kBinderStatusLiteral, |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 170 | method.GetName(), |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 171 | BuildArgList(types, method, true /* for method decl */), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 172 | modifiers}}; |
| 173 | } |
| 174 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 175 | unique_ptr<Declaration> BuildMetaMethodDecl(const AidlMethod& method, const TypeNamespace&, |
| 176 | const Options& options, bool for_interface) { |
| 177 | CHECK(!method.IsUserDefined()); |
| 178 | if (method.GetName() == kGetInterfaceVersion && options.Version()) { |
| 179 | std::ostringstream code; |
| 180 | if (for_interface) { |
| 181 | code << "virtual "; |
| 182 | } |
| 183 | code << "int32_t " << kGetInterfaceVersion << "()"; |
| 184 | if (for_interface) { |
| 185 | code << " = 0;\n"; |
| 186 | } else { |
| 187 | code << " override;\n"; |
| 188 | } |
| 189 | return unique_ptr<Declaration>(new LiteralDecl(code.str())); |
| 190 | } |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 194 | unique_ptr<CppNamespace> NestInNamespaces( |
| 195 | vector<unique_ptr<Declaration>> decls, |
| 196 | const vector<string>& package) { |
| 197 | if (package.empty()) { |
| 198 | // We should also be checking this before we get this far, but do it again |
| 199 | // for the sake of unit tests and meaningful errors. |
| 200 | LOG(FATAL) << "C++ generation requires a package declaration " |
| 201 | "for namespacing"; |
| 202 | } |
| 203 | auto it = package.crbegin(); // Iterate over the namespaces inner to outer |
| 204 | unique_ptr<CppNamespace> inner{new CppNamespace{*it, std::move(decls)}}; |
| 205 | ++it; |
| 206 | for (; it != package.crend(); ++it) { |
| 207 | inner.reset(new CppNamespace{*it, std::move(inner)}); |
| 208 | } |
| 209 | return inner; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 212 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl, |
| 213 | const vector<string>& package) { |
| 214 | vector<unique_ptr<Declaration>> decls; |
| 215 | decls.push_back(std::move(decl)); |
| 216 | return NestInNamespaces(std::move(decls), package); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Steven Moreland | 1c41e97 | 2018-07-09 16:07:00 -0700 | [diff] [blame] | 219 | bool DeclareLocalVariable(const AidlArgument& a, StatementBlock* b) { |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 220 | const Type* cpp_type = a.GetType().GetLanguageType<Type>(); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 221 | if (!cpp_type) { return false; } |
| 222 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 223 | string type = cpp_type->CppType(); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 224 | |
| 225 | b->AddLiteral(type + " " + BuildVarName(a)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 229 | string ClassName(const AidlDefinedType& defined_type, ClassNames type) { |
| 230 | string c_name = defined_type.GetName(); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 231 | |
| 232 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 233 | c_name = c_name.substr(1); |
| 234 | |
| 235 | switch (type) { |
| 236 | case ClassNames::CLIENT: |
| 237 | c_name = "Bp" + c_name; |
| 238 | break; |
| 239 | case ClassNames::SERVER: |
| 240 | c_name = "Bn" + c_name; |
| 241 | break; |
| 242 | case ClassNames::INTERFACE: |
| 243 | c_name = "I" + c_name; |
| 244 | break; |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 245 | case ClassNames::DEFAULT_IMPL: |
| 246 | c_name = "I" + c_name + "Default"; |
| 247 | break; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 248 | case ClassNames::BASE: |
| 249 | break; |
| 250 | } |
| 251 | return c_name; |
| 252 | } |
| 253 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 254 | string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_type) { |
| 255 | string class_name = ClassName(defined_type, header_type); |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 256 | for (size_t i = 1; i < class_name.size(); ++i) { |
| 257 | if (isupper(class_name[i])) { |
| 258 | class_name.insert(i, "_"); |
| 259 | ++i; |
| 260 | } |
| 261 | } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 262 | string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", defined_type.GetPackage().c_str(), |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 263 | class_name.c_str()); |
| 264 | for (char& c : ret) { |
| 265 | if (c == '.') { |
| 266 | c = '_'; |
| 267 | } |
| 268 | c = toupper(c); |
| 269 | } |
| 270 | return ret; |
| 271 | } |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 272 | |
| 273 | unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types, |
| 274 | const AidlInterface& interface, |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 275 | const AidlMethod& method, const Options& options) { |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 276 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 277 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| 278 | unique_ptr<MethodImpl> ret{new MethodImpl{ |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 279 | kBinderStatusLiteral, bp_name, method.GetName(), |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 280 | ArgList{BuildArgList(types, method, true /* for method decl */)}}}; |
| 281 | StatementBlock* b = ret->GetStatementBlock(); |
| 282 | |
| 283 | // Declare parcels to hold our query and the response. |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 284 | b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kDataVarName)); |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 285 | // Even if we're oneway, the transact method still takes a parcel. |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 286 | b->AddLiteral(StringPrintf("%s %s", kAndroidParcelLiteral, kReplyVarName)); |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 287 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 288 | // Declare the status_t variable we need for error handling. |
Christopher Wiley | 1095712 | 2015-12-04 14:35:38 -0800 | [diff] [blame] | 289 | b->AddLiteral(StringPrintf("%s %s = %s", kAndroidStatusLiteral, |
| 290 | kAndroidStatusVarName, |
| 291 | kAndroidStatusOk)); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 292 | // We unconditionally return a Status object. |
| 293 | b->AddLiteral(StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName)); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 294 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 295 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 296 | b->AddLiteral( |
| 297 | StringPrintf("ScopedTrace %s(ATRACE_TAG_AIDL, \"%s::%s::cppClient\")", |
| 298 | kTraceVarName, interface.GetName().c_str(), method.GetName().c_str())); |
| 299 | } |
| 300 | |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 301 | // Add the name of the interface we're hoping to call. |
| 302 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 303 | kAndroidStatusVarName, |
| 304 | new MethodCall(StringPrintf("%s.writeInterfaceToken", |
| 305 | kDataVarName), |
| 306 | "getInterfaceDescriptor()"))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 307 | b->AddStatement(GotoErrorOnBadStatus()); |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 308 | |
Christopher Wiley | 74b7bf1 | 2016-08-19 11:06:32 -0700 | [diff] [blame] | 309 | for (const auto& a: method.GetArguments()) { |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 310 | const Type* type = a->GetType().GetLanguageType<Type>(); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 311 | string var_name = ((a->IsOut()) ? "*" : "") + a->GetName(); |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 312 | var_name = type->WriteCast(var_name); |
Christopher Wiley | 74b7bf1 | 2016-08-19 11:06:32 -0700 | [diff] [blame] | 313 | |
| 314 | if (a->IsIn()) { |
| 315 | // Serialization looks roughly like: |
| 316 | // _aidl_ret_status = _aidl_data.WriteInt32(in_param_name); |
| 317 | // if (_aidl_ret_status != ::android::OK) { goto error; } |
| 318 | const string& method = type->WriteToParcelMethod(); |
| 319 | b->AddStatement(new Assignment( |
| 320 | kAndroidStatusVarName, |
| 321 | new MethodCall(StringPrintf("%s.%s", kDataVarName, method.c_str()), |
| 322 | ArgList(var_name)))); |
| 323 | b->AddStatement(GotoErrorOnBadStatus()); |
| 324 | } else if (a->IsOut() && a->GetType().IsArray()) { |
| 325 | // Special case, the length of the out array is written into the parcel. |
| 326 | // _aidl_ret_status = _aidl_data.writeVectorSize(&out_param_name); |
| 327 | // if (_aidl_ret_status != ::android::OK) { goto error; } |
| 328 | b->AddStatement(new Assignment( |
| 329 | kAndroidStatusVarName, |
| 330 | new MethodCall(StringPrintf("%s.writeVectorSize", kDataVarName), |
| 331 | ArgList(var_name)))); |
| 332 | b->AddStatement(GotoErrorOnBadStatus()); |
| 333 | } |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // Invoke the transaction on the remote binder and confirm status. |
| 337 | string transaction_code = StringPrintf( |
| 338 | "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str()); |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 339 | |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 340 | vector<string> args = {transaction_code, kDataVarName, |
| 341 | StringPrintf("&%s", kReplyVarName)}; |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 342 | |
| 343 | if (interface.IsOneway() || method.IsOneway()) { |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 344 | args.push_back("::android::IBinder::FLAG_ONEWAY"); |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 347 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 348 | kAndroidStatusVarName, |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 349 | new MethodCall("remote()->transact", |
Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 350 | ArgList(args)))); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 351 | |
| 352 | // If the method is not implemented in the remote side, try to call the |
| 353 | // default implementation, if provided. |
| 354 | vector<string> arg_names; |
| 355 | for (const auto& a : method.GetArguments()) { |
| 356 | arg_names.emplace_back(a->GetName()); |
| 357 | } |
| 358 | if (method.GetType().GetLanguageType<Type>() != types.VoidType()) { |
| 359 | arg_names.emplace_back(kReturnVarName); |
| 360 | } |
| 361 | b->AddLiteral(StringPrintf("if (UNLIKELY(_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && " |
| 362 | "%s::getDefaultImpl())) {\n" |
| 363 | " return %s::getDefaultImpl()->%s(%s);\n" |
| 364 | "}\n", |
| 365 | i_name.c_str(), i_name.c_str(), method.GetName().c_str(), |
| 366 | Join(arg_names, ", ").c_str()), |
| 367 | false /* no semicolon */); |
| 368 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 369 | b->AddStatement(GotoErrorOnBadStatus()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 370 | |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 371 | if (!interface.IsOneway() && !method.IsOneway()) { |
| 372 | // 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] | 373 | // _aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply); |
| 374 | // if (_aidl_ret_status != ::android::OK) { goto error; } |
| 375 | // if (!_aidl_status.isOk()) { return _aidl_ret_status; } |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 376 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 377 | kAndroidStatusVarName, |
| 378 | StringPrintf("%s.readFromParcel(%s)", kStatusVarName, kReplyVarName))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 379 | b->AddStatement(GotoErrorOnBadStatus()); |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 380 | IfStatement* exception_check = new IfStatement( |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 381 | new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName))); |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 382 | b->AddStatement(exception_check); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 383 | exception_check->OnTrue()->AddLiteral( |
| 384 | StringPrintf("return %s", kStatusVarName)); |
Christopher Wiley | 1227d61 | 2015-10-26 16:59:20 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // Type checking should guarantee that nothing below emits code until "return |
| 388 | // 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] | 389 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 390 | // If the method is expected to return something, read it first by convention. |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 391 | const Type* return_type = method.GetType().GetLanguageType<Type>(); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 392 | if (return_type != types.VoidType()) { |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 393 | const string& method_call = return_type->ReadFromParcelMethod(); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 394 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 395 | kAndroidStatusVarName, |
| 396 | new MethodCall(StringPrintf("%s.%s", kReplyVarName, |
| 397 | method_call.c_str()), |
| 398 | ArgList(kReturnVarName)))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 399 | b->AddStatement(GotoErrorOnBadStatus()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 403 | // Deserialization looks roughly like: |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 404 | // _aidl_ret_status = _aidl_reply.ReadInt32(out_param_name); |
| 405 | // if (_aidl_status != ::android::OK) { goto _aidl_error; } |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 406 | string method = |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 407 | a->GetType().GetLanguageType<Type>()->ReadFromParcelMethod(); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 408 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 409 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 410 | kAndroidStatusVarName, |
| 411 | new MethodCall(StringPrintf("%s.%s", kReplyVarName, |
| 412 | method.c_str()), |
| 413 | ArgList(a->GetName())))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 414 | b->AddStatement(GotoErrorOnBadStatus()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 417 | // If we've gotten to here, one of two things is true: |
| 418 | // 1) We've read some bad status_t |
| 419 | // 2) We've only read status_t == OK and there was no exception in the |
| 420 | // response. |
| 421 | // 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] | 422 | b->AddLiteral(StringPrintf("%s:\n", kErrorLabel), false /* no semicolon */); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 423 | b->AddLiteral( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 424 | StringPrintf("%s.setFromStatusT(%s)", kStatusVarName, |
| 425 | kAndroidStatusVarName)); |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 426 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 427 | b->AddLiteral(StringPrintf("return %s", kStatusVarName)); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 428 | |
| 429 | return unique_ptr<Declaration>(ret.release()); |
| 430 | } |
| 431 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 432 | unique_ptr<Declaration> DefineClientMetaTransaction(const TypeNamespace&, |
| 433 | const AidlInterface& interface, |
| 434 | const AidlMethod& method, |
| 435 | const Options& options) { |
| 436 | CHECK(!method.IsUserDefined()); |
| 437 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 438 | const string iface = ClassName(interface, ClassNames::INTERFACE); |
| 439 | const string proxy = ClassName(interface, ClassNames::CLIENT); |
| 440 | // Note: race condition can happen here, but no locking is required |
| 441 | // because 1) writing an interger is atomic and 2) this transaction |
| 442 | // will always return the same value, i.e., competing threads will |
| 443 | // give write the same value to cached_version_. |
| 444 | std::ostringstream code; |
| 445 | code << "int32_t " << proxy << "::" << kGetInterfaceVersion << "() {\n" |
| 446 | << " if (cached_version_ != -1) {\n" |
| 447 | << " ::android::Parcel data;\n" |
| 448 | << " ::android::Parcel reply;\n" |
| 449 | << " ::android::status_t err = remote()->transact(" << iface |
| 450 | << "::" << UpperCase(kGetInterfaceVersion) << ", data, &reply);\n" |
| 451 | << " if (err == ::android::OK) {\n" |
| 452 | << " cached_version_ = reply.readInt32();\n" |
| 453 | << " }\n" |
| 454 | << " }\n" |
| 455 | << " return cached_version_;\n" |
| 456 | << "}\n"; |
| 457 | return unique_ptr<Declaration>(new LiteralDecl(code.str())); |
| 458 | } |
| 459 | return nullptr; |
| 460 | } |
| 461 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 462 | } // namespace |
| 463 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 464 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, const AidlInterface& interface, |
| 465 | const Options& options) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 466 | vector<string> include_list = { |
| 467 | HeaderFile(interface, ClassNames::CLIENT, false), |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 468 | kParcelHeader, |
| 469 | kAndroidBaseMacrosHeader |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 470 | }; |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 471 | vector<unique_ptr<Declaration>> file_decls; |
| 472 | |
| 473 | // The constructor just passes the IBinder instance up to the super |
| 474 | // class. |
Christopher Wiley | 1db0348 | 2015-10-22 11:42:02 -0700 | [diff] [blame] | 475 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 476 | file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{ |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 477 | ClassName(interface, ClassNames::CLIENT), |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 478 | ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s", |
| 479 | kImplVarName)}, |
| 480 | { "BpInterface<" + i_name + ">(" + kImplVarName + ")" }}}); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 481 | |
| 482 | // Clients define a method per transaction. |
| 483 | for (const auto& method : interface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 484 | unique_ptr<Declaration> m; |
| 485 | if (method->IsUserDefined()) { |
| 486 | m = DefineClientTransaction(types, interface, *method, options); |
| 487 | } else { |
| 488 | m = DefineClientMetaTransaction(types, interface, *method, options); |
| 489 | } |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 490 | if (!m) { return nullptr; } |
| 491 | file_decls.push_back(std::move(m)); |
| 492 | } |
| 493 | return unique_ptr<Document>{new CppSource{ |
| 494 | include_list, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 495 | NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 498 | namespace { |
| 499 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 500 | bool HandleServerTransaction(const TypeNamespace& types, const AidlInterface& interface, |
| 501 | const AidlMethod& method, const Options& options, StatementBlock* b) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 502 | // Declare all the parameters now. In the common case, we expect no errors |
| 503 | // in serialization. |
| 504 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
Steven Moreland | 1c41e97 | 2018-07-09 16:07:00 -0700 | [diff] [blame] | 505 | if (!DeclareLocalVariable(*a, b)) { |
| 506 | return false; |
| 507 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // Declare a variable to hold the return value. |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 511 | const Type* return_type = method.GetType().GetLanguageType<Type>(); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 512 | if (return_type != types.VoidType()) { |
| 513 | b->AddLiteral(StringPrintf( |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 514 | "%s %s", return_type->CppType().c_str(), |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 515 | kReturnVarName)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 518 | // Check that the client is calling the correct interface. |
| 519 | IfStatement* interface_check = new IfStatement( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 520 | new MethodCall(StringPrintf("%s.checkInterface", |
| 521 | kDataVarName), "this"), |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 522 | true /* invert the check */); |
| 523 | b->AddStatement(interface_check); |
| 524 | interface_check->OnTrue()->AddStatement( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 525 | new Assignment(kAndroidStatusVarName, "::android::BAD_TYPE")); |
Christopher Wiley | 8993cb5 | 2015-10-21 09:53:24 -0700 | [diff] [blame] | 526 | interface_check->OnTrue()->AddLiteral("break"); |
| 527 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 528 | // Deserialize each "in" parameter to the transaction. |
Christopher Wiley | 74b7bf1 | 2016-08-19 11:06:32 -0700 | [diff] [blame] | 529 | for (const auto& a: method.GetArguments()) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 530 | // Deserialization looks roughly like: |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 531 | // _aidl_ret_status = _aidl_data.ReadInt32(&in_param_name); |
| 532 | // if (_aidl_ret_status != ::android::OK) { break; } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 533 | const Type* type = a->GetType().GetLanguageType<Type>(); |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 534 | const string& readMethod = type->ReadFromParcelMethod(); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 535 | |
Christopher Wiley | 74b7bf1 | 2016-08-19 11:06:32 -0700 | [diff] [blame] | 536 | if (a->IsIn()) { |
| 537 | b->AddStatement(new Assignment{ |
| 538 | kAndroidStatusVarName, |
| 539 | new MethodCall{string(kDataVarName) + "." + readMethod, |
| 540 | "&" + BuildVarName(*a)}}); |
| 541 | b->AddStatement(BreakOnStatusNotOk()); |
| 542 | } else if (a->IsOut() && a->GetType().IsArray()) { |
| 543 | // Special case, the length of the out array is written into the parcel. |
| 544 | // _aidl_ret_status = _aidl_data.resizeOutVector(&out_param_name); |
| 545 | // if (_aidl_ret_status != ::android::OK) { break; } |
| 546 | b->AddStatement(new Assignment{ |
| 547 | kAndroidStatusVarName, |
| 548 | new MethodCall{string(kDataVarName) + ".resizeOutVector", |
| 549 | "&" + BuildVarName(*a)}}); |
| 550 | b->AddStatement(BreakOnStatusNotOk()); |
| 551 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 554 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 555 | b->AddStatement(new Statement(new MethodCall("atrace_begin", |
| 556 | ArgList{{"ATRACE_TAG_AIDL", |
| 557 | StringPrintf("\"%s::%s::cppServer\"", |
| 558 | interface.GetName().c_str(), |
| 559 | method.GetName().c_str())}}))); |
| 560 | } |
| 561 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 562 | // Call the actual method. This is implemented by the subclass. |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 563 | vector<unique_ptr<AstNode>> status_args; |
| 564 | status_args.emplace_back(new MethodCall( |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 565 | method.GetName(), |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 566 | BuildArgList(types, method, false /* not for method decl */))); |
| 567 | b->AddStatement(new Statement(new MethodCall( |
| 568 | StringPrintf("%s %s", kBinderStatusLiteral, kStatusVarName), |
| 569 | ArgList(std::move(status_args))))); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 570 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 571 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 572 | b->AddStatement(new Statement(new MethodCall("atrace_end", |
| 573 | "ATRACE_TAG_AIDL"))); |
| 574 | } |
| 575 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 576 | // Write exceptions during transaction handling to parcel. |
| 577 | if (!method.IsOneway()) { |
| 578 | b->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 579 | kAndroidStatusVarName, |
| 580 | StringPrintf("%s.writeToParcel(%s)", kStatusVarName, kReplyVarName))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 581 | b->AddStatement(BreakOnStatusNotOk()); |
| 582 | IfStatement* exception_check = new IfStatement( |
| 583 | new LiteralExpression(StringPrintf("!%s.isOk()", kStatusVarName))); |
| 584 | b->AddStatement(exception_check); |
| 585 | exception_check->OnTrue()->AddLiteral("break"); |
| 586 | } |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 587 | |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 588 | // If we have a return value, write it first. |
| 589 | if (return_type != types.VoidType()) { |
Christopher Wiley | 2aaeda8 | 2015-10-19 15:16:49 -0700 | [diff] [blame] | 590 | string writeMethod = |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 591 | string(kReplyVarName) + "->" + |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 592 | return_type->WriteToParcelMethod(); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 593 | b->AddStatement(new Assignment{ |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 594 | kAndroidStatusVarName, new MethodCall{writeMethod, |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 595 | ArgList{return_type->WriteCast(kReturnVarName)}}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 596 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 599 | // Write each out parameter to the reply parcel. |
| 600 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 601 | // Serialization looks roughly like: |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 602 | // _aidl_ret_status = data.WriteInt32(out_param_name); |
| 603 | // if (_aidl_ret_status != ::android::OK) { break; } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 604 | const Type* type = a->GetType().GetLanguageType<Type>(); |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 605 | const string& writeMethod = type->WriteToParcelMethod(); |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 606 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 607 | b->AddStatement(new Assignment{ |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 608 | kAndroidStatusVarName, |
| 609 | new MethodCall{string(kReplyVarName) + "->" + writeMethod, |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 610 | type->WriteCast(BuildVarName(*a))}}); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 611 | b->AddStatement(BreakOnStatusNotOk()); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return true; |
| 615 | } |
| 616 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 617 | bool HandleServerMetaTransaction(const TypeNamespace&, const AidlInterface& interface, |
| 618 | const AidlMethod& method, const Options& options, |
| 619 | StatementBlock* b) { |
| 620 | CHECK(!method.IsUserDefined()); |
| 621 | |
| 622 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 623 | std::ostringstream code; |
| 624 | code << "_aidl_reply->writeInt32(" << ClassName(interface, ClassNames::INTERFACE) |
| 625 | << "::VERSION)"; |
| 626 | b->AddLiteral(code.str()); |
| 627 | return true; |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 632 | } // namespace |
| 633 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 634 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, const AidlInterface& interface, |
| 635 | const Options& options) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 636 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
| 637 | vector<string> include_list{ |
| 638 | HeaderFile(interface, ClassNames::SERVER, false), |
| 639 | kParcelHeader |
| 640 | }; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 641 | unique_ptr<MethodImpl> on_transact{new MethodImpl{ |
| 642 | kAndroidStatusLiteral, bn_name, "onTransact", |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 643 | ArgList{{StringPrintf("uint32_t %s", kCodeVarName), |
| 644 | StringPrintf("const %s& %s", kAndroidParcelLiteral, |
| 645 | kDataVarName), |
| 646 | StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName), |
| 647 | StringPrintf("uint32_t %s", kFlagsVarName)}} |
Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 648 | }}; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 649 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 650 | // Declare the status_t variable |
Christopher Wiley | 05f4f89 | 2015-10-14 13:30:43 -0700 | [diff] [blame] | 651 | on_transact->GetStatementBlock()->AddLiteral( |
Christopher Wiley | 1095712 | 2015-12-04 14:35:38 -0800 | [diff] [blame] | 652 | StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, |
| 653 | kAndroidStatusOk)); |
Christopher Wiley | 05f4f89 | 2015-10-14 13:30:43 -0700 | [diff] [blame] | 654 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 655 | // Add the all important switch statement, but retain a pointer to it. |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 656 | SwitchStatement* s = new SwitchStatement{kCodeVarName}; |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 657 | on_transact->GetStatementBlock()->AddStatement(s); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 658 | |
| 659 | // The switch statement has a case statement for each transaction code. |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 660 | for (const auto& method : interface.GetMethods()) { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 661 | StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName())); |
| 662 | if (!b) { return nullptr; } |
| 663 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 664 | bool success = false; |
| 665 | if (method->IsUserDefined()) { |
| 666 | success = HandleServerTransaction(types, interface, *method, options, b); |
| 667 | } else { |
| 668 | success = HandleServerMetaTransaction(types, interface, *method, options, b); |
| 669 | } |
| 670 | if (!success) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 671 | return nullptr; |
| 672 | } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // The switch statement has a default case which defers to the super class. |
| 676 | // The superclass handles a few pre-defined transactions. |
| 677 | StatementBlock* b = s->AddCase(""); |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 678 | b->AddLiteral(StringPrintf( |
| 679 | "%s = ::android::BBinder::onTransact(%s, %s, " |
| 680 | "%s, %s)", kAndroidStatusVarName, kCodeVarName, |
| 681 | kDataVarName, kReplyVarName, kFlagsVarName)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 682 | |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 683 | // If we saw a null reference, we can map that to an appropriate exception. |
| 684 | IfStatement* null_check = new IfStatement( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 685 | new LiteralExpression(string(kAndroidStatusVarName) + |
| 686 | " == ::android::UNEXPECTED_NULL")); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 687 | on_transact->GetStatementBlock()->AddStatement(null_check); |
| 688 | null_check->OnTrue()->AddStatement(new Assignment( |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 689 | kAndroidStatusVarName, |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 690 | StringPrintf("%s::fromExceptionCode(%s::EX_NULL_POINTER)" |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 691 | ".writeToParcel(%s)", |
| 692 | kBinderStatusLiteral, kBinderStatusLiteral, |
| 693 | kReplyVarName))); |
Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 694 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 695 | // Finally, the server's onTransact method just returns a status code. |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 696 | on_transact->GetStatementBlock()->AddLiteral( |
| 697 | StringPrintf("return %s", kAndroidStatusVarName)); |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 698 | |
| 699 | return unique_ptr<Document>{new CppSource{ |
| 700 | include_list, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 701 | NestInNamespaces(std::move(on_transact), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 704 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& types, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 705 | const AidlInterface& interface, const Options& options) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 706 | vector<string> include_list{ |
| 707 | HeaderFile(interface, ClassNames::INTERFACE, false), |
| 708 | HeaderFile(interface, ClassNames::CLIENT, false), |
| 709 | }; |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 710 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 711 | string fq_name = ClassName(interface, ClassNames::INTERFACE); |
| 712 | if (!interface.GetPackage().empty()) { |
| 713 | fq_name = interface.GetPackage() + "." + fq_name; |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 716 | vector<unique_ptr<Declaration>> decls; |
| 717 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 718 | unique_ptr<MacroDecl> meta_if{new MacroDecl{ |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 719 | "IMPLEMENT_META_INTERFACE", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 720 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE), |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 721 | '"' + fq_name + '"'}}}}; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 722 | decls.push_back(std::move(meta_if)); |
| 723 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 724 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 725 | const AidlConstantValue& value = constant->GetValue(); |
| 726 | if (value.GetType() != AidlConstantValue::Type::STRING) continue; |
| 727 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 728 | unique_ptr<MethodImpl> getter(new MethodImpl( |
| 729 | "const ::android::String16&", |
| 730 | ClassName(interface, ClassNames::INTERFACE), |
| 731 | constant->GetName(), |
| 732 | {})); |
| 733 | getter->GetStatementBlock()->AddLiteral( |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 734 | StringPrintf("static const ::android::String16 value(%s)", value.ToString().c_str())); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 735 | getter->GetStatementBlock()->AddLiteral("return value"); |
| 736 | decls.push_back(std::move(getter)); |
| 737 | } |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 738 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 739 | // Implement the default impl class. |
| 740 | // onAsBinder returns nullptr as this interface is not associated with a |
| 741 | // real binder. |
| 742 | const string default_impl(ClassName(interface, ClassNames::DEFAULT_IMPL)); |
| 743 | decls.emplace_back( |
| 744 | new LiteralDecl(StringPrintf("::android::IBinder* %s::onAsBinder() {\n" |
| 745 | " return nullptr;\n" |
| 746 | "}\n", |
| 747 | default_impl.c_str()))); |
| 748 | // Each interface method by default returns UNKNOWN_TRANSACTION with is |
| 749 | // the same status that is returned by transact() when the method is |
| 750 | // not implemented in the server side. In other words, these default |
| 751 | // methods do nothing; they only exist to aid making a real default |
| 752 | // impl class without having to override all methods in an interface. |
| 753 | for (const auto& method : interface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 754 | if (method->IsUserDefined()) { |
| 755 | std::ostringstream code; |
| 756 | code << "::android::binder::Status " << default_impl << "::" << method->GetName() |
| 757 | << BuildArgList(types, *method, true, true).ToString() << " {\n" |
| 758 | << " return ::android::binder::Status::fromStatusT(::android::UNKNOWN_TRANSACTION);\n" |
| 759 | << "}\n"; |
| 760 | decls.emplace_back(new LiteralDecl(code.str())); |
| 761 | } else { |
| 762 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 763 | std::ostringstream code; |
| 764 | code << "int32_t " << default_impl << "::" << kGetInterfaceVersion << "() {\n" |
| 765 | << " return 0;\n" |
| 766 | << "}\n"; |
| 767 | decls.emplace_back(new LiteralDecl(code.str())); |
| 768 | } |
| 769 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 770 | } |
| 771 | |
Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 772 | return unique_ptr<Document>{new CppSource{ |
| 773 | include_list, |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 774 | NestInNamespaces(std::move(decls), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 777 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, const AidlInterface& interface, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 778 | const Options& options) { |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 779 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 780 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 781 | |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 782 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{ |
| 783 | bp_name, |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 784 | ArgList{StringPrintf("const ::android::sp<::android::IBinder>& %s", |
| 785 | kImplVarName)}, |
Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 786 | ConstructorDecl::IS_EXPLICIT |
| 787 | }}; |
| 788 | unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{ |
| 789 | "~" + bp_name, |
| 790 | ArgList{}, |
| 791 | ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}}; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 792 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 793 | vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 794 | publics.push_back(std::move(constructor)); |
| 795 | publics.push_back(std::move(destructor)); |
| 796 | |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 797 | for (const auto& method: interface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 798 | if (method->IsUserDefined()) { |
| 799 | publics.push_back(BuildMethodDecl(*method, types, false)); |
| 800 | } else { |
| 801 | publics.push_back(BuildMetaMethodDecl(*method, types, options, false)); |
| 802 | } |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 805 | vector<unique_ptr<Declaration>> privates; |
| 806 | |
| 807 | if (options.Version() > 0) { |
| 808 | privates.emplace_back(new LiteralDecl("int32_t cached_version_ = -1;\n")); |
| 809 | } |
| 810 | |
| 811 | unique_ptr<ClassDecl> bp_class{new ClassDecl{ |
| 812 | bp_name, |
| 813 | "::android::BpInterface<" + i_name + ">", |
| 814 | std::move(publics), |
| 815 | std::move(privates), |
| 816 | }}; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 817 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 818 | return unique_ptr<Document>{new CppHeader{ |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 819 | BuildHeaderGuard(interface, ClassNames::CLIENT), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 820 | {kIBinderHeader, |
| 821 | kIInterfaceHeader, |
| 822 | "utils/Errors.h", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 823 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 824 | NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 827 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */, |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 828 | const AidlInterface& interface, const Options&) { |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 829 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 830 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 831 | |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 832 | unique_ptr<Declaration> on_transact{new MethodDecl{ |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 833 | kAndroidStatusLiteral, "onTransact", |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 834 | ArgList{{StringPrintf("uint32_t %s", kCodeVarName), |
| 835 | StringPrintf("const %s& %s", kAndroidParcelLiteral, |
| 836 | kDataVarName), |
| 837 | StringPrintf("%s* %s", kAndroidParcelLiteral, kReplyVarName), |
| 838 | StringPrintf("uint32_t %s = 0", kFlagsVarName)}}, |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 839 | MethodDecl::IS_OVERRIDE |
| 840 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 841 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 842 | std::vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 843 | publics.push_back(std::move(on_transact)); |
| 844 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 845 | unique_ptr<ClassDecl> bn_class{ |
| 846 | new ClassDecl{bn_name, |
Casey Dahlin | b8d9e88 | 2015-11-24 10:57:23 -0800 | [diff] [blame] | 847 | "::android::BnInterface<" + i_name + ">", |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 848 | std::move(publics), |
| 849 | {} |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 850 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 851 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 852 | return unique_ptr<Document>{new CppHeader{ |
Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 853 | BuildHeaderGuard(interface, ClassNames::SERVER), |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 854 | {"binder/IInterface.h", |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 855 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 856 | NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 859 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 860 | const AidlInterface& interface, const Options& options) { |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 861 | set<string> includes = {kIBinderHeader, kIInterfaceHeader, kStatusHeader, kStrongPointerHeader}; |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 862 | |
| 863 | for (const auto& method : interface.GetMethods()) { |
| 864 | for (const auto& argument : method->GetArguments()) { |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 865 | const Type* type = argument->GetType().GetLanguageType<Type>(); |
| 866 | type->GetHeaders(&includes); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 869 | const Type* return_type = method->GetType().GetLanguageType<Type>(); |
| 870 | return_type->GetHeaders(&includes); |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 873 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 874 | unique_ptr<ClassDecl> if_class{new ClassDecl{i_name, "::android::IInterface"}}; |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 875 | if_class->AddPublic(unique_ptr<Declaration>{new MacroDecl{ |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 876 | "DECLARE_META_INTERFACE", |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 877 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}}); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 878 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 879 | if (options.Version() > 0) { |
| 880 | std::ostringstream code; |
| 881 | code << "const int32_t VERSION = " << options.Version() << ";\n"; |
| 882 | |
| 883 | if_class->AddPublic(unique_ptr<Declaration>(new LiteralDecl(code.str()))); |
| 884 | } |
| 885 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 886 | std::vector<std::unique_ptr<Declaration>> string_constants; |
| 887 | unique_ptr<Enum> int_constant_enum{new Enum{"", "int32_t"}}; |
| 888 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 889 | const AidlConstantValue& value = constant->GetValue(); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 890 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 891 | switch (value.GetType()) { |
| 892 | case AidlConstantValue::Type::STRING: { |
| 893 | unique_ptr<Declaration> getter(new MethodDecl( |
| 894 | "const ::android::String16&", constant->GetName(), {}, MethodDecl::IS_STATIC)); |
| 895 | string_constants.push_back(std::move(getter)); |
| 896 | break; |
| 897 | } |
| 898 | case AidlConstantValue::Type::INTEGER: { |
| 899 | int_constant_enum->AddValue(constant->GetName(), value.ToString()); |
| 900 | break; |
| 901 | } |
| 902 | default: { |
| 903 | LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType()); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | if (int_constant_enum->HasValues()) { |
| 908 | if_class->AddPublic(std::move(int_constant_enum)); |
| 909 | } |
| 910 | if (!string_constants.empty()) { |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 911 | includes.insert(kString16Header); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 912 | |
| 913 | for (auto& string_constant : string_constants) { |
| 914 | if_class->AddPublic(std::move(string_constant)); |
| 915 | } |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 916 | } |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 917 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 918 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 919 | includes.insert(kTraceHeader); |
| 920 | } |
| 921 | |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 922 | if (!interface.GetMethods().empty()) { |
| 923 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
| 924 | for (const auto& method : interface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 925 | if (method->IsUserDefined()) { |
| 926 | // Each method gets an enum entry and pure virtual declaration. |
| 927 | if_class->AddPublic(BuildMethodDecl(*method, types, true)); |
| 928 | call_enum->AddValue( |
| 929 | UpperCase(method->GetName()), |
| 930 | StringPrintf("::android::IBinder::FIRST_CALL_TRANSACTION + %d", method->GetId())); |
| 931 | } else { |
| 932 | if_class->AddPublic(BuildMetaMethodDecl(*method, types, options, true)); |
| 933 | call_enum->AddValue(UpperCase(method->GetName()), std::to_string(method->GetId())); |
| 934 | } |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 935 | } |
| 936 | if_class->AddPublic(std::move(call_enum)); |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 937 | } |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 938 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 939 | vector<unique_ptr<Declaration>> decls; |
| 940 | decls.emplace_back(std::move(if_class)); |
| 941 | |
| 942 | // Base class for the default implementation. |
| 943 | vector<string> method_decls; |
| 944 | for (const auto& method : interface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 945 | if (method->IsUserDefined()) { |
| 946 | method_decls.emplace_back(BuildMethodDecl(*method, types, false)->ToString()); |
| 947 | } else { |
| 948 | method_decls.emplace_back(BuildMetaMethodDecl(*method, types, options, false)->ToString()); |
| 949 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 950 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame^] | 951 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 952 | decls.emplace_back(new LiteralDecl( |
| 953 | android::base::StringPrintf("class %s : public %s {\n" |
| 954 | "public:\n" |
| 955 | " ::android::IBinder* onAsBinder() override;\n" |
| 956 | " %s\n" |
| 957 | "};\n", |
| 958 | ClassName(interface, ClassNames::DEFAULT_IMPL).c_str(), |
| 959 | i_name.c_str(), Join(method_decls, " ").c_str()))); |
| 960 | |
| 961 | return unique_ptr<Document>{ |
| 962 | new CppHeader{BuildHeaderGuard(interface, ClassNames::INTERFACE), |
| 963 | vector<string>(includes.begin(), includes.end()), |
| 964 | NestInNamespaces(std::move(decls), interface.GetSplitPackage())}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 967 | std::unique_ptr<Document> BuildParcelHeader(const TypeNamespace& /*types*/, |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 968 | const AidlStructuredParcelable& parcel, |
| 969 | const Options&) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 970 | unique_ptr<ClassDecl> parcel_class{new ClassDecl{parcel.GetName(), "::android::Parcelable"}}; |
| 971 | |
| 972 | set<string> includes = {kStatusHeader, kParcelHeader}; |
| 973 | for (const auto& variable : parcel.GetFields()) { |
| 974 | const Type* type = variable->GetType().GetLanguageType<Type>(); |
| 975 | type->GetHeaders(&includes); |
| 976 | } |
| 977 | |
| 978 | for (const auto& variable : parcel.GetFields()) { |
| 979 | const Type* type = variable->GetType().GetLanguageType<Type>(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 980 | const AidlConstantValue* default_value = variable->GetDefaultValue(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 981 | |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 982 | std::ostringstream out; |
| 983 | out << type->CppType().c_str() << " " << variable->GetName().c_str(); |
| 984 | if (default_value) { |
| 985 | out << " = " << type->CppType().c_str() << "(" << default_value->ToString() << ")"; |
| 986 | } |
| 987 | out << ";\n"; |
| 988 | |
| 989 | parcel_class->AddPublic(std::unique_ptr<LiteralDecl>(new LiteralDecl(out.str()))); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel", |
Steven Moreland | ce39c53 | 2018-07-11 16:59:50 -0700 | [diff] [blame] | 993 | ArgList("const ::android::Parcel* _aidl_parcel"), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 994 | MethodDecl::IS_OVERRIDE)); |
| 995 | parcel_class->AddPublic(std::move(read)); |
| 996 | unique_ptr<MethodDecl> write(new MethodDecl(kAndroidStatusLiteral, "writeToParcel", |
Steven Moreland | ce39c53 | 2018-07-11 16:59:50 -0700 | [diff] [blame] | 997 | ArgList("::android::Parcel* _aidl_parcel"), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 998 | MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST)); |
| 999 | parcel_class->AddPublic(std::move(write)); |
| 1000 | |
| 1001 | return unique_ptr<Document>{new CppHeader{ |
| 1002 | BuildHeaderGuard(parcel, ClassNames::BASE), vector<string>(includes.begin(), includes.end()), |
| 1003 | NestInNamespaces(std::move(parcel_class), parcel.GetSplitPackage())}}; |
| 1004 | } |
Steven Moreland | 1c41e97 | 2018-07-09 16:07:00 -0700 | [diff] [blame] | 1005 | std::unique_ptr<Document> BuildParcelSource(const TypeNamespace& /*types*/, |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1006 | const AidlStructuredParcelable& parcel, |
| 1007 | const Options&) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1008 | unique_ptr<MethodImpl> read{new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), |
| 1009 | "readFromParcel", |
Steven Moreland | ce39c53 | 2018-07-11 16:59:50 -0700 | [diff] [blame] | 1010 | ArgList("const ::android::Parcel* _aidl_parcel")}}; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1011 | StatementBlock* read_block = read->GetStatementBlock(); |
| 1012 | read_block->AddLiteral( |
| 1013 | StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk)); |
| 1014 | for (const auto& variable : parcel.GetFields()) { |
| 1015 | string method = variable->GetType().GetLanguageType<Type>()->ReadFromParcelMethod(); |
| 1016 | |
| 1017 | read_block->AddStatement(new Assignment( |
| 1018 | kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()), |
| 1019 | ArgList("&" + variable->GetName())))); |
| 1020 | read_block->AddStatement(ReturnOnStatusNotOk()); |
| 1021 | } |
| 1022 | read_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName)); |
| 1023 | |
Steven Moreland | ce39c53 | 2018-07-11 16:59:50 -0700 | [diff] [blame] | 1024 | unique_ptr<MethodImpl> write{ |
| 1025 | new MethodImpl{kAndroidStatusLiteral, parcel.GetName(), "writeToParcel", |
| 1026 | ArgList("::android::Parcel* _aidl_parcel"), true /*const*/}}; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1027 | StatementBlock* write_block = write->GetStatementBlock(); |
| 1028 | write_block->AddLiteral( |
| 1029 | StringPrintf("%s %s = %s", kAndroidStatusLiteral, kAndroidStatusVarName, kAndroidStatusOk)); |
| 1030 | for (const auto& variable : parcel.GetFields()) { |
| 1031 | string method = variable->GetType().GetLanguageType<Type>()->WriteToParcelMethod(); |
| 1032 | |
| 1033 | write_block->AddStatement(new Assignment( |
| 1034 | kAndroidStatusVarName, new MethodCall(StringPrintf("_aidl_parcel->%s", method.c_str()), |
| 1035 | ArgList(variable->GetName())))); |
| 1036 | write_block->AddStatement(ReturnOnStatusNotOk()); |
| 1037 | } |
| 1038 | write_block->AddLiteral(StringPrintf("return %s", kAndroidStatusVarName)); |
| 1039 | |
| 1040 | vector<unique_ptr<Declaration>> file_decls; |
| 1041 | file_decls.push_back(std::move(read)); |
| 1042 | file_decls.push_back(std::move(write)); |
| 1043 | |
| 1044 | set<string> includes = {}; |
| 1045 | parcel.GetLanguageType<Type>()->GetHeaders(&includes); |
| 1046 | |
| 1047 | return unique_ptr<Document>{ |
| 1048 | new CppSource{vector<string>(includes.begin(), includes.end()), |
| 1049 | NestInNamespaces(std::move(file_decls), parcel.GetSplitPackage())}}; |
| 1050 | } |
| 1051 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1052 | bool WriteHeader(const Options& options, const TypeNamespace& types, const AidlInterface& interface, |
| 1053 | const IoDelegate& io_delegate, ClassNames header_type) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1054 | unique_ptr<Document> header; |
| 1055 | switch (header_type) { |
| 1056 | case ClassNames::INTERFACE: |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1057 | header = BuildInterfaceHeader(types, interface, options); |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1058 | break; |
| 1059 | case ClassNames::CLIENT: |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1060 | header = BuildClientHeader(types, interface, options); |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1061 | break; |
| 1062 | case ClassNames::SERVER: |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1063 | header = BuildServerHeader(types, interface, options); |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1064 | break; |
| 1065 | default: |
| 1066 | LOG(FATAL) << "aidl internal error"; |
| 1067 | } |
| 1068 | if (!header) { |
| 1069 | LOG(ERROR) << "aidl internal error: Failed to generate header."; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1072 | |
Christopher Wiley | 9d6e0b2 | 2015-11-13 12:18:16 -0800 | [diff] [blame] | 1073 | const string header_path = options.OutputHeaderDir() + OS_PATH_SEPARATOR + |
| 1074 | HeaderFile(interface, header_type); |
| 1075 | unique_ptr<CodeWriter> code_writer(io_delegate.GetCodeWriter(header_path)); |
| 1076 | header->Write(code_writer.get()); |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1077 | |
Christopher Wiley | 9d6e0b2 | 2015-11-13 12:18:16 -0800 | [diff] [blame] | 1078 | const bool success = code_writer->Close(); |
| 1079 | if (!success) { |
| 1080 | io_delegate.RemovePath(header_path); |
| 1081 | } |
| 1082 | |
| 1083 | return success; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 1086 | } // namespace internals |
| 1087 | |
| 1088 | using namespace internals; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 1089 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1090 | string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type, bool use_os_sep) { |
| 1091 | string file_path = defined_type.GetPackage(); |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 1092 | for (char& c: file_path) { |
| 1093 | if (c == '.') { |
| 1094 | c = (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 1095 | } |
| 1096 | } |
| 1097 | if (!file_path.empty()) { |
| 1098 | file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 1099 | } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1100 | file_path += ClassName(defined_type, class_type); |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 1101 | file_path += ".h"; |
| 1102 | |
| 1103 | return file_path; |
| 1104 | } |
| 1105 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1106 | bool GenerateCppInterface(const string& output_file, const Options& options, |
| 1107 | const TypeNamespace& types, const AidlInterface& interface, |
| 1108 | const IoDelegate& io_delegate) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1109 | auto interface_src = BuildInterfaceSource(types, interface, options); |
| 1110 | auto client_src = BuildClientSource(types, interface, options); |
| 1111 | auto server_src = BuildServerSource(types, interface, options); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 1112 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1113 | if (!interface_src || !client_src || !server_src) { |
| 1114 | return false; |
| 1115 | } |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 1116 | |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1117 | if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 1118 | interface.GetSplitPackage())) { |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1119 | LOG(ERROR) << "Failed to create directory structure for headers."; |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | if (!WriteHeader(options, types, interface, io_delegate, |
| 1124 | ClassNames::INTERFACE) || |
| 1125 | !WriteHeader(options, types, interface, io_delegate, |
| 1126 | ClassNames::CLIENT) || |
| 1127 | !WriteHeader(options, types, interface, io_delegate, |
| 1128 | ClassNames::SERVER)) { |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1132 | unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(output_file); |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 1133 | interface_src->Write(writer.get()); |
| 1134 | client_src->Write(writer.get()); |
| 1135 | server_src->Write(writer.get()); |
| 1136 | |
Christopher Wiley | 9d6e0b2 | 2015-11-13 12:18:16 -0800 | [diff] [blame] | 1137 | const bool success = writer->Close(); |
| 1138 | if (!success) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1139 | io_delegate.RemovePath(options.OutputFile()); |
Christopher Wiley | 9d6e0b2 | 2015-11-13 12:18:16 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | return success; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1145 | bool GenerateCppParcel(const string& output_file, const Options& options, |
| 1146 | const cpp::TypeNamespace& types, const AidlStructuredParcelable& parcelable, |
| 1147 | const IoDelegate& io_delegate) { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 1148 | auto header = BuildParcelHeader(types, parcelable, options); |
| 1149 | auto source = BuildParcelSource(types, parcelable, options); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1150 | |
| 1151 | if (!header || !source) { |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
| 1155 | if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), parcelable.GetSplitPackage())) { |
| 1156 | LOG(ERROR) << "Failed to create directory structure for headers."; |
| 1157 | } |
| 1158 | |
| 1159 | const string header_path = |
| 1160 | options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::BASE); |
| 1161 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 1162 | header->Write(header_writer.get()); |
| 1163 | CHECK(header_writer->Close()); |
| 1164 | |
Steven Moreland | 81079f9 | 2018-07-06 16:15:53 -0700 | [diff] [blame] | 1165 | // TODO(b/111362593): no unecessary files just to have consistent output with interfaces |
| 1166 | const string bp_header = |
| 1167 | options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::CLIENT); |
| 1168 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 1169 | bp_writer->Write("#error TODO(b/111362593) parcelables do not have bp classes"); |
| 1170 | CHECK(bp_writer->Close()); |
| 1171 | const string bn_header = |
| 1172 | options.OutputHeaderDir() + OS_PATH_SEPARATOR + HeaderFile(parcelable, ClassNames::SERVER); |
| 1173 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 1174 | bn_writer->Write("#error TODO(b/111362593) parcelables do not have bn classes"); |
| 1175 | CHECK(bn_writer->Close()); |
| 1176 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1177 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1178 | source->Write(source_writer.get()); |
| 1179 | CHECK(source_writer->Close()); |
| 1180 | |
| 1181 | return true; |
| 1182 | } |
| 1183 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1184 | bool GenerateCpp(const string& output_file, const Options& options, const TypeNamespace& types, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1185 | const AidlDefinedType& defined_type, const IoDelegate& io_delegate) { |
| 1186 | const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable(); |
| 1187 | if (parcelable != nullptr) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1188 | return GenerateCppParcel(output_file, options, types, *parcelable, io_delegate); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | const AidlInterface* interface = defined_type.AsInterface(); |
| 1192 | if (interface != nullptr) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1193 | return GenerateCppInterface(output_file, options, types, *interface, io_delegate); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | CHECK(false) << "Unrecognized type sent for cpp generation."; |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 1200 | } // namespace cpp |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 1201 | } // namespace aidl |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 1202 | } // namespace android |