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