GCC Code Coverage Report


Directory: ../
File: src/typechecker/StructManager.cpp
Date: 2025-12-12 07:25:08
Coverage Exec Excl Total
Lines: 97.4% 149 0 153
Functions: 100.0% 11 0 11
Branches: 57.5% 165 0 287

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