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