blob: 45ea65430bb687bb903b7033ece3a909fbf79113 [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_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052}
53
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000054template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070055bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
56 return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000057}
58
Ryan Mitchell9634efb2021-03-19 14:53:17 -070059template <typename T>
60struct NameEqualRange {
61 bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
62 return less_than_struct_with_name<T>(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -070064 bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
65 return greater_than_struct_with_name<T>(lhs, rhs);
66 }
67};
68
69template <typename T, typename U>
70bool less_than_struct_with_name_and_id(const T& lhs,
71 const std::pair<std::string_view, Maybe<U>>& rhs) {
72 if (lhs.id != rhs.second) {
73 return lhs.id < rhs.second;
74 }
75 return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076}
77
Ryan Mitchell9634efb2021-03-19 14:53:17 -070078template <typename T, typename Func, typename Elements>
79T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
80 const auto iter =
81 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
82 const bool found = iter != entries.end() && name == (*iter)->name;
83 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
Ryan Mitchell9634efb2021-03-19 14:53:17 -070086} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000087
Ryan Mitchell9634efb2021-03-19 14:53:17 -070088ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000089}
90
Ryan Mitchell9634efb2021-03-19 14:53:17 -070091ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
92 return FindElementsRunAction<ResourceTablePackage>(
93 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094}
95
Ryan Mitchell9634efb2021-03-19 14:53:17 -070096ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
97 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
98 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
99 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100}
101
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700102template <typename Func, typename Elements>
103static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) {
104 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
105 const bool found = iter != entries.end() && type == (*iter)->type;
106 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107}
108
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700109ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const {
110 return FindTypeRunAction(type, types,
111 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112}
113
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700114ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
115 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
116 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
117 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700120ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
121 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
122 return entries.emplace(iter, new ResourceEntry(name))->get();
123 });
124}
125
126ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
127 return FindElementsRunAction<ResourceEntry>(
128 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
129}
130
131ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
132 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
133 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
134 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800135}
136
137struct ConfigKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 const ConfigDescription* config;
139 const StringPiece& product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800140};
141
Adam Lesinski34a16872018-02-23 16:18:10 -0800142bool lt_config_key_ref(const std::unique_ptr<ResourceConfigValue>& lhs, const ConfigKey& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 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 Lesinskie4bb9eb2016-02-12 22:18:51 -0800148}
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700151 android::StringPiece product) {
152 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
153 lt_config_key_ref);
154 if (iter != values.end()) {
155 ResourceConfigValue* value = iter->get();
156 if (value->config == config && StringPiece(value->product) == product) {
157 return value;
158 }
159 }
160 return nullptr;
161}
162
163const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
164 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800165 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
166 lt_config_key_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 if (iter != values.end()) {
168 ResourceConfigValue* value = iter->get();
169 if (value->config == config && StringPiece(value->product) == product) {
170 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800171 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
173 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800174}
175
Adam Lesinskib1afa072017-03-29 13:52:38 -0700176ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
177 const StringPiece& product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800178 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
179 lt_config_key_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 if (iter != values.end()) {
181 ResourceConfigValue* value = iter->get();
182 if (value->config == config && StringPiece(value->product) == product) {
183 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800184 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
186 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700187 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800189}
190
Adam Lesinskib1afa072017-03-29 13:52:38 -0700191std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800193
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 auto iter = values.begin();
195 for (; iter != values.end(); ++iter) {
196 ResourceConfigValue* value = iter->get();
197 if (value->config == config) {
198 results.push_back(value);
199 ++iter;
200 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800201 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 for (; iter != values.end(); ++iter) {
205 ResourceConfigValue* value = iter->get();
206 if (value->config == config) {
207 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800208 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
210 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800211}
212
Adam Lesinski34a16872018-02-23 16:18:10 -0800213bool ResourceEntry::HasDefaultValue() const {
214 const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
215
216 // The default config should be at the top of the list, since the list is sorted.
217 for (auto& config_value : values) {
218 if (config_value->config == default_config) {
219 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700220 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 }
Adam Lesinski34a16872018-02-23 16:18:10 -0800222 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -0700223}
224
Adam Lesinski71be7052017-12-12 16:48:07 -0800225// The default handler for collisions.
226//
227// Typically, a weak value will be overridden by a strong value. An existing weak
228// value will not be overridden by an incoming weak value.
229//
230// There are some exceptions:
231//
232// Attributes: There are two types of Attribute values: USE and DECL.
233//
234// USE is anywhere an Attribute is declared without a format, and in a place that would
235// be legal to declare if the Attribute already existed. This is typically in a
236// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
237//
238// DECL is an absolute declaration of an Attribute and specifies an explicit format.
239//
240// A DECL will override a USE without error. Two DECLs must match in their format for there to be
241// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700242ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
243 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 Attribute* existing_attr = ValueCast<Attribute>(existing);
245 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
246 if (!incoming_attr) {
247 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // We're trying to add a weak resource but a resource
249 // already exists. Keep the existing.
250 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 // Override the weak resource with the new strong resource.
253 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 // The existing and incoming values are strong, this is an error
256 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700257 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 }
259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (!existing_attr) {
261 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 // The existing value is not an attribute and it is weak,
263 // so take the incoming attribute value.
264 return CollisionResult::kTakeNew;
265 }
266 // The existing value is not an attribute and it is strong,
267 // so the incoming attribute value is an error.
268 return CollisionResult::kConflict;
269 }
270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272
273 //
274 // Attribute specific handling. At this point we know both
275 // values are attributes. Since we can declare and define
276 // attributes all-over, we do special handling to see
277 // which definition sticks.
278 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800279 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
280 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700282 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 }
284
Adam Lesinskib1afa072017-03-29 13:52:38 -0700285 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 // Any incoming attribute is better than this.
287 return CollisionResult::kTakeNew;
288 }
289
Adam Lesinskib1afa072017-03-29 13:52:38 -0700290 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 // The incoming attribute may be a USE instead of a DECL.
292 // Keep the existing attribute.
293 return CollisionResult::kKeepOriginal;
294 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700299template <typename T, typename Comparer>
300struct SortedVectorInserter : public Comparer {
301 std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
302 const T& value) {
303 auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
304 return Comparer::operator()(lhs, rhs);
305 });
306 bool found =
307 it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
308 return std::make_pair(found, it);
309 }
310
311 T* Insert(std::vector<T>& el, T&& value) {
312 auto [found, it] = LowerBound(el, value);
313 if (found) {
314 return &*it;
315 }
316 return &*el.insert(it, std::move(value));
317 }
318};
319
320struct PackageViewComparer {
321 bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
322 return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
323 lhs, std::make_pair(rhs.name, rhs.id));
324 }
325};
326
327struct TypeViewComparer {
328 bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
329 return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.type < rhs.type;
330 }
331};
332
333struct EntryViewComparer {
334 bool operator()(const ResourceEntry* lhs, const ResourceEntry* rhs) {
335 return less_than_struct_with_name_and_id<ResourceEntry, ResourceId>(
336 *lhs, std::make_pair(rhs->name, rhs->id));
337 }
338};
339
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700340ResourceTableView ResourceTable::GetPartitionedView() const {
341 ResourceTableView view;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700342 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
343 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
344 SortedVectorInserter<const ResourceEntry*, EntryViewComparer> entry_inserter;
345
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700346 for (const auto& package : packages) {
347 for (const auto& type : package->types) {
348 for (const auto& entry : type->entries) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700349 ResourceTablePackageView new_package{
350 package->name, entry->id ? entry->id.value().package_id() : Maybe<uint8_t>{}};
351 auto view_package = package_inserter.Insert(view.packages, std::move(new_package));
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700352
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700353 ResourceTableTypeView new_type{type->type,
354 entry->id ? entry->id.value().type_id() : Maybe<uint8_t>{}};
355 auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
Adam Lesinski330edcd2015-05-04 17:40:56 -0700356
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700357 if (entry->visibility.level == Visibility::Level::kPublic) {
358 // Only mark the type visibility level as public, it doesn't care about being private.
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700359 view_type->visibility_level = Visibility::Level::kPublic;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700360 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700362 entry_inserter.Insert(view_type->entries, entry.get());
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700363 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800366
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700367 // The android runtime does not support querying resources when the there are multiple type ids
368 // for the same resource type within the same package. For this reason, if there are types with
369 // multiple type ids, each type needs to exist in its own package in order to be queried by name.
370 std::vector<ResourceTablePackageView> new_packages;
371 for (auto& package : view.packages) {
372 // If a new package was already created for a different type within this package, then
373 // we can reuse those packages for other types that need to be extracted from this package.
374 // `start_index` is the index of the first newly created package that can be reused.
375 const size_t start_index = new_packages.size();
376 std::map<ResourceType, size_t> type_new_package_index;
377 for (auto type_it = package.types.begin(); type_it != package.types.end();) {
378 auto& type = *type_it;
379 auto type_index_iter = type_new_package_index.find(type.type);
380 if (type_index_iter == type_new_package_index.end()) {
381 // First occurrence of the resource type in this package. Keep it in this package.
382 type_new_package_index.insert(type_index_iter, std::make_pair(type.type, start_index));
383 ++type_it;
384 continue;
385 }
386
387 // The resource type has already been seen for this package, so this type must be extracted to
388 // a new separate package.
389 const size_t index = type_index_iter->second;
390 if (new_packages.size() == index) {
391 new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
392 type_new_package_index[type.type] = index + 1;
393 }
394
395 // Move the type into a new package
396 auto& other_package = new_packages[index];
397 type_inserter.Insert(other_package.types, std::move(type));
398 type_it = package.types.erase(type_it);
399 }
400 }
401
402 for (auto& new_package : new_packages) {
403 // Insert newly created packages after their original packages
404 auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
405 view.packages.insert(++it, std::move(new_package));
406 }
407
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700408 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409}
410
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700411bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
412 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700413
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700414 const bool validate = validation_ == Validation::kEnabled;
415 const Source source = res.value ? res.value->GetSource() : Source{};
416 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700417 diag->Error(DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700418 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 return false;
420 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700422 if (res.id.has_value() && !res.id->first.is_valid()) {
423 diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
424 << res.id->first << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 return false;
426 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700427
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700428 auto package = FindOrCreatePackage(res.name.package);
429 auto type = package->FindOrCreateType(res.name.type);
430 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
431 NameEqualRange<ResourceEntry>{});
432 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700433
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700434 ResourceEntry* entry;
435 if (entry_count == 0) {
436 // Adding a new resource
437 entry = type->CreateEntry(res.name.entry);
438 } else if (entry_count == 1) {
439 // Assume that the existing resource is being modified
440 entry = entry_it.first->get();
441 } else {
442 // Multiple resources with the same name exist in the resource table. The only way to
443 // distinguish between them is using resource id since each resource should have a unique id.
444 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
445 << "' without specifying a resource id.";
446 entry = entry_it.first->get();
447 for (auto it = entry_it.first; it != entry_it.second; ++it) {
448 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
449 << "' with multiple entries without resource ids";
450 if ((*it)->id == res.id->first) {
451 entry = it->get();
452 break;
453 }
454 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700457 if (res.id.has_value()) {
458 if (entry->id && entry->id.value() != res.id->first) {
459 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
460 diag->Error(DiagMessage(source)
461 << "trying to add resource '" << res.name << "' with ID " << res.id->first
462 << " but resource already has ID " << entry->id.value());
463 return false;
464 }
465 entry = type->CreateEntry(res.name.entry);
466 }
467 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800469
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700470 if (res.visibility.has_value()) {
471 // Only mark the type visibility level as public, it doesn't care about being private.
472 if (res.visibility->level == Visibility::Level::kPublic) {
473 type->visibility_level = Visibility::Level::kPublic;
474 }
475
476 if (res.visibility->level > entry->visibility.level) {
477 // This symbol definition takes precedence, replace.
478 entry->visibility = res.visibility.value();
479 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700480
481 if (res.visibility->staged_api) {
482 entry->visibility.staged_api = entry->visibility.staged_api;
483 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800485
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700486 if (res.overlayable.has_value()) {
487 if (entry->overlayable_item) {
488 diag->Error(DiagMessage(res.overlayable->source)
489 << "duplicate overlayable declaration for resource '" << res.name << "'");
490 diag->Error(DiagMessage(entry->overlayable_item.value().source)
491 << "previous declaration here");
492 return false;
493 }
494 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 }
496
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700497 if (res.allow_new.has_value()) {
498 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 }
500
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700501 if (res.value != nullptr) {
502 auto config_value = entry->FindOrCreateValue(res.config, res.product);
503 if (!config_value->value) {
504 // Resource does not exist, add it now.
505 config_value->value = std::move(res.value);
506 } else {
507 // When validation is enabled, ensure that a resource cannot have multiple values defined for
508 // the same configuration.
509 auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
510 : CollisionResult::kKeepBoth;
511 switch (result) {
512 case CollisionResult::kKeepBoth:
513 // Insert the value ignoring for duplicate configurations
514 entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
515 entry->values.back()->value = std::move(res.value);
516 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700518 case CollisionResult::kTakeNew:
519 // Take the incoming value.
520 config_value->value = std::move(res.value);
521 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800522
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700523 case CollisionResult::kConflict:
524 diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
525 << "with config '" << res.config << "'");
526 diag->Error(DiagMessage(source) << "resource previously defined here");
527 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800528
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700529 case CollisionResult::kKeepOriginal:
530 break;
531 }
532 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800533 }
534
Adam Lesinski71be7052017-12-12 16:48:07 -0800535 return true;
536}
537
538Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800540 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 return {};
542 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800545 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 return {};
547 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800550 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return {};
552 }
553 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800554}
555
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700556Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
557 ResourceId id) const {
558 ResourceTablePackage* package = FindPackage(name.package);
559 if (package == nullptr) {
560 return {};
561 }
562
563 ResourceTableType* type = package->FindType(name.type);
564 if (type == nullptr) {
565 return {};
566 }
567
568 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
569 NameEqualRange<ResourceEntry>{});
570 for (auto it = entry_it.first; it != entry_it.second; ++it) {
571 if ((*it)->id == id) {
572 return SearchResult{package, type, it->get()};
573 }
574 }
575 return {};
576}
577
Shane Farmer0a5b2012017-06-22 12:24:12 -0700578std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
579 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700580 CloningValueTransformer cloner(&new_table->string_pool);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700581 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700582 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700583 for (const auto& type : pkg->types) {
584 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800585 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700586
587 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700588 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800589 new_entry->id = entry->id;
590 new_entry->visibility = entry->visibility;
591 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800592 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700593
594 for (const auto& config_value : entry->values) {
595 ResourceConfigValue* new_value =
596 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700597 new_value->value = config_value->value->Transform(cloner);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700598 }
599 }
600 }
601 }
602 return new_table;
603}
604
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700605NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
606 res_.name = name.ToResourceName();
607}
608
609NewResourceBuilder::NewResourceBuilder(const std::string& name) {
610 ResourceNameRef ref;
611 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
612 res_.name = ref.ToResourceName();
613}
614
615NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
616 android::ConfigDescription config,
617 std::string product) {
618 res_.value = std::move(value);
619 res_.config = std::move(config);
620 res_.product = std::move(product);
621 return *this;
622}
623
624NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
625 res_.id = std::make_pair(id, on_conflict);
626 return *this;
627}
628
629NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
630 res_.visibility = std::move(visibility);
631 return *this;
632}
633
634NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
635 res_.overlayable = std::move(overlayable);
636 return *this;
637}
638NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
639 res_.allow_new = std::move(allow_new);
640 return *this;
641}
642
643NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
644 res_.allow_mangled = allow_mangled;
645 return *this;
646}
647
648NewResource NewResourceBuilder::Build() {
649 return std::move(res_);
650}
651
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652} // namespace aapt