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