GCC Code Coverage Report


Directory: ../
File: src/typechecker/InterfaceManager.cpp
Date: 2025-12-19 06:54:40
Coverage Exec Excl Total
Lines: 92.9% 118 0 127
Functions: 100.0% 11 0 11
Branches: 54.5% 116 0 213

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