blob: e684bb06f1f5bcb2932e79248271fcd24ea42910 [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
20#include "Resource.h"
21#include "ResourceTable.h"
22#include "ResourceValues.h"
23#include "util/Util.h"
24
25#include <utils/JenkinsHash.h>
26#include <utils/LruCache.h>
27
Adam Lesinski64587af2016-02-18 18:33:06 -080028#include <android-base/macros.h>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include <androidfw/AssetManager.h>
30#include <algorithm>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include <memory>
32#include <vector>
33
34namespace aapt {
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036inline android::hash_t hash_type(const ResourceName& name) {
37 std::hash<std::u16string> strHash;
38 android::hash_t hash = 0;
Adam Lesinski64587af2016-02-18 18:33:06 -080039 hash = android::JenkinsHashMix(hash, (uint32_t) strHash(name.package));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040 hash = android::JenkinsHashMix(hash, (uint32_t) name.type);
Adam Lesinski64587af2016-02-18 18:33:06 -080041 hash = android::JenkinsHashMix(hash, (uint32_t) strHash(name.entry));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042 return hash;
43}
44
45inline android::hash_t hash_type(const ResourceId& id) {
46 return android::hash_type(id.id);
47}
48
Adam Lesinski64587af2016-02-18 18:33:06 -080049class ISymbolSource;
50
51class SymbolTable {
52public:
53 struct Symbol {
Adam Lesinski626b3db2016-04-07 13:24:59 -070054 Symbol() : Symbol(Maybe<ResourceId>{}) {
55 }
56
57 Symbol(const Maybe<ResourceId>& i) : Symbol(i, nullptr) {
58 }
59
60 Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr) :
61 Symbol(i, attr, false) {
62 }
63
64 Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr, bool pub) :
65 id(i), attribute(attr), isPublic(pub) {
66 }
67
68 Symbol(const Symbol&) = default;
69 Symbol(Symbol&&) = default;
70 Symbol& operator=(const Symbol&) = default;
71 Symbol& operator=(Symbol&&) = default;
72
Adam Lesinski64587af2016-02-18 18:33:06 -080073 Maybe<ResourceId> id;
Adam Lesinski76565542016-03-10 21:55:04 -080074 std::shared_ptr<Attribute> attribute;
Adam Lesinski626b3db2016-04-07 13:24:59 -070075 bool isPublic = false;
Adam Lesinski64587af2016-02-18 18:33:06 -080076 };
77
78 SymbolTable() : mCache(200), mIdCache(200) {
79 }
80
81 void appendSource(std::unique_ptr<ISymbolSource> source);
82 void prependSource(std::unique_ptr<ISymbolSource> source);
83
84 /**
85 * Never hold on to the result between calls to findByName or findById. The results
86 * are typically stored in a cache which may evict entries.
87 */
88 const Symbol* findByName(const ResourceName& name);
89 const Symbol* findById(ResourceId id);
90
Adam Lesinski76565542016-03-10 21:55:04 -080091 /**
92 * Let's the ISymbolSource decide whether looking up by name or ID is faster, if both
93 * are available.
94 */
95 const Symbol* findByReference(const Reference& ref);
96
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097private:
Adam Lesinski64587af2016-02-18 18:33:06 -080098 std::vector<std::unique_ptr<ISymbolSource>> mSources;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
100 // We use shared_ptr because unique_ptr is not supported and
101 // we need automatic deletion.
102 android::LruCache<ResourceName, std::shared_ptr<Symbol>> mCache;
Adam Lesinski64587af2016-02-18 18:33:06 -0800103 android::LruCache<ResourceId, std::shared_ptr<Symbol>> mIdCache;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104
Adam Lesinski64587af2016-02-18 18:33:06 -0800105 DISALLOW_COPY_AND_ASSIGN(SymbolTable);
106};
107
108/**
109 * An interface that a symbol source implements in order to surface symbol information
110 * to the symbol table.
111 */
112class ISymbolSource {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113public:
Adam Lesinski64587af2016-02-18 18:33:06 -0800114 virtual ~ISymbolSource() = default;
115
116 virtual std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) = 0;
117 virtual std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) = 0;
Adam Lesinski76565542016-03-10 21:55:04 -0800118
119 /**
120 * Default implementation tries the name if it exists, else the ID.
121 */
122 virtual std::unique_ptr<SymbolTable::Symbol> findByReference(const Reference& ref) {
123 if (ref.name) {
124 return findByName(ref.name.value());
125 } else if (ref.id) {
126 return findById(ref.id.value());
127 }
128 return {};
129 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800130};
131
132/**
133 * Exposes the resources in a ResourceTable as symbols for SymbolTable.
134 * Instances of this class must outlive the encompassed ResourceTable.
135 * Lookups by ID are ignored.
136 */
137class ResourceTableSymbolSource : public ISymbolSource {
138public:
139 explicit ResourceTableSymbolSource(ResourceTable* table) : mTable(table) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140 }
141
Adam Lesinski64587af2016-02-18 18:33:06 -0800142 std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinski64587af2016-02-18 18:33:06 -0800144 std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 return {};
146 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800147
148private:
149 ResourceTable* mTable;
150
151 DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152};
153
Adam Lesinski64587af2016-02-18 18:33:06 -0800154class AssetManagerSymbolSource : public ISymbolSource {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155public:
Adam Lesinski64587af2016-02-18 18:33:06 -0800156 AssetManagerSymbolSource() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157
Adam Lesinski64587af2016-02-18 18:33:06 -0800158 bool addAssetPath(const StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinski64587af2016-02-18 18:33:06 -0800160 std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override;
161 std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override;
Adam Lesinski76565542016-03-10 21:55:04 -0800162 std::unique_ptr<SymbolTable::Symbol> findByReference(const Reference& ref) override;
Adam Lesinski64587af2016-02-18 18:33:06 -0800163
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164private:
Adam Lesinski64587af2016-02-18 18:33:06 -0800165 android::AssetManager mAssets;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinski64587af2016-02-18 18:33:06 -0800167 DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168};
169
170} // namespace aapt
171
172#endif /* AAPT_PROCESS_SYMBOLTABLE_H */