blob: c80e62729e261997055d894d99ef0d68ff8186c0 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#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 Lesinski1ab598f2015-08-14 14:26:04 -070029#include "Resource.h"
30#include "ResourceTable.h"
31#include "ResourceValues.h"
32#include "util/Util.h"
33
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034namespace aapt {
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036inline android::hash_t hash_type(const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 std::hash<std::string> str_hash;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 android::hash_t hash = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 hash = android::JenkinsHashMix(hash, (uint32_t)name.type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 return hash;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043}
44
45inline android::hash_t hash_type(const ResourceId& id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return android::hash_type(id.id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047}
48
Adam Lesinski64587af2016-02-18 18:33:06 -080049class ISymbolSource;
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070050class ISymbolTableDelegate;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080051class NameMangler;
Adam Lesinski64587af2016-02-18 18:33:06 -080052
53class SymbolTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
55 struct Symbol {
Adam Lesinskic744ae82017-05-17 19:28:38 -070056 Symbol() = default;
Adam Lesinski626b3db2016-04-07 13:24:59 -070057
Adam Lesinskic744ae82017-05-17 19:28:38 -070058 explicit Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr = {},
59 bool pub = false)
60 : id(i), attribute(attr), is_public(pub) {
61 }
Adam Lesinski626b3db2016-04-07 13:24:59 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 Symbol(const Symbol&) = default;
64 Symbol(Symbol&&) = default;
65 Symbol& operator=(const Symbol&) = default;
66 Symbol& operator=(Symbol&&) = default;
Adam Lesinski626b3db2016-04-07 13:24:59 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 Maybe<ResourceId> id;
69 std::shared_ptr<Attribute> attribute;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 bool is_public = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 };
Adam Lesinski64587af2016-02-18 18:33:06 -080072
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070073 SymbolTable(NameMangler* mangler);
74
75 // Overrides the default ISymbolTableDelegate, which allows a custom defined strategy for
76 // looking up resources from a set of sources.
77 void SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate);
Adam Lesinski64587af2016-02-18 18:33:06 -080078
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080079 // Appends a symbol source. The cache is not cleared since entries that
80 // have already been found would take precedence due to ordering.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 void AppendSource(std::unique_ptr<ISymbolSource> source);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080082
83 // Prepends a symbol source so that its symbols take precedence. This will
84 // cause the existing cache to be cleared.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 void PrependSource(std::unique_ptr<ISymbolSource> source);
Adam Lesinski64587af2016-02-18 18:33:06 -080086
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080087 // NOTE: Never hold on to the result between calls to FindByXXX. The
88 // results are stored in a cache which may evict entries on subsequent calls.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 const Symbol* FindByName(const ResourceName& name);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080090
Chris Warrington481f0272018-02-06 14:03:39 +000091 // Finds the symbol from any package, for use as part of automatic conversion to namespaces.
92 // This returns the symbol from the highest priority package,
93 // which mimics the behavior of the resource merger and overlays.
94 // NOTE: Never hold on to the result between calls to FindByXXX. The
95 // results are stored in a cache which may evict entries on subsequent calls.
96 const Symbol* FindByNameInAnyPackage(const ResourceName& name);
97
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080098 // NOTE: Never hold on to the result between calls to FindByXXX. The
99 // results are stored in a cache which may evict entries on subsequent calls.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 const Symbol* FindById(const ResourceId& id);
Adam Lesinski64587af2016-02-18 18:33:06 -0800101
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800102 // Let's the ISymbolSource decide whether looking up by name or ID is faster,
103 // if both are available.
104 // NOTE: Never hold on to the result between calls to FindByXXX. The
105 // results are stored in a cache which may evict entries on subsequent calls.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 const Symbol* FindByReference(const Reference& ref);
Adam Lesinski76565542016-03-10 21:55:04 -0800107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 private:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800109 NameMangler* mangler_;
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700110 std::unique_ptr<ISymbolTableDelegate> delegate_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 std::vector<std::unique_ptr<ISymbolSource>> sources_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // We use shared_ptr because unique_ptr is not supported and
114 // we need automatic deletion.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 android::LruCache<ResourceName, std::shared_ptr<Symbol>> cache_;
116 android::LruCache<ResourceId, std::shared_ptr<Symbol>> id_cache_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 DISALLOW_COPY_AND_ASSIGN(SymbolTable);
Adam Lesinski64587af2016-02-18 18:33:06 -0800119};
120
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700121// Allows the customization of the lookup strategy/order of a symbol from a set of
122// symbol sources.
123class ISymbolTableDelegate {
124 public:
125 ISymbolTableDelegate() = default;
126 virtual ~ISymbolTableDelegate() = default;
127
128 // The name is already mangled and does not need further processing.
129 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
130 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) = 0;
131
132 virtual std::unique_ptr<SymbolTable::Symbol> FindById(
133 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) = 0;
134
135 private:
136 DISALLOW_COPY_AND_ASSIGN(ISymbolTableDelegate);
137};
138
139class DefaultSymbolTableDelegate : public ISymbolTableDelegate {
140 public:
141 DefaultSymbolTableDelegate() = default;
142 virtual ~DefaultSymbolTableDelegate() = default;
143
144 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
145 const ResourceName& name,
146 const std::vector<std::unique_ptr<ISymbolSource>>& sources) override;
147 virtual std::unique_ptr<SymbolTable::Symbol> FindById(
148 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) override;
149
150 private:
151 DISALLOW_COPY_AND_ASSIGN(DefaultSymbolTableDelegate);
152};
153
154// An interface that a symbol source implements in order to surface symbol information
155// to the symbol table.
Adam Lesinski64587af2016-02-18 18:33:06 -0800156class ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 public:
158 virtual ~ISymbolSource() = default;
Adam Lesinski64587af2016-02-18 18:33:06 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 const ResourceName& name) = 0;
Chris Warrington481f0272018-02-06 14:03:39 +0000162 // Finds the name of a symbol from any package,
163 // for use as part of automatic conversion to namespaces.
164 // This returns the symbol from the highest priority package,
165 // which mimics the behavior of the resource merger and overlays.
166 virtual std::string GetPackageForSymbol(const ResourceName& name) = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 virtual std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) = 0;
Adam Lesinski76565542016-03-10 21:55:04 -0800168
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700169 // Default implementation tries the name if it exists, else the ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 virtual std::unique_ptr<SymbolTable::Symbol> FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 const Reference& ref) {
172 if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 } else if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 return FindById(ref.id.value());
Adam Lesinski76565542016-03-10 21:55:04 -0800176 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 return {};
178 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800179};
180
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700181// Exposes the resources in a ResourceTable as symbols for SymbolTable.
182// Instances of this class must outlive the encompassed ResourceTable.
183// Lookups by ID are ignored.
Adam Lesinski64587af2016-02-18 18:33:06 -0800184class ResourceTableSymbolSource : public ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 explicit ResourceTableSymbolSource(ResourceTable* table) : table_(table) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 const ResourceName& name) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190
Chris Warrington481f0272018-02-06 14:03:39 +0000191 std::string GetPackageForSymbol(const ResourceName& name) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 return {};
194 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 ResourceTable* table_;
Adam Lesinski64587af2016-02-18 18:33:06 -0800198
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200};
201
Adam Lesinski64587af2016-02-18 18:33:06 -0800202class AssetManagerSymbolSource : public ISymbolSource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 public:
204 AssetManagerSymbolSource() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205
Adam Lesinskid5083f62017-01-16 15:07:21 -0800206 bool AddAssetPath(const android::StringPiece& path);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800207 std::map<size_t, std::string> GetAssignedPackageIds() const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 const ResourceName& name) override;
Chris Warrington481f0272018-02-06 14:03:39 +0000211 std::string GetPackageForSymbol(const ResourceName& name) override {
212 return {};
213 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override;
215 std::unique_ptr<SymbolTable::Symbol> FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 const Reference& ref) override;
Adam Lesinski64587af2016-02-18 18:33:06 -0800217
Adam Lesinskic6284372017-12-04 13:46:23 -0800218 android::AssetManager* GetAssetManager() {
219 return &assets_;
220 }
221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 android::AssetManager assets_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226};
227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229
230#endif /* AAPT_PROCESS_SYMBOLTABLE_H */