blob: 4ac25bd6a0e07a1e0aaae471d3b8a47f288d0d68 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "link/ReferenceLinker.h"
18
19#include "android-base/logging.h"
Udam Sainib228df32019-06-18 16:50:34 -070020#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "androidfw/ResourceTypes.h"
22
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023#include "Diagnostics.h"
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070024#include "ResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
27#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "link/Linkers.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "process/IResourceTableConsumer.h"
31#include "process/SymbolTable.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080032#include "trace/TraceBuffer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "util/Util.h"
34#include "xml/XmlUtil.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinski2eed52e2018-02-21 15:55:58 -080036using ::aapt::ResourceUtils::StringBuilder;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070037using ::android::StringPiece;
Udam Sainib228df32019-06-18 16:50:34 -070038using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041namespace {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070042struct LoggingResourceName {
43 LoggingResourceName(const Reference& ref, const CallSite& callsite,
44 const xml::IPackageDeclStack* decls)
45 : ref_(ref), callsite_(callsite), decls_(decls) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 }
47
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070048 const Reference& ref_;
49 const CallSite& callsite_;
50 const xml::IPackageDeclStack* decls_;
51};
52
53inline ::std::ostream& operator<<(::std::ostream& out, const LoggingResourceName& name) {
54 if (!name.ref_.name) {
55 out << name.ref_.id.value();
56 return out;
57 }
58
59 out << name.ref_.name.value();
60
61 Reference fully_qualified = name.ref_;
62 xml::ResolvePackage(name.decls_, &fully_qualified);
63
64 ResourceName& full_name = fully_qualified.name.value();
65 if (full_name.package.empty()) {
66 full_name.package = name.callsite_.package;
67 }
68
69 if (full_name != name.ref_.name.value()) {
70 out << " (aka " << full_name << ")";
71 }
72 return out;
73}
74
75} // namespace
76
77std::unique_ptr<Reference> ReferenceLinkerTransformer::TransformDerived(const Reference* value) {
78 auto linked_item =
79 ReferenceLinker::LinkReference(callsite_, *value, context_, symbols_, table_, package_decls_);
80 if (linked_item) {
81 auto linked_item_ptr = linked_item.release();
82 if (auto ref = ValueCast<Reference>(linked_item_ptr)) {
83 return std::unique_ptr<Reference>(ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070085 context_->GetDiagnostics()->Error(DiagMessage(value->GetSource())
86 << "value of '"
87 << LoggingResourceName(*value, callsite_, package_decls_)
88 << "' must be a resource reference");
89 delete linked_item_ptr;
90 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070092 error_ = true;
93 return CloningValueTransformer::TransformDerived(value);
94}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070096std::unique_ptr<Style> ReferenceLinkerTransformer::TransformDerived(const Style* style) {
97 // We visit the Style specially because during this phase, values of attributes are either
98 // RawString or Reference values. Now that we are expected to resolve all symbols, we can lookup
99 // the attributes to find out which types are allowed for the attributes' values.
100 auto new_style = CloningValueTransformer::TransformDerived(style);
101 if (new_style->parent) {
102 new_style->parent = *TransformDerived(&style->parent.value());
103 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700105 for (Style::Entry& entry : new_style->entries) {
106 std::string err_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700108 // Transform the attribute reference so that it is using the fully qualified package
109 // name. This will also mark the reference as being able to see private resources if
110 // there was a '*' in the reference or if the package came from the private namespace.
111 Reference transformed_reference = entry.key;
112 ResolvePackage(package_decls_, &transformed_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700114 // Find the attribute in the symbol table and check if it is visible from this callsite.
115 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveAttributeCheckVisibility(
116 transformed_reference, callsite_, context_, symbols_, &err_str);
117 if (symbol) {
118 // Assign our style key the correct ID. The ID may not exist.
119 entry.key.id = symbol->id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700121 // Link/resolve the final value if it's a reference.
122 entry.value = entry.value->Transform(*this);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700124 // Try to convert the value to a more specific, typed value based on the attribute it is
125 // set to.
126 entry.value = ParseValueWithAttribute(std::move(entry.value), symbol->attribute.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700128 // Now verify that the type of this item is compatible with the
129 // attribute it is defined for. We pass `nullptr` as the DiagMessage so that this
130 // check is fast and we avoid creating a DiagMessage when the match is successful.
131 if (!symbol->attribute->Matches(*entry.value, nullptr)) {
132 // The actual type of this item is incompatible with the attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 DiagMessage msg(entry.key.GetSource());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700134
135 // Call the matches method again, this time with a DiagMessage so we fill in the actual
136 // error message.
137 symbol->attribute->Matches(*entry.value, &msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 context_->GetDiagnostics()->Error(msg);
139 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700141 } else {
142 context_->GetDiagnostics()->Error(DiagMessage(entry.key.GetSource())
143 << "style attribute '"
144 << LoggingResourceName(entry.key, callsite_, package_decls_)
145 << "' " << err_str);
146
147 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
149 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700150 return new_style;
151}
Adam Lesinski467f1712015-11-16 17:35:44 -0800152
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700153std::unique_ptr<Item> ReferenceLinkerTransformer::TransformItem(const Reference* value) {
154 auto linked_value =
155 ReferenceLinker::LinkReference(callsite_, *value, context_, symbols_, table_, package_decls_);
156 if (linked_value) {
157 return linked_value;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700158 }
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700159 error_ = true;
160 return CloningValueTransformer::TransformDerived(value);
161}
Adam Lesinski467f1712015-11-16 17:35:44 -0800162
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700163// Transform a RawString value into a more specific, appropriate value, based on the
164// Attribute. If a non RawString value is passed in, this is an identity transform.
165std::unique_ptr<Item> ReferenceLinkerTransformer::ParseValueWithAttribute(
166 std::unique_ptr<Item> value, const Attribute* attr) {
167 if (RawString* raw_string = ValueCast<RawString>(value.get())) {
168 std::unique_ptr<Item> transformed =
169 ResourceUtils::TryParseItemForAttribute(*raw_string->value, attr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700171 // If we could not parse as any specific type, try a basic STRING.
172 if (!transformed && (attr->type_mask & android::ResTable_map::TYPE_STRING)) {
173 StringBuilder string_builder;
174 string_builder.AppendText(*raw_string->value);
175 if (string_builder) {
176 transformed = util::make_unique<String>(pool_->MakeRef(string_builder.to_string()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 }
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800178 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700180 if (transformed) {
181 return transformed;
182 }
183 }
184 return value;
185}
186
187namespace {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188
189class EmptyDeclStack : public xml::IPackageDeclStack {
190 public:
191 EmptyDeclStack() = default;
192
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700193 Maybe<xml::ExtractedPackage> TransformPackageAlias(const StringPiece& alias) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 if (alias.empty()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700195 return xml::ExtractedPackage{{}, true /*private*/};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 }
197 return {};
198 }
199
200 private:
201 DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700202};
203
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700204struct MacroDeclStack : public xml::IPackageDeclStack {
205 explicit MacroDeclStack(std::vector<Macro::Namespace> namespaces)
206 : alias_namespaces_(std::move(namespaces)) {
207 }
208
209 Maybe<xml::ExtractedPackage> TransformPackageAlias(const StringPiece& alias) const override {
210 if (alias.empty()) {
211 return xml::ExtractedPackage{{}, true /*private*/};
212 }
213 for (auto it = alias_namespaces_.rbegin(); it != alias_namespaces_.rend(); ++it) {
214 if (alias == StringPiece(it->alias)) {
215 return xml::ExtractedPackage{it->package_name, it->is_private};
216 }
217 }
218 return {};
219 }
220
221 private:
222 std::vector<Macro::Namespace> alias_namespaces_;
223};
224
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700225// The symbol is visible if it is public, or if the reference to it is requesting private access
226// or if the callsite comes from the same package.
227bool IsSymbolVisible(const SymbolTable::Symbol& symbol, const Reference& ref,
228 const CallSite& callsite) {
229 if (symbol.is_public || ref.private_reference) {
230 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 }
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700232
233 if (ref.name) {
234 const ResourceName& name = ref.name.value();
235 if (name.package.empty()) {
236 // If the symbol was found, and the package is empty, that means it was found in the local
237 // scope, which is always visible (private local).
238 return true;
239 }
240
241 // The symbol is visible if the reference is local to the same package it is defined in.
242 return callsite.package == name.package;
243 }
244
245 if (ref.id && symbol.id) {
246 return ref.id.value().package_id() == symbol.id.value().package_id();
247 }
248 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800249}
250
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700251} // namespace
252
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800253const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700254 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -0700255 IAaptContext* context,
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000256 SymbolTable* symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 if (reference.name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700258 const ResourceName& name = reference.name.value();
259 if (name.package.empty()) {
260 // Use the callsite's package name if no package name was defined.
Udam Sainib228df32019-06-18 16:50:34 -0700261 const SymbolTable::Symbol* symbol = symbols->FindByName(
262 ResourceName(callsite.package, name.type, name.entry));
263 if (symbol) {
264 return symbol;
265 }
266
267 // If the callsite package is the same as the current compilation package,
268 // check the feature split dependencies as well. Feature split resources
269 // can be referenced without a namespace, just like the base package.
Udam Sainib228df32019-06-18 16:50:34 -0700270 if (callsite.package == context->GetCompilationPackage()) {
271 const auto& split_name_dependencies = context->GetSplitNameDependencies();
272 for (const std::string& split_name : split_name_dependencies) {
273 std::string split_package =
274 StringPrintf("%s.%s", callsite.package.c_str(), split_name.c_str());
275 symbol = symbols->FindByName(ResourceName(split_package, name.type, name.entry));
276 if (symbol) {
277 return symbol;
278 }
279 }
280 }
281 return nullptr;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700282 }
283 return symbols->FindByName(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 } else if (reference.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 return symbols->FindById(reference.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 } else {
287 return nullptr;
288 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800289}
290
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800291const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800292 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -0700293 IAaptContext* context,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800294 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800295 std::string* out_error) {
Udam Sainib228df32019-06-18 16:50:34 -0700296 const SymbolTable::Symbol* symbol = ResolveSymbol(reference, callsite, context, symbols);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 if (!symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 if (out_error) *out_error = "not found";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 return nullptr;
300 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800301
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800302 if (!IsSymbolVisible(*symbol, reference, callsite)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 if (out_error) *out_error = "is private";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return nullptr;
305 }
306 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800307}
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility(
Udam Sainib228df32019-06-18 16:50:34 -0700310 const Reference& reference, const CallSite& callsite, IAaptContext* context,
311 SymbolTable* symbols, std::string* out_error) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800312 const SymbolTable::Symbol* symbol =
Udam Sainib228df32019-06-18 16:50:34 -0700313 ResolveSymbolCheckVisibility(reference, callsite, context, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 if (!symbol) {
315 return nullptr;
316 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800317
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return nullptr;
321 }
322 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800323}
324
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800325Maybe<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800326 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -0700327 IAaptContext* context,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800328 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800329 std::string* out_error) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800330 const SymbolTable::Symbol* symbol =
Udam Sainib228df32019-06-18 16:50:34 -0700331 ResolveAttributeCheckVisibility(reference, callsite, context, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 if (!symbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 return {};
334 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800335
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 return {};
339 }
Adam Lesinskic744ae82017-05-17 19:28:38 -0700340 return xml::AaptAttribute(*symbol->attribute, symbol->id);
Adam Lesinski467f1712015-11-16 17:35:44 -0800341}
342
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700343void ReferenceLinker::WriteAttributeName(const Reference& ref, const CallSite& callsite,
344 const xml::IPackageDeclStack* decls,
345 DiagMessage* out_msg) {
346 CHECK(out_msg != nullptr);
347 if (!ref.name) {
348 *out_msg << ref.id.value();
349 return;
350 }
351
352 const ResourceName& ref_name = ref.name.value();
353 CHECK_EQ(ref_name.type, ResourceType::kAttr);
354
355 if (!ref_name.package.empty()) {
356 *out_msg << ref_name.package << ":";
357 }
358 *out_msg << ref_name.entry;
359
360 Reference fully_qualified = ref;
361 xml::ResolvePackage(decls, &fully_qualified);
362
363 ResourceName& full_name = fully_qualified.name.value();
364 if (full_name.package.empty()) {
365 full_name.package = callsite.package;
366 }
367
368 if (full_name != ref.name.value()) {
369 *out_msg << " (aka " << full_name.package << ":" << full_name.entry << ")";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 }
Adam Lesinski28cacf02015-11-23 14:22:47 -0800371}
372
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700373std::unique_ptr<Item> ReferenceLinker::LinkReference(const CallSite& callsite,
374 const Reference& reference,
375 IAaptContext* context, SymbolTable* symbols,
376 ResourceTable* table,
377 const xml::IPackageDeclStack* decls) {
378 if (!reference.name && !reference.id) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700379 // This is @null.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700380 return std::make_unique<Reference>(reference);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700381 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800382
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700383 Reference transformed_reference = reference;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700384 xml::ResolvePackage(decls, &transformed_reference);
Adam Lesinski467f1712015-11-16 17:35:44 -0800385
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700386 if (transformed_reference.name.value().type == ResourceType::kMacro) {
387 if (transformed_reference.name.value().package.empty()) {
388 transformed_reference.name.value().package = callsite.package;
389 }
390
391 auto result = table->FindResource(transformed_reference.name.value());
392 if (!result || result.value().entry->values.empty()) {
393 context->GetDiagnostics()->Error(
394 DiagMessage(reference.GetSource())
395 << "failed to find definition for "
396 << LoggingResourceName(transformed_reference, callsite, decls));
397 return {};
398 }
399
400 auto& macro_values = result.value().entry->values;
401 CHECK(macro_values.size() == 1) << "Macros can only be defined in the default configuration.";
402
403 auto macro = ValueCast<Macro>(macro_values[0]->value.get());
404 CHECK(macro != nullptr) << "Value of macro resource is not a Macro (actual "
405 << *macro_values[0]->value << ")";
406
407 // Re-create the state used to parse the macro tag to compile the macro contents as if it was
408 // defined inline
409 uint32_t type_flags = 0;
410 if (reference.type_flags.has_value()) {
411 type_flags = reference.type_flags.value();
412 }
413
414 MacroDeclStack namespace_stack(macro->alias_namespaces);
415 FlattenedXmlSubTree sub_tree{.raw_value = macro->raw_value,
416 .style_string = macro->style_string,
417 .untranslatable_sections = macro->untranslatable_sections,
418 .namespace_resolver = &namespace_stack,
419 .source = macro->GetSource()};
420
421 auto new_value = ResourceParser::ParseXml(sub_tree, type_flags, reference.allow_raw, *table,
422 macro_values[0]->config, *context->GetDiagnostics());
423 if (new_value == nullptr) {
424 context->GetDiagnostics()->Error(
425 DiagMessage(reference.GetSource())
426 << "failed to substitute macro "
427 << LoggingResourceName(transformed_reference, callsite, decls)
428 << ": failed to parse contents as one of type(s) " << Attribute::MaskString(type_flags));
429 return {};
430 }
431
432 if (auto ref = ValueCast<Reference>(new_value.get())) {
433 return LinkReference(callsite, *ref, context, symbols, table, decls);
434 }
435 return new_value;
436 }
437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000439 const SymbolTable::Symbol* s =
Udam Sainib228df32019-06-18 16:50:34 -0700440 ResolveSymbolCheckVisibility(transformed_reference, callsite, context, symbols, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if (s) {
442 // The ID may not exist. This is fine because of the possibility of building
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 // against libraries without assigned IDs.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 // Ex: Linking against own resources when building a static library.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700445 auto new_ref = std::make_unique<Reference>(reference);
446 new_ref->id = s->id;
447 new_ref->is_dynamic = s->is_dynamic;
448 return std::move(new_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800450
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700451 context->GetDiagnostics()->Error(DiagMessage(reference.GetSource())
452 << "resource "
453 << LoggingResourceName(transformed_reference, callsite, decls)
454 << " " << err_str);
455 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -0800456}
457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800459 TRACE_NAME("ReferenceLinker::Consume");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 EmptyDeclStack decl_stack;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 bool error = false;
462 for (auto& package : table->packages) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700463 // Since we're linking, each package must have a name.
464 CHECK(!package->name.empty()) << "all packages being linked must have a name";
465
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 for (auto& type : package->types) {
467 for (auto& entry : type->entries) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700468 // First, unmangle the name if necessary.
469 ResourceName name(package->name, type->type, entry->name);
470 NameMangler::Unmangle(&name.entry, &name.package);
471
472 // Symbol state information may be lost if there is no value for the resource.
Adam Lesinski71be7052017-12-12 16:48:07 -0800473 if (entry->visibility.level != Visibility::Level::kUndefined && entry->values.empty()) {
474 context->GetDiagnostics()->Error(DiagMessage(entry->visibility.source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800475 << "no definition for declared symbol '" << name
476 << "'");
477 error = true;
478 }
479
480 // Ensure that definitions for values declared as overlayable exist
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800481 if (entry->overlayable_item && entry->values.empty()) {
482 context->GetDiagnostics()->Error(DiagMessage(entry->overlayable_item.value().source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800483 << "no definition for overlayable symbol '"
484 << name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700488 // The context of this resource is the package in which it is defined.
489 const CallSite callsite{name.package};
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700490 ReferenceLinkerTransformer reference_transformer(callsite, context,
491 context->GetExternalSymbols(),
492 &table->string_pool, table, &decl_stack);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 for (auto& config_value : entry->values) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700495 config_value->value = config_value->value->Transform(reference_transformer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 }
497
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700498 if (reference_transformer.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 error = true;
500 }
501 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 }
504 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505}
506
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507} // namespace aapt