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 | 14013 | 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 14013 times.
✗ Branch 2 → 43 not taken.
✓ Branch 3 → 4 taken 14013 times.
✗ Branch 3 → 40 not taken.
✓ Branch 4 → 5 taken 14013 times.
✗ Branch 4 → 38 not taken.
|
14013 | const std::string fctId = baseFunction.name + ":" + baseFunction.declNode->codeLoc.toPrettyLineAndColumn(); |
| 25 |
2/4✓ Branch 8 → 9 taken 14013 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 14013 times.
✗ Branch 9 → 44 not taken.
|
14013 | insertScope->functions.insert({fctId, FunctionManifestationList()}); |
| 26 | |||
| 27 | // Collect substantiations | ||
| 28 | 14013 | std::vector<Function> manifestations; | |
| 29 |
1/2✓ Branch 12 → 13 taken 14013 times.
✗ Branch 12 → 51 not taken.
|
14013 | substantiateOptionalParams(baseFunction, manifestations); |
| 30 |
1/2✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 14013 times.
|
14013 | assert(!manifestations.empty()); |
| 31 | |||
| 32 | // Save substantiations in declaration node | ||
| 33 | 14013 | Function *manifestationPtr = nullptr; | |
| 34 |
2/2✓ Branch 26 → 18 taken 14959 times.
✓ Branch 26 → 27 taken 14011 times.
|
28970 | for (const Function &manifestation : manifestations) { |
| 35 |
2/2✓ Branch 19 → 20 taken 14957 times.
✓ Branch 19 → 50 taken 2 times.
|
14959 | manifestationPtr = insertSubstantiation(insertScope, manifestation, baseFunction.declNode); |
| 36 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 14957 times.
|
14957 | assert(manifestationPtr != nullptr); |
| 37 |
1/2✓ Branch 22 → 23 taken 14957 times.
✗ Branch 22 → 24 not taken.
|
14957 | if (nodeFunctionList) |
| 38 |
1/2✓ Branch 23 → 24 taken 14957 times.
✗ Branch 23 → 50 not taken.
|
14957 | nodeFunctionList->push_back(manifestationPtr); |
| 39 | } | ||
| 40 | |||
| 41 |
1/2✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 14011 times.
|
14011 | if (!nodeFunctionList) |
| 42 | ✗ | return manifestationPtr; | |
| 43 | |||
| 44 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 14011 times.
|
14011 | assert(!nodeFunctionList->empty()); |
| 45 | 14011 | return nodeFunctionList->front(); | |
| 46 | 14015 | } | |
| 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 | 14013 | 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 3388 times.
✓ Branch 3 → 6 taken 10625 times.
|
14013 | if (baseFunction.paramList.empty()) { |
| 67 |
1/2✓ Branch 4 → 5 taken 3388 times.
✗ Branch 4 → 39 not taken.
|
3388 | manifestations.push_back(baseFunction); |
| 68 | 3388 | return; | |
| 69 | } | ||
| 70 | |||
| 71 | 10625 | ParamList currentFunctionParamTypes; | |
| 72 |
1/2✓ Branch 7 → 8 taken 10625 times.
✗ Branch 7 → 37 not taken.
|
10625 | currentFunctionParamTypes.reserve(baseFunction.paramList.size()); |
| 73 | 10625 | bool metFirstOptionalParam = false; | |
| 74 |
1/2✓ Branch 8 → 9 taken 10625 times.
✗ Branch 8 → 37 not taken.
|
10625 | Function manifestation = baseFunction; |
| 75 | |||
| 76 | // Loop over all parameters | ||
| 77 |
2/2✓ Branch 24 → 11 taken 16582 times.
✓ Branch 24 → 25 taken 10625 times.
|
27207 | for (const auto &[qualType, isOptional] : baseFunction.paramList) { |
| 78 | // Check if we have a mandatory parameter | ||
| 79 |
2/2✓ Branch 12 → 13 taken 15636 times.
✓ Branch 12 → 15 taken 946 times.
|
16582 | if (!isOptional) { |
| 80 |
1/2✓ Branch 13 → 14 taken 15636 times.
✗ Branch 13 → 32 not taken.
|
15636 | currentFunctionParamTypes.push_back({qualType, /*optional=*/false}); |
| 81 | 15636 | continue; | |
| 82 | } | ||
| 83 | |||
| 84 | // Add substantiation without the optional parameter | ||
| 85 |
2/2✓ Branch 15 → 16 taken 940 times.
✓ Branch 15 → 19 taken 6 times.
|
946 | if (!metFirstOptionalParam) { |
| 86 |
1/2✓ Branch 16 → 17 taken 940 times.
✗ Branch 16 → 34 not taken.
|
940 | manifestation.paramList = currentFunctionParamTypes; |
| 87 |
1/2✓ Branch 17 → 18 taken 940 times.
✗ Branch 17 → 34 not taken.
|
940 | manifestations.push_back(manifestation); |
| 88 | // Now we cannot accept mandatory parameters anymore | ||
| 89 | 940 | metFirstOptionalParam = true; | |
| 90 | } | ||
| 91 | |||
| 92 | // Add substantiation with the optional parameter | ||
| 93 |
1/2✓ Branch 19 → 20 taken 946 times.
✗ Branch 19 → 33 not taken.
|
946 | currentFunctionParamTypes.push_back({qualType, /*optional=*/false}); |
| 94 |
1/2✓ Branch 20 → 21 taken 946 times.
✗ Branch 20 → 34 not taken.
|
946 | manifestation.paramList = currentFunctionParamTypes; |
| 95 |
1/2✓ Branch 21 → 22 taken 946 times.
✗ Branch 21 → 34 not taken.
|
946 | manifestations.push_back(manifestation); |
| 96 | } | ||
| 97 | |||
| 98 | // Ensure at least once manifestation | ||
| 99 |
2/2✓ Branch 26 → 27 taken 9685 times.
✓ Branch 26 → 28 taken 940 times.
|
10625 | if (manifestations.empty()) |
| 100 |
1/2✓ Branch 27 → 28 taken 9685 times.
✗ Branch 27 → 35 not taken.
|
9685 | manifestations.push_back(baseFunction); |
| 101 | 10625 | } | |
| 102 | |||
| 103 | 6 | Function FunctionManager::createMainFunction(SymbolTableEntry *entry, const QualTypeList ¶mTypes, 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 ¶mType : paramTypes) |
| 106 |
1/2✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 24 not taken.
|
2 | paramList.push_back({paramType, false}); |
| 107 |
5/10✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 36 not taken.
✓ Branch 12 → 13 taken 6 times.
✗ Branch 12 → 33 not taken.
✓ Branch 13 → 14 taken 6 times.
✗ Branch 13 → 32 not taken.
✓ Branch 14 → 15 taken 6 times.
✗ Branch 14 → 31 not taken.
✓ Branch 16 → 17 taken 6 times.
✗ Branch 16 → 26 not taken.
|
12 | return {MAIN_FUNCTION_NAME, entry, QualType(TY_DYN), QualType(TY_INT), paramList, {}, declNode}; |
| 108 | 6 | } | |
| 109 | |||
| 110 | 17942 | Function *FunctionManager::insertSubstantiation(Scope *insertScope, const Function &newManifestation, const ASTNode *declNode) { | |
| 111 |
2/4✓ Branch 2 → 3 taken 17942 times.
✗ Branch 2 → 70 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 17942 times.
|
17942 | assert(newManifestation.hasSubstantiatedParams()); |
| 112 | |||
| 113 |
1/2✓ Branch 5 → 6 taken 17942 times.
✗ Branch 5 → 70 not taken.
|
17942 | const std::string signature = newManifestation.getSignature(true, true, false); |
| 114 | |||
| 115 | // Check if the function exists already | ||
| 116 |
5/8✓ Branch 6 → 7 taken 17942 times.
✗ Branch 6 → 59 not taken.
✓ Branch 7 → 8 taken 17942 times.
✗ Branch 7 → 59 not taken.
✓ Branch 8 → 9 taken 17942 times.
✗ Branch 8 → 59 not taken.
✓ Branch 30 → 10 taken 289713 times.
✓ Branch 30 → 31 taken 17940 times.
|
307653 | for (const auto &manifestations : insertScope->functions | std::views::values) { |
| 117 |
3/4✓ Branch 11 → 12 taken 289713 times.
✗ Branch 11 → 59 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 28 taken 289711 times.
|
289713 | if (manifestations.contains(signature)) { |
| 118 |
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; |
| 119 |
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"); |
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | // Retrieve the matching manifestation list of the scope | ||
| 124 |
3/6✓ Branch 31 → 32 taken 17940 times.
✗ Branch 31 → 65 not taken.
✓ Branch 32 → 33 taken 17940 times.
✗ Branch 32 → 62 not taken.
✓ Branch 33 → 34 taken 17940 times.
✗ Branch 33 → 60 not taken.
|
17940 | const std::string fctId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn(); |
| 125 |
2/4✓ Branch 36 → 37 taken 17940 times.
✗ Branch 36 → 66 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 17940 times.
|
17940 | assert(insertScope->functions.contains(fctId)); |
| 126 |
1/2✓ Branch 39 → 40 taken 17940 times.
✗ Branch 39 → 66 not taken.
|
17940 | FunctionManifestationList &manifestationList = insertScope->functions.at(fctId); |
| 127 | |||
| 128 | // Add substantiated function | ||
| 129 |
1/2✓ Branch 40 → 41 taken 17940 times.
✗ Branch 40 → 66 not taken.
|
17940 | manifestationList.emplace(signature, newManifestation); |
| 130 |
1/2✓ Branch 41 → 42 taken 17940 times.
✗ Branch 41 → 66 not taken.
|
35880 | return &manifestationList.at(signature); |
| 131 | 17942 | } | |
| 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 | 13141 | 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 13141 times.
✗ Branch 2 → 66 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 13141 times.
|
13141 | assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT})); |
| 146 | |||
| 147 | // Do cache lookup | ||
| 148 | 13141 | const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, {}); | |
| 149 |
3/4✓ Branch 8 → 9 taken 13141 times.
✗ Branch 8 → 80 not taken.
✓ Branch 9 → 10 taken 3736 times.
✓ Branch 9 → 12 taken 9405 times.
|
13141 | if (lookupCache.contains(cacheKey)) { |
| 150 | 3736 | lookupCacheHits++; | |
| 151 |
1/2✓ Branch 10 → 11 taken 3736 times.
✗ Branch 10 → 80 not taken.
|
3736 | return lookupCache.at(cacheKey); |
| 152 | } | ||
| 153 | 9405 | lookupCacheMisses++; | |
| 154 | |||
| 155 | 2771 | const auto pred = [&](const Arg &arg) { return arg.first.hasAnyGenericParts(); }; | |
| 156 |
5/8✓ Branch 12 → 13 taken 9405 times.
✗ Branch 12 → 80 not taken.
✓ Branch 13 → 14 taken 9030 times.
✓ Branch 13 → 17 taken 375 times.
✓ Branch 14 → 15 taken 9030 times.
✗ Branch 14 → 80 not taken.
✓ Branch 15 → 16 taken 9030 times.
✗ Branch 15 → 17 not taken.
|
9405 | 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 9405 times.
✗ Branch 18 → 80 not taken.
|
9405 | const FunctionRegistry functionRegistry = matchScope->functions; |
| 160 | // Loop over function registry to find functions, that match the requirements of the call | ||
| 161 | 9405 | std::vector<const Function *> matches; | |
| 162 |
2/2✓ Branch 55 → 21 taken 77928 times.
✓ Branch 55 → 56 taken 9405 times.
|
87333 | 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 77928 times.
✗ Branch 24 → 74 not taken.
|
77928 | const FunctionManifestationList manifestations = m; |
| 165 |
2/2✓ Branch 51 → 27 taken 87988 times.
✓ Branch 51 → 52 taken 28050 times.
|
116038 | for (const auto &[signature, presetFunction] : manifestations) { |
| 166 |
2/4✓ Branch 30 → 31 taken 87988 times.
✗ Branch 30 → 70 not taken.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 87988 times.
|
87988 | 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 87988 times.
✗ Branch 33 → 70 not taken.
✓ Branch 34 → 35 taken 31368 times.
✓ Branch 34 → 36 taken 56620 times.
|
87988 | if (presetFunction.isFullySubstantiated() != requestedFullySubstantiated) |
| 171 | 38110 | continue; | |
| 172 | |||
| 173 | // Copy the function to be able to substantiate types | ||
| 174 |
1/2✓ Branch 36 → 37 taken 56620 times.
✗ Branch 36 → 70 not taken.
|
56620 | Function candidate = presetFunction; |
| 175 | |||
| 176 | // Create empty type mapping | ||
| 177 | 56620 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 178 | |||
| 179 | 56620 | bool forceSubstantiation = false; | |
| 180 |
1/2✓ Branch 37 → 38 taken 56620 times.
✗ Branch 37 → 68 not taken.
|
56620 | const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping, |
| 181 | strictQualifierMatching, forceSubstantiation, nullptr); | ||
| 182 |
2/2✓ Branch 38 → 39 taken 48399 times.
✓ Branch 38 → 40 taken 8221 times.
|
56620 | if (matchResult == MatchResult::SKIP_FUNCTION) |
| 183 | 48399 | break; // Leave the whole function | |
| 184 |
2/2✓ Branch 40 → 41 taken 6742 times.
✓ Branch 40 → 42 taken 1479 times.
|
8221 | if (matchResult == MatchResult::SKIP_MANIFESTATION) |
| 185 | 6742 | continue; // Leave this manifestation and try the next one | |
| 186 | |||
| 187 | // Add to matches | ||
| 188 |
3/6✓ Branch 42 → 43 taken 1479 times.
✗ Branch 42 → 67 not taken.
✓ Branch 43 → 44 taken 1479 times.
✗ Branch 43 → 67 not taken.
✓ Branch 44 → 45 taken 1479 times.
✗ Branch 44 → 67 not taken.
|
1479 | matches.push_back(&matchScope->functions.at(defCodeLocStr).at(signature)); |
| 189 | |||
| 190 | 1479 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 191 |
2/2✓ Branch 47 → 48 taken 6742 times.
✓ Branch 47 → 49 taken 49878 times.
|
56620 | } |
| 192 | 77928 | } | |
| 193 | |||
| 194 | // Return the very match or a nullptr | ||
| 195 |
2/2✓ Branch 57 → 58 taken 1479 times.
✓ Branch 57 → 60 taken 7926 times.
|
9405 | return !matches.empty() ? matches.front() : nullptr; |
| 196 | 9405 | } | |
| 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 | 79970 | 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 79970 times.
✗ Branch 2 → 171 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 79970 times.
|
79970 | assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT, TY_INTERFACE})); |
| 215 | |||
| 216 | // Do cache lookup | ||
| 217 | 79970 | const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, templateTypeHints); | |
| 218 |
3/4✓ Branch 6 → 7 taken 79970 times.
✗ Branch 6 → 219 not taken.
✓ Branch 7 → 8 taken 12840 times.
✓ Branch 7 → 10 taken 67130 times.
|
79970 | if (lookupCache.contains(cacheKey)) { |
| 219 | 12840 | lookupCacheHits++; | |
| 220 |
1/2✓ Branch 8 → 9 taken 12840 times.
✗ Branch 8 → 219 not taken.
|
12840 | return lookupCache.at(cacheKey); |
| 221 | } | ||
| 222 | 67130 | lookupCacheMisses++; | |
| 223 | |||
| 224 | // Copy the registry to prevent iterating over items, that are created within the loop | ||
| 225 |
1/2✓ Branch 10 → 11 taken 67130 times.
✗ Branch 10 → 219 not taken.
|
67130 | FunctionRegistry functionRegistry = matchScope->functions; |
| 226 | // Loop over function registry to find functions, that match the requirements of the call | ||
| 227 | 67130 | std::vector<Function *> matches; | |
| 228 |
2/2✓ Branch 138 → 13 taken 904759 times.
✓ Branch 138 → 139 taken 67129 times.
|
971888 | 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 904759 times.
✗ Branch 16 → 200 not taken.
|
904759 | const FunctionManifestationList manifestations = m; |
| 231 |
2/2✓ Branch 134 → 19 taken 914654 times.
✓ Branch 134 → 135 taken 49729 times.
|
964383 | for (const auto &[signature, presetFunction] : manifestations) { |
| 232 |
2/4✓ Branch 22 → 23 taken 914654 times.
✗ Branch 22 → 196 not taken.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 914654 times.
|
914654 | 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 914654 times.
✗ Branch 25 → 196 not taken.
✓ Branch 26 → 27 taken 7719 times.
✓ Branch 26 → 28 taken 906935 times.
|
914654 | if (presetFunction.isGenericSubstantiation()) |
| 236 | 59624 | continue; | |
| 237 | |||
| 238 | // Copy the function to be able to substantiate types | ||
| 239 |
1/2✓ Branch 28 → 29 taken 906935 times.
✗ Branch 28 → 196 not taken.
|
906935 | Function candidate = presetFunction; |
| 240 | |||
| 241 | // Prepare type mapping, based on the given initial type mapping | ||
| 242 | 906935 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 243 | 906935 | typeMapping.clear(); | |
| 244 |
2/2✓ Branch 41 → 31 taken 41716 times.
✓ Branch 41 → 42 taken 906935 times.
|
948651 | for (size_t i = 0; i < std::min(templateTypeHints.size(), candidate.templateTypes.size()); i++) { |
| 245 |
2/4✓ Branch 31 → 32 taken 41716 times.
✗ Branch 31 → 194 not taken.
✓ Branch 32 → 33 taken 41716 times.
✗ Branch 32 → 194 not taken.
|
41716 | const std::string &typeName = candidate.templateTypes.at(i).getSubType(); |
| 246 |
1/2✓ Branch 33 → 34 taken 41716 times.
✗ Branch 33 → 194 not taken.
|
41716 | const QualType &templateType = templateTypeHints.at(i); |
| 247 |
2/4✓ Branch 34 → 35 taken 41716 times.
✗ Branch 34 → 174 not taken.
✓ Branch 35 → 36 taken 41716 times.
✗ Branch 35 → 172 not taken.
|
41716 | typeMapping.insert({typeName, templateType}); |
| 248 | } | ||
| 249 | |||
| 250 | 906935 | bool forceSubstantiation = false; | |
| 251 |
2/2✓ Branch 42 → 43 taken 906934 times.
✓ Branch 42 → 194 taken 1 time.
|
906935 | const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping, |
| 252 | strictQualifierMatching, forceSubstantiation, callNode); | ||
| 253 |
2/2✓ Branch 43 → 44 taken 851670 times.
✓ Branch 43 → 45 taken 55264 times.
|
906934 | if (matchResult == MatchResult::SKIP_FUNCTION) |
| 254 | 851670 | break; // Leave the whole function | |
| 255 |
2/2✓ Branch 45 → 46 taken 46052 times.
✓ Branch 45 → 47 taken 9212 times.
|
55264 | if (matchResult == MatchResult::SKIP_MANIFESTATION) |
| 256 | 46052 | 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 | 9212 | candidate.used = true; | |
| 260 | 9212 | candidate.entry->used = true; | |
| 261 | |||
| 262 | // Check if the function is generic needs to be substantiated | ||
| 263 |
6/6✓ Branch 48 → 49 taken 5932 times.
✓ Branch 48 → 51 taken 3280 times.
✓ Branch 49 → 50 taken 5853 times.
✓ Branch 49 → 51 taken 79 times.
✓ Branch 52 → 53 taken 5853 times.
✓ Branch 52 → 65 taken 3359 times.
|
9212 | if (presetFunction.templateTypes.empty() && !forceSubstantiation) { |
| 264 |
5/10✓ Branch 53 → 54 taken 5853 times.
✗ Branch 53 → 194 not taken.
✓ Branch 54 → 55 taken 5853 times.
✗ Branch 54 → 59 not taken.
✓ Branch 55 → 56 taken 5853 times.
✗ Branch 55 → 194 not taken.
✓ Branch 56 → 57 taken 5853 times.
✗ Branch 56 → 194 not taken.
✓ Branch 57 → 58 taken 5853 times.
✗ Branch 57 → 59 not taken.
|
5853 | assert(matchScope->functions.contains(fctId) && matchScope->functions.at(fctId).contains(signature)); |
| 265 |
3/6✓ Branch 60 → 61 taken 5853 times.
✗ Branch 60 → 175 not taken.
✓ Branch 61 → 62 taken 5853 times.
✗ Branch 61 → 175 not taken.
✓ Branch 62 → 63 taken 5853 times.
✗ Branch 62 → 175 not taken.
|
5853 | matches.push_back(&matchScope->functions.at(fctId).at(signature)); |
| 266 | 5853 | matches.back()->used = true; | |
| 267 | 5853 | 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 3359 times.
✗ Branch 65 → 194 not taken.
|
3359 | const std::string newSignature = candidate.getSignature(true, true, false); |
| 272 |
4/6✓ Branch 66 → 67 taken 3359 times.
✗ Branch 66 → 192 not taken.
✓ Branch 67 → 68 taken 3359 times.
✗ Branch 67 → 192 not taken.
✓ Branch 68 → 69 taken 376 times.
✓ Branch 68 → 74 taken 2983 times.
|
3359 | if (matchScope->functions.at(fctId).contains(newSignature)) { |
| 273 |
3/6✓ Branch 69 → 70 taken 376 times.
✗ Branch 69 → 176 not taken.
✓ Branch 70 → 71 taken 376 times.
✗ Branch 70 → 176 not taken.
✓ Branch 71 → 72 taken 376 times.
✗ Branch 71 → 176 not taken.
|
376 | matches.push_back(&matchScope->functions.at(fctId).at(newSignature)); |
| 274 | 376 | matches.back()->used = true; | |
| 275 | 376 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 276 | } | ||
| 277 | |||
| 278 | // Insert the substantiated version if required | ||
| 279 |
1/2✓ Branch 74 → 75 taken 2983 times.
✗ Branch 74 → 192 not taken.
|
2983 | Function *substantiatedFunction = insertSubstantiation(matchScope, candidate, presetFunction.declNode); |
| 280 |
2/4✓ Branch 75 → 76 taken 2983 times.
✗ Branch 75 → 192 not taken.
✓ Branch 76 → 77 taken 2983 times.
✗ Branch 76 → 192 not taken.
|
2983 | substantiatedFunction->genericPreset = &matchScope->functions.at(fctId).at(signature); |
| 281 | 2983 | substantiatedFunction->alreadyTypeChecked = false; | |
| 282 |
2/4✓ Branch 77 → 78 taken 2983 times.
✗ Branch 77 → 192 not taken.
✓ Branch 78 → 79 taken 2983 times.
✗ Branch 78 → 192 not taken.
|
2983 | substantiatedFunction->declNode->getFctManifestations(reqName)->push_back(substantiatedFunction); |
| 283 | |||
| 284 | // Copy function entry | ||
| 285 |
1/2✓ Branch 79 → 80 taken 2983 times.
✗ Branch 79 → 192 not taken.
|
2983 | const std::string newScopeName = substantiatedFunction->getScopeName(); |
| 286 |
1/2✓ Branch 80 → 81 taken 2983 times.
✗ Branch 80 → 190 not taken.
|
2983 | matchScope->lookupStrict(presetFunction.entry->name)->used = true; |
| 287 |
1/2✓ Branch 83 → 84 taken 2983 times.
✗ Branch 83 → 190 not taken.
|
2983 | substantiatedFunction->entry = matchScope->symbolTable.copySymbol(presetFunction.entry->name, newScopeName); |
| 288 |
1/2✗ Branch 84 → 85 not taken.
✓ Branch 84 → 86 taken 2983 times.
|
2983 | assert(substantiatedFunction->entry != nullptr); |
| 289 | |||
| 290 | // Copy function scope | ||
| 291 |
1/2✓ Branch 86 → 87 taken 2983 times.
✗ Branch 86 → 190 not taken.
|
2983 | const std::string oldScopeName = presetFunction.getScopeName(); |
| 292 |
1/2✓ Branch 87 → 88 taken 2983 times.
✗ Branch 87 → 188 not taken.
|
2983 | Scope *childScope = matchScope->copyChildScope(oldScopeName, newScopeName); |
| 293 |
1/2✗ Branch 88 → 89 not taken.
✓ Branch 88 → 90 taken 2983 times.
|
2983 | assert(childScope != nullptr); |
| 294 | 2983 | childScope->isGenericScope = false; | |
| 295 | 2983 | 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 4198 times.
✓ Branch 100 → 101 taken 2983 times.
|
7181 | for (const auto &[typeName, concreteType] : substantiatedFunction->typeMapping) |
| 299 |
2/4✓ Branch 95 → 96 taken 4198 times.
✗ Branch 95 → 179 not taken.
✓ Branch 96 → 97 taken 4198 times.
✗ Branch 96 → 177 not taken.
|
4198 | childScope->insertGenericType(typeName, GenericType(concreteType)); |
| 300 | |||
| 301 | // Substantiate the 'this' entry in the new function scope | ||
| 302 |
5/6✓ Branch 104 → 105 taken 1853 times.
✓ Branch 104 → 108 taken 1130 times.
✓ Branch 106 → 107 taken 1853 times.
✗ Branch 106 → 108 not taken.
✓ Branch 109 → 110 taken 1853 times.
✓ Branch 109 → 123 taken 1130 times.
|
2983 | if (presetFunction.isMethod() && !presetFunction.templateTypes.empty()) { |
| 303 |
1/2✓ Branch 112 → 113 taken 1853 times.
✗ Branch 112 → 183 not taken.
|
5559 | SymbolTableEntry *thisEntry = childScope->lookupStrict(THIS_VARIABLE_NAME); |
| 304 |
1/2✗ Branch 118 → 119 not taken.
✓ Branch 118 → 120 taken 1853 times.
|
1853 | assert(thisEntry != nullptr); |
| 305 |
2/4✓ Branch 120 → 121 taken 1853 times.
✗ Branch 120 → 187 not taken.
✓ Branch 121 → 122 taken 1853 times.
✗ Branch 121 → 187 not taken.
|
1853 | thisEntry->updateType(candidate.thisType.toPtr(callNode), /*overwriteExistingType=*/true); |
| 306 | } | ||
| 307 | |||
| 308 | // Add to matched functions | ||
| 309 |
1/2✓ Branch 123 → 124 taken 2983 times.
✗ Branch 123 → 188 not taken.
|
2983 | matches.push_back(substantiatedFunction); |
| 310 | |||
| 311 | 2983 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 312 |
2/2✓ Branch 130 → 131 taken 51905 times.
✓ Branch 130 → 132 taken 855029 times.
|
910294 | } |
| 313 | 904759 | } | |
| 314 | |||
| 315 | // If no matches were found, return a nullptr | ||
| 316 |
2/2✓ Branch 140 → 141 taken 57918 times.
✓ Branch 140 → 142 taken 9211 times.
|
67129 | if (matches.empty()) |
| 317 | 57918 | return nullptr; | |
| 318 | |||
| 319 | // Check if more than one function matches the requirements | ||
| 320 |
2/2✓ Branch 143 → 144 taken 1 time.
✓ Branch 143 → 163 taken 9210 times.
|
9211 | if (matches.size() > 1) { |
| 321 |
1/2✓ Branch 144 → 145 taken 1 time.
✗ Branch 144 → 214 not taken.
|
1 | std::stringstream errorMessage; |
| 322 |
3/6✓ Branch 145 → 146 taken 1 time.
✗ Branch 145 → 212 not taken.
✓ Branch 146 → 147 taken 1 time.
✗ Branch 146 → 212 not taken.
✓ Branch 147 → 148 taken 1 time.
✗ Branch 147 → 212 not taken.
|
1 | errorMessage << "The function/procedure '" << reqName << "' is ambiguous. All of the following match the requested criteria:"; |
| 323 |
2/2✓ Branch 157 → 150 taken 2 times.
✓ Branch 157 → 158 taken 1 time.
|
3 | for (const Function *match : matches) |
| 324 |
3/6✓ Branch 151 → 152 taken 2 times.
✗ Branch 151 → 205 not taken.
✓ Branch 152 → 153 taken 2 times.
✗ Branch 152 → 204 not taken.
✓ Branch 153 → 154 taken 2 times.
✗ Branch 153 → 202 not taken.
|
2 | errorMessage << "\n " << match->getSignature(); |
| 325 |
2/4✓ Branch 159 → 160 taken 1 time.
✗ Branch 159 → 209 not taken.
✓ Branch 160 → 161 taken 1 time.
✗ Branch 160 → 206 not taken.
|
1 | throw SemanticError(callNode, FUNCTION_AMBIGUITY, errorMessage.str()); |
| 326 | 1 | } | |
| 327 | 9210 | Function *matchedFunction = matches.front(); | |
| 328 | |||
| 329 | // Insert into cache | ||
| 330 |
1/2✓ Branch 164 → 165 taken 9210 times.
✗ Branch 164 → 215 not taken.
|
9210 | lookupCache[cacheKey] = matchedFunction; |
| 331 | |||
| 332 | // Trigger revisit in type checker if required | ||
| 333 |
1/2✓ Branch 165 → 166 taken 9210 times.
✗ Branch 165 → 215 not taken.
|
9210 | TypeChecker::requestRevisitIfRequired(matchedFunction); |
| 334 | |||
| 335 | // Return the very match | ||
| 336 | 9210 | return matchedFunction; | |
| 337 | 67132 | } | |
| 338 | |||
| 339 | 963555 | 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 900069 times.
✓ Branch 3 → 5 taken 63486 times.
|
963555 | if (!matchName(candidate, reqName)) |
| 345 | 900069 | return MatchResult::SKIP_FUNCTION; // Leave the whole manifestation list, because all have the same name | |
| 346 | |||
| 347 | // Check 'this' type requirement | ||
| 348 |
2/2✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 63484 times.
|
63486 | if (!matchThisType(candidate, reqThisType, typeMapping, strictQualifierMatching, callNode)) |
| 349 | 2 | 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 52791 times.
✓ Branch 9 → 11 taken 10692 times.
|
63484 | if (!matchArgTypes(candidate, reqArgs, typeMapping, strictQualifierMatching, forceSubstantiation, callNode)) |
| 353 | 52791 | 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 10691 times.
|
10692 | 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 | 10691 | substantiateReturnType(candidate, typeMapping, callNode); | |
| 361 | |||
| 362 | // Set the match scope to the scope of the concrete substantiation | ||
| 363 | 10691 | const QualType &thisType = candidate.thisType; | |
| 364 |
2/2✓ Branch 17 → 18 taken 6020 times.
✓ Branch 17 → 25 taken 4671 times.
|
10691 | 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 63 times.
✓ Branch 18 → 23 taken 5957 times.
|
6020 | if (matchScope->isGenericScope) { |
| 367 | 63 | const Struct *spiceStruct = thisType.getStruct(candidate.declNode); | |
| 368 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 63 times.
|
63 | assert(spiceStruct != nullptr); |
| 369 | 63 | matchScope = spiceStruct->scope; | |
| 370 | } | ||
| 371 |
1/2✓ Branch 23 → 24 taken 6020 times.
✗ Branch 23 → 27 not taken.
|
6020 | candidate.thisType = candidate.thisType.getWithBodyScope(matchScope); |
| 372 | } | ||
| 373 | |||
| 374 | 10691 | 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 | 963555 | 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 | 63486 | bool FunctionManager::matchThisType(Function &candidate, const QualType &reqThisType, TypeMapping &typeMapping, | |
| 397 | bool strictQualifierMatching, const ASTNode *callNode) { | ||
| 398 | 63486 | QualType &candidateThisType = candidate.thisType; | |
| 399 | |||
| 400 | // Shortcut for procedures | ||
| 401 |
7/10✓ Branch 2 → 3 taken 63486 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 44320 times.
✓ Branch 3 → 7 taken 19166 times.
✓ Branch 4 → 5 taken 44320 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 44320 times.
✗ Branch 5 → 7 not taken.
✓ Branch 8 → 9 taken 44320 times.
✓ Branch 8 → 10 taken 19166 times.
|
63486 | if (candidateThisType.is(TY_DYN) && reqThisType.is(TY_DYN)) |
| 402 | 44320 | return true; | |
| 403 | |||
| 404 | // Give the type matcher a way to retrieve instances of GenericType by their name | ||
| 405 | 41400 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 406 | 3068 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 407 | 19166 | }; | |
| 408 | |||
| 409 | // Check if the requested 'this' type matches the candidate 'this' type. The type mapping may be extended | ||
| 410 |
3/4✓ Branch 11 → 12 taken 19166 times.
✗ Branch 11 → 21 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 14 taken 19164 times.
|
19166 | if (!TypeMatcher::matchRequestedToCandidateType(candidateThisType, reqThisType, typeMapping, genericTypeResolver, |
| 411 | strictQualifierMatching)) | ||
| 412 | 2 | return false; | |
| 413 | |||
| 414 | // Substantiate the candidate param type, based on the type mapping | ||
| 415 |
3/4✓ Branch 14 → 15 taken 19164 times.
✗ Branch 14 → 21 not taken.
✓ Branch 15 → 16 taken 3167 times.
✓ Branch 15 → 17 taken 15997 times.
|
19164 | if (candidateThisType.hasAnyGenericParts()) |
| 416 |
1/2✓ Branch 16 → 17 taken 3167 times.
✗ Branch 16 → 21 not taken.
|
3167 | TypeMatcher::substantiateTypeWithTypeMapping(candidateThisType, typeMapping, callNode); |
| 417 | |||
| 418 | 19164 | return true; | |
| 419 | 19166 | } | |
| 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 | 63484 | bool FunctionManager::matchArgTypes(Function &candidate, const ArgList &reqArgs, TypeMapping &typeMapping, | |
| 433 | bool strictQualifierMatching, bool &needsSubstantiation, const ASTNode *callNode) { | ||
| 434 | 63484 | 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 63407 times.
✓ Branch 2 → 7 taken 77 times.
✓ Branch 5 → 6 taken 7987 times.
✓ Branch 5 → 7 taken 55420 times.
✓ Branch 8 → 9 taken 7987 times.
✓ Branch 8 → 10 taken 55497 times.
|
63484 | if (!candidate.isVararg && reqArgs.size() != candidateParamList.size()) |
| 438 | 7987 | return false; | |
| 439 | // In the case of a vararg function, we only disallow fewer arguments than parameters | ||
| 440 |
4/6✓ Branch 10 → 11 taken 77 times.
✓ Branch 10 → 15 taken 55420 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 77 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 55497 times.
|
55497 | 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 | 116730 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 445 | 5736 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 446 | 55497 | }; | |
| 447 | |||
| 448 | // Loop over all parameters | ||
| 449 |
2/2✓ Branch 66 → 20 taken 60994 times.
✓ Branch 66 → 67 taken 10692 times.
|
71686 | 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 312 times.
✓ Branch 20 → 24 taken 60682 times.
✓ Branch 22 → 23 taken 81 times.
✓ Branch 22 → 24 taken 231 times.
✓ Branch 25 → 26 taken 81 times.
✓ Branch 25 → 29 taken 60913 times.
|
60994 | if (candidate.isVararg && i >= candidateParamList.size()) { |
| 453 |
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)); |
| 454 | 81 | needsSubstantiation = true; // We need to modify the candidate param types | |
| 455 | 81 | continue; | |
| 456 | } | ||
| 457 | |||
| 458 | // Retrieve actual and requested types | ||
| 459 |
2/4✓ Branch 29 → 30 taken 60913 times.
✗ Branch 29 → 84 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 60913 times.
|
60913 | assert(!candidateParamList.at(i).isOptional); |
| 460 |
1/2✓ Branch 32 → 33 taken 60913 times.
✗ Branch 32 → 84 not taken.
|
60913 | QualType &candidateType = candidateParamList.at(i).qualType; |
| 461 |
1/2✓ Branch 33 → 34 taken 60913 times.
✗ Branch 33 → 84 not taken.
|
60913 | 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 60913 times.
✗ Branch 36 → 84 not taken.
✓ Branch 37 → 38 taken 44804 times.
✓ Branch 37 → 39 taken 16109 times.
|
60913 | if (!TypeMatcher::matchRequestedToCandidateType(candidateType, requestedType, typeMapping, genericTypeResolver, |
| 465 | strictQualifierMatching)) | ||
| 466 | 44804 | return false; | |
| 467 | |||
| 468 | // Substantiate the candidate param type, based on the type mapping | ||
| 469 |
3/4✓ Branch 39 → 40 taken 16109 times.
✗ Branch 39 → 84 not taken.
✓ Branch 40 → 41 taken 4766 times.
✓ Branch 40 → 42 taken 11343 times.
|
16109 | if (candidateType.hasAnyGenericParts()) |
| 470 |
1/2✓ Branch 41 → 42 taken 4766 times.
✗ Branch 41 → 84 not taken.
|
4766 | 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 16109 times.
✗ Branch 42 → 84 not taken.
✓ Branch 43 → 44 taken 1 time.
✓ Branch 43 → 54 taken 16108 times.
|
16109 | 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 16108 times.
✗ Branch 54 → 81 not taken.
✓ Branch 55 → 56 taken 16108 times.
✗ Branch 55 → 81 not taken.
✓ Branch 56 → 57 taken 27 times.
✓ Branch 56 → 60 taken 16081 times.
✓ Branch 57 → 58 taken 27 times.
✗ Branch 57 → 81 not taken.
✓ Branch 58 → 59 taken 3 times.
✓ Branch 58 → 60 taken 24 times.
✓ Branch 61 → 62 taken 3 times.
✓ Branch 61 → 64 taken 16105 times.
|
16108 | if (requestedType.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE}) && requestedType.hasLambdaCaptures()) { |
| 481 |
1/2✓ Branch 62 → 63 taken 3 times.
✗ Branch 62 → 83 not taken.
|
3 | candidateType = candidateType.getWithLambdaCaptures(); |
| 482 | 3 | needsSubstantiation = true; | |
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | 10692 | return true; | |
| 487 | 55497 | } | |
| 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 | 10691 | void FunctionManager::substantiateReturnType(Function &candidate, const TypeMapping &typeMapping, const ASTNode *callNode) { | |
| 497 |
2/2✓ Branch 3 → 4 taken 707 times.
✓ Branch 3 → 5 taken 9984 times.
|
10691 | if (candidate.returnType.hasAnyGenericParts()) |
| 498 | 707 | TypeMatcher::substantiateTypeWithTypeMapping(candidate.returnType, typeMapping, callNode); | |
| 499 | 10691 | } | |
| 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 | 8804 | const GenericType *FunctionManager::getGenericTypeOfCandidateByName(const Function &candidate, | |
| 509 | const std::string &templateTypeName) { | ||
| 510 |
1/2✓ Branch 11 → 4 taken 10237 times.
✗ Branch 11 → 12 not taken.
|
10237 | for (const GenericType &templateType : candidate.templateTypes) { |
| 511 |
3/4✓ Branch 5 → 6 taken 10237 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 8804 times.
✓ Branch 7 → 9 taken 1433 times.
|
10237 | if (templateType.getSubType() == templateTypeName) |
| 512 | 8804 | return &templateType; | |
| 513 | } | ||
| 514 | ✗ | return nullptr; | |
| 515 | } | ||
| 516 | |||
| 517 | /** | ||
| 518 | * Calculate the cache key for the function lookup cache | ||
| 519 | * | ||
| 520 | * @param scope Scope to match against | ||
| 521 | * @param name Function name requirement | ||
| 522 | * @param thisType This type requirement | ||
| 523 | * @param args Argument requirement | ||
| 524 | * @param templateTypes Template type requirement | ||
| 525 | * @return Cache key | ||
| 526 | */ | ||
| 527 | 93111 | uint64_t FunctionManager::getCacheKey(const Scope *scope, const std::string &name, const QualType &thisType, const ArgList &args, | |
| 528 | const QualTypeList &templateTypes) { | ||
| 529 | 93111 | uint64_t hash = 0; | |
| 530 | 93111 | hashCombine64(hash, hashPointer(scope)); | |
| 531 | 93111 | hashCombine64(hash, std::hash<std::string>{}(name)); | |
| 532 | 93111 | hashCombine64(hash, std::hash<QualType>{}(thisType)); | |
| 533 |
2/2✓ Branch 19 → 10 taken 141898 times.
✓ Branch 19 → 20 taken 93111 times.
|
235009 | for (const auto &[first, second] : args) { |
| 534 | 141898 | hashCombine64(hash, std::hash<QualType>{}(first)); | |
| 535 | 141898 | hashCombine64(hash, std::hash<bool>{}(second)); | |
| 536 | } | ||
| 537 | 93111 | hashCombine64(hash, hashVector(templateTypes)); | |
| 538 | 93111 | return hash; | |
| 539 | } | ||
| 540 | |||
| 541 | /** | ||
| 542 | * Clear the lookup cache | ||
| 543 | */ | ||
| 544 | 456 | void FunctionManager::cleanup() { | |
| 545 | 456 | lookupCache.clear(); | |
| 546 | 456 | lookupCacheHits = 0; | |
| 547 | 456 | lookupCacheMisses = 0; | |
| 548 | 456 | } | |
| 549 | |||
| 550 | /** | ||
| 551 | * Dump usage statistics for the lookup cache | ||
| 552 | */ | ||
| 553 | 210 | std::string FunctionManager::dumpLookupCacheStatistics() { | |
| 554 |
1/2✓ Branch 2 → 3 taken 210 times.
✗ Branch 2 → 22 not taken.
|
210 | std::stringstream stats; |
| 555 |
2/4✓ Branch 3 → 4 taken 210 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 210 times.
✗ Branch 4 → 20 not taken.
|
210 | stats << "FunctionManager lookup cache statistics:" << std::endl; |
| 556 |
3/6✓ Branch 5 → 6 taken 210 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 210 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 210 times.
✗ Branch 8 → 20 not taken.
|
210 | stats << " lookup cache entries: " << lookupCache.size() << std::endl; |
| 557 |
3/6✓ Branch 9 → 10 taken 210 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 210 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 210 times.
✗ Branch 11 → 20 not taken.
|
210 | stats << " lookup cache hits: " << lookupCacheHits << std::endl; |
| 558 |
3/6✓ Branch 12 → 13 taken 210 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 210 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 210 times.
✗ Branch 14 → 20 not taken.
|
210 | stats << " lookup cache misses: " << lookupCacheMisses << std::endl; |
| 559 |
1/2✓ Branch 15 → 16 taken 210 times.
✗ Branch 15 → 20 not taken.
|
420 | return stats.str(); |
| 560 | 210 | } | |
| 561 | |||
| 562 | } // namespace spice::compiler | ||
| 563 |