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