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> |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 22 | #include "android-base/expected.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
| 24 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 25 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "util/Util.h" |
| 28 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 29 | using android::base::expected; |
| 30 | using android::base::unexpected; |
| 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | template <typename T> |
| 36 | using Result = expected<T, std::string>; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 37 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 38 | template <typename Id, typename Key> |
| 39 | struct NextIdFinder { |
Brandon Liu | 4cb4624 | 2023-02-16 02:55:56 +0000 | [diff] [blame^] | 40 | std::map<Id, Key> pre_assigned_ids_; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 41 | explicit NextIdFinder(Id start_id = 0u) : next_id_(start_id){}; |
| 42 | |
| 43 | // Attempts to reserve an identifier for the specified key. |
| 44 | // If the identifier is already reserved by a different key, an error message is returned. |
| 45 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
| 46 | Result<Id> ReserveId(Key key, Id id); |
| 47 | |
| 48 | // Retrieves the next available identifier that has not been reserved. |
| 49 | std::optional<Id> NextId(); |
| 50 | |
| 51 | private: |
| 52 | // Attempts to set `next_id_` to the next available identifier that has not been reserved. |
| 53 | // Returns whether there were any available identifiers. |
| 54 | std::optional<Id> SkipToNextAvailableId(); |
| 55 | |
| 56 | Id next_id_; |
| 57 | bool next_id_called_ = false; |
| 58 | bool exhausted_ = false; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 59 | typename std::map<Id, Key>::iterator next_preassigned_id_; |
| 60 | }; |
| 61 | |
| 62 | struct TypeGroup { |
| 63 | explicit TypeGroup(uint8_t package_id, uint8_t type_id) |
| 64 | : package_id_(package_id), type_id_(type_id){}; |
| 65 | |
| 66 | // Attempts to reserve the resource id for the specified resource name. |
| 67 | // If the id is already reserved by a different name, an error message is returned. |
| 68 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
| 69 | Result<std::monostate> ReserveId(const ResourceName& name, ResourceId id); |
| 70 | |
| 71 | // Retrieves the next available resource id that has not been reserved. |
| 72 | Result<ResourceId> NextId(); |
| 73 | |
| 74 | private: |
| 75 | uint8_t package_id_; |
| 76 | uint8_t type_id_; |
| 77 | NextIdFinder<uint16_t, ResourceName> next_entry_id_; |
| 78 | }; |
| 79 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 80 | struct ResourceTypeKey { |
| 81 | ResourceType type; |
| 82 | uint8_t id; |
| 83 | |
| 84 | bool operator<(const ResourceTypeKey& other) const { |
| 85 | return (type != other.type) ? type < other.type : id < other.id; |
| 86 | } |
| 87 | |
| 88 | bool operator==(const ResourceTypeKey& other) const { |
| 89 | return type == other.type && id == other.id; |
| 90 | } |
| 91 | |
| 92 | bool operator!=(const ResourceTypeKey& other) const { |
| 93 | return !(*this == other); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | ::std::ostream& operator<<(::std::ostream& out, const ResourceTypeKey& type) { |
| 98 | return out << type.type; |
| 99 | } |
| 100 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 101 | struct IdAssignerContext { |
| 102 | IdAssignerContext(std::string package_name, uint8_t package_id) |
| 103 | : package_name_(std::move(package_name)), package_id_(package_id) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 105 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 106 | // Attempts to reserve the resource id for the specified resource name. |
| 107 | // Returns whether the id was reserved successfully. |
| 108 | // Reserving identifiers must be completed before `NextId` is called for the first time. |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 109 | bool ReserveId(const ResourceName& name, ResourceId id, const Visibility& visibility, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 110 | android::IDiagnostics* diag); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 111 | |
| 112 | // Retrieves the next available resource id that has not been reserved. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 113 | std::optional<ResourceId> NextId(const ResourceName& name, android::IDiagnostics* diag); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | std::string package_name_; |
| 117 | uint8_t package_id_; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 118 | std::map<ResourceTypeKey, TypeGroup> types_; |
| 119 | std::map<ResourceType, uint8_t> non_staged_type_ids_; |
| 120 | NextIdFinder<uint8_t, ResourceTypeKey> type_id_finder_ = |
| 121 | NextIdFinder<uint8_t, ResourceTypeKey>(1); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | bool IdAssigner::Consume(IAaptContext* context, ResourceTable* table) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 127 | IdAssignerContext assigned_ids(context->GetCompilationPackage(), context->GetPackageId()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | for (auto& package : table->packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | for (auto& type : package->types) { |
| 130 | for (auto& entry : type->entries) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 131 | const ResourceName name(package->name, type->named_type, entry->name); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 132 | if (entry->id && !assigned_ids.ReserveId(name, entry->id.value(), entry->visibility, |
| 133 | context->GetDiagnostics())) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | auto v = entry->visibility; |
| 138 | v.staged_api = true; |
| 139 | if (entry->staged_id && !assigned_ids.ReserveId(name, entry->staged_id.value().id, v, |
| 140 | context->GetDiagnostics())) { |
| 141 | return false; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | if (assigned_id_map_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | // Assign the pre-assigned stable ID meant for this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | const auto iter = assigned_id_map_->find(name); |
| 147 | if (iter != assigned_id_map_->end()) { |
| 148 | const ResourceId assigned_id = iter->second; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 149 | if (!assigned_ids.ReserveId(name, assigned_id, entry->visibility, |
| 150 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 153 | entry->id = assigned_id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | } |
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 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | if (assigned_id_map_) { |
Brandon Liu | 4cb4624 | 2023-02-16 02:55:56 +0000 | [diff] [blame^] | 161 | // Reserve all the IDs mentioned in the stable ID map. That way we won't assign IDs that were |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 162 | // listed in the map if they don't exist in the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | for (const auto& stable_id_entry : *assigned_id_map_) { |
| 164 | const ResourceName& pre_assigned_name = stable_id_entry.first; |
| 165 | const ResourceId& pre_assigned_id = stable_id_entry.second; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 166 | if (!assigned_ids.ReserveId(pre_assigned_name, pre_assigned_id, {}, |
| 167 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 172 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 173 | // Assign any resources without IDs the next available ID. Gaps will be filled if possible, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | // unless those IDs have been reserved. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | for (auto& package : table->packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | for (auto& type : package->types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | for (auto& entry : type->entries) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 178 | const ResourceName name(package->name, type->named_type, entry->name); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 179 | if (entry->id) { |
| 180 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 182 | auto id = assigned_ids.NextId(name, context->GetDiagnostics()); |
| 183 | if (!id.has_value()) { |
| 184 | return false; |
| 185 | } |
| 186 | entry->id = id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | } |
| 190 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 193 | namespace { |
Brandon Liu | 4cb4624 | 2023-02-16 02:55:56 +0000 | [diff] [blame^] | 194 | static const std::string_view staged_type_overlap_error = |
| 195 | "Staged public resource type IDs have conflict with non staged public resources type " |
| 196 | "IDs, please restart staged resource type ID assignment at 0xff in public-staging.xml " |
| 197 | "and also delete all the overlapping groups in public-final.xml"; |
| 198 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 199 | template <typename Id, typename Key> |
| 200 | Result<Id> NextIdFinder<Id, Key>::ReserveId(Key key, Id id) { |
| 201 | CHECK(!next_id_called_) << "ReserveId cannot be called after NextId"; |
| 202 | auto assign_result = pre_assigned_ids_.emplace(id, key); |
| 203 | if (!assign_result.second && assign_result.first->second != key) { |
| 204 | std::stringstream error; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 205 | error << "ID is already assigned to " << assign_result.first->second; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 206 | return unexpected(error.str()); |
| 207 | } |
| 208 | return id; |
| 209 | } |
| 210 | |
| 211 | template <typename Id, typename Key> |
| 212 | std::optional<Id> NextIdFinder<Id, Key>::NextId() { |
| 213 | if (!next_id_called_) { |
| 214 | next_id_called_ = true; |
| 215 | next_preassigned_id_ = pre_assigned_ids_.begin(); |
| 216 | } |
| 217 | return SkipToNextAvailableId(); |
| 218 | } |
| 219 | |
| 220 | template <typename Id, typename Key> |
| 221 | std::optional<Id> NextIdFinder<Id, Key>::SkipToNextAvailableId() { |
| 222 | if (exhausted_) { |
| 223 | return {}; |
| 224 | } |
| 225 | while (next_preassigned_id_ != pre_assigned_ids_.end()) { |
| 226 | if (next_preassigned_id_->first == next_id_) { |
| 227 | if (next_id_ == std::numeric_limits<Id>::max()) { |
| 228 | // The last identifier was reserved so there are no more available identifiers. |
| 229 | exhausted_ = true; |
| 230 | return {}; |
| 231 | } |
| 232 | ++next_id_; |
| 233 | ++next_preassigned_id_; |
| 234 | continue; |
| 235 | } |
| 236 | CHECK(next_preassigned_id_->first > next_id_) << "Preassigned IDs are not in sorted order"; |
| 237 | break; |
| 238 | } |
| 239 | if (next_id_ == std::numeric_limits<Id>::max()) { |
| 240 | // There are no more identifiers after this one, but this one is still available so return it. |
| 241 | exhausted_ = true; |
| 242 | } |
| 243 | return next_id_++; |
| 244 | } |
| 245 | |
| 246 | Result<std::monostate> TypeGroup::ReserveId(const ResourceName& name, ResourceId id) { |
| 247 | if (type_id_ != id.type_id()) { |
| 248 | // Currently there cannot be multiple type ids for a single type. |
| 249 | std::stringstream error; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 250 | error << "type '" << name.type << "' already has ID " << std::hex << (int)type_id_; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 251 | return unexpected(error.str()); |
| 252 | } |
| 253 | |
| 254 | auto assign_result = next_entry_id_.ReserveId(name, id.entry_id()); |
| 255 | if (!assign_result.has_value()) { |
| 256 | std::stringstream error; |
| 257 | error << "entry " << assign_result.error(); |
| 258 | return unexpected(error.str()); |
| 259 | } |
| 260 | return {}; |
| 261 | } |
| 262 | |
| 263 | Result<ResourceId> TypeGroup::NextId() { |
| 264 | auto entry_id = next_entry_id_.NextId(); |
| 265 | if (!entry_id.has_value()) { |
| 266 | std::stringstream error; |
| 267 | error << "resource type ID has exceeded the maximum number of resource entries (" |
| 268 | << (std::numeric_limits<uint16_t>::max() + 1u) << ")"; |
| 269 | return unexpected(error.str()); |
| 270 | } |
| 271 | return ResourceId(package_id_, type_id_, entry_id.value()); |
| 272 | } |
| 273 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 274 | bool IdAssignerContext::ReserveId(const ResourceName& name, ResourceId id, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 275 | const Visibility& visibility, android::IDiagnostics* diag) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 276 | if (package_id_ != id.package_id()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 277 | diag->Error(android::DiagMessage() |
| 278 | << "can't assign ID " << id << " to resource " << name |
| 279 | << " because package already has ID " << std::hex << (int)id.package_id()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 283 | auto key = ResourceTypeKey{name.type.type, id.type_id()}; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 284 | auto type = types_.find(key); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 285 | if (type == types_.end()) { |
| 286 | // The type has not been assigned an id yet. Ensure that the specified id is not being used by |
| 287 | // another type. |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 288 | auto assign_result = type_id_finder_.ReserveId(key, id.type_id()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 289 | if (!assign_result.has_value()) { |
Brandon Liu | 4cb4624 | 2023-02-16 02:55:56 +0000 | [diff] [blame^] | 290 | auto pre_assigned_type = type_id_finder_.pre_assigned_ids_[id.type_id()].type; |
| 291 | bool pre_assigned_type_staged = |
| 292 | non_staged_type_ids_.find(pre_assigned_type) == non_staged_type_ids_.end(); |
| 293 | auto hex_type_id = fmt::format("{:#04x}", (int)id.type_id()); |
| 294 | bool current_type_staged = visibility.staged_api; |
| 295 | diag->Error(android::DiagMessage() |
| 296 | << "can't assign type ID " << hex_type_id << " to " |
| 297 | << (current_type_staged ? "staged type " : "non staged type ") << name.type.type |
| 298 | << " because this type ID have been assigned to " |
| 299 | << (pre_assigned_type_staged ? "staged type " : "non staged type ") |
| 300 | << pre_assigned_type); |
| 301 | if (pre_assigned_type_staged || current_type_staged) { |
| 302 | diag->Error(android::DiagMessage() << staged_type_overlap_error); |
| 303 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 304 | return false; |
| 305 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 306 | type = types_.emplace(key, TypeGroup(package_id_, id.type_id())).first; |
| 307 | } |
| 308 | |
| 309 | if (!visibility.staged_api) { |
| 310 | // Ensure that non-staged resources can only exist in one type ID. |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 311 | auto non_staged_type = non_staged_type_ids_.emplace(name.type.type, id.type_id()); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 312 | if (!non_staged_type.second && non_staged_type.first->second != id.type_id()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 313 | diag->Error(android::DiagMessage() |
| 314 | << "can't assign ID " << id << " to resource " << name |
| 315 | << " because type already has ID " << std::hex << (int)id.type_id()); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 316 | return false; |
| 317 | } |
Brandon Liu | 4cb4624 | 2023-02-16 02:55:56 +0000 | [diff] [blame^] | 318 | } else { |
| 319 | // Ensure that staged public resources cannot have the same type name and type id with |
| 320 | // non staged public resources. |
| 321 | auto non_staged_type = non_staged_type_ids_.find(name.type.type); |
| 322 | if (non_staged_type != non_staged_type_ids_.end() && non_staged_type->second == id.type_id()) { |
| 323 | diag->Error( |
| 324 | android::DiagMessage() |
| 325 | << "can`t assign type ID " << fmt::format("{:#04x}", (int)id.type_id()) |
| 326 | << " to staged type " << name.type.type << " because type ID " |
| 327 | << fmt::format("{:#04x}", (int)id.type_id()) |
| 328 | << " already has been assigned to a non staged resource type with the same type name"); |
| 329 | diag->Error(android::DiagMessage() << staged_type_overlap_error); |
| 330 | return false; |
| 331 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | auto assign_result = type->second.ReserveId(name, id); |
| 335 | if (!assign_result.has_value()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 336 | diag->Error(android::DiagMessage() << "can't assign ID " << id << " to resource " << name |
| 337 | << " because " << assign_result.error()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
| 341 | return true; |
| 342 | } |
| 343 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 344 | std::optional<ResourceId> IdAssignerContext::NextId(const ResourceName& name, |
| 345 | android::IDiagnostics* diag) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 346 | // The package name is not known during the compile stage. |
| 347 | // Resources without a package name are considered a part of the app being linked. |
| 348 | CHECK(name.package.empty() || name.package == package_name_); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 349 | |
| 350 | // Find the type id for non-staged resources of this type. |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 351 | auto non_staged_type = non_staged_type_ids_.find(name.type.type); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 352 | if (non_staged_type == non_staged_type_ids_.end()) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 353 | auto next_type_id = type_id_finder_.NextId(); |
| 354 | CHECK(next_type_id.has_value()) << "resource type IDs allocated have exceeded maximum (256)"; |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 355 | non_staged_type = non_staged_type_ids_.emplace(name.type.type, *next_type_id).first; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 358 | ResourceTypeKey key{name.type.type, non_staged_type->second}; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 359 | auto type = types_.find(key); |
| 360 | if (type == types_.end()) { |
| 361 | type = types_.emplace(key, TypeGroup(package_id_, key.id)).first; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | auto assign_result = type->second.NextId(); |
| 365 | if (!assign_result.has_value()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 366 | diag->Error(android::DiagMessage() << "can't assign resource ID to resource " << name |
| 367 | << " because " << assign_result.error()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 368 | return {}; |
| 369 | } |
| 370 | return assign_result.value(); |
| 371 | } |
| 372 | } // namespace |
| 373 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | } // namespace aapt |