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 400 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 400 times.
✗ Branch 3 → 9 not taken.
400 insertScope->interfaces.emplace(spiceInterface.declNode->codeLoc, InterfaceManifestationList());
25
26 // Save substantiation in declaration node
27
1/2
✓ Branch 5 → 6 taken 400 times.
✗ Branch 5 → 12 not taken.
400 Interface *substantiation = insertSubstantiation(insertScope, spiceInterface, spiceInterface.declNode);
28
1/2
✓ Branch 6 → 7 taken 400 times.
✗ Branch 6 → 12 not taken.
400 nodeInterfaceList->push_back(substantiation);
29
30 400 return substantiation;
31 }
32
33 1334 Interface *InterfaceManager::insertSubstantiation(Scope *insertScope, Interface &newManifestation, const ASTNode *declNode) {
34
1/2
✓ Branch 2 → 3 taken 1334 times.
✗ Branch 2 → 31 not taken.
1334 const std::string signature = newManifestation.getSignature();
35
36 // Make sure that the manifestation does not exist already
37
2/2
✓ Branch 13 → 5 taken 1349 times.
✓ Branch 13 → 14 taken 1334 times.
2683 for ([[maybe_unused]] const auto &manifestations : insertScope->interfaces)
38
3/6
✓ Branch 6 → 7 taken 1349 times.
✗ Branch 6 → 27 not taken.
✓ Branch 7 → 8 taken 1349 times.
✗ Branch 7 → 25 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1349 times.
1349 assert(!manifestations.second.contains(newManifestation.getSignature()));
39
40 // Retrieve the matching manifestation list of the scope
41
2/4
✓ Branch 14 → 15 taken 1334 times.
✗ Branch 14 → 29 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1334 times.
1334 assert(insertScope->interfaces.contains(declNode->codeLoc));
42
1/2
✓ Branch 17 → 18 taken 1334 times.
✗ Branch 17 → 29 not taken.
1334 InterfaceManifestationList &manifestationList = insertScope->interfaces.at(declNode->codeLoc);
43
44 // Add substantiated interface
45 1334 newManifestation.manifestationIndex = manifestationList.size();
46
1/2
✓ Branch 19 → 20 taken 1334 times.
✗ Branch 19 → 29 not taken.
1334 manifestationList.emplace(signature, newManifestation);
47
1/2
✓ Branch 20 → 21 taken 1334 times.
✗ Branch 20 → 29 not taken.
2668 return &manifestationList.at(signature);
48 1334 }
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 12178 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 12178 times.
✗ Branch 2 → 159 not taken.
12178 const ConditionalLock lock(symbolRegistryMutex);
64
65 // Do cache lookup
66 12178 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqTemplateTypes);
67
3/4
✓ Branch 4 → 5 taken 12178 times.
✗ Branch 4 → 124 not taken.
✓ Branch 7 → 8 taken 10465 times.
✓ Branch 7 → 10 taken 1713 times.
12178 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
68 10465 lookupCacheHits++;
69 10465 return it->second;
70 }
71 1713 lookupCacheMisses++;
72
73 // Loop over interface registry to find interfaces, that match the requirements of the instantiation
74 1713 std::vector<Interface *> matches;
75
2/2
✓ Branch 103 → 12 taken 1408 times.
✓ Branch 103 → 104 taken 1713 times.
3121 for (auto &[defCodeLocStr, manifestations] : matchScope->interfaces) {
76
2/2
✓ Branch 100 → 17 taken 8320 times.
✓ Branch 100 → 101 taken 1124 times.
9444 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 8320 times.
✗ Branch 20 → 143 not taken.
✓ Branch 21 → 22 taken 1408 times.
✓ Branch 21 → 23 taken 6912 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 1408 times.
✓ Branch 25 → 26 taken 6912 times.
✓ Branch 25 → 27 taken 1408 times.
8320 if (presetInterface.isGenericSubstantiation() || presetInterface.isNewlyInserted)
79 7102 continue;
80
81 // Copy the interface to be able to substantiate types
82
1/2
✓ Branch 27 → 28 taken 1408 times.
✗ Branch 27 → 143 not taken.
1408 Interface candidate = presetInterface;
83
84 // Check name requirement
85
2/2
✓ Branch 29 → 30 taken 10 times.
✓ Branch 29 → 31 taken 1398 times.
1408 if (!matchName(candidate, reqName))
86 10 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 1398 TypeMapping &typeMapping = candidate.typeMapping;
90 1398 typeMapping.clear();
91
1/2
✓ Branch 33 → 34 taken 1398 times.
✗ Branch 33 → 141 not taken.
1398 typeMapping.reserve(candidate.templateTypes.size());
92
93 // Check template types requirement
94
2/4
✓ Branch 34 → 35 taken 1398 times.
✗ Branch 34 → 141 not taken.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 1398 times.
1398 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 1398 times.
✗ Branch 37 → 141 not taken.
1398 substantiateSignatures(candidate, typeMapping, node);
99
100 // We found a match! -> Set the actual candidate and its entry to used
101 1398 candidate.used = true;
102 1398 candidate.entry->used = true;
103
104 // Check if it needs to be substantiated
105
2/2
✓ Branch 39 → 40 taken 190 times.
✓ Branch 39 → 52 taken 1208 times.
1398 if (presetInterface.templateTypes.empty()) {
106
5/10
✓ Branch 40 → 41 taken 190 times.
✗ Branch 40 → 125 not taken.
✓ Branch 41 → 42 taken 190 times.
✗ Branch 41 → 46 not taken.
✓ Branch 42 → 43 taken 190 times.
✗ Branch 42 → 125 not taken.
✓ Branch 43 → 44 taken 190 times.
✗ Branch 43 → 125 not taken.
✓ Branch 44 → 45 taken 190 times.
✗ Branch 44 → 46 not taken.
190 assert(matchScope->interfaces.contains(defCodeLocStr) && matchScope->interfaces.at(defCodeLocStr).contains(mangledName));
107
2/4
✓ Branch 47 → 48 taken 190 times.
✗ Branch 47 → 125 not taken.
✓ Branch 48 → 49 taken 190 times.
✗ Branch 48 → 125 not taken.
190 Interface *match = &matchScope->interfaces.at(defCodeLocStr).at(mangledName);
108 190 match->used = true;
109
1/2
✓ Branch 49 → 50 taken 190 times.
✗ Branch 49 → 125 not taken.
190 matches.push_back(match);
110 190 continue; // Match was successful -> match the next interface
111 190 }
112
113 // Check if we already have this manifestation and can simply re-use it
114
4/6
✓ Branch 52 → 53 taken 1208 times.
✗ Branch 52 → 128 not taken.
✓ Branch 53 → 54 taken 1208 times.
✗ Branch 53 → 126 not taken.
✓ Branch 57 → 58 taken 274 times.
✓ Branch 57 → 62 taken 934 times.
1208 if (auto it = manifestations.find(candidate.getSignature()); it != manifestations.end()) {
115 274 it->second.used = true;
116
1/2
✓ Branch 60 → 61 taken 274 times.
✗ Branch 60 → 129 not taken.
274 matches.push_back(&it->second);
117 274 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 934 times.
✗ Branch 62 → 141 not taken.
934 Interface *substantiatedInterface = insertSubstantiation(matchScope, candidate, presetInterface.declNode);
122
2/4
✓ Branch 64 → 65 taken 934 times.
✗ Branch 64 → 141 not taken.
✓ Branch 65 → 66 taken 934 times.
✗ Branch 65 → 141 not taken.
934 substantiatedInterface->genericPreset = &matchScope->interfaces.at(defCodeLocStr).at(mangledName);
123
2/4
✓ Branch 66 → 67 taken 934 times.
✗ Branch 66 → 141 not taken.
✓ Branch 67 → 68 taken 934 times.
✗ Branch 67 → 141 not taken.
934 substantiatedInterface->declNode->getInterfaceManifestations()->push_back(substantiatedInterface);
124 934 substantiatedInterface->isNewlyInserted = true; // To not iterate over it in the same matching
125
126 // Copy interface entry
127
1/2
✓ Branch 68 → 69 taken 934 times.
✗ Branch 68 → 141 not taken.
934 const std::string &newSignature = substantiatedInterface->getSignature();
128
1/2
✓ Branch 69 → 70 taken 934 times.
✗ Branch 69 → 139 not taken.
934 matchScope->lookupStrict(substantiatedInterface->name)->used = true;
129
1/2
✓ Branch 72 → 73 taken 934 times.
✗ Branch 72 → 139 not taken.
934 substantiatedInterface->entry = matchScope->symbolTable.copySymbol(substantiatedInterface->name, newSignature);
130
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 934 times.
934 assert(substantiatedInterface->entry != nullptr);
131
132 // Copy interface scope
133
1/2
✓ Branch 75 → 76 taken 934 times.
✗ Branch 75 → 139 not taken.
934 const std::string &oldScopeName = presetInterface.getScopeName();
134
1/2
✓ Branch 76 → 77 taken 934 times.
✗ Branch 76 → 137 not taken.
934 const std::string &newScopeName = substantiatedInterface->getScopeName();
135
1/2
✓ Branch 77 → 78 taken 934 times.
✗ Branch 77 → 135 not taken.
934 matchScope->copyChildScope(oldScopeName, newScopeName);
136
1/2
✓ Branch 78 → 79 taken 934 times.
✗ Branch 78 → 135 not taken.
934 substantiatedInterface->scope = matchScope->getChildScope(newScopeName);
137
1/2
✗ Branch 79 → 80 not taken.
✓ Branch 79 → 81 taken 934 times.
934 assert(substantiatedInterface->scope != nullptr);
138 934 substantiatedInterface->scope->isGenericScope = false;
139
140 // Attach the template types to the new interface entry
141
1/2
✓ Branch 81 → 82 taken 934 times.
✗ Branch 81 → 134 not taken.
934 QualType entryType = substantiatedInterface->entry->getQualType()
142
2/4
✓ Branch 82 → 83 taken 934 times.
✗ Branch 82 → 133 not taken.
✓ Branch 83 → 84 taken 934 times.
✗ Branch 83 → 131 not taken.
1868 .getWithTemplateTypes(substantiatedInterface->getTemplateTypes())
143
1/2
✓ Branch 84 → 85 taken 934 times.
✗ Branch 84 → 131 not taken.
934 .getWithBodyScope(substantiatedInterface->scope);
144
1/2
✓ Branch 86 → 87 taken 934 times.
✗ Branch 86 → 135 not taken.
934 substantiatedInterface->entry->updateType(entryType, true);
145
146 // Add to matched interfaces
147
1/2
✓ Branch 87 → 88 taken 934 times.
✗ Branch 87 → 135 not taken.
934 matches.push_back(substantiatedInterface);
148
3/3
✓ Branch 93 → 94 taken 934 times.
✓ Branch 93 → 96 taken 190 times.
✓ Branch 93 → 97 taken 284 times.
1408 }
149 }
150
151 // If no matches were found, return a nullptr
152
2/2
✓ Branch 105 → 106 taken 315 times.
✓ Branch 105 → 107 taken 1398 times.
1713 if (matches.empty())
153 315 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 1398 times.
1398 if (matches.size() > 1)
157 throw SemanticError(node, INTERFACE_AMBIGUITY, "Multiple interfaces match the requested signature");
158 1398 Interface *matchedInterface = matches.front();
159 1398 matchedInterface->isNewlyInserted = false;
160
161 // Insert into cache
162
1/2
✓ Branch 118 → 119 taken 1398 times.
✗ Branch 118 → 155 not taken.
1398 lookupCache[cacheKey] = matchedInterface;
163
164 1398 return matchedInterface;
165 12178 }
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 1408 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 1398 bool InterfaceManager::matchTemplateTypes(Interface &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping,
186 const ASTNode *node) {
187 // Check if the number of types match
188 1398 const size_t typeCount = reqTemplateTypes.size();
189
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1398 times.
1398 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 4004 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
194 1208 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
195 1398 };
196
197 // Loop over all template types
198
2/2
✓ Branch 17 → 8 taken 1208 times.
✓ Branch 17 → 18 taken 1398 times.
2606 for (size_t i = 0; i < typeCount; i++) {
199
1/2
✓ Branch 8 → 9 taken 1208 times.
✗ Branch 8 → 22 not taken.
1208 const QualType &reqType = reqTemplateTypes.at(i);
200
1/2
✓ Branch 9 → 10 taken 1208 times.
✗ Branch 9 → 22 not taken.
1208 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 1208 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1208 times.
1208 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 1208 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 1208 times.
✗ Branch 14 → 16 not taken.
1208 if (candidateType.hasAnyGenericParts())
208
1/2
✓ Branch 15 → 16 taken 1208 times.
✗ Branch 15 → 22 not taken.
1208 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node);
209 }
210
211 1398 return true;
212 1398 }
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 1398 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 5020 times.
✓ Branch 50 → 51 taken 1398 times.
7816 for (Function *method : candidate.methods) {
224 // Skip methods, that are already fully substantiated
225
3/4
✓ Branch 6 → 7 taken 5020 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 1847 times.
✓ Branch 7 → 9 taken 3173 times.
5020 if (method->isFullySubstantiated())
226 1847 continue;
227
228 // Substantiate return type
229
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 3173 times.
3173 if (method->isNormalFunction())
230 FunctionManager::substantiateReturnType(*method, typeMapping, node);
231
232 // Substantiate param types
233
2/2
✓ Branch 39 → 25 taken 2 times.
✓ Branch 39 → 40 taken 3173 times.
6348 for (auto &[qualType, isOptional] : method->paramList)
234
2/4
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 52 not taken.
✓ Branch 28 → 29 taken 2 times.
✗ Branch 28 → 30 not taken.
2 if (qualType.isBase(TY_GENERIC))
235
1/2
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 52 not taken.
2 TypeMatcher::substantiateTypeWithTypeMapping(qualType, typeMapping, node);
236 }
237 1398 }
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 1208 const GenericType *InterfaceManager::getGenericTypeOfCandidateByName(const Interface &candidate,
247 const std::string &templateTypeName) {
248
1/2
✓ Branch 22 → 4 taken 1208 times.
✗ Branch 22 → 23 not taken.
2416 for (const GenericType &templateType : candidate.templateTypes) {
249
2/4
✓ Branch 6 → 7 taken 1208 times.
✗ Branch 6 → 25 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1208 times.
1208 if (!templateType.is(TY_GENERIC))
250 continue;
251
2/4
✓ Branch 9 → 10 taken 1208 times.
✗ Branch 9 → 25 not taken.
✓ Branch 11 → 12 taken 1208 times.
✗ Branch 11 → 13 not taken.
1208 if (templateType.getSubType() == templateTypeName)
252 1208 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 12178 uint64_t InterfaceManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) {
266 12178 uint64_t hash = 0;
267 12178 hashCombine64(hash, hashPointer(scope));
268 12178 hashCombine64(hash, std::hash<std::string>{}(name));
269 12178 hashCombine64(hash, hashVector(templateTypes));
270 12178 return hash;
271 }
272
273 /**
274 * Clear the lookup cache
275 */
276 607 void InterfaceManager::cleanup() {
277 607 lookupCache.clear();
278 607 lookupCacheHits = 0;
279 607 lookupCacheMisses = 0;
280 607 }
281
282 /**
283 * Dump usage statistics for the lookup cache
284 */
285 357 std::string InterfaceManager::dumpLookupCacheStatistics() {
286
1/2
✓ Branch 2 → 3 taken 357 times.
✗ Branch 2 → 22 not taken.
357 std::stringstream stats;
287
2/4
✓ Branch 3 → 4 taken 357 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 357 times.
✗ Branch 4 → 20 not taken.
357 stats << "InterfaceManager lookup cache statistics:" << std::endl;
288
3/6
✓ Branch 5 → 6 taken 357 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 357 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 357 times.
✗ Branch 8 → 20 not taken.
357 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
289
3/6
✓ Branch 9 → 10 taken 357 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 357 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 357 times.
✗ Branch 11 → 20 not taken.
357 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
290
3/6
✓ Branch 12 → 13 taken 357 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 357 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 357 times.
✗ Branch 14 → 20 not taken.
357 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
291
1/2
✓ Branch 15 → 16 taken 357 times.
✗ Branch 15 → 20 not taken.
714 return stats.str();
292 357 }
293
294 } // namespace spice::compiler
295