GCC Code Coverage Report


Directory: ../
File: src/global/TypeRegistry.cpp
Date: 2025-06-14 23:29:02
Exec Total Coverage
Lines: 27 28 96.4%
Functions: 8 9 88.9%
Branches: 28 50 56.0%

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 5354358 uint64_t TypeRegistry::getTypeHash(const Type &type) {
22 5354358 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 5354356 const Type *TypeRegistry::getOrInsert(const Type &&type) {
32 5354356 const uint64_t hash = getTypeHash(type);
33
34 // Check if type already exists
35
1/2
✓ Branch 0 (3→4) taken 5354356 times.
✗ Branch 1 (3→21) not taken.
5354356 const auto it = types.find(hash);
36
2/2
✓ Branch 0 (6→7) taken 5338710 times.
✓ Branch 1 (6→9) taken 15646 times.
5354356 if (it != types.end())
37 5338710 return it->second.get();
38
39 // Create new type
40
2/4
✓ Branch 0 (9→10) taken 15646 times.
✗ Branch 1 (9→20) not taken.
✓ Branch 2 (10→11) taken 15646 times.
✗ Branch 3 (10→18) not taken.
15646 const auto [iter, inserted] = types.emplace(hash, std::make_unique<Type>(type));
41 15646 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 0 (2→3) taken 4225418 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 4225418 times.
✗ Branch 3 (3→8) not taken.
4225418 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 1532 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType) {
60
2/4
✓ Branch 0 (2→3) taken 1532 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 1532 times.
✗ Branch 3 (3→8) not taken.
1532 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 1010 const Type *TypeRegistry::getOrInsert(SuperType superType, const std::string &subType, uint64_t typeId,
74 const TypeChainElementData &data, const QualTypeList &templateTypes) {
75
2/4
✓ Branch 0 (2→3) taken 1010 times.
✗ Branch 1 (2→10) not taken.
✓ Branch 2 (3→4) taken 1010 times.
✗ Branch 3 (3→8) not taken.
1010 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 0 (2→3) taken 1126396 times.
✗ Branch 1 (2→14) not taken.
✓ Branch 2 (3→4) taken 1126396 times.
✗ Branch 3 (3→12) not taken.
✓ Branch 4 (4→5) taken 1126396 times.
✗ Branch 5 (4→10) not taken.
1126396 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 199 std::string TypeRegistry::dump() {
97 199 std::vector<std::string> typeStrings;
98
1/2
✓ Branch 0 (3→4) taken 199 times.
✗ Branch 1 (3→38) not taken.
199 typeStrings.reserve(types.size());
99
4/6
✓ Branch 0 (4→5) taken 199 times.
✗ Branch 1 (4→34) not taken.
✓ Branch 2 (5→6) taken 199 times.
✗ Branch 3 (5→34) not taken.
✓ Branch 4 (14→7) taken 12920 times.
✓ Branch 5 (14→15) taken 199 times.
13119 for (const std::unique_ptr<Type> &type : types | std::views::values)
100
2/4
✓ Branch 0 (9→10) taken 12920 times.
✗ Branch 1 (9→33) not taken.
✓ Branch 2 (10→11) taken 12920 times.
✗ Branch 3 (10→31) not taken.
12920 typeStrings.push_back(type->getName(false, true));
101 // Sort to ensure deterministic output
102
1/2
✓ Branch 0 (15→16) taken 199 times.
✗ Branch 1 (15→38) not taken.
199 std::ranges::sort(typeStrings);
103 // Serialize type registry
104
1/2
✓ Branch 0 (16→17) taken 199 times.
✗ Branch 1 (16→38) not taken.
199 std::stringstream typeRegistryString;
105
2/2
✓ Branch 0 (24→19) taken 12920 times.
✓ Branch 1 (24→25) taken 199 times.
13119 for (const std::string &typeString : typeStrings)
106
2/4
✓ Branch 0 (20→21) taken 12920 times.
✗ Branch 1 (20→35) not taken.
✓ Branch 2 (21→22) taken 12920 times.
✗ Branch 3 (21→35) not taken.
12920 typeRegistryString << typeString << "\n";
107
1/2
✓ Branch 0 (25→26) taken 199 times.
✗ Branch 1 (25→36) not taken.
398 return typeRegistryString.str();
108 199 }
109
110 /**
111 * Clear the type registry
112 */
113 410 void TypeRegistry::clear() { types.clear(); }
114
115 } // namespace spice::compiler
116