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> |
| 21 | #include <string> |
| 22 | #include <tuple> |
| 23 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 26 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 27 | #include "androidfw/ResourceTypes.h" |
| 28 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 29 | #include "Debug.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 30 | #include "NameMangler.h" |
| 31 | #include "ResourceValues.h" |
| 32 | #include "ValueVisitor.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 33 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 34 | #include "text/Unicode.h" |
| 35 | #include "util/Util.h" |
| 36 | |
| 37 | using ::aapt::text::IsValidResourceEntryName; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 38 | using ::android::ConfigDescription; |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 39 | using ::android::StringPiece; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 40 | using ::android::base::StringPrintf; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | namespace aapt { |
| 43 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 44 | const char* Overlayable::kActorScheme = "overlay"; |
| 45 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 46 | static bool less_than_type_and_id(const std::unique_ptr<ResourceTableType>& lhs, |
| 47 | const std::pair<ResourceType, Maybe<uint8_t>>& rhs) { |
| 48 | return lhs->type < rhs.first || (lhs->type == rhs.first && rhs.second && lhs->id < rhs.second); |
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> |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 52 | static 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] | 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> |
| 57 | static bool less_than_struct_with_name_and_id(const std::unique_ptr<T>& lhs, |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 58 | const std::pair<StringPiece, Maybe<uint16_t>>& rhs) { |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 59 | int name_cmp = lhs->name.compare(0, lhs->name.size(), rhs.first.data(), rhs.first.size()); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 60 | return name_cmp < 0 || (name_cmp == 0 && rhs.second && lhs->id < rhs.second); |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 63 | ResourceTablePackage* ResourceTable::FindPackage(const StringPiece& name) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | const auto last = packages.end(); |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 65 | auto iter = std::lower_bound(packages.begin(), last, name, |
| 66 | less_than_struct_with_name<ResourceTablePackage>); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | if (iter != last && name == (*iter)->name) { |
| 68 | return iter->get(); |
| 69 | } |
| 70 | return nullptr; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 73 | ResourceTablePackage* ResourceTable::FindPackageById(uint8_t id) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | for (auto& package : packages) { |
| 75 | if (package->id && package->id.value() == id) { |
| 76 | return package.get(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | } |
| 79 | return nullptr; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 82 | ResourceTablePackage* ResourceTable::CreatePackage(const StringPiece& name, Maybe<uint8_t> id) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 83 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | ResourceTablePackage* package = FindOrCreatePackage(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | if (id && !package->id) { |
| 86 | package->id = id; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 87 | return package; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (id && package->id && package->id.value() != id.value()) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | return package; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | } |
| 95 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 96 | ResourceTablePackage* ResourceTable::CreatePackageAllowingDuplicateNames(const StringPiece& name, |
| 97 | const Maybe<uint8_t> id) { |
| 98 | const auto last = packages.end(); |
| 99 | auto iter = std::lower_bound(packages.begin(), last, std::make_pair(name, id), |
| 100 | less_than_struct_with_name_and_id<ResourceTablePackage>); |
| 101 | |
| 102 | if (iter != last && name == (*iter)->name && id == (*iter)->id) { |
| 103 | return iter->get(); |
| 104 | } |
| 105 | |
| 106 | std::unique_ptr<ResourceTablePackage> new_package = util::make_unique<ResourceTablePackage>(); |
| 107 | new_package->name = name.to_string(); |
| 108 | new_package->id = id; |
| 109 | return packages.emplace(iter, std::move(new_package))->get(); |
| 110 | } |
| 111 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 112 | ResourceTablePackage* ResourceTable::FindOrCreatePackage(const StringPiece& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | const auto last = packages.end(); |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 114 | auto iter = std::lower_bound(packages.begin(), last, name, |
| 115 | less_than_struct_with_name<ResourceTablePackage>); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | if (iter != last && name == (*iter)->name) { |
| 117 | return iter->get(); |
| 118 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 120 | std::unique_ptr<ResourceTablePackage> new_package = util::make_unique<ResourceTablePackage>(); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 121 | new_package->name = name.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | return packages.emplace(iter, std::move(new_package))->get(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 125 | ResourceTableType* ResourceTablePackage::FindType(ResourceType type, const Maybe<uint8_t> id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | const auto last = types.end(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 127 | auto iter = std::lower_bound(types.begin(), last, std::make_pair(type, id), |
| 128 | less_than_type_and_id); |
| 129 | if (iter != last && (*iter)->type == type && (!id || id == (*iter)->id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | return iter->get(); |
| 131 | } |
| 132 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 135 | ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type, |
| 136 | const Maybe<uint8_t> id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | const auto last = types.end(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 138 | auto iter = std::lower_bound(types.begin(), last, std::make_pair(type, id), |
| 139 | less_than_type_and_id); |
| 140 | if (iter != last && (*iter)->type == type && (!id || id == (*iter)->id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | return iter->get(); |
| 142 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 143 | |
| 144 | auto new_type = new ResourceTableType(type); |
| 145 | new_type->id = id; |
| 146 | return types.emplace(iter, std::move(new_type))->get(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 149 | ResourceEntry* ResourceTableType::FindEntry(const StringPiece& name, const Maybe<uint16_t> id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | const auto last = entries.end(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 151 | auto iter = std::lower_bound(entries.begin(), last, std::make_pair(name, id), |
| 152 | less_than_struct_with_name_and_id<ResourceEntry>); |
| 153 | if (iter != last && name == (*iter)->name && (!id || id == (*iter)->id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | return iter->get(); |
| 155 | } |
| 156 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 159 | ResourceEntry* ResourceTableType::FindOrCreateEntry(const StringPiece& name, |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 160 | const Maybe<uint16_t > id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | auto last = entries.end(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 162 | auto iter = std::lower_bound(entries.begin(), last, std::make_pair(name, id), |
| 163 | less_than_struct_with_name_and_id<ResourceEntry>); |
| 164 | if (iter != last && name == (*iter)->name && (!id || id == (*iter)->id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | return iter->get(); |
| 166 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 167 | |
| 168 | auto new_entry = new ResourceEntry(name); |
| 169 | new_entry->id = id; |
| 170 | return entries.emplace(iter, std::move(new_entry))->get(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config) { |
| 174 | return FindValue(config, StringPiece()); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | struct ConfigKey { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | const ConfigDescription* config; |
| 179 | const StringPiece& product; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 182 | 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] | 183 | int cmp = lhs->config.compare(*rhs.config); |
| 184 | if (cmp == 0) { |
| 185 | cmp = StringPiece(lhs->product).compare(rhs.product); |
| 186 | } |
| 187 | return cmp < 0; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 191 | const StringPiece& product) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 192 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 193 | lt_config_key_ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | if (iter != values.end()) { |
| 195 | ResourceConfigValue* value = iter->get(); |
| 196 | if (value->config == config && StringPiece(value->product) == product) { |
| 197 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 198 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | } |
| 200 | return nullptr; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 203 | ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config, |
| 204 | const StringPiece& product) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 205 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
| 206 | lt_config_key_ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | if (iter != values.end()) { |
| 208 | ResourceConfigValue* value = iter->get(); |
| 209 | if (value->config == config && StringPiece(value->product) == product) { |
| 210 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 211 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | } |
| 213 | ResourceConfigValue* newValue = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 214 | values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | return newValue; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 218 | std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | std::vector<ResourceConfigValue*> results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 220 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 221 | auto iter = values.begin(); |
| 222 | for (; iter != values.end(); ++iter) { |
| 223 | ResourceConfigValue* value = iter->get(); |
| 224 | if (value->config == config) { |
| 225 | results.push_back(value); |
| 226 | ++iter; |
| 227 | break; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 228 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 230 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | for (; iter != values.end(); ++iter) { |
| 232 | ResourceConfigValue* value = iter->get(); |
| 233 | if (value->config == config) { |
| 234 | results.push_back(value); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 235 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 236 | } |
| 237 | return results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 240 | bool ResourceEntry::HasDefaultValue() const { |
| 241 | const ConfigDescription& default_config = ConfigDescription::DefaultConfig(); |
| 242 | |
| 243 | // The default config should be at the top of the list, since the list is sorted. |
| 244 | for (auto& config_value : values) { |
| 245 | if (config_value->config == default_config) { |
| 246 | return true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 247 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | } |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 249 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 252 | // The default handler for collisions. |
| 253 | // |
| 254 | // Typically, a weak value will be overridden by a strong value. An existing weak |
| 255 | // value will not be overridden by an incoming weak value. |
| 256 | // |
| 257 | // There are some exceptions: |
| 258 | // |
| 259 | // Attributes: There are two types of Attribute values: USE and DECL. |
| 260 | // |
| 261 | // USE is anywhere an Attribute is declared without a format, and in a place that would |
| 262 | // be legal to declare if the Attribute already existed. This is typically in a |
| 263 | // <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak. |
| 264 | // |
| 265 | // DECL is an absolute declaration of an Attribute and specifies an explicit format. |
| 266 | // |
| 267 | // A DECL will override a USE without error. Two DECLs must match in their format for there to be |
| 268 | // no error. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 269 | ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing, |
| 270 | Value* incoming) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | Attribute* existing_attr = ValueCast<Attribute>(existing); |
| 272 | Attribute* incoming_attr = ValueCast<Attribute>(incoming); |
| 273 | if (!incoming_attr) { |
| 274 | if (incoming->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | // We're trying to add a weak resource but a resource |
| 276 | // already exists. Keep the existing. |
| 277 | return CollisionResult::kKeepOriginal; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | } else if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | // Override the weak resource with the new strong resource. |
| 280 | return CollisionResult::kTakeNew; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 281 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | // The existing and incoming values are strong, this is an error |
| 283 | // if the values are not both attributes. |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 284 | return CollisionResult::kConflict; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | if (!existing_attr) { |
| 288 | if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | // The existing value is not an attribute and it is weak, |
| 290 | // so take the incoming attribute value. |
| 291 | return CollisionResult::kTakeNew; |
| 292 | } |
| 293 | // The existing value is not an attribute and it is strong, |
| 294 | // so the incoming attribute value is an error. |
| 295 | return CollisionResult::kConflict; |
| 296 | } |
| 297 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | CHECK(incoming_attr != nullptr && existing_attr != nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | |
| 300 | // |
| 301 | // Attribute specific handling. At this point we know both |
| 302 | // values are attributes. Since we can declare and define |
| 303 | // attributes all-over, we do special handling to see |
| 304 | // which definition sticks. |
| 305 | // |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 306 | if (existing_attr->IsCompatibleWith(*incoming_attr)) { |
| 307 | // 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] | 308 | // Keep the strongest one. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 309 | return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 312 | 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] | 313 | // Any incoming attribute is better than this. |
| 314 | return CollisionResult::kTakeNew; |
| 315 | } |
| 316 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 317 | 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] | 318 | // The incoming attribute may be a USE instead of a DECL. |
| 319 | // Keep the existing attribute. |
| 320 | return CollisionResult::kKeepOriginal; |
| 321 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 322 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | return CollisionResult::kConflict; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 326 | ResourceTable::CollisionResult ResourceTable::IgnoreCollision(Value* /** existing **/, |
| 327 | Value* /** incoming **/) { |
| 328 | return CollisionResult::kKeepBoth; |
| 329 | } |
| 330 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 331 | static StringPiece ResourceNameValidator(const StringPiece& name) { |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 332 | if (!IsValidResourceEntryName(name)) { |
| 333 | return name; |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 334 | } |
| 335 | return {}; |
| 336 | } |
| 337 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 338 | static StringPiece SkipNameValidator(const StringPiece& /*name*/) { |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 339 | return {}; |
| 340 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | bool ResourceTable::AddResource(const ResourceNameRef& name, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 343 | const ConfigDescription& config, |
| 344 | const StringPiece& product, |
| 345 | std::unique_ptr<Value> value, |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 346 | IDiagnostics* diag) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 347 | return AddResourceImpl(name, ResourceId{}, config, product, std::move(value), |
| 348 | (validate_resources_ ? ResourceNameValidator : SkipNameValidator), |
| 349 | (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 352 | bool ResourceTable::AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id, |
| 353 | const ConfigDescription& config, const StringPiece& product, |
| 354 | std::unique_ptr<Value> value, IDiagnostics* diag) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 355 | return AddResourceImpl(name, res_id, config, product, std::move(value), |
| 356 | (validate_resources_ ? ResourceNameValidator : SkipNameValidator), |
| 357 | (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 360 | bool ResourceTable::AddResourceMangled(const ResourceNameRef& name, const ConfigDescription& config, |
| 361 | const StringPiece& product, std::unique_ptr<Value> value, |
| 362 | IDiagnostics* diag) { |
| 363 | return AddResourceImpl(name, ResourceId{}, config, product, std::move(value), SkipNameValidator, |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 364 | (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 367 | bool ResourceTable::AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id, |
| 368 | const ConfigDescription& config, |
| 369 | const StringPiece& product, |
| 370 | std::unique_ptr<Value> value, IDiagnostics* diag) { |
| 371 | return AddResourceImpl(name, id, config, product, std::move(value), SkipNameValidator, |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 372 | (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 375 | bool ResourceTable::ValidateName(NameValidator name_validator, const ResourceNameRef& name, |
| 376 | const Source& source, IDiagnostics* diag) { |
| 377 | const StringPiece bad_char = name_validator(name.entry); |
| 378 | if (!bad_char.empty()) { |
| 379 | diag->Error(DiagMessage(source) << "resource '" << name << "' has invalid entry name '" |
| 380 | << name.entry << "'. Invalid character '" << bad_char << "'"); |
| 381 | return false; |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 386 | bool ResourceTable::AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id, |
| 387 | const ConfigDescription& config, const StringPiece& product, |
| 388 | std::unique_ptr<Value> value, NameValidator name_validator, |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 389 | const CollisionResolverFunc& conflict_resolver, |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 390 | IDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 391 | CHECK(value != nullptr); |
| 392 | CHECK(diag != nullptr); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 393 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 394 | const Source& source = value->GetSource(); |
| 395 | if (!ValidateName(name_validator, name, source, diag)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | return false; |
| 397 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 398 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 399 | // Check for package names appearing twice with two different package ids |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 400 | ResourceTablePackage* package = FindOrCreatePackage(name.package); |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 401 | if (res_id.is_valid() && package->id && package->id.value() != res_id.package_id()) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 402 | diag->Error(DiagMessage(source) |
| 403 | << "trying to add resource '" << name << "' with ID " << res_id |
| 404 | << " but package '" << package->name << "' already has ID " |
| 405 | << StringPrintf("%02x", package->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | return false; |
| 407 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 408 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 409 | // Whether or not to error on duplicate resources |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 410 | bool check_id = validate_resources_ && res_id.is_valid(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 411 | // Whether or not to create a duplicate resource if the id does not match |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 412 | bool use_id = !validate_resources_ && res_id.is_valid(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 413 | |
| 414 | ResourceTableType* type = package->FindOrCreateType(name.type, use_id ? res_id.type_id() |
| 415 | : Maybe<uint8_t>()); |
| 416 | |
| 417 | // Check for types appearing twice with two different type ids |
| 418 | if (check_id && type->id && type->id.value() != res_id.type_id()) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 419 | diag->Error(DiagMessage(source) |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 420 | << "trying to add resource '" << name << "' with ID " << res_id |
| 421 | << " but type '" << type->type << "' already has ID " |
| 422 | << StringPrintf("%02x", type->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 423 | return false; |
| 424 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 425 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 426 | ResourceEntry* entry = type->FindOrCreateEntry(name.entry, use_id ? res_id.entry_id() |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 427 | : Maybe<uint16_t>()); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 428 | |
| 429 | // Check for entries appearing twice with two different entry ids |
| 430 | if (check_id && entry->id && entry->id.value() != res_id.entry_id()) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 431 | diag->Error(DiagMessage(source) |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 432 | << "trying to add resource '" << name << "' with ID " << res_id |
| 433 | << " but resource already has ID " |
| 434 | << ResourceId(package->id.value(), type->id.value(), entry->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 435 | return false; |
| 436 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 437 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 438 | ResourceConfigValue* config_value = entry->FindOrCreateValue(config, product); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 439 | if (!config_value->value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 440 | // Resource does not exist, add it now. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | config_value->value = std::move(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 442 | } else { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 443 | switch (conflict_resolver(config_value->value.get(), value.get())) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 444 | case CollisionResult::kKeepBoth: |
| 445 | // Insert the value ignoring for duplicate configurations |
| 446 | entry->values.push_back(util::make_unique<ResourceConfigValue>(config, product)); |
| 447 | entry->values.back()->value = std::move(value); |
| 448 | break; |
| 449 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 450 | case CollisionResult::kTakeNew: |
| 451 | // Take the incoming value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | config_value->value = std::move(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 453 | break; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 454 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 455 | case CollisionResult::kConflict: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 456 | diag->Error(DiagMessage(source) << "duplicate value for resource '" << name << "' " |
| 457 | << "with config '" << config << "'"); |
| 458 | diag->Error(DiagMessage(source) << "resource previously defined here"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 459 | return false; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 460 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 461 | case CollisionResult::kKeepOriginal: |
| 462 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 463 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 464 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 465 | |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 466 | if (res_id.is_valid()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 467 | package->id = res_id.package_id(); |
| 468 | type->id = res_id.type_id(); |
| 469 | entry->id = res_id.entry_id(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 470 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 471 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 475 | bool ResourceTable::GetValidateResources() { |
| 476 | return validate_resources_; |
| 477 | } |
| 478 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 479 | bool ResourceTable::SetVisibility(const ResourceNameRef& name, const Visibility& visibility, |
| 480 | IDiagnostics* diag) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 481 | return SetVisibilityImpl(name, visibility, {}, ResourceNameValidator, diag); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 484 | bool ResourceTable::SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility, |
| 485 | const ResourceId& res_id, IDiagnostics* diag) { |
| 486 | return SetVisibilityImpl(name, visibility, res_id, ResourceNameValidator, diag); |
| 487 | } |
| 488 | |
| 489 | bool ResourceTable::SetVisibilityWithIdMangled(const ResourceNameRef& name, |
| 490 | const Visibility& visibility, |
| 491 | const ResourceId& res_id, IDiagnostics* diag) { |
| 492 | return SetVisibilityImpl(name, visibility, res_id, SkipNameValidator, diag); |
| 493 | } |
| 494 | |
| 495 | bool ResourceTable::SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility, |
| 496 | const ResourceId& res_id, NameValidator name_validator, |
| 497 | IDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 498 | CHECK(diag != nullptr); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 499 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 500 | const Source& source = visibility.source; |
| 501 | if (!ValidateName(name_validator, name, source, diag)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 502 | return false; |
| 503 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 504 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 505 | // Check for package names appearing twice with two different package ids |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 506 | ResourceTablePackage* package = FindOrCreatePackage(name.package); |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 507 | if (res_id.is_valid() && package->id && package->id.value() != res_id.package_id()) { |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 508 | diag->Error(DiagMessage(source) |
| 509 | << "trying to add resource '" << name << "' with ID " << res_id |
| 510 | << " but package '" << package->name << "' already has ID " |
| 511 | << StringPrintf("%02x", package->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | return false; |
| 513 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 514 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 515 | // Whether or not to error on duplicate resources |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 516 | bool check_id = validate_resources_ && res_id.is_valid(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 517 | // Whether or not to create a duplicate resource if the id does not match |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 518 | bool use_id = !validate_resources_ && res_id.is_valid(); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 519 | |
| 520 | ResourceTableType* type = package->FindOrCreateType(name.type, use_id ? res_id.type_id() |
| 521 | : Maybe<uint8_t>()); |
| 522 | |
| 523 | // Check for types appearing twice with two different type ids |
| 524 | if (check_id && type->id && type->id.value() != res_id.type_id()) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 525 | diag->Error(DiagMessage(source) |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 526 | << "trying to add resource '" << name << "' with ID " << res_id |
| 527 | << " but type '" << type->type << "' already has ID " |
| 528 | << StringPrintf("%02x", type->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 529 | return false; |
| 530 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 531 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 532 | ResourceEntry* entry = type->FindOrCreateEntry(name.entry, use_id ? res_id.entry_id() |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 533 | : Maybe<uint16_t>()); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 534 | |
| 535 | // Check for entries appearing twice with two different entry ids |
| 536 | if (check_id && entry->id && entry->id.value() != res_id.entry_id()) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 537 | diag->Error(DiagMessage(source) |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 538 | << "trying to add resource '" << name << "' with ID " << res_id |
| 539 | << " but resource already has ID " |
| 540 | << ResourceId(package->id.value(), type->id.value(), entry->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 541 | return false; |
| 542 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 543 | |
Ryan Mitchell | cd78feb | 2019-12-18 15:20:48 -0800 | [diff] [blame] | 544 | if (res_id.is_valid()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | package->id = res_id.package_id(); |
| 546 | type->id = res_id.type_id(); |
| 547 | entry->id = res_id.entry_id(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 549 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 550 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 551 | if (visibility.level == Visibility::Level::kPublic) { |
| 552 | type->visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 553 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 554 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 555 | if (visibility.level == Visibility::Level::kUndefined && |
| 556 | entry->visibility.level != Visibility::Level::kUndefined) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 557 | // We can't undefine a symbol (remove its visibility). Ignore. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 558 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 561 | if (visibility.level < entry->visibility.level) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 562 | // We can't downgrade public to private. Ignore. |
| 563 | return true; |
| 564 | } |
| 565 | |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 566 | // This symbol definition takes precedence, replace. |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 567 | entry->visibility = visibility; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 568 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 571 | bool ResourceTable::SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, |
| 572 | IDiagnostics* diag) { |
| 573 | return SetAllowNewImpl(name, allow_new, ResourceNameValidator, diag); |
| 574 | } |
| 575 | |
| 576 | bool ResourceTable::SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new, |
| 577 | IDiagnostics* diag) { |
| 578 | return SetAllowNewImpl(name, allow_new, SkipNameValidator, diag); |
| 579 | } |
| 580 | |
| 581 | bool ResourceTable::SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new, |
| 582 | NameValidator name_validator, IDiagnostics* diag) { |
| 583 | CHECK(diag != nullptr); |
| 584 | |
| 585 | if (!ValidateName(name_validator, name, allow_new.source, diag)) { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | ResourceTablePackage* package = FindOrCreatePackage(name.package); |
| 590 | ResourceTableType* type = package->FindOrCreateType(name.type); |
| 591 | ResourceEntry* entry = type->FindOrCreateEntry(name.entry); |
| 592 | entry->allow_new = allow_new; |
| 593 | return true; |
| 594 | } |
| 595 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 596 | bool ResourceTable::SetOverlayable(const ResourceNameRef& name, const OverlayableItem& overlayable, |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 597 | IDiagnostics* diag) { |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 598 | return SetOverlayableImpl(name, overlayable, ResourceNameValidator, diag); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 601 | bool ResourceTable::SetOverlayableImpl(const ResourceNameRef& name, |
| 602 | const OverlayableItem& overlayable, |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 603 | NameValidator name_validator, IDiagnostics *diag) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 604 | CHECK(diag != nullptr); |
| 605 | |
| 606 | if (!ValidateName(name_validator, name, overlayable.source, diag)) { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | ResourceTablePackage* package = FindOrCreatePackage(name.package); |
| 611 | ResourceTableType* type = package->FindOrCreateType(name.type); |
| 612 | ResourceEntry* entry = type->FindOrCreateEntry(name.entry); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 613 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 614 | if (entry->overlayable_item) { |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 615 | diag->Error(DiagMessage(overlayable.source) |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 616 | << "duplicate overlayable declaration for resource '" << name << "'"); |
| 617 | diag->Error(DiagMessage(entry->overlayable_item.value().source) |
| 618 | << "previous declaration here"); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 619 | return false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 620 | } |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 621 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 622 | entry->overlayable_item = overlayable; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 623 | return true; |
| 624 | } |
| 625 | |
| 626 | Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 627 | ResourceTablePackage* package = FindPackage(name.package); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 628 | if (package == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 629 | return {}; |
| 630 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 631 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 632 | ResourceTableType* type = package->FindType(name.type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 633 | if (type == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | return {}; |
| 635 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 636 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 637 | ResourceEntry* entry = type->FindEntry(name.entry); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 638 | if (entry == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 639 | return {}; |
| 640 | } |
| 641 | return SearchResult{package, type, entry}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 644 | std::unique_ptr<ResourceTable> ResourceTable::Clone() const { |
| 645 | std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>(); |
| 646 | for (const auto& pkg : packages) { |
| 647 | ResourceTablePackage* new_pkg = new_table->CreatePackage(pkg->name, pkg->id); |
| 648 | for (const auto& type : pkg->types) { |
| 649 | ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 650 | new_type->id = type->id; |
| 651 | new_type->visibility_level = type->visibility_level; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 652 | |
| 653 | for (const auto& entry : type->entries) { |
| 654 | ResourceEntry* new_entry = new_type->FindOrCreateEntry(entry->name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 655 | new_entry->id = entry->id; |
| 656 | new_entry->visibility = entry->visibility; |
| 657 | new_entry->allow_new = entry->allow_new; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 658 | new_entry->overlayable_item = entry->overlayable_item; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 659 | |
| 660 | for (const auto& config_value : entry->values) { |
| 661 | ResourceConfigValue* new_value = |
| 662 | new_entry->FindOrCreateValue(config_value->config, config_value->product); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 663 | new_value->value.reset(config_value->value->Clone(&new_table->string_pool)); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | return new_table; |
| 669 | } |
| 670 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 671 | } // namespace aapt |