blob: cff98728c325e1abc4334ca241a94d92019a0a37 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceTable.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <memory>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <tuple>
22
Adam Lesinski66ea8402017-06-28 11:44:11 -070023#include "android-base/logging.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020024#include "androidfw/ConfigDescription.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070025#include "androidfw/ResourceTypes.h"
26
Adam Lesinski66ea8402017-06-28 11:44:11 -070027#include "NameMangler.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070028#include "ResourceUtils.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070029#include "ResourceValues.h"
30#include "ValueVisitor.h"
31#include "text/Unicode.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070032#include "trace/TraceBuffer.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070033#include "util/Util.h"
34
35using ::aapt::text::IsValidResourceEntryName;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020036using ::android::ConfigDescription;
Adam Lesinski66ea8402017-06-28 11:44:11 -070037using ::android::StringPiece;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Ryan Mitchell54237ff2018-12-13 15:44:29 -080042const char* Overlayable::kActorScheme = "overlay";
43
Ryan Mitchell9634efb2021-03-19 14:53:17 -070044namespace {
45bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
46 return lhs->type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070050bool less_than_type_and_id(const T& lhs, const std::pair<ResourceType, Maybe<uint8_t>>& rhs) {
51 return lhs.id != rhs.second ? lhs.id < rhs.second : lhs.type < rhs.first;
52}
53
54template <typename T>
55bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057}
58
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000059template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070060bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
61 return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000062}
63
Ryan Mitchell9634efb2021-03-19 14:53:17 -070064template <typename T>
65struct NameEqualRange {
66 bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
67 return less_than_struct_with_name<T>(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -070069 bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
70 return greater_than_struct_with_name<T>(lhs, rhs);
71 }
72};
73
74template <typename T, typename U>
75bool less_than_struct_with_name_and_id(const T& lhs,
76 const std::pair<std::string_view, Maybe<U>>& rhs) {
77 if (lhs.id != rhs.second) {
78 return lhs.id < rhs.second;
79 }
80 return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081}
82
Ryan Mitchell9634efb2021-03-19 14:53:17 -070083template <typename T, typename U>
84bool less_than_struct_with_name_and_id_pointer(const T* lhs,
85 const std::pair<std::string_view, Maybe<U>>& rhs) {
86 return less_than_struct_with_name_and_id(*lhs, rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087}
88
Ryan Mitchell9634efb2021-03-19 14:53:17 -070089template <typename T, typename Func, typename Elements>
90T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
91 const auto iter =
92 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
93 const bool found = iter != entries.end() && name == (*iter)->name;
94 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095}
96
Ryan Mitchell9634efb2021-03-19 14:53:17 -070097} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000098
Ryan Mitchell9634efb2021-03-19 14:53:17 -070099ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000100}
101
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700102ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
103 return FindElementsRunAction<ResourceTablePackage>(
104 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105}
106
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700107ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
108 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
109 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
110 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111}
112
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700113template <typename Func, typename Elements>
114static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) {
115 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
116 const bool found = iter != entries.end() && type == (*iter)->type;
117 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118}
119
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700120ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const {
121 return FindTypeRunAction(type, types,
122 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123}
124
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700125ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
126 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
127 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
128 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700131ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
132 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
133 return entries.emplace(iter, new ResourceEntry(name))->get();
134 });
135}
136
137ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
138 return FindElementsRunAction<ResourceEntry>(
139 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
140}
141
142ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
143 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
144 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
145 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800146}
147
148struct ConfigKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 const ConfigDescription* config;
150 const StringPiece& product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800151};
152
Adam Lesinski34a16872018-02-23 16:18:10 -0800153bool lt_config_key_ref(const std::unique_ptr<ResourceConfigValue>& lhs, const ConfigKey& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 int cmp = lhs->config.compare(*rhs.config);
155 if (cmp == 0) {
156 cmp = StringPiece(lhs->product).compare(rhs.product);
157 }
158 return cmp < 0;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800159}
160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700162 android::StringPiece product) {
163 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
164 lt_config_key_ref);
165 if (iter != values.end()) {
166 ResourceConfigValue* value = iter->get();
167 if (value->config == config && StringPiece(value->product) == product) {
168 return value;
169 }
170 }
171 return nullptr;
172}
173
174const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
175 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800176 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
177 lt_config_key_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 if (iter != values.end()) {
179 ResourceConfigValue* value = iter->get();
180 if (value->config == config && StringPiece(value->product) == product) {
181 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800182 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 }
184 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800185}
186
Adam Lesinskib1afa072017-03-29 13:52:38 -0700187ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
188 const StringPiece& product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800189 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
190 lt_config_key_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 if (iter != values.end()) {
192 ResourceConfigValue* value = iter->get();
193 if (value->config == config && StringPiece(value->product) == product) {
194 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800195 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700198 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800200}
201
Adam Lesinskib1afa072017-03-29 13:52:38 -0700202std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 auto iter = values.begin();
206 for (; iter != values.end(); ++iter) {
207 ResourceConfigValue* value = iter->get();
208 if (value->config == config) {
209 results.push_back(value);
210 ++iter;
211 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800212 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 for (; iter != values.end(); ++iter) {
216 ResourceConfigValue* value = iter->get();
217 if (value->config == config) {
218 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800219 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 }
221 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800222}
223
Adam Lesinski34a16872018-02-23 16:18:10 -0800224bool ResourceEntry::HasDefaultValue() const {
225 const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
226
227 // The default config should be at the top of the list, since the list is sorted.
228 for (auto& config_value : values) {
229 if (config_value->config == default_config) {
230 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700231 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
Adam Lesinski34a16872018-02-23 16:18:10 -0800233 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -0700234}
235
Adam Lesinski71be7052017-12-12 16:48:07 -0800236// The default handler for collisions.
237//
238// Typically, a weak value will be overridden by a strong value. An existing weak
239// value will not be overridden by an incoming weak value.
240//
241// There are some exceptions:
242//
243// Attributes: There are two types of Attribute values: USE and DECL.
244//
245// USE is anywhere an Attribute is declared without a format, and in a place that would
246// be legal to declare if the Attribute already existed. This is typically in a
247// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
248//
249// DECL is an absolute declaration of an Attribute and specifies an explicit format.
250//
251// A DECL will override a USE without error. Two DECLs must match in their format for there to be
252// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700253ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
254 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 Attribute* existing_attr = ValueCast<Attribute>(existing);
256 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
257 if (!incoming_attr) {
258 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 // We're trying to add a weak resource but a resource
260 // already exists. Keep the existing.
261 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 // Override the weak resource with the new strong resource.
264 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 // The existing and incoming values are strong, this is an error
267 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700268 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 }
270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (!existing_attr) {
272 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 // The existing value is not an attribute and it is weak,
274 // so take the incoming attribute value.
275 return CollisionResult::kTakeNew;
276 }
277 // The existing value is not an attribute and it is strong,
278 // so the incoming attribute value is an error.
279 return CollisionResult::kConflict;
280 }
281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283
284 //
285 // Attribute specific handling. At this point we know both
286 // values are attributes. Since we can declare and define
287 // attributes all-over, we do special handling to see
288 // which definition sticks.
289 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800290 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
291 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700293 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 }
295
Adam Lesinskib1afa072017-03-29 13:52:38 -0700296 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 // Any incoming attribute is better than this.
298 return CollisionResult::kTakeNew;
299 }
300
Adam Lesinskib1afa072017-03-29 13:52:38 -0700301 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 // The incoming attribute may be a USE instead of a DECL.
303 // Keep the existing attribute.
304 return CollisionResult::kKeepOriginal;
305 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700306
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308}
309
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700310ResourceTableView ResourceTable::GetPartitionedView() const {
311 ResourceTableView view;
312 for (const auto& package : packages) {
313 for (const auto& type : package->types) {
314 for (const auto& entry : type->entries) {
315 std::pair<std::string_view, Maybe<uint8_t>> package_key(package->name, {});
316 std::pair<std::string_view, Maybe<ResourceId>> entry_key(entry->name, {});
317 std::pair<ResourceType, Maybe<uint8_t>> type_key(type->type, {});
318 if (entry->id) {
319 // If the entry has a defined id, use the id to determine insertion position.
320 package_key.second = entry->id.value().package_id();
321 type_key.second = entry->id.value().type_id();
322 entry_key.second = entry->id.value();
323 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700324
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700325 auto package_it =
326 std::lower_bound(view.packages.begin(), view.packages.end(), package_key,
327 less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>);
328 if (package_it == view.packages.end() || package_it->name != package_key.first ||
329 package_it->id != package_key.second) {
330 ResourceTablePackageView new_package{std::string(package_key.first), package_key.second};
331 package_it = view.packages.insert(package_it, new_package);
332 }
Adam Lesinskib1afa072017-03-29 13:52:38 -0700333
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700334 auto type_it = std::lower_bound(package_it->types.begin(), package_it->types.end(),
335 type_key, less_than_type_and_id<ResourceTableTypeView>);
336 if (type_it == package_it->types.end() || type_key.first != type_it->type ||
337 type_it->id != type_key.second) {
338 ResourceTableTypeView new_type{type_key.first, type_key.second};
339 type_it = package_it->types.insert(type_it, new_type);
340 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700341
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700342 if (entry->visibility.level == Visibility::Level::kPublic) {
343 // Only mark the type visibility level as public, it doesn't care about being private.
344 type_it->visibility_level = Visibility::Level::kPublic;
345 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700347 auto entry_it =
348 std::lower_bound(type_it->entries.begin(), type_it->entries.end(), entry_key,
349 less_than_struct_with_name_and_id_pointer<ResourceEntry, ResourceId>);
350 type_it->entries.insert(entry_it, entry.get());
351 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700355 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356}
357
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700358bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
359 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700360
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700361 const bool validate = validation_ == Validation::kEnabled;
362 const Source source = res.value ? res.value->GetSource() : Source{};
363 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700364 diag->Error(DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700365 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return false;
367 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700369 if (res.id.has_value() && !res.id->first.is_valid()) {
370 diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
371 << res.id->first << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 return false;
373 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700375 auto package = FindOrCreatePackage(res.name.package);
376 auto type = package->FindOrCreateType(res.name.type);
377 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
378 NameEqualRange<ResourceEntry>{});
379 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700380
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700381 ResourceEntry* entry;
382 if (entry_count == 0) {
383 // Adding a new resource
384 entry = type->CreateEntry(res.name.entry);
385 } else if (entry_count == 1) {
386 // Assume that the existing resource is being modified
387 entry = entry_it.first->get();
388 } else {
389 // Multiple resources with the same name exist in the resource table. The only way to
390 // distinguish between them is using resource id since each resource should have a unique id.
391 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
392 << "' without specifying a resource id.";
393 entry = entry_it.first->get();
394 for (auto it = entry_it.first; it != entry_it.second; ++it) {
395 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
396 << "' with multiple entries without resource ids";
397 if ((*it)->id == res.id->first) {
398 entry = it->get();
399 break;
400 }
401 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700404 if (res.id.has_value()) {
405 if (entry->id && entry->id.value() != res.id->first) {
406 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
407 diag->Error(DiagMessage(source)
408 << "trying to add resource '" << res.name << "' with ID " << res.id->first
409 << " but resource already has ID " << entry->id.value());
410 return false;
411 }
412 entry = type->CreateEntry(res.name.entry);
413 }
414 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800416
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700417 if (res.visibility.has_value()) {
418 // Only mark the type visibility level as public, it doesn't care about being private.
419 if (res.visibility->level == Visibility::Level::kPublic) {
420 type->visibility_level = Visibility::Level::kPublic;
421 }
422
423 if (res.visibility->level > entry->visibility.level) {
424 // This symbol definition takes precedence, replace.
425 entry->visibility = res.visibility.value();
426 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800428
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700429 if (res.overlayable.has_value()) {
430 if (entry->overlayable_item) {
431 diag->Error(DiagMessage(res.overlayable->source)
432 << "duplicate overlayable declaration for resource '" << res.name << "'");
433 diag->Error(DiagMessage(entry->overlayable_item.value().source)
434 << "previous declaration here");
435 return false;
436 }
437 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 }
439
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700440 if (res.allow_new.has_value()) {
441 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 }
443
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700444 if (res.value != nullptr) {
445 auto config_value = entry->FindOrCreateValue(res.config, res.product);
446 if (!config_value->value) {
447 // Resource does not exist, add it now.
448 config_value->value = std::move(res.value);
449 } else {
450 // When validation is enabled, ensure that a resource cannot have multiple values defined for
451 // the same configuration.
452 auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
453 : CollisionResult::kKeepBoth;
454 switch (result) {
455 case CollisionResult::kKeepBoth:
456 // Insert the value ignoring for duplicate configurations
457 entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
458 entry->values.back()->value = std::move(res.value);
459 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700461 case CollisionResult::kTakeNew:
462 // Take the incoming value.
463 config_value->value = std::move(res.value);
464 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800465
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700466 case CollisionResult::kConflict:
467 diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
468 << "with config '" << res.config << "'");
469 diag->Error(DiagMessage(source) << "resource previously defined here");
470 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800471
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700472 case CollisionResult::kKeepOriginal:
473 break;
474 }
475 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800476 }
477
Adam Lesinski71be7052017-12-12 16:48:07 -0800478 return true;
479}
480
481Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800483 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 return {};
485 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800488 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 return {};
490 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800493 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 return {};
495 }
496 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497}
498
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700499Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
500 ResourceId id) const {
501 ResourceTablePackage* package = FindPackage(name.package);
502 if (package == nullptr) {
503 return {};
504 }
505
506 ResourceTableType* type = package->FindType(name.type);
507 if (type == nullptr) {
508 return {};
509 }
510
511 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
512 NameEqualRange<ResourceEntry>{});
513 for (auto it = entry_it.first; it != entry_it.second; ++it) {
514 if ((*it)->id == id) {
515 return SearchResult{package, type, it->get()};
516 }
517 }
518 return {};
519}
520
Shane Farmer0a5b2012017-06-22 12:24:12 -0700521std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
522 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
523 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700524 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700525 for (const auto& type : pkg->types) {
526 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800527 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700528
529 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700530 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800531 new_entry->id = entry->id;
532 new_entry->visibility = entry->visibility;
533 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800534 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700535
536 for (const auto& config_value : entry->values) {
537 ResourceConfigValue* new_value =
538 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Adam Lesinski71be7052017-12-12 16:48:07 -0800539 new_value->value.reset(config_value->value->Clone(&new_table->string_pool));
Shane Farmer0a5b2012017-06-22 12:24:12 -0700540 }
541 }
542 }
543 }
544 return new_table;
545}
546
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700547NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
548 res_.name = name.ToResourceName();
549}
550
551NewResourceBuilder::NewResourceBuilder(const std::string& name) {
552 ResourceNameRef ref;
553 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
554 res_.name = ref.ToResourceName();
555}
556
557NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
558 android::ConfigDescription config,
559 std::string product) {
560 res_.value = std::move(value);
561 res_.config = std::move(config);
562 res_.product = std::move(product);
563 return *this;
564}
565
566NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
567 res_.id = std::make_pair(id, on_conflict);
568 return *this;
569}
570
571NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
572 res_.visibility = std::move(visibility);
573 return *this;
574}
575
576NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
577 res_.overlayable = std::move(overlayable);
578 return *this;
579}
580NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
581 res_.allow_new = std::move(allow_new);
582 return *this;
583}
584
585NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
586 res_.allow_mangled = allow_mangled;
587 return *this;
588}
589
590NewResource NewResourceBuilder::Build() {
591 return std::move(res_);
592}
593
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594} // namespace aapt