blob: c52c91c0832f1731682e8e6572b8a8f7ce19d14a [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#include "ConfigDescription.h"
Adam Lesinski769de982015-04-10 19:43:55 -070019#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "ValueVisitor.h"
22#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <androidfw/ResourceTypes.h>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include <memory>
27#include <string>
28#include <tuple>
29
30namespace aapt {
31
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032static bool lessThanType(const std::unique_ptr<ResourceTableType>& lhs,
33 ResourceType rhs) {
34 return lhs->type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035}
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037template <typename T>
38static bool lessThanStructWithName(const std::unique_ptr<T>& lhs,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070039 const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041}
42
Adam Lesinskid0f116b2016-07-08 15:00:32 -070043ResourceTablePackage* ResourceTable::findPackage(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 const auto last = packages.end();
45 auto iter = std::lower_bound(packages.begin(), last, name,
46 lessThanStructWithName<ResourceTablePackage>);
47 if (iter != last && name == (*iter)->name) {
48 return iter->get();
49 }
50 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051}
52
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053ResourceTablePackage* ResourceTable::findPackageById(uint8_t id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 for (auto& package : packages) {
55 if (package->id && package->id.value() == id) {
56 return package.get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
59 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060}
61
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062ResourceTablePackage* ResourceTable::createPackage(const StringPiece& name,
63 Maybe<uint8_t> id) {
64 ResourceTablePackage* package = findOrCreatePackage(name);
65 if (id && !package->id) {
66 package->id = id;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070067 return package;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
69
70 if (id && package->id && package->id.value() != id.value()) {
71 return nullptr;
72 }
73 return package;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074}
75
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076ResourceTablePackage* ResourceTable::findOrCreatePackage(
77 const StringPiece& name) {
78 const auto last = packages.end();
79 auto iter = std::lower_bound(packages.begin(), last, name,
80 lessThanStructWithName<ResourceTablePackage>);
81 if (iter != last && name == (*iter)->name) {
82 return iter->get();
83 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 std::unique_ptr<ResourceTablePackage> newPackage =
86 util::make_unique<ResourceTablePackage>();
87 newPackage->name = name.toString();
88 return packages.emplace(iter, std::move(newPackage))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089}
90
91ResourceTableType* ResourceTablePackage::findType(ResourceType type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 const auto last = types.end();
93 auto iter = std::lower_bound(types.begin(), last, type, lessThanType);
94 if (iter != last && (*iter)->type == type) {
95 return iter->get();
96 }
97 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098}
99
100ResourceTableType* ResourceTablePackage::findOrCreateType(ResourceType type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 const auto last = types.end();
102 auto iter = std::lower_bound(types.begin(), last, type, lessThanType);
103 if (iter != last && (*iter)->type == type) {
104 return iter->get();
105 }
106 return types.emplace(iter, new ResourceTableType(type))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107}
108
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700109ResourceEntry* ResourceTableType::findEntry(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 const auto last = entries.end();
111 auto iter = std::lower_bound(entries.begin(), last, name,
112 lessThanStructWithName<ResourceEntry>);
113 if (iter != last && name == (*iter)->name) {
114 return iter->get();
115 }
116 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117}
118
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700119ResourceEntry* ResourceTableType::findOrCreateEntry(const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 auto last = entries.end();
121 auto iter = std::lower_bound(entries.begin(), last, name,
122 lessThanStructWithName<ResourceEntry>);
123 if (iter != last && name == (*iter)->name) {
124 return iter->get();
125 }
126 return entries.emplace(iter, new ResourceEntry(name))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800129ResourceConfigValue* ResourceEntry::findValue(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 return findValue(config, StringPiece());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800131}
132
133struct ConfigKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 const ConfigDescription* config;
135 const StringPiece& product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800136};
137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138bool ltConfigKeyRef(const std::unique_ptr<ResourceConfigValue>& lhs,
139 const ConfigKey& rhs) {
140 int cmp = lhs->config.compare(*rhs.config);
141 if (cmp == 0) {
142 cmp = StringPiece(lhs->product).compare(rhs.product);
143 }
144 return cmp < 0;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800145}
146
147ResourceConfigValue* ResourceEntry::findValue(const ConfigDescription& config,
148 const StringPiece& product) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 auto iter = std::lower_bound(values.begin(), values.end(),
150 ConfigKey{&config, product}, ltConfigKeyRef);
151 if (iter != values.end()) {
152 ResourceConfigValue* value = iter->get();
153 if (value->config == config && StringPiece(value->product) == product) {
154 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800155 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800158}
159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160ResourceConfigValue* ResourceEntry::findOrCreateValue(
161 const ConfigDescription& config, const StringPiece& product) {
162 auto iter = std::lower_bound(values.begin(), values.end(),
163 ConfigKey{&config, product}, ltConfigKeyRef);
164 if (iter != values.end()) {
165 ResourceConfigValue* value = iter->get();
166 if (value->config == config && StringPiece(value->product) == product) {
167 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800168 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 }
170 ResourceConfigValue* newValue =
171 values
172 .insert(iter, util::make_unique<ResourceConfigValue>(config, product))
173 ->get();
174 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800175}
176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177std::vector<ResourceConfigValue*> ResourceEntry::findAllValues(
178 const ConfigDescription& config) {
179 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 Lesinski458b8772016-04-25 14:20:21 -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 Lesinski6f6ceb72014-11-14 14:48:12 -0800211/**
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700212 * The default handler for collisions.
Adam Lesinski8197cc462016-08-19 12:16:49 -0700213 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 * Typically, a weak value will be overridden by a strong value. An existing
215 * weak
Adam Lesinski8197cc462016-08-19 12:16:49 -0700216 * value will not be overridden by an incoming weak value.
217 *
218 * There are some exceptions:
219 *
220 * Attributes: There are two types of Attribute values: USE and DECL.
221 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 * USE is anywhere an Attribute is declared without a format, and in a place
223 * that would
Adam Lesinski8197cc462016-08-19 12:16:49 -0700224 * be legal to declare if the Attribute already existed. This is typically in a
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 * <declare-styleable> tag. Attributes defined in a <declare-styleable> are also
226 * weak.
Adam Lesinski8197cc462016-08-19 12:16:49 -0700227 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 * DECL is an absolute declaration of an Attribute and specifies an explicit
229 * format.
Adam Lesinski8197cc462016-08-19 12:16:49 -0700230 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 * A DECL will override a USE without error. Two DECLs must match in their
232 * format for there to be
Adam Lesinski8197cc462016-08-19 12:16:49 -0700233 * no error.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 */
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700235ResourceTable::CollisionResult ResourceTable::resolveValueCollision(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 Value* existing, Value* incoming) {
237 Attribute* existingAttr = valueCast<Attribute>(existing);
238 Attribute* incomingAttr = valueCast<Attribute>(incoming);
239 if (!incomingAttr) {
240 if (incoming->isWeak()) {
241 // We're trying to add a weak resource but a resource
242 // already exists. Keep the existing.
243 return CollisionResult::kKeepOriginal;
244 } else if (existing->isWeak()) {
245 // Override the weak resource with the new strong resource.
246 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // The existing and incoming values are strong, this is an error
249 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700250 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 }
252
253 if (!existingAttr) {
254 if (existing->isWeak()) {
255 // The existing value is not an attribute and it is weak,
256 // so take the incoming attribute value.
257 return CollisionResult::kTakeNew;
258 }
259 // The existing value is not an attribute and it is strong,
260 // so the incoming attribute value is an error.
261 return CollisionResult::kConflict;
262 }
263
264 assert(incomingAttr && existingAttr);
265
266 //
267 // Attribute specific handling. At this point we know both
268 // values are attributes. Since we can declare and define
269 // attributes all-over, we do special handling to see
270 // which definition sticks.
271 //
272 if (existingAttr->typeMask == incomingAttr->typeMask) {
273 // The two attributes are both DECLs, but they are plain attributes
274 // with the same formats.
275 // Keep the strongest one.
276 return existingAttr->isWeak() ? CollisionResult::kTakeNew
277 : CollisionResult::kKeepOriginal;
278 }
279
280 if (existingAttr->isWeak() &&
281 existingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
282 // Any incoming attribute is better than this.
283 return CollisionResult::kTakeNew;
284 }
285
286 if (incomingAttr->isWeak() &&
287 incomingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
288 // The incoming attribute may be a USE instead of a DECL.
289 // Keep the existing attribute.
290 return CollisionResult::kKeepOriginal;
291 }
292 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700295static constexpr const char* kValidNameChars = "._-";
296static constexpr const char* kValidNameMangledChars = "._-$";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700297
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800298bool ResourceTable::addResource(const ResourceNameRef& name,
299 const ConfigDescription& config,
300 const StringPiece& product,
301 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700302 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return addResourceImpl(name, {}, config, product, std::move(value),
304 kValidNameChars, resolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305}
306
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800307bool ResourceTable::addResource(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700308 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800309 const ConfigDescription& config,
310 const StringPiece& product,
311 std::unique_ptr<Value> value,
312 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return addResourceImpl(name, resId, config, product, std::move(value),
314 kValidNameChars, resolveValueCollision, diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800315}
316
317bool ResourceTable::addFileReference(const ResourceNameRef& name,
318 const ConfigDescription& config,
319 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700320 const StringPiece& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 return addFileReferenceImpl(name, config, source, path, nullptr,
323 kValidNameChars, diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800324}
325
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326bool ResourceTable::addFileReferenceAllowMangled(
327 const ResourceNameRef& name, const ConfigDescription& config,
328 const Source& source, const StringPiece& path, io::IFile* file,
329 IDiagnostics* diag) {
330 return addFileReferenceImpl(name, config, source, path, file,
331 kValidNameMangledChars, diag);
Adam Lesinski355f2852016-02-13 20:26:45 -0800332}
333
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334bool ResourceTable::addFileReferenceImpl(
335 const ResourceNameRef& name, const ConfigDescription& config,
336 const Source& source, const StringPiece& path, io::IFile* file,
337 const char* validChars, IDiagnostics* diag) {
338 std::unique_ptr<FileReference> fileRef =
339 util::make_unique<FileReference>(stringPool.makeRef(path));
340 fileRef->setSource(source);
341 fileRef->file = file;
342 return addResourceImpl(name, ResourceId{}, config, StringPiece{},
343 std::move(fileRef), validChars, resolveValueCollision,
344 diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700345}
346
347bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
348 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800349 const StringPiece& product,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350 std::unique_ptr<Value> value,
351 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 return addResourceImpl(name, ResourceId{}, config, product, std::move(value),
353 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700354}
355
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700356bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700357 const ResourceId& id,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700358 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800359 const StringPiece& product,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700360 std::unique_ptr<Value> value,
361 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 return addResourceImpl(name, id, config, product, std::move(value),
363 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700364}
365
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366bool ResourceTable::addResourceImpl(
367 const ResourceNameRef& name, const ResourceId& resId,
368 const ConfigDescription& config, const StringPiece& product,
369 std::unique_ptr<Value> value, const char* validChars,
370 const CollisionResolverFunc& conflictResolver, IDiagnostics* diag) {
371 assert(value && "value can't be nullptr");
372 assert(diag && "diagnostics can't be nullptr");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700373
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 auto badCharIter =
375 util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
376 if (badCharIter != name.entry.end()) {
377 diag->error(DiagMessage(value->getSource())
378 << "resource '" << name << "' has invalid entry name '"
379 << name.entry << "'. Invalid character '"
380 << StringPiece(badCharIter, 1) << "'");
381 return false;
382 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 ResourceTablePackage* package = findOrCreatePackage(name.package);
385 if (resId.isValid() && package->id &&
386 package->id.value() != resId.packageId()) {
387 diag->error(DiagMessage(value->getSource())
388 << "trying to add resource '" << name << "' with ID " << resId
389 << " but package '" << package->name << "' already has ID "
390 << std::hex << (int)package->id.value() << std::dec);
391 return false;
392 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800393
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 ResourceTableType* type = package->findOrCreateType(name.type);
395 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
396 diag->error(DiagMessage(value->getSource())
397 << "trying to add resource '" << name << "' with ID " << resId
398 << " but type '" << type->type << "' already has ID "
399 << std::hex << (int)type->id.value() << std::dec);
400 return false;
401 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
404 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
405 diag->error(DiagMessage(value->getSource())
406 << "trying to add resource '" << name << "' with ID " << resId
407 << " but resource already has ID "
408 << ResourceId(package->id.value(), type->id.value(),
409 entry->id.value()));
410 return false;
411 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700412
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 ResourceConfigValue* configValue = entry->findOrCreateValue(config, product);
414 if (!configValue->value) {
415 // Resource does not exist, add it now.
416 configValue->value = std::move(value);
417
418 } else {
419 switch (conflictResolver(configValue->value.get(), value.get())) {
420 case CollisionResult::kTakeNew:
421 // Take the incoming value.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800422 configValue->value = std::move(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800424
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 case CollisionResult::kConflict:
426 diag->error(DiagMessage(value->getSource())
427 << "duplicate value for resource '" << name << "' "
428 << "with config '" << config << "'");
429 diag->error(DiagMessage(configValue->value->getSource())
430 << "resource previously defined here");
431 return false;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700432
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 case CollisionResult::kKeepOriginal:
434 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 if (resId.isValid()) {
439 package->id = resId.packageId();
440 type->id = resId.typeId();
441 entry->id = resId.entryId();
442 }
443 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444}
445
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446bool ResourceTable::setSymbolState(const ResourceNameRef& name,
447 const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700448 const Symbol& symbol, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 return setSymbolStateImpl(name, resId, symbol, kValidNameChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700450}
451
Adam Lesinskie78fd612015-10-22 12:48:43 -0700452bool ResourceTable::setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700453 const ResourceId& resId,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 const Symbol& symbol,
455 IDiagnostics* diag) {
456 return setSymbolStateImpl(name, resId, symbol, kValidNameMangledChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700457}
458
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459bool ResourceTable::setSymbolStateImpl(const ResourceNameRef& name,
460 const ResourceId& resId,
461 const Symbol& symbol,
462 const char* validChars,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700463 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 assert(diag && "diagnostics can't be nullptr");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700465
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 auto badCharIter =
467 util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
468 if (badCharIter != name.entry.end()) {
469 diag->error(DiagMessage(symbol.source)
470 << "resource '" << name << "' has invalid entry name '"
471 << name.entry << "'. Invalid character '"
472 << StringPiece(badCharIter, 1) << "'");
473 return false;
474 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 ResourceTablePackage* package = findOrCreatePackage(name.package);
477 if (resId.isValid() && package->id &&
478 package->id.value() != resId.packageId()) {
479 diag->error(DiagMessage(symbol.source)
480 << "trying to add resource '" << name << "' with ID " << resId
481 << " but package '" << package->name << "' already has ID "
482 << std::hex << (int)package->id.value() << std::dec);
483 return false;
484 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 ResourceTableType* type = package->findOrCreateType(name.type);
487 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
488 diag->error(DiagMessage(symbol.source)
489 << "trying to add resource '" << name << "' with ID " << resId
490 << " but type '" << type->type << "' already has ID "
491 << std::hex << (int)type->id.value() << std::dec);
492 return false;
493 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
496 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
497 diag->error(DiagMessage(symbol.source)
498 << "trying to add resource '" << name << "' with ID " << resId
499 << " but resource already has ID "
500 << ResourceId(package->id.value(), type->id.value(),
501 entry->id.value()));
502 return false;
503 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 if (resId.isValid()) {
506 package->id = resId.packageId();
507 type->id = resId.typeId();
508 entry->id = resId.entryId();
509 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800510
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 // Only mark the type state as public, it doesn't care about being private.
512 if (symbol.state == SymbolState::kPublic) {
513 type->symbolStatus.state = SymbolState::kPublic;
514 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 if (symbol.state == SymbolState::kUndefined &&
517 entry->symbolStatus.state != SymbolState::kUndefined) {
518 // We can't undefine a symbol (remove its visibility). Ignore.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800519 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 }
521
522 if (symbol.state == SymbolState::kPrivate &&
523 entry->symbolStatus.state == SymbolState::kPublic) {
524 // We can't downgrade public to private. Ignore.
525 return true;
526 }
527
528 entry->symbolStatus = std::move(symbol);
529 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800530}
531
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532Maybe<ResourceTable::SearchResult> ResourceTable::findResource(
533 const ResourceNameRef& name) {
534 ResourceTablePackage* package = findPackage(name.package);
535 if (!package) {
536 return {};
537 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 ResourceTableType* type = package->findType(name.type);
540 if (!type) {
541 return {};
542 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 ResourceEntry* entry = type->findEntry(name.entry);
545 if (!entry) {
546 return {};
547 }
548 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549}
550
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551} // namespace aapt