blob: 28f09aa4836520309fd6bf593256b99ef3b581e4 [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"
20#include "androidfw/ResourceTypes.h"
21
Adam Lesinskicacb28f2016-10-19 12:18:14 -070022#include "Diagnostics.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceTable.h"
24#include "ResourceUtils.h"
25#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "link/Linkers.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "process/IResourceTableConsumer.h"
29#include "process/SymbolTable.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080030#include "trace/TraceBuffer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "util/Util.h"
32#include "xml/XmlUtil.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033
Adam Lesinski2eed52e2018-02-21 15:55:58 -080034using ::aapt::ResourceUtils::StringBuilder;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070035using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037namespace aapt {
38
39namespace {
40
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070041// The ReferenceLinkerVisitor will follow all references and make sure they point
42// to resources that actually exist, either in the local resource table, or as external
43// symbols. Once the target resource has been found, the ID of the resource will be assigned
44// to the reference object.
45//
46// NOTE: All of the entries in the ResourceTable must be assigned IDs.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070047class ReferenceLinkerVisitor : public DescendingValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070049 using DescendingValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskif34b6f42017-03-03 16:33:26 -080051 ReferenceLinkerVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols,
52 StringPool* string_pool, xml::IPackageDeclStack* decl)
53 : callsite_(callsite),
54 context_(context),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 symbols_(symbols),
56 package_decls_(decl),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080057 string_pool_(string_pool) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 void Visit(Reference* ref) override {
Adam Lesinskif34b6f42017-03-03 16:33:26 -080060 if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, package_decls_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
63 }
64
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070065 // We visit the Style specially because during this phase, values of attributes are
66 // all RawString values. Now that we are expected to resolve all symbols, we can
67 // lookup the attributes to find out which types are allowed for the attributes' values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 Visit(&style->parent.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 }
72
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::string err_str;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070076 // Transform the attribute reference so that it is using the fully qualified package
77 // name. This will also mark the reference as being able to see private resources if
78 // there was a '*' in the reference or if the package came from the private namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 Reference transformed_reference = entry.key;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070080 ResolvePackage(package_decls_, &transformed_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070082 // Find the attribute in the symbol table and check if it is visible from this callsite.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080083 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveAttributeCheckVisibility(
Chris Warrington58e2fbf2018-07-23 14:12:20 +000084 transformed_reference, callsite_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 if (symbol) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070086 // Assign our style key the correct ID. The ID may not exist.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 entry.key.id = symbol->id;
88
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070089 // Try to convert the value to a more specific, typed value based on the attribute it is
90 // set to.
Adam Lesinskif34b6f42017-03-03 16:33:26 -080091 entry.value = ParseValueWithAttribute(std::move(entry.value), symbol->attribute.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092
93 // Link/resolve the final value (mostly if it's a reference).
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 entry.value->Accept(this);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095
96 // Now verify that the type of this item is compatible with the
Adam Lesinski3124e7c2017-06-13 16:03:55 -070097 // attribute it is defined for. We pass `nullptr` as the DiagMessage so that this
98 // check is fast and we avoid creating a DiagMessage when the match is successful.
99 if (!symbol->attribute->Matches(*entry.value, nullptr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 // The actual type of this item is incompatible with the attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 DiagMessage msg(entry.key.GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700103 // Call the matches method again, this time with a DiagMessage so we fill in the actual
104 // error message.
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700105 symbol->attribute->Matches(*entry.value, &msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 context_->GetDiagnostics()->Error(msg);
107 error_ = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108 }
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 DiagMessage msg(entry.key.GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 msg << "style attribute '";
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700113 ReferenceLinker::WriteResourceName(entry.key, callsite_, package_decls_, &msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 msg << "' " << err_str;
115 context_->GetDiagnostics()->Error(msg);
116 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
118 }
119 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800120
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700121 bool HasError() {
122 return error_;
123 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 DISALLOW_COPY_AND_ASSIGN(ReferenceLinkerVisitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700128 // Transform a RawString value into a more specific, appropriate value, based on the
129 // Attribute. If a non RawString value is passed in, this is an identity transform.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 std::unique_ptr<Item> ParseValueWithAttribute(std::unique_ptr<Item> value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 const Attribute* attr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 if (RawString* raw_string = ValueCast<RawString>(value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 std::unique_ptr<Item> transformed =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ResourceUtils::TryParseItemForAttribute(*raw_string->value, attr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 // If we could not parse as any specific type, try a basic STRING.
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800137 if (!transformed && (attr->type_mask & android::ResTable_map::TYPE_STRING)) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800138 StringBuilder string_builder;
139 string_builder.AppendText(*raw_string->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 if (string_builder) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800141 transformed =
142 util::make_unique<String>(string_pool_->MakeRef(string_builder.to_string()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 if (transformed) {
147 return transformed;
148 }
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 return value;
151 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800153 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 IAaptContext* context_;
155 SymbolTable* symbols_;
156 xml::IPackageDeclStack* package_decls_;
157 StringPool* string_pool_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 bool error_ = false;
159};
160
161class EmptyDeclStack : public xml::IPackageDeclStack {
162 public:
163 EmptyDeclStack() = default;
164
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700165 Maybe<xml::ExtractedPackage> TransformPackageAlias(const StringPiece& alias) const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (alias.empty()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700167 return xml::ExtractedPackage{{}, true /*private*/};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 }
169 return {};
170 }
171
172 private:
173 DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174};
175
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700176// The symbol is visible if it is public, or if the reference to it is requesting private access
177// or if the callsite comes from the same package.
178bool IsSymbolVisible(const SymbolTable::Symbol& symbol, const Reference& ref,
179 const CallSite& callsite) {
180 if (symbol.is_public || ref.private_reference) {
181 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700183
184 if (ref.name) {
185 const ResourceName& name = ref.name.value();
186 if (name.package.empty()) {
187 // If the symbol was found, and the package is empty, that means it was found in the local
188 // scope, which is always visible (private local).
189 return true;
190 }
191
192 // The symbol is visible if the reference is local to the same package it is defined in.
193 return callsite.package == name.package;
194 }
195
196 if (ref.id && symbol.id) {
197 return ref.id.value().package_id() == symbol.id.value().package_id();
198 }
199 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800200}
201
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700202} // namespace
203
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800204const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700205 const CallSite& callsite,
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000206 SymbolTable* symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 if (reference.name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700208 const ResourceName& name = reference.name.value();
209 if (name.package.empty()) {
210 // Use the callsite's package name if no package name was defined.
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000211 return symbols->FindByName(ResourceName(callsite.package, name.type, name.entry));
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700212 }
213 return symbols->FindByName(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 } else if (reference.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 return symbols->FindById(reference.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 } else {
217 return nullptr;
218 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800219}
220
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800221const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800222 const CallSite& callsite,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800223 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800224 std::string* out_error) {
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000225 const SymbolTable::Symbol* symbol = ResolveSymbol(reference, callsite, symbols);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 if (!symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 if (out_error) *out_error = "not found";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 return nullptr;
229 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800230
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800231 if (!IsSymbolVisible(*symbol, reference, callsite)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 if (out_error) *out_error = "is private";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 return nullptr;
234 }
235 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800236}
237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility(
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000239 const Reference& reference, const CallSite& callsite, SymbolTable* symbols,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800240 std::string* out_error) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800241 const SymbolTable::Symbol* symbol =
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000242 ResolveSymbolCheckVisibility(reference, callsite, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 if (!symbol) {
244 return nullptr;
245 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 return nullptr;
250 }
251 return symbol;
Adam Lesinski467f1712015-11-16 17:35:44 -0800252}
253
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800254Maybe<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800255 const CallSite& callsite,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800256 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800257 std::string* out_error) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800258 const SymbolTable::Symbol* symbol =
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000259 ResolveAttributeCheckVisibility(reference, callsite, symbols, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 if (!symbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 return {};
262 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 if (!symbol->attribute) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 if (out_error) *out_error = "is not an attribute";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 return {};
267 }
Adam Lesinskic744ae82017-05-17 19:28:38 -0700268 return xml::AaptAttribute(*symbol->attribute, symbol->id);
Adam Lesinski467f1712015-11-16 17:35:44 -0800269}
270
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700271void ReferenceLinker::WriteResourceName(const Reference& ref, const CallSite& callsite,
272 const xml::IPackageDeclStack* decls, DiagMessage* out_msg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 CHECK(out_msg != nullptr);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700274 if (!ref.name) {
275 *out_msg << ref.id.value();
276 return;
277 }
Adam Lesinski28cacf02015-11-23 14:22:47 -0800278
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700279 *out_msg << ref.name.value();
280
281 Reference fully_qualified = ref;
282 xml::ResolvePackage(decls, &fully_qualified);
283
284 ResourceName& full_name = fully_qualified.name.value();
285 if (full_name.package.empty()) {
286 full_name.package = callsite.package;
287 }
288
289 if (full_name != ref.name.value()) {
290 *out_msg << " (aka " << full_name << ")";
291 }
292}
293
294void ReferenceLinker::WriteAttributeName(const Reference& ref, const CallSite& callsite,
295 const xml::IPackageDeclStack* decls,
296 DiagMessage* out_msg) {
297 CHECK(out_msg != nullptr);
298 if (!ref.name) {
299 *out_msg << ref.id.value();
300 return;
301 }
302
303 const ResourceName& ref_name = ref.name.value();
304 CHECK_EQ(ref_name.type, ResourceType::kAttr);
305
306 if (!ref_name.package.empty()) {
307 *out_msg << ref_name.package << ":";
308 }
309 *out_msg << ref_name.entry;
310
311 Reference fully_qualified = ref;
312 xml::ResolvePackage(decls, &fully_qualified);
313
314 ResourceName& full_name = fully_qualified.name.value();
315 if (full_name.package.empty()) {
316 full_name.package = callsite.package;
317 }
318
319 if (full_name != ref.name.value()) {
320 *out_msg << " (aka " << full_name.package << ":" << full_name.entry << ")";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 }
Adam Lesinski28cacf02015-11-23 14:22:47 -0800322}
323
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800324bool ReferenceLinker::LinkReference(const CallSite& callsite, Reference* reference,
325 IAaptContext* context, SymbolTable* symbols,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700326 const xml::IPackageDeclStack* decls) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 CHECK(reference != nullptr);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700328 if (!reference->name && !reference->id) {
329 // This is @null.
330 return true;
331 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 Reference transformed_reference = *reference;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700334 xml::ResolvePackage(decls, &transformed_reference);
Adam Lesinski467f1712015-11-16 17:35:44 -0800335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000337 const SymbolTable::Symbol* s =
338 ResolveSymbolCheckVisibility(transformed_reference, callsite, symbols, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 if (s) {
340 // The ID may not exist. This is fine because of the possibility of building
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 // against libraries without assigned IDs.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 // Ex: Linking against own resources when building a static library.
343 reference->id = s->id;
Todd Kennedy32512992018-04-25 16:45:59 -0700344 reference->is_dynamic = s->is_dynamic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 return true;
346 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 DiagMessage error_msg(reference->GetSource());
349 error_msg << "resource ";
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700350 WriteResourceName(*reference, callsite, decls, &error_msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 error_msg << " " << err_str;
352 context->GetDiagnostics()->Error(error_msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800354}
355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800357 TRACE_NAME("ReferenceLinker::Consume");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 EmptyDeclStack decl_stack;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 bool error = false;
360 for (auto& package : table->packages) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700361 // Since we're linking, each package must have a name.
362 CHECK(!package->name.empty()) << "all packages being linked must have a name";
363
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 for (auto& type : package->types) {
365 for (auto& entry : type->entries) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700366 // First, unmangle the name if necessary.
367 ResourceName name(package->name, type->type, entry->name);
368 NameMangler::Unmangle(&name.entry, &name.package);
369
370 // Symbol state information may be lost if there is no value for the resource.
Adam Lesinski71be7052017-12-12 16:48:07 -0800371 if (entry->visibility.level != Visibility::Level::kUndefined && entry->values.empty()) {
372 context->GetDiagnostics()->Error(DiagMessage(entry->visibility.source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800373 << "no definition for declared symbol '" << name
374 << "'");
375 error = true;
376 }
377
378 // Ensure that definitions for values declared as overlayable exist
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800379 if (entry->overlayable_item && entry->values.empty()) {
380 context->GetDiagnostics()->Error(DiagMessage(entry->overlayable_item.value().source)
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800381 << "no definition for overlayable symbol '"
382 << name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700386 // The context of this resource is the package in which it is defined.
387 const CallSite callsite{name.package};
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800388 ReferenceLinkerVisitor visitor(callsite, context, context->GetExternalSymbols(),
389 &table->string_pool, &decl_stack);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 for (auto& config_value : entry->values) {
392 config_value->value->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 }
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 if (visitor.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 error = true;
397 }
398 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 }
401 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700402}
403
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404} // namespace aapt