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