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