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