Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "link/Linkers.h" |
| 18 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 19 | #include "androidfw/ResourceTypes.h" |
| 20 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "Diagnostics.h" |
| 22 | #include "ResourceUtils.h" |
| 23 | #include "SdkConstants.h" |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 24 | #include "ValueVisitor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "process/SymbolTable.h" |
| 28 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 29 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
| 31 | namespace aapt { |
| 32 | |
| 33 | namespace { |
| 34 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 35 | // Visits all references (including parents of styles, references in styles, arrays, etc) and |
| 36 | // links their symbolic name to their Resource ID, performing mangling and package aliasing |
| 37 | // as needed. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 38 | class ReferenceVisitor : public DescendingValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 40 | using DescendingValueVisitor::Visit; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 42 | ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols, |
| 43 | xml::IPackageDeclStack* decls) |
| 44 | : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | void Visit(Reference* ref) override { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 47 | if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | error_ = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 49 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 51 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 52 | bool HasError() const { |
| 53 | return error_; |
| 54 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor); |
| 58 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 59 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | IAaptContext* context_; |
| 61 | SymbolTable* symbols_; |
| 62 | xml::IPackageDeclStack* decls_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | bool error_; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 66 | // Visits each xml Element and compiles the attributes within. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 67 | class XmlVisitor : public xml::PackageAwareVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | using xml::PackageAwareVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 71 | XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 72 | SymbolTable* symbols) |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 73 | : source_(source), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | callsite_(callsite), |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 75 | context_(context), |
| 76 | symbols_(symbols), |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 77 | reference_visitor_(callsite, context, symbols, this) { |
| 78 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | void Visit(xml::Element* el) override { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 81 | // The default Attribute allows everything except enums or flags. |
| 82 | constexpr const static uint32_t kDefaultTypeMask = |
| 83 | 0xffffffffu & ~(android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS); |
| 84 | const static Attribute kDefaultAttribute(true /* weak */, kDefaultTypeMask); |
| 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | const Source source = source_.WithLine(el->line_number); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 88 | // If the attribute has no namespace, interpret values as if |
| 89 | // they were assigned to the default Attribute. |
| 90 | |
| 91 | const Attribute* attribute = &kDefaultAttribute; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 92 | |
| 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 Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 96 | Reference attr_ref( |
| 97 | ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | attr_ref.private_reference = maybe_package.value().private_namespace; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | std::string err_str; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 101 | attr.compiled_attribute = |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 102 | ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, symbols_, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 104 | if (!attr.compiled_attribute) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 105 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | error_ = true; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 111 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | } |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 113 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 114 | attribute = &attr.compiled_attribute.value().attribute; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 117 | attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | if (attr.compiled_value) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 119 | // With a compiledValue, we must resolve the reference and assign it an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | attr.compiled_value->SetSource(source); |
| 121 | attr.compiled_value->Accept(&reference_visitor_); |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 122 | } 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 Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 125 | msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " " |
| 126 | << *attribute; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 127 | context_->GetDiagnostics()->Error(msg); |
| 128 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | // Call the super implementation. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | xml::PackageAwareVisitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 135 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 136 | bool HasError() { |
| 137 | return error_ || reference_visitor_.HasError(); |
| 138 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | |
| 140 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(XmlVisitor); |
| 142 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 143 | Source source_; |
| 144 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | IAaptContext* context_; |
| 146 | SymbolTable* symbols_; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | ReferenceVisitor reference_visitor_; |
| 149 | bool error_ = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 154 | bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 155 | CallSite callsite{resource->file.name.package}; |
| 156 | |
| 157 | std::string out_name = resource->file.name.entry; |
| 158 | NameMangler::Unmangle(&out_name, &callsite.package); |
| 159 | |
| 160 | if (callsite.package.empty()) { |
| 161 | // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml |
| 162 | // for example. |
| 163 | callsite.package = context->GetCompilationPackage(); |
| 164 | } |
| 165 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 166 | XmlVisitor visitor(resource->file.source, callsite, context, context->GetExternalSymbols()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | if (resource->root) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | resource->root->Accept(&visitor); |
| 169 | return !visitor.HasError(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | } |
| 171 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | } // namespace aapt |