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 2804 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 2804 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 2804 times.
✗ Branch 3 → 17 not taken.
✓ Branch 4 → 5 taken 2804 times.
✗ Branch 4 → 15 not taken.
2804 const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn();
24
1/2
✓ Branch 8 → 9 taken 2804 times.
✗ Branch 8 → 21 not taken.
2804 insertScope->structs.emplace(structId, StructManifestationList());
25
26 // Save substantiation in declaration node
27
1/2
✓ Branch 10 → 11 taken 2804 times.
✗ Branch 10 → 24 not taken.
2804 Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode);
28
1/2
✓ Branch 11 → 12 taken 2804 times.
✗ Branch 11 → 24 not taken.
2804 nodeStructList->push_back(substantiation);
29
30 2804 return substantiation;
31 2804 }
32
33 4837 Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) {
34
1/2
✓ Branch 2 → 3 taken 4837 times.
✗ Branch 2 → 42 not taken.
4837 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 4837 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 4837 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 4837 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 52215 times.
✓ Branch 13 → 14 taken 4837 times.
57052 for (const auto &val : insertScope->structs | std::views::values)
39
2/4
✓ Branch 8 → 9 taken 52215 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 52215 times.
52215 assert(!val.contains(signature));
40 #endif
41
42 // Retrieve the matching manifestation list of the scope
43
3/6
✓ Branch 14 → 15 taken 4837 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 4837 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 4837 times.
✗ Branch 16 → 32 not taken.
4837 const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
44
2/4
✓ Branch 19 → 20 taken 4837 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 4837 times.
4837 assert(insertScope->structs.contains(structId));
45
1/2
✓ Branch 22 → 23 taken 4837 times.
✗ Branch 22 → 38 not taken.
4837 StructManifestationList &manifestationList = insertScope->structs.at(structId);
46
47 // Add substantiated struct
48 4837 newManifestation.manifestationIndex = manifestationList.size();
49
1/2
✓ Branch 24 → 25 taken 4837 times.
✗ Branch 24 → 38 not taken.
4837 manifestationList.emplace(signature, newManifestation);
50
1/2
✓ Branch 25 → 26 taken 4837 times.
✗ Branch 25 → 38 not taken.
9674 return &manifestationList.at(signature);
51 4837 }
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 115371 Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes,
64 const ASTNode *node) {
65 // Do cache lookup
66 115371 const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes);
67
3/4
✓ Branch 3 → 4 taken 115371 times.
✗ Branch 3 → 177 not taken.
✓ Branch 6 → 7 taken 110069 times.
✓ Branch 6 → 9 taken 5302 times.
115371 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
68 110069 lookupCacheHits++;
69 110069 return it->second;
70 }
71 5302 lookupCacheMisses++;
72
73 // Loop over struct registry to find structs, that match the requirements of the instantiation
74 5302 std::vector<Struct *> matches;
75
2/2
✓ Branch 157 → 11 taken 93067 times.
✓ Branch 157 → 158 taken 5302 times.
98369 for (auto &[structId, manifestations] : matchScope->structs) {
76
2/2
✓ Branch 154 → 16 taken 111149 times.
✓ Branch 154 → 155 taken 4202 times.
115351 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 111149 times.
✗ Branch 19 → 203 not taken.
✓ Branch 20 → 21 taken 93067 times.
✓ Branch 20 → 22 taken 18082 times.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 93067 times.
✓ Branch 24 → 25 taken 18082 times.
✓ Branch 24 → 26 taken 93067 times.
111149 if (presetStruct.isGenericSubstantiation() || presetStruct.isNewlyInserted)
79 20251 continue;
80
81 // Copy the struct to be able to substantiate types
82
1/2
✓ Branch 26 → 27 taken 93067 times.
✗ Branch 26 → 203 not taken.
93067 Struct candidate = presetStruct;
83
84 // Check name requirement
85
2/2
✓ Branch 28 → 29 taken 87967 times.
✓ Branch 28 → 30 taken 5100 times.
93067 if (!matchName(candidate, qt))
86 87967 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 5100 TypeMapping &typeMapping = candidate.typeMapping;
90 5100 typeMapping.clear();
91
1/2
✓ Branch 32 → 33 taken 5100 times.
✗ Branch 32 → 201 not taken.
5100 typeMapping.reserve(candidate.templateTypes.size());
92
93 // Check template types requirement
94
3/4
✓ Branch 33 → 34 taken 5100 times.
✗ Branch 33 → 201 not taken.
✓ Branch 34 → 35 taken 1 time.
✓ Branch 34 → 36 taken 5099 times.
5100 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 5099 times.
✗ Branch 36 → 201 not taken.
5099 substantiateFieldTypes(candidate, typeMapping, node);
99
100 // We found a match! -> Set the actual candidate and its entry to used
101 5099 candidate.used = true;
102 5099 candidate.entry->used = true;
103
104 // Check if it needs to be substantiated
105
2/2
✓ Branch 38 → 39 taken 2168 times.
✓ Branch 38 → 51 taken 2931 times.
5099 if (presetStruct.templateTypes.empty()) {
106
5/10
✓ Branch 39 → 40 taken 2168 times.
✗ Branch 39 → 178 not taken.
✓ Branch 40 → 41 taken 2168 times.
✗ Branch 40 → 45 not taken.
✓ Branch 41 → 42 taken 2168 times.
✗ Branch 41 → 178 not taken.
✓ Branch 42 → 43 taken 2168 times.
✗ Branch 42 → 178 not taken.
✓ Branch 43 → 44 taken 2168 times.
✗ Branch 43 → 45 not taken.
2168 assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName));
107
2/4
✓ Branch 46 → 47 taken 2168 times.
✗ Branch 46 → 178 not taken.
✓ Branch 47 → 48 taken 2168 times.
✗ Branch 47 → 178 not taken.
2168 Struct *match = &matchScope->structs.at(structId).at(mangledName);
108 2168 match->used = true;
109
1/2
✓ Branch 48 → 49 taken 2168 times.
✗ Branch 48 → 178 not taken.
2168 matches.push_back(match);
110 2168 continue; // Match was successful -> match the next struct
111 2168 }
112
113 // Check if we already have this manifestation and can simply re-use it
114
4/6
✓ Branch 51 → 52 taken 2931 times.
✗ Branch 51 → 181 not taken.
✓ Branch 52 → 53 taken 2931 times.
✗ Branch 52 → 179 not taken.
✓ Branch 56 → 57 taken 898 times.
✓ Branch 56 → 61 taken 2033 times.
2931 if (const auto it = manifestations.find(candidate.getSignature()); it != manifestations.end()) {
115 898 it->second.used = true;
116
1/2
✓ Branch 59 → 60 taken 898 times.
✗ Branch 59 → 182 not taken.
898 matches.push_back(&it->second);
117 898 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 2033 times.
✗ Branch 61 → 201 not taken.
2033 Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode);
122
2/4
✓ Branch 63 → 64 taken 2033 times.
✗ Branch 63 → 201 not taken.
✓ Branch 64 → 65 taken 2033 times.
✗ Branch 64 → 201 not taken.
2033 substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName);
123
2/4
✓ Branch 65 → 66 taken 2033 times.
✗ Branch 65 → 201 not taken.
✓ Branch 66 → 67 taken 2033 times.
✗ Branch 66 → 201 not taken.
2033 substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct);
124 2033 substantiatedStruct->isNewlyInserted = true; // To not iterate over it in the same matching
125
126 // Copy struct entry
127
1/2
✓ Branch 67 → 68 taken 2033 times.
✗ Branch 67 → 201 not taken.
2033 const std::string newSignature = substantiatedStruct->getSignature();
128
1/2
✓ Branch 68 → 69 taken 2033 times.
✗ Branch 68 → 199 not taken.
2033 matchScope->lookupStrict(substantiatedStruct->name)->used = true;
129
1/2
✓ Branch 71 → 72 taken 2033 times.
✗ Branch 71 → 199 not taken.
2033 substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature);
130
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 74 taken 2033 times.
2033 assert(substantiatedStruct->entry != nullptr);
131
132 // Copy struct scope
133
1/2
✓ Branch 74 → 75 taken 2033 times.
✗ Branch 74 → 199 not taken.
2033 const std::string &oldScopeName = presetStruct.getScopeName();
134
1/2
✓ Branch 75 → 76 taken 2033 times.
✗ Branch 75 → 197 not taken.
2033 const std::string &newScopeName = substantiatedStruct->getScopeName();
135
1/2
✓ Branch 76 → 77 taken 2033 times.
✗ Branch 76 → 195 not taken.
2033 substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName);
136
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 2033 times.
2033 assert(substantiatedStruct->scope != nullptr);
137 2033 substantiatedStruct->scope->isGenericScope = false;
138
139 // Attach the template types to the new struct entry
140
1/2
✓ Branch 79 → 80 taken 2033 times.
✗ Branch 79 → 187 not taken.
2033 QualType entryType = substantiatedStruct->entry->getQualType()
141
2/4
✓ Branch 80 → 81 taken 2033 times.
✗ Branch 80 → 186 not taken.
✓ Branch 81 → 82 taken 2033 times.
✗ Branch 81 → 184 not taken.
4066 .getWithTemplateTypes(substantiatedStruct->getTemplateTypes())
142
1/2
✓ Branch 82 → 83 taken 2033 times.
✗ Branch 82 → 184 not taken.
2033 .getWithBodyScope(substantiatedStruct->scope);
143
1/2
✓ Branch 84 → 85 taken 2033 times.
✗ Branch 84 → 195 not taken.
2033 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 2033 times.
2033 assert(substantiatedStruct->scope != nullptr);
147 2033 const size_t fieldCount = substantiatedStruct->fieldTypes.size();
148
1/2
✓ Branch 88 → 89 taken 2033 times.
✗ Branch 88 → 195 not taken.
2033 const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount;
149
2/2
✓ Branch 113 → 90 taken 4734 times.
✓ Branch 113 → 114 taken 2033 times.
6767 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 4734 times.
4734 SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i);
152
3/6
✓ Branch 95 → 96 taken 4734 times.
✗ Branch 95 → 99 not taken.
✓ Branch 96 → 97 taken 4734 times.
✗ Branch 96 → 190 not taken.
✓ Branch 97 → 98 taken 4734 times.
✗ Branch 97 → 99 not taken.
4734 assert(fieldEntry != nullptr && fieldEntry->isField());
153
1/2
✓ Branch 100 → 101 taken 4734 times.
✗ Branch 100 → 190 not taken.
4734 QualType &fieldType = substantiatedStruct->fieldTypes.at(i);
154
1/2
✓ Branch 101 → 102 taken 4734 times.
✗ Branch 101 → 190 not taken.
4734 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 4734 times.
✗ Branch 102 → 190 not taken.
✓ Branch 103 → 104 taken 4734 times.
✗ Branch 103 → 190 not taken.
✓ Branch 104 → 105 taken 92 times.
✓ Branch 104 → 108 taken 4642 times.
4734 if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true))
158
2/4
✓ Branch 105 → 106 taken 92 times.
✗ Branch 105 → 188 not taken.
✓ Branch 106 → 107 taken 92 times.
✗ Branch 106 → 188 not taken.
92 fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope));
159
160
1/2
✓ Branch 108 → 109 taken 4734 times.
✗ Branch 108 → 190 not taken.
4734 fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true);
161
162 // Instantiate structs
163
3/4
✓ Branch 109 → 110 taken 4734 times.
✗ Branch 109 → 190 not taken.
✓ Branch 110 → 111 taken 1890 times.
✓ Branch 110 → 112 taken 2844 times.
4734 if (baseType.is(TY_STRUCT))
164
1/2
✓ Branch 111 → 112 taken 1890 times.
✗ Branch 111 → 190 not taken.
1890 (void)baseType.getStruct(node);
165 }
166
167 // Instantiate implemented interfaces if required
168
2/2
✓ Branch 140 → 116 taken 981 times.
✓ Branch 140 → 141 taken 2033 times.
5047 for (QualType &interfaceType : substantiatedStruct->interfaceTypes) {
169 // Skip non-generic interfaces
170
3/4
✓ Branch 118 → 119 taken 981 times.
✗ Branch 118 → 193 not taken.
✓ Branch 119 → 120 taken 2 times.
✓ Branch 119 → 121 taken 979 times.
981 if (!interfaceType.hasAnyGenericParts())
171 2 continue;
172
173 // Build template types
174
2/4
✓ Branch 121 → 122 taken 979 times.
✗ Branch 121 → 193 not taken.
✓ Branch 122 → 123 taken 979 times.
✗ Branch 122 → 193 not taken.
979 QualTypeList templateTypes = interfaceType.getTemplateTypes();
175
1/2
✓ Branch 123 → 124 taken 979 times.
✗ Branch 123 → 191 not taken.
979 TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node);
176
177 // Instantiate interface
178
1/2
✓ Branch 124 → 125 taken 979 times.
✗ Branch 124 → 191 not taken.
979 Interface *spiceInterface = interfaceType.getInterface(node, templateTypes);
179
1/2
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 979 times.
979 assert(spiceInterface != nullptr);
180
1/2
✓ Branch 127 → 128 taken 979 times.
✗ Branch 127 → 191 not taken.
979 interfaceType = spiceInterface->entry->getQualType();
181 979 }
182
183 // Add to matched structs
184
1/2
✓ Branch 141 → 142 taken 2033 times.
✗ Branch 141 → 195 not taken.
2033 matches.push_back(substantiatedStruct);
185
3/3
✓ Branch 147 → 148 taken 2033 times.
✓ Branch 147 → 150 taken 2169 times.
✓ Branch 147 → 151 taken 88865 times.
93067 }
186 }
187
188 // If no matches were found, return a nullptr
189
2/2
✓ Branch 159 → 160 taken 203 times.
✓ Branch 159 → 161 taken 5099 times.
5302 if (matches.empty())
190 203 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 5099 times.
5099 if (matches.size() > 1)
194 throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature");
195 5099 Struct *matchedStruct = matches.front();
196 5099 matchedStruct->isNewlyInserted = false;
197
198 // Insert into cache
199
1/2
✓ Branch 172 → 173 taken 5099 times.
✗ Branch 172 → 215 not taken.
5099 lookupCache[cacheKey] = matchedStruct;
200
201 5099 return matchedStruct;
202 5302 }
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 93067 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 5100 bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping,
223 const ASTNode *node) {
224 // Check if the number of types match
225 5100 const size_t typeCount = reqTemplateTypes.size();
226
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 5099 times.
5100 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 14399 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
231 4201 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
232 5099 };
233
234 // Loop over all template types
235
2/2
✓ Branch 17 → 8 taken 4201 times.
✓ Branch 17 → 18 taken 5099 times.
9300 for (size_t i = 0; i < typeCount; i++) {
236
1/2
✓ Branch 8 → 9 taken 4201 times.
✗ Branch 8 → 22 not taken.
4201 const QualType &reqType = reqTemplateTypes.at(i);
237
1/2
✓ Branch 9 → 10 taken 4201 times.
✗ Branch 9 → 22 not taken.
4201 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 4201 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 4201 times.
4201 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 4201 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 4201 times.
✗ Branch 14 → 16 not taken.
4201 if (candidateType.hasAnyGenericParts())
245
1/2
✓ Branch 15 → 16 taken 4201 times.
✗ Branch 15 → 22 not taken.
4201 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
246 }
247
248 5099 return true;
249 5099 }
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 5099 void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) {
259 // Loop over all implicit fields and substantiate the generic ones
260 5099 const size_t implicitFieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size();
261
2/2
✓ Branch 16 → 5 taken 1440 times.
✓ Branch 16 → 17 taken 5099 times.
6539 for (size_t i = 0; i < implicitFieldCount; i++) {
262
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1440 times.
1440 SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i);
263
1/2
✓ Branch 10 → 11 taken 1440 times.
✗ Branch 10 → 35 not taken.
1440 QualType fieldType = fieldEntry->getQualType();
264
3/4
✓ Branch 11 → 12 taken 1440 times.
✗ Branch 11 → 35 not taken.
✓ Branch 12 → 13 taken 689 times.
✓ Branch 12 → 15 taken 751 times.
1440 if (fieldType.hasAnyGenericParts()) {
265
1/2
✓ Branch 13 → 14 taken 689 times.
✗ Branch 13 → 35 not taken.
689 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
266
1/2
✓ Branch 14 → 15 taken 689 times.
✗ Branch 14 → 35 not taken.
689 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 10690 times.
✓ Branch 33 → 34 taken 5099 times.
20888 for (QualType &fieldType : candidate.fieldTypes)
272
3/4
✓ Branch 21 → 22 taken 10690 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 4439 times.
✓ Branch 22 → 24 taken 6251 times.
10690 if (fieldType.hasAnyGenericParts())
273
1/2
✓ Branch 23 → 24 taken 4439 times.
✗ Branch 23 → 36 not taken.
4439 TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node);
274 5099 }
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 4201 const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) {
284
1/2
✓ Branch 22 → 4 taken 5476 times.
✗ Branch 22 → 23 not taken.
9677 for (const GenericType &templateType : candidate.templateTypes) {
285
3/4
✓ Branch 6 → 7 taken 5476 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 933 times.
✓ Branch 7 → 9 taken 4543 times.
5476 if (!templateType.is(TY_GENERIC))
286 933 continue;
287
3/4
✓ Branch 9 → 10 taken 4543 times.
✗ Branch 9 → 25 not taken.
✓ Branch 11 → 12 taken 4201 times.
✓ Branch 11 → 13 taken 342 times.
4543 if (templateType.getSubType() == templateTypeName)
288 4201 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 115371 uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
302 115371 uint64_t hash = 0;
303 115371 hashCombine64(hash, hashPointer(scope));
304 115371 hashCombine64(hash, std::hash<std::string>{}(name));
305 115371 hashCombine64(hash, hashVector(templateTypes));
306 115371 return hash;
307 }
308
309 /**
310 * Clear the lookup cache
311 */
312 560 void StructManager::cleanup() {
313 560 lookupCache.clear();
314 560 lookupCacheHits = 0;
315 560 lookupCacheMisses = 0;
316 560 }
317
318 /**
319 * Dump usage statistics for the lookup cache
320 */
321 310 std::string StructManager::dumpLookupCacheStatistics() {
322
1/2
✓ Branch 2 → 3 taken 310 times.
✗ Branch 2 → 22 not taken.
310 std::stringstream stats;
323
2/4
✓ Branch 3 → 4 taken 310 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 310 times.
✗ Branch 4 → 20 not taken.
310 stats << "StructManager lookup cache statistics:" << std::endl;
324
3/6
✓ Branch 5 → 6 taken 310 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 310 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 310 times.
✗ Branch 8 → 20 not taken.
310 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
325
3/6
✓ Branch 9 → 10 taken 310 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 310 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 310 times.
✗ Branch 11 → 20 not taken.
310 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
326
3/6
✓ Branch 12 → 13 taken 310 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 310 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 310 times.
✗ Branch 14 → 20 not taken.
310 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
327
1/2
✓ Branch 15 → 16 taken 310 times.
✗ Branch 15 → 20 not taken.
620 return stats.str();
328 310 }
329
330 } // namespace spice::compiler
331