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