Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [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 | |
| 17 | #ifndef AAPT_LINKER_REFERENCELINKER_H |
| 18 | #define AAPT_LINKER_REFERENCELINKER_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 22 | #include "Resource.h" |
| 23 | #include "ResourceValues.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 24 | #include "link/Linkers.h" |
| 25 | #include "process/IResourceTableConsumer.h" |
| 26 | #include "process/SymbolTable.h" |
| 27 | #include "xml/XmlDom.h" |
| 28 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 31 | // A ValueTransformer that returns fully linked versions of resource and macro references. |
| 32 | class ReferenceLinkerTransformer : public CloningValueTransformer { |
| 33 | public: |
| 34 | ReferenceLinkerTransformer(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols, |
| 35 | StringPool* string_pool, ResourceTable* table, |
| 36 | xml::IPackageDeclStack* decl) |
| 37 | : CloningValueTransformer(string_pool), |
| 38 | callsite_(callsite), |
| 39 | context_(context), |
| 40 | symbols_(symbols), |
| 41 | table_(table), |
| 42 | package_decls_(decl) { |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<Reference> TransformDerived(const Reference* value) override; |
| 46 | std::unique_ptr<Item> TransformItem(const Reference* value) override; |
| 47 | std::unique_ptr<Style> TransformDerived(const Style* value) override; |
| 48 | |
| 49 | bool HasError() { |
| 50 | return error_; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | // Transform a RawString value into a more specific, appropriate value, based on the |
| 55 | // Attribute. If a non RawString value is passed in, this is an identity transform. |
| 56 | std::unique_ptr<Item> ParseValueWithAttribute(std::unique_ptr<Item> value, const Attribute* attr); |
| 57 | |
| 58 | const CallSite& callsite_; |
| 59 | IAaptContext* context_; |
| 60 | SymbolTable* symbols_; |
| 61 | ResourceTable* table_; |
| 62 | xml::IPackageDeclStack* package_decls_; |
| 63 | bool error_ = false; |
| 64 | }; |
| 65 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 66 | // Resolves all references to resources in the ResourceTable and assigns them IDs. |
| 67 | // The ResourceTable must already have IDs assigned to each resource. |
| 68 | // Once the ResourceTable is processed by this linker, it is ready to be flattened. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | class ReferenceLinker : public IResourceTableConsumer { |
| 70 | public: |
| 71 | ReferenceLinker() = default; |
| 72 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 73 | // Performs name mangling and looks up the resource in the symbol table. Uses the callsite's |
Chris Warrington | 58e2fbf | 2018-07-23 14:12:20 +0000 | [diff] [blame] | 74 | // package if the reference has no package name defined (implicit). |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 75 | // Returns nullptr if the symbol was not found. |
| 76 | static const SymbolTable::Symbol* ResolveSymbol(const Reference& reference, |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 77 | const CallSite& callsite, |
| 78 | IAaptContext* context, |
| 79 | SymbolTable* symbols); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 81 | // Performs name mangling and looks up the resource in the symbol table. If the symbol is not |
| 82 | // visible by the reference at the callsite, nullptr is returned. |
| 83 | // `out_error` holds the error message. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 84 | static const SymbolTable::Symbol* ResolveSymbolCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 85 | const CallSite& callsite, |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 86 | IAaptContext* context, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 87 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 88 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 90 | // Same as ResolveSymbolCheckVisibility(), but also makes sure the symbol is an attribute. |
| 91 | // That is, the return value will have a non-null value for ISymbolTable::Symbol::attribute. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 92 | static const SymbolTable::Symbol* ResolveAttributeCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 93 | const CallSite& callsite, |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 94 | IAaptContext* context, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 95 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 96 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 97 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 98 | // Resolves the attribute reference and returns an xml::AaptAttribute if successful. |
| 99 | // If resolution fails, outError holds the error message. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 100 | static Maybe<xml::AaptAttribute> CompileXmlAttribute(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 101 | const CallSite& callsite, |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 102 | IAaptContext* context, |
Chris Warrington | 58e2fbf | 2018-07-23 14:12:20 +0000 | [diff] [blame] | 103 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 104 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 105 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 106 | // Writes the resource name to the DiagMessage, using the |
| 107 | // "orig_name (aka <transformed_name>)" syntax. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 108 | /*static void WriteResourceName(const Reference& orig, const CallSite& callsite, |
| 109 | const xml::IPackageDeclStack* decls, DiagMessage* out_msg);*/ |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 110 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 111 | // Same as WriteResourceName but omits the 'attr' part. |
| 112 | static void WriteAttributeName(const Reference& ref, const CallSite& callsite, |
| 113 | const xml::IPackageDeclStack* decls, DiagMessage* out_msg); |
| 114 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 115 | // Returns a fully linked version a resource reference. |
| 116 | // |
| 117 | // If the reference points to a non-macro resource, the xml::IPackageDeclStack is used to |
| 118 | // determine the fully qualified name of the referenced resource. If the symbol is visible |
| 119 | // to the reference at the callsite, a copy of the reference with an updated updated ID is |
| 120 | // returned. |
| 121 | // |
| 122 | // If the reference points to a macro, the ResourceTable is used to find the macro definition and |
| 123 | // substitute its contents in place of the reference. |
| 124 | // |
| 125 | // Returns nullptr on failure, and an error message is logged to the IDiagnostics in the context. |
| 126 | static std::unique_ptr<Item> LinkReference(const CallSite& callsite, const Reference& reference, |
| 127 | IAaptContext* context, SymbolTable* symbols, |
| 128 | ResourceTable* table, |
| 129 | const xml::IPackageDeclStack* decls); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 130 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 131 | // Links all references in the ResourceTable. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 133 | |
| 134 | private: |
| 135 | DISALLOW_COPY_AND_ASSIGN(ReferenceLinker); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | } // namespace aapt |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 139 | |
| 140 | #endif /* AAPT_LINKER_REFERENCELINKER_H */ |