blob: d68f7dd44c9f9f42d4534d2d0cc33e7a3b8d3710 [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/Linkers.h"
18
Adam Lesinski38665542016-12-28 12:25:46 -050019#include "androidfw/ResourceTypes.h"
20
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Diagnostics.h"
22#include "ResourceUtils.h"
23#include "SdkConstants.h"
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070024#include "ValueVisitor.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "process/IResourceTableConsumer.h"
27#include "process/SymbolTable.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080028#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080030#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
32namespace aapt {
33
34namespace {
35
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070036// Visits all references (including parents of styles, references in styles, arrays, etc) and
37// links their symbolic name to their Resource ID, performing mangling and package aliasing
38// as needed.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070039class ReferenceVisitor : public DescendingValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070041 using DescendingValueVisitor::Visit;
Adam Lesinski467f1712015-11-16 17:35:44 -080042
Adam Lesinskif34b6f42017-03-03 16:33:26 -080043 ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols,
44 xml::IPackageDeclStack* decls)
45 : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 void Visit(Reference* ref) override {
Adam Lesinskif34b6f42017-03-03 16:33:26 -080048 if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 error_ = true;
Adam Lesinski467f1712015-11-16 17:35:44 -080050 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 }
Adam Lesinski467f1712015-11-16 17:35:44 -080052
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070053 bool HasError() const {
54 return error_;
55 }
Adam Lesinski467f1712015-11-16 17:35:44 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor);
59
Adam Lesinskif34b6f42017-03-03 16:33:26 -080060 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 IAaptContext* context_;
62 SymbolTable* symbols_;
63 xml::IPackageDeclStack* decls_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 bool error_;
Adam Lesinski467f1712015-11-16 17:35:44 -080065};
66
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070067// Visits each xml Element and compiles the attributes within.
Adam Lesinski467f1712015-11-16 17:35:44 -080068class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 using xml::PackageAwareVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinskif34b6f42017-03-03 16:33:26 -080072 XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context,
Adam Lesinskic744ae82017-05-17 19:28:38 -070073 SymbolTable* symbols)
Adam Lesinskif34b6f42017-03-03 16:33:26 -080074 : source_(source),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 callsite_(callsite),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080076 context_(context),
77 symbols_(symbols),
Adam Lesinskic744ae82017-05-17 19:28:38 -070078 reference_visitor_(callsite, context, symbols, this) {
79 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 void Visit(xml::Element* el) override {
Adam Lesinski38665542016-12-28 12:25:46 -050082 // The default Attribute allows everything except enums or flags.
Adam Lesinski73bff1e2017-12-08 16:06:10 -080083 Attribute default_attribute(android::ResTable_map::TYPE_ANY);
84 default_attribute.SetWeak(true);
Adam Lesinski38665542016-12-28 12:25:46 -050085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 const Source source = source_.WithLine(el->line_number);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski38665542016-12-28 12:25:46 -050088 // If the attribute has no namespace, interpret values as if
89 // they were assigned to the default Attribute.
90
Adam Lesinski73bff1e2017-12-08 16:06:10 -080091 const Attribute* attribute = &default_attribute;
Adam Lesinski38665542016-12-28 12:25:46 -050092
93 if (Maybe<xml::ExtractedPackage> maybe_package =
94 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
95 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070096 Reference attr_ref(
97 ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 attr_ref.private_reference = maybe_package.value().private_namespace;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000101 attr.compiled_attribute =
102 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
Adam Lesinski38665542016-12-28 12:25:46 -0500104 if (!attr.compiled_attribute) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700105 DiagMessage error_msg(source);
106 error_msg << "attribute ";
107 ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
108 error_msg << " " << err_str;
109 context_->GetDiagnostics()->Error(error_msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 error_ = true;
Adam Lesinski38665542016-12-28 12:25:46 -0500111 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 }
Adam Lesinski38665542016-12-28 12:25:46 -0500113
Adam Lesinskic744ae82017-05-17 19:28:38 -0700114 attribute = &attr.compiled_attribute.value().attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
116
Adam Lesinski38665542016-12-28 12:25:46 -0500117 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 if (attr.compiled_value) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700119 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 attr.compiled_value->SetSource(source);
121 attr.compiled_value->Accept(&reference_visitor_);
Adam Lesinski38665542016-12-28 12:25:46 -0500122 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
123 // We won't be able to encode this as a string.
124 DiagMessage msg(source);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700125 msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
126 << *attribute;
Adam Lesinski38665542016-12-28 12:25:46 -0500127 context_->GetDiagnostics()->Error(msg);
128 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130 }
131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800135
Adam Lesinski6b372992017-08-09 10:54:23 -0700136 bool HasError() {
137 return error_ || reference_visitor_.HasError();
138 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139
140 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
142
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800143 Source source_;
144 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 IAaptContext* context_;
146 SymbolTable* symbols_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 ReferenceVisitor reference_visitor_;
149 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150};
151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
Adam Lesinski38665542016-12-28 12:25:46 -0500154bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800155 TRACE_NAME("XmlReferenceLinker::Consume");
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700156 CallSite callsite{resource->file.name.package};
157
158 std::string out_name = resource->file.name.entry;
159 NameMangler::Unmangle(&out_name, &callsite.package);
160
161 if (callsite.package.empty()) {
162 // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
163 // for example.
164 callsite.package = context->GetCompilationPackage();
165 }
166
Adam Lesinskic744ae82017-05-17 19:28:38 -0700167 XmlVisitor visitor(resource->file.source, callsite, context, context->GetExternalSymbols());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 if (resource->root) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 resource->root->Accept(&visitor);
170 return !visitor.HasError();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
172 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173}
174
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175} // namespace aapt