blob: f9707e41bb3d6624accc577e03a49b76ec4ff7cc [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,
38 const StringPiece16& 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 Lesinski1ab598f2015-08-14 14:26:04 -070042ResourceTablePackage* ResourceTable::findPackage(const StringPiece16& name) {
43 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 Lesinski9ba47d82015-10-13 11:37:10 -070061ResourceTablePackage* ResourceTable::createPackage(const StringPiece16& 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 Lesinski1ab598f2015-08-14 14:26:04 -070074ResourceTablePackage* ResourceTable::findOrCreatePackage(const StringPiece16& name) {
75 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
105ResourceEntry* ResourceTableType::findEntry(const StringPiece16& name) {
106 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
115ResourceEntry* ResourceTableType::findOrCreateEntry(const StringPiece16& name) {
116 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.
206 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207int ResourceTable::resolveValueCollision(Value* existing, Value* incoming) {
208 Attribute* existingAttr = valueCast<Attribute>(existing);
209 Attribute* incomingAttr = valueCast<Attribute>(incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211 if (!incomingAttr) {
212 if (incoming->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213 // We're trying to add a weak resource but a resource
214 // already exists. Keep the existing.
215 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216 } else if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217 // Override the weak resource with the new strong resource.
218 return 1;
219 }
220 // The existing and incoming values are strong, this is an error
221 // if the values are not both attributes.
222 return 0;
223 }
224
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 if (!existingAttr) {
226 if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 // The existing value is not an attribute and it is weak,
228 // so take the incoming attribute value.
229 return 1;
230 }
231 // The existing value is not an attribute and it is strong,
232 // so the incoming attribute value is an error.
233 return 0;
234 }
235
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236 assert(incomingAttr && existingAttr);
237
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 //
239 // Attribute specific handling. At this point we know both
240 // values are attributes. Since we can declare and define
241 // attributes all-over, we do special handling to see
242 // which definition sticks.
243 //
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244 if (existingAttr->typeMask == incomingAttr->typeMask) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245 // The two attributes are both DECLs, but they are plain attributes
246 // with the same formats.
247 // Keep the strongest one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 return existingAttr->isWeak() ? 1 : -1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 }
250
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251 if (existingAttr->isWeak() && existingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252 // Any incoming attribute is better than this.
253 return 1;
254 }
255
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 if (incomingAttr->isWeak() && incomingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257 // The incoming attribute may be a USE instead of a DECL.
258 // Keep the existing attribute.
259 return -1;
260 }
261 return 0;
262}
263
264static constexpr const char16_t* kValidNameChars = u"._-";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700265static constexpr const char16_t* kValidNameMangledChars = u"._-$";
266
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800267bool ResourceTable::addResource(const ResourceNameRef& name,
268 const ConfigDescription& config,
269 const StringPiece& product,
270 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700271 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800272 return addResourceImpl(name, {}, config, product, std::move(value), kValidNameChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800273 resolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274}
275
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800276bool ResourceTable::addResource(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700277 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800278 const ConfigDescription& config,
279 const StringPiece& product,
280 std::unique_ptr<Value> value,
281 IDiagnostics* diag) {
282 return addResourceImpl(name, resId, config, product, std::move(value), kValidNameChars,
283 resolveValueCollision, diag);
284}
285
286bool ResourceTable::addFileReference(const ResourceNameRef& name,
287 const ConfigDescription& config,
288 const Source& source,
289 const StringPiece16& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290 IDiagnostics* diag) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800291 return addFileReferenceImpl(name, config, source, path, nullptr, kValidNameChars, diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800292}
293
Adam Lesinski355f2852016-02-13 20:26:45 -0800294bool ResourceTable::addFileReferenceAllowMangled(const ResourceNameRef& name,
295 const ConfigDescription& config,
296 const Source& source,
297 const StringPiece16& path,
298 io::IFile* file,
299 IDiagnostics* diag) {
300 return addFileReferenceImpl(name, config, source, path, file, kValidNameMangledChars, diag);
301}
302
303bool ResourceTable::addFileReferenceImpl(const ResourceNameRef& name,
304 const ConfigDescription& config,
305 const Source& source,
306 const StringPiece16& path,
307 io::IFile* file,
308 const char16_t* validChars,
309 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700310 std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>(
311 stringPool.makeRef(path));
312 fileRef->setSource(source);
Adam Lesinski355f2852016-02-13 20:26:45 -0800313 fileRef->file = file;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800314 return addResourceImpl(name, ResourceId{}, config, StringPiece{}, std::move(fileRef),
Adam Lesinski355f2852016-02-13 20:26:45 -0800315 kValidNameChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700316}
317
318bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
319 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800320 const StringPiece& product,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321 std::unique_ptr<Value> value,
322 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800323 return addResourceImpl(name, ResourceId{}, config, product, std::move(value),
324 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700325}
326
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700327bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700328 const ResourceId& id,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700329 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800330 const StringPiece& product,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700331 std::unique_ptr<Value> value,
332 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800333 return addResourceImpl(name, id, config, product, std::move(value), kValidNameMangledChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800334 resolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700335}
336
Adam Lesinskifb48d292015-11-07 15:52:13 -0800337bool ResourceTable::addResourceImpl(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700338 const ResourceId& resId,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800339 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800340 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800341 std::unique_ptr<Value> value,
342 const char16_t* validChars,
343 std::function<int(Value*,Value*)> conflictResolver,
344 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700345 assert(value && "value can't be nullptr");
346 assert(diag && "diagnostics can't be nullptr");
347
Adam Lesinski330edcd2015-05-04 17:40:56 -0700348 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700350 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351 << "resource '"
352 << name
353 << "' has invalid entry name '"
354 << name.entry
355 << "'. Invalid character '"
356 << StringPiece16(badCharIter, 1)
357 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358 return false;
359 }
360
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361 ResourceTablePackage* package = findOrCreatePackage(name.package);
362 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700363 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364 << "trying to add resource '"
365 << name
366 << "' with ID "
367 << resId
368 << " but package '"
369 << package->name
370 << "' already has ID "
371 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372 return false;
373 }
374
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700375 ResourceTableType* type = package->findOrCreateType(name.type);
376 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700377 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378 << "trying to add resource '"
379 << name
380 << "' with ID "
381 << resId
382 << " but type '"
383 << type->type
384 << "' already has ID "
385 << std::hex << (int) type->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386 return false;
387 }
388
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
390 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700391 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700392 << "trying to add resource '"
393 << name
394 << "' with ID "
395 << resId
396 << " but resource already has ID "
397 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
398 return false;
399 }
400
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800401 ResourceConfigValue* configValue = entry->findOrCreateValue(config, product);
402 if (!configValue->value) {
403 // Resource does not exist, add it now.
404 configValue->value = std::move(value);
405
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 } else {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800407 int collisionResult = conflictResolver(configValue->value.get(), value.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408 if (collisionResult > 0) {
409 // Take the incoming value.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800410 configValue->value = std::move(value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411 } else if (collisionResult == 0) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700412 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700413 << "duplicate value for resource '" << name << "' "
Adam Lesinskie78fd612015-10-22 12:48:43 -0700414 << "with config '" << config << "'");
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800415 diag->error(DiagMessage(configValue->value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416 << "resource previously defined here");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800417 return false;
418 }
419 }
420
421 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700422 package->id = resId.packageId();
423 type->id = resId.typeId();
424 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425 }
426 return true;
427}
428
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700429bool ResourceTable::setSymbolState(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700430 const Symbol& symbol, IDiagnostics* diag) {
431 return setSymbolStateImpl(name, resId, symbol, kValidNameChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700432}
433
Adam Lesinskie78fd612015-10-22 12:48:43 -0700434bool ResourceTable::setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700435 const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700436 const Symbol& symbol, IDiagnostics* diag) {
437 return setSymbolStateImpl(name, resId, symbol, kValidNameMangledChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700438}
439
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700440bool ResourceTable::setSymbolStateImpl(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700441 const Symbol& symbol, const char16_t* validChars,
442 IDiagnostics* diag) {
443 assert(diag && "diagnostics can't be nullptr");
444
Adam Lesinski330edcd2015-05-04 17:40:56 -0700445 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700447 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700448 << "resource '"
449 << name
450 << "' has invalid entry name '"
451 << name.entry
452 << "'. Invalid character '"
453 << StringPiece16(badCharIter, 1)
454 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455 return false;
456 }
457
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458 ResourceTablePackage* package = findOrCreatePackage(name.package);
459 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700460 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461 << "trying to add resource '"
462 << name
463 << "' with ID "
464 << resId
465 << " but package '"
466 << package->name
467 << "' already has ID "
468 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469 return false;
470 }
471
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700472 ResourceTableType* type = package->findOrCreateType(name.type);
473 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700474 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700475 << "trying to add resource '"
476 << name
477 << "' with ID "
478 << resId
479 << " but type '"
480 << type->type
481 << "' already has ID "
482 << std::hex << (int) type->id.value() << std::dec);
483 return false;
484 }
485
486 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
487 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700488 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489 << "trying to add resource '"
490 << name
491 << "' with ID "
492 << resId
493 << " but resource already has ID "
494 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495 return false;
496 }
497
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 package->id = resId.packageId();
500 type->id = resId.typeId();
501 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800503
504 // Only mark the type state as public, it doesn't care about being private.
505 if (symbol.state == SymbolState::kPublic) {
506 type->symbolStatus.state = SymbolState::kPublic;
507 }
508
509 if (symbol.state == SymbolState::kUndefined &&
510 entry->symbolStatus.state != SymbolState::kUndefined) {
511 // We can't undefine a symbol (remove its visibility). Ignore.
512 return true;
513 }
514
515 if (symbol.state == SymbolState::kPrivate &&
516 entry->symbolStatus.state == SymbolState::kPublic) {
517 // We can't downgrade public to private. Ignore.
518 return true;
519 }
520
521 entry->symbolStatus = std::move(symbol);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522 return true;
523}
524
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700525Maybe<ResourceTable::SearchResult>
526ResourceTable::findResource(const ResourceNameRef& name) {
527 ResourceTablePackage* package = findPackage(name.package);
528 if (!package) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700529 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800530 }
531
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700532 ResourceTableType* type = package->findType(name.type);
533 if (!type) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700534 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800535 }
536
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 ResourceEntry* entry = type->findEntry(name.entry);
538 if (!entry) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700539 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800540 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541 return SearchResult{ package, type, entry };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800542}
543
544} // namespace aapt