GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.8% 30 / 0 / 31
Functions: 88.9% 8 / 0 / 9
Branches: 55.4% 31 / 0 / 56

src/global/TypeRegistry.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "TypeRegistry.h"
4
5 #include <algorithm>
6 #include <cassert>
7 #include <ranges>
8 #include <sstream>
9
10 #include <symboltablebuilder/Type.h>
11 #include <util/Concurrency.h>
12 #include <util/CustomHashFunctions.h>
13
14 namespace spice::compiler {
15
16 // Static member initialization
17 std::unordered_map<uint64_t, std::unique_ptr<Type>> TypeRegistry::types = {};
18 std::mutex TypeRegistry::typesMutex;
19
20 /**
21 * Compute the hash for a type (aka type id)
22 *
23 * @param type Input type
24 * @return type hash / type id
25 */
26 12804921 uint64_t TypeRegistry::getTypeHash(const Type &type) { return std::hash<Type>{}(type); }
27
28 /**
29 * Get or insert a type into the type registry
30 *
31 * @param type The type to insert
32 * @return The inserted type
33 */
34 12804884 const Type *TypeRegistry::getOrInsert(const Type &&type) {
35 12804884 const uint64_t hash = getTypeHash(type);
36
37
1/2
✓ Branch 3 → 4 taken 12804884 times.
✗ Branch 3 → 29 not taken.
12804884 ConditionalLock lock(typesMutex);
38
39 // Check if type already exists
40
1/2
✓ Branch 4 → 5 taken 12804884 times.
✗ Branch 4 → 27 not taken.
12804884 const auto it = types.find(hash);
41
2/2
✓ Branch 7 → 8 taken 12751045 times.
✓ Branch 7 → 14 taken 53839 times.
12804884 if (it != types.end()) {
42 12751045 const Type *cachedType = it->second.get();
43 // This check should identify hash collisions
44
2/4
✓ Branch 10 → 11 taken 12751045 times.
✗ Branch 10 → 27 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 12751045 times.
12751045 assert(cachedType->typeChain == type.typeChain);
45 12751045 return cachedType;
46 }
47
48 // Create new type
49
2/4
✓ Branch 14 → 15 taken 53839 times.
✗ Branch 14 → 26 not taken.
✓ Branch 15 → 16 taken 53839 times.
✗ Branch 15 → 24 not taken.
53839 const auto [iter, inserted] = types.emplace(hash, std::make_unique<Type>(type));
50 53839 return iter->second.get();
51 12804884 }
52
53 /**
54 * Get or insert a type into the type registry
55 *
56 * @param superType The super type of the type
57 * @return The inserted type
58 */
59
2/4
✓ Branch 2 → 3 taken 3996227 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 3996227 times.
✗ Branch 3 → 8 not taken.
3996227 const Type *TypeRegistry::getOrInsert(SuperType superType) { return getOrInsert(Type(superType)); }
60
61 /**
62 * Get or insert a type into the type registry
63 *
64 * @param superType The super type of the type
65 * @param subType The sub type of the type
66 * @return The inserted type
67 */
68 5660 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType) {
69
2/4
✓ Branch 2 → 3 taken 5660 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 5660 times.
✗ Branch 3 → 8 not taken.
5660 return getOrInsert(Type(superType, subType));
70 }
71
72 /**
73 * Get or insert a type into the type registry
74 *
75 * @param superType The super type of the type
76 * @param subType The sub type of the type
77 * @param typeId The type ID of the type
78 * @param data The data of the type
79 * @param templateTypes The template types of the type
80 * @return The inserted type
81 */
82 6303 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType, uint64_t typeId,
83 const TypeChainElementData &data, const QualTypeList &templateTypes) {
84
2/4
✓ Branch 2 → 3 taken 6303 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 6303 times.
✗ Branch 3 → 8 not taken.
6303 return getOrInsert(Type(superType, subType, typeId, data, templateTypes));
85 }
86
87 /**
88 * Get or insert a type into the type registry
89 *
90 * @param typeChain The type chain of the type
91 * @return The inserted type
92 */
93
3/6
✓ Branch 2 → 3 taken 8796694 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 8796694 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 8796694 times.
✗ Branch 4 → 10 not taken.
8796694 const Type *TypeRegistry::getOrInsert(const TypeChain &typeChain) { return getOrInsert(Type(typeChain)); }
94
95 /**
96 * Get the number of types in the type registry
97 *
98 * @return The number of types in the type registry
99 */
100 size_t TypeRegistry::getTypeCount() { return types.size(); }
101
102 /**
103 * Dump all types in the type registry
104 */
105 333 std::string TypeRegistry::dump() {
106 333 std::vector<std::string> typeStrings;
107
1/2
✓ Branch 3 → 4 taken 333 times.
✗ Branch 3 → 46 not taken.
333 typeStrings.reserve(types.size());
108
4/6
✓ Branch 4 → 5 taken 333 times.
✗ Branch 4 → 42 not taken.
✓ Branch 5 → 6 taken 333 times.
✗ Branch 5 → 42 not taken.
✓ Branch 14 → 7 taken 49882 times.
✓ Branch 14 → 15 taken 333 times.
50215 for (const std::unique_ptr<Type> &type : types | std::views::values)
109
2/4
✓ Branch 9 → 10 taken 49882 times.
✗ Branch 9 → 41 not taken.
✓ Branch 10 → 11 taken 49882 times.
✗ Branch 10 → 39 not taken.
49882 typeStrings.push_back(type->getName(false, true, true));
110 // Sort to ensure deterministic output
111
1/2
✓ Branch 15 → 16 taken 333 times.
✗ Branch 15 → 46 not taken.
333 std::ranges::sort(typeStrings);
112 // Serialize type registry
113
1/2
✓ Branch 16 → 17 taken 333 times.
✗ Branch 16 → 46 not taken.
333 std::stringstream typeRegistryString;
114
2/2
✓ Branch 32 → 19 taken 49882 times.
✓ Branch 32 → 33 taken 333 times.
50548 for (const std::string &typeString : typeStrings)
115
2/4
✓ Branch 21 → 22 taken 49882 times.
✗ Branch 21 → 43 not taken.
✓ Branch 22 → 23 taken 49882 times.
✗ Branch 22 → 43 not taken.
49882 typeRegistryString << typeString << "\n";
116
1/2
✓ Branch 33 → 34 taken 333 times.
✗ Branch 33 → 44 not taken.
666 return typeRegistryString.str();
117 333 }
118
119 /**
120 * Clear the type registry
121 */
122 573 void TypeRegistry::clear() { types.clear(); }
123
124 } // namespace spice::compiler
125