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