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 | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 54 | ReferenceLinkerVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols, |
| 55 | StringPool* string_pool, xml::IPackageDeclStack* decl) |
| 56 | : callsite_(callsite), |
| 57 | context_(context), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | symbols_(symbols), |
| 59 | package_decls_(decl), |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 60 | string_pool_(string_pool) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | void Visit(Reference* ref) override { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 63 | if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, package_decls_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 97 | const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveAttributeCheckVisibility( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 98 | transformed_reference, callsite_, symbols_, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | if (symbol) { |
| 100 | // Assign our style key the correct ID. |
| 101 | // The ID may not exist. |
| 102 | entry.key.id = symbol->id; |
| 103 | |
| 104 | // Try to convert the value to a more specific, typed value based on the |
| 105 | // attribute it is set to. |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 106 | entry.value = ParseValueWithAttribute(std::move(entry.value), symbol->attribute.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | |
| 108 | // Link/resolve the final value (mostly if it's a reference). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | entry.value->Accept(this); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | |
| 111 | // Now verify that the type of this item is compatible with the |
| 112 | // attribute it |
| 113 | // is defined for. We pass `nullptr` as the DiagMessage so that this |
| 114 | // check is |
| 115 | // fast and we avoid creating a DiagMessage when the match is |
| 116 | // successful. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | if (!symbol->attribute->Matches(entry.value.get(), nullptr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | // The actual type of this item is incompatible with the attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | DiagMessage msg(entry.key.GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | |
| 121 | // Call the matches method again, this time with a DiagMessage so we |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 122 | // fill in the actual error message. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | symbol->attribute->Matches(entry.value.get(), &msg); |
| 124 | context_->GetDiagnostics()->Error(msg); |
| 125 | error_ = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | DiagMessage msg(entry.key.GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | msg << "style attribute '"; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 131 | ReferenceLinker::WriteResourceName(&msg, entry.key, transformed_reference); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | msg << "' " << err_str; |
| 133 | context_->GetDiagnostics()->Error(msg); |
| 134 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | bool HasError() { return error_; } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(ReferenceLinkerVisitor); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Transform a RawString value into a more specific, appropriate value, based |
| 146 | * on the |
| 147 | * Attribute. If a non RawString value is passed in, this is an identity |
| 148 | * transform. |
| 149 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | std::unique_ptr<Item> ParseValueWithAttribute(std::unique_ptr<Item> value, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | const Attribute* attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | if (RawString* raw_string = ValueCast<RawString>(value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | std::unique_ptr<Item> transformed = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | ResourceUtils::TryParseItemForAttribute(*raw_string->value, attr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | // 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] | 157 | if (!transformed && (attr->type_mask & android::ResTable_map::TYPE_STRING)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | util::StringBuilder string_builder; |
| 159 | string_builder.Append(*raw_string->value); |
| 160 | if (string_builder) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 161 | transformed = util::make_unique<String>(string_pool_->MakeRef(string_builder.ToString())); |
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 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 164 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | if (transformed) { |
| 166 | return transformed; |
| 167 | } |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 168 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | return value; |
| 170 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 172 | const CallSite& callsite_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | IAaptContext* context_; |
| 174 | SymbolTable* symbols_; |
| 175 | xml::IPackageDeclStack* package_decls_; |
| 176 | StringPool* string_pool_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | bool error_ = false; |
| 178 | }; |
| 179 | |
| 180 | class EmptyDeclStack : public xml::IPackageDeclStack { |
| 181 | public: |
| 182 | EmptyDeclStack() = default; |
| 183 | |
| 184 | Maybe<xml::ExtractedPackage> TransformPackageAlias( |
| 185 | const StringPiece& alias, |
| 186 | const StringPiece& local_package) const override { |
| 187 | if (alias.empty()) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 188 | return xml::ExtractedPackage{local_package.to_string(), true /* private */}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | } |
| 190 | return {}; |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | DISALLOW_COPY_AND_ASSIGN(EmptyDeclStack); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | } // namespace |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | * The symbol is visible if it is public, or if the reference to it is |
| 201 | * requesting private access |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 202 | * or if the callsite comes from the same package. |
| 203 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | bool ReferenceLinker::IsSymbolVisible(const SymbolTable::Symbol& symbol, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | const Reference& ref, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | const CallSite& callsite) { |
| 207 | if (!symbol.is_public && !ref.private_reference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | if (ref.name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | return callsite.resource.package == ref.name.value().package; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | } else if (ref.id && symbol.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | return ref.id.value().package_id() == symbol.id.value().package_id(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | } else { |
| 213 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 214 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | } |
| 216 | return true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 219 | const SymbolTable::Symbol* ReferenceLinker::ResolveSymbol(const Reference& reference, |
| 220 | SymbolTable* symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 221 | if (reference.name) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 222 | return symbols->FindByName(reference.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | } else if (reference.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | return symbols->FindById(reference.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | } else { |
| 226 | return nullptr; |
| 227 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 230 | const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 231 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 232 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 233 | std::string* out_error) { |
| 234 | const SymbolTable::Symbol* symbol = ResolveSymbol(reference, symbols); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | if (!symbol) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | if (out_error) *out_error = "not found"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | return nullptr; |
| 238 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 239 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 240 | if (!IsSymbolVisible(*symbol, reference, callsite)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | if (out_error) *out_error = "is private"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | return nullptr; |
| 243 | } |
| 244 | return symbol; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | const SymbolTable::Symbol* ReferenceLinker::ResolveAttributeCheckVisibility( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 248 | const Reference& reference, const CallSite& callsite, SymbolTable* symbols, |
| 249 | std::string* out_error) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 250 | const SymbolTable::Symbol* symbol = |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 251 | ResolveSymbolCheckVisibility(reference, callsite, symbols, out_error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | if (!symbol) { |
| 253 | return nullptr; |
| 254 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 255 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | if (!symbol->attribute) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | if (out_error) *out_error = "is not an attribute"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | return nullptr; |
| 259 | } |
| 260 | return symbol; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 263 | Maybe<xml::AaptAttribute> ReferenceLinker::CompileXmlAttribute(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 264 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 265 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 266 | std::string* out_error) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 267 | const SymbolTable::Symbol* symbol = |
| 268 | ResolveAttributeCheckVisibility(reference, callsite, symbols, out_error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | if (!symbol) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | return {}; |
| 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 {}; |
| 276 | } |
| 277 | return xml::AaptAttribute{symbol->id, *symbol->attribute}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | void ReferenceLinker::WriteResourceName(DiagMessage* out_msg, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | const Reference& orig, |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 282 | const Reference& transformed) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | CHECK(out_msg != nullptr); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 284 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | if (orig.name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | *out_msg << orig.name.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | if (transformed.name.value() != orig.name.value()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | *out_msg << " (aka " << transformed.name.value() << ")"; |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 289 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | *out_msg << orig.id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | } |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 295 | bool ReferenceLinker::LinkReference(const CallSite& callsite, Reference* reference, |
| 296 | IAaptContext* context, SymbolTable* symbols, |
| 297 | xml::IPackageDeclStack* decls) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | CHECK(reference != nullptr); |
| 299 | CHECK(reference->name || reference->id); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 301 | Reference transformed_reference = *reference; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 302 | TransformReferenceFromNamespace(decls, context->GetCompilationPackage(), &transformed_reference); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 303 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 304 | std::string err_str; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 305 | const SymbolTable::Symbol* s = |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 306 | ResolveSymbolCheckVisibility(transformed_reference, callsite, symbols, &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | if (s) { |
| 308 | // 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] | 309 | // against libraries without assigned IDs. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | // Ex: Linking against own resources when building a static library. |
| 311 | reference->id = s->id; |
| 312 | return true; |
| 313 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 314 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | DiagMessage error_msg(reference->GetSource()); |
| 316 | error_msg << "resource "; |
| 317 | WriteResourceName(&error_msg, *reference, transformed_reference); |
| 318 | error_msg << " " << err_str; |
| 319 | context->GetDiagnostics()->Error(error_msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 323 | bool ReferenceLinker::Consume(IAaptContext* context, ResourceTable* table) { |
| 324 | EmptyDeclStack decl_stack; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 325 | bool error = false; |
| 326 | for (auto& package : table->packages) { |
| 327 | for (auto& type : package->types) { |
| 328 | for (auto& entry : type->entries) { |
| 329 | // Symbol state information may be lost if there is no value for the |
| 330 | // resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | if (entry->symbol_status.state != SymbolState::kUndefined && |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 332 | entry->values.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | context->GetDiagnostics()->Error( |
| 334 | DiagMessage(entry->symbol_status.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | << "no definition for declared symbol '" |
| 336 | << ResourceNameRef(package->name, type->type, entry->name) |
| 337 | << "'"); |
| 338 | error = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 339 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 341 | CallSite callsite = {ResourceNameRef(package->name, type->type, entry->name)}; |
| 342 | ReferenceLinkerVisitor visitor(callsite, context, context->GetExternalSymbols(), |
| 343 | &table->string_pool, &decl_stack); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 344 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | for (auto& config_value : entry->values) { |
| 346 | config_value->value->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | if (visitor.HasError()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | error = true; |
| 351 | } |
| 352 | } |
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 | } |
| 355 | return !error; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | } // namespace aapt |