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