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