Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "ResourceTable.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <memory> |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 21 | #include <optional> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <tuple> |
| 23 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
| 27 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 28 | #include "NameMangler.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 29 | #include "ResourceUtils.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 30 | #include "ResourceValues.h" |
| 31 | #include "ValueVisitor.h" |
| 32 | #include "text/Unicode.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 33 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 34 | #include "util/Util.h" |
| 35 | |
| 36 | using ::aapt::text::IsValidResourceEntryName; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 37 | using ::android::ConfigDescription; |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 38 | using ::android::StringPiece; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 39 | using ::android::base::StringPrintf; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | namespace aapt { |
| 42 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 43 | const char* Overlayable::kActorScheme = "overlay"; |
| 44 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 45 | namespace { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 46 | bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, |
| 47 | const ResourceNamedTypeRef& rhs) { |
| 48 | return lhs->named_type < rhs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | template <typename T> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 52 | bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, StringPiece rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | } |
| 55 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 56 | template <typename T> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 57 | bool greater_than_struct_with_name(StringPiece lhs, const std::unique_ptr<T>& rhs) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 58 | return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0; |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 61 | template <typename T> |
| 62 | struct NameEqualRange { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 63 | bool operator()(const std::unique_ptr<T>& lhs, StringPiece rhs) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 64 | return less_than_struct_with_name<T>(lhs, rhs); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | } |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 66 | bool operator()(StringPiece lhs, const std::unique_ptr<T>& rhs) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 67 | return greater_than_struct_with_name<T>(lhs, rhs); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | template <typename T, typename U> |
| 72 | bool less_than_struct_with_name_and_id(const T& lhs, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 73 | const std::pair<std::string_view, std::optional<U>>& rhs) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 74 | if (lhs.id != rhs.second) { |
| 75 | return lhs.id < rhs.second; |
| 76 | } |
| 77 | return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 80 | template <typename T, typename Func, typename Elements> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 81 | T* FindElementsRunAction(android::StringPiece name, Elements& entries, Func action) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 82 | const auto iter = |
| 83 | std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>); |
| 84 | const bool found = iter != entries.end() && name == (*iter)->name; |
| 85 | return action(found, iter); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 88 | struct ConfigKey { |
| 89 | const ConfigDescription* config; |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 90 | StringPiece product; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | template <typename T> |
| 94 | bool lt_config_key_ref(const T& lhs, const ConfigKey& rhs) { |
| 95 | int cmp = lhs->config.compare(*rhs.config); |
| 96 | if (cmp == 0) { |
| 97 | cmp = StringPiece(lhs->product).compare(rhs.product); |
| 98 | } |
| 99 | return cmp < 0; |
| 100 | } |
| 101 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 102 | } // namespace |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 103 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 104 | ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) { |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 107 | ResourceTablePackage* ResourceTable::FindPackage(android::StringPiece name) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 108 | return FindElementsRunAction<ResourceTablePackage>( |
| 109 | name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 112 | ResourceTablePackage* ResourceTable::FindOrCreatePackage(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 113 | return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) { |
| 114 | return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get(); |
| 115 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 118 | template <typename Func, typename Elements> |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 119 | static ResourceTableType* FindTypeRunAction(const ResourceNamedTypeRef& type, Elements& entries, |
| 120 | Func action) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 121 | const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 122 | const bool found = iter != entries.end() && type == (*iter)->named_type; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 123 | return action(found, iter); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 126 | ResourceTableType* ResourceTablePackage::FindTypeWithDefaultName(const ResourceType type) const { |
| 127 | auto named_type = ResourceNamedTypeWithDefaultName(type); |
| 128 | return FindType(named_type); |
| 129 | } |
| 130 | |
| 131 | ResourceTableType* ResourceTablePackage::FindType(const ResourceNamedTypeRef& type) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 132 | return FindTypeRunAction(type, types, |
| 133 | [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 136 | ResourceTableType* ResourceTablePackage::FindOrCreateType(const ResourceNamedTypeRef& type) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 137 | return FindTypeRunAction(type, types, [&](bool found, auto& iter) { |
| 138 | return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get(); |
| 139 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 142 | ResourceEntry* ResourceTableType::CreateEntry(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 143 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 144 | return entries.emplace(iter, new ResourceEntry(name))->get(); |
| 145 | }); |
| 146 | } |
| 147 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 148 | ResourceEntry* ResourceTableType::FindEntry(android::StringPiece name) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 149 | return FindElementsRunAction<ResourceEntry>( |
| 150 | name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
| 151 | } |
| 152 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 153 | ResourceEntry* ResourceTableType::FindOrCreateEntry(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 154 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 155 | return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get(); |
| 156 | }); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config, |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 160 | android::StringPiece product) { |
| 161 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 162 | lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 163 | if (iter != values.end()) { |
| 164 | ResourceConfigValue* value = iter->get(); |
| 165 | if (value->config == config && StringPiece(value->product) == product) { |
| 166 | return value; |
| 167 | } |
| 168 | } |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
| 172 | const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config, |
| 173 | android::StringPiece product) const { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 174 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 175 | lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | if (iter != values.end()) { |
| 177 | ResourceConfigValue* value = iter->get(); |
| 178 | if (value->config == config && StringPiece(value->product) == product) { |
| 179 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 180 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } |
| 182 | return nullptr; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 185 | ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 186 | StringPiece product) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 187 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 188 | lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | if (iter != values.end()) { |
| 190 | ResourceConfigValue* value = iter->get(); |
| 191 | if (value->config == config && StringPiece(value->product) == product) { |
| 192 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 193 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | } |
| 195 | ResourceConfigValue* newValue = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 196 | values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | return newValue; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 200 | std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | std::vector<ResourceConfigValue*> results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | auto iter = values.begin(); |
| 204 | for (; iter != values.end(); ++iter) { |
| 205 | ResourceConfigValue* value = iter->get(); |
| 206 | if (value->config == config) { |
| 207 | results.push_back(value); |
| 208 | ++iter; |
| 209 | break; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 210 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | for (; iter != values.end(); ++iter) { |
| 214 | ResourceConfigValue* value = iter->get(); |
| 215 | if (value->config == config) { |
| 216 | results.push_back(value); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 217 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | } |
| 219 | return results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 222 | bool ResourceEntry::HasDefaultValue() const { |
| 223 | const ConfigDescription& default_config = ConfigDescription::DefaultConfig(); |
| 224 | |
| 225 | // The default config should be at the top of the list, since the list is sorted. |
| 226 | for (auto& config_value : values) { |
| 227 | if (config_value->config == default_config) { |
| 228 | return true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 229 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | } |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 231 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 234 | // The default handler for collisions. |
| 235 | // |
| 236 | // Typically, a weak value will be overridden by a strong value. An existing weak |
| 237 | // value will not be overridden by an incoming weak value. |
| 238 | // |
| 239 | // There are some exceptions: |
| 240 | // |
| 241 | // Attributes: There are two types of Attribute values: USE and DECL. |
| 242 | // |
| 243 | // USE is anywhere an Attribute is declared without a format, and in a place that would |
| 244 | // be legal to declare if the Attribute already existed. This is typically in a |
| 245 | // <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak. |
| 246 | // |
| 247 | // DECL is an absolute declaration of an Attribute and specifies an explicit format. |
| 248 | // |
| 249 | // A DECL will override a USE without error. Two DECLs must match in their format for there to be |
| 250 | // no error. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 251 | ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing, |
| 252 | Value* incoming) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | Attribute* existing_attr = ValueCast<Attribute>(existing); |
| 254 | Attribute* incoming_attr = ValueCast<Attribute>(incoming); |
| 255 | if (!incoming_attr) { |
| 256 | if (incoming->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | // We're trying to add a weak resource but a resource |
| 258 | // already exists. Keep the existing. |
| 259 | return CollisionResult::kKeepOriginal; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | } else if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | // Override the weak resource with the new strong resource. |
| 262 | return CollisionResult::kTakeNew; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 263 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | // The existing and incoming values are strong, this is an error |
| 265 | // if the values are not both attributes. |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 266 | return CollisionResult::kConflict; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | if (!existing_attr) { |
| 270 | if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 271 | // The existing value is not an attribute and it is weak, |
| 272 | // so take the incoming attribute value. |
| 273 | return CollisionResult::kTakeNew; |
| 274 | } |
| 275 | // The existing value is not an attribute and it is strong, |
| 276 | // so the incoming attribute value is an error. |
| 277 | return CollisionResult::kConflict; |
| 278 | } |
| 279 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | CHECK(incoming_attr != nullptr && existing_attr != nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | |
| 282 | // |
| 283 | // Attribute specific handling. At this point we know both |
| 284 | // values are attributes. Since we can declare and define |
| 285 | // attributes all-over, we do special handling to see |
| 286 | // which definition sticks. |
| 287 | // |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 288 | if (existing_attr->IsCompatibleWith(*incoming_attr)) { |
| 289 | // The two attributes are both DECLs, but they are plain attributes with compatible formats. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | // Keep the strongest one. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 291 | return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 294 | if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | // Any incoming attribute is better than this. |
| 296 | return CollisionResult::kTakeNew; |
| 297 | } |
| 298 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 299 | if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | // The incoming attribute may be a USE instead of a DECL. |
| 301 | // Keep the existing attribute. |
| 302 | return CollisionResult::kKeepOriginal; |
| 303 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 304 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | return CollisionResult::kConflict; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 308 | namespace { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 309 | template <typename T, typename Comparer> |
| 310 | struct SortedVectorInserter : public Comparer { |
| 311 | std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el, |
| 312 | const T& value) { |
| 313 | auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) { |
| 314 | return Comparer::operator()(lhs, rhs); |
| 315 | }); |
| 316 | bool found = |
| 317 | it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it); |
| 318 | return std::make_pair(found, it); |
| 319 | } |
| 320 | |
| 321 | T* Insert(std::vector<T>& el, T&& value) { |
| 322 | auto [found, it] = LowerBound(el, value); |
| 323 | if (found) { |
| 324 | return &*it; |
| 325 | } |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 326 | return &*el.insert(it, std::forward<T>(value)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 327 | } |
| 328 | }; |
| 329 | |
| 330 | struct PackageViewComparer { |
| 331 | bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) { |
| 332 | return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>( |
| 333 | lhs, std::make_pair(rhs.name, rhs.id)); |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | struct TypeViewComparer { |
| 338 | bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 339 | return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.named_type < rhs.named_type; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 340 | } |
| 341 | }; |
| 342 | |
| 343 | struct EntryViewComparer { |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 344 | bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) { |
| 345 | return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>( |
| 346 | lhs, std::make_pair(rhs.name, rhs.id)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 347 | } |
| 348 | }; |
| 349 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 350 | void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package, |
| 351 | const ResourceTableType* type, const std::string& entry_name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 352 | const std::optional<ResourceId>& id, const Visibility& visibility, |
| 353 | const std::optional<AllowNew>& allow_new, |
| 354 | const std::optional<OverlayableItem>& overlayable_item, |
| 355 | const std::optional<StagedId>& staged_id, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 356 | const std::vector<std::unique_ptr<ResourceConfigValue>>& values) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 357 | SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter; |
| 358 | SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 359 | SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 360 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 361 | ResourceTablePackageView new_package{package->name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 362 | id ? id.value().package_id() : std::optional<uint8_t>{}}; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 363 | auto view_package = package_inserter.Insert(table.packages, std::move(new_package)); |
| 364 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 365 | ResourceTableTypeView new_type{type->named_type, |
| 366 | id ? id.value().type_id() : std::optional<uint8_t>{}}; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 367 | auto view_type = type_inserter.Insert(view_package->types, std::move(new_type)); |
| 368 | |
| 369 | if (visibility.level == Visibility::Level::kPublic) { |
| 370 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 371 | view_type->visibility_level = Visibility::Level::kPublic; |
| 372 | } |
| 373 | |
| 374 | ResourceTableEntryView new_entry{.name = entry_name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 375 | .id = id ? id.value().entry_id() : std::optional<uint16_t>{}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 376 | .visibility = visibility, |
| 377 | .allow_new = allow_new, |
| 378 | .overlayable_item = overlayable_item, |
| 379 | .staged_id = staged_id}; |
| 380 | for (auto& value : values) { |
| 381 | new_entry.values.emplace_back(value.get()); |
| 382 | } |
| 383 | |
| 384 | entry_inserter.Insert(view_type->entries, std::move(new_entry)); |
| 385 | } |
| 386 | } // namespace |
| 387 | |
| 388 | const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config, |
| 389 | android::StringPiece product) const { |
| 390 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 391 | lt_config_key_ref<const ResourceConfigValue*>); |
| 392 | if (iter != values.end()) { |
| 393 | const ResourceConfigValue* value = *iter; |
| 394 | if (value->config == config && StringPiece(value->product) == product) { |
| 395 | return value; |
| 396 | } |
| 397 | } |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
| 401 | ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const { |
| 402 | ResourceTableView view; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 403 | for (const auto& package : packages) { |
| 404 | for (const auto& type : package->types) { |
| 405 | for (const auto& entry : type->entries) { |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 406 | InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id, |
| 407 | entry->visibility, entry->allow_new, entry->overlayable_item, |
| 408 | entry->staged_id, entry->values); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 409 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 410 | if (options.create_alias_entries && entry->staged_id) { |
| 411 | auto alias_id = entry->staged_id.value().id; |
| 412 | InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id, |
| 413 | entry->visibility, entry->allow_new, entry->overlayable_item, {}, |
| 414 | entry->values); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 415 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 416 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 417 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 418 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 419 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 420 | // The android runtime does not support querying resources when the there are multiple type ids |
| 421 | // for the same resource type within the same package. For this reason, if there are types with |
| 422 | // multiple type ids, each type needs to exist in its own package in order to be queried by name. |
| 423 | std::vector<ResourceTablePackageView> new_packages; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 424 | SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter; |
| 425 | SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 426 | for (auto& package : view.packages) { |
| 427 | // If a new package was already created for a different type within this package, then |
| 428 | // we can reuse those packages for other types that need to be extracted from this package. |
| 429 | // `start_index` is the index of the first newly created package that can be reused. |
| 430 | const size_t start_index = new_packages.size(); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 431 | std::map<ResourceNamedType, size_t> type_new_package_index; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 432 | for (auto type_it = package.types.begin(); type_it != package.types.end();) { |
| 433 | auto& type = *type_it; |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 434 | auto type_index_iter = type_new_package_index.find(type.named_type); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 435 | if (type_index_iter == type_new_package_index.end()) { |
| 436 | // First occurrence of the resource type in this package. Keep it in this package. |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 437 | type_new_package_index.insert(type_index_iter, |
| 438 | std::make_pair(type.named_type, start_index)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 439 | ++type_it; |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | // The resource type has already been seen for this package, so this type must be extracted to |
| 444 | // a new separate package. |
| 445 | const size_t index = type_index_iter->second; |
| 446 | if (new_packages.size() == index) { |
| 447 | new_packages.emplace_back(ResourceTablePackageView{package.name, package.id}); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Move the type into a new package |
| 451 | auto& other_package = new_packages[index]; |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 452 | type_new_package_index[type.named_type] = index + 1; |
Greg Kaiser | b40f3ab | 2021-10-18 06:37:26 -0700 | [diff] [blame] | 453 | type_inserter.Insert(other_package.types, std::move(type)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 454 | type_it = package.types.erase(type_it); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | for (auto& new_package : new_packages) { |
| 459 | // Insert newly created packages after their original packages |
| 460 | auto [_, it] = package_inserter.LowerBound(view.packages, new_package); |
| 461 | view.packages.insert(++it, std::move(new_package)); |
| 462 | } |
| 463 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 464 | return view; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 467 | bool ResourceTable::AddResource(NewResource&& res, android::IDiagnostics* diag) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 468 | CHECK(diag != nullptr) << "Diagnostic pointer is null"; |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 469 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 470 | const bool validate = validation_ == Validation::kEnabled; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 471 | const android::Source source = res.value ? res.value->GetSource() : android::Source{}; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 472 | if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 473 | diag->Error(android::DiagMessage(source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 474 | << "resource '" << res.name << "' has invalid entry name '" << res.name.entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 477 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 478 | if (res.id.has_value() && !res.id->first.is_valid()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 479 | diag->Error(android::DiagMessage(source) |
| 480 | << "trying to add resource '" << res.name << "' with ID " << res.id->first |
| 481 | << " but that ID is invalid"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 484 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 485 | auto package = FindOrCreatePackage(res.name.package); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 486 | auto type = package->FindOrCreateType(res.name.type); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 487 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry, |
| 488 | NameEqualRange<ResourceEntry>{}); |
| 489 | const size_t entry_count = std::distance(entry_it.first, entry_it.second); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 490 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 491 | ResourceEntry* entry; |
| 492 | if (entry_count == 0) { |
| 493 | // Adding a new resource |
| 494 | entry = type->CreateEntry(res.name.entry); |
| 495 | } else if (entry_count == 1) { |
| 496 | // Assume that the existing resource is being modified |
| 497 | entry = entry_it.first->get(); |
| 498 | } else { |
| 499 | // Multiple resources with the same name exist in the resource table. The only way to |
| 500 | // distinguish between them is using resource id since each resource should have a unique id. |
| 501 | CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name |
| 502 | << "' without specifying a resource id."; |
| 503 | entry = entry_it.first->get(); |
| 504 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 505 | CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name |
| 506 | << "' with multiple entries without resource ids"; |
| 507 | if ((*it)->id == res.id->first) { |
| 508 | entry = it->get(); |
| 509 | break; |
| 510 | } |
| 511 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 513 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 514 | if (res.id.has_value()) { |
| 515 | if (entry->id && entry->id.value() != res.id->first) { |
| 516 | if (res.id->second != OnIdConflict::CREATE_ENTRY) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 517 | diag->Error(android::DiagMessage(source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 518 | << "trying to add resource '" << res.name << "' with ID " << res.id->first |
| 519 | << " but resource already has ID " << entry->id.value()); |
| 520 | return false; |
| 521 | } |
| 522 | entry = type->CreateEntry(res.name.entry); |
| 523 | } |
| 524 | entry->id = res.id->first; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 526 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 527 | if (res.visibility.has_value()) { |
| 528 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 529 | if (res.visibility->level == Visibility::Level::kPublic) { |
| 530 | type->visibility_level = Visibility::Level::kPublic; |
| 531 | } |
| 532 | |
| 533 | if (res.visibility->level > entry->visibility.level) { |
| 534 | // This symbol definition takes precedence, replace. |
| 535 | entry->visibility = res.visibility.value(); |
| 536 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 537 | |
| 538 | if (res.visibility->staged_api) { |
| 539 | entry->visibility.staged_api = entry->visibility.staged_api; |
| 540 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 541 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 542 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 543 | if (res.overlayable.has_value()) { |
| 544 | if (entry->overlayable_item) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 545 | diag->Error(android::DiagMessage(res.overlayable->source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 546 | << "duplicate overlayable declaration for resource '" << res.name << "'"); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 547 | diag->Error(android::DiagMessage(entry->overlayable_item.value().source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 548 | << "previous declaration here"); |
| 549 | return false; |
| 550 | } |
| 551 | entry->overlayable_item = res.overlayable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 554 | if (res.allow_new.has_value()) { |
| 555 | entry->allow_new = res.allow_new.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 558 | if (res.staged_id.has_value()) { |
| 559 | entry->staged_id = res.staged_id.value(); |
| 560 | } |
| 561 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 562 | if (res.value != nullptr) { |
| 563 | auto config_value = entry->FindOrCreateValue(res.config, res.product); |
| 564 | if (!config_value->value) { |
| 565 | // Resource does not exist, add it now. |
| 566 | config_value->value = std::move(res.value); |
| 567 | } else { |
| 568 | // When validation is enabled, ensure that a resource cannot have multiple values defined for |
| 569 | // the same configuration. |
| 570 | auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get()) |
| 571 | : CollisionResult::kKeepBoth; |
| 572 | switch (result) { |
| 573 | case CollisionResult::kKeepBoth: |
| 574 | // Insert the value ignoring for duplicate configurations |
| 575 | entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product)); |
| 576 | entry->values.back()->value = std::move(res.value); |
| 577 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 578 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 579 | case CollisionResult::kTakeNew: |
| 580 | // Take the incoming value. |
| 581 | config_value->value = std::move(res.value); |
| 582 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 583 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 584 | case CollisionResult::kConflict: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 585 | diag->Error(android::DiagMessage(source) |
| 586 | << "duplicate value for resource '" << res.name << "' " |
| 587 | << "with config '" << res.config << "'"); |
| 588 | diag->Error(android::DiagMessage(source) << "resource previously defined here"); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 589 | return false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 590 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 591 | case CollisionResult::kKeepOriginal: |
| 592 | break; |
| 593 | } |
| 594 | } |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 597 | return true; |
| 598 | } |
| 599 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 600 | std::optional<ResourceTable::SearchResult> ResourceTable::FindResource( |
| 601 | const ResourceNameRef& name) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 602 | ResourceTablePackage* package = FindPackage(name.package); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 603 | if (package == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | return {}; |
| 605 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 606 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 607 | ResourceTableType* type = package->FindType(name.type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 608 | if (type == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 609 | return {}; |
| 610 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 611 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 612 | ResourceEntry* entry = type->FindEntry(name.entry); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 613 | if (entry == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 614 | return {}; |
| 615 | } |
| 616 | return SearchResult{package, type, entry}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 619 | std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name, |
| 620 | ResourceId id) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 621 | ResourceTablePackage* package = FindPackage(name.package); |
| 622 | if (package == nullptr) { |
| 623 | return {}; |
| 624 | } |
| 625 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 626 | ResourceTableType* type = package->FindType(name.type); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 627 | if (type == nullptr) { |
| 628 | return {}; |
| 629 | } |
| 630 | |
| 631 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, |
| 632 | NameEqualRange<ResourceEntry>{}); |
| 633 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 634 | if ((*it)->id == id) { |
| 635 | return SearchResult{package, type, it->get()}; |
| 636 | } |
| 637 | } |
| 638 | return {}; |
| 639 | } |
| 640 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 641 | bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const { |
| 642 | ResourceTablePackage* package = FindPackage(name.package); |
| 643 | if (package == nullptr) { |
| 644 | return {}; |
| 645 | } |
| 646 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 647 | ResourceTableType* type = package->FindType(name.type); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 648 | if (type == nullptr) { |
| 649 | return {}; |
| 650 | } |
| 651 | |
| 652 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, |
| 653 | NameEqualRange<ResourceEntry>{}); |
| 654 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 655 | if ((*it)->id == id) { |
| 656 | type->entries.erase(it); |
| 657 | return true; |
| 658 | } |
| 659 | } |
| 660 | return false; |
| 661 | } |
| 662 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 663 | std::unique_ptr<ResourceTable> ResourceTable::Clone() const { |
| 664 | std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>(); |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 665 | CloningValueTransformer cloner(&new_table->string_pool); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 666 | for (const auto& pkg : packages) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 667 | ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 668 | for (const auto& type : pkg->types) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 669 | ResourceTableType* new_type = new_pkg->FindOrCreateType(type->named_type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 670 | new_type->visibility_level = type->visibility_level; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 671 | |
| 672 | for (const auto& entry : type->entries) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 673 | ResourceEntry* new_entry = new_type->CreateEntry(entry->name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 674 | new_entry->id = entry->id; |
| 675 | new_entry->visibility = entry->visibility; |
| 676 | new_entry->allow_new = entry->allow_new; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 677 | new_entry->overlayable_item = entry->overlayable_item; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 678 | |
| 679 | for (const auto& config_value : entry->values) { |
| 680 | ResourceConfigValue* new_value = |
| 681 | new_entry->FindOrCreateValue(config_value->config, config_value->product); |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 682 | new_value->value = config_value->value->Transform(cloner); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | return new_table; |
| 688 | } |
| 689 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 690 | NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) { |
| 691 | res_.name = name.ToResourceName(); |
| 692 | } |
| 693 | |
| 694 | NewResourceBuilder::NewResourceBuilder(const std::string& name) { |
| 695 | ResourceNameRef ref; |
| 696 | CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name; |
| 697 | res_.name = ref.ToResourceName(); |
| 698 | } |
| 699 | |
| 700 | NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value, |
| 701 | android::ConfigDescription config, |
| 702 | std::string product) { |
| 703 | res_.value = std::move(value); |
| 704 | res_.config = std::move(config); |
| 705 | res_.product = std::move(product); |
| 706 | return *this; |
| 707 | } |
| 708 | |
| 709 | NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) { |
| 710 | res_.id = std::make_pair(id, on_conflict); |
| 711 | return *this; |
| 712 | } |
| 713 | |
| 714 | NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) { |
| 715 | res_.visibility = std::move(visibility); |
| 716 | return *this; |
| 717 | } |
| 718 | |
| 719 | NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) { |
| 720 | res_.overlayable = std::move(overlayable); |
| 721 | return *this; |
| 722 | } |
| 723 | NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) { |
| 724 | res_.allow_new = std::move(allow_new); |
| 725 | return *this; |
| 726 | } |
| 727 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 728 | NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) { |
| 729 | res_.staged_id = std::move(staged_alias); |
| 730 | return *this; |
| 731 | } |
| 732 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 733 | NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) { |
| 734 | res_.allow_mangled = allow_mangled; |
| 735 | return *this; |
| 736 | } |
| 737 | |
| 738 | NewResource NewResourceBuilder::Build() { |
| 739 | return std::move(res_); |
| 740 | } |
| 741 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 742 | } // namespace aapt |