src/global/TypeNameDisambiguator.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeNameDisambiguator.h" | ||
| 4 | |||
| 5 | namespace spice::compiler { | ||
| 6 | |||
| 7 | // Static member initialization | ||
| 8 | std::unordered_map<std::string, std::vector<uint64_t>> TypeNameDisambiguator::claimedTypeIds = {}; | ||
| 9 | |||
| 10 | /** | ||
| 11 | * Get the disambiguation suffix for a struct/interface type with the given name and type id. | ||
| 12 | * The first type id to claim a name keeps it unchanged (empty suffix); every further distinct type id sharing the name | ||
| 13 | * receives a deterministic, dot-separated suffix (e.g. ".1", ".2"). A dot is used on purpose: it is not a valid Spice | ||
| 14 | * identifier character, so the disambiguated name can never collide with a genuine, user-declared type name. | ||
| 15 | * | ||
| 16 | * @param name Simple type name | ||
| 17 | * @param typeId Custom type id of the type | ||
| 18 | * @return Disambiguation suffix (empty for the first claimer of the name) | ||
| 19 | */ | ||
| 20 | 955913 | std::string TypeNameDisambiguator::getDisambiguationSuffix(const std::string &name, uint64_t typeId) { | |
| 21 | 955913 | std::vector<uint64_t> &ids = claimedTypeIds[name]; | |
| 22 | |||
| 23 | // Find the index of the type id among the ones already claiming this name | ||
| 24 | 955913 | size_t index = ids.size(); | |
| 25 |
2/2✓ Branch 10 → 5 taken 981444 times.
✓ Branch 10 → 11 taken 3353 times.
|
984797 | for (size_t i = 0; i < ids.size(); i++) { |
| 26 |
2/2✓ Branch 6 → 7 taken 952560 times.
✓ Branch 6 → 8 taken 28884 times.
|
981444 | if (ids.at(i) == typeId) { |
| 27 | 952560 | index = i; | |
| 28 | 952560 | break; | |
| 29 | } | ||
| 30 | } | ||
| 31 | // If not seen yet, claim the name | ||
| 32 |
2/2✓ Branch 12 → 13 taken 3353 times.
✓ Branch 12 → 14 taken 952560 times.
|
955913 | if (index == ids.size()) |
| 33 | 3353 | ids.push_back(typeId); | |
| 34 | |||
| 35 | // The first claimer keeps the plain name to avoid churn for the common, non-colliding case | ||
| 36 |
2/2✓ Branch 14 → 15 taken 927029 times.
✓ Branch 14 → 21 taken 28884 times.
|
955913 | if (index == 0) |
| 37 |
1/2✓ Branch 17 → 18 taken 927029 times.
✗ Branch 17 → 27 not taken.
|
1854058 | return ""; |
| 38 |
2/4✓ Branch 21 → 22 taken 28884 times.
✗ Branch 21 → 32 not taken.
✓ Branch 22 → 23 taken 28884 times.
✗ Branch 22 → 30 not taken.
|
57768 | return "." + std::to_string(index); |
| 39 | } | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Clear the disambiguation registry. Must be called between compilations. | ||
| 43 | */ | ||
| 44 | 567 | void TypeNameDisambiguator::clear() { claimedTypeIds.clear(); } | |
| 45 | |||
| 46 | } // namespace spice::compiler | ||
| 47 |