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/ReferenceLinker.h" |
| 18 | |
| 19 | #include "android-base/logging.h" |
| 20 | #include "androidfw/ResourceTypes.h" |
| 21 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 22 | #include "Diagnostics.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "ResourceTable.h" |
| 24 | #include "ResourceUtils.h" |
| 25 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "link/Linkers.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "process/IResourceTableConsumer.h" |
| 29 | #include "process/SymbolTable.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 30 | #include "util/Util.h" |
| 31 | #include "xml/XmlUtil.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 33 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | namespace aapt { |
| 36 | |
| 37 | namespace { |
| 38 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 39 | // The ReferenceLinkerVisitor will follow all references and make sure they point |
| 40 | // to resources that actually exist, either in the local resource table, or as external |
| 41 | // symbols. Once the target resource has been found, the ID of the resource will be assigned |
| 42 | // to the reference object. |
| 43 | // |
| 44 | // NOTE: All of the entries in the ResourceTable must be assigned IDs. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 45 | class ReferenceLinkerVisitor : public DescendingValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame^] | 47 | using DescendingValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 49 | ReferenceLinkerVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols, |
| 50 | StringPool* string_pool, xml::IPackageDeclStack* decl) |
| 51 | : callsite_(callsite), |
| 52 | context_(context), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | symbols_(symbols), |
| 54 | package_decls_(decl), |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 55 | string_pool_(string_pool) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | void Visit(Reference* ref) override { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 58 | if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, package_decls_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 63 | // We visit the Style specially because during this phase, values of attributes are |
| 64 | // all RawString values. Now that we are expected to resolve all symbols, we can |
| 65 | // lookup the attributes to find out which types are allowed for the attributes' values. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | void Visit(Style* style) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | if (style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | Visit(&style->parent.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | for (Style::Entry& entry : style->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | std::string err_str; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 74 | // Transform the attribute reference so that it is using the fully qualified package |
| 75 | // name. This will also mark the reference as being able to see private resources if |
| 76 | // there was a '*' in the reference or if the package came from the private namespace. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | Reference transformed_reference = entry.key; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 78 | ResolvePackage(package_decls_, &transformed_reference); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 80 | // Find the attribute in the symbol table and check if it is visible from this callsite. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 81 | const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveAttributeCheckVisibility( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 82 | transformed_reference, callsite_, symbols_, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | if (symbol) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 84 | // Assign our style key the correct ID. The ID may not exist. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | entry.key.id = symbol->id; |
| 86 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 87 | // Try to convert the value to a more specific, typed value based on the attribute it is |
| 88 | // set to. |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 89 | entry.value = ParseValueWithAttribute(std::move(entry.value), symbol->attribute.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | |
| 91 | // Link/resolve the final value (mostly if it's a reference). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | entry.value->Accept(this); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | |
| 94 | // Now verify that the type of this item is compatible with the |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 95 | // attribute it is defined for. We pass `nullptr` as the DiagMessage so that this |
| 96 | // check is fast and we avoid creating a DiagMessage when the match is successful. |
| 97 | if (!symbol->attribute->Matches(*entry.value, nullptr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | // The actual type of this item is incompatible with the attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | DiagMessage msg(entry.key.GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 101 | // Call the matches method again, this time with a DiagMessage so we fill in the actual |
| 102 | // error message. |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 103 | symbol->attribute->Matches(*entry.value, &msg); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | context_->GetDiagnostics()->Error(msg); |
| 105 | error_ = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | DiagMessage msg(entry.key.GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | msg << "style attribute '"; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 111 | ReferenceLinker::WriteResourceName(entry.key, callsite_, package_decls_, &msg); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | msg << "' " << err_str; |
| 113 | context_->GetDiagnostics()->Error(msg); |
| 114 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 119 | bool HasError() { |
| 120 | return error_; |
| 121 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 122 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | DISALLOW_COPY_AND_ASSIGN(ReferenceLinkerVisitor); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 126 | // Transform a RawString value into a more specific, appropriate value, based on the |
| 127 | // Attribute. If a non RawString value is passed in, this is an identity transform. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | std::unique_ptr<Item> ParseValueWithAttribute(std::unique_ptr<Item> value, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | const Attribute* attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | if (RawString* raw_string = ValueCast<RawString>(value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | std::unique_ptr<Item> transformed = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | ResourceUtils::TryParseItemForAttribute(*raw_string->value, attr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | // If we could not parse as any specific type, try a basic STRING. |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 135 | if (!transformed && (attr->type_mask & android::ResTable_map::TYPE_STRING)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | util::StringBuilder string_builder; |
| 137 | string_builder.Append(*raw_string->value); |
| 138 | if (string_builder) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 139 | transformed = util::make_unique<String>(string_pool_->MakeRef(string_builder.ToString())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | if (transformed) { |
| 144 | return transformed; |
| 145 | } |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 146 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | return value; |
| 148 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 150 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | IAaptContext* context_; |
| 152 | SymbolTable* symbols_; |
| 153 | xml::IPackageDeclStack* package_decls_; |
| 154 | StringPool* string_pool_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | bool error_ = false; |
| 156 | }; |
| 157 | |
| 158 | class EmptyDeclStack : public xml::IPackageDeclStack { |
| 159 | public: |
| 160 | EmptyDeclStack() = default; |
| 161 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 162 | Maybe<xml::ExtractedPackage> TransformPackageAlias(const StringPiece& alias) const override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | if (alias.empty()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 164 | return xml::ExtractedPackage{{}, true /*private*/}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | } |
| 166 | return {}; |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 173 | // The symbol is visible if it is public, or if the reference to it is requesting private access |
| 174 | // or if the callsite comes from the same package. |
| 175 | bool IsSymbolVisible(const SymbolTable::Symbol& symbol, const Reference& ref, |
| 176 | const CallSite& callsite) { |
| 177 | if (symbol.is_public || ref.private_reference) { |
| 178 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | } |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 180 | |
| 181 | if (ref.name) { |
| 182 | const ResourceName& name = ref.name.value(); |
| 183 | if (name.package.empty()) { |
| 184 | // If the symbol was found, and the package is empty, that means it was found in the local |
| 185 | // scope, which is always visible (private local). |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | // The symbol is visible if the reference is local to the same package it is defined in. |
| 190 | return callsite.package == name.package; |
| 191 | } |
| 192 | |
| 193 | if (ref.id && symbol.id) { |
| 194 | return ref.id.value().package_id() == symbol.id.value().package_id(); |
| 195 | } |
| 196 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 199 | } // namespace |
| 200 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 201 | const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference, |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 202 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 203 | SymbolTable* symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | if (reference.name) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 205 | const ResourceName& name = reference.name.value(); |
| 206 | if (name.package.empty()) { |
| 207 | // Use the callsite's package name if no package name was defined. |
| 208 | return symbols->FindByName(ResourceName(callsite.package, name.type, name.entry)); |
| 209 | } |
| 210 | return symbols->FindByName(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | } else if (reference.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 212 | return symbols->FindById(reference.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | } else { |
| 214 | return nullptr; |
| 215 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 218 | const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 219 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 220 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 221 | std::string* out_error) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 222 | const SymbolTable::Symbol* symbol = ResolveSymbol(reference, callsite, symbols); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | if (!symbol) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | if (out_error) *out_error = "not found"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | return nullptr; |
| 226 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 227 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 228 | if (!IsSymbolVisible(*symbol, reference, callsite)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | if (out_error) *out_error = "is private"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
| 232 | return symbol; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 236 | const Reference& reference, const CallSite& callsite, SymbolTable* symbols, |
| 237 | std::string* out_error) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 238 | const SymbolTable::Symbol* symbol = |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 239 | ResolveSymbolCheckVisibility(reference, callsite, symbols, out_error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | if (!symbol) { |
| 241 | return nullptr; |
| 242 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | if (!symbol->attribute) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | if (out_error) *out_error = "is not an attribute"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | return nullptr; |
| 247 | } |
| 248 | return symbol; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 251 | Maybe<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 252 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 253 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 254 | std::string* out_error) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 255 | const SymbolTable::Symbol* symbol = |
| 256 | ResolveAttributeCheckVisibility(reference, callsite, symbols, out_error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | if (!symbol) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | return {}; |
| 259 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 260 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | if (!symbol->attribute) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | if (out_error) *out_error = "is not an attribute"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | return {}; |
| 264 | } |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 265 | return xml::AaptAttribute(*symbol->attribute, symbol->id); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 268 | void ReferenceLinker::WriteResourceName(const Reference& ref, const CallSite& callsite, |
| 269 | const xml::IPackageDeclStack* decls, DiagMessage* out_msg) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | CHECK(out_msg != nullptr); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 271 | if (!ref.name) { |
| 272 | *out_msg << ref.id.value(); |
| 273 | return; |
| 274 | } |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 275 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 276 | *out_msg << ref.name.value(); |
| 277 | |
| 278 | Reference fully_qualified = ref; |
| 279 | xml::ResolvePackage(decls, &fully_qualified); |
| 280 | |
| 281 | ResourceName& full_name = fully_qualified.name.value(); |
| 282 | if (full_name.package.empty()) { |
| 283 | full_name.package = callsite.package; |
| 284 | } |
| 285 | |
| 286 | if (full_name != ref.name.value()) { |
| 287 | *out_msg << " (aka " << full_name << ")"; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void ReferenceLinker::WriteAttributeName(const Reference& ref, const CallSite& callsite, |
| 292 | const xml::IPackageDeclStack* decls, |
| 293 | DiagMessage* out_msg) { |
| 294 | CHECK(out_msg != nullptr); |
| 295 | if (!ref.name) { |
| 296 | *out_msg << ref.id.value(); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | const ResourceName& ref_name = ref.name.value(); |
| 301 | CHECK_EQ(ref_name.type, ResourceType::kAttr); |
| 302 | |
| 303 | if (!ref_name.package.empty()) { |
| 304 | *out_msg << ref_name.package << ":"; |
| 305 | } |
| 306 | *out_msg << ref_name.entry; |
| 307 | |
| 308 | Reference fully_qualified = ref; |
| 309 | xml::ResolvePackage(decls, &fully_qualified); |
| 310 | |
| 311 | ResourceName& full_name = fully_qualified.name.value(); |
| 312 | if (full_name.package.empty()) { |
| 313 | full_name.package = callsite.package; |
| 314 | } |
| 315 | |
| 316 | if (full_name != ref.name.value()) { |
| 317 | *out_msg << " (aka " << full_name.package << ":" << full_name.entry << ")"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | } |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 321 | bool ReferenceLinker::LinkReference(const CallSite& callsite, Reference* reference, |
| 322 | IAaptContext* context, SymbolTable* symbols, |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 323 | const xml::IPackageDeclStack* decls) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | CHECK(reference != nullptr); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 325 | if (!reference->name && !reference->id) { |
| 326 | // This is @null. |
| 327 | return true; |
| 328 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | Reference transformed_reference = *reference; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 331 | xml::ResolvePackage(decls, &transformed_reference); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 332 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | std::string err_str; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 334 | const SymbolTable::Symbol* s = |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 335 | ResolveSymbolCheckVisibility(transformed_reference, callsite, symbols, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | if (s) { |
| 337 | // The ID may not exist. This is fine because of the possibility of building |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | // against libraries without assigned IDs. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 339 | // Ex: Linking against own resources when building a static library. |
| 340 | reference->id = s->id; |
| 341 | return true; |
| 342 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 343 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | DiagMessage error_msg(reference->GetSource()); |
| 345 | error_msg << "resource "; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 346 | WriteResourceName(*reference, callsite, decls, &error_msg); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | error_msg << " " << err_str; |
| 348 | context->GetDiagnostics()->Error(error_msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 349 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) { |
| 353 | EmptyDeclStack decl_stack; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 354 | bool error = false; |
| 355 | for (auto& package : table->packages) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 356 | // Since we're linking, each package must have a name. |
| 357 | CHECK(!package->name.empty()) << "all packages being linked must have a name"; |
| 358 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 359 | for (auto& type : package->types) { |
| 360 | for (auto& entry : type->entries) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 361 | // First, unmangle the name if necessary. |
| 362 | ResourceName name(package->name, type->type, entry->name); |
| 363 | NameMangler::Unmangle(&name.entry, &name.package); |
| 364 | |
| 365 | // Symbol state information may be lost if there is no value for the resource. |
| 366 | if (entry->symbol_status.state != SymbolState::kUndefined && entry->values.empty()) { |
| 367 | context->GetDiagnostics()->Error(DiagMessage(entry->symbol_status.source) |
| 368 | << "no definition for declared symbol '" << name << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | error = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 370 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 371 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 372 | // The context of this resource is the package in which it is defined. |
| 373 | const CallSite callsite{name.package}; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 374 | ReferenceLinkerVisitor visitor(callsite, context, context->GetExternalSymbols(), |
| 375 | &table->string_pool, &decl_stack); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 377 | for (auto& config_value : entry->values) { |
| 378 | config_value->value->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | if (visitor.HasError()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | error = true; |
| 383 | } |
| 384 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 385 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 386 | } |
| 387 | return !error; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | } // namespace aapt |