GCC Code Coverage Report


Directory: ../
File: src/global/TypeRegistry.cpp
Date: 2025-11-03 08:42:58
Coverage Exec Excl Total
Lines: 96.4% 27 0 28
Functions: 88.9% 8 0 9
Branches: 56.0% 28 0 50

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 * Compute the hash for a type (aka type id)
17 *
18 * @param type Input type
19 * @return type hash / type id
20 */
21 6053268 uint64_t TypeRegistry::getTypeHash(const Type &type) {
22 6053268 return std::hash<Type>{}(type);
23 }
24
25 /**
26 * Get or insert a type into the type registry
27 *
28 * @param type The type to insert
29 * @return The inserted type
30 */
31 6053266 const Type *TypeRegistry::getOrInsert(const Type &&type) {
32 6053266 const uint64_t hash = getTypeHash(type);
33
34 // Check if type already exists
35
1/2
✓ Branch 3 → 4 taken 6053266 times.
✗ Branch 3 → 21 not taken.
6053266 const auto it = types.find(hash);
36
2/2
✓ Branch 6 → 7 taken 6035141 times.
✓ Branch 6 → 9 taken 18125 times.
6053266 if (it != types.end())
37 6035141 return it->second.get();
38
39 // Create new type
40
2/4
✓ Branch 9 → 10 taken 18125 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 18125 times.
✗ Branch 10 → 18 not taken.
18125 const auto [iter, inserted] = types.emplace(hash, std::make_unique<Type>(type));
41 18125 return iter->second.get();
42 }
43
44 /**
45 * Get or insert a type into the type registry
46 *
47 * @param superType The super type of the type
48 * @return The inserted type
49 */
50
2/4
✓ Branch 2 → 3 taken 4738347 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 4738347 times.
✗ Branch 3 → 8 not taken.
4738347 const Type *TypeRegistry::getOrInsert(SuperType superType) { return getOrInsert(Type(superType)); }
51
52 /**
53 * Get or insert a type into the type registry
54 *
55 * @param superType The super type of the type
56 * @param subType The sub type of the type
57 * @return The inserted type
58 */
59 1709 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType) {
60
2/4
✓ Branch 2 → 3 taken 1709 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 1709 times.
✗ Branch 3 → 8 not taken.
1709 return getOrInsert(Type(superType, subType));
61 }
62
63 /**
64 * Get or insert a type into the type registry
65 *
66 * @param superType The super type of the type
67 * @param subType The sub type of the type
68 * @param typeId The type ID of the type
69 * @param data The data of the type
70 * @param templateTypes The template types of the type
71 * @return The inserted type
72 */
73 1125 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType, uint64_t typeId,
74 const TypeChainElementData &data, const QualTypeList &templateTypes) {
75
2/4
✓ Branch 2 → 3 taken 1125 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 1125 times.
✗ Branch 3 → 8 not taken.
1125 return getOrInsert(Type(superType, subType, typeId, data, templateTypes));
76 }
77
78 /**
79 * Get or insert a type into the type registry
80 *
81 * @param typeChain The type chain of the type
82 * @return The inserted type
83 */
84
3/6
✓ Branch 2 → 3 taken 1312085 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 1312085 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 1312085 times.
✗ Branch 4 → 10 not taken.
1312085 const Type *TypeRegistry::getOrInsert(const TypeChain &typeChain) { return getOrInsert(Type(typeChain)); }
85
86 /**
87 * Get the number of types in the type registry
88 *
89 * @return The number of types in the type registry
90 */
91 size_t TypeRegistry::getTypeCount() { return types.size(); }
92
93 /**
94 * Dump all types in the type registry
95 */
96 204 std::string TypeRegistry::dump() {
97 204 std::vector<std::string> typeStrings;
98
1/2
✓ Branch 3 → 4 taken 204 times.
✗ Branch 3 → 38 not taken.
204 typeStrings.reserve(types.size());
99
4/6
✓ Branch 4 → 5 taken 204 times.
✗ Branch 4 → 34 not taken.
✓ Branch 5 → 6 taken 204 times.
✗ Branch 5 → 34 not taken.
✓ Branch 14 → 7 taken 15219 times.
✓ Branch 14 → 15 taken 204 times.
15423 for (const std::unique_ptr<Type> &type : types | std::views::values)
100
2/4
✓ Branch 9 → 10 taken 15219 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 15219 times.
✗ Branch 10 → 31 not taken.
15219 typeStrings.push_back(type->getName(false, true));
101 // Sort to ensure deterministic output
102
1/2
✓ Branch 15 → 16 taken 204 times.
✗ Branch 15 → 38 not taken.
204 std::ranges::sort(typeStrings);
103 // Serialize type registry
104
1/2
✓ Branch 16 → 17 taken 204 times.
✗ Branch 16 → 38 not taken.
204 std::stringstream typeRegistryString;
105
2/2
✓ Branch 24 → 19 taken 15219 times.
✓ Branch 24 → 25 taken 204 times.
15423 for (const std::string &typeString : typeStrings)
106
2/4
✓ Branch 20 → 21 taken 15219 times.
✗ Branch 20 → 35 not taken.
✓ Branch 21 → 22 taken 15219 times.
✗ Branch 21 → 35 not taken.
15219 typeRegistryString << typeString << "\n";
107
1/2
✓ Branch 25 → 26 taken 204 times.
✗ Branch 25 → 36 not taken.
408 return typeRegistryString.str();
108 204 }
109
110 /**
111 * Clear the type registry
112 */
113 420 void TypeRegistry::clear() { types.clear(); }
114
115 } // namespace spice::compiler
116