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