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" |
| 24 | #include "ValueVisitor.h" |
| 25 | #include "link/Linkers.h" |
| 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "process/SymbolTable.h" |
| 28 | #include "xml/XmlDom.h" |
| 29 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 32 | // Resolves all references to resources in the ResourceTable and assigns them IDs. |
| 33 | // The ResourceTable must already have IDs assigned to each resource. |
| 34 | // 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] | 35 | class ReferenceLinker : public IResourceTableConsumer { |
| 36 | public: |
| 37 | ReferenceLinker() = default; |
| 38 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 39 | // Performs name mangling and looks up the resource in the symbol table. Uses the callsite's |
| 40 | // package if the reference has no package name defined (implicit). |
| 41 | // Returns nullptr if the symbol was not found. |
| 42 | static const SymbolTable::Symbol* ResolveSymbol(const Reference& reference, |
| 43 | const CallSite& callsite, SymbolTable* symbols); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 45 | // Performs name mangling and looks up the resource in the symbol table. If the symbol is not |
| 46 | // visible by the reference at the callsite, nullptr is returned. |
| 47 | // `out_error` holds the error message. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 48 | static const SymbolTable::Symbol* ResolveSymbolCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 49 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 50 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 51 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 53 | // Same as ResolveSymbolCheckVisibility(), but also makes sure the symbol is an attribute. |
| 54 | // 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] | 55 | static const SymbolTable::Symbol* ResolveAttributeCheckVisibility(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 56 | const CallSite& callsite, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 57 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 58 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 60 | // Resolves the attribute reference and returns an xml::AaptAttribute if successful. |
| 61 | // If resolution fails, outError holds the error message. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 62 | static Maybe<xml::AaptAttribute> CompileXmlAttribute(const Reference& reference, |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 63 | const CallSite& callsite, |
| 64 | SymbolTable* symbols, |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 65 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 67 | // Writes the resource name to the DiagMessage, using the |
| 68 | // "orig_name (aka <transformed_name>)" syntax. |
| 69 | static void WriteResourceName(const Reference& orig, const CallSite& callsite, |
| 70 | const xml::IPackageDeclStack* decls, DiagMessage* out_msg); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 71 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 72 | // Same as WriteResourceName but omits the 'attr' part. |
| 73 | static void WriteAttributeName(const Reference& ref, const CallSite& callsite, |
| 74 | const xml::IPackageDeclStack* decls, DiagMessage* out_msg); |
| 75 | |
| 76 | // Transforms the package name of the reference to the fully qualified package name using |
| 77 | // the xml::IPackageDeclStack, then mangles and looks up the symbol. If the symbol is visible |
| 78 | // to the reference at the callsite, the reference is updated with an ID. |
| 79 | // Returns false on failure, and an error message is logged to the IDiagnostics in the context. |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 80 | static bool LinkReference(const CallSite& callsite, Reference* reference, IAaptContext* context, |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 81 | SymbolTable* symbols, const xml::IPackageDeclStack* decls); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 83 | // Links all references in the ResourceTable. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 85 | |
| 86 | private: |
| 87 | DISALLOW_COPY_AND_ASSIGN(ReferenceLinker); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | } // namespace aapt |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 91 | |
| 92 | #endif /* AAPT_LINKER_REFERENCELINKER_H */ |