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