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