GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.4% 250 / 0 / 254
Functions: 100.0% 18 / 0 / 18
Branches: 64.9% 287 / 0 / 442

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 <ast/ASTNodes.h>
6 #include <exception/SemanticError.h>
7 #include <model/GenericType.h>
8 #include <symboltablebuilder/Scope.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/TypeChecker.h>
11 #include <typechecker/TypeMatcher.h>
12 #include <util/CodeLoc.h>
13 #include <util/CustomHashFunctions.h>
14
15 namespace spice::compiler {
16
17 // Static member initialization
18 std::unordered_map<uint64_t, Function *> FunctionManager::lookupCache = {};
19 size_t FunctionManager::lookupCacheHits = 0;
20 size_t FunctionManager::lookupCacheMisses = 0;
21
22 14128 Function *FunctionManager::insert(Scope *insertScope, const Function &baseFunction, std::vector<Function *> *nodeFunctionList) {
23 // Open a new manifestation list for the function definition
24
3/6
✓ Branch 2 → 3 taken 14128 times.
✗ Branch 2 → 43 not taken.
✓ Branch 3 → 4 taken 14128 times.
✗ Branch 3 → 40 not taken.
✓ Branch 4 → 5 taken 14128 times.
✗ Branch 4 → 38 not taken.
14128 const std::string fctId = baseFunction.name + ":" + baseFunction.declNode->codeLoc.toPrettyLineAndColumn();
25
2/4
✓ Branch 8 → 9 taken 14128 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 14128 times.
✗ Branch 9 → 44 not taken.
14128 insertScope->functions.insert({fctId, FunctionManifestationList()});
26
27 // Collect substantiations
28 14128 std::vector<Function> manifestations;
29
1/2
✓ Branch 12 → 13 taken 14128 times.
✗ Branch 12 → 51 not taken.
14128 substantiateOptionalParams(baseFunction, manifestations);
30
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 14128 times.
14128 assert(!manifestations.empty());
31
32 // Save substantiations in declaration node
33 14128 Function *manifestationPtr = nullptr;
34
2/2
✓ Branch 26 → 18 taken 15077 times.
✓ Branch 26 → 27 taken 14126 times.
29203 for (const Function &manifestation : manifestations) {
35
2/2
✓ Branch 19 → 20 taken 15075 times.
✓ Branch 19 → 50 taken 2 times.
15077 manifestationPtr = insertSubstantiation(insertScope, manifestation, baseFunction.declNode);
36
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 15075 times.
15075 assert(manifestationPtr != nullptr);
37
1/2
✓ Branch 22 → 23 taken 15075 times.
✗ Branch 22 → 24 not taken.
15075 if (nodeFunctionList)
38
1/2
✓ Branch 23 → 24 taken 15075 times.
✗ Branch 23 → 50 not taken.
15075 nodeFunctionList->push_back(manifestationPtr);
39 }
40
41
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 14126 times.
14126 if (!nodeFunctionList)
42 return manifestationPtr;
43
44
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 14126 times.
14126 assert(!nodeFunctionList->empty());
45 14126 return nodeFunctionList->front();
46 14130 }
47
48 /**
49 * Create definite functions from ambiguous ones, in regard to optional arguments.
50 *
51 * Example:
52 * int testFunc(string, int?, double?)
53 * gets
54 * int testFunc(string)
55 * int testFunc(string, int)
56 * int testFunc(string, int, double)
57 *
58 * This method also accepts functions, that are already definite, but does nothing to them.
59 *
60 * @param baseFunction Ambiguous base function
61 * @param manifestations Vector to store the definite manifestations
62 * @return True, if there were optional arguments found
63 */
64 14128 void FunctionManager::substantiateOptionalParams(const Function &baseFunction, std::vector<Function> &manifestations) {
65 // Handle the case of no parameters -> simply return the base function
66
2/2
✓ Branch 3 → 4 taken 3441 times.
✓ Branch 3 → 6 taken 10687 times.
14128 if (baseFunction.paramList.empty()) {
67
1/2
✓ Branch 4 → 5 taken 3441 times.
✗ Branch 4 → 39 not taken.
3441 manifestations.push_back(baseFunction);
68 3441 return;
69 }
70
71 10687 ParamList currentFunctionParamTypes;
72
1/2
✓ Branch 7 → 8 taken 10687 times.
✗ Branch 7 → 37 not taken.
10687 currentFunctionParamTypes.reserve(baseFunction.paramList.size());
73 10687 bool metFirstOptionalParam = false;
74
1/2
✓ Branch 8 → 9 taken 10687 times.
✗ Branch 8 → 37 not taken.
10687 Function manifestation = baseFunction;
75
76 // Loop over all parameters
77
2/2
✓ Branch 24 → 11 taken 16672 times.
✓ Branch 24 → 25 taken 10687 times.
27359 for (const auto &[qualType, isOptional] : baseFunction.paramList) {
78 // Check if we have a mandatory parameter
79
2/2
✓ Branch 12 → 13 taken 15723 times.
✓ Branch 12 → 15 taken 949 times.
16672 if (!isOptional) {
80
1/2
✓ Branch 13 → 14 taken 15723 times.
✗ Branch 13 → 32 not taken.
15723 currentFunctionParamTypes.push_back({qualType, /*optional=*/false});
81 15723 continue;
82 }
83
84 // Add substantiation without the optional parameter
85
2/2
✓ Branch 15 → 16 taken 943 times.
✓ Branch 15 → 19 taken 6 times.
949 if (!metFirstOptionalParam) {
86
1/2
✓ Branch 16 → 17 taken 943 times.
✗ Branch 16 → 34 not taken.
943 manifestation.paramList = currentFunctionParamTypes;
87
1/2
✓ Branch 17 → 18 taken 943 times.
✗ Branch 17 → 34 not taken.
943 manifestations.push_back(manifestation);
88 // Now we cannot accept mandatory parameters anymore
89 943 metFirstOptionalParam = true;
90 }
91
92 // Add substantiation with the optional parameter
93
1/2
✓ Branch 19 → 20 taken 949 times.
✗ Branch 19 → 33 not taken.
949 currentFunctionParamTypes.push_back({qualType, /*optional=*/false});
94
1/2
✓ Branch 20 → 21 taken 949 times.
✗ Branch 20 → 34 not taken.
949 manifestation.paramList = currentFunctionParamTypes;
95
1/2
✓ Branch 21 → 22 taken 949 times.
✗ Branch 21 → 34 not taken.
949 manifestations.push_back(manifestation);
96 }
97
98 // Ensure at least once manifestation
99
2/2
✓ Branch 26 → 27 taken 9744 times.
✓ Branch 26 → 28 taken 943 times.
10687 if (manifestations.empty())
100
1/2
✓ Branch 27 → 28 taken 9744 times.
✗ Branch 27 → 35 not taken.
9744 manifestations.push_back(baseFunction);
101 10687 }
102
103 6 Function FunctionManager::createMainFunction(SymbolTableEntry *entry, const QualTypeList &paramTypes, ASTNode *declNode) {
104 6 ParamList paramList;
105
2/2
✓ Branch 8 → 4 taken 2 times.
✓ Branch 8 → 9 taken 6 times.
8 for (const QualType &paramType : paramTypes)
106
1/2
✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 24 not taken.
2 paramList.push_back({paramType, false});
107
4/8
✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 31 not taken.
✓ Branch 12 → 13 taken 6 times.
✗ Branch 12 → 28 not taken.
✓ Branch 13 → 14 taken 6 times.
✗ Branch 13 → 27 not taken.
✓ Branch 14 → 15 taken 6 times.
✗ Branch 14 → 26 not taken.
12 return {MAIN_FUNCTION_NAME, entry, QualType(TY_DYN), QualType(TY_INT), paramList, {}, declNode};
108 6 }
109
110 18094 Function *FunctionManager::insertSubstantiation(Scope *insertScope, const Function &newManifestation, const ASTNode *declNode) {
111
2/4
✓ Branch 2 → 3 taken 18094 times.
✗ Branch 2 → 70 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 18094 times.
18094 assert(newManifestation.hasSubstantiatedParams());
112
113
1/2
✓ Branch 5 → 6 taken 18094 times.
✗ Branch 5 → 70 not taken.
18094 const std::string signature = newManifestation.getSignature(true, false, false);
114
115 // Check if the function exists already
116
5/8
✓ Branch 6 → 7 taken 18094 times.
✗ Branch 6 → 59 not taken.
✓ Branch 7 → 8 taken 18094 times.
✗ Branch 7 → 59 not taken.
✓ Branch 8 → 9 taken 18094 times.
✗ Branch 8 → 59 not taken.
✓ Branch 30 → 10 taken 291078 times.
✓ Branch 30 → 31 taken 18091 times.
309169 for (const auto &manifestations : insertScope->functions | std::views::values) {
117
3/4
✓ Branch 11 → 12 taken 291078 times.
✗ Branch 11 → 59 not taken.
✓ Branch 12 → 13 taken 3 times.
✓ Branch 12 → 28 taken 291075 times.
291078 if (manifestations.contains(signature)) {
118
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 2 times.
3 const SemanticErrorType errorType = newManifestation.isFunction() ? FUNCTION_DECLARED_TWICE : PROCEDURE_DECLARED_TWICE;
119
4/8
✓ Branch 20 → 21 taken 3 times.
✗ Branch 20 → 53 not taken.
✓ Branch 21 → 22 taken 3 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 3 times.
✗ Branch 22 → 49 not taken.
✓ Branch 23 → 24 taken 3 times.
✗ Branch 23 → 47 not taken.
3 throw SemanticError(declNode, errorType, "'" + newManifestation.getSignature(true, false) + "' is declared twice");
120 }
121 }
122
123 // Retrieve the matching manifestation list of the scope
124
3/6
✓ Branch 31 → 32 taken 18091 times.
✗ Branch 31 → 65 not taken.
✓ Branch 32 → 33 taken 18091 times.
✗ Branch 32 → 62 not taken.
✓ Branch 33 → 34 taken 18091 times.
✗ Branch 33 → 60 not taken.
18091 const std::string fctId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn();
125
2/4
✓ Branch 36 → 37 taken 18091 times.
✗ Branch 36 → 66 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 18091 times.
18091 assert(insertScope->functions.contains(fctId));
126
1/2
✓ Branch 39 → 40 taken 18091 times.
✗ Branch 39 → 66 not taken.
18091 FunctionManifestationList &manifestationList = insertScope->functions.at(fctId);
127
128 // Add substantiated function
129
1/2
✓ Branch 40 → 41 taken 18091 times.
✗ Branch 40 → 66 not taken.
18091 manifestationList.emplace(signature, newManifestation);
130
1/2
✓ Branch 41 → 42 taken 18091 times.
✗ Branch 41 → 66 not taken.
36182 return &manifestationList.at(signature);
131 18094 }
132
133 /**
134 * Checks if a function exists by matching it, but not setting it to used
135 *
136 * @param matchScope Scope to match against
137 * @param reqName Function name requirement
138 * @param reqThisType This type requirement
139 * @param reqArgs Argument requirement
140 * @param strictQualifierMatching Match argument and this type qualifiers strictly
141 * @return Found function or nullptr
142 */
143 13249 const Function *FunctionManager::lookup(Scope *matchScope, const std::string &reqName, const QualType &reqThisType,
144 const ArgList &reqArgs, bool strictQualifierMatching) {
145
2/4
✓ Branch 2 → 3 taken 13249 times.
✗ Branch 2 → 66 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 13249 times.
13249 assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT}));
146
147 // Do cache lookup
148 13249 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, {});
149
3/4
✓ Branch 8 → 9 taken 13249 times.
✗ Branch 8 → 80 not taken.
✓ Branch 9 → 10 taken 3771 times.
✓ Branch 9 → 12 taken 9478 times.
13249 if (lookupCache.contains(cacheKey)) {
150 3771 lookupCacheHits++;
151
1/2
✓ Branch 10 → 11 taken 3771 times.
✗ Branch 10 → 80 not taken.
3771 return lookupCache.at(cacheKey);
152 }
153 9478 lookupCacheMisses++;
154
155 2791 const auto pred = [&](const Arg &arg) { return arg.first.hasAnyGenericParts(); };
156
5/8
✓ Branch 12 → 13 taken 9478 times.
✗ Branch 12 → 80 not taken.
✓ Branch 13 → 14 taken 9095 times.
✓ Branch 13 → 17 taken 383 times.
✓ Branch 14 → 15 taken 9095 times.
✗ Branch 14 → 80 not taken.
✓ Branch 15 → 16 taken 9095 times.
✗ Branch 15 → 17 not taken.
9478 const bool requestedFullySubstantiated = !reqThisType.hasAnyGenericParts() && std::ranges::none_of(reqArgs, pred);
157
158 // Copy the registry to prevent iterating over items, that are created within the loop
159
1/2
✓ Branch 18 → 19 taken 9478 times.
✗ Branch 18 → 80 not taken.
9478 const FunctionRegistry functionRegistry = matchScope->functions;
160 // Loop over function registry to find functions, that match the requirements of the call
161 9478 std::vector<const Function *> matches;
162
2/2
✓ Branch 55 → 21 taken 78537 times.
✓ Branch 55 → 56 taken 9478 times.
88015 for (const auto &[defCodeLocStr, m] : functionRegistry) {
163 // Copy the manifestation list to prevent iterating over items, that are created within the loop
164
1/2
✓ Branch 24 → 25 taken 78537 times.
✗ Branch 24 → 74 not taken.
78537 const FunctionManifestationList manifestations = m;
165
2/2
✓ Branch 51 → 27 taken 88730 times.
✓ Branch 51 → 52 taken 28311 times.
117041 for (const auto &[signature, presetFunction] : manifestations) {
166
2/4
✓ Branch 30 → 31 taken 88730 times.
✗ Branch 30 → 70 not taken.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 88730 times.
88730 assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point
167
168 // - search for concrete fct: Only match against fully substantiated versions to prevent double matching of a function
169 // - search for generic fct: Only match against generic preset functions
170
3/4
✓ Branch 33 → 34 taken 88730 times.
✗ Branch 33 → 70 not taken.
✓ Branch 34 → 35 taken 31697 times.
✓ Branch 34 → 36 taken 57033 times.
88730 if (presetFunction.isFullySubstantiated() != requestedFullySubstantiated)
171 38504 continue;
172
173 // Copy the function to be able to substantiate types
174
1/2
✓ Branch 36 → 37 taken 57033 times.
✗ Branch 36 → 70 not taken.
57033 Function candidate = presetFunction;
175
176 // Create empty type mapping
177 57033 TypeMapping &typeMapping = candidate.typeMapping;
178
179 57033 bool forceSubstantiation = false;
180
1/2
✓ Branch 37 → 38 taken 57033 times.
✗ Branch 37 → 68 not taken.
57033 const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping,
181 strictQualifierMatching, forceSubstantiation, nullptr);
182
2/2
✓ Branch 38 → 39 taken 48761 times.
✓ Branch 38 → 40 taken 8272 times.
57033 if (matchResult == MatchResult::SKIP_FUNCTION)
183 48761 break; // Leave the whole function
184
2/2
✓ Branch 40 → 41 taken 6807 times.
✓ Branch 40 → 42 taken 1465 times.
8272 if (matchResult == MatchResult::SKIP_MANIFESTATION)
185 6807 continue; // Leave this manifestation and try the next one
186
187 // Add to matches
188
3/6
✓ Branch 42 → 43 taken 1465 times.
✗ Branch 42 → 67 not taken.
✓ Branch 43 → 44 taken 1465 times.
✗ Branch 43 → 67 not taken.
✓ Branch 44 → 45 taken 1465 times.
✗ Branch 44 → 67 not taken.
1465 matches.push_back(&matchScope->functions.at(defCodeLocStr).at(signature));
189
190 1465 break; // Leave the whole manifestation list to not double-match the manifestation
191
2/2
✓ Branch 47 → 48 taken 6807 times.
✓ Branch 47 → 49 taken 50226 times.
57033 }
192 78537 }
193
194 // Return the very match or a nullptr
195
2/2
✓ Branch 57 → 58 taken 1465 times.
✓ Branch 57 → 60 taken 8013 times.
9478 return !matches.empty() ? matches.front() : nullptr;
196 9478 }
197
198 /**
199 * Check if there is a function in the scope, fulfilling all given requirements and if found, return it.
200 * If more than one function matches the requirement, an error gets thrown.
201 *
202 * @param matchScope Scope to match against
203 * @param reqName Function name requirement
204 * @param reqThisType This type requirement
205 * @param reqArgs Argument requirement
206 * @param templateTypeHints Template type requirement
207 * @param strictQualifierMatching Match argument and this type qualifiers strictly
208 * @param callNode Call AST node for printing error messages
209 * @return Matched function or nullptr
210 */
211 80495 Function *FunctionManager::match(Scope *matchScope, const std::string &reqName, const QualType &reqThisType,
212 const ArgList &reqArgs, const QualTypeList &templateTypeHints, bool strictQualifierMatching,
213 const ASTNode *callNode) {
214
2/4
✓ Branch 2 → 3 taken 80495 times.
✗ Branch 2 → 153 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 80495 times.
80495 assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT, TY_INTERFACE}));
215
216 // Do cache lookup
217 80495 const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, templateTypeHints);
218
3/4
✓ Branch 6 → 7 taken 80495 times.
✗ Branch 6 → 188 not taken.
✓ Branch 7 → 8 taken 12893 times.
✓ Branch 7 → 10 taken 67602 times.
80495 if (lookupCache.contains(cacheKey)) {
219 12893 lookupCacheHits++;
220
1/2
✓ Branch 8 → 9 taken 12893 times.
✗ Branch 8 → 188 not taken.
12893 return lookupCache.at(cacheKey);
221 }
222 67602 lookupCacheMisses++;
223
224 // Copy the registry to prevent iterating over items, that are created within the loop
225
1/2
✓ Branch 10 → 11 taken 67602 times.
✗ Branch 10 → 188 not taken.
67602 FunctionRegistry functionRegistry = matchScope->functions;
226 // Loop over function registry to find functions, that match the requirements of the call
227 67602 std::vector<Function *> matches;
228
2/2
✓ Branch 138 → 13 taken 907029 times.
✓ Branch 138 → 139 taken 67600 times.
974629 for (const auto &[fctId, m] : functionRegistry) {
229 // Copy the manifestation list to prevent iterating over items, that are created within the loop
230
1/2
✓ Branch 16 → 17 taken 907029 times.
✗ Branch 16 → 182 not taken.
907029 const FunctionManifestationList manifestations = m;
231
2/2
✓ Branch 134 → 19 taken 916955 times.
✓ Branch 134 → 135 taken 50092 times.
967047 for (const auto &[signature, presetFunction] : manifestations) {
232
2/4
✓ Branch 22 → 23 taken 916955 times.
✗ Branch 22 → 178 not taken.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 916955 times.
916955 assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point
233
234 // Skip generic substantiations to prevent double matching of a function
235
3/4
✓ Branch 25 → 26 taken 916955 times.
✗ Branch 25 → 178 not taken.
✓ Branch 26 → 27 taken 7745 times.
✓ Branch 26 → 28 taken 909210 times.
916955 if (presetFunction.isGenericSubstantiation())
236 60018 continue;
237
238 // Copy the function to be able to substantiate types
239
1/2
✓ Branch 28 → 29 taken 909210 times.
✗ Branch 28 → 178 not taken.
909210 Function candidate = presetFunction;
240
241 // Prepare type mapping, based on the given initial type mapping
242 909210 TypeMapping &typeMapping = candidate.typeMapping;
243 909210 typeMapping.clear();
244
2/2
✓ Branch 41 → 31 taken 41787 times.
✓ Branch 41 → 42 taken 909210 times.
950997 for (size_t i = 0; i < std::min(templateTypeHints.size(), candidate.templateTypes.size()); i++) {
245
2/4
✓ Branch 31 → 32 taken 41787 times.
✗ Branch 31 → 176 not taken.
✓ Branch 32 → 33 taken 41787 times.
✗ Branch 32 → 176 not taken.
41787 const std::string &typeName = candidate.templateTypes.at(i).getSubType();
246
1/2
✓ Branch 33 → 34 taken 41787 times.
✗ Branch 33 → 176 not taken.
41787 const QualType &templateType = templateTypeHints.at(i);
247
2/4
✓ Branch 34 → 35 taken 41787 times.
✗ Branch 34 → 156 not taken.
✓ Branch 35 → 36 taken 41787 times.
✗ Branch 35 → 154 not taken.
41787 typeMapping.insert({typeName, templateType});
248 }
249
250 909210 bool forceSubstantiation = false;
251
2/2
✓ Branch 42 → 43 taken 909209 times.
✓ Branch 42 → 176 taken 1 time.
909210 const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping,
252 strictQualifierMatching, forceSubstantiation, callNode);
253
2/2
✓ Branch 43 → 44 taken 853540 times.
✓ Branch 43 → 45 taken 55669 times.
909209 if (matchResult == MatchResult::SKIP_FUNCTION)
254 853540 break; // Leave the whole function
255
2/2
✓ Branch 45 → 46 taken 46400 times.
✓ Branch 45 → 47 taken 9269 times.
55669 if (matchResult == MatchResult::SKIP_MANIFESTATION)
256 46400 continue; // Leave this manifestation and try the next one
257
258 // We found a match! -> Set the actual candidate and its entry to used
259 9269 candidate.used = true;
260 9269 candidate.entry->used = true;
261
262 // Check if the function is generic needs to be substantiated
263
6/6
✓ Branch 48 → 49 taken 5953 times.
✓ Branch 48 → 51 taken 3316 times.
✓ Branch 49 → 50 taken 5873 times.
✓ Branch 49 → 51 taken 80 times.
✓ Branch 52 → 53 taken 5873 times.
✓ Branch 52 → 65 taken 3396 times.
9269 if (presetFunction.templateTypes.empty() && !forceSubstantiation) {
264
5/10
✓ Branch 53 → 54 taken 5873 times.
✗ Branch 53 → 176 not taken.
✓ Branch 54 → 55 taken 5873 times.
✗ Branch 54 → 59 not taken.
✓ Branch 55 → 56 taken 5873 times.
✗ Branch 55 → 176 not taken.
✓ Branch 56 → 57 taken 5873 times.
✗ Branch 56 → 176 not taken.
✓ Branch 57 → 58 taken 5873 times.
✗ Branch 57 → 59 not taken.
5873 assert(matchScope->functions.contains(fctId) && matchScope->functions.at(fctId).contains(signature));
265
3/6
✓ Branch 60 → 61 taken 5873 times.
✗ Branch 60 → 157 not taken.
✓ Branch 61 → 62 taken 5873 times.
✗ Branch 61 → 157 not taken.
✓ Branch 62 → 63 taken 5873 times.
✗ Branch 62 → 157 not taken.
5873 matches.push_back(&matchScope->functions.at(fctId).at(signature));
266 5873 matches.back()->used = true;
267 5873 continue; // Match was successful -> match the next function
268 }
269
270 // Check if we already have this manifestation and can simply re-use it
271
1/2
✓ Branch 65 → 66 taken 3396 times.
✗ Branch 65 → 176 not taken.
3396 const std::string nonGenericSignature = candidate.getSignature(true, false, false);
272
4/6
✓ Branch 66 → 67 taken 3396 times.
✗ Branch 66 → 174 not taken.
✓ Branch 67 → 68 taken 3396 times.
✗ Branch 67 → 174 not taken.
✓ Branch 68 → 69 taken 379 times.
✓ Branch 68 → 74 taken 3017 times.
3396 if (matchScope->functions.at(fctId).contains(nonGenericSignature)) {
273
3/6
✓ Branch 69 → 70 taken 379 times.
✗ Branch 69 → 158 not taken.
✓ Branch 70 → 71 taken 379 times.
✗ Branch 70 → 158 not taken.
✓ Branch 71 → 72 taken 379 times.
✗ Branch 71 → 158 not taken.
379 matches.push_back(&matchScope->functions.at(fctId).at(nonGenericSignature));
274 379 matches.back()->used = true;
275 379 break; // Leave the whole manifestation list to not double-match the manifestation
276 }
277
278 // Insert the substantiated version if required
279
2/2
✓ Branch 74 → 75 taken 3016 times.
✓ Branch 74 → 174 taken 1 time.
3017 Function *substantiatedFunction = insertSubstantiation(matchScope, candidate, presetFunction.declNode);
280
2/4
✓ Branch 75 → 76 taken 3016 times.
✗ Branch 75 → 174 not taken.
✓ Branch 76 → 77 taken 3016 times.
✗ Branch 76 → 174 not taken.
3016 substantiatedFunction->genericPreset = &matchScope->functions.at(fctId).at(signature);
281 3016 substantiatedFunction->alreadyTypeChecked = false;
282
2/4
✓ Branch 77 → 78 taken 3016 times.
✗ Branch 77 → 174 not taken.
✓ Branch 78 → 79 taken 3016 times.
✗ Branch 78 → 174 not taken.
3016 substantiatedFunction->declNode->getFctManifestations(reqName)->push_back(substantiatedFunction);
283
284 // Copy function entry
285
1/2
✓ Branch 79 → 80 taken 3016 times.
✗ Branch 79 → 174 not taken.
3016 const std::string newScopeName = substantiatedFunction->getScopeName();
286
1/2
✓ Branch 80 → 81 taken 3016 times.
✗ Branch 80 → 172 not taken.
3016 matchScope->lookupStrict(presetFunction.entry->name)->used = true;
287
1/2
✓ Branch 83 → 84 taken 3016 times.
✗ Branch 83 → 172 not taken.
3016 substantiatedFunction->entry = matchScope->symbolTable.copySymbol(presetFunction.entry->name, newScopeName);
288
1/2
✗ Branch 84 → 85 not taken.
✓ Branch 84 → 86 taken 3016 times.
3016 assert(substantiatedFunction->entry != nullptr);
289
290 // Copy function scope
291
1/2
✓ Branch 86 → 87 taken 3016 times.
✗ Branch 86 → 172 not taken.
3016 const std::string oldScopeName = presetFunction.getScopeName();
292
1/2
✓ Branch 87 → 88 taken 3016 times.
✗ Branch 87 → 170 not taken.
3016 Scope *childScope = matchScope->copyChildScope(oldScopeName, newScopeName);
293
1/2
✗ Branch 88 → 89 not taken.
✓ Branch 88 → 90 taken 3016 times.
3016 assert(childScope != nullptr);
294 3016 childScope->isGenericScope = false;
295 3016 substantiatedFunction->bodyScope = childScope;
296
297 // Insert symbols for generic type names with concrete types into the child block
298
2/2
✓ Branch 100 → 92 taken 4228 times.
✓ Branch 100 → 101 taken 3016 times.
7244 for (const auto &[typeName, concreteType] : substantiatedFunction->typeMapping)
299
2/4
✓ Branch 95 → 96 taken 4228 times.
✗ Branch 95 → 161 not taken.
✓ Branch 96 → 97 taken 4228 times.
✗ Branch 96 → 159 not taken.
4228 childScope->insertGenericType(typeName, GenericType(concreteType));
300
301 // Substantiate the 'this' entry in the new function scope
302
6/6
✓ Branch 104 → 105 taken 1888 times.
✓ Branch 104 → 108 taken 1128 times.
✓ Branch 106 → 107 taken 1887 times.
✓ Branch 106 → 108 taken 1 time.
✓ Branch 109 → 110 taken 1887 times.
✓ Branch 109 → 123 taken 1129 times.
3016 if (presetFunction.isMethod() && !presetFunction.templateTypes.empty()) {
303
1/2
✓ Branch 112 → 113 taken 1887 times.
✗ Branch 112 → 165 not taken.
5661 SymbolTableEntry *thisEntry = childScope->lookupStrict(THIS_VARIABLE_NAME);
304
1/2
✗ Branch 118 → 119 not taken.
✓ Branch 118 → 120 taken 1887 times.
1887 assert(thisEntry != nullptr);
305
2/4
✓ Branch 120 → 121 taken 1887 times.
✗ Branch 120 → 169 not taken.
✓ Branch 121 → 122 taken 1887 times.
✗ Branch 121 → 169 not taken.
1887 thisEntry->updateType(candidate.thisType.toPtr(callNode), /*overwriteExistingType=*/true);
306 }
307
308 // Add to matched functions
309
1/2
✓ Branch 123 → 124 taken 3016 times.
✗ Branch 123 → 170 not taken.
3016 matches.push_back(substantiatedFunction);
310
311 3016 break; // Leave the whole manifestation list to not double-match the manifestation
312
2/2
✓ Branch 130 → 131 taken 52273 times.
✓ Branch 130 → 132 taken 856935 times.
912606 }
313 907029 }
314
315 // If no matches were found, return a nullptr
316
2/2
✓ Branch 140 → 141 taken 58333 times.
✓ Branch 140 → 142 taken 9267 times.
67600 if (matches.empty())
317 58333 return nullptr;
318
319
1/2
✗ Branch 143 → 144 not taken.
✓ Branch 143 → 145 taken 9267 times.
9267 assert(matches.size() == 1);
320 9267 Function *matchedFunction = matches.front();
321
322 // Insert into cache
323
1/2
✓ Branch 146 → 147 taken 9267 times.
✗ Branch 146 → 184 not taken.
9267 lookupCache[cacheKey] = matchedFunction;
324
325 // Trigger revisit in type checker if required
326
1/2
✓ Branch 147 → 148 taken 9267 times.
✗ Branch 147 → 184 not taken.
9267 TypeChecker::requestRevisitIfRequired(matchedFunction);
327
328 // Return the very match
329 9267 return matchedFunction;
330 67604 }
331
332 966243 MatchResult FunctionManager::matchManifestation(Function &candidate, Scope *&matchScope, const std::string &reqName,
333 const QualType &reqThisType, const ArgList &reqArgs, TypeMapping &typeMapping,
334 bool strictQualifierMatching, bool &forceSubstantiation,
335 const ASTNode *callNode) {
336 // Check name requirement
337
2/2
✓ Branch 3 → 4 taken 902301 times.
✓ Branch 3 → 5 taken 63942 times.
966243 if (!matchName(candidate, reqName))
338 902301 return MatchResult::SKIP_FUNCTION; // Leave the whole manifestation list, because all have the same name
339
340 // Check 'this' type requirement
341
2/2
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 63940 times.
63942 if (!matchThisType(candidate, reqThisType, typeMapping, strictQualifierMatching, callNode))
342 2 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
343
344 // Check arg types requirement
345
2/2
✓ Branch 9 → 10 taken 53204 times.
✓ Branch 9 → 11 taken 10735 times.
63940 if (!matchArgTypes(candidate, reqArgs, typeMapping, strictQualifierMatching, forceSubstantiation, callNode))
346 53204 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
347
348 // Check if there are unresolved generic types
349
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 10734 times.
10735 if (typeMapping.size() < candidate.templateTypes.size())
350 1 return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one
351
352 // Substantiate return type
353 10734 substantiateReturnType(candidate, typeMapping, callNode);
354
355 // Set the match scope to the scope of the concrete substantiation
356 10734 const QualType &thisType = candidate.thisType;
357
2/2
✓ Branch 17 → 18 taken 6052 times.
✓ Branch 17 → 25 taken 4682 times.
10734 if (!thisType.is(TY_DYN)) {
358 // If we only have the generic struct scope, lookup the concrete manifestation scope
359
2/2
✓ Branch 18 → 19 taken 65 times.
✓ Branch 18 → 23 taken 5987 times.
6052 if (matchScope->isGenericScope) {
360 65 const Struct *spiceStruct = thisType.getStruct(candidate.declNode);
361
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 65 times.
65 assert(spiceStruct != nullptr);
362 65 matchScope = spiceStruct->scope;
363 }
364
1/2
✓ Branch 23 → 24 taken 6052 times.
✗ Branch 23 → 27 not taken.
6052 candidate.thisType = candidate.thisType.getWithBodyScope(matchScope);
365 }
366
367 10734 return MatchResult::MATCHED;
368 }
369
370 /**
371 * Checks if the matching candidate fulfills the name requirement
372 *
373 * @param candidate Matching candidate function
374 * @param reqName Requested function name
375 * @return Fulfilled or not
376 */
377 966243 bool FunctionManager::matchName(const Function &candidate, const std::string &reqName) { return candidate.name == reqName; }
378
379 /**
380 * Checks if the matching candidate fulfills the 'this' type requirement
381 *
382 * @param candidate Matching candidate function
383 * @param reqThisType Requested 'this' type
384 * @param typeMapping Concrete template type mapping
385 * @param strictQualifierMatching Match qualifiers strictly
386 * @param callNode Call AST node for printing error messages
387 * @return Fulfilled or not
388 */
389 63942 bool FunctionManager::matchThisType(Function &candidate, const QualType &reqThisType, TypeMapping &typeMapping,
390 bool strictQualifierMatching, const ASTNode *callNode) {
391 63942 QualType &candidateThisType = candidate.thisType;
392
393 // Shortcut for procedures
394
7/10
✓ Branch 2 → 3 taken 63942 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 44633 times.
✓ Branch 3 → 7 taken 19309 times.
✓ Branch 4 → 5 taken 44633 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 44633 times.
✗ Branch 5 → 7 not taken.
✓ Branch 8 → 9 taken 44633 times.
✓ Branch 8 → 10 taken 19309 times.
63942 if (candidateThisType.is(TY_DYN) && reqThisType.is(TY_DYN))
395 44633 return true;
396
397 // Give the type matcher a way to retrieve instances of GenericType by their name
398 41764 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
399 3146 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
400 19309 };
401
402 // Check if the requested 'this' type matches the candidate 'this' type. The type mapping may be extended
403
3/4
✓ Branch 11 → 12 taken 19309 times.
✗ Branch 11 → 21 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 14 taken 19307 times.
19309 if (!TypeMatcher::matchRequestedToCandidateType(candidateThisType, reqThisType, typeMapping, genericTypeResolver,
404 strictQualifierMatching))
405 2 return false;
406
407 // Substantiate the candidate param type, based on the type mapping
408
3/4
✓ Branch 14 → 15 taken 19307 times.
✗ Branch 14 → 21 not taken.
✓ Branch 15 → 16 taken 3261 times.
✓ Branch 15 → 17 taken 16046 times.
19307 if (candidateThisType.hasAnyGenericParts())
409
1/2
✓ Branch 16 → 17 taken 3261 times.
✗ Branch 16 → 21 not taken.
3261 TypeMatcher::substantiateTypeWithTypeMapping(candidateThisType, typeMapping, callNode);
410
411 19307 return true;
412 19309 }
413
414 /**
415 * Checks if the matching candidate fulfills the argument types requirement
416 *
417 * @param candidate Matching candidate function
418 * @param reqArgs Requested argument types
419 * @param typeMapping Concrete template type mapping
420 * @param strictQualifierMatching Match qualifiers strictly
421 * @param needsSubstantiation We want to create a substantiation after successfully matching
422 * @param callNode Call AST node for printing error messages
423 * @return Fulfilled or not
424 */
425 63940 bool FunctionManager::matchArgTypes(Function &candidate, const ArgList &reqArgs, TypeMapping &typeMapping,
426 bool strictQualifierMatching, bool &needsSubstantiation, const ASTNode *callNode) {
427 63940 std::vector<Param> &candidateParamList = candidate.paramList;
428
429 // If the number of arguments does not match with the number of params, the matching fails
430
6/6
✓ Branch 2 → 3 taken 63863 times.
✓ Branch 2 → 7 taken 77 times.
✓ Branch 5 → 6 taken 8044 times.
✓ Branch 5 → 7 taken 55819 times.
✓ Branch 8 → 9 taken 8044 times.
✓ Branch 8 → 10 taken 55896 times.
63940 if (!candidate.isVararg && reqArgs.size() != candidateParamList.size())
431 8044 return false;
432 // In the case of a vararg function, we only disallow fewer arguments than parameters
433
4/6
✓ Branch 10 → 11 taken 77 times.
✓ Branch 10 → 15 taken 55819 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 77 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 55896 times.
55896 if (candidate.isVararg && reqArgs.size() < candidateParamList.size())
434 return false;
435
436 // Give the type matcher a way to retrieve instances of GenericType by their name
437 117531 TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) {
438 5739 return getGenericTypeOfCandidateByName(candidate, genericTypeName);
439 55896 };
440
441 // Loop over all parameters
442
2/2
✓ Branch 66 → 20 taken 61386 times.
✓ Branch 66 → 67 taken 10735 times.
72121 for (size_t i = 0; i < reqArgs.size(); i++) {
443 // In the case of a vararg function candidate, we can accept additional arguments, that are not defined in the candidate,
444 // but we need to modify the candidate param list to accept them
445
6/6
✓ Branch 20 → 21 taken 312 times.
✓ Branch 20 → 24 taken 61074 times.
✓ Branch 22 → 23 taken 81 times.
✓ Branch 22 → 24 taken 231 times.
✓ Branch 25 → 26 taken 81 times.
✓ Branch 25 → 29 taken 61305 times.
61386 if (candidate.isVararg && i >= candidateParamList.size()) {
446
2/4
✓ Branch 26 → 27 taken 81 times.
✗ Branch 26 → 71 not taken.
✓ Branch 27 → 28 taken 81 times.
✗ Branch 27 → 71 not taken.
81 candidateParamList.push_back(Param(reqArgs.at(i).first, false));
447 81 needsSubstantiation = true; // We need to modify the candidate param types
448 81 continue;
449 }
450
451 // Retrieve actual and requested types
452
2/4
✓ Branch 29 → 30 taken 61305 times.
✗ Branch 29 → 84 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 61305 times.
61305 assert(!candidateParamList.at(i).isOptional);
453
1/2
✓ Branch 32 → 33 taken 61305 times.
✗ Branch 32 → 84 not taken.
61305 QualType &candidateType = candidateParamList.at(i).qualType;
454
1/2
✓ Branch 33 → 34 taken 61305 times.
✗ Branch 33 → 84 not taken.
61305 const auto &[requestedType, isArgTemporary] = reqArgs.at(i);
455
456 // Check if the requested param type matches the candidate param type. The type mapping may be extended
457
3/4
✓ Branch 36 → 37 taken 61305 times.
✗ Branch 36 → 84 not taken.
✓ Branch 37 → 38 taken 45160 times.
✓ Branch 37 → 39 taken 16145 times.
61305 if (!TypeMatcher::matchRequestedToCandidateType(candidateType, requestedType, typeMapping, genericTypeResolver,
458 strictQualifierMatching))
459 45160 return false;
460
461 // Substantiate the candidate param type, based on the type mapping
462
3/4
✓ Branch 39 → 40 taken 16145 times.
✗ Branch 39 → 84 not taken.
✓ Branch 40 → 41 taken 4781 times.
✓ Branch 40 → 42 taken 11364 times.
16145 if (candidateType.hasAnyGenericParts())
463
1/2
✓ Branch 41 → 42 taken 4781 times.
✗ Branch 41 → 84 not taken.
4781 TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, callNode);
464
465 // Check if we try to bind a non-ref temporary to a non-const ref parameter
466
3/4
✓ Branch 42 → 43 taken 16145 times.
✗ Branch 42 → 84 not taken.
✓ Branch 43 → 44 taken 1 time.
✓ Branch 43 → 54 taken 16144 times.
16145 if (!candidateType.canBind(requestedType, isArgTemporary)) {
467
1/2
✓ Branch 44 → 45 taken 1 time.
✗ Branch 44 → 53 not taken.
1 if (callNode)
468
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");
469 return false;
470 }
471
472 // If we have a function/procedure type we need to take care of the information, if it takes captures
473
9/12
✓ Branch 54 → 55 taken 16144 times.
✗ Branch 54 → 81 not taken.
✓ Branch 55 → 56 taken 16144 times.
✗ Branch 55 → 81 not taken.
✓ Branch 56 → 57 taken 31 times.
✓ Branch 56 → 60 taken 16113 times.
✓ Branch 57 → 58 taken 31 times.
✗ Branch 57 → 81 not taken.
✓ Branch 58 → 59 taken 4 times.
✓ Branch 58 → 60 taken 27 times.
✓ Branch 61 → 62 taken 4 times.
✓ Branch 61 → 64 taken 16140 times.
16144 if (requestedType.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE}) && requestedType.hasLambdaCaptures()) {
474
1/2
✓ Branch 62 → 63 taken 4 times.
✗ Branch 62 → 83 not taken.
4 candidateType = candidateType.getWithLambdaCaptures();
475 4 needsSubstantiation = true;
476 }
477 }
478
479 10735 return true;
480 55896 }
481
482 /**
483 * Substantiates the candidate return type, based on the given type mapping
484 *
485 * @param candidate Matching candidate function
486 * @param typeMapping Concrete template type mapping
487 * @param callNode AST node for error messages
488 */
489 10734 void FunctionManager::substantiateReturnType(Function &candidate, const TypeMapping &typeMapping, const ASTNode *callNode) {
490
2/2
✓ Branch 3 → 4 taken 718 times.
✓ Branch 3 → 5 taken 10016 times.
10734 if (candidate.returnType.hasAnyGenericParts())
491 718 TypeMatcher::substantiateTypeWithTypeMapping(candidate.returnType, typeMapping, callNode);
492 10734 }
493
494 /**
495 * Searches the candidate template types for a generic type object with a certain name and return it
496 *
497 * @param candidate Matching candidate function
498 * @param templateTypeName Template type name
499 * @return Generic type object
500 */
501 8885 const GenericType *FunctionManager::getGenericTypeOfCandidateByName(const Function &candidate,
502 const std::string &templateTypeName) {
503
1/2
✓ Branch 11 → 4 taken 10319 times.
✗ Branch 11 → 12 not taken.
10319 for (const GenericType &templateType : candidate.templateTypes) {
504
3/4
✓ Branch 5 → 6 taken 10319 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 8885 times.
✓ Branch 7 → 9 taken 1434 times.
10319 if (templateType.getSubType() == templateTypeName)
505 8885 return &templateType;
506 }
507 return nullptr;
508 }
509
510 /**
511 * Calculate the cache key for the function lookup cache
512 *
513 * @param scope Scope to match against
514 * @param name Function name requirement
515 * @param thisType This type requirement
516 * @param args Argument requirement
517 * @param templateTypes Template type requirement
518 * @return Cache key
519 */
520 93744 uint64_t FunctionManager::getCacheKey(const Scope *scope, const std::string &name, const QualType &thisType, const ArgList &args,
521 const QualTypeList &templateTypes) {
522 93744 uint64_t hash = 0;
523 93744 hashCombine64(hash, hashPointer(scope));
524 93744 hashCombine64(hash, std::hash<std::string>{}(name));
525 93744 hashCombine64(hash, std::hash<QualType>{}(thisType));
526
2/2
✓ Branch 19 → 10 taken 142758 times.
✓ Branch 19 → 20 taken 93744 times.
236502 for (const auto &[first, second] : args) {
527 142758 hashCombine64(hash, std::hash<QualType>{}(first));
528 142758 hashCombine64(hash, std::hash<bool>{}(second));
529 }
530 93744 hashCombine64(hash, hashVector(templateTypes));
531 93744 return hash;
532 }
533
534 /**
535 * Clear the lookup cache
536 */
537 443 void FunctionManager::cleanup() {
538 443 lookupCache.clear();
539 443 lookupCacheHits = 0;
540 443 lookupCacheMisses = 0;
541 443 }
542
543 /**
544 * Dump usage statistics for the lookup cache
545 */
546 205 std::string FunctionManager::dumpLookupCacheStatistics() {
547
1/2
✓ Branch 2 → 3 taken 205 times.
✗ Branch 2 → 22 not taken.
205 std::stringstream stats;
548
2/4
✓ Branch 3 → 4 taken 205 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 205 times.
✗ Branch 4 → 20 not taken.
205 stats << "FunctionManager lookup cache statistics:" << std::endl;
549
3/6
✓ Branch 5 → 6 taken 205 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 205 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 205 times.
✗ Branch 8 → 20 not taken.
205 stats << " lookup cache entries: " << lookupCache.size() << std::endl;
550
3/6
✓ Branch 9 → 10 taken 205 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 205 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 205 times.
✗ Branch 11 → 20 not taken.
205 stats << " lookup cache hits: " << lookupCacheHits << std::endl;
551
3/6
✓ Branch 12 → 13 taken 205 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 205 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 205 times.
✗ Branch 14 → 20 not taken.
205 stats << " lookup cache misses: " << lookupCacheMisses << std::endl;
552
1/2
✓ Branch 15 → 16 taken 205 times.
✗ Branch 15 → 20 not taken.
410 return stats.str();
553 205 }
554
555 } // namespace spice::compiler
556