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