GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.1% 153 / 0 / 156
Functions: 100.0% 11 / 0 / 11
Branches: 58.5% 162 / 0 / 277

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 842 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 842 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 842 times.
✗ Branch 3 → 17 not taken.
✓ Branch 4 → 5 taken 842 times.
✗ Branch 4 → 15 not taken.
842 const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn();
24
1/2
✓ Branch 8 → 9 taken 842 times.
✗ Branch 8 → 21 not taken.
842 insertScope->structs.emplace(structId, StructManifestationList());
25
26 // Save substantiation in declaration node
27
1/2
✓ Branch 10 → 11 taken 842 times.
✗ Branch 10 → 24 not taken.
842 Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode);
28
1/2
✓ Branch 11 → 12 taken 842 times.
✗ Branch 11 → 24 not taken.
842 nodeStructList->push_back(substantiation);
29
30 842 return substantiation;
31 842 }
32
33 1437 Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) {
34
1/2
✓ Branch 2 → 3 taken 1437 times.
✗ Branch 2 → 42 not taken.
1437 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 1437 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 1437 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 1437 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 2238 times.
✓ Branch 13 → 14 taken 1437 times.
3675 for (const auto &val : insertScope->structs | std::views::values)
39
2/4
✓ Branch 8 → 9 taken 2238 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2238 times.
2238 assert(!val.contains(signature));
40 #endif
41
42 // Retrieve the matching manifestation list of the scope
43
3/6
✓ Branch 14 → 15 taken 1437 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 1437 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 1437 times.
✗ Branch 16 → 32 not taken.
1437 const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
44
2/4
✓ Branch 19 → 20 taken 1437 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1437 times.
1437 assert(insertScope->structs.contains(structId));
45
1/2
✓ Branch 22 → 23 taken 1437 times.
✗ Branch 22 → 38 not taken.
1437 StructManifestationList &manifestationList = insertScope->structs.at(structId);
46
47 // Add substantiated struct
48 1437 newManifestation.manifestationIndex = manifestationList.size();
49
1/2
✓ Branch 24 → 25 taken 1437 times.
✗ Branch 24 → 38 not taken.
1437 manifestationList.emplace(signature, newManifestation);
50
1/2
✓ Branch 25 → 26 taken 1437 times.
✗ Branch 25 → 38 not taken.
2874 return &manifestationList.at(signature);
51 1437 }
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 33168 Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes,
64 const ASTNode *node) {
65 // Do cache lookup
66 33168 const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes);
67
3/4
✓ Branch 3 → 4 taken 33168 times.
✗ Branch 3 → 177 not taken.
✓ Branch 6 → 7 taken 31564 times.
✓ Branch 6 → 9 taken 1604 times.
33168 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
68 31564 lookupCacheHits++;
69 31564 return it->second;
70 }
71 1604 lookupCacheMisses++;
72
73 // Loop over struct registry to find structs, that match the requirements of the instantiation
74 1604 std::vector<Struct *> matches;
75
2/2
✓ Branch 157 → 11 taken 2556 times.
✓ Branch 157 → 158 taken 1604 times.
4160 for (auto &[structId, manifestations] : matchScope->structs) {
76
2/2
✓ Branch 154 → 16 taken 4147 times.
✓ Branch 154 → 155 taken 1134 times.
5281 for (const auto &[mangledName, presetStruct] : manifestations) {
77 // Skip generic and newly inserted substantiations to prevent double matching of a struct
78
6/8
✓ Branch 19 → 20 taken 4147 times.
✗ Branch 19 → 203 not taken.
✓ Branch 20 → 21 taken 2556 times.
✓ Branch 20 → 22 taken 1591 times.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2556 times.
✓ Branch 24 → 25 taken 1591 times.
✓ Branch 24 → 26 taken 2556 times.
4147 if (presetStruct.isGenericSubstantiation() || presetStruct.isNewlyInserted)
79 2130 continue;
80
81 // Copy the struct to be able to substantiate types
82
1/2
✓ Branch 26 → 27 taken 2556 times.
✗ Branch 26 → 203 not taken.
2556 Struct candidate = presetStruct;
83
84 // Check name requirement
85
2/2
✓ Branch 28 → 29 taken 1012 times.
✓ Branch 28 → 30 taken 1544 times.
2556 if (!matchName(candidate, qt))
86 1012 break; // Leave the whole manifestation list, because all manifestations in this list have the same name
87
88 // Prepare mapping table from generic type name to concrete type
89 1544 TypeMapping &typeMapping = candidate.typeMapping;
90 1544 typeMapping.clear();
91
1/2
✓ Branch 32 → 33 taken 1544 times.
✗ Branch 32 → 201 not taken.
1544 typeMapping.reserve(candidate.templateTypes.size());
92
93 // Check template types requirement
94
3/4
✓ Branch 33 → 34 taken 1544 times.
✗ Branch 33 → 201 not taken.
✓ Branch 34 → 35 taken 1 time.
✓ Branch 34 → 36 taken 1543 times.
1544 if (!matchTemplateTypes(candidate, reqTemplateTypes, typeMapping, node))
95 1 continue; // Leave this manifestation and continue with the next one
96
97 // Map field types from generic to concrete
98
1/2
✓ Branch 36 → 37 taken 1543 times.
✗ Branch 36 → 201 not taken.
1543 substantiateFieldTypes(candidate, typeMapping, node);
99
100 // We found a match! -> Set the actual candidate and its entry to used
101 1543 candidate.used = true;
102 1543 candidate.entry->used = true;
103
104 // Check if it needs to be substantiated
105
2/2
✓ Branch 38 → 39 taken 538 times.
✓ Branch 38 → 51 taken 1005 times.
1543 if (presetStruct.templateTypes.empty()) {
106
5/10
✓ Branch 39 → 40 taken 538 times.
✗ Branch 39 → 178 not taken.
✓ Branch 40 → 41 taken 538 times.
✗ Branch 40 → 45 not taken.
✓ Branch 41 → 42 taken 538 times.
✗ Branch 41 → 178 not taken.
✓ Branch 42 → 43 taken 538 times.
✗ Branch 42 → 178 not taken.
✓ Branch 43 → 44 taken 538 times.
✗ Branch 43 → 45 not taken.
538 assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName));
107
2/4
✓ Branch 46 → 47 taken 538 times.
✗ Branch 46 → 178 not taken.
✓ Branch 47 → 48 taken 538 times.
✗ Branch 47 → 178 not taken.
538 Struct *match = &matchScope->structs.at(structId).at(mangledName);
108 538 match->used = true;
109
1/2
✓ Branch 48 → 49 taken 538 times.
✗ Branch 48 → 178 not taken.
538 matches.push_back(match);
110 538 continue; // Match was successful -> match the next struct
111 538 }
112
113 // Check if we already have this manifestation and can simply re-use it
114
4/6
✓ Branch 51 → 52 taken 1005 times.
✗ Branch 51 → 181 not taken.
✓ Branch 52 → 53 taken 1005 times.
✗ Branch 52 → 179 not taken.
✓ Branch 56 → 57 taken 410 times.
✓ Branch 56 → 61 taken 595 times.
1005 if (const auto it = manifestations.find(candidate.getSignature()); it != manifestations.end()) {
115 410 it->second.used = true;
116
1/2
✓ Branch 59 → 60 taken 410 times.
✗ Branch 59 → 182 not taken.
410 matches.push_back(&it->second);
117 410 break; // Leave the whole manifestation list to not double-match the manifestation
118 }
119
120 // Insert the substantiated version if required
121
1/2
✓ Branch 61 → 63 taken 595 times.
✗ Branch 61 → 201 not taken.
595 Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode);
122
2/4
✓ Branch 63 → 64 taken 595 times.
✗ Branch 63 → 201 not taken.
✓ Branch 64 → 65 taken 595 times.
✗ Branch 64 → 201 not taken.
595 substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName);
123
2/4
✓ Branch 65 → 66 taken 595 times.
✗ Branch 65 → 201 not taken.
✓ Branch 66 → 67 taken 595 times.
✗ Branch 66 → 201 not taken.
595 substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct);
124 595 substantiatedStruct->isNewlyInserted = true; // To not iterate over it in the same matching
125
126 // Copy struct entry
127
1/2
✓ Branch 67 → 68 taken 595 times.
✗ Branch 67 → 201 not taken.
595 const std::string newSignature = substantiatedStruct->getSignature();
128
1/2
✓ Branch 68 → 69 taken 595 times.
✗ Branch 68 → 199 not taken.
595 matchScope->lookupStrict(substantiatedStruct->name)->used = true;
129
1/2
✓ Branch 71 → 72 taken 595 times.
✗ Branch 71 → 199 not taken.
595 substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature);
130
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 74 taken 595 times.
595 assert(substantiatedStruct->entry != nullptr);
131
132 // Copy struct scope
133
1/2
✓ Branch 74 → 75 taken 595 times.
✗ Branch 74 → 199 not taken.
595 const std::string &oldScopeName = presetStruct.getScopeName();
134
1/2
✓ Branch 75 → 76 taken 595 times.
✗ Branch 75 → 197 not taken.
595 const std::string &newScopeName = substantiatedStruct->getScopeName();
135
1/2
✓ Branch 76 → 77 taken 595 times.
✗ Branch 76 → 195 not taken.
595 substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName);
136
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 595 times.
595 assert(substantiatedStruct->scope != nullptr);
137 595 substantiatedStruct->scope->isGenericScope = false;
138
139 // Attach the template types to the new struct entry
140
1/2
✓ Branch 79 → 80 taken 595 times.
✗ Branch 79 → 187 not taken.
595 QualType entryType = substantiatedStruct->entry->getQualType()
141
2/4
✓ Branch 80 → 81 taken 595 times.
✗ Branch 80 → 186 not taken.
✓ Branch 81 → 82 taken 595 times.
✗ Branch 81 → 184 not taken.
1190 .getWithTemplateTypes(substantiatedStruct->getTemplateTypes())
142
1/2
✓ Branch 82 → 83 taken 595 times.
✗ Branch 82 → 184 not taken.
595 .getWithBodyScope(substantiatedStruct->scope);
143
1/2
✓ Branch 84 → 85 taken 595 times.
✗ Branch 84 → 195 not taken.
595 substantiatedStruct->entry->updateType(entryType, true);
144
145 // Replace symbol types of field entries with concrete types
146
1/2
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 87 taken 595 times.
595 assert(substantiatedStruct->scope != nullptr);
147 595 const size_t fieldCount = substantiatedStruct->fieldTypes.size();
148
1/2
✓ Branch 88 → 89 taken 595 times.
✗ Branch 88 → 195 not taken.
595 const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount;
149
2/2
✓ Branch 113 → 90 taken 1389 times.
✓ Branch 113 → 114 taken 595 times.
1984 for (size_t i = 0; i < fieldCount; i++) {
150 // Replace field type with concrete template type
151
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 92 taken 1389 times.
1389 SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i);
152
3/6
✓ Branch 95 → 96 taken 1389 times.
✗ Branch 95 → 99 not taken.
✓ Branch 96 → 97 taken 1389 times.
✗ Branch 96 → 190 not taken.
✓ Branch 97 → 98 taken 1389 times.
✗ Branch 97 → 99 not taken.
1389 assert(fieldEntry != nullptr && fieldEntry->isField());
153
1/2
✓ Branch 100 → 101 taken 1389 times.
✗ Branch 100 → 190 not taken.
1389 QualType &fieldType = substantiatedStruct->fieldTypes.at(i);
154
1/2
✓ Branch 101 → 102 taken 1389 times.
✗ Branch 101 → 190 not taken.
1389 QualType baseType = fieldType.getBase();
155
156 // Set the body scope of fields that are of type <candidate-struct>*
157
4/6
✓ Branch 102 → 103 taken 1389 times.
✗ Branch 102 → 190 not taken.
✓ Branch 103 → 104 taken 1389 times.
✗ Branch 103 → 190 not taken.
✓ Branch 104 → 105 taken 35 times.
✓ Branch 104 → 108 taken 1354 times.
1389 if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true))
158
2/4
✓ Branch 105 → 106 taken 35 times.
✗ Branch 105 → 188 not taken.
✓ Branch 106 → 107 taken 35 times.
✗ Branch 106 → 188 not taken.
35 fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope));
159
160
1/2
✓ Branch 108 → 109 taken 1389 times.
✗ Branch 108 → 190 not taken.
1389 fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true);
161
162 // Instantiate structs
163
3/4
✓ Branch 109 → 110 taken 1389 times.
✗ Branch 109 → 190 not taken.
✓ Branch 110 → 111 taken 567 times.
✓ Branch 110 → 112 taken 822 times.
1389 if (baseType.is(TY_STRUCT))
164
1/2
✓ Branch 111 → 112 taken 567 times.
✗ Branch 111 → 190 not taken.
567 (void)baseType.getStruct(node);
165 }
166
167 // Instantiate implemented interfaces if required
168
2/2
✓ Branch 140 → 116 taken 222 times.
✓ Branch 140 → 141 taken 595 times.
1412 for (QualType &interfaceType : substantiatedStruct->interfaceTypes) {
169 // Skip non-generic interfaces
170
3/4
✓ Branch 118 → 119 taken 222 times.
✗ Branch 118 → 193 not taken.
✓ Branch 119 → 120 taken 1 time.
✓ Branch 119 → 121 taken 221 times.
222 if (!interfaceType.hasAnyGenericParts())
171 1 continue;
172
173 // Build template types
174
2/4
✓ Branch 121 → 122 taken 221 times.
✗ Branch 121 → 193 not taken.
✓ Branch 122 → 123 taken 221 times.
✗ Branch 122 → 193 not taken.
221 QualTypeList templateTypes = interfaceType.getTemplateTypes();
175
1/2
✓ Branch 123 → 124 taken 221 times.
✗ Branch 123 → 191 not taken.
221 TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node);
176
177 // Instantiate interface
178
1/2
✓ Branch 124 → 125 taken 221 times.
✗ Branch 124 → 191 not taken.
221 Interface *spiceInterface = interfaceType.getInterface(node, templateTypes);
179
1/2
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 221 times.
221 assert(spiceInterface != nullptr);
180
1/2
✓ Branch 127 → 128 taken 221 times.
✗ Branch 127 → 191 not taken.
221 interfaceType = spiceInterface->entry->getQualType();
181 221 }
182
183 // Add to matched structs
184
1/2
✓ Branch 141 → 142 taken 595 times.
✗ Branch 141 → 195 not taken.
595 matches.push_back(substantiatedStruct);
185
3/3
✓ Branch 147 → 148 taken 595 times.
✓ Branch 147 → 150 taken 539 times.
✓ Branch 147 → 151 taken 1422 times.
2556 }
186 }
187
188 // If no matches were found, return a nullptr
189
2/2
✓ Branch 159 → 160 taken 61 times.
✓ Branch 159 → 161 taken 1543 times.
1604 if (matches.empty())
190 61 return nullptr;
191
192 // Check if more than one struct matches the requirements
193
1/2
✗ Branch 162 → 163 not taken.
✓ Branch 162 → 171 taken 1543 times.
1543 if (matches.size() > 1)
194 throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature");
195 1543 Struct *matchedStruct = matches.front();
196 1543 matchedStruct->isNewlyInserted = false;
197
198 // Insert into cache
199
1/2
✓ Branch 172 → 173 taken 1543 times.
✗ Branch 172 → 215 not taken.
1543 lookupCache[cacheKey] = matchedStruct;
200
201 1543 return matchedStruct;
202 1604 }
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 2556 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 1544 bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping,
223 const ASTNode *node) {
224 // Check if the number of types match
225 1544 const size_t typeCount = reqTemplateTypes.size();
226
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1543 times.
1544 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 4528 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
231 1442 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
232 1543 };
233
234 // Loop over all template types
235
2/2
✓ Branch 17 → 8 taken 1442 times.
✓ Branch 17 → 18 taken 1543 times.
2985 for (size_t i = 0; i < typeCount; i++) {
236
1/2
✓ Branch 8 → 9 taken 1442 times.
✗ Branch 8 → 22 not taken.
1442 const QualType &reqType = reqTemplateTypes.at(i);
237
1/2
✓ Branch 9 → 10 taken 1442 times.
✗ Branch 9 → 22 not taken.
1442 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 1442 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1442 times.
1442 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 1442 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 1442 times.
✗ Branch 14 → 16 not taken.
1442 if (candidateType.hasAnyGenericParts())
245
1/2
✓ Branch 15 → 16 taken 1442 times.
✗ Branch 15 → 22 not taken.
1442 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
246 }
247
248 1543 return true;
249 1543 }
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 1543 void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) {
259 // Loop over all implicit fields and substantiate the generic ones
260 1543 const size_t fieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size();
261
2/2
✓ Branch 16 → 5 taken 417 times.
✓ Branch 16 → 17 taken 1543 times.
1960 for (size_t i = 0; i < fieldCount; i++) {
262
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 417 times.
417 SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i);
263
1/2
✓ Branch 10 → 11 taken 417 times.
✗ Branch 10 → 35 not taken.
417 QualType fieldType = fieldEntry->getQualType();
264
3/4
✓ Branch 11 → 12 taken 417 times.
✗ Branch 11 → 35 not taken.
✓ Branch 12 → 13 taken 316 times.
✓ Branch 12 → 15 taken 101 times.
417 if (fieldType.hasAnyGenericParts()) {
265
1/2
✓ Branch 13 → 14 taken 316 times.
✗ Branch 13 → 35 not taken.
316 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
266
1/2
✓ Branch 14 → 15 taken 316 times.
✗ Branch 14 → 35 not taken.
316 fieldEntry->updateType(fieldType, true);
267 }
268 }
269
270 // Loop over all explicit field types and substantiate the generic ones
271
2/2
✓ Branch 33 → 19 taken 3476 times.
✓ Branch 33 → 34 taken 1543 times.
6562 for (QualType &fieldType : candidate.fieldTypes)
272
3/4
✓ Branch 21 → 22 taken 3476 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 1583 times.
✓ Branch 22 → 24 taken 1893 times.
3476 if (fieldType.hasAnyGenericParts())
273
1/2
✓ Branch 23 → 24 taken 1583 times.
✗ Branch 23 → 36 not taken.
1583 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
274 1543 }
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 1442 const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) {
284
1/2
✓ Branch 22 → 4 taken 1881 times.
✗ Branch 22 → 23 not taken.
3323 for (const GenericType &templateType : candidate.templateTypes) {
285
3/4
✓ Branch 6 → 7 taken 1881 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 278 times.
✓ Branch 7 → 9 taken 1603 times.
1881 if (!templateType.is(TY_GENERIC))
286 278 continue;
287
3/4
✓ Branch 9 → 10 taken 1603 times.
✗ Branch 9 → 25 not taken.
✓ Branch 11 → 12 taken 1442 times.
✓ Branch 11 → 13 taken 161 times.
1603 if (templateType.getSubType() == templateTypeName)
288 1442 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 33168 uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
302 33168 uint64_t hash = 0;
303 33168 hashCombine64(hash, hashPointer(scope));
304 33168 hashCombine64(hash, std::hash<std::string>{}(name));
305 33168 hashCombine64(hash, hashVector(templateTypes));
306 33168 return hash;
307 }
308
309 /**
310 * Clear the lookup cache
311 */
312 472 void StructManager::cleanup() {
313 472 lookupCache.clear();
314 472 lookupCacheHits = 0;
315 472 lookupCacheMisses = 0;
316 472 }
317
318 /**
319 * Dump usage statistics for the lookup cache
320 */
321 235 std::string StructManager::dumpLookupCacheStatistics() {
322
1/2
✓ Branch 2 → 3 taken 235 times.
✗ Branch 2 → 22 not taken.
235 std::stringstream stats;
323
2/4
✓ Branch 3 → 4 taken 235 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 235 times.
✗ Branch 4 → 20 not taken.
235 stats << "StructManager lookup cache statistics:" << std::endl;
324
3/6
✓ Branch 5 → 6 taken 235 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 235 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 235 times.
✗ Branch 8 → 20 not taken.
235 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
325
3/6
✓ Branch 9 → 10 taken 235 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 235 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 235 times.
✗ Branch 11 → 20 not taken.
235 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
326
3/6
✓ Branch 12 → 13 taken 235 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 235 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 235 times.
✗ Branch 14 → 20 not taken.
235 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
327
1/2
✓ Branch 15 → 16 taken 235 times.
✗ Branch 15 → 20 not taken.
470 return stats.str();
328 235 }
329
330 } // namespace spice::compiler
331