Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "TypeRegistry.h" | ||
4 | |||
5 | #include <ranges> | ||
6 | #include <sstream> | ||
7 | |||
8 | #include <util/CustomHashFunctions.h> | ||
9 | |||
10 | namespace spice::compiler { | ||
11 | |||
12 | // Static member initialization | ||
13 | std::unordered_map<uint64_t, std::unique_ptr<Type>> TypeRegistry::types = {}; | ||
14 | |||
15 | /** | ||
16 | * Get or insert a type into the type registry | ||
17 | * | ||
18 | * @param type The type to insert | ||
19 | * @return The inserted type | ||
20 | */ | ||
21 | 4802800 | const Type *TypeRegistry::getOrInsert(const Type &&type) { | |
22 | 4802800 | const uint64_t hash = std::hash<Type>{}(type); | |
23 | |||
24 | // Check if type already exists | ||
25 |
1/2✓ Branch 0 (3→4) taken 4802800 times.
✗ Branch 1 (3→21) not taken.
|
4802800 | const auto it = types.find(hash); |
26 |
2/2✓ Branch 0 (6→7) taken 4788176 times.
✓ Branch 1 (6→9) taken 14624 times.
|
4802800 | if (it != types.end()) |
27 | 4788176 | return it->second.get(); | |
28 | |||
29 | // Create new type | ||
30 |
2/4✓ Branch 0 (9→10) taken 14624 times.
✗ Branch 1 (9→20) not taken.
✓ Branch 2 (10→11) taken 14624 times.
✗ Branch 3 (10→18) not taken.
|
14624 | const auto [iter, inserted] = types.emplace(hash, std::make_unique<Type>(type)); |
31 | 14624 | return iter->second.get(); | |
32 | } | ||
33 | |||
34 | /** | ||
35 | * Get or insert a type into the type registry | ||
36 | * | ||
37 | * @param superType The super type of the type | ||
38 | * @return The inserted type | ||
39 | */ | ||
40 |
2/4✓ Branch 0 (2→3) taken 3752991 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 3752991 times.
✗ Branch 3 (3→8) not taken.
|
3752991 | const Type *TypeRegistry::getOrInsert(SuperType superType) { return getOrInsert(Type(superType)); } |
41 | |||
42 | /** | ||
43 | * Get or insert a type into the type registry | ||
44 | * | ||
45 | * @param superType The super type of the type | ||
46 | * @param subType The sub type of the type | ||
47 | * @return The inserted type | ||
48 | */ | ||
49 | 1397 | const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType) { | |
50 |
2/4✓ Branch 0 (2→3) taken 1397 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 1397 times.
✗ Branch 3 (3→8) not taken.
|
1397 | return getOrInsert(Type(superType, subType)); |
51 | } | ||
52 | |||
53 | /** | ||
54 | * Get or insert a type into the type registry | ||
55 | * | ||
56 | * @param superType The super type of the type | ||
57 | * @param subType The sub type of the type | ||
58 | * @param typeId The type ID of the type | ||
59 | * @param data The data of the type | ||
60 | * @param templateTypes The template types of the type | ||
61 | * @return The inserted type | ||
62 | */ | ||
63 | 944 | const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType, uint64_t typeId, | |
64 | const TypeChainElementData &data, const QualTypeList &templateTypes) { | ||
65 |
2/4✓ Branch 0 (2→3) taken 944 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 944 times.
✗ Branch 3 (3→8) not taken.
|
944 | return getOrInsert(Type(superType, subType, typeId, data, templateTypes)); |
66 | } | ||
67 | |||
68 | /** | ||
69 | * Get or insert a type into the type registry | ||
70 | * | ||
71 | * @param typeChain The type chain of the type | ||
72 | * @return The inserted type | ||
73 | */ | ||
74 |
3/6✓ Branch 0 (2→3) taken 1047468 times.
✗ Branch 1 (2→14) not taken.
✓ Branch 2 (3→4) taken 1047468 times.
✗ Branch 3 (3→12) not taken.
✓ Branch 4 (4→5) taken 1047468 times.
✗ Branch 5 (4→10) not taken.
|
1047468 | const Type *TypeRegistry::getOrInsert(const TypeChain &typeChain) { return getOrInsert(Type(typeChain)); } |
75 | |||
76 | /** | ||
77 | * Get the number of types in the type registry | ||
78 | * | ||
79 | * @return The number of types in the type registry | ||
80 | */ | ||
81 | ✗ | size_t TypeRegistry::getTypeCount() { return types.size(); } | |
82 | |||
83 | /** | ||
84 | * Dump all types in the type registry | ||
85 | */ | ||
86 | 194 | std::string TypeRegistry::dump() { | |
87 | 194 | std::vector<std::string> typeStrings; | |
88 |
1/2✓ Branch 0 (3→4) taken 194 times.
✗ Branch 1 (3→38) not taken.
|
194 | typeStrings.reserve(types.size()); |
89 |
4/6✓ Branch 0 (4→5) taken 194 times.
✗ Branch 1 (4→34) not taken.
✓ Branch 2 (5→6) taken 194 times.
✗ Branch 3 (5→34) not taken.
✓ Branch 4 (14→7) taken 11971 times.
✓ Branch 5 (14→15) taken 194 times.
|
12165 | for (const std::unique_ptr<Type>& type : types | std::views::values) |
90 |
2/4✓ Branch 0 (9→10) taken 11971 times.
✗ Branch 1 (9→33) not taken.
✓ Branch 2 (10→11) taken 11971 times.
✗ Branch 3 (10→31) not taken.
|
11971 | typeStrings.push_back(type->getName(false)); |
91 | // Sort to ensure deterministic output | ||
92 |
1/2✓ Branch 0 (15→16) taken 194 times.
✗ Branch 1 (15→38) not taken.
|
194 | std::ranges::sort(typeStrings); |
93 | // Serialize type registry | ||
94 |
1/2✓ Branch 0 (16→17) taken 194 times.
✗ Branch 1 (16→38) not taken.
|
194 | std::stringstream typeRegistryString; |
95 |
2/2✓ Branch 0 (24→19) taken 11971 times.
✓ Branch 1 (24→25) taken 194 times.
|
12165 | for (const std::string &typeString : typeStrings) |
96 |
2/4✓ Branch 0 (20→21) taken 11971 times.
✗ Branch 1 (20→35) not taken.
✓ Branch 2 (21→22) taken 11971 times.
✗ Branch 3 (21→35) not taken.
|
11971 | typeRegistryString << typeString << "\n"; |
97 |
1/2✓ Branch 0 (25→26) taken 194 times.
✗ Branch 1 (25→36) not taken.
|
388 | return typeRegistryString.str(); |
98 | 194 | } | |
99 | |||
100 | /** | ||
101 | * Clear the type registry | ||
102 | */ | ||
103 | 401 | void TypeRegistry::clear() { types.clear(); } | |
104 | |||
105 | } // namespace spice::compiler | ||
106 |