blob: bdc6a8c5d4f95c0f6c1d059b17b018cc80ea3682 [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/**
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700204 * The default handler for collisions.
Adam Lesinski8197cc462016-08-19 12:16:49 -0700205 *
206 * Typically, a weak value will be overridden by a strong value. An existing weak
207 * value will not be overridden by an incoming weak value.
208 *
209 * There are some exceptions:
210 *
211 * Attributes: There are two types of Attribute values: USE and DECL.
212 *
213 * USE is anywhere an Attribute is declared without a format, and in a place that would
214 * be legal to declare if the Attribute already existed. This is typically in a
215 * <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
216 *
217 * DECL is an absolute declaration of an Attribute and specifies an explicit format.
218 *
219 * A DECL will override a USE without error. Two DECLs must match in their format for there to be
220 * no error.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 */
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700222ResourceTable::CollisionResult ResourceTable::resolveValueCollision(
223 Value* existing, Value* incoming) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224 Attribute* existingAttr = valueCast<Attribute>(existing);
225 Attribute* incomingAttr = valueCast<Attribute>(incoming);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226 if (!incomingAttr) {
227 if (incoming->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 // We're trying to add a weak resource but a resource
229 // already exists. Keep the existing.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700230 return CollisionResult::kKeepOriginal;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231 } else if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232 // Override the weak resource with the new strong resource.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700233 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 }
235 // The existing and incoming values are strong, this is an error
236 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700237 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 }
239
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240 if (!existingAttr) {
241 if (existing->isWeak()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242 // The existing value is not an attribute and it is weak,
243 // so take the incoming attribute value.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700244 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245 }
246 // The existing value is not an attribute and it is strong,
247 // so the incoming attribute value is an error.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700248 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 }
250
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251 assert(incomingAttr && existingAttr);
252
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 //
254 // Attribute specific handling. At this point we know both
255 // values are attributes. Since we can declare and define
256 // attributes all-over, we do special handling to see
257 // which definition sticks.
258 //
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 if (existingAttr->typeMask == incomingAttr->typeMask) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260 // The two attributes are both DECLs, but they are plain attributes
261 // with the same formats.
262 // Keep the strongest one.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700263 return existingAttr->isWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264 }
265
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266 if (existingAttr->isWeak() && existingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267 // Any incoming attribute is better than this.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700268 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269 }
270
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 if (incomingAttr->isWeak() && incomingAttr->typeMask == android::ResTable_map::TYPE_ANY) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272 // The incoming attribute may be a USE instead of a DECL.
273 // Keep the existing attribute.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700274 return CollisionResult::kKeepOriginal;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 }
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700276 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277}
278
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700279static constexpr const char* kValidNameChars = "._-";
280static constexpr const char* kValidNameMangledChars = "._-$";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700281
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800282bool ResourceTable::addResource(const ResourceNameRef& name,
283 const ConfigDescription& config,
284 const StringPiece& product,
285 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700286 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800287 return addResourceImpl(name, {}, config, product, std::move(value), kValidNameChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800288 resolveValueCollision, diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289}
290
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800291bool ResourceTable::addResource(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700292 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800293 const ConfigDescription& config,
294 const StringPiece& product,
295 std::unique_ptr<Value> value,
296 IDiagnostics* diag) {
297 return addResourceImpl(name, resId, config, product, std::move(value), kValidNameChars,
298 resolveValueCollision, diag);
299}
300
301bool ResourceTable::addFileReference(const ResourceNameRef& name,
302 const ConfigDescription& config,
303 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700304 const StringPiece& path,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305 IDiagnostics* diag) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800306 return addFileReferenceImpl(name, config, source, path, nullptr, kValidNameChars, diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800307}
308
Adam Lesinski355f2852016-02-13 20:26:45 -0800309bool ResourceTable::addFileReferenceAllowMangled(const ResourceNameRef& name,
310 const ConfigDescription& config,
311 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700312 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800313 io::IFile* file,
314 IDiagnostics* diag) {
315 return addFileReferenceImpl(name, config, source, path, file, kValidNameMangledChars, diag);
316}
317
318bool ResourceTable::addFileReferenceImpl(const ResourceNameRef& name,
319 const ConfigDescription& config,
320 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700321 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800322 io::IFile* file,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700323 const char* validChars,
Adam Lesinski355f2852016-02-13 20:26:45 -0800324 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700325 std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>(
326 stringPool.makeRef(path));
327 fileRef->setSource(source);
Adam Lesinski355f2852016-02-13 20:26:45 -0800328 fileRef->file = file;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800329 return addResourceImpl(name, ResourceId{}, config, StringPiece{}, std::move(fileRef),
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700330 validChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700331}
332
333bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
334 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800335 const StringPiece& product,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336 std::unique_ptr<Value> value,
337 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800338 return addResourceImpl(name, ResourceId{}, config, product, std::move(value),
339 kValidNameMangledChars, resolveValueCollision, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700340}
341
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700342bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700343 const ResourceId& id,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700344 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800345 const StringPiece& product,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700346 std::unique_ptr<Value> value,
347 IDiagnostics* diag) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800348 return addResourceImpl(name, id, config, product, std::move(value), kValidNameMangledChars,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800349 resolveValueCollision, diag);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700350}
351
Adam Lesinskifb48d292015-11-07 15:52:13 -0800352bool ResourceTable::addResourceImpl(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700353 const ResourceId& resId,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800354 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800355 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800356 std::unique_ptr<Value> value,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700357 const char* validChars,
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700358 const CollisionResolverFunc& conflictResolver,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800359 IDiagnostics* diag) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700360 assert(value && "value can't be nullptr");
361 assert(diag && "diagnostics can't be nullptr");
362
Adam Lesinski330edcd2015-05-04 17:40:56 -0700363 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700365 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700366 << "resource '"
367 << name
368 << "' has invalid entry name '"
369 << name.entry
370 << "'. Invalid character '"
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700371 << StringPiece(badCharIter, 1)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700372 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800373 return false;
374 }
375
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700376 ResourceTablePackage* package = findOrCreatePackage(name.package);
377 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700378 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700379 << "trying to add resource '"
380 << name
381 << "' with ID "
382 << resId
383 << " but package '"
384 << package->name
385 << "' already has ID "
386 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800387 return false;
388 }
389
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700390 ResourceTableType* type = package->findOrCreateType(name.type);
391 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700392 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393 << "trying to add resource '"
394 << name
395 << "' with ID "
396 << resId
397 << " but type '"
398 << type->type
399 << "' already has ID "
400 << std::hex << (int) type->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401 return false;
402 }
403
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700404 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
405 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700406 diag->error(DiagMessage(value->getSource())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700407 << "trying to add resource '"
408 << name
409 << "' with ID "
410 << resId
411 << " but resource already has ID "
412 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
413 return false;
414 }
415
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800416 ResourceConfigValue* configValue = entry->findOrCreateValue(config, product);
417 if (!configValue->value) {
418 // Resource does not exist, add it now.
419 configValue->value = std::move(value);
420
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421 } else {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700422 switch (conflictResolver(configValue->value.get(), value.get())) {
423 case CollisionResult::kTakeNew:
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424 // Take the incoming value.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800425 configValue->value = std::move(value);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700426 break;
427
428 case CollisionResult::kConflict:
Adam Lesinskie78fd612015-10-22 12:48:43 -0700429 diag->error(DiagMessage(value->getSource())
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700430 << "duplicate value for resource '" << name << "' "
431 << "with config '" << config << "'");
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800432 diag->error(DiagMessage(configValue->value->getSource())
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700433 << "resource previously defined here");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434 return false;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700435
436 case CollisionResult::kKeepOriginal:
437 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438 }
439 }
440
441 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700442 package->id = resId.packageId();
443 type->id = resId.typeId();
444 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445 }
446 return true;
447}
448
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700449bool ResourceTable::setSymbolState(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700450 const Symbol& symbol, IDiagnostics* diag) {
451 return setSymbolStateImpl(name, resId, symbol, kValidNameChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700452}
453
Adam Lesinskie78fd612015-10-22 12:48:43 -0700454bool ResourceTable::setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700455 const ResourceId& resId,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700456 const Symbol& symbol, IDiagnostics* diag) {
457 return setSymbolStateImpl(name, resId, symbol, kValidNameMangledChars, diag);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700458}
459
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700460bool ResourceTable::setSymbolStateImpl(const ResourceNameRef& name, const ResourceId& resId,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700461 const Symbol& symbol, const char* validChars,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700462 IDiagnostics* diag) {
463 assert(diag && "diagnostics can't be nullptr");
464
Adam Lesinski330edcd2015-05-04 17:40:56 -0700465 auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800466 if (badCharIter != name.entry.end()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700467 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700468 << "resource '"
469 << name
470 << "' has invalid entry name '"
471 << name.entry
472 << "'. Invalid character '"
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700473 << StringPiece(badCharIter, 1)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474 << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475 return false;
476 }
477
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700478 ResourceTablePackage* package = findOrCreatePackage(name.package);
479 if (resId.isValid() && package->id && package->id.value() != resId.packageId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700480 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700481 << "trying to add resource '"
482 << name
483 << "' with ID "
484 << resId
485 << " but package '"
486 << package->name
487 << "' already has ID "
488 << std::hex << (int) package->id.value() << std::dec);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800489 return false;
490 }
491
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 ResourceTableType* type = package->findOrCreateType(name.type);
493 if (resId.isValid() && type->id && type->id.value() != resId.typeId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700494 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700495 << "trying to add resource '"
496 << name
497 << "' with ID "
498 << resId
499 << " but type '"
500 << type->type
501 << "' already has ID "
502 << std::hex << (int) type->id.value() << std::dec);
503 return false;
504 }
505
506 ResourceEntry* entry = type->findOrCreateEntry(name.entry);
507 if (resId.isValid() && entry->id && entry->id.value() != resId.entryId()) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700508 diag->error(DiagMessage(symbol.source)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509 << "trying to add resource '"
510 << name
511 << "' with ID "
512 << resId
513 << " but resource already has ID "
514 << ResourceId(package->id.value(), type->id.value(), entry->id.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800515 return false;
516 }
517
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 if (resId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519 package->id = resId.packageId();
520 type->id = resId.typeId();
521 entry->id = resId.entryId();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800523
524 // Only mark the type state as public, it doesn't care about being private.
525 if (symbol.state == SymbolState::kPublic) {
526 type->symbolStatus.state = SymbolState::kPublic;
527 }
528
529 if (symbol.state == SymbolState::kUndefined &&
530 entry->symbolStatus.state != SymbolState::kUndefined) {
531 // We can't undefine a symbol (remove its visibility). Ignore.
532 return true;
533 }
534
535 if (symbol.state == SymbolState::kPrivate &&
536 entry->symbolStatus.state == SymbolState::kPublic) {
537 // We can't downgrade public to private. Ignore.
538 return true;
539 }
540
541 entry->symbolStatus = std::move(symbol);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800542 return true;
543}
544
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700545Maybe<ResourceTable::SearchResult>
546ResourceTable::findResource(const ResourceNameRef& name) {
547 ResourceTablePackage* package = findPackage(name.package);
548 if (!package) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700549 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800550 }
551
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552 ResourceTableType* type = package->findType(name.type);
553 if (!type) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700554 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800555 }
556
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700557 ResourceEntry* entry = type->findEntry(name.entry);
558 if (!entry) {
Adam Lesinskica2fc352015-04-03 12:08:26 -0700559 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800560 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561 return SearchResult{ package, type, entry };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800562}
563
564} // namespace aapt