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