GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.2% 334 / 0 / 340
Functions: 100.0% 27 / 0 / 27
Branches: 66.8% 438 / 0 / 656

src/typechecker/FunctionManager.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "FunctionManager.h"
4
5 #include <limits>
6
7 #include <ast/ASTNodes.h>
8 #include <exception/SemanticError.h>
9 #include <model/GenericType.h>
10 #include <symboltablebuilder/Scope.h>
11 #include <symboltablebuilder/SymbolTableBuilder.h>
12 #include <typechecker/TypeChecker.h>
13 #include <typechecker/TypeMatcher.h>
14 #include <util/CodeLoc.h>
15 #include <util/CustomHashFunctions.h>
16
17 namespace spice::compiler {
18
19 // Static member initialization
20 std::unordered_map<uint64_t, Function *> FunctionManager::lookupCache = {};
21 size_t FunctionManager::lookupCacheHits = 0;
22 size_t FunctionManager::lookupCacheMisses = 0;
23
24 20638 Function *FunctionManager::insert(Scope *insertScope, const Function &baseFunction, std::vector<Function *> *nodeFunctionList) {
25 // Open a new manifestation list for the function definition
26
3/6
✓ Branch 2 → 3 taken 20638 times.
✗ Branch 2 → 49 not taken.
✓ Branch 3 → 4 taken 20638 times.
✗ Branch 3 → 46 not taken.
✓ Branch 4 → 5 taken 20638 times.
✗ Branch 4 → 44 not taken.
20638 const std::string fctId = baseFunction.name + ":" + baseFunction.declNode->codeLoc.toPrettyLineAndColumn();
27
1/2
✓ Branch 8 → 9 taken 20638 times.
✗ Branch 8 → 50 not taken.
20638 insertScope->functions.emplace(fctId, FunctionManifestationList());
28
29 // Collect substantiations
30 20638 std::vector<Function> manifestations;
31
1/2
✓ Branch 10 → 11 taken 20638 times.
✗ Branch 10 → 54 not taken.
20638 substantiateOptionalParams(baseFunction, manifestations);
32
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 20638 times.
20638 assert(!manifestations.empty());
33
34 // Save substantiations in declaration node
35 20638 Function *manifestationPtr = nullptr;
36
2/2
✓ Branch 32 → 16 taken 21938 times.
✓ Branch 32 → 33 taken 20636 times.
63212 for (const Function &manifestation : manifestations) {
37
2/2
✓ Branch 18 → 19 taken 21936 times.
✓ Branch 18 → 53 taken 2 times.
21938 manifestationPtr = insertSubstantiation(insertScope, manifestation, baseFunction.declNode);
38
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 21936 times.
21936 assert(manifestationPtr != nullptr);
39
1/2
✓ Branch 21 → 22 taken 21936 times.
✗ Branch 21 → 23 not taken.
21936 if (nodeFunctionList)
40
1/2
✓ Branch 22 → 23 taken 21936 times.
✗ Branch 22 → 53 not taken.
21936 nodeFunctionList->push_back(manifestationPtr);
41 }
42
43
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 20636 times.
20636 if (!nodeFunctionList)
44 return manifestationPtr;
45
46
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 20636 times.
20636 assert(!nodeFunctionList->empty());
47 20636 return nodeFunctionList->front();
48 20640 }
49
50 /**
51 * Create definite functions from ambiguous ones, in regard to optional arguments.
52 *
53 * Example:
54 * int testFunc(string, int?, double?)
55 * gets
56 * int testFunc(string)
57 * int testFunc(string, int)
58 * int testFunc(string, int, double)
59 *
60 * This method also accepts functions, that are already definite, but does nothing to them.
61 *
62 * @param baseFunction Ambiguous base function
63 * @param manifestations Vector to store the definite manifestations
64 * @return True, if there were optional arguments found
65 */
66 20638 void FunctionManager::substantiateOptionalParams(const Function &baseFunction, std::vector<Function> &manifestations) {
67 // Handle the case of no parameters -> simply return the base function
68
2/2
✓ Branch 3 → 4 taken 5030 times.
✓ Branch 3 → 6 taken 15608 times.
20638 if (baseFunction.paramList.empty()) {
69
1/2
✓ Branch 4 → 5 taken 5030 times.
✗ Branch 4 → 47 not taken.
5030 manifestations.push_back(baseFunction);
70 5030 return;
71 }
72
73 15608 ParamList currentFunctionParamTypes;
74
1/2
✓ Branch 7 → 8 taken 15608 times.
✗ Branch 7 → 45 not taken.
15608 currentFunctionParamTypes.reserve(baseFunction.paramList.size());
75 15608 bool metFirstOptionalParam = false;
76
1/2
✓ Branch 8 → 9 taken 15608 times.
✗ Branch 8 → 45 not taken.
15608 Function manifestation = baseFunction;
77
78 // Loop over all parameters
79
2/2
✓ Branch 32 → 11 taken 23913 times.
✓ Branch 32 → 33 taken 15608 times.
55129 for (const auto &[qualType, isOptional] : baseFunction.paramList) {
80 // Check if we have a mandatory parameter
81
2/2
✓ Branch 13 → 14 taken 22613 times.
✓ Branch 13 → 16 taken 1300 times.
23913 if (!isOptional) {
82
1/2
✓ Branch 14 → 15 taken 22613 times.
✗ Branch 14 → 40 not taken.
22613 currentFunctionParamTypes.push_back({qualType, /*optional=*/false});
83 22613 continue;
84 }
85
86 // Add substantiation without the optional parameter
87
2/2
✓ Branch 16 → 17 taken 1277 times.
✓ Branch 16 → 20 taken 23 times.
1300 if (!metFirstOptionalParam) {
88
1/2
✓ Branch 17 → 18 taken 1277 times.
✗ Branch 17 → 42 not taken.
1277 manifestation.paramList = currentFunctionParamTypes;
89
1/2
✓ Branch 18 → 19 taken 1277 times.
✗ Branch 18 → 42 not taken.
1277 manifestations.push_back(manifestation);
90 // Now we cannot accept mandatory parameters anymore
91 1277 metFirstOptionalParam = true;
92 }
93
94 // Add substantiation with the optional parameter
95
1/2
✓ Branch 20 → 21 taken 1300 times.
✗ Branch 20 → 41 not taken.
1300 currentFunctionParamTypes.push_back({qualType, /*optional=*/false});
96
1/2
✓ Branch 21 → 22 taken 1300 times.
✗ Branch 21 → 42 not taken.
1300 manifestation.paramList = currentFunctionParamTypes;
97
1/2
✓ Branch 22 → 23 taken 1300 times.
✗ Branch 22 → 42 not taken.
1300 manifestations.push_back(manifestation);
98 }
99
100 // Ensure at least once manifestation
101
2/2
✓ Branch 34 → 35 taken 14331 times.
✓ Branch 34 → 36 taken 1277 times.
15608 if (manifestations.empty())
102
1/2
✓ Branch 35 → 36 taken 14331 times.
✗ Branch 35 → 43 not taken.
14331 manifestations.push_back(baseFunction);
103 15608 }
104
105 6 Function FunctionManager::createMainFunction(SymbolTableEntry *entry, const QualTypeList &paramTypes, ASTNode *declNode) {
106 6 ParamList paramList;
107
2/2
✓ Branch 16 → 4 taken 2 times.
✓ Branch 16 → 17 taken 6 times.
14 for (const QualType &paramType : paramTypes)
108
1/2
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 33 not taken.
2 paramList.push_back({paramType, false});
109
5/10
✓ Branch 19 → 20 taken 6 times.
✗ Branch 19 → 45 not taken.
✓ Branch 20 → 21 taken 6 times.
✗ Branch 20 → 42 not taken.
✓ Branch 21 → 22 taken 6 times.
✗ Branch 21 → 41 not taken.
✓ Branch 22 → 23 taken 6 times.
✗ Branch 22 → 40 not taken.
✓ Branch 24 → 25 taken 6 times.
✗ Branch 24 → 35 not taken.
18 return {MAIN_FUNCTION_NAME, entry, QualType(TY_DYN), QualType(TY_INT), paramList, {}, declNode};
110 6 }
111
112 26986 Function *FunctionManager::insertSubstantiation(Scope *insertScope, const Function &newManifestation, const ASTNode *declNode) {
113
2/4
✓ Branch 2 → 3 taken 26986 times.
✗ Branch 2 → 70 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 26986 times.
26986 assert(newManifestation.hasSubstantiatedParams());
114
115
1/2
✓ Branch 5 → 6 taken 26986 times.
✗ Branch 5 → 70 not taken.
26986 const std::string signature = newManifestation.getSignature(true, true, false);
116
117 // Check if the function exists already
118
5/8
✓ Branch 6 → 7 taken 26986 times.
✗ Branch 6 → 59 not taken.
✓ Branch 7 → 8 taken 26986 times.
✗ Branch 7 → 59 not taken.
✓ Branch 8 → 9 taken 26986 times.
✗ Branch 8 → 59 not taken.
✓ Branch 30 → 10 taken 398025 times.
✓ Branch 30 → 31 taken 26984 times.
425009 for (const auto &manifestations : insertScope->functions | std::views::values) {
119
3/4
✓ Branch 11 → 12 taken 398025 times.
✗ Branch 11 → 59 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 28 taken 398023 times.
398025 if (manifestations.contains(signature)) {
120
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 1 time.
2 const SemanticErrorType errorType = newManifestation.isFunction() ? FUNCTION_DECLARED_TWICE : PROCEDURE_DECLARED_TWICE;
121
4/8
✓ Branch 20 → 21 taken 2 times.
✗ Branch 20 → 53 not taken.
✓ Branch 21 → 22 taken 2 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 2 times.
✗ Branch 22 → 49 not taken.
✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 47 not taken.
2 throw SemanticError(declNode, errorType, "'" + newManifestation.getSignature(true, false) + "' is declared twice");
122 }
123 }
124
125 // Retrieve the matching manifestation list of the scope
126
3/6
✓ Branch 31 → 32 taken 26984 times.
✗ Branch 31 → 65 not taken.
✓ Branch 32 → 33 taken 26984 times.
✗ Branch 32 → 62 not taken.
✓ Branch 33 → 34 taken 26984 times.
✗ Branch 33 → 60 not taken.
26984 const std::string fctId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
127
2/4
✓ Branch 36 → 37 taken 26984 times.
✗ Branch 36 → 66 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 26984 times.
26984 assert(insertScope->functions.contains(fctId));
128
1/2
✓ Branch 39 → 40 taken 26984 times.
✗ Branch 39 → 66 not taken.
26984 FunctionManifestationList &manifestationList = insertScope->functions.at(fctId);
129
130 // Add substantiated function
131
1/2
✓ Branch 40 → 41 taken 26984 times.
✗ Branch 40 → 66 not taken.
26984 manifestationList.emplace(signature, newManifestation);
132
1/2
✓ Branch 41 → 42 taken 26984 times.
✗ Branch 41 → 66 not taken.
53968 return &manifestationList.at(signature);
133 26986 }
134
135 /**
136 * Checks if a function exists by matching it, but not setting it to used
137 *
138 * @param matchScope Scope to match against
139 * @param reqName Function name requirement
140 * @param reqThisType This type requirement
141 * @param reqArgs Argument requirement
142 * @param strictQualifierMatching Match argument and this type qualifiers strictly
143 * @return Found function or nullptr
144 */
145 15165 const Function *FunctionManager::lookup(Scope *matchScope, const std::string &reqName, const QualType &reqThisType,
146 const ArgList &reqArgs, bool strictQualifierMatching) {
147
2/4
✓ Branch 2 → 3 taken 15165 times.
✗ Branch 2 → 64 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 15165 times.
15165 assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT}));
148
149 // Do cache lookup
150 15165 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, {});
151
3/4
✓ Branch 8 → 9 taken 15165 times.
✗ Branch 8 → 65 not taken.
✓ Branch 11 → 12 taken 3351 times.
✓ Branch 11 → 14 taken 11814 times.
15165 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
152 3351 lookupCacheHits++;
153 3351 return it->second;
154 }
155 11814 lookupCacheMisses++;
156
157 4679 const auto pred = [&](const Arg &arg) { return arg.first.hasAnyGenericParts(); };
158
5/8
✓ Branch 14 → 15 taken 11814 times.
✗ Branch 14 → 74 not taken.
✓ Branch 15 → 16 taken 11382 times.
✓ Branch 15 → 19 taken 432 times.
✓ Branch 16 → 17 taken 11382 times.
✗ Branch 16 → 74 not taken.
✓ Branch 17 → 18 taken 11382 times.
✗ Branch 17 → 19 not taken.
11814 const bool requestedFullySubstantiated = !reqThisType.hasAnyGenericParts() && std::ranges::none_of(reqArgs, pred);
159
160 // Loop over function registry to find functions, that match the requirements of the call
161 11814 std::vector<const Function *> matches;
162
2/2
✓ Branch 54 → 22 taken 83796 times.
✓ Branch 54 → 55 taken 11814 times.
95610 for (const auto &[defCodeLocStr, manifestations] : matchScope->functions) {
163
2/2
✓ Branch 51 → 27 taken 100536 times.
✓ Branch 51 → 52 taken 35935 times.
136471 for (const auto &[signature, presetFunction] : manifestations) {
164
2/4
✓ Branch 30 → 31 taken 100536 times.
✗ Branch 30 → 69 not taken.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 100536 times.
100536 assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point
165
166 // - search for concrete fct: Only match against fully substantiated versions to prevent double matching of a function
167 // - search for generic fct: Only match against generic preset functions
168
3/4
✓ Branch 33 → 34 taken 100536 times.
✗ Branch 33 → 69 not taken.
✓ Branch 34 → 35 taken 41985 times.
✓ Branch 34 → 36 taken 58551 times.
100536 if (presetFunction.isFullySubstantiated() != requestedFullySubstantiated)
169 52675 continue;
170
171 // Copy the function to be able to substantiate types
172
1/2
✓ Branch 36 → 37 taken 58551 times.
✗ Branch 36 → 69 not taken.
58551 Function candidate = presetFunction;
173
174 // Create empty type mapping
175 58551 TypeMapping &typeMapping = candidate.typeMapping;
176
177 58551 bool forceSubstantiation = false;
178
1/2
✓ Branch 37 → 38 taken 58551 times.
✗ Branch 37 → 67 not taken.
58551 const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping,
179 strictQualifierMatching, forceSubstantiation, nullptr);
180
2/2
✓ Branch 38 → 39 taken 46322 times.
✓ Branch 38 → 40 taken 12229 times.
58551 if (matchResult == MatchResult::SKIP_FUNCTION)
181 46322 break; // Leave the whole function
182
2/2
✓ Branch 40 → 41 taken 10690 times.
✓ Branch 40 → 42 taken 1539 times.
12229 if (matchResult == MatchResult::SKIP_MANIFESTATION)
183 10690 continue; // Leave this manifestation and try the next one
184
185 // Add to matches
186
3/6
✓ Branch 42 → 43 taken 1539 times.
✗ Branch 42 → 66 not taken.
✓ Branch 43 → 44 taken 1539 times.
✗ Branch 43 → 66 not taken.
✓ Branch 44 → 45 taken 1539 times.
✗ Branch 44 → 66 not taken.
1539 matches.push_back(&matchScope->functions.at(defCodeLocStr).at(signature));
187
188 1539 break; // Leave the whole manifestation list to not double-match the manifestation
189
2/2
✓ Branch 47 → 48 taken 10690 times.
✓ Branch 47 → 49 taken 47861 times.
58551 }
190 }
191
192 // Return the very match or a nullptr
193
2/2
✓ Branch 56 → 57 taken 1539 times.
✓ Branch 56 → 59 taken 10275 times.
11814 return !matches.empty() ? matches.front() : nullptr;
194 11814 }
195
196 /**
197 * Check if there is a function in the scope, fulfilling all given requirements and if found, return it.
198 * If more than one function matches the requirement, an error gets thrown.
199 *
200 * @param matchScope Scope to match against
201 * @param reqName Function name requirement
202 * @param reqThisType This type requirement
203 * @param reqArgs Argument requirement
204 * @param templateTypeHints Template type requirement
205 * @param strictQualifierMatching Match argument and this type qualifiers strictly
206 * @param callNode Call AST node for printing error messages
207 * @return Matched function or nullptr
208 */
209 129144 Function *FunctionManager::match(Scope *matchScope, const std::string &reqName, const QualType &reqThisType,
210 const ArgList &reqArgs, const QualTypeList &templateTypeHints, bool strictQualifierMatching,
211 const ASTNode *callNode) {
212
2/4
✓ Branch 2 → 3 taken 129144 times.
✗ Branch 2 → 181 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 129144 times.
129144 assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT, TY_INTERFACE}));
213
214 // Do cache lookup
215 129144 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, templateTypeHints);
216
3/4
✓ Branch 6 → 7 taken 129144 times.
✗ Branch 6 → 182 not taken.
✓ Branch 9 → 10 taken 21336 times.
✓ Branch 9 → 12 taken 107808 times.
129144 if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) {
217 21336 lookupCacheHits++;
218 21336 return it->second;
219 }
220 107808 lookupCacheMisses++;
221
222 // Loop over function registry to find functions, that match the requirements of the call
223 107808 std::vector<Function *> matches;
224
2/2
✓ Branch 140 → 14 taken 1336399 times.
✓ Branch 140 → 141 taken 107807 times.
1444206 for (auto &[fctId, manifestations] : matchScope->functions) {
225
2/2
✓ Branch 137 → 19 taken 1354528 times.
✓ Branch 137 → 138 taken 74560 times.
1429088 for (const auto &[signature, presetFunction] : manifestations) {
226
2/4
✓ Branch 22 → 23 taken 1354528 times.
✗ Branch 22 → 205 not taken.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 1354528 times.
1354528 assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point
227
228 // Skip generic and newly inserted substantiations to prevent double matching of a function
229
6/8
✓ Branch 25 → 26 taken 1354528 times.
✗ Branch 25 → 205 not taken.
✓ Branch 26 → 27 taken 1339369 times.
✓ Branch 26 → 28 taken 15159 times.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 1339369 times.
✓ Branch 30 → 31 taken 15159 times.
✓ Branch 30 → 32 taken 1339369 times.
1354528 if (presetFunction.isGenericSubstantiation() || presetFunction.isNewlyInserted)
230 92689 continue;
231
232 // Copy the function to be able to substantiate types
233
1/2
✓ Branch 32 → 33 taken 1339369 times.
✗ Branch 32 → 205 not taken.
1339369 Function candidate = presetFunction;
234
235 // Prepare type mapping, based on the given initial type mapping
236 1339369 TypeMapping &typeMapping = candidate.typeMapping;
237 1339369 typeMapping.clear();
238
2/2
✓ Branch 43 → 35 taken 45202 times.
✓ Branch 43 → 44 taken 1339369 times.
1384571 for (size_t i = 0; i < std::min(templateTypeHints.size(), candidate.templateTypes.size()); i++) {
239
2/4
✓ Branch 35 → 36 taken 45202 times.
✗ Branch 35 → 203 not taken.
✓ Branch 36 → 37 taken 45202 times.
✗ Branch 36 → 203 not taken.
45202 const std::string &typeName = candidate.templateTypes.at(i).getSubType();
240
1/2
✓ Branch 37 → 38 taken 45202 times.
✗ Branch 37 → 203 not taken.
45202 const QualType &templateType = templateTypeHints.at(i);
241
1/2
✓ Branch 38 → 39 taken 45202 times.
✗ Branch 38 → 203 not taken.
45202 typeMapping.emplace(typeName, templateType);
242 }
243
244 1339369 bool forceSubstantiation = false;
245
2/2
✓ Branch 44 → 45 taken 1339368 times.
✓ Branch 44 → 203 taken 1 time.
1339369 const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping,
246 strictQualifierMatching, forceSubstantiation, callNode);
247
2/2
✓ Branch 45 → 46 taken 1256176 times.
✓ Branch 45 → 47 taken 83192 times.
1339368 if (matchResult == MatchResult::SKIP_FUNCTION)
248 1256176 break; // Leave the whole function
249
2/2
✓ Branch 47 → 48 taken 69333 times.
✓ Branch 47 → 49 taken 13859 times.
83192 if (matchResult == MatchResult::SKIP_MANIFESTATION)
250 69333 continue; // Leave this manifestation and try the next one
251
252 // We found a match! -> Set the actual candidate and its entry to used
253 13859 candidate.used = true;
254 13859 candidate.entry->used = true;
255
256 // Check if the function is generic needs to be substantiated
257
6/6
✓ Branch 50 → 51 taken 8340 times.
✓ Branch 50 → 53 taken 5519 times.
✓ Branch 51 → 52 taken 8197 times.
✓ Branch 51 → 53 taken 143 times.
✓ Branch 54 → 55 taken 8197 times.
✓ Branch 54 → 67 taken 5662 times.
13859 if (presetFunction.templateTypes.empty() && !forceSubstantiation) {
258
5/10
✓ Branch 55 → 56 taken 8197 times.
✗ Branch 55 → 183 not taken.
✓ Branch 56 → 57 taken 8197 times.
✗ Branch 56 → 61 not taken.
✓ Branch 57 → 58 taken 8197 times.
✗ Branch 57 → 183 not taken.
✓ Branch 58 → 59 taken 8197 times.
✗ Branch 58 → 183 not taken.
✓ Branch 59 → 60 taken 8197 times.
✗ Branch 59 → 61 not taken.
8197 assert(matchScope->functions.contains(fctId) && matchScope->functions.at(fctId).contains(signature));
259
2/4
✓ Branch 62 → 63 taken 8197 times.
✗ Branch 62 → 183 not taken.
✓ Branch 63 → 64 taken 8197 times.
✗ Branch 63 → 183 not taken.
8197 Function *match = &matchScope->functions.at(fctId).at(signature);
260 8197 match->used = true;
261
1/2
✓ Branch 64 → 65 taken 8197 times.
✗ Branch 64 → 183 not taken.
8197 matches.push_back(match);
262 8197 continue; // Match was successful -> match the next function
263 8197 }
264
265 // Check if we already have this manifestation and can simply re-use it
266
1/2
✓ Branch 67 → 68 taken 5662 times.
✗ Branch 67 → 203 not taken.
5662 const std::string newSignature = candidate.getSignature(true, true, false);
267
3/4
✓ Branch 68 → 69 taken 5662 times.
✗ Branch 68 → 185 not taken.
✓ Branch 71 → 72 taken 614 times.
✓ Branch 71 → 76 taken 5048 times.
5662 if (const auto it = manifestations.find(newSignature); it != manifestations.end()) {
268 614 it->second.used = true;
269
1/2
✓ Branch 74 → 75 taken 614 times.
✗ Branch 74 → 184 not taken.
614 matches.push_back(&it->second);
270 614 break; // Leave the whole manifestation list to not double-match the manifestation
271 }
272
273 // Insert the substantiated version if required
274
1/2
✓ Branch 76 → 78 taken 5048 times.
✗ Branch 76 → 201 not taken.
5048 Function *substantiatedFunction = insertSubstantiation(matchScope, candidate, presetFunction.declNode);
275
2/4
✓ Branch 78 → 79 taken 5048 times.
✗ Branch 78 → 201 not taken.
✓ Branch 79 → 80 taken 5048 times.
✗ Branch 79 → 201 not taken.
5048 substantiatedFunction->genericPreset = &matchScope->functions.at(fctId).at(signature);
276 5048 substantiatedFunction->alreadyTypeChecked = false;
277
2/4
✓ Branch 80 → 81 taken 5048 times.
✗ Branch 80 → 201 not taken.
✓ Branch 81 → 82 taken 5048 times.
✗ Branch 81 → 201 not taken.
5048 substantiatedFunction->declNode->getFctManifestations(reqName)->push_back(substantiatedFunction);
278 5048 substantiatedFunction->isNewlyInserted = true; // To not iterate over it in the same matching
279
280 // Copy function entry
281
1/2
✓ Branch 82 → 83 taken 5048 times.
✗ Branch 82 → 201 not taken.
5048 const std::string newScopeName = substantiatedFunction->getScopeName();
282
1/2
✓ Branch 83 → 84 taken 5048 times.
✗ Branch 83 → 199 not taken.
5048 matchScope->lookupStrict(presetFunction.entry->name)->used = true;
283
1/2
✓ Branch 86 → 87 taken 5048 times.
✗ Branch 86 → 199 not taken.
5048 substantiatedFunction->entry = matchScope->symbolTable.copySymbol(presetFunction.entry->name, newScopeName);
284
1/2
✗ Branch 87 → 88 not taken.
✓ Branch 87 → 89 taken 5048 times.
5048 assert(substantiatedFunction->entry != nullptr);
285
286 // Copy function scope
287
1/2
✓ Branch 89 → 90 taken 5048 times.
✗ Branch 89 → 199 not taken.
5048 const std::string oldScopeName = presetFunction.getScopeName();
288
1/2
✓ Branch 90 → 91 taken 5048 times.
✗ Branch 90 → 197 not taken.
5048 Scope *childScope = matchScope->copyChildScope(oldScopeName, newScopeName);
289
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 5048 times.
5048 assert(childScope != nullptr);
290 5048 childScope->isGenericScope = false;
291 5048 substantiatedFunction->bodyScope = childScope;
292
293 // Insert symbols for generic type names with concrete types into the child block
294
2/2
✓ Branch 103 → 95 taken 6714 times.
✓ Branch 103 → 104 taken 5048 times.
11762 for (const auto &[typeName, concreteType] : substantiatedFunction->typeMapping)
295
2/4
✓ Branch 98 → 99 taken 6714 times.
✗ Branch 98 → 188 not taken.
✓ Branch 99 → 100 taken 6714 times.
✗ Branch 99 → 186 not taken.
6714 childScope->insertGenericType(typeName, GenericType(concreteType));
296
297 // Substantiate the 'this' entry in the new function scope
298
6/6
✓ Branch 107 → 108 taken 3611 times.
✓ Branch 107 → 111 taken 1437 times.
✓ Branch 109 → 110 taken 3610 times.
✓ Branch 109 → 111 taken 1 time.
✓ Branch 112 → 113 taken 3610 times.
✓ Branch 112 → 126 taken 1438 times.
5048 if (presetFunction.isMethod() && !presetFunction.templateTypes.empty()) {
299
1/2
✓ Branch 115 → 116 taken 3610 times.
✗ Branch 115 → 192 not taken.
10830 SymbolTableEntry *thisEntry = childScope->lookupStrict(THIS_VARIABLE_NAME);
300
1/2
✗ Branch 121 → 122 not taken.
✓ Branch 121 → 123 taken 3610 times.
3610 assert(thisEntry != nullptr);
301
2/4
✓ Branch 123 → 124 taken 3610 times.
✗ Branch 123 → 196 not taken.
✓ Branch 124 → 125 taken 3610 times.
✗ Branch 124 → 196 not taken.
3610 thisEntry->updateType(candidate.thisType.toPtr(callNode), /*overwriteExistingType=*/true);
302 }
303
304 // Add to matched functions
305
1/2
✓ Branch 126 → 127 taken 5048 times.
✗ Branch 126 → 197 not taken.
5048 matches.push_back(substantiatedFunction);
306
307 5048 break; // Leave the whole manifestation list to not double-match the manifestation
308
2/2
✓ Branch 133 → 134 taken 77530 times.
✓ Branch 133 → 135 taken 1261838 times.
1345031 }
309 }
310
311 // If no matches were found, return a nullptr
312
2/2
✓ Branch 142 → 143 taken 93955 times.
✓ Branch 142 → 144 taken 13852 times.
107807 if (matches.empty())
313 93955 return nullptr;
314
315 // Tie-breaking: if multiple candidates match, narrow them by qualifier specificity (see breakOverloadTie).
316
1/2
✓ Branch 144 → 145 taken 13852 times.
✗ Branch 144 → 221 not taken.
13852 breakOverloadTie(matches, reqArgs);
317
318 // Check if more than one function matches the requirements
319
2/2
✓ Branch 146 → 147 taken 1 time.
✓ Branch 146 → 174 taken 13851 times.
13852 if (matches.size() > 1) {
320
1/2
✓ Branch 147 → 148 taken 1 time.
✗ Branch 147 → 220 not taken.
1 std::stringstream errorMessage;
321
3/6
✓ Branch 148 → 149 taken 1 time.
✗ Branch 148 → 218 not taken.
✓ Branch 149 → 150 taken 1 time.
✗ Branch 149 → 218 not taken.
✓ Branch 150 → 151 taken 1 time.
✗ Branch 150 → 218 not taken.
1 errorMessage << "The function/procedure '" << reqName << "' is ambiguous. All of the following match the requested criteria:";
322
2/2
✓ Branch 168 → 153 taken 2 times.
✓ Branch 168 → 169 taken 1 time.
4 for (const Function *match : matches)
323
3/6
✓ Branch 155 → 156 taken 2 times.
✗ Branch 155 → 211 not taken.
✓ Branch 156 → 157 taken 2 times.
✗ Branch 156 → 210 not taken.
✓ Branch 157 → 158 taken 2 times.
✗ Branch 157 → 208 not taken.
2 errorMessage << "\n " << match->getSignature();
324
2/4
✓ Branch 170 → 171 taken 1 time.
✗ Branch 170 → 215 not taken.
✓ Branch 171 → 172 taken 1 time.
✗ Branch 171 → 212 not taken.
1 throw SemanticError(callNode, FUNCTION_AMBIGUITY, errorMessage.str());
325 1 }
326 13851 Function *matchedFunction = matches.front();
327 13851 matchedFunction->isNewlyInserted = false;
328
329 // Insert into cache
330
1/2
✓ Branch 175 → 176 taken 13851 times.
✗ Branch 175 → 221 not taken.
13851 lookupCache[cacheKey] = matchedFunction;
331
332 // Trigger revisit in type checker if required
333
1/2
✓ Branch 176 → 177 taken 13851 times.
✗ Branch 176 → 221 not taken.
13851 TypeChecker::requestRevisitIfRequired(matchedFunction);
334
335 // Return the very match
336 13851 return matchedFunction;
337 107808 }
338
339 1397920 MatchResult FunctionManager::matchManifestation(Function &candidate, Scope *&matchScope, const std::string &reqName,
340 const QualType &reqThisType, const ArgList &reqArgs, TypeMapping &typeMapping,
341 bool strictQualifierMatching, bool &forceSubstantiation,
342 const ASTNode *callNode) {
343 // Check name requirement
344
2/2
✓ Branch 3 → 4 taken 1302498 times.
✓ Branch 3 → 5 taken 95422 times.
1397920 if (!matchName(candidate, reqName))
345 1302498 return MatchResult::SKIP_FUNCTION; // Leave the whole manifestation list, because all have the same name
346
347 // Check 'this' type requirement
348
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 95422 times.
95422 if (!matchThisType(candidate, reqThisType, typeMapping, strictQualifierMatching, callNode))
349 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
350
351 // Check arg types requirement
352
2/2
✓ Branch 9 → 10 taken 80022 times.
✓ Branch 9 → 11 taken 15399 times.
95422 if (!matchArgTypes(candidate, reqArgs, typeMapping, strictQualifierMatching, forceSubstantiation, callNode))
353 80022 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
354
355 // Check if there are unresolved generic types
356
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 15398 times.
15399 if (typeMapping.size() < candidate.templateTypes.size())
357 1 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
358
359 // Substantiate return type
360 15398 substantiateReturnType(candidate, typeMapping, callNode);
361
362 // Set the match scope to the scope of the concrete substantiation
363 15398 const QualType &thisType = candidate.thisType;
364
2/2
✓ Branch 17 → 18 taken 9166 times.
✓ Branch 17 → 25 taken 6232 times.
15398 if (!thisType.is(TY_DYN)) {
365 // If we only have the generic struct scope, lookup the concrete manifestation scope
366
2/2
✓ Branch 18 → 19 taken 132 times.
✓ Branch 18 → 23 taken 9034 times.
9166 if (matchScope->isGenericScope) {
367 132 const Struct *spiceStruct = thisType.getStruct(candidate.declNode);
368
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 132 times.
132 assert(spiceStruct != nullptr);
369 132 matchScope = spiceStruct->scope;
370 }
371
1/2
✓ Branch 23 → 24 taken 9166 times.
✗ Branch 23 → 27 not taken.
9166 candidate.thisType = candidate.thisType.getWithBodyScope(matchScope);
372 }
373
374 15398 return MatchResult::MATCHED;
375 }
376
377 /**
378 * Checks if the matching candidate fulfills the name requirement
379 *
380 * @param candidate Matching candidate function
381 * @param reqName Requested function name
382 * @return Fulfilled or not
383 */
384 1397920 bool FunctionManager::matchName(const Function &candidate, const std::string &reqName) { return candidate.name == reqName; }
385
386 /**
387 * Checks if the matching candidate fulfills the 'this' type requirement
388 *
389 * @param candidate Matching candidate function
390 * @param reqThisType Requested 'this' type
391 * @param typeMapping Concrete template type mapping
392 * @param strictQualifierMatching Match qualifiers strictly
393 * @param callNode Call AST node for printing error messages
394 * @return Fulfilled or not
395 */
396 95422 bool FunctionManager::matchThisType(Function &candidate, const QualType &reqThisType, TypeMapping &typeMapping,
397 bool strictQualifierMatching, const ASTNode *callNode) {
398 95422 QualType &candidateThisType = candidate.thisType;
399
400 // Shortcut for procedures
401
7/10
✓ Branch 2 → 3 taken 95422 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 65672 times.
✓ Branch 3 → 7 taken 29750 times.
✓ Branch 4 → 5 taken 65672 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 65672 times.
✗ Branch 5 → 7 not taken.
✓ Branch 8 → 9 taken 65672 times.
✓ Branch 8 → 10 taken 29750 times.
95422 if (candidateThisType.is(TY_DYN) && reqThisType.is(TY_DYN))
402 65672 return true;
403
404 // Give the type matcher a way to retrieve instances of GenericType by their name
405 65380 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
406 5880 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
407 29750 };
408
409 // Check if the requested 'this' type matches the candidate 'this' type. The type mapping may be extended
410
2/4
✓ Branch 11 → 12 taken 29750 times.
✗ Branch 11 → 21 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 29750 times.
29750 if (!TypeMatcher::matchRequestedToCandidateType(candidateThisType, reqThisType, typeMapping, genericTypeResolver,
411 strictQualifierMatching))
412 return false;
413
414 // Substantiate the candidate param type, based on the type mapping
415
3/4
✓ Branch 14 → 15 taken 29750 times.
✗ Branch 14 → 21 not taken.
✓ Branch 15 → 16 taken 6093 times.
✓ Branch 15 → 17 taken 23657 times.
29750 if (candidateThisType.hasAnyGenericParts())
416
1/2
✓ Branch 16 → 17 taken 6093 times.
✗ Branch 16 → 21 not taken.
6093 TypeMatcher::substantiateTypeWithTypeMapping(candidateThisType, typeMapping, callNode);
417
418 29750 return true;
419 29750 }
420
421 /**
422 * Checks if the matching candidate fulfills the argument types requirement
423 *
424 * @param candidate Matching candidate function
425 * @param reqArgs Requested argument types
426 * @param typeMapping Concrete template type mapping
427 * @param strictQualifierMatching Match qualifiers strictly
428 * @param needsSubstantiation We want to create a substantiation after successfully matching
429 * @param callNode Call AST node for printing error messages
430 * @return Fulfilled or not
431 */
432 95422 bool FunctionManager::matchArgTypes(Function &candidate, const ArgList &reqArgs, TypeMapping &typeMapping,
433 bool strictQualifierMatching, bool &needsSubstantiation, const ASTNode *callNode) {
434 95422 std::vector<Param> &candidateParamList = candidate.paramList;
435
436 // If the number of arguments does not match with the number of params, the matching fails
437
6/6
✓ Branch 2 → 3 taken 95282 times.
✓ Branch 2 → 7 taken 140 times.
✓ Branch 5 → 6 taken 12348 times.
✓ Branch 5 → 7 taken 82934 times.
✓ Branch 8 → 9 taken 12348 times.
✓ Branch 8 → 10 taken 83074 times.
95422 if (!candidate.isVararg && reqArgs.size() != candidateParamList.size())
438 12348 return false;
439 // In the case of a vararg function, we only disallow fewer arguments than parameters
440
4/6
✓ Branch 10 → 11 taken 140 times.
✓ Branch 10 → 15 taken 82934 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 140 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 83074 times.
83074 if (candidate.isVararg && reqArgs.size() < candidateParamList.size())
441 return false;
442
443 // Give the type matcher a way to retrieve instances of GenericType by their name
444 174066 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
445 7918 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
446 83074 };
447
448 // Loop over all parameters
449
2/2
✓ Branch 66 → 20 taken 89909 times.
✓ Branch 66 → 67 taken 15399 times.
105308 for (size_t i = 0; i < reqArgs.size(); i++) {
450 // In the case of a vararg function candidate, we can accept additional arguments, that are not defined in the candidate,
451 // but we need to modify the candidate param list to accept them
452
6/6
✓ Branch 20 → 21 taken 561 times.
✓ Branch 20 → 24 taken 89348 times.
✓ Branch 22 → 23 taken 144 times.
✓ Branch 22 → 24 taken 417 times.
✓ Branch 25 → 26 taken 144 times.
✓ Branch 25 → 29 taken 89765 times.
89909 if (candidate.isVararg && i >= candidateParamList.size()) {
453
2/4
✓ Branch 26 → 27 taken 144 times.
✗ Branch 26 → 71 not taken.
✓ Branch 27 → 28 taken 144 times.
✗ Branch 27 → 71 not taken.
144 candidateParamList.push_back(Param(reqArgs.at(i).first, false));
454 144 needsSubstantiation = true; // We need to modify the candidate param types
455 144 continue;
456 }
457
458 // Retrieve actual and requested types
459
2/4
✓ Branch 29 → 30 taken 89765 times.
✗ Branch 29 → 84 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 89765 times.
89765 assert(!candidateParamList.at(i).isOptional);
460
1/2
✓ Branch 32 → 33 taken 89765 times.
✗ Branch 32 → 84 not taken.
89765 QualType &candidateType = candidateParamList.at(i).qualType;
461
1/2
✓ Branch 33 → 34 taken 89765 times.
✗ Branch 33 → 84 not taken.
89765 const auto &[requestedType, isArgTemporary] = reqArgs.at(i);
462
463 // Check if the requested param type matches the candidate param type. The type mapping may be extended
464
3/4
✓ Branch 36 → 37 taken 89765 times.
✗ Branch 36 → 84 not taken.
✓ Branch 37 → 38 taken 67674 times.
✓ Branch 37 → 39 taken 22091 times.
89765 if (!TypeMatcher::matchRequestedToCandidateType(candidateType, requestedType, typeMapping, genericTypeResolver,
465 strictQualifierMatching))
466 67674 return false;
467
468 // Substantiate the candidate param type, based on the type mapping
469
3/4
✓ Branch 39 → 40 taken 22091 times.
✗ Branch 39 → 84 not taken.
✓ Branch 40 → 41 taken 6332 times.
✓ Branch 40 → 42 taken 15759 times.
22091 if (candidateType.hasAnyGenericParts())
470
1/2
✓ Branch 41 → 42 taken 6332 times.
✗ Branch 41 → 84 not taken.
6332 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, callNode);
471
472 // Check if we try to bind a non-ref temporary to a non-const ref parameter
473
3/4
✓ Branch 42 → 43 taken 22091 times.
✗ Branch 42 → 84 not taken.
✓ Branch 43 → 44 taken 1 time.
✓ Branch 43 → 54 taken 22090 times.
22091 if (!candidateType.canBind(requestedType, isArgTemporary)) {
474
1/2
✓ Branch 44 → 45 taken 1 time.
✗ Branch 44 → 53 not taken.
1 if (callNode)
475
2/4
✓ Branch 48 → 49 taken 1 time.
✗ Branch 48 → 75 not taken.
✓ Branch 49 → 50 taken 1 time.
✗ Branch 49 → 72 not taken.
3 throw SemanticError(callNode, TEMP_TO_NON_CONST_REF, "Temporary values can only be bound to const reference parameters");
476 return false;
477 }
478
479 // If we have a function/procedure type we need to take care of the information, if it takes captures
480
9/12
✓ Branch 54 → 55 taken 22090 times.
✗ Branch 54 → 81 not taken.
✓ Branch 55 → 56 taken 22090 times.
✗ Branch 55 → 81 not taken.
✓ Branch 56 → 57 taken 36 times.
✓ Branch 56 → 60 taken 22054 times.
✓ Branch 57 → 58 taken 36 times.
✗ Branch 57 → 81 not taken.
✓ Branch 58 → 59 taken 4 times.
✓ Branch 58 → 60 taken 32 times.
✓ Branch 61 → 62 taken 4 times.
✓ Branch 61 → 64 taken 22086 times.
22090 if (requestedType.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE}) && requestedType.hasLambdaCaptures()) {
481
1/2
✓ Branch 62 → 63 taken 4 times.
✗ Branch 62 → 83 not taken.
4 candidateType = candidateType.getWithLambdaCaptures();
482 4 needsSubstantiation = true;
483 }
484 }
485
486 15399 return true;
487 83074 }
488
489 /**
490 * Substantiates the candidate return type, based on the given type mapping
491 *
492 * @param candidate Matching candidate function
493 * @param typeMapping Concrete template type mapping
494 * @param callNode AST node for error messages
495 */
496 15398 void FunctionManager::substantiateReturnType(Function &candidate, const TypeMapping &typeMapping, const ASTNode *callNode) {
497
2/2
✓ Branch 3 → 4 taken 1339 times.
✓ Branch 3 → 5 taken 14059 times.
15398 if (candidate.returnType.hasAnyGenericParts())
498 1339 TypeMatcher::substantiateTypeWithTypeMapping(candidate.returnType, typeMapping, callNode);
499 15398 }
500
501 /**
502 * Searches the candidate template types for a generic type object with a certain name and return it
503 *
504 * @param candidate Matching candidate function
505 * @param templateTypeName Template type name
506 * @return Generic type object
507 */
508 13798 const GenericType *FunctionManager::getGenericTypeOfCandidateByName(const Function &candidate,
509 const std::string &templateTypeName) {
510
1/2
✓ Branch 19 → 4 taken 15846 times.
✗ Branch 19 → 20 not taken.
29644 for (const GenericType &templateType : candidate.templateTypes) {
511
3/4
✓ Branch 6 → 7 taken 15846 times.
✗ Branch 6 → 22 not taken.
✓ Branch 8 → 9 taken 13798 times.
✓ Branch 8 → 10 taken 2048 times.
15846 if (templateType.getSubType() == templateTypeName)
512 13798 return &templateType;
513 }
514 return nullptr;
515 }
516
517 /**
518 * Narrow a multi-match overload set by preferring the candidate whose parameter qualifiers most closely match
519 * the argument qualifiers. This resolves the typical copy-vs-move ctor ambiguity where both a `const T&`
520 * (copy) and a `T&` (move) ctor match a non-const lvalue argument - we prefer the non-const-ref candidate
521 * (move) since it requires no constification. When the argument is const, we prefer the const-ref candidate
522 * (copy) since binding to a non-const ref would require const-loss.
523 *
524 * Modifies `matches` in place, removing any candidate that scores worse than the best one. A no-op if there
525 * are fewer than two candidates.
526 *
527 * @param matches Candidate list to narrow
528 * @param reqArgs Argument list from the call site
529 */
530 13852 void FunctionManager::breakOverloadTie(std::vector<Function *> &matches, const ArgList &reqArgs) {
531
2/2
✓ Branch 3 → 4 taken 13845 times.
✓ Branch 3 → 5 taken 7 times.
13852 if (matches.size() < 2)
532 13845 return;
533
534 7 constexpr int CONSTIFY_PENALTY = 1;
535 7 constexpr int CONST_LOSS_PENALTY = 100;
536 35 const auto scoreSpecificity = [&](const Function *f) {
537 28 int penalty = 0;
538
2/2
✓ Branch 19 → 3 taken 28 times.
✓ Branch 19 → 20 taken 28 times.
56 for (size_t i = 0; i < std::min(reqArgs.size(), f->paramList.size()); i++) {
539
1/2
✓ Branch 3 → 4 taken 28 times.
✗ Branch 3 → 24 not taken.
28 const QualType &paramType = f->paramList.at(i).qualType;
540
1/2
✓ Branch 4 → 5 taken 28 times.
✗ Branch 4 → 24 not taken.
28 const QualType &argType = reqArgs.at(i).first;
541
2/4
✓ Branch 5 → 6 taken 28 times.
✗ Branch 5 → 22 not taken.
✓ Branch 6 → 7 taken 28 times.
✗ Branch 6 → 22 not taken.
28 const bool paramIsConst = paramType.removeReferenceWrapper().isConst();
542
2/4
✓ Branch 7 → 8 taken 28 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 28 times.
✗ Branch 8 → 23 not taken.
28 const bool argIsConst = argType.removeReferenceWrapper().isConst();
543
4/4
✓ Branch 9 → 10 taken 12 times.
✓ Branch 9 → 12 taken 16 times.
✓ Branch 10 → 11 taken 8 times.
✓ Branch 10 → 12 taken 4 times.
28 if (paramIsConst && !argIsConst)
544 8 penalty += CONSTIFY_PENALTY;
545
4/4
✓ Branch 12 → 13 taken 16 times.
✓ Branch 12 → 15 taken 4 times.
✓ Branch 13 → 14 taken 4 times.
✓ Branch 13 → 15 taken 12 times.
20 else if (!paramIsConst && argIsConst)
546 4 penalty += CONST_LOSS_PENALTY;
547 }
548 28 return penalty;
549 7 };
550
551 7 int bestScore = std::numeric_limits<int>::max();
552
2/2
✓ Branch 20 → 7 taken 14 times.
✓ Branch 20 → 21 taken 7 times.
28 for (const Function *m : matches)
553
1/2
✓ Branch 9 → 10 taken 14 times.
✗ Branch 9 → 46 not taken.
14 bestScore = std::min(bestScore, scoreSpecificity(m));
554 7 std::vector<Function *> filtered;
555
1/2
✓ Branch 22 → 23 taken 7 times.
✗ Branch 22 → 49 not taken.
7 filtered.reserve(matches.size());
556
2/2
✓ Branch 39 → 25 taken 14 times.
✓ Branch 39 → 40 taken 7 times.
28 for (Function *m : matches)
557
3/4
✓ Branch 27 → 28 taken 14 times.
✗ Branch 27 → 48 not taken.
✓ Branch 28 → 29 taken 8 times.
✓ Branch 28 → 30 taken 6 times.
14 if (scoreSpecificity(m) == bestScore)
558
1/2
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 48 not taken.
8 filtered.push_back(m);
559 7 matches = std::move(filtered);
560 7 }
561
562 /**
563 * Calculate the cache key for the function lookup cache
564 *
565 * @param scope Scope to match against
566 * @param name Function name requirement
567 * @param thisType This type requirement
568 * @param args Argument requirement
569 * @param templateTypes Template type requirement
570 * @return Cache key
571 */
572 144309 uint64_t FunctionManager::getCacheKey(const Scope *scope, const std::string &name, const QualType &thisType, const ArgList &args,
573 const QualTypeList &templateTypes) {
574 144309 uint64_t hash = 0;
575 144309 hashCombine64(hash, hashPointer(scope));
576 144309 hashCombine64(hash, std::hash<std::string>{}(name));
577 144309 hashCombine64(hash, std::hash<QualType>{}(thisType));
578
2/2
✓ Branch 27 → 10 taken 223402 times.
✓ Branch 27 → 28 taken 144309 times.
735422 for (const auto &[first, second] : args) {
579 223402 hashCombine64(hash, std::hash<QualType>{}(first));
580 223402 hashCombine64(hash, std::hash<bool>{}(second));
581 }
582 144309 hashCombine64(hash, hashVector(templateTypes));
583 144309 return hash;
584 }
585
586 3206 bool FunctionManager::hasCtor(const Scope *matchScope, CtorKind kind) {
587
5/8
✓ Branch 2 → 3 taken 3206 times.
✗ Branch 2 → 60 not taken.
✓ Branch 3 → 4 taken 3206 times.
✗ Branch 3 → 60 not taken.
✓ Branch 4 → 5 taken 3206 times.
✗ Branch 4 → 60 not taken.
✓ Branch 55 → 6 taken 17619 times.
✓ Branch 55 → 56 taken 2275 times.
19894 for (const auto &manifestations : matchScope->functions | std::views::values) {
588
5/8
✓ Branch 7 → 8 taken 17619 times.
✗ Branch 7 → 59 not taken.
✓ Branch 8 → 9 taken 17619 times.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 17619 times.
✗ Branch 9 → 59 not taken.
✓ Branch 51 → 11 taken 19599 times.
✓ Branch 51 → 52 taken 16688 times.
36287 for (const auto &function : manifestations | std::views::values) {
589 // If it is no ctor, skip it
590
3/4
✓ Branch 12 → 13 taken 19599 times.
✗ Branch 12 → 59 not taken.
✓ Branch 13 → 14 taken 13573 times.
✓ Branch 13 → 15 taken 6026 times.
19599 if (function.name != CTOR_FUNCTION_NAME)
591 13573 continue;
592 // Classify the ctor based on its parameter list
593
6/8
✓ Branch 16 → 17 taken 3340 times.
✓ Branch 16 → 25 taken 2686 times.
✓ Branch 17 → 18 taken 3340 times.
✗ Branch 17 → 58 not taken.
✓ Branch 18 → 19 taken 3340 times.
✗ Branch 18 → 58 not taken.
✓ Branch 19 → 20 taken 1222 times.
✓ Branch 19 → 25 taken 2118 times.
7248 const bool singleSelfRefParam = function.paramList.size() == 1 && function.paramList.at(0).qualType.isRef() &&
594
5/8
✓ Branch 20 → 21 taken 1222 times.
✗ Branch 20 → 58 not taken.
✓ Branch 21 → 22 taken 1222 times.
✗ Branch 21 → 58 not taken.
✓ Branch 22 → 23 taken 1222 times.
✗ Branch 22 → 58 not taken.
✓ Branch 23 → 24 taken 696 times.
✓ Branch 23 → 25 taken 526 times.
1222 function.paramList.at(0).qualType.getBase() == function.thisType;
595
6/8
✓ Branch 26 → 27 taken 696 times.
✓ Branch 26 → 31 taken 5330 times.
✓ Branch 27 → 28 taken 696 times.
✗ Branch 27 → 59 not taken.
✓ Branch 28 → 29 taken 696 times.
✗ Branch 28 → 59 not taken.
✓ Branch 29 → 30 taken 685 times.
✓ Branch 29 → 31 taken 11 times.
6026 const bool isCopyCtor = singleSelfRefParam && function.paramList.at(0).qualType.isConstRef();
596
6/8
✓ Branch 32 → 33 taken 696 times.
✓ Branch 32 → 37 taken 5330 times.
✓ Branch 33 → 34 taken 696 times.
✗ Branch 33 → 59 not taken.
✓ Branch 34 → 35 taken 696 times.
✗ Branch 34 → 59 not taken.
✓ Branch 35 → 36 taken 11 times.
✓ Branch 35 → 37 taken 685 times.
6026 const bool isMoveCtor = singleSelfRefParam && !function.paramList.at(0).qualType.isConstRef();
597
3/4
✓ Branch 38 → 39 taken 3608 times.
✓ Branch 38 → 42 taken 1777 times.
✓ Branch 38 → 45 taken 641 times.
✗ Branch 38 → 49 not taken.
6026 switch (kind) {
598 3608 case CtorKind::COPY:
599
2/2
✓ Branch 39 → 40 taken 337 times.
✓ Branch 39 → 41 taken 3271 times.
3608 if (isCopyCtor)
600 931 return true;
601 3271 break;
602 1777 case CtorKind::MOVE:
603
2/2
✓ Branch 42 → 43 taken 4 times.
✓ Branch 42 → 44 taken 1773 times.
1777 if (isMoveCtor)
604 4 return true;
605 1773 break;
606 641 case CtorKind::ANY_NON_COPY_NON_MOVE:
607
4/4
✓ Branch 45 → 46 taken 592 times.
✓ Branch 45 → 48 taken 49 times.
✓ Branch 46 → 47 taken 590 times.
✓ Branch 46 → 48 taken 2 times.
641 if (!isCopyCtor && !isMoveCtor)
608 590 return true;
609 51 break;
610 }
611 }
612 }
613 2275 return false;
614 }
615
616 658 bool FunctionManager::hasAnyNonCopyCtor(const Scope *matchScope) { return hasCtor(matchScope, CtorKind::ANY_NON_COPY_NON_MOVE); }
617
618 1746 bool FunctionManager::hasCopyCtor(const Scope *matchScope) { return hasCtor(matchScope, CtorKind::COPY); }
619
620 798 bool FunctionManager::hasUserCopyCtor(const Scope *matchScope) {
621
5/8
✓ Branch 2 → 3 taken 798 times.
✗ Branch 2 → 42 not taken.
✓ Branch 3 → 4 taken 798 times.
✗ Branch 3 → 42 not taken.
✓ Branch 4 → 5 taken 798 times.
✗ Branch 4 → 42 not taken.
✓ Branch 37 → 6 taken 3441 times.
✓ Branch 37 → 38 taken 660 times.
4101 for (const auto &manifestations : matchScope->functions | std::views::values) {
622
5/8
✓ Branch 7 → 8 taken 3441 times.
✗ Branch 7 → 41 not taken.
✓ Branch 8 → 9 taken 3441 times.
✗ Branch 8 → 41 not taken.
✓ Branch 9 → 10 taken 3441 times.
✗ Branch 9 → 41 not taken.
✓ Branch 34 → 11 taken 3729 times.
✓ Branch 34 → 35 taken 3303 times.
7032 for (const auto &function : manifestations | std::views::values) {
623
7/8
✓ Branch 12 → 13 taken 3729 times.
✗ Branch 12 → 41 not taken.
✓ Branch 13 → 14 taken 1641 times.
✓ Branch 13 → 15 taken 2088 times.
✓ Branch 14 → 15 taken 197 times.
✓ Branch 14 → 16 taken 1444 times.
✓ Branch 17 → 18 taken 2285 times.
✓ Branch 17 → 19 taken 1444 times.
3729 if (function.name != CTOR_FUNCTION_NAME || function.implicitDefault)
624 2285 continue;
625
6/8
✓ Branch 20 → 21 taken 885 times.
✓ Branch 20 → 29 taken 559 times.
✓ Branch 21 → 22 taken 885 times.
✗ Branch 21 → 40 not taken.
✓ Branch 22 → 23 taken 885 times.
✗ Branch 22 → 40 not taken.
✓ Branch 23 → 24 taken 176 times.
✓ Branch 23 → 29 taken 709 times.
1620 const bool isCopyCtor = function.paramList.size() == 1 && function.paramList.at(0).qualType.isConstRef() &&
626
5/8
✓ Branch 24 → 25 taken 176 times.
✗ Branch 24 → 40 not taken.
✓ Branch 25 → 26 taken 176 times.
✗ Branch 25 → 40 not taken.
✓ Branch 26 → 27 taken 176 times.
✗ Branch 26 → 40 not taken.
✓ Branch 27 → 28 taken 138 times.
✓ Branch 27 → 29 taken 38 times.
176 function.paramList.at(0).qualType.getBase() == function.thisType;
627
2/2
✓ Branch 30 → 31 taken 138 times.
✓ Branch 30 → 32 taken 1306 times.
1444 if (isCopyCtor)
628 138 return true;
629 }
630 }
631 660 return false;
632 }
633
634 802 bool FunctionManager::hasMoveCtor(const Scope *matchScope) { return hasCtor(matchScope, CtorKind::MOVE); }
635
636 4582 Function *FunctionManager::findMoveCtor(Scope *matchScope) {
637
5/8
✓ Branch 2 → 3 taken 4582 times.
✗ Branch 2 → 41 not taken.
✓ Branch 3 → 4 taken 4582 times.
✗ Branch 3 → 41 not taken.
✓ Branch 4 → 5 taken 4582 times.
✗ Branch 4 → 41 not taken.
✓ Branch 36 → 6 taken 46212 times.
✓ Branch 36 → 37 taken 4535 times.
50747 for (auto &manifestations : matchScope->functions | std::views::values) {
638
5/8
✓ Branch 7 → 8 taken 46212 times.
✗ Branch 7 → 40 not taken.
✓ Branch 8 → 9 taken 46212 times.
✗ Branch 8 → 40 not taken.
✓ Branch 9 → 10 taken 46212 times.
✗ Branch 9 → 40 not taken.
✓ Branch 33 → 11 taken 61978 times.
✓ Branch 33 → 34 taken 46165 times.
108143 for (auto &function : manifestations | std::views::values) {
639
3/4
✓ Branch 12 → 13 taken 61978 times.
✗ Branch 12 → 40 not taken.
✓ Branch 13 → 14 taken 48698 times.
✓ Branch 13 → 15 taken 13280 times.
61978 if (function.name != CTOR_FUNCTION_NAME)
640 48698 continue;
641
4/6
✓ Branch 17 → 18 taken 8848 times.
✗ Branch 17 → 39 not taken.
✓ Branch 18 → 19 taken 8848 times.
✗ Branch 18 → 39 not taken.
✓ Branch 19 → 20 taken 4906 times.
✓ Branch 19 → 28 taken 3942 times.
22128 const bool isMoveCtor = function.paramList.size() == 1 && function.paramList.at(0).qualType.isRef() &&
642
6/8
✓ Branch 16 → 17 taken 8848 times.
✓ Branch 16 → 28 taken 4432 times.
✓ Branch 20 → 21 taken 4906 times.
✗ Branch 20 → 39 not taken.
✓ Branch 21 → 22 taken 4906 times.
✗ Branch 21 → 39 not taken.
✓ Branch 22 → 23 taken 991 times.
✓ Branch 22 → 28 taken 3915 times.
23119 !function.paramList.at(0).qualType.isConstRef() &&
643
5/8
✓ Branch 23 → 24 taken 991 times.
✗ Branch 23 → 39 not taken.
✓ Branch 24 → 25 taken 991 times.
✗ Branch 24 → 39 not taken.
✓ Branch 25 → 26 taken 991 times.
✗ Branch 25 → 39 not taken.
✓ Branch 26 → 27 taken 47 times.
✓ Branch 26 → 28 taken 944 times.
991 function.paramList.at(0).qualType.getBase() == function.thisType;
644
2/2
✓ Branch 29 → 30 taken 47 times.
✓ Branch 29 → 31 taken 13233 times.
13280 if (isMoveCtor)
645 47 return &function;
646 }
647 }
648 4535 return nullptr;
649 }
650
651 7258 bool FunctionManager::hasDtor(const Scope *matchScope) {
652
5/8
✓ Branch 2 → 3 taken 7258 times.
✗ Branch 2 → 24 not taken.
✓ Branch 3 → 4 taken 7258 times.
✗ Branch 3 → 24 not taken.
✓ Branch 4 → 5 taken 7258 times.
✗ Branch 4 → 24 not taken.
✓ Branch 20 → 6 taken 49910 times.
✓ Branch 20 → 21 taken 4599 times.
54509 for (const auto &manifestations : matchScope->functions | std::views::values)
653
5/8
✓ Branch 7 → 8 taken 49910 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 49910 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 49910 times.
✗ Branch 9 → 23 not taken.
✓ Branch 17 → 11 taken 57030 times.
✓ Branch 17 → 18 taken 47251 times.
104281 for (const auto &function : manifestations | std::views::values)
654
3/4
✓ Branch 12 → 13 taken 57030 times.
✗ Branch 12 → 23 not taken.
✓ Branch 13 → 14 taken 2659 times.
✓ Branch 13 → 15 taken 54371 times.
57030 if (function.name == DTOR_FUNCTION_NAME)
655 2659 return true;
656 4599 return false;
657 }
658
659 /**
660 * Clear the lookup cache
661 */
662 510 void FunctionManager::cleanup() {
663 510 lookupCache.clear();
664 510 lookupCacheHits = 0;
665 510 lookupCacheMisses = 0;
666 510 }
667
668 /**
669 * Dump usage statistics for the lookup cache
670 */
671 273 std::string FunctionManager::dumpLookupCacheStatistics() {
672
1/2
✓ Branch 2 → 3 taken 273 times.
✗ Branch 2 → 22 not taken.
273 std::stringstream stats;
673
2/4
✓ Branch 3 → 4 taken 273 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 273 times.
✗ Branch 4 → 20 not taken.
273 stats << "FunctionManager lookup cache statistics:" << std::endl;
674
3/6
✓ Branch 5 → 6 taken 273 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 273 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 273 times.
✗ Branch 8 → 20 not taken.
273 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
675
3/6
✓ Branch 9 → 10 taken 273 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 273 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 273 times.
✗ Branch 11 → 20 not taken.
273 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
676
3/6
✓ Branch 12 → 13 taken 273 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 273 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 273 times.
✗ Branch 14 → 20 not taken.
273 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
677
1/2
✓ Branch 15 → 16 taken 273 times.
✗ Branch 15 → 20 not taken.
546 return stats.str();
678 273 }
679
680 } // namespace spice::compiler
681