GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.4% 150 / 0 / 154
Functions: 100.0% 11 / 0 / 11
Branches: 57.5% 165 / 0 / 287

src/typechecker/StructManager.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "StructManager.h"
4
5 #include <ast/ASTNodes.h>
6 #include <exception/SemanticError.h>
7 #include <model/GenericType.h>
8 #include <model/Interface.h>
9 #include <symboltablebuilder/Scope.h>
10 #include <typechecker/TypeMatcher.h>
11 #include <util/CodeLoc.h>
12 #include <util/CustomHashFunctions.h>
13
14 namespace spice::compiler {
15
16 // Static member initialization
17 std::unordered_map<uint64_t, Struct *> StructManager::lookupCache = {};
18 size_t StructManager::lookupCacheHits = 0;
19 size_t StructManager::lookupCacheMisses = 0;
20
21 731 Struct *StructManager::insert(Scope *insertScope, Struct &spiceStruct, std::vector<Struct *> *nodeStructList) {
22 // Open a new manifestation list. Which gets filled by the substantiated manifestations of the struct
23
3/6
✓ Branch 2 → 3 taken 731 times.
✗ Branch 2 → 22 not taken.
✓ Branch 3 → 4 taken 731 times.
✗ Branch 3 → 19 not taken.
✓ Branch 4 → 5 taken 731 times.
✗ Branch 4 → 17 not taken.
731 const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn();
24
2/4
✓ Branch 8 → 9 taken 731 times.
✗ Branch 8 → 25 not taken.
✓ Branch 9 → 10 taken 731 times.
✗ Branch 9 → 23 not taken.
731 insertScope->structs.insert({structId, StructManifestationList()});
25
26 // Save substantiation in declaration node
27
1/2
✓ Branch 12 → 13 taken 731 times.
✗ Branch 12 → 29 not taken.
731 Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode);
28
1/2
✓ Branch 13 → 14 taken 731 times.
✗ Branch 13 → 29 not taken.
731 nodeStructList->push_back(substantiation);
29
30 731 return substantiation;
31 731 }
32
33 1203 Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) {
34
1/2
✓ Branch 2 → 3 taken 1203 times.
✗ Branch 2 → 42 not taken.
1203 const std::string signature = newManifestation.getSignature();
35
36 #ifndef NDEBUG
37 // Make sure that the manifestation does not exist already
38
5/8
✓ Branch 3 → 4 taken 1203 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 1203 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 1203 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 1856 times.
✓ Branch 13 → 14 taken 1203 times.
3059 for (const auto &val : insertScope->structs | std::views::values)
39
2/4
✓ Branch 8 → 9 taken 1856 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1856 times.
1856 assert(!val.contains(signature));
40 #endif
41
42 // Retrieve the matching manifestation list of the scope
43
3/6
✓ Branch 14 → 15 taken 1203 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 1203 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 1203 times.
✗ Branch 16 → 32 not taken.
1203 const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
44
2/4
✓ Branch 19 → 20 taken 1203 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1203 times.
1203 assert(insertScope->structs.contains(structId));
45
1/2
✓ Branch 22 → 23 taken 1203 times.
✗ Branch 22 → 38 not taken.
1203 StructManifestationList &manifestationList = insertScope->structs.at(structId);
46
47 // Add substantiated struct
48 1203 newManifestation.manifestationIndex = manifestationList.size();
49
1/2
✓ Branch 24 → 25 taken 1203 times.
✗ Branch 24 → 38 not taken.
1203 manifestationList.emplace(signature, newManifestation);
50
1/2
✓ Branch 25 → 26 taken 1203 times.
✗ Branch 25 → 38 not taken.
2406 return &manifestationList.at(signature);
51 1203 }
52
53 /**
54 * Check if there is a struct in this scope, fulfilling all given requirements and if found, return it.
55 * If more than one struct matches the requirement, an error gets thrown
56 *
57 * @param matchScope Scope to match against
58 * @param qt Struct name requirement
59 * @param reqTemplateTypes Template types to substantiate generic types
60 * @param node Instantiation AST node for printing error messages
61 * @return Matched struct or nullptr
62 */
63 26433 Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes,
64 const ASTNode *node) {
65 // Do cache lookup
66 26433 const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes);
67
3/4
✓ Branch 3 → 4 taken 26433 times.
✗ Branch 3 → 213 not taken.
✓ Branch 4 → 5 taken 25088 times.
✓ Branch 4 → 7 taken 1345 times.
26433 if (lookupCache.contains(cacheKey)) {
68 25088 lookupCacheHits++;
69
1/2
✓ Branch 5 → 6 taken 25088 times.
✗ Branch 5 → 213 not taken.
25088 return lookupCache.at(cacheKey);
70 }
71 1345 lookupCacheMisses++;
72
73 // Copy the registry to prevent iterating over items, that are created within the loop
74
1/2
✓ Branch 7 → 8 taken 1345 times.
✗ Branch 7 → 213 not taken.
1345 StructRegistry structRegistry = matchScope->structs;
75 // Loop over struct registry to find structs, that match the requirements of the instantiation
76 1345 std::vector<Struct *> matches;
77
2/2
✓ Branch 145 → 10 taken 2132 times.
✓ Branch 145 → 146 taken 1345 times.
3477 for (const auto &[structId, m] : structRegistry) {
78 // Copy the manifestation list to prevent iterating over items, that are created within the loop
79
1/2
✓ Branch 13 → 14 taken 2132 times.
✗ Branch 13 → 198 not taken.
2132 const StructManifestationList manifestations = m;
80
2/2
✓ Branch 141 → 16 taken 2867 times.
✓ Branch 141 → 142 taken 939 times.
3806 for (const auto &[mangledName, presetStruct] : manifestations) {
81 // Skip generic substantiations to prevent double matching of a struct
82
3/4
✓ Branch 19 → 20 taken 2867 times.
✗ Branch 19 → 194 not taken.
✓ Branch 20 → 21 taken 735 times.
✓ Branch 20 → 22 taken 2132 times.
2867 if (presetStruct.isGenericSubstantiation())
83 1202 continue;
84
85 // Copy the struct to be able to substantiate types
86
1/2
✓ Branch 22 → 23 taken 2132 times.
✗ Branch 22 → 194 not taken.
2132 Struct candidate = presetStruct;
87
88 // Check name requirement
89
2/2
✓ Branch 24 → 25 taken 839 times.
✓ Branch 24 → 26 taken 1293 times.
2132 if (!matchName(candidate, qt))
90 839 break; // Leave the whole manifestation list, because all manifestations in this list have the same name
91
92 // Prepare mapping table from generic type name to concrete type
93 1293 TypeMapping &typeMapping = candidate.typeMapping;
94 1293 typeMapping.clear();
95
1/2
✓ Branch 28 → 29 taken 1293 times.
✗ Branch 28 → 192 not taken.
1293 typeMapping.reserve(candidate.templateTypes.size());
96
97 // Check template types requirement
98
3/4
✓ Branch 29 → 30 taken 1293 times.
✗ Branch 29 → 192 not taken.
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 1292 times.
1293 if (!matchTemplateTypes(candidate, reqTemplateTypes, typeMapping, node))
99 1 continue; // Leave this manifestation and continue with the next one
100
101 // Map field types from generic to concrete
102
1/2
✓ Branch 32 → 33 taken 1292 times.
✗ Branch 32 → 192 not taken.
1292 substantiateFieldTypes(candidate, typeMapping, node);
103
104 // We found a match! -> Set the actual candidate and its entry to used
105 1292 candidate.used = true;
106 1292 candidate.entry->used = true;
107
108 // Check if it needs to be substantiated
109
2/2
✓ Branch 34 → 35 taken 466 times.
✓ Branch 34 → 47 taken 826 times.
1292 if (presetStruct.templateTypes.empty()) {
110
5/10
✓ Branch 35 → 36 taken 466 times.
✗ Branch 35 → 192 not taken.
✓ Branch 36 → 37 taken 466 times.
✗ Branch 36 → 41 not taken.
✓ Branch 37 → 38 taken 466 times.
✗ Branch 37 → 192 not taken.
✓ Branch 38 → 39 taken 466 times.
✗ Branch 38 → 192 not taken.
✓ Branch 39 → 40 taken 466 times.
✗ Branch 39 → 41 not taken.
466 assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName));
111
3/6
✓ Branch 42 → 43 taken 466 times.
✗ Branch 42 → 167 not taken.
✓ Branch 43 → 44 taken 466 times.
✗ Branch 43 → 167 not taken.
✓ Branch 44 → 45 taken 466 times.
✗ Branch 44 → 167 not taken.
466 matches.push_back(&matchScope->structs.at(structId).at(mangledName));
112 466 matches.back()->used = true;
113 466 continue; // Match was successful -> match the next struct
114 }
115
116 // Check if we already have this manifestation and can simply re-use it
117
4/6
✓ Branch 47 → 48 taken 826 times.
✗ Branch 47 → 170 not taken.
✓ Branch 48 → 49 taken 826 times.
✗ Branch 48 → 168 not taken.
✓ Branch 50 → 51 taken 354 times.
✓ Branch 50 → 58 taken 472 times.
826 if (manifestations.contains(candidate.getSignature())) {
118
4/8
✓ Branch 51 → 52 taken 354 times.
✗ Branch 51 → 174 not taken.
✓ Branch 52 → 53 taken 354 times.
✗ Branch 52 → 173 not taken.
✓ Branch 53 → 54 taken 354 times.
✗ Branch 53 → 171 not taken.
✓ Branch 54 → 55 taken 354 times.
✗ Branch 54 → 171 not taken.
354 matches.push_back(&matchScope->structs.at(structId).at(candidate.getSignature()));
119 354 matches.back()->used = true;
120 354 break; // Leave the whole manifestation list to not double-match the manifestation
121 }
122
123 // Insert the substantiated version if required
124
1/2
✓ Branch 58 → 59 taken 472 times.
✗ Branch 58 → 192 not taken.
472 Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode);
125
2/4
✓ Branch 59 → 60 taken 472 times.
✗ Branch 59 → 192 not taken.
✓ Branch 60 → 61 taken 472 times.
✗ Branch 60 → 192 not taken.
472 substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName);
126
2/4
✓ Branch 61 → 62 taken 472 times.
✗ Branch 61 → 192 not taken.
✓ Branch 62 → 63 taken 472 times.
✗ Branch 62 → 192 not taken.
472 substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct);
127
128 // Copy struct entry
129
1/2
✓ Branch 63 → 64 taken 472 times.
✗ Branch 63 → 192 not taken.
472 const std::string newSignature = substantiatedStruct->getSignature();
130
1/2
✓ Branch 64 → 65 taken 472 times.
✗ Branch 64 → 190 not taken.
472 matchScope->lookupStrict(substantiatedStruct->name)->used = true;
131
1/2
✓ Branch 67 → 68 taken 472 times.
✗ Branch 67 → 190 not taken.
472 substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature);
132
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 472 times.
472 assert(substantiatedStruct->entry != nullptr);
133
134 // Copy struct scope
135
1/2
✓ Branch 70 → 71 taken 472 times.
✗ Branch 70 → 190 not taken.
472 const std::string &oldScopeName = presetStruct.getScopeName();
136
1/2
✓ Branch 71 → 72 taken 472 times.
✗ Branch 71 → 188 not taken.
472 const std::string &newScopeName = substantiatedStruct->getScopeName();
137
1/2
✓ Branch 72 → 73 taken 472 times.
✗ Branch 72 → 186 not taken.
472 substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName);
138
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 472 times.
472 assert(substantiatedStruct->scope != nullptr);
139 472 substantiatedStruct->scope->isGenericScope = false;
140
141 // Attach the template types to the new struct entry
142
1/2
✓ Branch 75 → 76 taken 472 times.
✗ Branch 75 → 178 not taken.
472 QualType entryType = substantiatedStruct->entry->getQualType()
143
2/4
✓ Branch 76 → 77 taken 472 times.
✗ Branch 76 → 177 not taken.
✓ Branch 77 → 78 taken 472 times.
✗ Branch 77 → 175 not taken.
944 .getWithTemplateTypes(substantiatedStruct->getTemplateTypes())
144
1/2
✓ Branch 78 → 79 taken 472 times.
✗ Branch 78 → 175 not taken.
472 .getWithBodyScope(substantiatedStruct->scope);
145
1/2
✓ Branch 80 → 81 taken 472 times.
✗ Branch 80 → 186 not taken.
472 substantiatedStruct->entry->updateType(entryType, true);
146
147 // Replace symbol types of field entries with concrete types
148
1/2
✗ Branch 81 → 82 not taken.
✓ Branch 81 → 83 taken 472 times.
472 assert(substantiatedStruct->scope != nullptr);
149 472 const size_t fieldCount = substantiatedStruct->fieldTypes.size();
150
1/2
✓ Branch 84 → 85 taken 472 times.
✗ Branch 84 → 186 not taken.
472 const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount;
151
2/2
✓ Branch 109 → 86 taken 1095 times.
✓ Branch 109 → 110 taken 472 times.
1567 for (size_t i = 0; i < fieldCount; i++) {
152 // Replace field type with concrete template type
153
1/2
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 1095 times.
1095 SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i);
154
3/6
✓ Branch 91 → 92 taken 1095 times.
✗ Branch 91 → 95 not taken.
✓ Branch 92 → 93 taken 1095 times.
✗ Branch 92 → 181 not taken.
✓ Branch 93 → 94 taken 1095 times.
✗ Branch 93 → 95 not taken.
1095 assert(fieldEntry != nullptr && fieldEntry->isField());
155
1/2
✓ Branch 96 → 97 taken 1095 times.
✗ Branch 96 → 181 not taken.
1095 QualType &fieldType = substantiatedStruct->fieldTypes.at(i);
156
1/2
✓ Branch 97 → 98 taken 1095 times.
✗ Branch 97 → 181 not taken.
1095 QualType baseType = fieldType.getBase();
157
158 // Set the body scope of fields that are of type <candidate-struct>*
159
4/6
✓ Branch 98 → 99 taken 1095 times.
✗ Branch 98 → 181 not taken.
✓ Branch 99 → 100 taken 1095 times.
✗ Branch 99 → 181 not taken.
✓ Branch 100 → 101 taken 33 times.
✓ Branch 100 → 104 taken 1062 times.
1095 if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true))
160
2/4
✓ Branch 101 → 102 taken 33 times.
✗ Branch 101 → 179 not taken.
✓ Branch 102 → 103 taken 33 times.
✗ Branch 102 → 179 not taken.
33 fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope));
161
162
1/2
✓ Branch 104 → 105 taken 1095 times.
✗ Branch 104 → 181 not taken.
1095 fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true);
163
164 // Instantiate structs
165
3/4
✓ Branch 105 → 106 taken 1095 times.
✗ Branch 105 → 181 not taken.
✓ Branch 106 → 107 taken 422 times.
✓ Branch 106 → 108 taken 673 times.
1095 if (baseType.is(TY_STRUCT))
166
1/2
✓ Branch 107 → 108 taken 422 times.
✗ Branch 107 → 181 not taken.
422 (void)baseType.getStruct(node);
167 }
168
169 // Instantiate implemented interfaces if required
170
2/2
✓ Branch 127 → 112 taken 166 times.
✓ Branch 127 → 128 taken 472 times.
638 for (QualType &interfaceType : substantiatedStruct->interfaceTypes) {
171 // Skip non-generic interfaces
172
2/4
✓ Branch 113 → 114 taken 166 times.
✗ Branch 113 → 184 not taken.
✗ Branch 114 → 115 not taken.
✓ Branch 114 → 116 taken 166 times.
166 if (!interfaceType.hasAnyGenericParts())
173 continue;
174
175 // Build template types
176
2/4
✓ Branch 116 → 117 taken 166 times.
✗ Branch 116 → 184 not taken.
✓ Branch 117 → 118 taken 166 times.
✗ Branch 117 → 184 not taken.
166 QualTypeList templateTypes = interfaceType.getTemplateTypes();
177
1/2
✓ Branch 118 → 119 taken 166 times.
✗ Branch 118 → 182 not taken.
166 TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node);
178
179 // Instantiate interface
180
1/2
✓ Branch 119 → 120 taken 166 times.
✗ Branch 119 → 182 not taken.
166 Interface *spiceInterface = interfaceType.getInterface(node, templateTypes);
181
1/2
✗ Branch 120 → 121 not taken.
✓ Branch 120 → 122 taken 166 times.
166 assert(spiceInterface != nullptr);
182
1/2
✓ Branch 122 → 123 taken 166 times.
✗ Branch 122 → 182 not taken.
166 interfaceType = spiceInterface->entry->getQualType();
183 166 }
184
185 // Add to matched structs
186
1/2
✓ Branch 128 → 129 taken 472 times.
✗ Branch 128 → 186 not taken.
472 matches.push_back(substantiatedStruct);
187
3/3
✓ Branch 134 → 135 taken 472 times.
✓ Branch 134 → 137 taken 467 times.
✓ Branch 134 → 138 taken 1193 times.
2132 }
188 2132 }
189
190 // If no matches were found, return a nullptr
191
2/2
✓ Branch 147 → 148 taken 53 times.
✓ Branch 147 → 149 taken 1292 times.
1345 if (matches.empty())
192 53 return nullptr;
193
194 // Check if more than one struct matches the requirements
195
1/2
✗ Branch 150 → 151 not taken.
✓ Branch 150 → 159 taken 1292 times.
1292 if (matches.size() > 1)
196 throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature");
197
198 // Insert into cache
199
1/2
✓ Branch 160 → 161 taken 1292 times.
✗ Branch 160 → 209 not taken.
1292 lookupCache[cacheKey] = matches.front();
200
201 1292 return matches.front();
202 1345 }
203
204 /**
205 * Checks if the matching candidate fulfills the name requirement
206 *
207 * @param candidate Matching candidate struct
208 * @param reqName Requested struct name
209 * @return Fulfilled or not
210 */
211 2132 bool StructManager::matchName(const Struct &candidate, const std::string &reqName) { return candidate.name == reqName; }
212
213 /**
214 * Checks if the matching candidate fulfills the template types requirement
215 *
216 * @param candidate Matching candidate struct
217 * @param reqTemplateTypes Requested struct template types
218 * @param typeMapping Generic type mapping
219 * @param node Instantiation AST node for printing error messages
220 * @return Fulfilled or not
221 */
222 1293 bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping,
223 const ASTNode *node) {
224 // Check if the number of types match
225 1293 const size_t typeCount = reqTemplateTypes.size();
226
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1292 times.
1293 if (typeCount != candidate.templateTypes.size())
227 1 return false;
228
229 // Give the type matcher a way to retrieve instances of GenericType by their name
230 3787 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
231 1203 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
232 1292 };
233
234 // Loop over all template types
235
2/2
✓ Branch 17 → 8 taken 1203 times.
✓ Branch 17 → 18 taken 1292 times.
2495 for (size_t i = 0; i < typeCount; i++) {
236
1/2
✓ Branch 8 → 9 taken 1203 times.
✗ Branch 8 → 22 not taken.
1203 const QualType &reqType = reqTemplateTypes.at(i);
237
1/2
✓ Branch 9 → 10 taken 1203 times.
✗ Branch 9 → 22 not taken.
1203 QualType &candidateType = candidate.templateTypes.at(i);
238
239 // Check if the requested template type matches the candidate template type. The type mapping may be extended
240
2/4
✓ Branch 10 → 11 taken 1203 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1203 times.
1203 if (!TypeMatcher::matchRequestedToCandidateType(candidateType, reqType, typeMapping, genericTypeResolver, false))
241 return false;
242
243 // Substantiate the candidate param type, based on the type mapping
244
2/4
✓ Branch 13 → 14 taken 1203 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 1203 times.
✗ Branch 14 → 16 not taken.
1203 if (candidateType.hasAnyGenericParts())
245
1/2
✓ Branch 15 → 16 taken 1203 times.
✗ Branch 15 → 22 not taken.
1203 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
246 }
247
248 1292 return true;
249 1292 }
250
251 /**
252 * Come up with the concrete field types, by applying the type mapping onto the generic field types
253 *
254 * @param candidate Candidate struct
255 * @param typeMapping Generic type mapping
256 * @param node Instantiation AST node for printing error messages
257 */
258 1292 void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) {
259 // Loop over all implicit fields and substantiate the generic ones
260 1292 const size_t fieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size();
261
2/2
✓ Branch 16 → 5 taken 334 times.
✓ Branch 16 → 17 taken 1292 times.
1626 for (size_t i = 0; i < fieldCount; i++) {
262
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 334 times.
334 SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i);
263
1/2
✓ Branch 10 → 11 taken 334 times.
✗ Branch 10 → 27 not taken.
334 QualType fieldType = fieldEntry->getQualType();
264
3/4
✓ Branch 11 → 12 taken 334 times.
✗ Branch 11 → 27 not taken.
✓ Branch 12 → 13 taken 272 times.
✓ Branch 12 → 15 taken 62 times.
334 if (fieldType.hasAnyGenericParts()) {
265
1/2
✓ Branch 13 → 14 taken 272 times.
✗ Branch 13 → 27 not taken.
272 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
266
1/2
✓ Branch 14 → 15 taken 272 times.
✗ Branch 14 → 27 not taken.
272 fieldEntry->updateType(fieldType, true);
267 }
268 }
269
270 // Loop over all explicit field types and substantiate the generic ones
271
2/2
✓ Branch 25 → 19 taken 2895 times.
✓ Branch 25 → 26 taken 1292 times.
4187 for (QualType &fieldType : candidate.fieldTypes)
272
3/4
✓ Branch 20 → 21 taken 2895 times.
✗ Branch 20 → 28 not taken.
✓ Branch 21 → 22 taken 1327 times.
✓ Branch 21 → 23 taken 1568 times.
2895 if (fieldType.hasAnyGenericParts())
273
1/2
✓ Branch 22 → 23 taken 1327 times.
✗ Branch 22 → 28 not taken.
1327 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
274 1292 }
275
276 /**
277 * Searches the candidate template types for a generic type object with a certain name and return it
278 *
279 * @param candidate Matching candidate struct
280 * @param templateTypeName Template type name
281 * @return Generic type object
282 */
283 1203 const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) {
284
1/2
✓ Branch 14 → 4 taken 1582 times.
✗ Branch 14 → 15 not taken.
1582 for (const GenericType &templateType : candidate.templateTypes) {
285
3/4
✓ Branch 5 → 6 taken 1582 times.
✗ Branch 5 → 17 not taken.
✓ Branch 6 → 7 taken 235 times.
✓ Branch 6 → 8 taken 1347 times.
1582 if (!templateType.is(TY_GENERIC))
286 235 continue;
287
3/4
✓ Branch 8 → 9 taken 1347 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 1203 times.
✓ Branch 10 → 12 taken 144 times.
1347 if (templateType.getSubType() == templateTypeName)
288 1203 return &templateType;
289 }
290 return nullptr;
291 }
292
293 /**
294 * Calculate the cache key for the struct lookup cache
295 *
296 * @param scope Scope to match against
297 * @param name Struct name requirement
298 * @param templateTypes Template types to substantiate generic types
299 * @return Cache key
300 */
301 26433 uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
302 26433 uint64_t hash = 0;
303 26433 hashCombine64(hash, hashPointer(scope));
304 26433 hashCombine64(hash, std::hash<std::string>{}(name));
305 26433 hashCombine64(hash, hashVector(templateTypes));
306 26433 return hash;
307 }
308
309 /**
310 * Clear the lookup cache
311 */
312 449 void StructManager::cleanup() {
313 449 lookupCache.clear();
314 449 lookupCacheHits = 0;
315 449 lookupCacheMisses = 0;
316 449 }
317
318 /**
319 * Dump usage statistics for the lookup cache
320 */
321 206 std::string StructManager::dumpLookupCacheStatistics() {
322
1/2
✓ Branch 2 → 3 taken 206 times.
✗ Branch 2 → 22 not taken.
206 std::stringstream stats;
323
2/4
✓ Branch 3 → 4 taken 206 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 206 times.
✗ Branch 4 → 20 not taken.
206 stats << "StructManager lookup cache statistics:" << std::endl;
324
3/6
✓ Branch 5 → 6 taken 206 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 206 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 206 times.
✗ Branch 8 → 20 not taken.
206 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
325
3/6
✓ Branch 9 → 10 taken 206 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 206 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 206 times.
✗ Branch 11 → 20 not taken.
206 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
326
3/6
✓ Branch 12 → 13 taken 206 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 206 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 206 times.
✗ Branch 14 → 20 not taken.
206 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
327
1/2
✓ Branch 15 → 16 taken 206 times.
✗ Branch 15 → 20 not taken.
412 return stats.str();
328 206 }
329
330 } // namespace spice::compiler
331