GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.4% 152 / 0 / 156
Functions: 100.0% 11 / 0 / 11
Branches: 58.1% 161 / 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 747 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 747 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 747 times.
✗ Branch 3 → 17 not taken.
✓ Branch 4 → 5 taken 747 times.
✗ Branch 4 → 15 not taken.
747 const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn();
24
1/2
✓ Branch 8 → 9 taken 747 times.
✗ Branch 8 → 21 not taken.
747 insertScope->structs.emplace(structId, StructManifestationList());
25
26 // Save substantiation in declaration node
27
1/2
✓ Branch 10 → 11 taken 747 times.
✗ Branch 10 → 24 not taken.
747 Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode);
28
1/2
✓ Branch 11 → 12 taken 747 times.
✗ Branch 11 → 24 not taken.
747 nodeStructList->push_back(substantiation);
29
30 747 return substantiation;
31 747 }
32
33 1220 Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) {
34
1/2
✓ Branch 2 → 3 taken 1220 times.
✗ Branch 2 → 42 not taken.
1220 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 1220 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 1220 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 1220 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 1911 times.
✓ Branch 13 → 14 taken 1220 times.
3131 for (const auto &val : insertScope->structs | std::views::values)
39
2/4
✓ Branch 8 → 9 taken 1911 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1911 times.
1911 assert(!val.contains(signature));
40 #endif
41
42 // Retrieve the matching manifestation list of the scope
43
3/6
✓ Branch 14 → 15 taken 1220 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 1220 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 1220 times.
✗ Branch 16 → 32 not taken.
1220 const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
44
2/4
✓ Branch 19 → 20 taken 1220 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1220 times.
1220 assert(insertScope->structs.contains(structId));
45
1/2
✓ Branch 22 → 23 taken 1220 times.
✗ Branch 22 → 38 not taken.
1220 StructManifestationList &manifestationList = insertScope->structs.at(structId);
46
47 // Add substantiated struct
48 1220 newManifestation.manifestationIndex = manifestationList.size();
49
1/2
✓ Branch 24 → 25 taken 1220 times.
✗ Branch 24 → 38 not taken.
1220 manifestationList.emplace(signature, newManifestation);
50
1/2
✓ Branch 25 → 26 taken 1220 times.
✗ Branch 25 → 38 not taken.
2440 return &manifestationList.at(signature);
51 1220 }
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 26897 Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes,
64 const ASTNode *node) {
65 // Do cache lookup
66 26897 const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes);
67
3/4
✓ Branch 3 → 4 taken 26897 times.
✗ Branch 3 → 168 not taken.
✓ Branch 6 → 7 taken 25535 times.
✓ Branch 6 → 9 taken 1362 times.
26897 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
68 25535 lookupCacheHits++;
69 25535 return it->second;
70 }
71 1362 lookupCacheMisses++;
72
73 // Loop over struct registry to find structs, that match the requirements of the instantiation
74 1362 std::vector<Struct *> matches;
75
2/2
✓ Branch 148 → 12 taken 2194 times.
✓ Branch 148 → 149 taken 1362 times.
3556 for (auto &[structId, manifestations] : matchScope->structs) {
76
2/2
✓ Branch 145 → 17 taken 3304 times.
✓ Branch 145 → 146 taken 959 times.
4263 for (const auto &[mangledName, presetStruct] : manifestations) {
77 // Skip generic and newly inserted substantiations to prevent double matching of a struct
78
6/8
✓ Branch 20 → 21 taken 3304 times.
✗ Branch 20 → 194 not taken.
✓ Branch 21 → 22 taken 2194 times.
✓ Branch 21 → 23 taken 1110 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 2194 times.
✓ Branch 25 → 26 taken 1110 times.
✓ Branch 25 → 27 taken 2194 times.
3304 if (presetStruct.isGenericSubstantiation() || presetStruct.isNewlyInserted)
79 1596 continue;
80
81 // Copy the struct to be able to substantiate types
82
1/2
✓ Branch 27 → 28 taken 2194 times.
✗ Branch 27 → 194 not taken.
2194 Struct candidate = presetStruct;
83
84 // Check name requirement
85
2/2
✓ Branch 29 → 30 taken 884 times.
✓ Branch 29 → 31 taken 1310 times.
2194 if (!matchName(candidate, qt))
86 884 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 1310 TypeMapping &typeMapping = candidate.typeMapping;
90 1310 typeMapping.clear();
91
1/2
✓ Branch 33 → 34 taken 1310 times.
✗ Branch 33 → 192 not taken.
1310 typeMapping.reserve(candidate.templateTypes.size());
92
93 // Check template types requirement
94
3/4
✓ Branch 34 → 35 taken 1310 times.
✗ Branch 34 → 192 not taken.
✓ Branch 35 → 36 taken 1 time.
✓ Branch 35 → 37 taken 1309 times.
1310 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 37 → 38 taken 1309 times.
✗ Branch 37 → 192 not taken.
1309 substantiateFieldTypes(candidate, typeMapping, node);
99
100 // We found a match! -> Set the actual candidate and its entry to used
101 1309 candidate.used = true;
102 1309 candidate.entry->used = true;
103
104 // Check if it needs to be substantiated
105
2/2
✓ Branch 39 → 40 taken 485 times.
✓ Branch 39 → 52 taken 824 times.
1309 if (presetStruct.templateTypes.empty()) {
106
5/10
✓ Branch 40 → 41 taken 485 times.
✗ Branch 40 → 169 not taken.
✓ Branch 41 → 42 taken 485 times.
✗ Branch 41 → 46 not taken.
✓ Branch 42 → 43 taken 485 times.
✗ Branch 42 → 169 not taken.
✓ Branch 43 → 44 taken 485 times.
✗ Branch 43 → 169 not taken.
✓ Branch 44 → 45 taken 485 times.
✗ Branch 44 → 46 not taken.
485 assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName));
107
2/4
✓ Branch 47 → 48 taken 485 times.
✗ Branch 47 → 169 not taken.
✓ Branch 48 → 49 taken 485 times.
✗ Branch 48 → 169 not taken.
485 Struct *match = &matchScope->structs.at(structId).at(mangledName);
108 485 match->used = true;
109
1/2
✓ Branch 49 → 50 taken 485 times.
✗ Branch 49 → 169 not taken.
485 matches.push_back(match);
110 485 continue; // Match was successful -> match the next struct
111 485 }
112
113 // Check if we already have this manifestation and can simply re-use it
114
4/6
✓ Branch 52 → 53 taken 824 times.
✗ Branch 52 → 172 not taken.
✓ Branch 53 → 54 taken 824 times.
✗ Branch 53 → 170 not taken.
✓ Branch 57 → 58 taken 351 times.
✓ Branch 57 → 62 taken 473 times.
824 if (const auto it = manifestations.find(candidate.getSignature()); it != manifestations.end()) {
115 351 it->second.used = true;
116
1/2
✓ Branch 60 → 61 taken 351 times.
✗ Branch 60 → 173 not taken.
351 matches.push_back(&it->second);
117 351 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 62 → 63 taken 473 times.
✗ Branch 62 → 192 not taken.
473 Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode);
122
2/4
✓ Branch 63 → 64 taken 473 times.
✗ Branch 63 → 192 not taken.
✓ Branch 64 → 65 taken 473 times.
✗ Branch 64 → 192 not taken.
473 substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName);
123
2/4
✓ Branch 65 → 66 taken 473 times.
✗ Branch 65 → 192 not taken.
✓ Branch 66 → 67 taken 473 times.
✗ Branch 66 → 192 not taken.
473 substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct);
124 473 substantiatedStruct->isNewlyInserted = true; // To not iterate over it in the same matching
125
126 // Copy struct entry
127
1/2
✓ Branch 67 → 68 taken 473 times.
✗ Branch 67 → 192 not taken.
473 const std::string newSignature = substantiatedStruct->getSignature();
128
1/2
✓ Branch 68 → 69 taken 473 times.
✗ Branch 68 → 190 not taken.
473 matchScope->lookupStrict(substantiatedStruct->name)->used = true;
129
1/2
✓ Branch 71 → 72 taken 473 times.
✗ Branch 71 → 190 not taken.
473 substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature);
130
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 74 taken 473 times.
473 assert(substantiatedStruct->entry != nullptr);
131
132 // Copy struct scope
133
1/2
✓ Branch 74 → 75 taken 473 times.
✗ Branch 74 → 190 not taken.
473 const std::string &oldScopeName = presetStruct.getScopeName();
134
1/2
✓ Branch 75 → 76 taken 473 times.
✗ Branch 75 → 188 not taken.
473 const std::string &newScopeName = substantiatedStruct->getScopeName();
135
1/2
✓ Branch 76 → 77 taken 473 times.
✗ Branch 76 → 186 not taken.
473 substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName);
136
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 473 times.
473 assert(substantiatedStruct->scope != nullptr);
137 473 substantiatedStruct->scope->isGenericScope = false;
138
139 // Attach the template types to the new struct entry
140
1/2
✓ Branch 79 → 80 taken 473 times.
✗ Branch 79 → 178 not taken.
473 QualType entryType = substantiatedStruct->entry->getQualType()
141
2/4
✓ Branch 80 → 81 taken 473 times.
✗ Branch 80 → 177 not taken.
✓ Branch 81 → 82 taken 473 times.
✗ Branch 81 → 175 not taken.
946 .getWithTemplateTypes(substantiatedStruct->getTemplateTypes())
142
1/2
✓ Branch 82 → 83 taken 473 times.
✗ Branch 82 → 175 not taken.
473 .getWithBodyScope(substantiatedStruct->scope);
143
1/2
✓ Branch 84 → 85 taken 473 times.
✗ Branch 84 → 186 not taken.
473 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 473 times.
473 assert(substantiatedStruct->scope != nullptr);
147 473 const size_t fieldCount = substantiatedStruct->fieldTypes.size();
148
1/2
✓ Branch 88 → 89 taken 473 times.
✗ Branch 88 → 186 not taken.
473 const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount;
149
2/2
✓ Branch 113 → 90 taken 1093 times.
✓ Branch 113 → 114 taken 473 times.
1566 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 1093 times.
1093 SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i);
152
3/6
✓ Branch 95 → 96 taken 1093 times.
✗ Branch 95 → 99 not taken.
✓ Branch 96 → 97 taken 1093 times.
✗ Branch 96 → 181 not taken.
✓ Branch 97 → 98 taken 1093 times.
✗ Branch 97 → 99 not taken.
1093 assert(fieldEntry != nullptr && fieldEntry->isField());
153
1/2
✓ Branch 100 → 101 taken 1093 times.
✗ Branch 100 → 181 not taken.
1093 QualType &fieldType = substantiatedStruct->fieldTypes.at(i);
154
1/2
✓ Branch 101 → 102 taken 1093 times.
✗ Branch 101 → 181 not taken.
1093 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 1093 times.
✗ Branch 102 → 181 not taken.
✓ Branch 103 → 104 taken 1093 times.
✗ Branch 103 → 181 not taken.
✓ Branch 104 → 105 taken 33 times.
✓ Branch 104 → 108 taken 1060 times.
1093 if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true))
158
2/4
✓ Branch 105 → 106 taken 33 times.
✗ Branch 105 → 179 not taken.
✓ Branch 106 → 107 taken 33 times.
✗ Branch 106 → 179 not taken.
33 fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope));
159
160
1/2
✓ Branch 108 → 109 taken 1093 times.
✗ Branch 108 → 181 not taken.
1093 fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true);
161
162 // Instantiate structs
163
3/4
✓ Branch 109 → 110 taken 1093 times.
✗ Branch 109 → 181 not taken.
✓ Branch 110 → 111 taken 427 times.
✓ Branch 110 → 112 taken 666 times.
1093 if (baseType.is(TY_STRUCT))
164
1/2
✓ Branch 111 → 112 taken 427 times.
✗ Branch 111 → 181 not taken.
427 (void)baseType.getStruct(node);
165 }
166
167 // Instantiate implemented interfaces if required
168
2/2
✓ Branch 131 → 116 taken 164 times.
✓ Branch 131 → 132 taken 473 times.
637 for (QualType &interfaceType : substantiatedStruct->interfaceTypes) {
169 // Skip non-generic interfaces
170
2/4
✓ Branch 117 → 118 taken 164 times.
✗ Branch 117 → 184 not taken.
✗ Branch 118 → 119 not taken.
✓ Branch 118 → 120 taken 164 times.
164 if (!interfaceType.hasAnyGenericParts())
171 continue;
172
173 // Build template types
174
2/4
✓ Branch 120 → 121 taken 164 times.
✗ Branch 120 → 184 not taken.
✓ Branch 121 → 122 taken 164 times.
✗ Branch 121 → 184 not taken.
164 QualTypeList templateTypes = interfaceType.getTemplateTypes();
175
1/2
✓ Branch 122 → 123 taken 164 times.
✗ Branch 122 → 182 not taken.
164 TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node);
176
177 // Instantiate interface
178
1/2
✓ Branch 123 → 124 taken 164 times.
✗ Branch 123 → 182 not taken.
164 Interface *spiceInterface = interfaceType.getInterface(node, templateTypes);
179
1/2
✗ Branch 124 → 125 not taken.
✓ Branch 124 → 126 taken 164 times.
164 assert(spiceInterface != nullptr);
180
1/2
✓ Branch 126 → 127 taken 164 times.
✗ Branch 126 → 182 not taken.
164 interfaceType = spiceInterface->entry->getQualType();
181 164 }
182
183 // Add to matched structs
184
1/2
✓ Branch 132 → 133 taken 473 times.
✗ Branch 132 → 186 not taken.
473 matches.push_back(substantiatedStruct);
185
3/3
✓ Branch 138 → 139 taken 473 times.
✓ Branch 138 → 141 taken 486 times.
✓ Branch 138 → 142 taken 1235 times.
2194 }
186 }
187
188 // If no matches were found, return a nullptr
189
2/2
✓ Branch 150 → 151 taken 53 times.
✓ Branch 150 → 152 taken 1309 times.
1362 if (matches.empty())
190 53 return nullptr;
191
192 // Check if more than one struct matches the requirements
193
1/2
✗ Branch 153 → 154 not taken.
✓ Branch 153 → 162 taken 1309 times.
1309 if (matches.size() > 1)
194 throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature");
195 1309 Struct *matchedStruct = matches.front();
196 1309 matchedStruct->isNewlyInserted = false;
197
198 // Insert into cache
199
1/2
✓ Branch 163 → 164 taken 1309 times.
✗ Branch 163 → 206 not taken.
1309 lookupCache[cacheKey] = matchedStruct;
200
201 1309 return matchedStruct;
202 1362 }
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 2194 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 1310 bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping,
223 const ASTNode *node) {
224 // Check if the number of types match
225 1310 const size_t typeCount = reqTemplateTypes.size();
226
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1309 times.
1310 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 3816 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
231 1198 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
232 1309 };
233
234 // Loop over all template types
235
2/2
✓ Branch 17 → 8 taken 1198 times.
✓ Branch 17 → 18 taken 1309 times.
2507 for (size_t i = 0; i < typeCount; i++) {
236
1/2
✓ Branch 8 → 9 taken 1198 times.
✗ Branch 8 → 22 not taken.
1198 const QualType &reqType = reqTemplateTypes.at(i);
237
1/2
✓ Branch 9 → 10 taken 1198 times.
✗ Branch 9 → 22 not taken.
1198 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 1198 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1198 times.
1198 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 1198 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 1198 times.
✗ Branch 14 → 16 not taken.
1198 if (candidateType.hasAnyGenericParts())
245
1/2
✓ Branch 15 → 16 taken 1198 times.
✗ Branch 15 → 22 not taken.
1198 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
246 }
247
248 1309 return true;
249 1309 }
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 1309 void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) {
259 // Loop over all implicit fields and substantiate the generic ones
260 1309 const size_t fieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size();
261
2/2
✓ Branch 16 → 5 taken 331 times.
✓ Branch 16 → 17 taken 1309 times.
1640 for (size_t i = 0; i < fieldCount; i++) {
262
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 331 times.
331 SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i);
263
1/2
✓ Branch 10 → 11 taken 331 times.
✗ Branch 10 → 27 not taken.
331 QualType fieldType = fieldEntry->getQualType();
264
3/4
✓ Branch 11 → 12 taken 331 times.
✗ Branch 11 → 27 not taken.
✓ Branch 12 → 13 taken 268 times.
✓ Branch 12 → 15 taken 63 times.
331 if (fieldType.hasAnyGenericParts()) {
265
1/2
✓ Branch 13 → 14 taken 268 times.
✗ Branch 13 → 27 not taken.
268 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
266
1/2
✓ Branch 14 → 15 taken 268 times.
✗ Branch 14 → 27 not taken.
268 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 2905 times.
✓ Branch 25 → 26 taken 1309 times.
4214 for (QualType &fieldType : candidate.fieldTypes)
272
3/4
✓ Branch 20 → 21 taken 2905 times.
✗ Branch 20 → 28 not taken.
✓ Branch 21 → 22 taken 1322 times.
✓ Branch 21 → 23 taken 1583 times.
2905 if (fieldType.hasAnyGenericParts())
273
1/2
✓ Branch 22 → 23 taken 1322 times.
✗ Branch 22 → 28 not taken.
1322 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
274 1309 }
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 1198 const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) {
284
1/2
✓ Branch 14 → 4 taken 1574 times.
✗ Branch 14 → 15 not taken.
1574 for (const GenericType &templateType : candidate.templateTypes) {
285
3/4
✓ Branch 5 → 6 taken 1574 times.
✗ Branch 5 → 17 not taken.
✓ Branch 6 → 7 taken 233 times.
✓ Branch 6 → 8 taken 1341 times.
1574 if (!templateType.is(TY_GENERIC))
286 233 continue;
287
3/4
✓ Branch 8 → 9 taken 1341 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 1198 times.
✓ Branch 10 → 12 taken 143 times.
1341 if (templateType.getSubType() == templateTypeName)
288 1198 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 26897 uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
302 26897 uint64_t hash = 0;
303 26897 hashCombine64(hash, hashPointer(scope));
304 26897 hashCombine64(hash, std::hash<std::string>{}(name));
305 26897 hashCombine64(hash, hashVector(templateTypes));
306 26897 return hash;
307 }
308
309 /**
310 * Clear the lookup cache
311 */
312 458 void StructManager::cleanup() {
313 458 lookupCache.clear();
314 458 lookupCacheHits = 0;
315 458 lookupCacheMisses = 0;
316 458 }
317
318 /**
319 * Dump usage statistics for the lookup cache
320 */
321 223 std::string StructManager::dumpLookupCacheStatistics() {
322
1/2
✓ Branch 2 → 3 taken 223 times.
✗ Branch 2 → 22 not taken.
223 std::stringstream stats;
323
2/4
✓ Branch 3 → 4 taken 223 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 223 times.
✗ Branch 4 → 20 not taken.
223 stats << "StructManager lookup cache statistics:" << std::endl;
324
3/6
✓ Branch 5 → 6 taken 223 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 223 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 223 times.
✗ Branch 8 → 20 not taken.
223 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
325
3/6
✓ Branch 9 → 10 taken 223 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 223 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 223 times.
✗ Branch 11 → 20 not taken.
223 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
326
3/6
✓ Branch 12 → 13 taken 223 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 223 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 223 times.
✗ Branch 14 → 20 not taken.
223 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
327
1/2
✓ Branch 15 → 16 taken 223 times.
✗ Branch 15 → 20 not taken.
446 return stats.str();
328 223 }
329
330 } // namespace spice::compiler
331