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 | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 24 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "process/IResourceTableConsumer.h" |
| 26 | #include "process/SymbolTable.h" |
| 27 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 28 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | namespace { |
| 33 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 34 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | * 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 Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 39 | * as needed. |
| 40 | */ |
| 41 | class ReferenceVisitor : public ValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | using ValueVisitor::Visit; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 45 | ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, xml::IPackageDeclStack* decls, |
| 46 | CallSite* callsite) |
| 47 | : context_(context), symbols_(symbols), decls_(decls), callsite_(callsite), error_(false) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | void Visit(Reference* ref) override { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 50 | if (!ReferenceLinker::LinkReference(ref, context_, symbols_, decls_, callsite_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | error_ = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | bool HasError() const { return error_; } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor); |
| 59 | |
| 60 | IAaptContext* context_; |
| 61 | SymbolTable* symbols_; |
| 62 | xml::IPackageDeclStack* decls_; |
| 63 | CallSite* callsite_; |
| 64 | bool error_; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Visits each xml Element and compiles the attributes within. |
| 69 | */ |
| 70 | class XmlVisitor : public xml::PackageAwareVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | using xml::PackageAwareVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | void Visit(xml::Element* el) override { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 84 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | const Source source = source_.WithLine(el->line_number); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 91 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | // Empty package means the 'current' or 'local' package. |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 103 | attribute_package = context_->GetCompilationPackage(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 106 | Reference attr_ref(ResourceNameRef(attribute_package, ResourceType::kAttr, attr.name)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | attr_ref.private_reference = maybe_package.value().private_namespace; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | std::string err_str; |
| 110 | attr.compiled_attribute = ReferenceLinker::CompileXmlAttribute( |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 111 | attr_ref, context_->GetNameMangler(), symbols_, callsite_, &err_str); |
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 | if (!attr.compiled_attribute) { |
| 114 | context_->GetDiagnostics()->Error(DiagMessage(source) << "attribute '" |
| 115 | << attribute_package << ":" |
| 116 | << attr.name << "' " << err_str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | error_ = true; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 118 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | } |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 120 | |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 133 | attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | if (attr.compiled_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | // With a compiledValue, we must resolve the reference and assign it an |
| 136 | // ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | attr.compiled_value->SetSource(source); |
| 138 | attr.compiled_value->Accept(&reference_visitor_); |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 139 | } 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | // Call the super implementation. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | xml::PackageAwareVisitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | bool HasError() { return error_ || reference_visitor_.HasError(); } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | |
| 159 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 173 | bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | sdk_levels_found_.clear(); |
| 175 | CallSite callsite = {resource->file.name}; |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame^] | 176 | XmlVisitor visitor(context, context->GetExternalSymbols(), resource->file.source, |
| 177 | &sdk_levels_found_, &callsite); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | if (resource->root) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | resource->root->Accept(&visitor); |
| 180 | return !visitor.HasError(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } |
| 182 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | } // namespace aapt |