blob: 9905f827d663b894d6bbd8547a1c19511d7dc81f [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>
21#include <string>
22#include <tuple>
23
Adam Lesinski66ea8402017-06-28 11:44:11 -070024#include "android-base/logging.h"
Adam Lesinski71be7052017-12-12 16:48:07 -080025#include "android-base/stringprintf.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070026#include "androidfw/ResourceTypes.h"
27
28#include "ConfigDescription.h"
29#include "NameMangler.h"
30#include "ResourceValues.h"
31#include "ValueVisitor.h"
32#include "text/Unicode.h"
33#include "util/Util.h"
34
35using ::aapt::text::IsValidResourceEntryName;
36using ::android::StringPiece;
Adam Lesinski71be7052017-12-12 16:48:07 -080037using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080038
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039namespace aapt {
40
Adam Lesinskib1afa072017-03-29 13:52:38 -070041static bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 return lhs->type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045template <typename T>
Adam Lesinskib1afa072017-03-29 13:52:38 -070046static bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
Adam Lesinski71be7052017-12-12 16:48:07 -080050ResourceTablePackage* ResourceTable::FindPackage(const StringPiece& name) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 const auto last = packages.end();
Adam Lesinskib1afa072017-03-29 13:52:38 -070052 auto iter = std::lower_bound(packages.begin(), last, name,
53 less_than_struct_with_name<ResourceTablePackage>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 if (iter != last && name == (*iter)->name) {
55 return iter->get();
56 }
57 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058}
59
Adam Lesinski71be7052017-12-12 16:48:07 -080060ResourceTablePackage* ResourceTable::FindPackageById(uint8_t id) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 for (auto& package : packages) {
62 if (package->id && package->id.value() == id) {
63 return package.get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 }
66 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067}
68
Adam Lesinskib1afa072017-03-29 13:52:38 -070069ResourceTablePackage* ResourceTable::CreatePackage(const StringPiece& name, Maybe<uint8_t> id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 ResourceTablePackage* package = FindOrCreatePackage(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 if (id && !package->id) {
72 package->id = id;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070073 return package;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 }
75
76 if (id && package->id && package->id.value() != id.value()) {
77 return nullptr;
78 }
79 return package;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080}
81
Adam Lesinskib1afa072017-03-29 13:52:38 -070082ResourceTablePackage* ResourceTable::FindOrCreatePackage(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 const auto last = packages.end();
Adam Lesinskib1afa072017-03-29 13:52:38 -070084 auto iter = std::lower_bound(packages.begin(), last, name,
85 less_than_struct_with_name<ResourceTablePackage>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 if (iter != last && name == (*iter)->name) {
87 return iter->get();
88 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskib1afa072017-03-29 13:52:38 -070090 std::unique_ptr<ResourceTablePackage> new_package = util::make_unique<ResourceTablePackage>();
Adam Lesinskid5083f62017-01-16 15:07:21 -080091 new_package->name = name.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 return packages.emplace(iter, std::move(new_package))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093}
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095ResourceTableType* ResourceTablePackage::FindType(ResourceType type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 const auto last = types.end();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 auto iter = std::lower_bound(types.begin(), last, type, less_than_type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 if (iter != last && (*iter)->type == type) {
99 return iter->get();
100 }
101 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102}
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 const auto last = types.end();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 auto iter = std::lower_bound(types.begin(), last, type, less_than_type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 if (iter != last && (*iter)->type == type) {
108 return iter->get();
109 }
110 return types.emplace(iter, new ResourceTableType(type))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111}
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113ResourceEntry* ResourceTableType::FindEntry(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 const auto last = entries.end();
Adam Lesinskib1afa072017-03-29 13:52:38 -0700115 auto iter =
116 std::lower_bound(entries.begin(), last, name, less_than_struct_with_name<ResourceEntry>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 if (iter != last && name == (*iter)->name) {
118 return iter->get();
119 }
120 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121}
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123ResourceEntry* ResourceTableType::FindOrCreateEntry(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 auto last = entries.end();
Adam Lesinskib1afa072017-03-29 13:52:38 -0700125 auto iter =
126 std::lower_bound(entries.begin(), last, name, less_than_struct_with_name<ResourceEntry>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 if (iter != last && name == (*iter)->name) {
128 return iter->get();
129 }
130 return entries.emplace(iter, new ResourceEntry(name))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config) {
134 return FindValue(config, StringPiece());
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 Lesinskib1afa072017-03-29 13:52:38 -0700142bool ltConfigKeyRef(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,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800151 const StringPiece& product) {
Adam Lesinskib1afa072017-03-29 13:52:38 -0700152 auto iter =
153 std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, ltConfigKeyRef);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 if (iter != values.end()) {
155 ResourceConfigValue* value = iter->get();
156 if (value->config == config && StringPiece(value->product) == product) {
157 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800158 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
160 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800161}
162
Adam Lesinskib1afa072017-03-29 13:52:38 -0700163ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
164 const StringPiece& product) {
165 auto iter =
166 std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, ltConfigKeyRef);
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 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700174 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800176}
177
Adam Lesinskib1afa072017-03-29 13:52:38 -0700178std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 auto iter = values.begin();
182 for (; iter != values.end(); ++iter) {
183 ResourceConfigValue* value = iter->get();
184 if (value->config == config) {
185 results.push_back(value);
186 ++iter;
187 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800188 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 for (; iter != values.end(); ++iter) {
192 ResourceConfigValue* value = iter->get();
193 if (value->config == config) {
194 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800195 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800198}
199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200std::vector<ResourceConfigValue*> ResourceEntry::FindValuesIf(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 const std::function<bool(ResourceConfigValue*)>& f) {
202 std::vector<ResourceConfigValue*> results;
203 for (auto& configValue : values) {
204 if (f(configValue.get())) {
205 results.push_back(configValue.get());
Adam Lesinski458b8772016-04-25 14:20:21 -0700206 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
208 return results;
Adam Lesinski458b8772016-04-25 14:20:21 -0700209}
210
Adam Lesinski71be7052017-12-12 16:48:07 -0800211// The default handler for collisions.
212//
213// Typically, a weak value will be overridden by a strong value. An existing weak
214// value will not be overridden by an incoming weak value.
215//
216// There are some exceptions:
217//
218// Attributes: There are two types of Attribute values: USE and DECL.
219//
220// USE is anywhere an Attribute is declared without a format, and in a place that would
221// be legal to declare if the Attribute already existed. This is typically in a
222// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
223//
224// DECL is an absolute declaration of an Attribute and specifies an explicit format.
225//
226// A DECL will override a USE without error. Two DECLs must match in their format for there to be
227// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700228ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
229 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 Attribute* existing_attr = ValueCast<Attribute>(existing);
231 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
232 if (!incoming_attr) {
233 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 // We're trying to add a weak resource but a resource
235 // already exists. Keep the existing.
236 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // Override the weak resource with the new strong resource.
239 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 // The existing and incoming values are strong, this is an error
242 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700243 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 }
245
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 if (!existing_attr) {
247 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // The existing value is not an attribute and it is weak,
249 // so take the incoming attribute value.
250 return CollisionResult::kTakeNew;
251 }
252 // The existing value is not an attribute and it is strong,
253 // so the incoming attribute value is an error.
254 return CollisionResult::kConflict;
255 }
256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258
259 //
260 // Attribute specific handling. At this point we know both
261 // values are attributes. Since we can declare and define
262 // attributes all-over, we do special handling to see
263 // which definition sticks.
264 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800265 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
266 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700268 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 }
270
Adam Lesinskib1afa072017-03-29 13:52:38 -0700271 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 // Any incoming attribute is better than this.
273 return CollisionResult::kTakeNew;
274 }
275
Adam Lesinskib1afa072017-03-29 13:52:38 -0700276 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The incoming attribute may be a USE instead of a DECL.
278 // Keep the existing attribute.
279 return CollisionResult::kKeepOriginal;
280 }
281 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
Adam Lesinski71be7052017-12-12 16:48:07 -0800284static StringPiece ResourceNameValidator(const StringPiece& name) {
Adam Lesinski66ea8402017-06-28 11:44:11 -0700285 if (!IsValidResourceEntryName(name)) {
286 return name;
Adam Lesinskib1afa072017-03-29 13:52:38 -0700287 }
288 return {};
289}
290
Adam Lesinski71be7052017-12-12 16:48:07 -0800291static StringPiece SkipNameValidator(const StringPiece& /*name*/) {
Adam Lesinskib1afa072017-03-29 13:52:38 -0700292 return {};
293}
Adam Lesinski330edcd2015-05-04 17:40:56 -0700294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295bool ResourceTable::AddResource(const ResourceNameRef& name,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800296 const ConfigDescription& config,
297 const StringPiece& product,
298 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700299 IDiagnostics* diag) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800300 return AddResourceImpl(name, {}, config, product, std::move(value), ResourceNameValidator,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700301 ResolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700302}
303
Adam Lesinski71be7052017-12-12 16:48:07 -0800304bool ResourceTable::AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id,
305 const ConfigDescription& config, const StringPiece& product,
306 std::unique_ptr<Value> value, IDiagnostics* diag) {
307 return AddResourceImpl(name, res_id, config, product, std::move(value), ResourceNameValidator,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700308 ResolveValueCollision, diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311bool ResourceTable::AddFileReference(const ResourceNameRef& name,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800312 const ConfigDescription& config,
313 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700314 const StringPiece& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315 IDiagnostics* diag) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800316 return AddFileReferenceImpl(name, config, source, path, nullptr, ResourceNameValidator, diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800317}
318
Adam Lesinski71be7052017-12-12 16:48:07 -0800319bool ResourceTable::AddFileReferenceMangled(const ResourceNameRef& name,
320 const ConfigDescription& config, const Source& source,
321 const StringPiece& path, io::IFile* file,
322 IDiagnostics* diag) {
323 return AddFileReferenceImpl(name, config, source, path, file, SkipNameValidator, diag);
Adam Lesinski355f2852016-02-13 20:26:45 -0800324}
325
Adam Lesinskib1afa072017-03-29 13:52:38 -0700326bool ResourceTable::AddFileReferenceImpl(const ResourceNameRef& name,
327 const ConfigDescription& config, const Source& source,
328 const StringPiece& path, io::IFile* file,
329 NameValidator name_validator, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 std::unique_ptr<FileReference> fileRef =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 util::make_unique<FileReference>(string_pool.MakeRef(path));
332 fileRef->SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 fileRef->file = file;
Adam Lesinskib1afa072017-03-29 13:52:38 -0700334 return AddResourceImpl(name, ResourceId{}, config, StringPiece{}, std::move(fileRef),
335 name_validator, ResolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700336}
337
Adam Lesinski71be7052017-12-12 16:48:07 -0800338bool ResourceTable::AddResourceMangled(const ResourceNameRef& name, const ConfigDescription& config,
339 const StringPiece& product, std::unique_ptr<Value> value,
340 IDiagnostics* diag) {
341 return AddResourceImpl(name, ResourceId{}, config, product, std::move(value), SkipNameValidator,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700342 ResolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700343}
344
Adam Lesinski71be7052017-12-12 16:48:07 -0800345bool ResourceTable::AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id,
346 const ConfigDescription& config,
347 const StringPiece& product,
348 std::unique_ptr<Value> value, IDiagnostics* diag) {
349 return AddResourceImpl(name, id, config, product, std::move(value), SkipNameValidator,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700350 ResolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700351}
352
Adam Lesinski71be7052017-12-12 16:48:07 -0800353bool ResourceTable::ValidateName(NameValidator name_validator, const ResourceNameRef& name,
354 const Source& source, IDiagnostics* diag) {
355 const StringPiece bad_char = name_validator(name.entry);
356 if (!bad_char.empty()) {
357 diag->Error(DiagMessage(source) << "resource '" << name << "' has invalid entry name '"
358 << name.entry << "'. Invalid character '" << bad_char << "'");
359 return false;
360 }
361 return true;
362}
363
Adam Lesinskib1afa072017-03-29 13:52:38 -0700364bool ResourceTable::AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id,
365 const ConfigDescription& config, const StringPiece& product,
366 std::unique_ptr<Value> value, NameValidator name_validator,
Adam Lesinski71be7052017-12-12 16:48:07 -0800367 const CollisionResolverFunc& conflict_resolver,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700368 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 CHECK(value != nullptr);
370 CHECK(diag != nullptr);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700371
Adam Lesinski71be7052017-12-12 16:48:07 -0800372 const Source& source = value->GetSource();
373 if (!ValidateName(name_validator, name, source, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 return false;
375 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 ResourceTablePackage* package = FindOrCreatePackage(name.package);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800378 if (res_id.is_valid_dynamic() && package->id && package->id.value() != res_id.package_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800379 diag->Error(DiagMessage(source) << "trying to add resource '" << name << "' with ID " << res_id
380 << " but package '" << package->name << "' already has ID "
381 << StringPrintf("%02x", package->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 return false;
383 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 ResourceTableType* type = package->FindOrCreateType(name.type);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800386 if (res_id.is_valid_dynamic() && type->id && type->id.value() != res_id.type_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800387 diag->Error(DiagMessage(source)
388 << "trying to add resource '" << name << "' with ID " << res_id << " but type '"
389 << type->type << "' already has ID " << StringPrintf("%02x", type->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 return false;
391 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 ResourceEntry* entry = type->FindOrCreateEntry(name.entry);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800394 if (res_id.is_valid_dynamic() && entry->id && entry->id.value() != res_id.entry_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800395 diag->Error(DiagMessage(source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 << "trying to add resource '" << name << "' with ID " << res_id
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 << " but resource already has ID "
Adam Lesinski71be7052017-12-12 16:48:07 -0800398 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 return false;
400 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 ResourceConfigValue* config_value = entry->FindOrCreateValue(config, product);
Adam Lesinski71be7052017-12-12 16:48:07 -0800403 if (config_value->value == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 // Resource does not exist, add it now.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 config_value->value = std::move(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 } else {
Adam Lesinski71be7052017-12-12 16:48:07 -0800407 switch (conflict_resolver(config_value->value.get(), value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 case CollisionResult::kTakeNew:
409 // Take the incoming value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 config_value->value = std::move(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800412
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 case CollisionResult::kConflict:
Adam Lesinski71be7052017-12-12 16:48:07 -0800414 diag->Error(DiagMessage(source) << "duplicate value for resource '" << name << "' "
415 << "with config '" << config << "'");
416 diag->Error(DiagMessage(source) << "resource previously defined here");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 return false;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700418
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 case CollisionResult::kKeepOriginal:
420 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800424 if (res_id.is_valid_dynamic()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 package->id = res_id.package_id();
426 type->id = res_id.type_id();
427 entry->id = res_id.entry_id();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 }
429 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430}
431
Adam Lesinski71be7052017-12-12 16:48:07 -0800432bool ResourceTable::SetVisibility(const ResourceNameRef& name, const Visibility& visibility,
433 IDiagnostics* diag) {
434 return SetVisibilityImpl(name, visibility, ResourceId{}, ResourceNameValidator, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700435}
436
Adam Lesinski71be7052017-12-12 16:48:07 -0800437bool ResourceTable::SetVisibilityMangled(const ResourceNameRef& name, const Visibility& visibility,
438 IDiagnostics* diag) {
439 return SetVisibilityImpl(name, visibility, ResourceId{}, SkipNameValidator, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700440}
441
Adam Lesinski71be7052017-12-12 16:48:07 -0800442bool ResourceTable::SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility,
443 const ResourceId& res_id, IDiagnostics* diag) {
444 return SetVisibilityImpl(name, visibility, res_id, ResourceNameValidator, diag);
445}
446
447bool ResourceTable::SetVisibilityWithIdMangled(const ResourceNameRef& name,
448 const Visibility& visibility,
449 const ResourceId& res_id, IDiagnostics* diag) {
450 return SetVisibilityImpl(name, visibility, res_id, SkipNameValidator, diag);
451}
452
453bool ResourceTable::SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility,
454 const ResourceId& res_id, NameValidator name_validator,
455 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 CHECK(diag != nullptr);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700457
Adam Lesinski71be7052017-12-12 16:48:07 -0800458 const Source& source = visibility.source;
459 if (!ValidateName(name_validator, name, source, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 return false;
461 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 ResourceTablePackage* package = FindOrCreatePackage(name.package);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800464 if (res_id.is_valid_dynamic() && package->id && package->id.value() != res_id.package_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800465 diag->Error(DiagMessage(source) << "trying to add resource '" << name << "' with ID " << res_id
466 << " but package '" << package->name << "' already has ID "
467 << StringPrintf("%02x", package->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 return false;
469 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 ResourceTableType* type = package->FindOrCreateType(name.type);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800472 if (res_id.is_valid_dynamic() && type->id && type->id.value() != res_id.type_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800473 diag->Error(DiagMessage(source)
474 << "trying to add resource '" << name << "' with ID " << res_id << " but type '"
475 << type->type << "' already has ID " << StringPrintf("%02x", type->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 return false;
477 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 ResourceEntry* entry = type->FindOrCreateEntry(name.entry);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800480 if (res_id.is_valid_dynamic() && entry->id && entry->id.value() != res_id.entry_id()) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800481 diag->Error(DiagMessage(source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 << "trying to add resource '" << name << "' with ID " << res_id
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 << " but resource already has ID "
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700484 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 return false;
486 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800488 if (res_id.is_valid_dynamic()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 package->id = res_id.package_id();
490 type->id = res_id.type_id();
491 entry->id = res_id.entry_id();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800493
Adam Lesinski71be7052017-12-12 16:48:07 -0800494 // Only mark the type visibility level as public, it doesn't care about being private.
495 if (visibility.level == Visibility::Level::kPublic) {
496 type->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800498
Adam Lesinski71be7052017-12-12 16:48:07 -0800499 if (visibility.level == Visibility::Level::kUndefined &&
500 entry->visibility.level != Visibility::Level::kUndefined) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 // We can't undefine a symbol (remove its visibility). Ignore.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 }
504
Adam Lesinski71be7052017-12-12 16:48:07 -0800505 if (visibility.level < entry->visibility.level) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 // We can't downgrade public to private. Ignore.
507 return true;
508 }
509
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700510 // This symbol definition takes precedence, replace.
Adam Lesinski71be7052017-12-12 16:48:07 -0800511 entry->visibility = visibility;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513}
514
Adam Lesinski71be7052017-12-12 16:48:07 -0800515bool ResourceTable::SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new,
516 IDiagnostics* diag) {
517 return SetAllowNewImpl(name, allow_new, ResourceNameValidator, diag);
518}
519
520bool ResourceTable::SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new,
521 IDiagnostics* diag) {
522 return SetAllowNewImpl(name, allow_new, SkipNameValidator, diag);
523}
524
525bool ResourceTable::SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new,
526 NameValidator name_validator, IDiagnostics* diag) {
527 CHECK(diag != nullptr);
528
529 if (!ValidateName(name_validator, name, allow_new.source, diag)) {
530 return false;
531 }
532
533 ResourceTablePackage* package = FindOrCreatePackage(name.package);
534 ResourceTableType* type = package->FindOrCreateType(name.type);
535 ResourceEntry* entry = type->FindOrCreateEntry(name.entry);
536 entry->allow_new = allow_new;
537 return true;
538}
539
540bool ResourceTable::SetOverlayable(const ResourceNameRef& name, const Overlayable& overlayable,
541 IDiagnostics* diag) {
542 return SetOverlayableImpl(name, overlayable, ResourceNameValidator, diag);
543}
544
545bool ResourceTable::SetOverlayableMangled(const ResourceNameRef& name,
546 const Overlayable& overlayable, IDiagnostics* diag) {
547 return SetOverlayableImpl(name, overlayable, SkipNameValidator, diag);
548}
549
550bool ResourceTable::SetOverlayableImpl(const ResourceNameRef& name, const Overlayable& overlayable,
551 NameValidator name_validator, IDiagnostics* diag) {
552 CHECK(diag != nullptr);
553
554 if (!ValidateName(name_validator, name, overlayable.source, diag)) {
555 return false;
556 }
557
558 ResourceTablePackage* package = FindOrCreatePackage(name.package);
559 ResourceTableType* type = package->FindOrCreateType(name.type);
560 ResourceEntry* entry = type->FindOrCreateEntry(name.entry);
561 if (entry->overlayable) {
562 diag->Error(DiagMessage(overlayable.source)
563 << "duplicate overlayable declaration for resource '" << name << "'");
564 diag->Error(DiagMessage(entry->overlayable.value().source) << "previous declaration here");
565 return false;
566 }
567 entry->overlayable = overlayable;
568 return true;
569}
570
571Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800573 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 return {};
575 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800578 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return {};
580 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800583 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 return {};
585 }
586 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587}
588
Shane Farmer0a5b2012017-06-22 12:24:12 -0700589std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
590 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
591 for (const auto& pkg : packages) {
592 ResourceTablePackage* new_pkg = new_table->CreatePackage(pkg->name, pkg->id);
593 for (const auto& type : pkg->types) {
594 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800595 new_type->id = type->id;
596 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700597
598 for (const auto& entry : type->entries) {
599 ResourceEntry* new_entry = new_type->FindOrCreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800600 new_entry->id = entry->id;
601 new_entry->visibility = entry->visibility;
602 new_entry->allow_new = entry->allow_new;
603 new_entry->overlayable = entry->overlayable;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700604
605 for (const auto& config_value : entry->values) {
606 ResourceConfigValue* new_value =
607 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Adam Lesinski71be7052017-12-12 16:48:07 -0800608 new_value->value.reset(config_value->value->Clone(&new_table->string_pool));
Shane Farmer0a5b2012017-06-22 12:24:12 -0700609 }
610 }
611 }
612 }
613 return new_table;
614}
615
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616} // namespace aapt