summaryrefslogtreecommitdiffstats
path: root/tools/aapt2/Linker.cpp
blob: c37cc932cd3b6b740da1dd64a96cba7ed57aae43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Linker.h"
#include "Logger.h"
#include "NameMangler.h"
#include "Resolver.h"
#include "ResourceParser.h"
#include "ResourceTable.h"
#include "ResourceValues.h"
#include "StringPiece.h"
#include "Util.h"

#include <androidfw/AssetManager.h>
#include <array>
#include <bitset>
#include <iostream>
#include <map>
#include <ostream>
#include <set>
#include <sstream>
#include <tuple>
#include <vector>

namespace aapt {

Linker::Args::Args(const ResourceNameRef& r, const SourceLine& s) : referrer(r), source(s) {
}

Linker::Linker(const std::shared_ptr<ResourceTable>& table,
               const std::shared_ptr<IResolver>& resolver, const Options& options) :
        mResolver(resolver), mTable(table), mOptions(options), mError(false) {
}

bool Linker::linkAndValidate() {
    std::bitset<256> usedTypeIds;
    std::array<std::set<uint16_t>, 256> usedIds;
    usedTypeIds.set(0);

    // Collect which resource IDs are already taken.
    for (auto& type : *mTable) {
        if (type->typeId != ResourceTableType::kUnsetTypeId) {
            // The ID for this type has already been set. We
            // mark this ID as taken so we don't re-assign it
            // later.
            usedTypeIds.set(type->typeId);
        }

        for (auto& entry : type->entries) {
            if (type->typeId != ResourceTableType::kUnsetTypeId &&
                    entry->entryId != ResourceEntry::kUnsetEntryId) {
                // The ID for this entry has already been set. We
                // mark this ID as taken so we don't re-assign it
                // later.
                usedIds[type->typeId].insert(entry->entryId);
            }
        }
    }

    // Assign resource IDs that are available.
    size_t nextTypeIndex = 0;
    for (auto& type : *mTable) {
        if (type->typeId == ResourceTableType::kUnsetTypeId) {
            while (nextTypeIndex < usedTypeIds.size() && usedTypeIds[nextTypeIndex]) {
                nextTypeIndex++;
            }
            type->typeId = nextTypeIndex++;
        }

        const auto endEntryIter = std::end(usedIds[type->typeId]);
        auto nextEntryIter = std::begin(usedIds[type->typeId]);
        size_t nextIndex = 0;
        for (auto& entry : type->entries) {
            if (entry->entryId == ResourceTableType::kUnsetTypeId) {
                while (nextEntryIter != endEntryIter &&
                        nextIndex == *nextEntryIter) {
                    nextIndex++;
                    ++nextEntryIter;
                }
                entry->entryId = nextIndex++;
            }
        }
    }

    // Now do reference linking.
    for (auto& type : *mTable) {
        for (auto& entry : type->entries) {
            if (entry->publicStatus.isPublic && entry->values.empty()) {
                // A public resource has no values. It will not be encoded
                // properly without a symbol table. This is a unresolved symbol.
                addUnresolvedSymbol(ResourceNameRef{
                        mTable->getPackage(), type->type, entry->name },
                        entry->publicStatus.source);
                continue;
            }

            for (auto& valueConfig : entry->values) {
                // Dispatch to the right method of this linker
                // based on the value's type.
                valueConfig.value->accept(*this, Args{
                        ResourceNameRef{ mTable->getPackage(), type->type, entry->name },
                        valueConfig.source
                });
            }
        }
    }
    return !mError;
}

const Linker::ResourceNameToSourceMap& Linker::getUnresolvedReferences() const {
    return mUnresolvedSymbols;
}

void Linker::doResolveReference(Reference& reference, const SourceLine& source) {
    Maybe<ResourceId> result = mResolver->findId(reference.name);
    if (!result) {
        addUnresolvedSymbol(reference.name, source);
        return;
    }
    assert(result.value().isValid());

    if (mOptions.linkResourceIds) {
        reference.id = result.value();
    } else {
        reference.id = 0;
    }
}

const Attribute* Linker::doResolveAttribute(Reference& attribute, const SourceLine& source) {
    Maybe<IResolver::Entry> result = mResolver->findAttribute(attribute.name);
    if (!result || !result.value().attr) {
        addUnresolvedSymbol(attribute.name, source);
        return nullptr;
    }

    const IResolver::Entry& entry = result.value();
    assert(entry.id.isValid());

    if (mOptions.linkResourceIds) {
        attribute.id = entry.id;
    } else {
        attribute.id = 0;
    }
    return entry.attr;
}

void Linker::visit(Reference& reference, ValueVisitorArgs& a) {
    Args& args = static_cast<Args&>(a);

    if (reference.name.entry.empty()) {
        // We can't have a completely bad reference.
        if (!reference.id.isValid()) {
            Logger::error() << "srsly? " << args.referrer << std::endl;
            assert(reference.id.isValid());
        }

        // This reference has no name but has an ID.
        // It is a really bad error to have no name and have the same
        // package ID.
        assert(reference.id.packageId() != mTable->getPackageId());

        // The reference goes outside this package, let it stay as a
        // resource ID because it will not change.
        return;
    }

    doResolveReference(reference, args.source);

    // TODO(adamlesinski): Verify the referencedType is another reference
    // or a compatible primitive.
}

void Linker::processAttributeValue(const ResourceNameRef& name, const SourceLine& source,
        const Attribute& attr, std::unique_ptr<Item>& value) {
    std::unique_ptr<Item> convertedValue;
    visitFunc<RawString>(*value, [&](RawString& str) {
        // This is a raw string, so check if it can be converted to anything.
        // We can NOT swap value with the converted value in here, since
        // we called through the original value.

        auto onCreateReference = [&](const ResourceName& name) {
            // We should never get here. All references would have been
            // parsed in the parser phase.
            assert(false);
        };

        convertedValue = ResourceParser::parseItemForAttribute(*str.value, attr,
                                                               onCreateReference);
        if (!convertedValue && attr.typeMask & android::ResTable_map::TYPE_STRING) {
            // Last effort is to parse as a string.
            util::StringBuilder builder;
            builder.append(*str.value);
            if (builder) {
                convertedValue = util::make_unique<String>(
                        mTable->getValueStringPool().makeRef(builder.str()));
            }
        }
    });

    if (convertedValue) {
        value = std::move(convertedValue);
    }

    // Process this new or old value (it can be a reference!).
    value->accept(*this, Args{ name, source });

    // Flatten the value to see what resource type it is.
    android::Res_value resValue;
    value->flatten(resValue);

    // Always allow references.
    const uint32_t typeMask = attr.typeMask | android::ResTable_map::TYPE_REFERENCE;
    if (!(typeMask & ResourceParser::androidTypeToAttributeTypeMask(resValue.dataType))) {
        Logger::error(source)
                << *value
                << " is not compatible with attribute "
                << attr
                << "."
                << std::endl;
        mError = true;
    }
}

void Linker::visit(Style& style, ValueVisitorArgs& a) {
    Args& args = static_cast<Args&>(a);

    if (style.parent.name.isValid() || style.parent.id.isValid()) {
        visit(style.parent, a);
    }

    for (Style::Entry& styleEntry : style.entries) {
        const Attribute* attr = doResolveAttribute(styleEntry.key, args.source);
        if (attr) {
            processAttributeValue(args.referrer, args.source, *attr, styleEntry.value);
        }
    }
}

void Linker::visit(Attribute& attr, ValueVisitorArgs& a) {
    static constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM |
            android::ResTable_map::TYPE_FLAGS;
    if (attr.typeMask & kMask) {
        for (auto& symbol : attr.symbols) {
            visit(symbol.symbol, a);
        }
    }
}

void Linker::visit(Styleable& styleable, ValueVisitorArgs& a) {
    for (auto& attrRef : styleable.entries) {
        visit(attrRef, a);
    }
}

void Linker::visit(Array& array, ValueVisitorArgs& a) {
    Args& args = static_cast<Args&>(a);

    for (auto& item : array.items) {
        item->accept(*this, Args{ args.referrer, args.source });
    }
}

void Linker::visit(Plural& plural, ValueVisitorArgs& a) {
    Args& args = static_cast<Args&>(a);

    for (auto& item : plural.values) {
        if (item) {
            item->accept(*this, Args{ args.referrer, args.source });
        }
    }
}

void Linker::addUnresolvedSymbol(const ResourceNameRef& name, const SourceLine& source) {
    mUnresolvedSymbols[name.toResourceName()].push_back(source);
}

} // namespace aapt