blob: 721fc26399bcc1fe0e05856f0bd4926dc89b95b4 [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 Lesinski467f1712015-11-16 17:35:44 -080024#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "process/IResourceTableConsumer.h"
26#include "process/SymbolTable.h"
27#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080028#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
30namespace aapt {
31
32namespace {
33
Adam Lesinski467f1712015-11-16 17:35:44 -080034/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 * Visits all references (including parents of styles, references in styles,
36 * arrays, etc) and
37 * links their symbolic name to their Resource ID, performing mangling and
38 * package aliasing
Adam Lesinski467f1712015-11-16 17:35:44 -080039 * as needed.
40 */
41class ReferenceVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 using ValueVisitor::Visit;
Adam Lesinski467f1712015-11-16 17:35:44 -080044
Adam Lesinskif34b6f42017-03-03 16:33:26 -080045 ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols,
46 xml::IPackageDeclStack* decls)
47 : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void Visit(Reference* ref) override {
Adam Lesinskif34b6f42017-03-03 16:33:26 -080050 if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 error_ = true;
Adam Lesinski467f1712015-11-16 17:35:44 -080052 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinski467f1712015-11-16 17:35:44 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 bool HasError() const { return error_; }
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
67/**
68 * Visits each xml Element and compiles the attributes within.
69 */
70class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 using xml::PackageAwareVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073
Adam Lesinskif34b6f42017-03-03 16:33:26 -080074 XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context,
Adam Lesinskic744ae82017-05-17 19:28:38 -070075 SymbolTable* symbols)
Adam Lesinskif34b6f42017-03-03 16:33:26 -080076 : source_(source),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 callsite_(callsite),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080078 context_(context),
79 symbols_(symbols),
Adam Lesinskic744ae82017-05-17 19:28:38 -070080 reference_visitor_(callsite, context, symbols, this) {
81 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 void Visit(xml::Element* el) override {
Adam Lesinski38665542016-12-28 12:25:46 -050084 // The default Attribute allows everything except enums or flags.
85 constexpr const static uint32_t kDefaultTypeMask =
86 0xffffffffu & ~(android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS);
87 const static Attribute kDefaultAttribute(true /* weak */, kDefaultTypeMask);
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 const Source source = source_.WithLine(el->line_number);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski38665542016-12-28 12:25:46 -050091 // If the attribute has no namespace, interpret values as if
92 // they were assigned to the default Attribute.
93
94 const Attribute* attribute = &kDefaultAttribute;
95 std::string attribute_package;
96
97 if (Maybe<xml::ExtractedPackage> maybe_package =
98 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
99 // There is a valid package name for this attribute. We will look this up.
100 attribute_package = maybe_package.value().package;
101 if (attribute_package.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // Empty package means the 'current' or 'local' package.
Adam Lesinski38665542016-12-28 12:25:46 -0500103 attribute_package = context_->GetCompilationPackage();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 }
105
Adam Lesinski38665542016-12-28 12:25:46 -0500106 Reference attr_ref(ResourceNameRef(attribute_package, ResourceType::kAttr, attr.name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 attr_ref.private_reference = maybe_package.value().private_namespace;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 std::string err_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800110 attr.compiled_attribute =
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800111 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112
Adam Lesinski38665542016-12-28 12:25:46 -0500113 if (!attr.compiled_attribute) {
114 context_->GetDiagnostics()->Error(DiagMessage(source) << "attribute '"
115 << attribute_package << ":"
116 << attr.name << "' " << err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 error_ = true;
Adam Lesinski38665542016-12-28 12:25:46 -0500118 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Adam Lesinski38665542016-12-28 12:25:46 -0500120
Adam Lesinskic744ae82017-05-17 19:28:38 -0700121 attribute = &attr.compiled_attribute.value().attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 }
123
Adam Lesinski38665542016-12-28 12:25:46 -0500124 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 if (attr.compiled_value) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700126 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 attr.compiled_value->SetSource(source);
128 attr.compiled_value->Accept(&reference_visitor_);
Adam Lesinski38665542016-12-28 12:25:46 -0500129 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
130 // We won't be able to encode this as a string.
131 DiagMessage msg(source);
132 msg << "'" << attr.value << "' "
133 << "is incompatible with attribute ";
134 if (!attribute_package.empty()) {
135 msg << attribute_package << ":";
136 }
137 msg << attr.name << " " << *attribute;
138 context_->GetDiagnostics()->Error(msg);
139 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141 }
142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 bool HasError() { return error_ || reference_visitor_.HasError(); }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148
149 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
151
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800152 Source source_;
153 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 IAaptContext* context_;
155 SymbolTable* symbols_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 ReferenceVisitor reference_visitor_;
158 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159};
160
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinski38665542016-12-28 12:25:46 -0500163bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800164 const CallSite callsite = {resource->file.name};
Adam Lesinskic744ae82017-05-17 19:28:38 -0700165 XmlVisitor visitor(resource->file.source, callsite, context, context->GetExternalSymbols());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 if (resource->root) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 resource->root->Accept(&visitor);
168 return !visitor.HasError();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 }
170 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171}
172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173} // namespace aapt