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 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "compile/IdAssigner.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <map> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | #include "process/IResourceTableConsumer.h" |
| 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 29 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 30 | * Assigns the intended ID to the ResourceTablePackage, ResourceTableType, and |
| 31 | * ResourceEntry, |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 32 | * as long as there is no existing ID or the ID is the same. |
| 33 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | static bool AssignId(IDiagnostics* diag, const ResourceId& id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | const ResourceName& name, ResourceTablePackage* pkg, |
| 36 | ResourceTableType* type, ResourceEntry* entry) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | if (pkg->id.value() == id.package_id()) { |
| 38 | if (!type->id || type->id.value() == id.type_id()) { |
| 39 | type->id = id.type_id(); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | if (!entry->id || entry->id.value() == id.entry_id()) { |
| 42 | entry->id = id.entry_id(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | return true; |
| 44 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 45 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | const ResourceId existing_id(pkg->id.value(), type->id ? type->id.value() : 0, |
| 49 | entry->id ? entry->id.value() : 0); |
| 50 | diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " |
| 51 | << name << " with conflicting ID " << existing_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | return false; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | bool IdAssigner::Consume(IAaptContext* context, ResourceTable* table) { |
| 56 | std::map<ResourceId, ResourceName> assigned_ids; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | for (auto& package : table->packages) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | CHECK(bool(package->id)) << "packages must have manually assigned IDs"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | for (auto& type : package->types) { |
| 62 | for (auto& entry : type->entries) { |
| 63 | const ResourceName name(package->name, type->type, entry->name); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | if (assigned_id_map_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | // Assign the pre-assigned stable ID meant for this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | const auto iter = assigned_id_map_->find(name); |
| 68 | if (iter != assigned_id_map_->end()) { |
| 69 | const ResourceId assigned_id = iter->second; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | const bool result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | AssignId(context->GetDiagnostics(), assigned_id, name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | package.get(), type.get(), entry.get()); |
| 73 | if (!result) { |
| 74 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 77 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | if (package->id && type->id && entry->id) { |
| 80 | // If the ID is set for this resource, then reserve it. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | ResourceId resource_id(package->id.value(), type->id.value(), |
| 82 | entry->id.value()); |
| 83 | auto result = assigned_ids.insert({resource_id, name}); |
| 84 | const ResourceName& existing_name = result.first->second; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | if (!result.second) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | DiagMessage() << "resource " << name << " has same ID " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | << resource_id << " as " << existing_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 91 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | if (assigned_id_map_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | // Reserve all the IDs mentioned in the stable ID map. That way we won't |
| 98 | // assign |
| 99 | // IDs that were listed in the map if they don't exist in the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | for (const auto& stable_id_entry : *assigned_id_map_) { |
| 101 | const ResourceName& pre_assigned_name = stable_id_entry.first; |
| 102 | const ResourceId& pre_assigned_id = stable_id_entry.second; |
| 103 | auto result = assigned_ids.insert({pre_assigned_id, pre_assigned_name}); |
| 104 | const ResourceName& existing_name = result.first->second; |
| 105 | if (!result.second && existing_name != pre_assigned_name) { |
| 106 | context->GetDiagnostics()->Error( |
| 107 | DiagMessage() << "stable ID " << pre_assigned_id << " for resource " |
| 108 | << pre_assigned_name |
| 109 | << " is already taken by resource " << existing_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | // Assign any resources without IDs the next available ID. Gaps will be filled |
| 116 | // if possible, |
| 117 | // unless those IDs have been reserved. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | const auto assigned_ids_iter_end = assigned_ids.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | for (auto& package : table->packages) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | CHECK(bool(package->id)) << "packages must have manually assigned IDs"; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | // Build a half filled ResourceId object, which will be used to find the |
| 124 | // closest matching |
| 125 | // reserved ID in the assignedId map. From that point the next available |
| 126 | // type ID can be |
| 127 | // found. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | ResourceId resource_id(package->id.value(), 0, 0); |
| 129 | uint8_t next_expected_type_id = 1; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 130 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | // Find the closest matching ResourceId that is <= the one with only the |
| 132 | // package set. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | auto next_type_iter = assigned_ids.lower_bound(resource_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | for (auto& type : package->types) { |
| 135 | if (!type->id) { |
| 136 | // We need to assign a type ID. Iterate over the reserved IDs until we |
| 137 | // find |
| 138 | // some type ID that is a distance of 2 greater than the last one we've |
| 139 | // seen. |
| 140 | // That means there is an available type ID between these reserved IDs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | while (next_type_iter != assigned_ids_iter_end) { |
| 142 | if (next_type_iter->first.package_id() != package->id.value()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | break; |
| 144 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | const uint8_t type_id = next_type_iter->first.type_id(); |
| 147 | if (type_id > next_expected_type_id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | // There is a gap in the type IDs, so use the missing one. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | type->id = next_expected_type_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | break; |
| 151 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 152 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | // Set our expectation to be the next type ID after the reserved one |
| 154 | // we |
| 155 | // just saw. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | next_expected_type_id = type_id + 1; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 157 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | // Move to the next reserved ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | ++next_type_iter; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 160 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | |
| 162 | if (!type->id) { |
| 163 | // We must have hit the end of the reserved IDs and not found a gap. |
| 164 | // That means the next ID is available. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | type->id = next_expected_type_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | resource_id = ResourceId(package->id.value(), type->id.value(), 0); |
| 170 | uint16_t next_expected_entry_id = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | |
| 172 | // Find the closest matching ResourceId that is <= the one with only the |
| 173 | // package |
| 174 | // and type set. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | auto next_entry_iter = assigned_ids.lower_bound(resource_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | for (auto& entry : type->entries) { |
| 177 | if (!entry->id) { |
| 178 | // We need to assign an entry ID. Iterate over the reserved IDs until |
| 179 | // we find |
| 180 | // some entry ID that is a distance of 2 greater than the last one |
| 181 | // we've seen. |
| 182 | // That means there is an available entry ID between these reserved |
| 183 | // IDs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | while (next_entry_iter != assigned_ids_iter_end) { |
| 185 | if (next_entry_iter->first.package_id() != package->id.value() || |
| 186 | next_entry_iter->first.type_id() != type->id.value()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | const uint16_t entry_id = next_entry_iter->first.entry_id(); |
| 191 | if (entry_id > next_expected_entry_id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | // There is a gap in the entry IDs, so use the missing one. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | entry->id = next_expected_entry_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | |
| 197 | // Set our expectation to be the next type ID after the reserved one |
| 198 | // we |
| 199 | // just saw. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | next_expected_entry_id = entry_id + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | |
| 202 | // Move to the next reserved entry ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | ++next_entry_iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if (!entry->id) { |
| 207 | // We must have hit the end of the reserved IDs and not found a gap. |
| 208 | // That means the next ID is available. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | entry->id = next_expected_entry_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 213 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | } |
| 215 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | } // namespace aapt |