GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 94.7% 124 / 0 / 131
Functions: 100.0% 11 / 0 / 11
Branches: 56.0% 116 / 0 / 207

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