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> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include <tuple> |
| 22 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 24 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 25 | #include "androidfw/ResourceTypes.h" |
| 26 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 27 | #include "NameMangler.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 28 | #include "ResourceUtils.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 29 | #include "ResourceValues.h" |
| 30 | #include "ValueVisitor.h" |
| 31 | #include "text/Unicode.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 32 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 33 | #include "util/Util.h" |
| 34 | |
| 35 | using ::aapt::text::IsValidResourceEntryName; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 36 | using ::android::ConfigDescription; |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 37 | using ::android::StringPiece; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 38 | using ::android::base::StringPrintf; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | namespace aapt { |
| 41 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 42 | const char* Overlayable::kActorScheme = "overlay"; |
| 43 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) { |
| 46 | return lhs->type < rhs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | template <typename T> |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 50 | bool less_than_type_and_id(const T& lhs, const std::pair<ResourceType, Maybe<uint8_t>>& rhs) { |
| 51 | return lhs.id != rhs.second ? lhs.id < rhs.second : lhs.type < rhs.first; |
| 52 | } |
| 53 | |
| 54 | template <typename T> |
| 55 | bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | 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] | 57 | } |
| 58 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 59 | template <typename T> |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 60 | bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) { |
| 61 | 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] | 62 | } |
| 63 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 64 | template <typename T> |
| 65 | struct NameEqualRange { |
| 66 | bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const { |
| 67 | return less_than_struct_with_name<T>(lhs, rhs); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 69 | bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const { |
| 70 | return greater_than_struct_with_name<T>(lhs, rhs); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | template <typename T, typename U> |
| 75 | bool less_than_struct_with_name_and_id(const T& lhs, |
| 76 | const std::pair<std::string_view, Maybe<U>>& rhs) { |
| 77 | if (lhs.id != rhs.second) { |
| 78 | return lhs.id < rhs.second; |
| 79 | } |
| 80 | 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] | 81 | } |
| 82 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 83 | template <typename T, typename U> |
| 84 | bool less_than_struct_with_name_and_id_pointer(const T* lhs, |
| 85 | const std::pair<std::string_view, Maybe<U>>& rhs) { |
| 86 | return less_than_struct_with_name_and_id(*lhs, rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 89 | template <typename T, typename Func, typename Elements> |
| 90 | T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) { |
| 91 | const auto iter = |
| 92 | std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>); |
| 93 | const bool found = iter != entries.end() && name == (*iter)->name; |
| 94 | return action(found, iter); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 97 | } // namespace |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 98 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 99 | ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) { |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 102 | ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const { |
| 103 | return FindElementsRunAction<ResourceTablePackage>( |
| 104 | name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 107 | ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) { |
| 108 | return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) { |
| 109 | return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get(); |
| 110 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 113 | template <typename Func, typename Elements> |
| 114 | static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) { |
| 115 | const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type); |
| 116 | const bool found = iter != entries.end() && type == (*iter)->type; |
| 117 | return action(found, iter); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 120 | ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const { |
| 121 | return FindTypeRunAction(type, types, |
| 122 | [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 125 | ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) { |
| 126 | return FindTypeRunAction(type, types, [&](bool found, auto& iter) { |
| 127 | return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get(); |
| 128 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 130 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 131 | ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) { |
| 132 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 133 | return entries.emplace(iter, new ResourceEntry(name))->get(); |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const { |
| 138 | return FindElementsRunAction<ResourceEntry>( |
| 139 | name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
| 140 | } |
| 141 | |
| 142 | ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) { |
| 143 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 144 | return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get(); |
| 145 | }); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | struct ConfigKey { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | const ConfigDescription* config; |
| 150 | const StringPiece& product; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 153 | bool lt_config_key_ref(const std::unique_ptr<ResourceConfigValue>& lhs, const ConfigKey& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | int cmp = lhs->config.compare(*rhs.config); |
| 155 | if (cmp == 0) { |
| 156 | cmp = StringPiece(lhs->product).compare(rhs.product); |
| 157 | } |
| 158 | return cmp < 0; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config, |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 162 | android::StringPiece product) { |
| 163 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 164 | lt_config_key_ref); |
| 165 | if (iter != values.end()) { |
| 166 | ResourceConfigValue* value = iter->get(); |
| 167 | if (value->config == config && StringPiece(value->product) == product) { |
| 168 | return value; |
| 169 | } |
| 170 | } |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config, |
| 175 | android::StringPiece product) const { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 176 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 177 | lt_config_key_ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | if (iter != values.end()) { |
| 179 | ResourceConfigValue* value = iter->get(); |
| 180 | if (value->config == config && StringPiece(value->product) == product) { |
| 181 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 182 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | } |
| 184 | return nullptr; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 187 | ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config, |
| 188 | const StringPiece& product) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 189 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 190 | lt_config_key_ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | if (iter != values.end()) { |
| 192 | ResourceConfigValue* value = iter->get(); |
| 193 | if (value->config == config && StringPiece(value->product) == product) { |
| 194 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 195 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | } |
| 197 | ResourceConfigValue* newValue = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 198 | values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | return newValue; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 202 | std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | std::vector<ResourceConfigValue*> results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 204 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | auto iter = values.begin(); |
| 206 | for (; iter != values.end(); ++iter) { |
| 207 | ResourceConfigValue* value = iter->get(); |
| 208 | if (value->config == config) { |
| 209 | results.push_back(value); |
| 210 | ++iter; |
| 211 | break; |
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 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | for (; iter != values.end(); ++iter) { |
| 216 | ResourceConfigValue* value = iter->get(); |
| 217 | if (value->config == config) { |
| 218 | results.push_back(value); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 219 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } |
| 221 | return results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 224 | bool ResourceEntry::HasDefaultValue() const { |
| 225 | const ConfigDescription& default_config = ConfigDescription::DefaultConfig(); |
| 226 | |
| 227 | // The default config should be at the top of the list, since the list is sorted. |
| 228 | for (auto& config_value : values) { |
| 229 | if (config_value->config == default_config) { |
| 230 | return true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 231 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | } |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 233 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 236 | // The default handler for collisions. |
| 237 | // |
| 238 | // Typically, a weak value will be overridden by a strong value. An existing weak |
| 239 | // value will not be overridden by an incoming weak value. |
| 240 | // |
| 241 | // There are some exceptions: |
| 242 | // |
| 243 | // Attributes: There are two types of Attribute values: USE and DECL. |
| 244 | // |
| 245 | // USE is anywhere an Attribute is declared without a format, and in a place that would |
| 246 | // be legal to declare if the Attribute already existed. This is typically in a |
| 247 | // <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak. |
| 248 | // |
| 249 | // DECL is an absolute declaration of an Attribute and specifies an explicit format. |
| 250 | // |
| 251 | // A DECL will override a USE without error. Two DECLs must match in their format for there to be |
| 252 | // no error. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 253 | ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing, |
| 254 | Value* incoming) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | Attribute* existing_attr = ValueCast<Attribute>(existing); |
| 256 | Attribute* incoming_attr = ValueCast<Attribute>(incoming); |
| 257 | if (!incoming_attr) { |
| 258 | if (incoming->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | // We're trying to add a weak resource but a resource |
| 260 | // already exists. Keep the existing. |
| 261 | return CollisionResult::kKeepOriginal; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | } else if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | // Override the weak resource with the new strong resource. |
| 264 | return CollisionResult::kTakeNew; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 265 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | // The existing and incoming values are strong, this is an error |
| 267 | // if the values are not both attributes. |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 268 | return CollisionResult::kConflict; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | if (!existing_attr) { |
| 272 | if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | // The existing value is not an attribute and it is weak, |
| 274 | // so take the incoming attribute value. |
| 275 | return CollisionResult::kTakeNew; |
| 276 | } |
| 277 | // The existing value is not an attribute and it is strong, |
| 278 | // so the incoming attribute value is an error. |
| 279 | return CollisionResult::kConflict; |
| 280 | } |
| 281 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | CHECK(incoming_attr != nullptr && existing_attr != nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | |
| 284 | // |
| 285 | // Attribute specific handling. At this point we know both |
| 286 | // values are attributes. Since we can declare and define |
| 287 | // attributes all-over, we do special handling to see |
| 288 | // which definition sticks. |
| 289 | // |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 290 | if (existing_attr->IsCompatibleWith(*incoming_attr)) { |
| 291 | // 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] | 292 | // Keep the strongest one. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 293 | return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 296 | 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] | 297 | // Any incoming attribute is better than this. |
| 298 | return CollisionResult::kTakeNew; |
| 299 | } |
| 300 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 301 | 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] | 302 | // The incoming attribute may be a USE instead of a DECL. |
| 303 | // Keep the existing attribute. |
| 304 | return CollisionResult::kKeepOriginal; |
| 305 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 306 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | return CollisionResult::kConflict; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 310 | ResourceTableView ResourceTable::GetPartitionedView() const { |
| 311 | ResourceTableView view; |
| 312 | for (const auto& package : packages) { |
| 313 | for (const auto& type : package->types) { |
| 314 | for (const auto& entry : type->entries) { |
| 315 | std::pair<std::string_view, Maybe<uint8_t>> package_key(package->name, {}); |
| 316 | std::pair<std::string_view, Maybe<ResourceId>> entry_key(entry->name, {}); |
| 317 | std::pair<ResourceType, Maybe<uint8_t>> type_key(type->type, {}); |
| 318 | if (entry->id) { |
| 319 | // If the entry has a defined id, use the id to determine insertion position. |
| 320 | package_key.second = entry->id.value().package_id(); |
| 321 | type_key.second = entry->id.value().type_id(); |
| 322 | entry_key.second = entry->id.value(); |
| 323 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 324 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 325 | auto package_it = |
| 326 | std::lower_bound(view.packages.begin(), view.packages.end(), package_key, |
| 327 | less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>); |
| 328 | if (package_it == view.packages.end() || package_it->name != package_key.first || |
| 329 | package_it->id != package_key.second) { |
| 330 | ResourceTablePackageView new_package{std::string(package_key.first), package_key.second}; |
| 331 | package_it = view.packages.insert(package_it, new_package); |
| 332 | } |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 333 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 334 | auto type_it = std::lower_bound(package_it->types.begin(), package_it->types.end(), |
| 335 | type_key, less_than_type_and_id<ResourceTableTypeView>); |
| 336 | if (type_it == package_it->types.end() || type_key.first != type_it->type || |
| 337 | type_it->id != type_key.second) { |
| 338 | ResourceTableTypeView new_type{type_key.first, type_key.second}; |
| 339 | type_it = package_it->types.insert(type_it, new_type); |
| 340 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 341 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 342 | if (entry->visibility.level == Visibility::Level::kPublic) { |
| 343 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 344 | type_it->visibility_level = Visibility::Level::kPublic; |
| 345 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 346 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 347 | auto entry_it = |
| 348 | std::lower_bound(type_it->entries.begin(), type_it->entries.end(), entry_key, |
| 349 | less_than_struct_with_name_and_id_pointer<ResourceEntry, ResourceId>); |
| 350 | type_it->entries.insert(entry_it, entry.get()); |
| 351 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 352 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 354 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 355 | return view; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 358 | bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) { |
| 359 | CHECK(diag != nullptr) << "Diagnostic pointer is null"; |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 360 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 361 | const bool validate = validation_ == Validation::kEnabled; |
| 362 | const Source source = res.value ? res.value->GetSource() : Source{}; |
| 363 | if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 364 | diag->Error(DiagMessage(source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 365 | << "resource '" << res.name << "' has invalid entry name '" << res.name.entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 368 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 369 | if (res.id.has_value() && !res.id->first.is_valid()) { |
| 370 | diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID " |
| 371 | << res.id->first << " but that ID is invalid"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 374 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 375 | auto package = FindOrCreatePackage(res.name.package); |
| 376 | auto type = package->FindOrCreateType(res.name.type); |
| 377 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry, |
| 378 | NameEqualRange<ResourceEntry>{}); |
| 379 | 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] | 380 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 381 | ResourceEntry* entry; |
| 382 | if (entry_count == 0) { |
| 383 | // Adding a new resource |
| 384 | entry = type->CreateEntry(res.name.entry); |
| 385 | } else if (entry_count == 1) { |
| 386 | // Assume that the existing resource is being modified |
| 387 | entry = entry_it.first->get(); |
| 388 | } else { |
| 389 | // Multiple resources with the same name exist in the resource table. The only way to |
| 390 | // distinguish between them is using resource id since each resource should have a unique id. |
| 391 | CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name |
| 392 | << "' without specifying a resource id."; |
| 393 | entry = entry_it.first->get(); |
| 394 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 395 | CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name |
| 396 | << "' with multiple entries without resource ids"; |
| 397 | if ((*it)->id == res.id->first) { |
| 398 | entry = it->get(); |
| 399 | break; |
| 400 | } |
| 401 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 403 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 404 | if (res.id.has_value()) { |
| 405 | if (entry->id && entry->id.value() != res.id->first) { |
| 406 | if (res.id->second != OnIdConflict::CREATE_ENTRY) { |
| 407 | diag->Error(DiagMessage(source) |
| 408 | << "trying to add resource '" << res.name << "' with ID " << res.id->first |
| 409 | << " but resource already has ID " << entry->id.value()); |
| 410 | return false; |
| 411 | } |
| 412 | entry = type->CreateEntry(res.name.entry); |
| 413 | } |
| 414 | entry->id = res.id->first; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 415 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 416 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 417 | if (res.visibility.has_value()) { |
| 418 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 419 | if (res.visibility->level == Visibility::Level::kPublic) { |
| 420 | type->visibility_level = Visibility::Level::kPublic; |
| 421 | } |
| 422 | |
| 423 | if (res.visibility->level > entry->visibility.level) { |
| 424 | // This symbol definition takes precedence, replace. |
| 425 | entry->visibility = res.visibility.value(); |
| 426 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 427 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 428 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 429 | if (res.overlayable.has_value()) { |
| 430 | if (entry->overlayable_item) { |
| 431 | diag->Error(DiagMessage(res.overlayable->source) |
| 432 | << "duplicate overlayable declaration for resource '" << res.name << "'"); |
| 433 | diag->Error(DiagMessage(entry->overlayable_item.value().source) |
| 434 | << "previous declaration here"); |
| 435 | return false; |
| 436 | } |
| 437 | entry->overlayable_item = res.overlayable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 440 | if (res.allow_new.has_value()) { |
| 441 | entry->allow_new = res.allow_new.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 444 | if (res.value != nullptr) { |
| 445 | auto config_value = entry->FindOrCreateValue(res.config, res.product); |
| 446 | if (!config_value->value) { |
| 447 | // Resource does not exist, add it now. |
| 448 | config_value->value = std::move(res.value); |
| 449 | } else { |
| 450 | // When validation is enabled, ensure that a resource cannot have multiple values defined for |
| 451 | // the same configuration. |
| 452 | auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get()) |
| 453 | : CollisionResult::kKeepBoth; |
| 454 | switch (result) { |
| 455 | case CollisionResult::kKeepBoth: |
| 456 | // Insert the value ignoring for duplicate configurations |
| 457 | entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product)); |
| 458 | entry->values.back()->value = std::move(res.value); |
| 459 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 460 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 461 | case CollisionResult::kTakeNew: |
| 462 | // Take the incoming value. |
| 463 | config_value->value = std::move(res.value); |
| 464 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 465 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 466 | case CollisionResult::kConflict: |
| 467 | diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' " |
| 468 | << "with config '" << res.config << "'"); |
| 469 | diag->Error(DiagMessage(source) << "resource previously defined here"); |
| 470 | return false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 471 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 472 | case CollisionResult::kKeepOriginal: |
| 473 | break; |
| 474 | } |
| 475 | } |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 478 | return true; |
| 479 | } |
| 480 | |
| 481 | Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 482 | ResourceTablePackage* package = FindPackage(name.package); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 483 | if (package == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 484 | return {}; |
| 485 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 486 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | ResourceTableType* type = package->FindType(name.type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 488 | if (type == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 489 | return {}; |
| 490 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 491 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | ResourceEntry* entry = type->FindEntry(name.entry); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 493 | if (entry == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 494 | return {}; |
| 495 | } |
| 496 | return SearchResult{package, type, entry}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 499 | Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name, |
| 500 | ResourceId id) const { |
| 501 | ResourceTablePackage* package = FindPackage(name.package); |
| 502 | if (package == nullptr) { |
| 503 | return {}; |
| 504 | } |
| 505 | |
| 506 | ResourceTableType* type = package->FindType(name.type); |
| 507 | if (type == nullptr) { |
| 508 | return {}; |
| 509 | } |
| 510 | |
| 511 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, |
| 512 | NameEqualRange<ResourceEntry>{}); |
| 513 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 514 | if ((*it)->id == id) { |
| 515 | return SearchResult{package, type, it->get()}; |
| 516 | } |
| 517 | } |
| 518 | return {}; |
| 519 | } |
| 520 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 521 | std::unique_ptr<ResourceTable> ResourceTable::Clone() const { |
| 522 | std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>(); |
| 523 | for (const auto& pkg : packages) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 524 | ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 525 | for (const auto& type : pkg->types) { |
| 526 | ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 527 | new_type->visibility_level = type->visibility_level; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 528 | |
| 529 | for (const auto& entry : type->entries) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 530 | ResourceEntry* new_entry = new_type->CreateEntry(entry->name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 531 | new_entry->id = entry->id; |
| 532 | new_entry->visibility = entry->visibility; |
| 533 | new_entry->allow_new = entry->allow_new; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 534 | new_entry->overlayable_item = entry->overlayable_item; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 535 | |
| 536 | for (const auto& config_value : entry->values) { |
| 537 | ResourceConfigValue* new_value = |
| 538 | new_entry->FindOrCreateValue(config_value->config, config_value->product); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 539 | new_value->value.reset(config_value->value->Clone(&new_table->string_pool)); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | return new_table; |
| 545 | } |
| 546 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 547 | NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) { |
| 548 | res_.name = name.ToResourceName(); |
| 549 | } |
| 550 | |
| 551 | NewResourceBuilder::NewResourceBuilder(const std::string& name) { |
| 552 | ResourceNameRef ref; |
| 553 | CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name; |
| 554 | res_.name = ref.ToResourceName(); |
| 555 | } |
| 556 | |
| 557 | NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value, |
| 558 | android::ConfigDescription config, |
| 559 | std::string product) { |
| 560 | res_.value = std::move(value); |
| 561 | res_.config = std::move(config); |
| 562 | res_.product = std::move(product); |
| 563 | return *this; |
| 564 | } |
| 565 | |
| 566 | NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) { |
| 567 | res_.id = std::make_pair(id, on_conflict); |
| 568 | return *this; |
| 569 | } |
| 570 | |
| 571 | NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) { |
| 572 | res_.visibility = std::move(visibility); |
| 573 | return *this; |
| 574 | } |
| 575 | |
| 576 | NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) { |
| 577 | res_.overlayable = std::move(overlayable); |
| 578 | return *this; |
| 579 | } |
| 580 | NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) { |
| 581 | res_.allow_new = std::move(allow_new); |
| 582 | return *this; |
| 583 | } |
| 584 | |
| 585 | NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) { |
| 586 | res_.allow_mangled = allow_mangled; |
| 587 | return *this; |
| 588 | } |
| 589 | |
| 590 | NewResource NewResourceBuilder::Build() { |
| 591 | return std::move(res_); |
| 592 | } |
| 593 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 594 | } // namespace aapt |