blob: 6ebb80f4be8f59f8d5f24695d18f12bc314db8e8 [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 Lesinski1ef0fa92017-08-15 21:32:49 -070034// Visits all references (including parents of styles, references in styles, arrays, etc) and
35// links their symbolic name to their Resource ID, performing mangling and package aliasing
36// as needed.
Adam Lesinski467f1712015-11-16 17:35:44 -080037class ReferenceVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 using ValueVisitor::Visit;
Adam Lesinski467f1712015-11-16 17:35:44 -080040
Adam Lesinskif34b6f42017-03-03 16:33:26 -080041 ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols,
42 xml::IPackageDeclStack* decls)
43 : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 void Visit(Reference* ref) override {
Adam Lesinskif34b6f42017-03-03 16:33:26 -080046 if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 error_ = true;
Adam Lesinski467f1712015-11-16 17:35:44 -080048 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 }
Adam Lesinski467f1712015-11-16 17:35:44 -080050
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070051 bool HasError() const {
52 return error_;
53 }
Adam Lesinski467f1712015-11-16 17:35:44 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor);
57
Adam Lesinskif34b6f42017-03-03 16:33:26 -080058 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 IAaptContext* context_;
60 SymbolTable* symbols_;
61 xml::IPackageDeclStack* decls_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 bool error_;
Adam Lesinski467f1712015-11-16 17:35:44 -080063};
64
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070065// Visits each xml Element and compiles the attributes within.
Adam Lesinski467f1712015-11-16 17:35:44 -080066class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 using xml::PackageAwareVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskif34b6f42017-03-03 16:33:26 -080070 XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context,
Adam Lesinskic744ae82017-05-17 19:28:38 -070071 SymbolTable* symbols)
Adam Lesinskif34b6f42017-03-03 16:33:26 -080072 : source_(source),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 callsite_(callsite),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080074 context_(context),
75 symbols_(symbols),
Adam Lesinskic744ae82017-05-17 19:28:38 -070076 reference_visitor_(callsite, context, symbols, this) {
77 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 void Visit(xml::Element* el) override {
Adam Lesinski38665542016-12-28 12:25:46 -050080 // The default Attribute allows everything except enums or flags.
81 constexpr const static uint32_t kDefaultTypeMask =
82 0xffffffffu & ~(android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS);
83 const static Attribute kDefaultAttribute(true /* weak */, kDefaultTypeMask);
84
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 const Source source = source_.WithLine(el->line_number);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski38665542016-12-28 12:25:46 -050087 // If the attribute has no namespace, interpret values as if
88 // they were assigned to the default Attribute.
89
90 const Attribute* attribute = &kDefaultAttribute;
Adam Lesinski38665542016-12-28 12:25:46 -050091
92 if (Maybe<xml::ExtractedPackage> maybe_package =
93 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
94 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070095 Reference attr_ref(
96 ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 attr_ref.private_reference = maybe_package.value().private_namespace;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 std::string err_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800100 attr.compiled_attribute =
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800101 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102
Adam Lesinski38665542016-12-28 12:25:46 -0500103 if (!attr.compiled_attribute) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700104 DiagMessage error_msg(source);
105 error_msg << "attribute ";
106 ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
107 error_msg << " " << err_str;
108 context_->GetDiagnostics()->Error(error_msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 error_ = true;
Adam Lesinski38665542016-12-28 12:25:46 -0500110 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
Adam Lesinski38665542016-12-28 12:25:46 -0500112
Adam Lesinskic744ae82017-05-17 19:28:38 -0700113 attribute = &attr.compiled_attribute.value().attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 }
115
Adam Lesinski38665542016-12-28 12:25:46 -0500116 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 if (attr.compiled_value) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700118 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 attr.compiled_value->SetSource(source);
120 attr.compiled_value->Accept(&reference_visitor_);
Adam Lesinski38665542016-12-28 12:25:46 -0500121 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
122 // We won't be able to encode this as a string.
123 DiagMessage msg(source);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700124 msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
125 << *attribute;
Adam Lesinski38665542016-12-28 12:25:46 -0500126 context_->GetDiagnostics()->Error(msg);
127 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129 }
130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800134
Adam Lesinski6b372992017-08-09 10:54:23 -0700135 bool HasError() {
136 return error_ || reference_visitor_.HasError();
137 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138
139 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
141
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800142 Source source_;
143 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 IAaptContext* context_;
145 SymbolTable* symbols_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 ReferenceVisitor reference_visitor_;
148 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149};
150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
Adam Lesinski38665542016-12-28 12:25:46 -0500153bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700154 CallSite callsite{resource->file.name.package};
155
156 std::string out_name = resource->file.name.entry;
157 NameMangler::Unmangle(&out_name, &callsite.package);
158
159 if (callsite.package.empty()) {
160 // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
161 // for example.
162 callsite.package = context->GetCompilationPackage();
163 }
164
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