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