blob: ae5d299678850153cb52693b6a72086cc9ed0caa [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
17#include "ConfigDescription.h"
Adam Lesinski769de982015-04-10 19:43:55 -070018#include "NameMangler.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "ResourceTable.h"
20#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
24#include <algorithm>
25#include <androidfw/ResourceTypes.h>
26#include <memory>
27#include <string>
28#include <tuple>
29
30namespace aapt {
31
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032static bool lessThanType(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
33 return lhs->type < rhs;
34}
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036template <typename T>
37static bool lessThanStructWithName(const std::unique_ptr<T>& lhs,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070038 const StringPiece& rhs) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
40}
41
Adam Lesinskid0f116b2016-07-08 15:00:32 -070042ResourceTablePackage* ResourceTable::findPackage(const StringPiece& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043 const auto last = packages.end();
44 auto iter = std::lower_bound(packages.begin(), last, name,
45 lessThanStructWithName<ResourceTablePackage>);
46 if (iter != last && name == (*iter)->name) {
47 return iter->get();
48 }
49 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052ResourceTablePackage* ResourceTable::findPackageById(uint8_t id) {
53 for (auto& package : packages) {
54 if (package->id && package->id.value() == id) {
55 return package.get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 }
57 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058 return nullptr;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059}
60
Adam Lesinskid0f116b2016-07-08 15:00:32 -070061ResourceTablePackage* ResourceTable::createPackage(const StringPiece& name, Maybe<uint8_t> id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 ResourceTablePackage* package = findOrCreatePackage(name);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070063 if (id && !package->id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 package->id = id;
65 return package;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinski9ba47d82015-10-13 11:37:10 -070068 if (id && package->id && package->id.value() != id.value()) {
69 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -070071 return package;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072}
73
Adam Lesinskid0f116b2016-07-08 15:00:32 -070074ResourceTablePackage* ResourceTable::findOrCreatePackage(const StringPiece& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 const auto last = packages.end();
76 auto iter = std::lower_bound(packages.begin(), last, name,
77 lessThanStructWithName<ResourceTablePackage>);
78 if (iter != last && name == (*iter)->name) {
79 return iter->get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080 }
81
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 std::unique_ptr<ResourceTablePackage> newPackage = util::make_unique<ResourceTablePackage>();
83 newPackage->name = name.toString();
84 return packages.emplace(iter, std::move(newPackage))->get();
85}
86
87ResourceTableType* ResourceTablePackage::findType(ResourceType type) {
88 const auto last = types.end();
89 auto iter = std::lower_bound(types.begin(), last, type, lessThanType);
90 if (iter != last && (*iter)->type == type) {
91 return iter->get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 return nullptr;
94}
95
96ResourceTableType* ResourceTablePackage::findOrCreateType(ResourceType type) {
97 const auto last = types.end();
98 auto iter = std::lower_bound(types.begin(), last, type, lessThanType);
99 if (iter != last && (*iter)->type == type) {
100 return iter->get();
101 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800102 return types.emplace(iter, new ResourceTableType(type))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103}
104
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700105ResourceEntry* ResourceTableType::findEntry(const StringPiece& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 const auto last = entries.end();
107 auto iter = std::lower_bound(entries.begin(), last, name,
108 lessThanStructWithName<ResourceEntry>);
109 if (iter != last && name == (*iter)->name) {
110 return iter->get();
111 }
112 return nullptr;
113}
114
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700115ResourceEntry* ResourceTableType::findOrCreateEntry(const StringPiece& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 auto last = entries.end();
117 auto iter = std::lower_bound(entries.begin(), last, name,
118 lessThanStructWithName<ResourceEntry>);
119 if (iter != last && name == (*iter)->name) {
120 return iter->get();
121 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122 return entries.emplace(iter, new ResourceEntry(name))->get();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800125ResourceConfigValue* ResourceEntry::findValue(const ConfigDescription& config) {
126 return findValue(config, StringPiece());
127}
128
129struct ConfigKey {
130 const ConfigDescription* config;
131 const StringPiece& product;
132};
133
134bool ltConfigKeyRef(const std::unique_ptr<ResourceConfigValue>& lhs, const ConfigKey& rhs) {
135 int cmp = lhs->config.compare(*rhs.config);
136 if (cmp == 0) {
137 cmp = StringPiece(lhs->product).compare(rhs.product);
138 }
139 return cmp < 0;
140}
141
142ResourceConfigValue* ResourceEntry::findValue(const ConfigDescription& config,
143 const StringPiece& product) {
144 auto iter = std::lower_bound(values.begin(), values.end(),
145 ConfigKey{ &config, product }, ltConfigKeyRef);
146 if (iter != values.end()) {
147 ResourceConfigValue* value = iter->get();
148 if (value->config == config && StringPiece(value->product) == product) {
149 return value;
150 }
151 }
152 return nullptr;
153}
154
155ResourceConfigValue* ResourceEntry::findOrCreateValue(const ConfigDescription& config,
156 const StringPiece& product) {
157 auto iter = std::lower_bound(values.begin(), values.end(),
158 ConfigKey{ &config, product }, ltConfigKeyRef);
159 if (iter != values.end()) {
160 ResourceConfigValue* value = iter->get();
161 if (value->config == config && StringPiece(value->product) == product) {
162 return value;
163 }
164 }
165 ResourceConfigValue* newValue = values.insert(
166 iter, util::make_unique<ResourceConfigValue>(config, product))->get();
167 return newValue;
168}
169
170std::vector<ResourceConfigValue*> ResourceEntry::findAllValues(const ConfigDescription& config) {
171 std::vector<ResourceConfigValue*> results;
172
173 auto iter = values.begin();
174 for (; iter != values.end(); ++iter) {
175 ResourceConfigValue* value = iter->get();
176 if (value->config == config) {
177 results.push_back(value);
178 ++iter;
179 break;
180 }
181 }
182
183 for (; iter != values.end(); ++iter) {
184 ResourceConfigValue* value = iter->get();
185 if (value->config == config) {
186 results.push_back(value);
187 }
188 }
189 return results;
190}
191
Adam Lesinski458b8772016-04-25 14:20:21 -0700192std::vector<ResourceConfigValue*> ResourceEntry::findValuesIf(
193 const std::function<bool(ResourceConfigValue*)>& f) {
194 std::vector<ResourceConfigValue*> results;
195 for (auto& configValue : values) {
196 if (f(configValue.get())) {
197 results.push_back(configValue.get());
198 }
199 }
200 return results;
201}
202
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203/**
204 * The default handler for collisions. A return value of -1 means keep the
205 * existing value, 0 means fail, and +1 means take the incoming value.
Adam Lesinski8197cc462016-08-19 12:16:49 -0700206 *
207 * Typically, a weak value will be overridden by a strong value. An existing weak
208 * value will not be overridden by an incoming weak value.
209 *
210 * There are some exceptions:
211 *
212 * Attributes: There are two types of Attribute values: USE and DECL.
213 *
214 * USE is anywhere an Attribute is declared without a format, and in a place that would
215 * be legal to declare if the Attribute already existed. This is typically in a
216 * <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
217 *
218 * DECL is an absolute declaration of an Attribute and specifies an explicit format.
219 *
220 * A DECL will override a USE without error. Two DECLs must match in their format for there to be
221 * no error.
222 *
223 * Styleables: Styleables are not actual resources, but they are treated as such during the
224 * compilation phase. Styleables are allowed to override each other, and their definitions merge
225 * and accumulate. If both values are Styleables, we just merge them into the existing value.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227int ResourceTable::resolveValueCollision(Value* existing, Value* incoming) {
Adam Lesinski8197cc462016-08-19 12:16:49 -0700228 if (Styleable* existingStyleable = valueCast<Styleable>(existing)) {
229 if (Styleable* incomingStyleable = valueCast<Styleable>(incoming)) {
230 // Styleables get merged.
231 existingStyleable->mergeWith(incomingStyleable);
232 return -1;
233 }
234 }
235
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236 Attribute* existingAttr = valueCast<Attribute>(existing);
237 Attribute* incomingAttr = valueCast<Attribute>(incoming);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238 if (!incomingAttr) {
239 if (incoming->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240 // We're trying to add a weak resource but a resource
241 // already exists. Keep the existing.
242 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243 } else if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 // Override the weak resource with the new strong resource.
245 return 1;
246 }
247 // The existing and incoming values are strong, this is an error
248 // if the values are not both attributes.
249 return 0;
250 }
251
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 if (!existingAttr) {
253 if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 // The existing value is not an attribute and it is weak,
255 // so take the incoming attribute value.
256 return 1;
257 }
258 // The existing value is not an attribute and it is strong,
259 // so the incoming attribute value is an error.
260 return 0;
261 }
262
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263 assert(incomingAttr && existingAttr);
264
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 //
266 // Attribute specific handling. At this point we know both
267 // values are attributes. Since we can declare and define
268 // attributes all-over, we do special handling to see
269 // which definition sticks.
270 //
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 if (existingAttr->typeMask == incomingAttr->typeMask) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272 // The two attributes are both DECLs, but they are plain attributes
273 // with the same formats.
274 // Keep the strongest one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700275 return existingAttr->isWeak() ? 1 : -1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276 }
277
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278 if (existingAttr->isWeak() && existingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279 // Any incoming attribute is better than this.
280 return 1;
281 }
282
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 if (incomingAttr->isWeak() && incomingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284 // The incoming attribute may be a USE instead of a DECL.
285 // Keep the existing attribute.
286 return -1;
287 }
288 return 0;
289}
290
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700291static constexpr const char* kValidNameChars = "._-";
292static constexpr const char* kValidNameMangledChars = "._-$";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700293
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800294bool ResourceTable::addResource(const ResourceNameRef& name,
295 const ConfigDescription& config,
296 const StringPiece& product,
297 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700298 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800299 return addResourceImpl(name, {}, config, product, std::move(value), kValidNameChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800300 resolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301}
302
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800303bool ResourceTable::addResource(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700304 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800305 const ConfigDescription& config,
306 const StringPiece& product,
307 std::unique_ptr<Value> value,
308 IDiagnostics* diag) {
309 return addResourceImpl(name, resId, config, product, std::move(value), kValidNameChars,
310 resolveValueCollision, diag);
311}
312
313bool ResourceTable::addFileReference(const ResourceNameRef& name,
314 const ConfigDescription& config,
315 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700316 const StringPiece& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 IDiagnostics* diag) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800318 return addFileReferenceImpl(name, config, source, path, nullptr, kValidNameChars, diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800319}
320
Adam Lesinski355f2852016-02-13 20:26:45 -0800321bool ResourceTable::addFileReferenceAllowMangled(const ResourceNameRef& name,
322 const ConfigDescription& config,
323 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700324 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800325 io::IFile* file,
326 IDiagnostics* diag) {
327 return addFileReferenceImpl(name, config, source, path, file, kValidNameMangledChars, diag);
328}
329
330bool ResourceTable::addFileReferenceImpl(const ResourceNameRef& name,
331 const ConfigDescription& config,
332 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700333 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800334 io::IFile* file,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700335 const char* validChars,
Adam Lesinski355f2852016-02-13 20:26:45 -0800336 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700337 std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>(
338 stringPool.makeRef(path));
339 fileRef->setSource(source);
Adam Lesinski355f2852016-02-13 20:26:45 -0800340 fileRef->file = file;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800341 return addResourceImpl(name, ResourceId{}, config, StringPiece{}, std::move(fileRef),
Adam Lesinski355f2852016-02-13 20:26:45 -0800342 kValidNameChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700343}
344
345bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
346 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800347 const StringPiece& product,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 std::unique_ptr<Value> value,
349 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800350 return addResourceImpl(name, ResourceId{}, config, product, std::move(value),
351 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700352}
353
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700354bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700355 const ResourceId& id,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700356 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800357 const StringPiece& product,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700358 std::unique_ptr<Value> value,
359 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800360 return addResourceImpl(name, id, config, product, std::move(value), kValidNameMangledChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800361 resolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700362}
363
Adam Lesinskifb48d292015-11-07 15:52:13 -0800364bool ResourceTable::addResourceImpl(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700365 const ResourceId& resId,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800366 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800367 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800368 std::unique_ptr<Value> value,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700369 const char* validChars,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700370 const std::function<int(Value*,Value*)>& conflictResolver,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800371 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700372 assert(value && "value can't be nullptr");
373 assert(diag && "diagnostics can't be nullptr");
374
Adam Lesinski330edcd2015-05-04 17:40:56 -0700375 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700377 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378 << "resource '"
379 << name
380 << "' has invalid entry name '"
381 << name.entry
382 << "'. Invalid character '"
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700383 << StringPiece(badCharIter, 1)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385 return false;
386 }
387
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700388 ResourceTablePackage* package = findOrCreatePackage(name.package);
389 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700390 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 << "trying to add resource '"
392 << name
393 << "' with ID "
394 << resId
395 << " but package '"
396 << package->name
397 << "' already has ID "
398 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800399 return false;
400 }
401
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700402 ResourceTableType* type = package->findOrCreateType(name.type);
403 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700404 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405 << "trying to add resource '"
406 << name
407 << "' with ID "
408 << resId
409 << " but type '"
410 << type->type
411 << "' already has ID "
412 << std::hex << (int) type->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413 return false;
414 }
415
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
417 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700418 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700419 << "trying to add resource '"
420 << name
421 << "' with ID "
422 << resId
423 << " but resource already has ID "
424 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
425 return false;
426 }
427
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800428 ResourceConfigValue* configValue = entry->findOrCreateValue(config, product);
429 if (!configValue->value) {
430 // Resource does not exist, add it now.
431 configValue->value = std::move(value);
432
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 } else {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800434 int collisionResult = conflictResolver(configValue->value.get(), value.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 if (collisionResult > 0) {
436 // Take the incoming value.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800437 configValue->value = std::move(value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438 } else if (collisionResult == 0) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700439 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 << "duplicate value for resource '" << name << "' "
Adam Lesinskie78fd612015-10-22 12:48:43 -0700441 << "with config '" << config << "'");
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800442 diag->error(DiagMessage(configValue->value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700443 << "resource previously defined here");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444 return false;
445 }
446 }
447
448 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700449 package->id = resId.packageId();
450 type->id = resId.typeId();
451 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800452 }
453 return true;
454}
455
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700456bool ResourceTable::setSymbolState(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700457 const Symbol& symbol, IDiagnostics* diag) {
458 return setSymbolStateImpl(name, resId, symbol, kValidNameChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700459}
460
Adam Lesinskie78fd612015-10-22 12:48:43 -0700461bool ResourceTable::setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700462 const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700463 const Symbol& symbol, IDiagnostics* diag) {
464 return setSymbolStateImpl(name, resId, symbol, kValidNameMangledChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700465}
466
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700467bool ResourceTable::setSymbolStateImpl(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700468 const Symbol& symbol, const char* validChars,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700469 IDiagnostics* diag) {
470 assert(diag && "diagnostics can't be nullptr");
471
Adam Lesinski330edcd2015-05-04 17:40:56 -0700472 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700474 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700475 << "resource '"
476 << name
477 << "' has invalid entry name '"
478 << name.entry
479 << "'. Invalid character '"
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700480 << StringPiece(badCharIter, 1)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700481 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482 return false;
483 }
484
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485 ResourceTablePackage* package = findOrCreatePackage(name.package);
486 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700487 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700488 << "trying to add resource '"
489 << name
490 << "' with ID "
491 << resId
492 << " but package '"
493 << package->name
494 << "' already has ID "
495 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496 return false;
497 }
498
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 ResourceTableType* type = package->findOrCreateType(name.type);
500 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700501 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502 << "trying to add resource '"
503 << name
504 << "' with ID "
505 << resId
506 << " but type '"
507 << type->type
508 << "' already has ID "
509 << std::hex << (int) type->id.value() << std::dec);
510 return false;
511 }
512
513 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
514 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700515 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700516 << "trying to add resource '"
517 << name
518 << "' with ID "
519 << resId
520 << " but resource already has ID "
521 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522 return false;
523 }
524
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800525 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526 package->id = resId.packageId();
527 type->id = resId.typeId();
528 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800530
531 // Only mark the type state as public, it doesn't care about being private.
532 if (symbol.state == SymbolState::kPublic) {
533 type->symbolStatus.state = SymbolState::kPublic;
534 }
535
536 if (symbol.state == SymbolState::kUndefined &&
537 entry->symbolStatus.state != SymbolState::kUndefined) {
538 // We can't undefine a symbol (remove its visibility). Ignore.
539 return true;
540 }
541
542 if (symbol.state == SymbolState::kPrivate &&
543 entry->symbolStatus.state == SymbolState::kPublic) {
544 // We can't downgrade public to private. Ignore.
545 return true;
546 }
547
548 entry->symbolStatus = std::move(symbol);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549 return true;
550}
551
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552Maybe<ResourceTable::SearchResult>
553ResourceTable::findResource(const ResourceNameRef& name) {
554 ResourceTablePackage* package = findPackage(name.package);
555 if (!package) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700556 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800557 }
558
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700559 ResourceTableType* type = package->findType(name.type);
560 if (!type) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700561 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800562 }
563
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700564 ResourceEntry* entry = type->findEntry(name.entry);
565 if (!entry) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700566 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700568 return SearchResult{ package, type, entry };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569}
570
571} // namespace aapt