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 | |
| 17 | #ifndef AAPT_PROCESS_SYMBOLTABLE_H |
| 18 | #define AAPT_PROCESS_SYMBOLTABLE_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 20 | #include <algorithm> |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/macros.h" |
| 25 | #include "androidfw/AssetManager.h" |
| 26 | #include "utils/JenkinsHash.h" |
| 27 | #include "utils/LruCache.h" |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "Resource.h" |
| 30 | #include "ResourceTable.h" |
| 31 | #include "ResourceValues.h" |
| 32 | #include "util/Util.h" |
| 33 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | inline android::hash_t hash_type(const ResourceName& name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 37 | std::hash<std::string> str_hash; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | android::hash_t hash = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 39 | hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | hash = android::JenkinsHashMix(hash, (uint32_t)name.type); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 41 | hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | return hash; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | inline android::hash_t hash_type(const ResourceId& id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | return android::hash_type(id.id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 49 | class ISymbolSource; |
| 50 | |
| 51 | class SymbolTable { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | public: |
| 53 | struct Symbol { |
| 54 | Symbol() : Symbol(Maybe<ResourceId>{}) {} |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | explicit Symbol(const Maybe<ResourceId>& i) : Symbol(i, nullptr) {} |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr) |
| 59 | : Symbol(i, attr, false) {} |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr, |
| 62 | bool pub) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 63 | : id(i), attribute(attr), is_public(pub) {} |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | Symbol(const Symbol&) = default; |
| 66 | Symbol(Symbol&&) = default; |
| 67 | Symbol& operator=(const Symbol&) = default; |
| 68 | Symbol& operator=(Symbol&&) = default; |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | Maybe<ResourceId> id; |
| 71 | std::shared_ptr<Attribute> attribute; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 72 | bool is_public = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | }; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 75 | SymbolTable() : cache_(200), id_cache_(200) {} |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 77 | void AppendSource(std::unique_ptr<ISymbolSource> source); |
| 78 | void PrependSource(std::unique_ptr<ISymbolSource> source); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | /** |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 81 | * Never hold on to the result between calls to FindByName or FindById. The |
| 82 | * results stored in a cache which may evict entries. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 84 | const Symbol* FindByName(const ResourceName& name); |
| 85 | const Symbol* FindById(const ResourceId& id); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Let's the ISymbolSource decide whether looking up by name or ID is faster, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 89 | * if both are available. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 91 | const Symbol* FindByReference(const Reference& ref); |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 94 | std::vector<std::unique_ptr<ISymbolSource>> sources_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | // We use shared_ptr because unique_ptr is not supported and |
| 97 | // we need automatic deletion. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 98 | android::LruCache<ResourceName, std::shared_ptr<Symbol>> cache_; |
| 99 | android::LruCache<ResourceId, std::shared_ptr<Symbol>> id_cache_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(SymbolTable); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | * An interface that a symbol source implements in order to surface symbol |
| 106 | * information |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 107 | * to the symbol table. |
| 108 | */ |
| 109 | class ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | public: |
| 111 | virtual ~ISymbolSource() = default; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 113 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | const ResourceName& name) = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 115 | virtual std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) = 0; |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Default implementation tries the name if it exists, else the ID. |
| 119 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 120 | virtual std::unique_ptr<SymbolTable::Symbol> FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | const Reference& ref) { |
| 122 | if (ref.name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 123 | return FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | } else if (ref.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 125 | return FindById(ref.id.value()); |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 126 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | return {}; |
| 128 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * Exposes the resources in a ResourceTable as symbols for SymbolTable. |
| 133 | * Instances of this class must outlive the encompassed ResourceTable. |
| 134 | * Lookups by ID are ignored. |
| 135 | */ |
| 136 | class ResourceTableSymbolSource : public ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 138 | explicit ResourceTableSymbolSource(ResourceTable* table) : table_(table) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 140 | std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | const ResourceName& name) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 143 | std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | return {}; |
| 145 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 146 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 148 | ResourceTable* table_; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 149 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 153 | class AssetManagerSymbolSource : public ISymbolSource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | public: |
| 155 | AssetManagerSymbolSource() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 157 | bool AddAssetPath(const StringPiece& path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 159 | std::unique_ptr<SymbolTable::Symbol> FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 160 | const ResourceName& name) override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 161 | std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override; |
| 162 | std::unique_ptr<SymbolTable::Symbol> FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | const Reference& ref) override; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 166 | android::AssetManager assets_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | |
| 173 | #endif /* AAPT_PROCESS_SYMBOLTABLE_H */ |