GCC Code Coverage Report


Directory: ../
File: src/global/TypeRegistry.cpp
Date: 2024-11-22 23:10:59
Exec Total Coverage
Lines: 25 26 96.2%
Functions: 7 8 87.5%
Branches: 26 46 56.5%

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