Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018, 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_ndk.h" |
| 18 | |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 19 | #include "aidl.h" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 20 | #include "aidl_language.h" |
| 21 | #include "aidl_to_cpp_common.h" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 22 | #include "aidl_to_ndk.h" |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 23 | #include "logging.h" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 24 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
| 26 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace aidl { |
| 29 | namespace ndk { |
| 30 | |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 31 | static constexpr const char* kClazz = "_g_aidl_clazz"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 32 | static constexpr const char* kDescriptor = "descriptor"; |
| 33 | static constexpr const char* kVersion = "version"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 34 | static constexpr const char* kHash = "hash"; |
| 35 | static constexpr const char* kCachedVersion = "_aidl_cached_version"; |
| 36 | static constexpr const char* kCachedHash = "_aidl_cached_hash"; |
| 37 | static constexpr const char* kCachedHashMutex = "_aidl_cached_hash_mutex"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 38 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 39 | using namespace internals; |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 40 | namespace internals { |
| 41 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
| 42 | const AidlStructuredParcelable& defined_type, const Options& options); |
| 43 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
| 44 | const AidlStructuredParcelable& defined_type, const Options& options); |
| 45 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
| 46 | const AidlUnionDecl& defined_type, const Options& options); |
| 47 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
| 48 | const AidlUnionDecl& defined_type, const Options& options); |
| 49 | } // namespace internals |
| 50 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 51 | using cpp::ClassNames; |
| 52 | |
| 53 | void GenerateNdkInterface(const string& output_file, const Options& options, |
| 54 | const AidlTypenames& types, const AidlInterface& defined_type, |
| 55 | const IoDelegate& io_delegate) { |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 56 | const string i_header = options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 57 | unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header)); |
| 58 | GenerateInterfaceHeader(*i_writer, types, defined_type, options); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 59 | AIDL_FATAL_IF(!i_writer->Close(), i_header); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 60 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 61 | const string bp_header = |
| 62 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 63 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 64 | GenerateClientHeader(*bp_writer, types, defined_type, options); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 65 | AIDL_FATAL_IF(!bp_writer->Close(), bp_header); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 66 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 67 | const string bn_header = |
| 68 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 69 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 70 | GenerateServerHeader(*bn_writer, types, defined_type, options); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 71 | AIDL_FATAL_IF(!bn_writer->Close(), bn_header); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 72 | |
| 73 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 74 | GenerateSource(*source_writer, types, defined_type, options); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 75 | AIDL_FATAL_IF(!source_writer->Close(), output_file); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 78 | template <typename ParcelableType> |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 79 | void GenerateNdkParcel(const string& output_file, const Options& options, |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 80 | const AidlTypenames& types, const ParcelableType& defined_type, |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 81 | const IoDelegate& io_delegate) { |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 82 | const string header_path = |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 83 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 84 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 85 | GenerateParcelHeader(*header_writer, types, defined_type, options); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 86 | |
| 87 | unique_ptr<CodeWriter> source_writer(io_delegate.GetCodeWriter(output_file)); |
| 88 | if (defined_type.IsGeneric()) { |
| 89 | // Need to write source to header if this is a template |
| 90 | GenerateParcelSource(*header_writer, types, defined_type, options); |
| 91 | } else { |
| 92 | GenerateParcelSource(*source_writer, types, defined_type, options); |
| 93 | } |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 94 | (source_writer->Close()); |
| 95 | AIDL_FATAL_IF(!header_writer->Close(), header_path); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 96 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 97 | const string bp_header = |
| 98 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 99 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 100 | *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 101 | AIDL_FATAL_IF(!bp_writer->Close(), bp_header); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 102 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 103 | const string bn_header = |
| 104 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 105 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 106 | *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 107 | AIDL_FATAL_IF(!bn_writer->Close(), bn_header); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 110 | void GenerateNdkParcelDeclaration(const std::string& filename, const IoDelegate& io_delegate) { |
| 111 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 112 | *code_writer |
| 113 | << "// This file is intentionally left blank as placeholder for parcel declaration.\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 114 | AIDL_FATAL_IF(!code_writer->Close(), filename); |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 117 | void GenerateNdkEnumDeclaration(const string& output_file, const Options& options, |
| 118 | const AidlTypenames& types, const AidlEnumDeclaration& defined_type, |
| 119 | const IoDelegate& io_delegate) { |
| 120 | const string header_path = |
| 121 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
| 122 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 123 | GenerateEnumHeader(*header_writer, types, defined_type, options); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 124 | AIDL_FATAL_IF(!header_writer->Close(), header_path); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 125 | |
| 126 | const string bp_header = |
| 127 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
| 128 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 129 | *bp_writer << "#error TODO(b/111362593) enums do not have bp classes\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 130 | AIDL_FATAL_IF(!bp_writer->Close(), bp_header); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 131 | |
| 132 | const string bn_header = |
| 133 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
| 134 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 135 | *bn_writer << "#error TODO(b/111362593) enums do not have bn classes\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 136 | AIDL_FATAL_IF(!bn_writer->Close(), bn_header); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 137 | |
| 138 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 139 | *source_writer |
| 140 | << "// This file is intentionally left blank as placeholder for enum declaration.\n"; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 141 | AIDL_FATAL_IF(!source_writer->Close(), output_file); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 144 | void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types, |
| 145 | const AidlDefinedType& defined_type, const IoDelegate& io_delegate) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 146 | if (const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable(); |
| 147 | parcelable != nullptr) { |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 148 | GenerateNdkParcel<AidlStructuredParcelable>(output_file, options, types, *parcelable, |
| 149 | io_delegate); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (const AidlUnionDecl* union_decl = defined_type.AsUnionDeclaration(); union_decl != nullptr) { |
| 154 | GenerateNdkParcel<AidlUnionDecl>(output_file, options, types, *union_decl, io_delegate); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 158 | if (const AidlParcelable* parcelable_decl = defined_type.AsParcelable(); |
| 159 | parcelable_decl != nullptr) { |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 160 | GenerateNdkParcelDeclaration(output_file, io_delegate); |
| 161 | return; |
| 162 | } |
| 163 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 164 | if (const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration(); |
| 165 | enum_decl != nullptr) { |
| 166 | GenerateNdkEnumDeclaration(output_file, options, types, *enum_decl, io_delegate); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if (const AidlInterface* interface = defined_type.AsInterface(); interface != nullptr) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 171 | GenerateNdkInterface(output_file, options, types, *interface, io_delegate); |
| 172 | return; |
| 173 | } |
| 174 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 175 | AIDL_FATAL(defined_type) << "Unrecognized type sent for NDK cpp generation."; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 176 | } |
| 177 | namespace internals { |
| 178 | |
| 179 | void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 180 | out << "namespace aidl {\n"; |
| 181 | cpp::EnterNamespace(out, defined_type); |
| 182 | } |
| 183 | void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 184 | cpp::LeaveNamespace(out, defined_type); |
| 185 | out << "} // namespace aidl\n"; |
| 186 | } |
| 187 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 188 | static void StatusCheckGoto(CodeWriter& out) { |
| 189 | out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n"; |
| 190 | } |
| 191 | static void StatusCheckBreak(CodeWriter& out) { |
| 192 | out << "if (_aidl_ret_status != STATUS_OK) break;\n\n"; |
| 193 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 194 | static void StatusCheckReturn(CodeWriter& out) { |
| 195 | out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n"; |
| 196 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 197 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 198 | static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types, |
| 199 | const AidlDefinedType& defined_type) { |
Steven Moreland | 8769f93 | 2019-10-30 15:38:10 -0700 | [diff] [blame] | 200 | out << "#include <cstdint>\n"; |
| 201 | out << "#include <memory>\n"; |
| 202 | out << "#include <optional>\n"; |
| 203 | out << "#include <string>\n"; |
| 204 | out << "#include <vector>\n"; |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 205 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 206 | out << "#include <android/binder_stability.h>\n"; |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 207 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 208 | |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 209 | if (defined_type.IsSensitiveData()) { |
| 210 | out << "#include <android/binder_parcel_platform.h>\n"; |
| 211 | out << "#include <android/binder_ibinder_platform.h>\n"; |
| 212 | } |
| 213 | |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 214 | auto headerFilePath = [&types](const AidlTypeSpecifier& typespec) -> std::string { |
| 215 | const AidlDefinedType* type = types.TryGetDefinedType(typespec.GetName()); |
| 216 | if (type == nullptr) { |
| 217 | // could be a primitive type. |
| 218 | return ""; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 219 | } |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 220 | |
| 221 | if (type->AsInterface() != nullptr) { |
| 222 | return NdkHeaderFile(*type, ClassNames::RAW, false /*use_os_sep*/); |
| 223 | } else if (type->AsStructuredParcelable() != nullptr) { |
| 224 | return NdkHeaderFile(*type, ClassNames::RAW, false /*use_os_sep*/); |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 225 | } else if (type->AsUnionDeclaration() != nullptr) { |
| 226 | return NdkHeaderFile(*type, ClassNames::RAW, false /*use_os_sep*/); |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 227 | } else if (type->AsParcelable() != nullptr) { |
| 228 | return type->AsParcelable()->GetCppHeader(); |
| 229 | } else if (type->AsEnumDeclaration() != nullptr) { |
| 230 | return NdkHeaderFile(*type, ClassNames::RAW, false /*use_os_sep*/); |
| 231 | } else { |
| 232 | AIDL_FATAL(*type) << "Unrecognized type."; |
| 233 | return ""; |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | std::set<std::string> includes; |
| 238 | |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 239 | // visit a type and collect all reference types' headers |
| 240 | std::function<void(const AidlTypeSpecifier& type)> visit = [&](const AidlTypeSpecifier& type) { |
| 241 | includes.insert(headerFilePath(type)); |
| 242 | if (type.IsGeneric()) { |
| 243 | for (const auto& param : type.GetTypeParameters()) { |
| 244 | visit(*param); |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 249 | const AidlInterface* interface = defined_type.AsInterface(); |
| 250 | if (interface != nullptr) { |
| 251 | for (const auto& method : interface->GetMethods()) { |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 252 | visit(method->GetType()); |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 253 | for (const auto& argument : method->GetArguments()) { |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 254 | visit(argument->GetType()); |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 259 | auto visit_parcelable = [&](const auto& parcelable) { |
| 260 | for (const auto& field : parcelable.GetFields()) { |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 261 | visit(field->GetType()); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 262 | // Check the fields for generic type arguments |
| 263 | if (field->GetType().IsGeneric()) { |
| 264 | for (const auto& type_argument : field->GetType().GetTypeParameters()) { |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 265 | visit(*type_argument); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 268 | } |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable(); |
| 272 | if (parcelable != nullptr) { |
| 273 | visit_parcelable(*parcelable); |
| 274 | } |
| 275 | |
| 276 | const AidlUnionDecl* union_decl = defined_type.AsUnionDeclaration(); |
| 277 | if (union_decl != nullptr) { |
| 278 | visit_parcelable(*union_decl); |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration(); |
| 282 | if (enum_decl != nullptr) { |
Jooyung Han | 14004ed | 2020-10-16 03:49:57 +0900 | [diff] [blame] | 283 | visit(enum_decl->GetBackingType()); |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | for (const auto& path : includes) { |
| 287 | if (path == "") { |
| 288 | continue; |
| 289 | } |
| 290 | out << "#include <" << path << ">\n"; |
| 291 | } |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 292 | } |
Jiyong Park | f1f5c80 | 2020-05-19 17:33:00 +0900 | [diff] [blame] | 293 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 294 | static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types, |
| 295 | const AidlDefinedType& /*defined_type*/) { |
Steven Moreland | 8769f93 | 2019-10-30 15:38:10 -0700 | [diff] [blame] | 296 | out << "#include <android/binder_parcel_utils.h>\n"; |
| 297 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 298 | types.IterateTypes([&](const AidlDefinedType& a_defined_type) { |
| 299 | if (a_defined_type.AsInterface() != nullptr) { |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 300 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/) |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 301 | << ">\n"; |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 302 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/) |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 303 | << ">\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 304 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::RAW, false /*use_os_sep*/) |
| 305 | << ">\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 306 | } |
| 307 | }); |
| 308 | } |
| 309 | |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 310 | static void GenerateConstantDeclarations(CodeWriter& out, const AidlTypenames& types, |
| 311 | const AidlInterface& interface) { |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 312 | for (const auto& constant : interface.GetConstantDeclarations()) { |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 313 | const AidlTypeSpecifier& type = constant->GetType(); |
| 314 | |
| 315 | if (type.ToString() == "String") { |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 316 | out << "static const char* " << constant->GetName() << ";\n"; |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 317 | } else { |
| 318 | out << "enum : " << NdkNameOf(types, type, StorageMode::STACK) << " { " << constant->GetName() |
| 319 | << " = " << constant->ValueString(ConstantValueDecorator) << " };\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 322 | } |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 323 | |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 324 | static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) { |
| 325 | const std::string clazz = ClassName(interface, ClassNames::INTERFACE); |
| 326 | |
| 327 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 328 | const AidlConstantValue& value = constant->GetValue(); |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 329 | AIDL_FATAL_IF(value.GetType() == AidlConstantValue::Type::UNARY || |
| 330 | value.GetType() == AidlConstantValue::Type::BINARY, |
| 331 | value); |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 332 | if (value.GetType() == AidlConstantValue::Type::STRING) { |
| 333 | out << "const char* " << clazz << "::" << constant->GetName() << " = " |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 334 | << constant->ValueString(ConstantValueDecorator) << ";\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 339 | void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, |
| 340 | const Options& options) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 341 | GenerateSourceIncludes(out, types, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 342 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 343 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 344 | EnterNdkNamespace(out, defined_type); |
Jooyung Han | b8e01b9 | 2020-11-01 16:49:13 +0900 | [diff] [blame] | 345 | if (options.GenLog()) { |
| 346 | out << cpp::kToStringHelper; |
| 347 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 348 | GenerateClassSource(out, types, defined_type, options); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 349 | GenerateClientSource(out, types, defined_type, options); |
| 350 | GenerateServerSource(out, types, defined_type, options); |
| 351 | GenerateInterfaceSource(out, types, defined_type, options); |
| 352 | LeaveNdkNamespace(out, defined_type); |
| 353 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 354 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 355 | static std::string MethodId(const AidlMethod& m) { |
| 356 | return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)"; |
| 357 | } |
| 358 | |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 359 | static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types, |
| 360 | const AidlInterface& defined_type, |
| 361 | const AidlMethod& method, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 362 | const Options& options) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 363 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 364 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 365 | out << NdkMethodDecl(types, method, clazz) << " {\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 366 | out.Indent(); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 367 | out << "binder_status_t _aidl_ret_status = STATUS_OK;\n"; |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 368 | out << "::ndk::ScopedAStatus _aidl_status;\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 369 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 370 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 371 | out << "const std::lock_guard<std::mutex> lock(" << kCachedHashMutex << ");\n"; |
| 372 | out << "if (" << kCachedHash << " != \"-1\") {\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 373 | out.Indent(); |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 374 | out << "*_aidl_return = " << kCachedHash << ";\n" |
| 375 | << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n" |
| 376 | << "return _aidl_status;\n"; |
| 377 | out.Dedent(); |
| 378 | out << "}\n"; |
| 379 | } else if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 380 | out << "if (" << kCachedVersion << " != -1) {\n"; |
| 381 | out.Indent(); |
| 382 | out << "*_aidl_return = " << kCachedVersion << ";\n" |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 383 | << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n" |
| 384 | << "return _aidl_status;\n"; |
| 385 | out.Dedent(); |
| 386 | out << "}\n"; |
| 387 | } |
| 388 | out << "::ndk::ScopedAParcel _aidl_in;\n"; |
| 389 | out << "::ndk::ScopedAParcel _aidl_out;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 390 | out << "\n"; |
| 391 | |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 392 | if (options.GenLog()) { |
| 393 | out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::CLIENT), method, |
| 394 | false /* isServer */, true /* isNdk */); |
| 395 | } |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 396 | if (options.GenTraces()) { |
Devin Moore | f760087 | 2020-05-13 15:47:50 -0700 | [diff] [blame] | 397 | out << "ScopedTrace _aidl_trace(\"AIDL::" << Options::LanguageToString(options.TargetLanguage()) |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 398 | << "::" << ClassName(defined_type, ClassNames::INTERFACE) << "::" << method.GetName() |
| 399 | << "::client\");\n"; |
| 400 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 401 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 402 | out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n"; |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 403 | if (defined_type.IsSensitiveData()) { |
| 404 | out << "AParcel_markSensitive(_aidl_in.get());\n"; |
| 405 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 406 | StatusCheckGoto(out); |
| 407 | |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 408 | for (const auto& arg : method.GetArguments()) { |
| 409 | const std::string var_name = cpp::BuildVarName(*arg); |
| 410 | |
| 411 | if (arg->IsIn()) { |
| 412 | out << "_aidl_ret_status = "; |
| 413 | const std::string prefix = (arg->IsOut() ? "*" : ""); |
| 414 | WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name}); |
| 415 | out << ";\n"; |
| 416 | StatusCheckGoto(out); |
| 417 | } else if (arg->IsOut() && arg->GetType().IsArray()) { |
| 418 | out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name |
| 419 | << ");\n"; |
| 420 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 421 | } |
| 422 | out << "_aidl_ret_status = AIBinder_transact(\n"; |
| 423 | out.Indent(); |
| 424 | out << "asBinder().get(),\n"; |
| 425 | out << MethodId(method) << ",\n"; |
| 426 | out << "_aidl_in.getR(),\n"; |
| 427 | out << "_aidl_out.getR(),\n"; |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 428 | |
| 429 | std::vector<std::string> flags; |
| 430 | if (method.IsOneway()) flags.push_back("FLAG_ONEWAY"); |
| 431 | if (defined_type.IsSensitiveData()) flags.push_back("FLAG_CLEAR_BUF"); |
| 432 | out << (flags.empty() ? "0" : base::Join(flags, " | ")) << "\n"; |
| 433 | |
Steven Moreland | b72a1e7 | 2019-10-15 11:23:26 -0700 | [diff] [blame] | 434 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
| 435 | out << "| FLAG_PRIVATE_LOCAL\n"; |
| 436 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
| 437 | out << ");\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 438 | out.Dedent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 439 | |
| 440 | // If the method is not implmented in the server side but the client has |
| 441 | // provided the default implementation, call it instead of failing hard. |
| 442 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
| 443 | out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && "; |
| 444 | out << iface << "::getDefaultImpl()) {\n"; |
| 445 | out.Indent(); |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 446 | out << "_aidl_status = " << iface << "::getDefaultImpl()->" << method.GetName() << "("; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 447 | out << NdkArgList(types, method, FormatArgNameOnly) << ");\n"; |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 448 | out << "goto _aidl_status_return;\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 449 | out.Dedent(); |
| 450 | out << "}\n"; |
| 451 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 452 | StatusCheckGoto(out); |
| 453 | |
| 454 | if (!method.IsOneway()) { |
| 455 | out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n"; |
| 456 | StatusCheckGoto(out); |
| 457 | |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 458 | out << "if (!AStatus_isOk(_aidl_status.get())) goto _aidl_status_return;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 461 | if (method.GetType().GetName() != "void") { |
| 462 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 463 | ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 464 | out << ";\n"; |
| 465 | StatusCheckGoto(out); |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 466 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 467 | out << kCachedHash << " = *_aidl_return;\n"; |
| 468 | } else if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 469 | out << kCachedVersion << " = *_aidl_return;\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 470 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 471 | } |
Steven Moreland | 7c78d5d | 2018-10-15 14:21:11 -0700 | [diff] [blame] | 472 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 473 | out << "_aidl_ret_status = "; |
| 474 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)}); |
| 475 | out << ";\n"; |
| 476 | StatusCheckGoto(out); |
| 477 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 478 | |
| 479 | out << "_aidl_error:\n"; |
| 480 | out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"; |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 481 | out << "_aidl_status_return:\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 482 | if (options.GenLog()) { |
| 483 | out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::CLIENT), defined_type, |
| 484 | method, "_aidl_status", "_aidl_return", false /* isServer */, |
| 485 | true /* isNdk */); |
| 486 | } |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 487 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 488 | out << "return _aidl_status;\n"; |
| 489 | out.Dedent(); |
| 490 | out << "}\n"; |
| 491 | } |
| 492 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 493 | static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 494 | const AidlInterface& defined_type, |
| 495 | const AidlMethod& method, const Options& options) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 496 | out << "case " << MethodId(method) << ": {\n"; |
| 497 | out.Indent(); |
| 498 | for (const auto& arg : method.GetArguments()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 499 | out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg) |
| 500 | << ";\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 501 | } |
| 502 | if (method.GetType().GetName() != "void") { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 503 | out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 504 | } |
| 505 | out << "\n"; |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 506 | if (options.GenTraces()) { |
Devin Moore | f760087 | 2020-05-13 15:47:50 -0700 | [diff] [blame] | 507 | out << "ScopedTrace _aidl_trace(\"AIDL::" << Options::LanguageToString(options.TargetLanguage()) |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 508 | << "::" << ClassName(defined_type, ClassNames::INTERFACE) << "::" << method.GetName() |
| 509 | << "::server\");\n"; |
| 510 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 511 | |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 512 | for (const auto& arg : method.GetArguments()) { |
| 513 | const std::string var_name = cpp::BuildVarName(*arg); |
| 514 | |
| 515 | if (arg->IsIn()) { |
| 516 | out << "_aidl_ret_status = "; |
| 517 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name}); |
| 518 | out << ";\n"; |
| 519 | StatusCheckBreak(out); |
| 520 | } else if (arg->IsOut() && arg->GetType().IsArray()) { |
| 521 | out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n"; |
| 522 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 523 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 524 | if (options.GenLog()) { |
| 525 | out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::SERVER), method, |
| 526 | true /* isServer */, true /* isNdk */); |
| 527 | } |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 528 | out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "(" |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 529 | << NdkArgList(types, method, FormatArgForCall) << ");\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 530 | |
Jeongik Cha | eaf978e | 2019-05-04 00:32:35 +0900 | [diff] [blame] | 531 | if (options.GenLog()) { |
| 532 | out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::SERVER), defined_type, |
| 533 | method, "_aidl_status", "_aidl_return", true /* isServer */, |
| 534 | true /* isNdk */); |
| 535 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 536 | if (method.IsOneway()) { |
| 537 | // For a oneway transaction, the kernel will have already returned a result. This is for the |
| 538 | // in-process case when a oneway transaction is parceled/unparceled in the same process. |
| 539 | out << "_aidl_ret_status = STATUS_OK;\n"; |
| 540 | } else { |
| 541 | out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n"; |
| 542 | StatusCheckBreak(out); |
| 543 | |
| 544 | out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n"; |
| 545 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 546 | if (method.GetType().GetName() != "void") { |
| 547 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 548 | WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 549 | out << ";\n"; |
| 550 | StatusCheckBreak(out); |
| 551 | } |
Steven Moreland | 7c78d5d | 2018-10-15 14:21:11 -0700 | [diff] [blame] | 552 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 553 | out << "_aidl_ret_status = "; |
| 554 | WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)}); |
| 555 | out << ";\n"; |
| 556 | StatusCheckBreak(out); |
| 557 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 558 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 559 | out << "break;\n"; |
| 560 | out.Dedent(); |
| 561 | out << "}\n"; |
| 562 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 563 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 564 | void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 565 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 566 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 567 | const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER); |
Devin Moore | f760087 | 2020-05-13 15:47:50 -0700 | [diff] [blame] | 568 | if (options.GenTraces()) { |
| 569 | out << "class ScopedTrace {\n"; |
| 570 | out.Indent(); |
| 571 | out << "public:\n" |
Jiyong Park | dd57f1a | 2020-11-16 20:19:55 +0900 | [diff] [blame] | 572 | << "inline explicit ScopedTrace(const char* name) {\n" |
Devin Moore | f760087 | 2020-05-13 15:47:50 -0700 | [diff] [blame] | 573 | << "ATrace_beginSection(name);\n" |
| 574 | << "}\n" |
| 575 | << "inline ~ScopedTrace() {\n" |
| 576 | << "ATrace_endSection();\n" |
| 577 | << "}\n"; |
| 578 | out.Dedent(); |
| 579 | out << "};\n"; |
| 580 | } |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 581 | out << "static binder_status_t " |
| 582 | << "_aidl_onTransact" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 583 | << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, " |
| 584 | "AParcel* _aidl_out) {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 585 | out.Indent(); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 586 | out << "(void)_aidl_in;\n"; |
| 587 | out << "(void)_aidl_out;\n"; |
| 588 | out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n"; |
| 589 | if (!defined_type.GetMethods().empty()) { |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 590 | // we know this cast is valid because this method is only called by the ICInterface |
| 591 | // AIBinder_Class object which is associated with this class. |
| 592 | out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz |
| 593 | << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 594 | out << "switch (_aidl_code) {\n"; |
| 595 | out.Indent(); |
| 596 | for (const auto& method : defined_type.GetMethods()) { |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 597 | GenerateServerCaseDefinition(out, types, defined_type, *method, options); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 598 | } |
| 599 | out.Dedent(); |
| 600 | out << "}\n"; |
| 601 | } else { |
| 602 | out << "(void)_aidl_binder;\n"; |
| 603 | out << "(void)_aidl_code;\n"; |
| 604 | } |
| 605 | out << "return _aidl_ret_status;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 606 | out.Dedent(); |
Steven Moreland | 73d6624 | 2019-09-26 16:03:54 -0700 | [diff] [blame] | 607 | out << "}\n\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 608 | |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 609 | out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz |
| 610 | << "::" << kDescriptor << ", _aidl_onTransact);\n\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 611 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 612 | void GenerateClientSource(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 613 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 614 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 615 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 616 | out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 617 | out << clazz << "::~" << clazz << "() {}\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 618 | if (options.GenLog()) { |
Jooyung Han | 81003c3 | 2020-11-04 14:08:26 +0900 | [diff] [blame^] | 619 | out << "std::function<void(const " + clazz + "::TransactionLog&)> " << clazz << "::logFunc;\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 620 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 621 | out << "\n"; |
| 622 | for (const auto& method : defined_type.GetMethods()) { |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 623 | GenerateClientMethodDefinition(out, types, defined_type, *method, options); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 624 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 625 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 626 | void GenerateServerSource(CodeWriter& out, const AidlTypenames& types, |
| 627 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 628 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 629 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 630 | |
| 631 | out << "// Source for " << clazz << "\n"; |
| 632 | out << clazz << "::" << clazz << "() {}\n"; |
| 633 | out << clazz << "::~" << clazz << "() {}\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 634 | if (options.GenLog()) { |
Jooyung Han | 81003c3 | 2020-11-04 14:08:26 +0900 | [diff] [blame^] | 635 | out << "std::function<void(const " + clazz + "::TransactionLog&)> " << clazz << "::logFunc;\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 636 | } |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 637 | out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 638 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 639 | out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 640 | |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 641 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 642 | if (defined_type.IsVintfStability()) { |
| 643 | out << "AIBinder_markVintfStability(binder);\n"; |
| 644 | } else { |
| 645 | out << "AIBinder_markCompilationUnitStability(binder);\n"; |
| 646 | } |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 647 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 648 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 649 | out << "return ::ndk::SpAIBinder(binder);\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 650 | out.Dedent(); |
| 651 | out << "}\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 652 | |
| 653 | // Implement the meta methods |
| 654 | for (const auto& method : defined_type.GetMethods()) { |
| 655 | if (method->IsUserDefined()) { |
| 656 | continue; |
| 657 | } |
| 658 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 659 | out << NdkMethodDecl(types, *method, clazz) << " {\n"; |
| 660 | out.Indent(); |
| 661 | out << "*_aidl_return = " << iface << "::" << kVersion << ";\n"; |
| 662 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 663 | out.Dedent(); |
| 664 | out << "}\n"; |
| 665 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 666 | if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 667 | out << NdkMethodDecl(types, *method, clazz) << " {\n"; |
| 668 | out.Indent(); |
| 669 | out << "*_aidl_return = " << iface << "::" << kHash << ";\n"; |
| 670 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 671 | out.Dedent(); |
| 672 | out << "}\n"; |
| 673 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 674 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 675 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 676 | void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types, |
| 677 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 678 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 679 | const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 680 | |
| 681 | out << "// Source for " << clazz << "\n"; |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 682 | out << "const char* " << clazz << "::" << kDescriptor << " = \"" << defined_type.GetDescriptor() |
| 683 | << "\";\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 684 | out << clazz << "::" << clazz << "() {}\n"; |
| 685 | out << clazz << "::~" << clazz << "() {}\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 686 | out << "\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 687 | GenerateConstantDefinitions(out, defined_type); |
| 688 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 689 | |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 690 | out << "std::shared_ptr<" << clazz << "> " << clazz |
| 691 | << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 692 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 693 | out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n"; |
| 694 | out << "std::shared_ptr<::ndk::ICInterface> interface = " |
| 695 | "::ndk::ICInterface::asInterface(binder.get());\n"; |
| 696 | out << "if (interface) {\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 697 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 698 | out << "return std::static_pointer_cast<" << clazz << ">(interface);\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 699 | out.Dedent(); |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 700 | out << "}\n"; |
Steven Moreland | c09d34c | 2020-02-13 09:42:49 -0800 | [diff] [blame] | 701 | out << "return ::ndk::SharedRefBase::make<" << bp_clazz << ">(binder);\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 702 | out.Dedent(); |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 703 | out << "}\n\n"; |
| 704 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 705 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<" |
| 706 | << clazz << ">& instance) {\n"; |
| 707 | out.Indent(); |
| 708 | out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : " |
| 709 | "nullptr);\n"; |
| 710 | out.Dedent(); |
| 711 | out << "}\n"; |
| 712 | |
| 713 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<" |
| 714 | << clazz << ">* instance) {\n"; |
| 715 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 716 | out << "::ndk::SpAIBinder binder;\n"; |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 717 | out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 718 | out << "if (status != STATUS_OK) return status;\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 719 | out << "*instance = " << clazz << "::fromBinder(binder);\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 720 | out << "return STATUS_OK;\n"; |
| 721 | out.Dedent(); |
| 722 | out << "}\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 723 | |
| 724 | // defintion for static member setDefaultImpl |
Jiyong Park | dd57f1a | 2020-11-16 20:19:55 +0900 | [diff] [blame] | 725 | out << "bool " << clazz << "::setDefaultImpl(const std::shared_ptr<" << clazz << ">& impl) {\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 726 | out.Indent(); |
Jooyung Han | 4a04466 | 2020-05-13 17:17:07 +0900 | [diff] [blame] | 727 | out << "// Only one user of this interface can use this function\n"; |
| 728 | out << "// at a time. This is a heuristic to detect if two different\n"; |
| 729 | out << "// users in the same process use this function.\n"; |
| 730 | out << "assert(!" << clazz << "::default_impl);\n"; |
| 731 | out << "if (impl) {\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 732 | out.Indent(); |
| 733 | out << clazz << "::default_impl = impl;\n"; |
| 734 | out << "return true;\n"; |
| 735 | out.Dedent(); |
| 736 | out << "}\n"; |
| 737 | out << "return false;\n"; |
| 738 | out.Dedent(); |
| 739 | out << "}\n"; |
| 740 | |
| 741 | // definition for static member getDefaultImpl |
| 742 | out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n"; |
| 743 | out.Indent(); |
| 744 | out << "return " << clazz << "::default_impl;\n"; |
| 745 | out.Dedent(); |
| 746 | out << "}\n"; |
| 747 | |
| 748 | // definition for the static field default_impl |
| 749 | out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n"; |
| 750 | |
| 751 | // default implementation for the <Name>Default class members |
| 752 | const std::string defaultClazz = clazz + "Default"; |
| 753 | for (const auto& method : defined_type.GetMethods()) { |
| 754 | if (method->IsUserDefined()) { |
| 755 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 756 | << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n"; |
| 757 | out.Indent(); |
| 758 | out << "::ndk::ScopedAStatus _aidl_status;\n"; |
| 759 | out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n"; |
| 760 | out << "return _aidl_status;\n"; |
| 761 | out.Dedent(); |
| 762 | out << "}\n"; |
| 763 | } else { |
| 764 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 765 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 766 | << "int32_t* _aidl_return) {\n"; |
| 767 | out.Indent(); |
| 768 | out << "*_aidl_return = 0;\n"; |
| 769 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 770 | out.Dedent(); |
| 771 | out << "}\n"; |
| 772 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 773 | if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 774 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 775 | << "std::string* _aidl_return) {\n"; |
| 776 | out.Indent(); |
| 777 | out << "*_aidl_return = \"\";\n"; |
| 778 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 779 | out.Dedent(); |
| 780 | out << "}\n"; |
| 781 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n"; |
| 786 | out.Indent(); |
| 787 | out << "return ::ndk::SpAIBinder();\n"; |
| 788 | out.Dedent(); |
| 789 | out << "}\n"; |
| 790 | |
| 791 | out << "bool " << defaultClazz << "::isRemote() {\n"; |
| 792 | out.Indent(); |
| 793 | out << "return false;\n"; |
| 794 | out.Dedent(); |
| 795 | out << "}\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 796 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 797 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 798 | void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 799 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 800 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 801 | |
| 802 | out << "#pragma once\n\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 803 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 804 | << "\"\n"; |
| 805 | out << "\n"; |
| 806 | out << "#include <android/binder_ibinder.h>\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 807 | if (options.GenLog()) { |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 808 | out << "#include <functional>\n"; |
| 809 | out << "#include <chrono>\n"; |
| 810 | out << "#include <sstream>\n"; |
| 811 | } |
Devin Moore | 7d5a454 | 2020-04-29 15:37:25 -0700 | [diff] [blame] | 812 | if (options.GenTraces()) { |
| 813 | out << "#include <android/trace.h>\n"; |
| 814 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 815 | out << "\n"; |
| 816 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 817 | out << "class " << clazz << " : public ::ndk::BpCInterface<" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 818 | << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n"; |
| 819 | out << "public:\n"; |
| 820 | out.Indent(); |
Jiyong Park | dd57f1a | 2020-11-16 20:19:55 +0900 | [diff] [blame] | 821 | out << "explicit " << clazz << "(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 822 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 823 | out << "\n"; |
| 824 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 825 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 826 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 827 | |
| 828 | if (options.Version() > 0) { |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 829 | out << "int32_t " << kCachedVersion << " = -1;\n"; |
| 830 | } |
| 831 | |
| 832 | if (!options.Hash().empty()) { |
| 833 | out << "std::string " << kCachedHash << " = \"-1\";\n"; |
| 834 | out << "std::mutex " << kCachedHashMutex << ";\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 835 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 836 | if (options.GenLog()) { |
Jooyung Han | 81003c3 | 2020-11-04 14:08:26 +0900 | [diff] [blame^] | 837 | out << cpp::kTransactionLogStruct; |
| 838 | out << "static std::function<void(const TransactionLog&)> logFunc;\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 839 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 840 | out.Dedent(); |
| 841 | out << "};\n"; |
| 842 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 843 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 844 | void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types, |
| 845 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 846 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 847 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 848 | |
| 849 | out << "#pragma once\n\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 850 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 851 | << "\"\n"; |
| 852 | out << "\n"; |
| 853 | out << "#include <android/binder_ibinder.h>\n"; |
| 854 | out << "\n"; |
| 855 | EnterNdkNamespace(out, defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 856 | out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 857 | out << "public:\n"; |
| 858 | out.Indent(); |
| 859 | out << clazz << "();\n"; |
| 860 | out << "virtual ~" << clazz << "();\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 861 | |
| 862 | // Declare the meta methods |
| 863 | for (const auto& method : defined_type.GetMethods()) { |
| 864 | if (method->IsUserDefined()) { |
| 865 | continue; |
| 866 | } |
| 867 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 868 | out << NdkMethodDecl(types, *method) << " final override;\n"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 869 | } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 870 | out << NdkMethodDecl(types, *method) << " final override;\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 871 | } else { |
| 872 | AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented."; |
| 873 | } |
| 874 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 875 | if (options.GenLog()) { |
Jooyung Han | 81003c3 | 2020-11-04 14:08:26 +0900 | [diff] [blame^] | 876 | out << cpp::kTransactionLogStruct; |
| 877 | out << "static std::function<void(const TransactionLog&)> logFunc;\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 878 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 879 | out.Dedent(); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 880 | out << "protected:\n"; |
| 881 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 882 | out << "::ndk::SpAIBinder createBinder() override;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 883 | out.Dedent(); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 884 | out << "private:\n"; |
| 885 | out.Indent(); |
| 886 | out.Dedent(); |
| 887 | out << "};\n"; |
| 888 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 889 | } |
| 890 | void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 891 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 892 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 893 | |
| 894 | out << "#pragma once\n\n"; |
| 895 | out << "#include <android/binder_interface_utils.h>\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 896 | if (options.GenLog()) { |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 897 | out << "#include <functional>\n"; |
| 898 | out << "#include <chrono>\n"; |
| 899 | out << "#include <sstream>\n"; |
| 900 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 901 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 902 | |
| 903 | GenerateHeaderIncludes(out, types, defined_type); |
| 904 | out << "\n"; |
| 905 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 906 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 907 | out << "class " << clazz << " : public ::ndk::ICInterface {\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 908 | out << "public:\n"; |
| 909 | out.Indent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 910 | out << "static const char* " << kDescriptor << ";\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 911 | out << clazz << "();\n"; |
| 912 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 913 | out << "\n"; |
Steven Moreland | e689da2 | 2020-11-10 02:06:30 +0000 | [diff] [blame] | 914 | GenerateConstantDeclarations(out, types, defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 915 | if (options.Version() > 0) { |
| 916 | out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version()) |
| 917 | << ";\n"; |
| 918 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 919 | if (!options.Hash().empty()) { |
| 920 | out << "static inline const std::string " << kHash << " = \"" << options.Hash() << "\";\n"; |
| 921 | } |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 922 | out << "\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 923 | out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 924 | out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz |
| 925 | << ">& instance);"; |
Jiyong Park | 1b88cce | 2018-11-19 19:53:44 +0900 | [diff] [blame] | 926 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 927 | out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz |
| 928 | << ">* instance);"; |
| 929 | out << "\n"; |
Jiyong Park | dd57f1a | 2020-11-16 20:19:55 +0900 | [diff] [blame] | 930 | out << "static bool setDefaultImpl(const std::shared_ptr<" << clazz << ">& impl);"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 931 | out << "\n"; |
| 932 | out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();"; |
| 933 | out << "\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 934 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 935 | out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 936 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 937 | out.Dedent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 938 | out << "private:\n"; |
| 939 | out.Indent(); |
| 940 | out << "static std::shared_ptr<" << clazz << "> default_impl;\n"; |
| 941 | out.Dedent(); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 942 | out << "};\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 943 | |
| 944 | const std::string defaultClazz = clazz + "Default"; |
| 945 | |
| 946 | out << "class " << defaultClazz << " : public " << clazz << " {\n"; |
| 947 | out << "public:\n"; |
| 948 | out.Indent(); |
| 949 | for (const auto& method : defined_type.GetMethods()) { |
| 950 | if (method->IsUserDefined()) { |
| 951 | out << NdkMethodDecl(types, *method) << " override;\n"; |
| 952 | } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 953 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 954 | } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 955 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | out << "::ndk::SpAIBinder asBinder() override;\n"; |
| 959 | out << "bool isRemote() override;\n"; |
| 960 | out.Dedent(); |
| 961 | out << "};\n"; |
| 962 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 963 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 964 | } |
| 965 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 966 | const AidlStructuredParcelable& defined_type, |
| 967 | const Options& /*options*/) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 968 | const std::string clazz = ClassName(defined_type, ClassNames::RAW); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 969 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 970 | out << "#pragma once\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 971 | out << "#include <android/binder_interface_utils.h>\n"; |
Jeongik Cha | 93741c4 | 2020-09-22 11:57:28 +0900 | [diff] [blame] | 972 | out << "#include <android/binder_parcelable_utils.h>\n"; |
Jooyung Han | 74b1dd4 | 2020-11-01 22:17:16 +0900 | [diff] [blame] | 973 | |
| 974 | // used by toString() |
| 975 | out << "#include <codecvt>\n"; |
| 976 | out << "#include <locale>\n"; |
| 977 | out << "#include <sstream>\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 978 | |
| 979 | GenerateHeaderIncludes(out, types, defined_type); |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 980 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 981 | EnterNdkNamespace(out, defined_type); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 982 | out << cpp::TemplateDecl(defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 983 | out << "class " << clazz << " {\n"; |
| 984 | out << "public:\n"; |
| 985 | out.Indent(); |
Devin Moore | 369fdc0 | 2020-09-11 14:21:34 -0700 | [diff] [blame] | 986 | if (defined_type.IsFixedSize()) { |
| 987 | out << "typedef std::true_type fixed_size;\n"; |
| 988 | } else { |
| 989 | out << "typedef std::false_type fixed_size;\n"; |
| 990 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 991 | out << "static const char* descriptor;\n"; |
| 992 | out << "\n"; |
| 993 | for (const auto& variable : defined_type.GetFields()) { |
| 994 | out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName(); |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 995 | if (variable->GetType().GetName() == "ParcelableHolder") { |
| 996 | out << "{::ndk::" << (defined_type.IsVintfStability() ? "STABILITY_VINTF" : "STABILITY_LOCAL") |
| 997 | << "}"; |
| 998 | } |
Devin Moore | 83e7698 | 2020-09-30 09:32:35 -0700 | [diff] [blame] | 999 | if (defined_type.IsFixedSize()) { |
| 1000 | int alignment = NdkAlignmentOf(types, variable->GetType()); |
| 1001 | if (alignment > 0) { |
| 1002 | out << " __attribute__((aligned (" << std::to_string(alignment) << ")))"; |
| 1003 | } |
| 1004 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1005 | if (variable->GetDefaultValue()) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1006 | out << " = " << variable->ValueString(ConstantValueDecorator); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1007 | } |
| 1008 | out << ";\n"; |
| 1009 | } |
| 1010 | out << "\n"; |
| 1011 | out << "binder_status_t readFromParcel(const AParcel* parcel);\n"; |
| 1012 | out << "binder_status_t writeToParcel(AParcel* parcel) const;\n"; |
Jeongik Cha | 92d33d0 | 2020-05-14 00:53:57 +0900 | [diff] [blame] | 1013 | |
Jeongik Cha | 93741c4 | 2020-09-22 11:57:28 +0900 | [diff] [blame] | 1014 | out << "static const ::ndk::parcelable_stability_t _aidl_stability = ::ndk::" |
| 1015 | << (defined_type.IsVintfStability() ? "STABILITY_VINTF" : "STABILITY_LOCAL") << ";\n"; |
Jooyung Han | 74b1dd4 | 2020-11-01 22:17:16 +0900 | [diff] [blame] | 1016 | |
| 1017 | cpp::GenerateToString(out, defined_type); |
| 1018 | |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1019 | out.Dedent(); |
| 1020 | out << "};\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1021 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1022 | } |
| 1023 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1024 | const AidlStructuredParcelable& defined_type, |
| 1025 | const Options& /*options*/) { |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 1026 | std::string clazz = ClassName(defined_type, ClassNames::RAW); |
| 1027 | if (defined_type.IsGeneric()) { |
| 1028 | std::vector<std::string> template_params; |
| 1029 | for (const auto& parameter : defined_type.GetTypeParameters()) { |
| 1030 | template_params.push_back(parameter); |
| 1031 | } |
| 1032 | clazz += base::StringPrintf("<%s>", base::Join(template_params, ", ").c_str()); |
| 1033 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1034 | |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 1035 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1036 | << "\"\n"; |
| 1037 | out << "\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1038 | GenerateSourceIncludes(out, types, defined_type); |
| 1039 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1040 | EnterNdkNamespace(out, defined_type); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 1041 | out << cpp::TemplateDecl(defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 1042 | out << "const char* " << clazz << "::" << kDescriptor << " = \"" |
| 1043 | << defined_type.GetCanonicalName() << "\";\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1044 | out << "\n"; |
| 1045 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 1046 | out << cpp::TemplateDecl(defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1047 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n"; |
| 1048 | out.Indent(); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1049 | out << "int32_t _aidl_parcelable_size;\n"; |
Steven Moreland | 4348f9a | 2019-12-16 16:33:01 -0800 | [diff] [blame] | 1050 | out << "int32_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n"; |
| 1051 | out << "binder_status_t _aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n"; |
Jeongik Cha | 8b32998 | 2020-09-01 20:59:36 +0900 | [diff] [blame] | 1052 | out << "if (_aidl_start_pos > INT32_MAX - _aidl_parcelable_size) return STATUS_BAD_VALUE;\n"; |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1053 | out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n"; |
| 1054 | StatusCheckReturn(out); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1055 | |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1056 | for (const auto& variable : defined_type.GetFields()) { |
| 1057 | out << "_aidl_ret_status = "; |
| 1058 | ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()}); |
| 1059 | out << ";\n"; |
| 1060 | StatusCheckReturn(out); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1061 | out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n" |
| 1062 | << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n" |
| 1063 | << " return _aidl_ret_status;\n" |
| 1064 | << "}\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1065 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1066 | out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n" |
| 1067 | << "return _aidl_ret_status;\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1068 | out.Dedent(); |
| 1069 | out << "}\n"; |
| 1070 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 1071 | out << cpp::TemplateDecl(defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1072 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n"; |
| 1073 | out.Indent(); |
| 1074 | out << "binder_status_t _aidl_ret_status;\n"; |
| 1075 | |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1076 | out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n"; |
| 1077 | out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n"; |
| 1078 | StatusCheckReturn(out); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1079 | |
| 1080 | for (const auto& variable : defined_type.GetFields()) { |
| 1081 | out << "_aidl_ret_status = "; |
| 1082 | WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()}); |
| 1083 | out << ";\n"; |
| 1084 | StatusCheckReturn(out); |
| 1085 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 1086 | out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n"; |
| 1087 | out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n"; |
| 1088 | out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n"; |
| 1089 | out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n"; |
| 1090 | |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 1091 | out << "return _aidl_ret_status;\n"; |
| 1092 | out.Dedent(); |
| 1093 | out << "}\n"; |
| 1094 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1095 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1096 | } |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1097 | |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 1098 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
| 1099 | const AidlUnionDecl& defined_type, const Options& /*options*/) { |
| 1100 | const std::string clazz = ClassName(defined_type, ClassNames::RAW); |
| 1101 | cpp::UnionWriter uw{defined_type, types, |
| 1102 | [&](const AidlTypeSpecifier& type, const AidlTypenames& types) { |
| 1103 | return NdkNameOf(types, type, StorageMode::STACK); |
| 1104 | }, |
| 1105 | &ConstantValueDecorator}; |
| 1106 | |
| 1107 | out << "#pragma once\n"; |
| 1108 | out << "#include <android/binder_interface_utils.h>\n"; |
| 1109 | out << "#include <android/binder_parcelable_utils.h>\n"; |
Jooyung Han | 74b1dd4 | 2020-11-01 22:17:16 +0900 | [diff] [blame] | 1110 | |
| 1111 | // used by toString() |
| 1112 | out << "#include <codecvt>\n"; |
| 1113 | out << "#include <locale>\n"; |
| 1114 | out << "#include <sstream>\n"; |
| 1115 | |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 1116 | out << "\n"; |
| 1117 | |
| 1118 | for (const auto& header : cpp::UnionWriter::headers) { |
| 1119 | out << "#include <" << header << ">\n"; |
| 1120 | } |
| 1121 | GenerateHeaderIncludes(out, types, defined_type); |
| 1122 | |
| 1123 | EnterNdkNamespace(out, defined_type); |
| 1124 | out << cpp::TemplateDecl(defined_type); |
| 1125 | out << "class " << clazz << " {\n"; |
| 1126 | out << "public:\n"; |
| 1127 | out.Indent(); |
| 1128 | if (defined_type.IsFixedSize()) { |
| 1129 | out << "typedef std::true_type fixed_size;\n"; |
| 1130 | } else { |
| 1131 | out << "typedef std::false_type fixed_size;\n"; |
| 1132 | } |
| 1133 | out << "static const char* descriptor;\n"; |
| 1134 | out << "\n"; |
| 1135 | uw.PublicFields(out); |
| 1136 | |
| 1137 | out << "binder_status_t readFromParcel(const AParcel* _parcel);\n"; |
| 1138 | out << "binder_status_t writeToParcel(AParcel* _parcel) const;\n"; |
| 1139 | |
| 1140 | out << "static const ::ndk::parcelable_stability_t _aidl_stability = ::ndk::" |
| 1141 | << (defined_type.IsVintfStability() ? "STABILITY_VINTF" : "STABILITY_LOCAL") << ";\n"; |
Jooyung Han | 74b1dd4 | 2020-11-01 22:17:16 +0900 | [diff] [blame] | 1142 | cpp::GenerateToString(out, defined_type); |
Jooyung Han | 6beac04 | 2020-10-17 21:59:43 +0900 | [diff] [blame] | 1143 | out.Dedent(); |
| 1144 | out << "private:\n"; |
| 1145 | out.Indent(); |
| 1146 | uw.PrivateFields(out); |
| 1147 | out.Dedent(); |
| 1148 | out << "};\n"; |
| 1149 | LeaveNdkNamespace(out, defined_type); |
| 1150 | } |
| 1151 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
| 1152 | const AidlUnionDecl& defined_type, const Options& /*options*/) { |
| 1153 | std::string clazz = ClassName(defined_type, ClassNames::RAW); |
| 1154 | if (defined_type.IsGeneric()) { |
| 1155 | std::vector<std::string> template_params; |
| 1156 | for (const auto& parameter : defined_type.GetTypeParameters()) { |
| 1157 | template_params.push_back(parameter); |
| 1158 | } |
| 1159 | clazz += base::StringPrintf("<%s>", base::Join(template_params, ", ").c_str()); |
| 1160 | } |
| 1161 | |
| 1162 | cpp::UnionWriter uw{defined_type, types, |
| 1163 | [&](const AidlTypeSpecifier& type, const AidlTypenames& types) { |
| 1164 | return NdkNameOf(types, type, StorageMode::STACK); |
| 1165 | }, |
| 1166 | &ConstantValueDecorator}; |
| 1167 | cpp::ParcelWriterContext ctx{ |
| 1168 | .status_type = "binder_status_t", |
| 1169 | .status_ok = "STATUS_OK", |
| 1170 | .status_bad = "STATUS_BAD_VALUE", |
| 1171 | .read_func = |
| 1172 | [&](CodeWriter& out, const std::string& var, const AidlTypeSpecifier& type) { |
| 1173 | ReadFromParcelFor({out, types, type, "_parcel", "&" + var}); |
| 1174 | }, |
| 1175 | .write_func = |
| 1176 | [&](CodeWriter& out, const std::string& value, const AidlTypeSpecifier& type) { |
| 1177 | WriteToParcelFor({out, types, type, "_parcel", value}); |
| 1178 | }, |
| 1179 | }; |
| 1180 | |
| 1181 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
| 1182 | << "\"\n"; |
| 1183 | out << "\n"; |
| 1184 | GenerateSourceIncludes(out, types, defined_type); |
| 1185 | out << "\n"; |
| 1186 | EnterNdkNamespace(out, defined_type); |
| 1187 | out << cpp::TemplateDecl(defined_type); |
| 1188 | out << "const char* " << clazz << "::" << kDescriptor << " = \"" |
| 1189 | << defined_type.GetCanonicalName() << "\";\n"; |
| 1190 | out << "\n"; |
| 1191 | |
| 1192 | out << cpp::TemplateDecl(defined_type); |
| 1193 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* _parcel) {\n"; |
| 1194 | out.Indent(); |
| 1195 | uw.ReadFromParcel(out, ctx); |
| 1196 | out.Dedent(); |
| 1197 | out << "}\n"; |
| 1198 | |
| 1199 | out << cpp::TemplateDecl(defined_type); |
| 1200 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* _parcel) const {\n"; |
| 1201 | out.Indent(); |
| 1202 | uw.WriteToParcel(out, ctx); |
| 1203 | out.Dedent(); |
| 1204 | out << "}\n"; |
| 1205 | out << "\n"; |
| 1206 | LeaveNdkNamespace(out, defined_type); |
| 1207 | } |
| 1208 | |
Daniel Norman | 455d95c | 2019-11-19 09:55:39 -0800 | [diff] [blame] | 1209 | std::string GenerateEnumToString(const AidlTypenames& typenames, |
| 1210 | const AidlEnumDeclaration& enum_decl) { |
| 1211 | std::ostringstream code; |
| 1212 | code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n"; |
| 1213 | code << " switch(val) {\n"; |
| 1214 | std::set<std::string> unique_cases; |
| 1215 | for (const auto& enumerator : enum_decl.GetEnumerators()) { |
| 1216 | std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator); |
| 1217 | // Only add a case if its value has not yet been used in the switch |
| 1218 | // statement. C++ does not allow multiple cases with the same value, but |
| 1219 | // enums does allow this. In this scenario, the first declared |
| 1220 | // enumerator with the given value is printed. |
| 1221 | if (unique_cases.count(c) == 0) { |
| 1222 | unique_cases.insert(c); |
| 1223 | code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n"; |
| 1224 | code << " return \"" << enumerator->GetName() << "\";\n"; |
| 1225 | } |
| 1226 | } |
| 1227 | code << " default:\n"; |
| 1228 | code << " return std::to_string(static_cast<" |
| 1229 | << NdkNameOf(typenames, enum_decl.GetBackingType(), StorageMode::STACK) << ">(val));\n"; |
| 1230 | code << " }\n"; |
| 1231 | code << "}\n"; |
| 1232 | return code.str(); |
| 1233 | } |
| 1234 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1235 | void GenerateEnumHeader(CodeWriter& out, const AidlTypenames& types, |
| 1236 | const AidlEnumDeclaration& enum_decl, const Options& /*options*/) { |
| 1237 | out << "#pragma once\n"; |
| 1238 | out << "\n"; |
| 1239 | |
| 1240 | GenerateHeaderIncludes(out, types, enum_decl); |
Jooyung Han | 7a9aceb | 2019-12-17 14:18:15 +0000 | [diff] [blame] | 1241 | // enum specific headers |
| 1242 | out << "#include <array>\n"; |
| 1243 | out << "#include <android/binder_enums.h>\n"; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1244 | |
| 1245 | EnterNdkNamespace(out, enum_decl); |
| 1246 | out << "enum class " << enum_decl.GetName() << " : " |
| 1247 | << NdkNameOf(types, enum_decl.GetBackingType(), StorageMode::STACK) << " {\n"; |
| 1248 | out.Indent(); |
| 1249 | for (const auto& enumerator : enum_decl.GetEnumerators()) { |
| 1250 | out << enumerator->GetName() << " = " |
| 1251 | << enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator) << ",\n"; |
| 1252 | } |
| 1253 | out.Dedent(); |
| 1254 | out << "};\n"; |
Daniel Norman | 455d95c | 2019-11-19 09:55:39 -0800 | [diff] [blame] | 1255 | out << "\n"; |
| 1256 | out << GenerateEnumToString(types, enum_decl); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1257 | LeaveNdkNamespace(out, enum_decl); |
Jooyung Han | 7a9aceb | 2019-12-17 14:18:15 +0000 | [diff] [blame] | 1258 | |
| 1259 | out << "namespace ndk {\n"; |
| 1260 | out << "namespace internal {\n"; |
| 1261 | out << cpp::GenerateEnumValues(enum_decl, {"aidl"}); |
| 1262 | out << "} // namespace internal\n"; |
| 1263 | out << "} // namespace android\n"; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1266 | } // namespace internals |
| 1267 | } // namespace ndk |
| 1268 | } // namespace aidl |
| 1269 | } // namespace android |