blob: 3e73be4dbc546e170e474915088a357bb7780579 [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 Lesinski6f6ceb72014-11-14 14:48:12 -0800192/**
193 * The default handler for collisions. A return value of -1 means keep the
194 * existing value, 0 means fail, and +1 means take the incoming value.
195 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196int ResourceTable::resolveValueCollision(Value* existing, Value* incoming) {
197 Attribute* existingAttr = valueCast<Attribute>(existing);
198 Attribute* incomingAttr = valueCast<Attribute>(incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 if (!incomingAttr) {
201 if (incoming->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202 // We're trying to add a weak resource but a resource
203 // already exists. Keep the existing.
204 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 } else if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206 // Override the weak resource with the new strong resource.
207 return 1;
208 }
209 // The existing and incoming values are strong, this is an error
210 // if the values are not both attributes.
211 return 0;
212 }
213
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214 if (!existingAttr) {
215 if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216 // The existing value is not an attribute and it is weak,
217 // so take the incoming attribute value.
218 return 1;
219 }
220 // The existing value is not an attribute and it is strong,
221 // so the incoming attribute value is an error.
222 return 0;
223 }
224
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 assert(incomingAttr && existingAttr);
226
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 //
228 // Attribute specific handling. At this point we know both
229 // values are attributes. Since we can declare and define
230 // attributes all-over, we do special handling to see
231 // which definition sticks.
232 //
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233 if (existingAttr->typeMask == incomingAttr->typeMask) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 // The two attributes are both DECLs, but they are plain attributes
235 // with the same formats.
236 // Keep the strongest one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237 return existingAttr->isWeak() ? 1 : -1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 }
239
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240 if (existingAttr->isWeak() && existingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241 // Any incoming attribute is better than this.
242 return 1;
243 }
244
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 if (incomingAttr->isWeak() && incomingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246 // The incoming attribute may be a USE instead of a DECL.
247 // Keep the existing attribute.
248 return -1;
249 }
250 return 0;
251}
252
253static constexpr const char16_t* kValidNameChars = u"._-";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700254static constexpr const char16_t* kValidNameMangledChars = u"._-$";
255
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800256bool ResourceTable::addResource(const ResourceNameRef& name,
257 const ConfigDescription& config,
258 const StringPiece& product,
259 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700260 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800261 return addResourceImpl(name, {}, config, product, std::move(value), kValidNameChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800262 resolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263}
264
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800265bool ResourceTable::addResource(const ResourceNameRef& name,
266 const ResourceId resId,
267 const ConfigDescription& config,
268 const StringPiece& product,
269 std::unique_ptr<Value> value,
270 IDiagnostics* diag) {
271 return addResourceImpl(name, resId, config, product, std::move(value), kValidNameChars,
272 resolveValueCollision, diag);
273}
274
275bool ResourceTable::addFileReference(const ResourceNameRef& name,
276 const ConfigDescription& config,
277 const Source& source,
278 const StringPiece16& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279 IDiagnostics* diag) {
Adam Lesinskifb48d292015-11-07 15:52:13 -0800280 return addFileReference(name, config, source, path, resolveValueCollision, diag);
281}
282
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800283bool ResourceTable::addFileReference(const ResourceNameRef& name,
284 const ConfigDescription& config,
285 const Source& source,
286 const StringPiece16& path,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800287 std::function<int(Value*,Value*)> conflictResolver,
288 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700289 std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>(
290 stringPool.makeRef(path));
291 fileRef->setSource(source);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800292 return addResourceImpl(name, ResourceId{}, config, StringPiece{}, std::move(fileRef),
293 kValidNameChars, conflictResolver, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700294}
295
296bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
297 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800298 const StringPiece& product,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299 std::unique_ptr<Value> value,
300 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800301 return addResourceImpl(name, ResourceId{}, config, product, std::move(value),
302 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700303}
304
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700305bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
306 const ResourceId id,
307 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800308 const StringPiece& product,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700309 std::unique_ptr<Value> value,
310 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800311 return addResourceImpl(name, id, config, product, std::move(value), kValidNameMangledChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800312 resolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700313}
314
Adam Lesinskifb48d292015-11-07 15:52:13 -0800315bool ResourceTable::addResourceImpl(const ResourceNameRef& name,
316 const ResourceId resId,
317 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800318 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800319 std::unique_ptr<Value> value,
320 const char16_t* validChars,
321 std::function<int(Value*,Value*)> conflictResolver,
322 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700323 assert(value && "value can't be nullptr");
324 assert(diag && "diagnostics can't be nullptr");
325
Adam Lesinski330edcd2015-05-04 17:40:56 -0700326 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700328 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329 << "resource '"
330 << name
331 << "' has invalid entry name '"
332 << name.entry
333 << "'. Invalid character '"
334 << StringPiece16(badCharIter, 1)
335 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336 return false;
337 }
338
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 ResourceTablePackage* package = findOrCreatePackage(name.package);
340 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700341 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700342 << "trying to add resource '"
343 << name
344 << "' with ID "
345 << resId
346 << " but package '"
347 << package->name
348 << "' already has ID "
349 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350 return false;
351 }
352
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700353 ResourceTableType* type = package->findOrCreateType(name.type);
354 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700355 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700356 << "trying to add resource '"
357 << name
358 << "' with ID "
359 << resId
360 << " but type '"
361 << type->type
362 << "' already has ID "
363 << std::hex << (int) type->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 return false;
365 }
366
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700367 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
368 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700369 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700370 << "trying to add resource '"
371 << name
372 << "' with ID "
373 << resId
374 << " but resource already has ID "
375 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
376 return false;
377 }
378
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800379 ResourceConfigValue* configValue = entry->findOrCreateValue(config, product);
380 if (!configValue->value) {
381 // Resource does not exist, add it now.
382 configValue->value = std::move(value);
383
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384 } else {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800385 int collisionResult = conflictResolver(configValue->value.get(), value.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386 if (collisionResult > 0) {
387 // Take the incoming value.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800388 configValue->value = std::move(value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800389 } else if (collisionResult == 0) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700390 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 << "duplicate value for resource '" << name << "' "
Adam Lesinskie78fd612015-10-22 12:48:43 -0700392 << "with config '" << config << "'");
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800393 diag->error(DiagMessage(configValue->value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700394 << "resource previously defined here");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395 return false;
396 }
397 }
398
399 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700400 package->id = resId.packageId();
401 type->id = resId.typeId();
402 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403 }
404 return true;
405}
406
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700407bool ResourceTable::setSymbolState(const ResourceNameRef& name, const ResourceId resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700408 const Symbol& symbol, IDiagnostics* diag) {
409 return setSymbolStateImpl(name, resId, symbol, kValidNameChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700410}
411
Adam Lesinskie78fd612015-10-22 12:48:43 -0700412bool ResourceTable::setSymbolStateAllowMangled(const ResourceNameRef& name,
413 const ResourceId resId,
414 const Symbol& symbol, IDiagnostics* diag) {
415 return setSymbolStateImpl(name, resId, symbol, kValidNameMangledChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700416}
417
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700418bool ResourceTable::setSymbolStateImpl(const ResourceNameRef& name, const ResourceId resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700419 const Symbol& symbol, const char16_t* validChars,
420 IDiagnostics* diag) {
421 assert(diag && "diagnostics can't be nullptr");
422
Adam Lesinski330edcd2015-05-04 17:40:56 -0700423 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700425 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426 << "resource '"
427 << name
428 << "' has invalid entry name '"
429 << name.entry
430 << "'. Invalid character '"
431 << StringPiece16(badCharIter, 1)
432 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 return false;
434 }
435
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436 ResourceTablePackage* package = findOrCreatePackage(name.package);
437 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700438 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439 << "trying to add resource '"
440 << name
441 << "' with ID "
442 << resId
443 << " but package '"
444 << package->name
445 << "' already has ID "
446 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447 return false;
448 }
449
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700450 ResourceTableType* type = package->findOrCreateType(name.type);
451 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700452 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453 << "trying to add resource '"
454 << name
455 << "' with ID "
456 << resId
457 << " but type '"
458 << type->type
459 << "' already has ID "
460 << std::hex << (int) type->id.value() << std::dec);
461 return false;
462 }
463
464 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
465 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700466 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700467 << "trying to add resource '"
468 << name
469 << "' with ID "
470 << resId
471 << " but resource already has ID "
472 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473 return false;
474 }
475
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800476 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700477 package->id = resId.packageId();
478 type->id = resId.typeId();
479 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800481
482 // Only mark the type state as public, it doesn't care about being private.
483 if (symbol.state == SymbolState::kPublic) {
484 type->symbolStatus.state = SymbolState::kPublic;
485 }
486
487 if (symbol.state == SymbolState::kUndefined &&
488 entry->symbolStatus.state != SymbolState::kUndefined) {
489 // We can't undefine a symbol (remove its visibility). Ignore.
490 return true;
491 }
492
493 if (symbol.state == SymbolState::kPrivate &&
494 entry->symbolStatus.state == SymbolState::kPublic) {
495 // We can't downgrade public to private. Ignore.
496 return true;
497 }
498
499 entry->symbolStatus = std::move(symbol);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500 return true;
501}
502
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503Maybe<ResourceTable::SearchResult>
504ResourceTable::findResource(const ResourceNameRef& name) {
505 ResourceTablePackage* package = findPackage(name.package);
506 if (!package) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700507 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508 }
509
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510 ResourceTableType* type = package->findType(name.type);
511 if (!type) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700512 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513 }
514
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700515 ResourceEntry* entry = type->findEntry(name.entry);
516 if (!entry) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700517 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519 return SearchResult{ package, type, entry };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520}
521
522} // namespace aapt