GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.1% 154 / 0 / 157
Functions: 100.0% 11 / 0 / 11
Branches: 58.4% 163 / 0 / 279

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