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