blob: d1fbffa1debd0f385622634d8cf6792045d9300f [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
Ryan Mitchell4382e442021-07-14 12:53:01 -0700193 std::optional<xml::ExtractedPackage> TransformPackageAlias(
194 const StringPiece& alias) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 if (alias.empty()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700196 return xml::ExtractedPackage{{}, true /*private*/};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 }
198 return {};
199 }
200
201 private:
202 DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203};
204
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700205struct MacroDeclStack : public xml::IPackageDeclStack {
206 explicit MacroDeclStack(std::vector<Macro::Namespace> namespaces)
207 : alias_namespaces_(std::move(namespaces)) {
208 }
209
Ryan Mitchell4382e442021-07-14 12:53:01 -0700210 std::optional<xml::ExtractedPackage> TransformPackageAlias(
211 const StringPiece& alias) const override {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700212 if (alias.empty()) {
213 return xml::ExtractedPackage{{}, true /*private*/};
214 }
215 for (auto it = alias_namespaces_.rbegin(); it != alias_namespaces_.rend(); ++it) {
216 if (alias == StringPiece(it->alias)) {
217 return xml::ExtractedPackage{it->package_name, it->is_private};
218 }
219 }
220 return {};
221 }
222
223 private:
224 std::vector<Macro::Namespace> alias_namespaces_;
225};
226
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700227// The symbol is visible if it is public, or if the reference to it is requesting private access
228// or if the callsite comes from the same package.
229bool IsSymbolVisible(const SymbolTable::Symbol& symbol, const Reference& ref,
230 const CallSite& callsite) {
231 if (symbol.is_public || ref.private_reference) {
232 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 }
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700234
235 if (ref.name) {
236 const ResourceName& name = ref.name.value();
237 if (name.package.empty()) {
238 // If the symbol was found, and the package is empty, that means it was found in the local
239 // scope, which is always visible (private local).
240 return true;
241 }
242
243 // The symbol is visible if the reference is local to the same package it is defined in.
244 return callsite.package == name.package;
245 }
246
247 if (ref.id && symbol.id) {
248 return ref.id.value().package_id() == symbol.id.value().package_id();
249 }
250 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800251}
252
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700253} // namespace
254
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800255const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700256 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -0700257 IAaptContext* context,
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000258 SymbolTable* symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 if (reference.name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700260 const ResourceName& name = reference.name.value();
261 if (name.package.empty()) {
262 // Use the callsite's package name if no package name was defined.
Udam Sainib228df32019-06-18 16:50:34 -0700263 const SymbolTable::Symbol* symbol = symbols->FindByName(
264 ResourceName(callsite.package, name.type, name.entry));
265 if (symbol) {
266 return symbol;
267 }
268
269 // If the callsite package is the same as the current compilation package,
270 // check the feature split dependencies as well. Feature split resources
271 // can be referenced without a namespace, just like the base package.
Udam Sainib228df32019-06-18 16:50:34 -0700272 if (callsite.package == context->GetCompilationPackage()) {
273 const auto& split_name_dependencies = context->GetSplitNameDependencies();
274 for (const std::string& split_name : split_name_dependencies) {
275 std::string split_package =
276 StringPrintf("%s.%s", callsite.package.c_str(), split_name.c_str());
277 symbol = symbols->FindByName(ResourceName(split_package, name.type, name.entry));
278 if (symbol) {
279 return symbol;
280 }
281 }
282 }
283 return nullptr;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700284 }
285 return symbols->FindByName(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 } else if (reference.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 return symbols->FindById(reference.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 } else {
289 return nullptr;
290 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800291}
292
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800293const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800294 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -0700295 IAaptContext* context,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800296 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800297 std::string* out_error) {
Udam Sainib228df32019-06-18 16:50:34 -0700298 const SymbolTable::Symbol* symbol = ResolveSymbol(reference, callsite, context, symbols);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 if (!symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 if (out_error) *out_error = "not found";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return nullptr;
302 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800303
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800304 if (!IsSymbolVisible(*symbol, reference, callsite)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 if (out_error) *out_error = "is private";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return nullptr;
307 }
308 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility(
Udam Sainib228df32019-06-18 16:50:34 -0700312 const Reference& reference, const CallSite& callsite, IAaptContext* context,
313 SymbolTable* symbols, std::string* out_error) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800314 const SymbolTable::Symbol* symbol =
Udam Sainib228df32019-06-18 16:50:34 -0700315 ResolveSymbolCheckVisibility(reference, callsite, context, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 if (!symbol) {
317 return nullptr;
318 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800319
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 return nullptr;
323 }
324 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800325}
326
Ryan Mitchell4382e442021-07-14 12:53:01 -0700327std::optional<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference,
328 const CallSite& callsite,
329 IAaptContext* context,
330 SymbolTable* symbols,
331 std::string* out_error) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800332 const SymbolTable::Symbol* symbol =
Udam Sainib228df32019-06-18 16:50:34 -0700333 ResolveAttributeCheckVisibility(reference, callsite, context, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 if (!symbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 return {};
336 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800337
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 return {};
341 }
Adam Lesinskic744ae82017-05-17 19:28:38 -0700342 return xml::AaptAttribute(*symbol->attribute, symbol->id);
Adam Lesinski467f1712015-11-16 17:35:44 -0800343}
344
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700345void ReferenceLinker::WriteAttributeName(const Reference& ref, const CallSite& callsite,
346 const xml::IPackageDeclStack* decls,
347 DiagMessage* out_msg) {
348 CHECK(out_msg != nullptr);
349 if (!ref.name) {
350 *out_msg << ref.id.value();
351 return;
352 }
353
354 const ResourceName& ref_name = ref.name.value();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000355 CHECK_EQ(ref_name.type.type, ResourceType::kAttr);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700356
357 if (!ref_name.package.empty()) {
358 *out_msg << ref_name.package << ":";
359 }
360 *out_msg << ref_name.entry;
361
362 Reference fully_qualified = ref;
363 xml::ResolvePackage(decls, &fully_qualified);
364
365 ResourceName& full_name = fully_qualified.name.value();
366 if (full_name.package.empty()) {
367 full_name.package = callsite.package;
368 }
369
370 if (full_name != ref.name.value()) {
371 *out_msg << " (aka " << full_name.package << ":" << full_name.entry << ")";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 }
Adam Lesinski28cacf02015-11-23 14:22:47 -0800373}
374
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700375std::unique_ptr<Item> ReferenceLinker::LinkReference(const CallSite& callsite,
376 const Reference& reference,
377 IAaptContext* context, SymbolTable* symbols,
378 ResourceTable* table,
379 const xml::IPackageDeclStack* decls) {
380 if (!reference.name && !reference.id) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700381 // This is @null.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700382 return std::make_unique<Reference>(reference);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700383 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800384
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700385 Reference transformed_reference = reference;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700386 xml::ResolvePackage(decls, &transformed_reference);
Adam Lesinski467f1712015-11-16 17:35:44 -0800387
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000388 if (transformed_reference.name.value().type.type == ResourceType::kMacro) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700389 if (transformed_reference.name.value().package.empty()) {
390 transformed_reference.name.value().package = callsite.package;
391 }
392
393 auto result = table->FindResource(transformed_reference.name.value());
394 if (!result || result.value().entry->values.empty()) {
395 context->GetDiagnostics()->Error(
396 DiagMessage(reference.GetSource())
397 << "failed to find definition for "
398 << LoggingResourceName(transformed_reference, callsite, decls));
399 return {};
400 }
401
402 auto& macro_values = result.value().entry->values;
403 CHECK(macro_values.size() == 1) << "Macros can only be defined in the default configuration.";
404
405 auto macro = ValueCast<Macro>(macro_values[0]->value.get());
406 CHECK(macro != nullptr) << "Value of macro resource is not a Macro (actual "
407 << *macro_values[0]->value << ")";
408
409 // Re-create the state used to parse the macro tag to compile the macro contents as if it was
410 // defined inline
411 uint32_t type_flags = 0;
412 if (reference.type_flags.has_value()) {
413 type_flags = reference.type_flags.value();
414 }
415
416 MacroDeclStack namespace_stack(macro->alias_namespaces);
417 FlattenedXmlSubTree sub_tree{.raw_value = macro->raw_value,
418 .style_string = macro->style_string,
419 .untranslatable_sections = macro->untranslatable_sections,
420 .namespace_resolver = &namespace_stack,
421 .source = macro->GetSource()};
422
423 auto new_value = ResourceParser::ParseXml(sub_tree, type_flags, reference.allow_raw, *table,
424 macro_values[0]->config, *context->GetDiagnostics());
425 if (new_value == nullptr) {
426 context->GetDiagnostics()->Error(
427 DiagMessage(reference.GetSource())
428 << "failed to substitute macro "
429 << LoggingResourceName(transformed_reference, callsite, decls)
430 << ": failed to parse contents as one of type(s) " << Attribute::MaskString(type_flags));
431 return {};
432 }
433
434 if (auto ref = ValueCast<Reference>(new_value.get())) {
435 return LinkReference(callsite, *ref, context, symbols, table, decls);
436 }
437 return new_value;
438 }
439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000441 const SymbolTable::Symbol* s =
Udam Sainib228df32019-06-18 16:50:34 -0700442 ResolveSymbolCheckVisibility(transformed_reference, callsite, context, symbols, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 if (s) {
444 // The ID may not exist. This is fine because of the possibility of building
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 // against libraries without assigned IDs.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 // Ex: Linking against own resources when building a static library.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700447 auto new_ref = std::make_unique<Reference>(reference);
448 new_ref->id = s->id;
449 new_ref->is_dynamic = s->is_dynamic;
450 return std::move(new_ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800452
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700453 context->GetDiagnostics()->Error(DiagMessage(reference.GetSource())
454 << "resource "
455 << LoggingResourceName(transformed_reference, callsite, decls)
456 << " " << err_str);
457 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -0800458}
459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800461 TRACE_NAME("ReferenceLinker::Consume");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 EmptyDeclStack decl_stack;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 bool error = false;
464 for (auto& package : table->packages) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700465 // Since we're linking, each package must have a name.
466 CHECK(!package->name.empty()) << "all packages being linked must have a name";
467
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 for (auto& type : package->types) {
469 for (auto& entry : type->entries) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700470 // First, unmangle the name if necessary.
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000471 ResourceName name(package->name, type->named_type, entry->name);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700472 NameMangler::Unmangle(&name.entry, &name.package);
473
474 // Symbol state information may be lost if there is no value for the resource.
Adam Lesinski71be7052017-12-12 16:48:07 -0800475 if (entry->visibility.level != Visibility::Level::kUndefined && entry->values.empty()) {
476 context->GetDiagnostics()->Error(DiagMessage(entry->visibility.source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800477 << "no definition for declared symbol '" << name
478 << "'");
479 error = true;
480 }
481
482 // Ensure that definitions for values declared as overlayable exist
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800483 if (entry->overlayable_item && entry->values.empty()) {
484 context->GetDiagnostics()->Error(DiagMessage(entry->overlayable_item.value().source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800485 << "no definition for overlayable symbol '"
486 << name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700488 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700490 // The context of this resource is the package in which it is defined.
491 const CallSite callsite{name.package};
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700492 ReferenceLinkerTransformer reference_transformer(callsite, context,
493 context->GetExternalSymbols(),
494 &table->string_pool, table, &decl_stack);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 for (auto& config_value : entry->values) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700497 config_value->value = config_value->value->Transform(reference_transformer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 }
499
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700500 if (reference_transformer.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 error = true;
502 }
503 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 }
506 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507}
508
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509} // namespace aapt