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