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