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