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