blob: 341c9b3d5107b9b7578b3bb40a9b842484cb07fc [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#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "compile/IdAssigner.h"
19#include "process/IResourceTableConsumer.h"
20#include "util/Util.h"
21
22#include <bitset>
23#include <cassert>
24#include <set>
25
26namespace aapt {
27
28bool IdAssigner::consume(IAaptContext* context, ResourceTable* table) {
29 std::bitset<256> usedTypeIds;
30 std::set<uint16_t> usedEntryIds;
31
32 for (auto& package : table->packages) {
33 assert(package->id && "packages must have manually assigned IDs");
34
35 usedTypeIds.reset();
36
37 // Type ID 0 is invalid, reserve it.
38 usedTypeIds.set(0);
39
40 // Collect used type IDs.
41 for (auto& type : package->types) {
42 if (type->id) {
43 usedEntryIds.clear();
44
45 if (usedTypeIds[type->id.value()]) {
46 // This ID is already taken!
47 context->getDiagnostics()->error(DiagMessage()
48 << "type '" << type->type << "' in "
49 << "package '" << package->name << "' has "
50 << "duplicate ID "
51 << std::hex << (int) type->id.value()
52 << std::dec);
53 return false;
54 }
55
56 // Mark the type ID as taken.
57 usedTypeIds.set(type->id.value());
58 }
59
60 // Collect used entry IDs.
61 for (auto& entry : type->entries) {
62 if (entry->id) {
63 // Mark entry ID as taken.
64 if (!usedEntryIds.insert(entry->id.value()).second) {
65 // This ID existed before!
Adam Lesinski59e04c62016-02-04 15:59:23 -080066 ResourceNameRef nameRef(package->name, type->type, entry->name);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 context->getDiagnostics()->error(DiagMessage()
68 << "resource '" << nameRef << "' "
Adam Lesinski59e04c62016-02-04 15:59:23 -080069 << "has duplicate entry ID "
70 << std::hex << (int) entry->id.value()
71 << std::dec);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 return false;
73 }
74 }
75 }
76
77 // Assign unused entry IDs.
78 const auto endUsedEntryIter = usedEntryIds.end();
79 auto nextUsedEntryIter = usedEntryIds.begin();
80 uint16_t nextId = 0;
81 for (auto& entry : type->entries) {
82 if (!entry->id) {
83 // Assign the next available entryID.
84 while (nextUsedEntryIter != endUsedEntryIter &&
85 nextId == *nextUsedEntryIter) {
86 nextId++;
87 ++nextUsedEntryIter;
88 }
89 entry->id = nextId++;
90 }
91 }
92 }
93
94 // Assign unused type IDs.
95 size_t nextTypeId = 0;
96 for (auto& type : package->types) {
97 if (!type->id) {
98 while (nextTypeId < usedTypeIds.size() && usedTypeIds[nextTypeId]) {
99 nextTypeId++;
100 }
101 type->id = static_cast<uint8_t>(nextTypeId);
102 nextTypeId++;
103 }
104 }
105 }
106 return true;
107}
108
109} // namespace aapt