blob: 1dbe53c02ecfd70f7fdaeb95880942b867b747ce [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 Lesinski38665542016-12-28 12:25:46 -050045 ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, xml::IPackageDeclStack* decls,
46 CallSite* callsite)
47 : context_(context), symbols_(symbols), decls_(decls), callsite_(callsite), error_(false) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void Visit(Reference* ref) override {
Adam Lesinski38665542016-12-28 12:25:46 -050050 if (!ReferenceLinker::LinkReference(ref, context_, symbols_, decls_, callsite_)) {
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
60 IAaptContext* context_;
61 SymbolTable* symbols_;
62 xml::IPackageDeclStack* decls_;
63 CallSite* callsite_;
64 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 Lesinskicacb28f2016-10-19 12:18:14 -070074 XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::set<int>* sdk_levels_found, CallSite* callsite)
76 : context_(context),
77 symbols_(symbols),
78 source_(source),
79 sdk_levels_found_(sdk_levels_found),
80 callsite_(callsite),
81 reference_visitor_(context, symbols, this, callsite) {}
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;
110 attr.compiled_attribute = ReferenceLinker::CompileXmlAttribute(
Adam Lesinski38665542016-12-28 12:25:46 -0500111 attr_ref, context_->GetNameMangler(), symbols_, callsite_, &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
121 // Find this compiled attribute's SDK level.
122 const xml::AaptAttribute& aapt_attr = attr.compiled_attribute.value();
123 if (aapt_attr.id) {
124 // Record all SDK levels from which the attributes were defined.
125 const size_t sdk_level = FindAttributeSdkLevel(aapt_attr.id.value());
126 if (sdk_level > 1) {
127 sdk_levels_found_->insert(sdk_level);
128 }
129 }
130 attribute = &aapt_attr.attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 }
132
Adam Lesinski38665542016-12-28 12:25:46 -0500133 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 if (attr.compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 // With a compiledValue, we must resolve the reference and assign it an
136 // ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 attr.compiled_value->SetSource(source);
138 attr.compiled_value->Accept(&reference_visitor_);
Adam Lesinski38665542016-12-28 12:25:46 -0500139 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
140 // We won't be able to encode this as a string.
141 DiagMessage msg(source);
142 msg << "'" << attr.value << "' "
143 << "is incompatible with attribute ";
144 if (!attribute_package.empty()) {
145 msg << attribute_package << ":";
146 }
147 msg << attr.name << " " << *attribute;
148 context_->GetDiagnostics()->Error(msg);
149 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 }
152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 bool HasError() { return error_ || reference_visitor_.HasError(); }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158
159 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
161
162 IAaptContext* context_;
163 SymbolTable* symbols_;
164 Source source_;
165 std::set<int>* sdk_levels_found_;
166 CallSite* callsite_;
167 ReferenceVisitor reference_visitor_;
168 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169};
170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinski38665542016-12-28 12:25:46 -0500173bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 sdk_levels_found_.clear();
175 CallSite callsite = {resource->file.name};
Adam Lesinski38665542016-12-28 12:25:46 -0500176 XmlVisitor visitor(context, context->GetExternalSymbols(), resource->file.source,
177 &sdk_levels_found_, &callsite);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 if (resource->root) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 resource->root->Accept(&visitor);
180 return !visitor.HasError();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
182 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183}
184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185} // namespace aapt