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 | |
| 32 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | * Resolves all references to resources in the ResourceTable and assigns them |
| 34 | * IDs. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | * The ResourceTable must already have IDs assigned to each resource. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | * Once the ResourceTable is processed by this linker, it is ready to be |
| 37 | * flattened. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 38 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | class ReferenceLinker : public IResourceTableConsumer { |
| 40 | public: |
| 41 | ReferenceLinker() = default; |
| 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Returns true if the symbol is visible by the reference and from the |
| 45 | * callsite. |
| 46 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | static bool IsSymbolVisible(const SymbolTable::Symbol& symbol, |
| 48 | const Reference& ref, const CallSite& callsite); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Performs name mangling and looks up the resource in the symbol table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | * Returns nullptr if the symbol was not found. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | */ |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 54 | static const SymbolTable::Symbol* ResolveSymbol(const Reference& reference, SymbolTable* symbols); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Performs name mangling and looks up the resource in the symbol table. If |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | * the symbol is not visible by the reference at the callsite, nullptr is |
| 59 | * returned. out_error holds the error message. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | */ |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 61 | static const SymbolTable::Symbol* ResolveSymbolCheckVisibility(const Reference& reference, |
| 62 | SymbolTable* symbols, |
| 63 | CallSite* callsite, |
| 64 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Same as resolveSymbolCheckVisibility(), but also makes sure the symbol is |
| 68 | * an attribute. |
| 69 | * That is, the return value will have a non-null value for |
| 70 | * ISymbolTable::Symbol::attribute. |
| 71 | */ |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 72 | static const SymbolTable::Symbol* ResolveAttributeCheckVisibility(const Reference& reference, |
| 73 | SymbolTable* symbols, |
| 74 | CallSite* callsite, |
| 75 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Resolves the attribute reference and returns an xml::AaptAttribute if |
| 79 | * successful. |
| 80 | * If resolution fails, outError holds the error message. |
| 81 | */ |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 82 | static Maybe<xml::AaptAttribute> CompileXmlAttribute(const Reference& reference, |
| 83 | SymbolTable* symbols, CallSite* callsite, |
| 84 | std::string* out_error); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 85 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | /** |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | * Writes the resource name to the DiagMessage, using the |
| 88 | * "orig_name (aka <transformed_name>)" syntax. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | static void WriteResourceName(DiagMessage* out_msg, const Reference& orig, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | const Reference& transformed); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Transforms the package name of the reference to the fully qualified package |
| 95 | * name using |
| 96 | * the xml::IPackageDeclStack, then mangles and looks up the symbol. If the |
| 97 | * symbol is visible |
| 98 | * to the reference at the callsite, the reference is updated with an ID. |
| 99 | * Returns false on failure, and an error message is logged to the |
| 100 | * IDiagnostics in the context. |
| 101 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | static bool LinkReference(Reference* reference, IAaptContext* context, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | SymbolTable* symbols, xml::IPackageDeclStack* decls, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | CallSite* callsite); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 105 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Links all references in the ResourceTable. |
| 108 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 110 | |
| 111 | private: |
| 112 | DISALLOW_COPY_AND_ASSIGN(ReferenceLinker); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } // namespace aapt |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 116 | |
| 117 | #endif /* AAPT_LINKER_REFERENCELINKER_H */ |