GCC Code Coverage Report


Directory: ../
File: src/typechecker/InterfaceManager.cpp
Date: 2025-10-27 22:48:14
Coverage Exec Excl Total
Lines: 92.9% 118 0 127
Functions: 100.0% 11 0 11
Branches: 54.7% 122 0 223

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "InterfaceManager.h"
4
5 #include <ast/ASTNodes.h>
6 #include <exception/SemanticError.h>
7 #include <symboltablebuilder/Scope.h>
8 #include <symboltablebuilder/SymbolTableBuilder.h>
9 #include <typechecker/TypeMatcher.h>
10 #include <util/CustomHashFunctions.h>
11
12 namespace spice::compiler {
13
14 // Static member initialization
15 std::unordered_map<uint64_t, Interface *> InterfaceManager::lookupCache = {};
16 size_t InterfaceManager::lookupCacheHits = 0;
17 size_t InterfaceManager::lookupCacheMisses = 0;
18
19 92 Interface *InterfaceManager::insert(Scope *insertScope, Interface &spiceInterface, std::vector<Interface *> *nodeInterfaceList) {
20 // Open a new manifestation list. Which gets filled by the substantiated manifestations of the interface
21
1/2
✓ Branch 4 → 5 taken 92 times.
✗ Branch 4 → 11 not taken.
92 insertScope->interfaces.insert({spiceInterface.declNode->codeLoc, InterfaceManifestationList()});
22
23 // Save substantiation in declaration node
24
1/2
✓ Branch 7 → 8 taken 92 times.
✗ Branch 7 → 17 not taken.
92 Interface *substantiation = insertSubstantiation(insertScope, spiceInterface, spiceInterface.declNode);
25
1/2
✓ Branch 8 → 9 taken 92 times.
✗ Branch 8 → 17 not taken.
92 nodeInterfaceList->push_back(substantiation);
26
27 92 return substantiation;
28 }
29
30 247 Interface *InterfaceManager::insertSubstantiation(Scope *insertScope, Interface &newManifestation, const ASTNode *declNode) {
31
1/2
✓ Branch 2 → 3 taken 247 times.
✗ Branch 2 → 31 not taken.
247 const std::string signature = newManifestation.getSignature();
32
33 // Make sure that the manifestation does not exist already
34
2/2
✓ Branch 13 → 5 taken 247 times.
✓ Branch 13 → 14 taken 247 times.
494 for ([[maybe_unused]] const auto &manifestations : insertScope->interfaces)
35
3/6
✓ Branch 6 → 7 taken 247 times.
✗ Branch 6 → 27 not taken.
✓ Branch 7 → 8 taken 247 times.
✗ Branch 7 → 25 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 247 times.
247 assert(!manifestations.second.contains(newManifestation.getSignature()));
36
37 // Retrieve the matching manifestation list of the scope
38
2/4
✓ Branch 14 → 15 taken 247 times.
✗ Branch 14 → 29 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 247 times.
247 assert(insertScope->interfaces.contains(declNode->codeLoc));
39
1/2
✓ Branch 17 → 18 taken 247 times.
✗ Branch 17 → 29 not taken.
247 InterfaceManifestationList &manifestationList = insertScope->interfaces.at(declNode->codeLoc);
40
41 // Add substantiated interface
42 247 newManifestation.manifestationIndex = manifestationList.size();
43
1/2
✓ Branch 19 → 20 taken 247 times.
✗ Branch 19 → 29 not taken.
247 manifestationList.emplace(signature, newManifestation);
44
1/2
✓ Branch 20 → 21 taken 247 times.
✗ Branch 20 → 29 not taken.
494 return &manifestationList.at(signature);
45 247 }
46
47 /**
48 * Check if there is an interface in this scope, fulfilling all given requirements and if found, return it.
49 * If more than one interface matches the requirement, an error gets thrown
50 *
51 * @param matchScope Scope to match against
52 * @param reqName Interface name requirement
53 * @param reqTemplateTypes Template types to substantiate generic types
54 * @param node Instantiation AST node for printing error messages
55 * @return Matched interface or nullptr
56 */
57 1510 Interface *InterfaceManager::match(Scope *matchScope, const std::string &reqName, const QualTypeList &reqTemplateTypes,
58 const ASTNode *node) {
59 // Do cache lookup
60
1/2
✓ Branch 2 → 3 taken 1510 times.
✗ Branch 2 → 160 not taken.
1510 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqTemplateTypes);
61
3/4
✓ Branch 3 → 4 taken 1510 times.
✗ Branch 3 → 160 not taken.
✓ Branch 4 → 5 taken 1233 times.
✓ Branch 4 → 7 taken 277 times.
1510 if (lookupCache.contains(cacheKey)) {
62 1233 lookupCacheHits++;
63
1/2
✓ Branch 5 → 6 taken 1233 times.
✗ Branch 5 → 160 not taken.
1233 return lookupCache.at(cacheKey);
64 }
65 277 lookupCacheMisses++;
66
67 // Copy the registry to prevent iterating over items, that are created within the loop
68
1/2
✓ Branch 7 → 8 taken 277 times.
✗ Branch 7 → 160 not taken.
277 InterfaceRegistry interfaceRegistry = matchScope->interfaces;
69 // Loop over interface registry to find interfaces, that match the requirements of the instantiation
70 277 std::vector<Interface *> matches;
71
2/2
✓ Branch 98 → 10 taken 277 times.
✓ Branch 98 → 99 taken 277 times.
554 for (const auto &[defCodeLocStr, m] : interfaceRegistry) {
72 // Copy the manifestation list to prevent iterating over items, that are created within the loop
73
1/2
✓ Branch 13 → 14 taken 277 times.
✗ Branch 13 → 145 not taken.
277 const InterfaceManifestationList manifestations = m;
74
2/2
✓ Branch 94 → 16 taken 491 times.
✓ Branch 94 → 95 taken 176 times.
667 for (const auto &[mangledName, presetInterface] : manifestations) {
75 // Skip generic substantiations to prevent double matching of an interface
76
3/4
✓ Branch 19 → 20 taken 491 times.
✗ Branch 19 → 141 not taken.
✓ Branch 20 → 21 taken 214 times.
✓ Branch 20 → 22 taken 277 times.
491 if (presetInterface.isGenericSubstantiation())
77 235 continue;
78
79 // Copy the interface to be able to substantiate types
80
1/2
✓ Branch 22 → 23 taken 277 times.
✗ Branch 22 → 141 not taken.
277 Interface candidate = presetInterface;
81
82 // Check name requirement
83
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 277 times.
277 if (!matchName(candidate, reqName))
84 break; // Leave the whole manifestation list, because all manifestations in this list have the same name
85
86 // Prepare mapping table from generic type name to concrete type
87 277 TypeMapping &typeMapping = candidate.typeMapping;
88 277 typeMapping.clear();
89
1/2
✓ Branch 28 → 29 taken 277 times.
✗ Branch 28 → 139 not taken.
277 typeMapping.reserve(candidate.templateTypes.size());
90
91 // Check template types requirement
92
2/4
✓ Branch 29 → 30 taken 277 times.
✗ Branch 29 → 139 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 277 times.
277 if (!matchTemplateTypes(candidate, reqTemplateTypes, typeMapping, node))
93 continue; // Leave this manifestation and continue with the next one
94
95 // Map signatures from generic to concrete
96
1/2
✓ Branch 32 → 33 taken 277 times.
✗ Branch 32 → 139 not taken.
277 substantiateSignatures(candidate, typeMapping, node);
97
98 // We found a match! -> Set the actual candidate and its entry to used
99 277 candidate.used = true;
100 277 candidate.entry->used = true;
101
102 // Check if it needs to be substantiated
103
2/2
✓ Branch 34 → 35 taken 21 times.
✓ Branch 34 → 47 taken 256 times.
277 if (presetInterface.templateTypes.empty()) {
104
5/10
✓ Branch 35 → 36 taken 21 times.
✗ Branch 35 → 139 not taken.
✓ Branch 36 → 37 taken 21 times.
✗ Branch 36 → 41 not taken.
✓ Branch 37 → 38 taken 21 times.
✗ Branch 37 → 139 not taken.
✓ Branch 38 → 39 taken 21 times.
✗ Branch 38 → 139 not taken.
✓ Branch 39 → 40 taken 21 times.
✗ Branch 39 → 41 not taken.
21 assert(matchScope->interfaces.contains(defCodeLocStr) && matchScope->interfaces.at(defCodeLocStr).contains(mangledName));
105
3/6
✓ Branch 42 → 43 taken 21 times.
✗ Branch 42 → 120 not taken.
✓ Branch 43 → 44 taken 21 times.
✗ Branch 43 → 120 not taken.
✓ Branch 44 → 45 taken 21 times.
✗ Branch 44 → 120 not taken.
21 matches.push_back(&matchScope->interfaces.at(defCodeLocStr).at(mangledName));
106 21 matches.back()->used = true;
107 21 continue; // Match was successful -> match the next interface
108 }
109
110 // Check if we already have this manifestation and can simply re-use it
111
4/6
✓ Branch 47 → 48 taken 256 times.
✗ Branch 47 → 123 not taken.
✓ Branch 48 → 49 taken 256 times.
✗ Branch 48 → 121 not taken.
✓ Branch 50 → 51 taken 101 times.
✓ Branch 50 → 57 taken 155 times.
256 if (manifestations.contains(candidate.getSignature())) {
112
4/8
✓ Branch 51 → 52 taken 101 times.
✗ Branch 51 → 127 not taken.
✓ Branch 52 → 53 taken 101 times.
✗ Branch 52 → 126 not taken.
✓ Branch 53 → 54 taken 101 times.
✗ Branch 53 → 124 not taken.
✓ Branch 54 → 55 taken 101 times.
✗ Branch 54 → 124 not taken.
101 matches.push_back(&matchScope->interfaces.at(defCodeLocStr).at(candidate.getSignature()));
113 101 break; // Leave the whole manifestation list to not double-match the manifestation
114 }
115
116 // Insert the substantiated version if required
117
1/2
✓ Branch 57 → 58 taken 155 times.
✗ Branch 57 → 139 not taken.
155 Interface *substantiatedInterface = insertSubstantiation(matchScope, candidate, presetInterface.declNode);
118
2/4
✓ Branch 58 → 59 taken 155 times.
✗ Branch 58 → 139 not taken.
✓ Branch 59 → 60 taken 155 times.
✗ Branch 59 → 139 not taken.
155 substantiatedInterface->genericPreset = &matchScope->interfaces.at(defCodeLocStr).at(mangledName);
119
2/4
✓ Branch 60 → 61 taken 155 times.
✗ Branch 60 → 139 not taken.
✓ Branch 61 → 62 taken 155 times.
✗ Branch 61 → 139 not taken.
155 substantiatedInterface->declNode->getInterfaceManifestations()->push_back(substantiatedInterface);
120
121 // Copy interface entry
122
1/2
✓ Branch 62 → 63 taken 155 times.
✗ Branch 62 → 139 not taken.
155 const std::string newSignature = substantiatedInterface->getSignature();
123
1/2
✓ Branch 63 → 64 taken 155 times.
✗ Branch 63 → 137 not taken.
155 matchScope->lookupStrict(substantiatedInterface->name)->used = true;
124
1/2
✓ Branch 66 → 67 taken 155 times.
✗ Branch 66 → 137 not taken.
155 substantiatedInterface->entry = matchScope->symbolTable.copySymbol(substantiatedInterface->name, newSignature);
125
1/2
✗ Branch 67 → 68 not taken.
✓ Branch 67 → 69 taken 155 times.
155 assert(substantiatedInterface->entry != nullptr);
126
127 // Copy interface scope
128
1/2
✓ Branch 69 → 70 taken 155 times.
✗ Branch 69 → 137 not taken.
155 const std::string newScopeName = INTERFACE_SCOPE_PREFIX + newSignature;
129
2/4
✓ Branch 70 → 71 taken 155 times.
✗ Branch 70 → 130 not taken.
✓ Branch 71 → 72 taken 155 times.
✗ Branch 71 → 128 not taken.
155 matchScope->copyChildScope(INTERFACE_SCOPE_PREFIX + presetInterface.name, newScopeName);
130
1/2
✓ Branch 73 → 74 taken 155 times.
✗ Branch 73 → 135 not taken.
155 substantiatedInterface->scope = matchScope->getChildScope(newScopeName);
131
1/2
✗ Branch 74 → 75 not taken.
✓ Branch 74 → 76 taken 155 times.
155 assert(substantiatedInterface->scope != nullptr);
132 155 substantiatedInterface->scope->isGenericScope = false;
133
134 // Attach the template types to the new interface entry
135
1/2
✓ Branch 76 → 77 taken 155 times.
✗ Branch 76 → 134 not taken.
155 QualType entryType = substantiatedInterface->entry->getQualType()
136
2/4
✓ Branch 77 → 78 taken 155 times.
✗ Branch 77 → 133 not taken.
✓ Branch 78 → 79 taken 155 times.
✗ Branch 78 → 131 not taken.
310 .getWithTemplateTypes(substantiatedInterface->getTemplateTypes())
137
1/2
✓ Branch 79 → 80 taken 155 times.
✗ Branch 79 → 131 not taken.
155 .getWithBodyScope(substantiatedInterface->scope);
138
1/2
✓ Branch 81 → 82 taken 155 times.
✗ Branch 81 → 135 not taken.
155 substantiatedInterface->entry->updateType(entryType, true);
139
140 // Add to matched interfaces
141
1/2
✓ Branch 82 → 83 taken 155 times.
✗ Branch 82 → 135 not taken.
155 matches.push_back(substantiatedInterface);
142
3/3
✓ Branch 87 → 88 taken 155 times.
✓ Branch 87 → 90 taken 21 times.
✓ Branch 87 → 91 taken 101 times.
277 }
143 277 }
144
145 // If no matches were found, return a nullptr
146
1/2
✗ Branch 100 → 101 not taken.
✓ Branch 100 → 102 taken 277 times.
277 if (matches.empty())
147 return nullptr;
148
149 // Check if more than one interface matches the requirements
150
1/2
✗ Branch 103 → 104 not taken.
✓ Branch 103 → 112 taken 277 times.
277 if (matches.size() > 1)
151 throw SemanticError(node, INTERFACE_AMBIGUITY, "Multiple interfaces match the requested signature");
152
153 // Insert into cache
154
1/2
✓ Branch 113 → 114 taken 277 times.
✗ Branch 113 → 156 not taken.
277 lookupCache[cacheKey] = matches.front();
155
156 277 return matches.front();
157 277 }
158
159 /**
160 * Checks if the matching candidate fulfills the name requirement
161 *
162 * @param candidate Matching candidate interface
163 * @param reqName Requested interface name
164 * @return Fulfilled or not
165 */
166 277 bool InterfaceManager::matchName(const Interface &candidate, const std::string &reqName) { return candidate.name == reqName; }
167
168 /**
169 * Checks if the matching candidate fulfills the template types requirement
170 *
171 * @param candidate Matching candidate interface
172 * @param reqTemplateTypes Requested interface template types
173 * @param typeMapping Generic type mapping
174 * @param node Instantiation AST node for printing error messages
175 * @return Fulfilled or not
176 */
177 277 bool InterfaceManager::matchTemplateTypes(Interface &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping, const ASTNode *node) {
178 // Check if the number of types match
179 277 const size_t typeCount = reqTemplateTypes.size();
180
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 277 times.
277 if (typeCount != candidate.templateTypes.size())
181 return false;
182
183 // Give the type matcher a way to retrieve instances of GenericType by their name
184 810 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
185 256 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
186 277 };
187
188 // Loop over all template types
189
2/2
✓ Branch 17 → 8 taken 256 times.
✓ Branch 17 → 18 taken 277 times.
533 for (size_t i = 0; i < typeCount; i++) {
190
1/2
✓ Branch 8 → 9 taken 256 times.
✗ Branch 8 → 22 not taken.
256 const QualType &reqType = reqTemplateTypes.at(i);
191
1/2
✓ Branch 9 → 10 taken 256 times.
✗ Branch 9 → 22 not taken.
256 QualType &candidateType = candidate.templateTypes.at(i);
192
193 // Check if the requested template type matches the candidate template type. The type mapping may be extended
194
2/4
✓ Branch 10 → 11 taken 256 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 256 times.
256 if (!TypeMatcher::matchRequestedToCandidateType(candidateType, reqType, typeMapping, genericTypeResolver, false))
195 return false;
196
197 // Substantiate the candidate param type, based on the type mapping
198
2/4
✓ Branch 13 → 14 taken 256 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 256 times.
✗ Branch 14 → 16 not taken.
256 if (candidateType.hasAnyGenericParts())
199
1/2
✓ Branch 15 → 16 taken 256 times.
✗ Branch 15 → 22 not taken.
256 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
200 }
201
202 277 return true;
203 277 }
204
205 /**
206 * Come up with the concrete signatures, by applying the type mapping onto the generic signatures
207 *
208 * @param candidate Candidate interface
209 * @param typeMapping Generic type mapping
210 * @param node Instantiation AST node for printing error messages
211 */
212 277 void InterfaceManager::substantiateSignatures(Interface &candidate, const TypeMapping &typeMapping, const ASTNode *node) {
213 // Loop over all signatures and substantiate the generic ones
214
2/2
✓ Branch 34 → 4 taken 725 times.
✓ Branch 34 → 35 taken 277 times.
1002 for (Function *method : candidate.methods) {
215 // Skip methods, that are already fully substantiated
216
3/4
✓ Branch 5 → 6 taken 725 times.
✗ Branch 5 → 37 not taken.
✓ Branch 6 → 7 taken 321 times.
✓ Branch 6 → 8 taken 404 times.
725 if (method->isFullySubstantiated())
217 321 continue;
218
219 // Substantiate return type
220
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 404 times.
404 if (method->isNormalFunction())
221 FunctionManager::substantiateReturnType(*method, typeMapping, node);
222
223 // Substantiate param types
224
2/2
✓ Branch 30 → 24 taken 2 times.
✓ Branch 30 → 31 taken 404 times.
406 for (auto &[qualType, isOptional] : method->paramList)
225
2/4
✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 36 not taken.
✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 28 not taken.
2 if (qualType.isBase(TY_GENERIC))
226
1/2
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 36 not taken.
2 TypeMatcher::substantiateTypeWithTypeMapping(qualType, typeMapping, node);
227 }
228 277 }
229
230 /**
231 * Searches the candidate template types for a generic type object with a certain name and return it
232 *
233 * @param candidate Matching candidate interface
234 * @param templateTypeName Template type name
235 * @return Generic type object
236 */
237 256 const GenericType *InterfaceManager::getGenericTypeOfCandidateByName(const Interface &candidate,
238 const std::string &templateTypeName) {
239
1/2
✓ Branch 14 → 4 taken 256 times.
✗ Branch 14 → 15 not taken.
256 for (const GenericType &templateType : candidate.templateTypes) {
240
2/4
✓ Branch 5 → 6 taken 256 times.
✗ Branch 5 → 17 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 256 times.
256 if (!templateType.is(TY_GENERIC))
241 continue;
242
2/4
✓ Branch 8 → 9 taken 256 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 256 times.
✗ Branch 10 → 12 not taken.
256 if (templateType.getSubType() == templateTypeName)
243 256 return &templateType;
244 }
245 return nullptr;
246 }
247
248 /**
249 * Calculate the cache key for the interface lookup cache
250 *
251 * @param scope Scope to match against
252 * @param name Interface name requirement
253 * @param templateTypes Template types to substantiate generic types
254 * @return Cache key
255 */
256 1510 uint64_t InterfaceManager::getCacheKey(Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
257 1510 uint64_t acc = 1469598103934665603ull;
258
1/2
✓ Branch 3 → 4 taken 1510 times.
✗ Branch 3 → 16 not taken.
1510 acc = hash_combine64(acc, std::hash<Scope *>{}(scope));
259
1/2
✓ Branch 5 → 6 taken 1510 times.
✗ Branch 5 → 17 not taken.
1510 acc = hash_combine64(acc, std::hash<std::string>{}(name));
260
2/2
✓ Branch 13 → 8 taken 1407 times.
✓ Branch 13 → 14 taken 1510 times.
2917 for (const QualType &qt : templateTypes)
261
1/2
✓ Branch 10 → 11 taken 1407 times.
✗ Branch 10 → 18 not taken.
1407 acc = hash_combine64(acc, std::hash<QualType>{}(qt));
262 1510 return acc;
263 }
264
265 /**
266 * Clear the lookup cache
267 */
268 420 void InterfaceManager::cleanup() {
269 420 lookupCache.clear();
270 420 lookupCacheHits = 0;
271 420 lookupCacheMisses = 0;
272 420 }
273
274 /**
275 * Dump usage statistics for the lookup cache
276 */
277 198 std::string InterfaceManager::dumpLookupCacheStatistics() {
278
1/2
✓ Branch 2 → 3 taken 198 times.
✗ Branch 2 → 22 not taken.
198 std::stringstream stats;
279
2/4
✓ Branch 3 → 4 taken 198 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 198 times.
✗ Branch 4 → 20 not taken.
198 stats << "InterfaceManager lookup cache statistics:" << std::endl;
280
3/6
✓ Branch 5 → 6 taken 198 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 198 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 198 times.
✗ Branch 8 → 20 not taken.
198 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
281
3/6
✓ Branch 9 → 10 taken 198 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 198 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 198 times.
✗ Branch 11 → 20 not taken.
198 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
282
3/6
✓ Branch 12 → 13 taken 198 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 198 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 198 times.
✗ Branch 14 → 20 not taken.
198 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
283
1/2
✓ Branch 15 → 16 taken 198 times.
✗ Branch 15 → 20 not taken.
396 return stats.str();
284 198 }
285
286 } // namespace spice::compiler
287