Steven Moreland | 860b194 | 2018-08-16 14:59:28 -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 "aidl_to_cpp.h" |
| 18 | #include "aidl_language.h" |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 19 | #include "logging.h" |
| 20 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| 23 | |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 24 | #include <functional> |
| 25 | #include <unordered_map> |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 26 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 27 | using android::base::Join; |
| 28 | using android::base::Split; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 29 | using android::base::StringPrintf; |
Jeongik Cha | b5d962f | 2018-11-17 09:12:28 +0900 | [diff] [blame] | 30 | using std::ostringstream; |
| 31 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace aidl { |
| 34 | namespace cpp { |
| 35 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 36 | namespace { |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 37 | std::string RawParcelMethod(const AidlTypeSpecifier& type, const AidlTypenames& typenames, |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 38 | bool readMethod) { |
| 39 | static map<string, string> kBuiltin = { |
| 40 | {"byte", "Byte"}, |
| 41 | {"boolean", "Bool"}, |
| 42 | {"char", "Char"}, |
| 43 | {"double", "Double"}, |
| 44 | {"FileDescriptor", "UniqueFileDescriptor"}, |
| 45 | {"float", "Float"}, |
| 46 | {"IBinder", "StrongBinder"}, |
| 47 | {"int", "Int32"}, |
| 48 | {"long", "Int64"}, |
| 49 | {"ParcelFileDescriptor", "Parcelable"}, |
| 50 | {"String", "String16"}, |
| 51 | }; |
| 52 | |
| 53 | static map<string, string> kBuiltinVector = { |
| 54 | {"FileDescriptor", "UniqueFileDescriptorVector"}, |
| 55 | {"double", "DoubleVector"}, |
| 56 | {"char", "CharVector"}, |
| 57 | {"boolean", "BoolVector"}, |
| 58 | {"byte", "ByteVector"}, |
| 59 | {"float", "FloatVector"}, |
| 60 | {"IBinder", "StrongBinderVector"}, |
| 61 | {"String", "String16Vector"}, |
| 62 | {"int", "Int32Vector"}, |
| 63 | {"long", "Int64Vector"}, |
| 64 | {"ParcelFileDescriptor", "ParcelableVector"}, |
| 65 | }; |
| 66 | |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 67 | const bool nullable = type.IsNullable(); |
| 68 | const bool isVector = type.IsArray() || typenames.IsList(type); |
| 69 | const bool utf8 = type.IsUtf8InCpp(); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 70 | |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 71 | if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) { |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 72 | if (isVector) { |
| 73 | return "EnumVector"; |
| 74 | } else { |
| 75 | return RawParcelMethod(enum_decl->GetBackingType(), typenames, readMethod); |
| 76 | } |
| 77 | } |
| 78 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 79 | if (isVector) { |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 80 | string element_name; |
| 81 | if (typenames.IsList(type)) { |
| 82 | CHECK(type.GetTypeParameters().size() == 1); |
| 83 | element_name = type.GetTypeParameters().at(0)->GetName(); |
| 84 | } else { |
| 85 | element_name = type.GetName(); |
| 86 | } |
| 87 | if (kBuiltinVector.find(element_name) != kBuiltinVector.end()) { |
| 88 | CHECK(AidlTypenames::IsBuiltinTypename(element_name)); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 89 | if (utf8) { |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 90 | CHECK(element_name == "String"); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 91 | return readMethod ? "Utf8VectorFromUtf16Vector" : "Utf8VectorAsUtf16Vector"; |
| 92 | } |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 93 | return kBuiltinVector[element_name]; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 94 | } |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 95 | CHECK(!typenames.IsList(type)); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 96 | } else { |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 97 | const string& type_name = type.GetName(); |
| 98 | if (kBuiltin.find(type_name) != kBuiltin.end()) { |
| 99 | CHECK(AidlTypenames::IsBuiltinTypename(type_name)); |
| 100 | if (type_name == "IBinder" && nullable && readMethod) { |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 101 | return "NullableStrongBinder"; |
| 102 | } |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 103 | if (type_name == "ParcelFileDescriptor" && nullable && !readMethod) { |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 104 | return "NullableParcelable"; |
| 105 | } |
| 106 | if (utf8) { |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 107 | CHECK(type_name == "String"); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 108 | return readMethod ? "Utf8FromUtf16" : "Utf8AsUtf16"; |
| 109 | } |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 110 | return kBuiltin[type_name]; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 111 | } |
| 112 | } |
Devin Moore | 21b2677 | 2020-08-26 16:37:56 -0700 | [diff] [blame] | 113 | CHECK(!AidlTypenames::IsBuiltinTypename(type.GetName())); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 114 | auto definedType = typenames.TryGetDefinedType(type.GetName()); |
| 115 | if (definedType != nullptr && definedType->AsInterface() != nullptr) { |
| 116 | if (isVector) { |
| 117 | return "StrongBinderVector"; |
| 118 | } |
| 119 | if (nullable && readMethod) { |
| 120 | return "NullableStrongBinder"; |
| 121 | } |
| 122 | return "StrongBinder"; |
| 123 | } |
| 124 | |
| 125 | // The type must be either primitive or interface or parcelable, |
| 126 | // so it cannot be nullptr. |
| 127 | CHECK(definedType != nullptr) << type.GetName() << " is not found."; |
| 128 | |
| 129 | // Parcelable |
| 130 | if (isVector) { |
| 131 | return "ParcelableVector"; |
| 132 | } |
| 133 | if (nullable && !readMethod) { |
| 134 | return "NullableParcelable"; |
| 135 | } |
| 136 | return "Parcelable"; |
| 137 | } |
| 138 | |
| 139 | std::string GetRawCppName(const AidlTypeSpecifier& type) { |
| 140 | return "::" + Join(type.GetSplitName(), "::"); |
| 141 | } |
| 142 | |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 143 | std::string WrapIfNullable(const std::string type_str, const AidlTypeSpecifier& raw_type, |
| 144 | const AidlTypenames& typenames) { |
Devin Moore | f12cc11 | 2020-08-26 16:25:00 -0700 | [diff] [blame] | 145 | const auto& type = typenames.IsList(raw_type) ? (*raw_type.GetTypeParameters().at(0)) : raw_type; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 146 | |
| 147 | if (raw_type.IsNullable() && !AidlTypenames::IsPrimitiveTypename(type.GetName()) && |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 148 | type.GetName() != "IBinder" && typenames.GetEnumDeclaration(type) == nullptr) { |
Jooyung Han | 4312246 | 2020-01-23 13:03:10 +0900 | [diff] [blame] | 149 | return "::std::optional<" + type_str + ">"; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 150 | } |
| 151 | return type_str; |
| 152 | } |
| 153 | |
| 154 | std::string GetCppName(const AidlTypeSpecifier& raw_type, const AidlTypenames& typenames) { |
| 155 | // map from AIDL built-in type name to the corresponding Cpp type name |
| 156 | static map<string, string> m = { |
| 157 | {"boolean", "bool"}, |
| 158 | {"byte", "int8_t"}, |
| 159 | {"char", "char16_t"}, |
| 160 | {"double", "double"}, |
| 161 | {"FileDescriptor", "::android::base::unique_fd"}, |
| 162 | {"float", "float"}, |
| 163 | {"IBinder", "::android::sp<::android::IBinder>"}, |
| 164 | {"int", "int32_t"}, |
| 165 | {"long", "int64_t"}, |
| 166 | {"ParcelFileDescriptor", "::android::os::ParcelFileDescriptor"}, |
| 167 | {"String", "::android::String16"}, |
| 168 | {"void", "void"}, |
| 169 | }; |
Devin Moore | f12cc11 | 2020-08-26 16:25:00 -0700 | [diff] [blame] | 170 | CHECK(!typenames.IsList(raw_type) || raw_type.GetTypeParameters().size() == 1); |
| 171 | const auto& type = typenames.IsList(raw_type) ? (*raw_type.GetTypeParameters().at(0)) : raw_type; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 172 | const string& aidl_name = type.GetName(); |
| 173 | if (m.find(aidl_name) != m.end()) { |
| 174 | CHECK(AidlTypenames::IsBuiltinTypename(aidl_name)); |
| 175 | if (aidl_name == "byte" && type.IsArray()) { |
| 176 | return "uint8_t"; |
| 177 | } else if (raw_type.IsUtf8InCpp()) { |
| 178 | CHECK(aidl_name == "String"); |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 179 | return WrapIfNullable("::std::string", raw_type, typenames); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 180 | } |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 181 | return WrapIfNullable(m[aidl_name], raw_type, typenames); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 182 | } |
| 183 | auto definedType = typenames.TryGetDefinedType(type.GetName()); |
| 184 | if (definedType != nullptr && definedType->AsInterface() != nullptr) { |
| 185 | return "::android::sp<" + GetRawCppName(type) + ">"; |
| 186 | } |
| 187 | |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 188 | return WrapIfNullable(GetRawCppName(type), raw_type, typenames); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 189 | } |
| 190 | } // namespace |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 191 | std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 192 | if (type.IsArray()) { |
| 193 | return raw_value; |
| 194 | } |
| 195 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 196 | if (type.GetName() == "long") { |
| 197 | return raw_value + "L"; |
| 198 | } |
| 199 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 200 | if (type.GetName() == "String" && !type.IsUtf8InCpp()) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 201 | return "::android::String16(" + raw_value + ")"; |
| 202 | } |
| 203 | |
| 204 | return raw_value; |
| 205 | }; |
| 206 | |
Jeongik Cha | b5d962f | 2018-11-17 09:12:28 +0900 | [diff] [blame] | 207 | std::string GetTransactionIdFor(const AidlMethod& method) { |
| 208 | ostringstream output; |
| 209 | |
Jeongik Cha | f1470e2 | 2019-05-20 18:45:05 +0900 | [diff] [blame] | 210 | output << "::android::IBinder::FIRST_CALL_TRANSACTION + "; |
Jeongik Cha | b5d962f | 2018-11-17 09:12:28 +0900 | [diff] [blame] | 211 | output << method.GetId() << " /* " << method.GetName() << " */"; |
| 212 | return output.str(); |
| 213 | } |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 214 | |
| 215 | std::string CppNameOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) { |
Devin Moore | f12cc11 | 2020-08-26 16:25:00 -0700 | [diff] [blame] | 216 | if (type.IsArray() || typenames.IsList(type)) { |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 217 | std::string cpp_name = GetCppName(type, typenames); |
| 218 | if (type.IsNullable()) { |
Jooyung Han | 4312246 | 2020-01-23 13:03:10 +0900 | [diff] [blame] | 219 | return "::std::optional<::std::vector<" + cpp_name + ">>"; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 220 | } |
| 221 | return "::std::vector<" + cpp_name + ">"; |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 222 | } else if (type.IsGeneric()) { |
| 223 | std::vector<std::string> type_params; |
| 224 | for (const auto& parameter : type.GetTypeParameters()) { |
| 225 | type_params.push_back(CppNameOf(*parameter, typenames)); |
| 226 | } |
| 227 | return StringPrintf("%s<%s>", GetCppName(type, typenames).c_str(), |
| 228 | base::Join(type_params, ", ").c_str()); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 229 | } |
| 230 | return GetCppName(type, typenames); |
| 231 | } |
| 232 | |
Jiyong Park | 4f3e8c0 | 2019-11-22 14:28:47 +0900 | [diff] [blame] | 233 | bool IsNonCopyableType(const AidlTypeSpecifier& type, const AidlTypenames& typenames) { |
Devin Moore | f12cc11 | 2020-08-26 16:25:00 -0700 | [diff] [blame] | 234 | if (type.IsArray() || typenames.IsList(type)) { |
Jiyong Park | 4f3e8c0 | 2019-11-22 14:28:47 +0900 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
| 238 | const std::string cpp_name = GetCppName(type, typenames); |
| 239 | if (cpp_name == "::android::base::unique_fd") { |
| 240 | return true; |
| 241 | } |
| 242 | return false; |
| 243 | } |
| 244 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 245 | std::string ParcelReadMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) { |
| 246 | return "read" + RawParcelMethod(type, typenames, true /* readMethod */); |
| 247 | } |
| 248 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 249 | std::string ParcelReadCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames, |
| 250 | const std::string& variable_name) { |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 251 | if (auto enum_decl = typenames.GetEnumDeclaration(type); |
| 252 | enum_decl != nullptr && !type.IsArray()) { |
| 253 | return StringPrintf("reinterpret_cast<%s *>(%s)", |
| 254 | CppNameOf(enum_decl->GetBackingType(), typenames).c_str(), |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 255 | variable_name.c_str()); |
| 256 | } |
| 257 | |
| 258 | return variable_name; |
| 259 | } |
| 260 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 261 | std::string ParcelWriteMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) { |
| 262 | return "write" + RawParcelMethod(type, typenames, false /* readMethod */); |
| 263 | } |
| 264 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 265 | std::string ParcelWriteCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames, |
| 266 | const std::string& variable_name) { |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 267 | if (auto enum_decl = typenames.GetEnumDeclaration(type); |
| 268 | enum_decl != nullptr && !type.IsArray()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 269 | return StringPrintf("static_cast<%s>(%s)", |
| 270 | CppNameOf(enum_decl->GetBackingType(), typenames).c_str(), |
| 271 | variable_name.c_str()); |
| 272 | } |
| 273 | |
| 274 | if (typenames.GetInterface(type) != nullptr) { |
| 275 | return GetRawCppName(type) + "::asBinder(" + variable_name + ")"; |
| 276 | } |
| 277 | |
| 278 | return variable_name; |
| 279 | } |
| 280 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 281 | void AddHeaders(const AidlTypeSpecifier& type, const AidlTypenames& typenames, |
| 282 | std::set<std::string>* headers) { |
| 283 | CHECK(!typenames.IsList(type) || type.GetTypeParameters().size() == 1); |
| 284 | bool isVector = type.IsArray() || typenames.IsList(type); |
| 285 | bool isNullable = type.IsNullable(); |
| 286 | bool utf8 = type.IsUtf8InCpp(); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 287 | |
| 288 | if (isVector) { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 289 | headers->insert("vector"); |
| 290 | } |
| 291 | if (type.IsGeneric()) { |
| 292 | for (const auto& parameter : type.GetTypeParameters()) { |
| 293 | AddHeaders(*parameter, typenames, headers); |
| 294 | } |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 295 | } |
| 296 | if (isNullable) { |
| 297 | if (type.GetName() != "IBinder") { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 298 | headers->insert("optional"); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 299 | } |
| 300 | } |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 301 | if (typenames.IsList(type)) { |
| 302 | // Nothing else to do for List. |
| 303 | return; |
| 304 | } |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 305 | if (type.GetName() == "String") { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 306 | headers->insert(utf8 ? "string" : "utils/String16.h"); |
| 307 | return; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 308 | } |
| 309 | if (type.GetName() == "IBinder") { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 310 | headers->insert("binder/IBinder.h"); |
| 311 | return; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 312 | } |
| 313 | if (type.GetName() == "FileDescriptor") { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 314 | headers->insert("android-base/unique_fd.h"); |
| 315 | return; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 316 | } |
| 317 | if (type.GetName() == "ParcelFileDescriptor") { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 318 | headers->insert("binder/ParcelFileDescriptor.h"); |
| 319 | return; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static const std::set<string> need_cstdint{"byte", "int", "long"}; |
| 323 | if (need_cstdint.find(type.GetName()) != need_cstdint.end()) { |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 324 | headers->insert("cstdint"); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 325 | return; |
| 326 | } |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 327 | |
| 328 | if (AidlTypenames::IsPrimitiveTypename(type.GetName())) { |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | auto definedType = typenames.TryGetDefinedType(type.GetName()); |
| 333 | AIDL_FATAL_IF(definedType == nullptr, type) << "Unexpected type: " << type.GetName(); |
| 334 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 335 | if (definedType->AsInterface() != nullptr || definedType->AsStructuredParcelable() != nullptr || |
| 336 | definedType->AsEnumDeclaration() != nullptr) { |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 337 | AddHeaders(*definedType, headers); |
| 338 | } else if (definedType->AsParcelable() != nullptr) { |
| 339 | const std::string cpp_header = definedType->AsParcelable()->GetCppHeader(); |
| 340 | AIDL_FATAL_IF(cpp_header.empty(), definedType->AsParcelable()) |
| 341 | << "Parcelable " << definedType->AsParcelable()->GetCanonicalName() |
| 342 | << " has no C++ header defined."; |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 343 | headers->insert(cpp_header); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 347 | void AddHeaders(const AidlDefinedType& definedType, std::set<std::string>* headers) { |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 348 | vector<string> name = definedType.GetSplitPackage(); |
| 349 | name.push_back(definedType.GetName()); |
| 350 | const std::string cpp_header = Join(name, '/') + ".h"; |
Devin Moore | 2f2077a | 2020-08-28 11:27:53 -0700 | [diff] [blame] | 351 | headers->insert(cpp_header); |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 352 | } |
| 353 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 354 | } // namespace cpp |
| 355 | } // namespace aidl |
| 356 | } // namespace android |