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 | 15256 | 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 15256 times.
✗ Branch 2 → 41 not taken.
✓ Branch 3 → 4 taken 15256 times.
✗ Branch 3 → 38 not taken.
✓ Branch 4 → 5 taken 15256 times.
✗ Branch 4 → 36 not taken.
|
15256 | const std::string fctId = baseFunction.name + ":" + baseFunction.declNode->codeLoc.toPrettyLineAndColumn(); |
| 25 |
1/2✓ Branch 8 → 9 taken 15256 times.
✗ Branch 8 → 42 not taken.
|
15256 | insertScope->functions.emplace(fctId, FunctionManifestationList()); |
| 26 | |||
| 27 | // Collect substantiations | ||
| 28 | 15256 | std::vector<Function> manifestations; | |
| 29 |
1/2✓ Branch 10 → 11 taken 15256 times.
✗ Branch 10 → 46 not taken.
|
15256 | substantiateOptionalParams(baseFunction, manifestations); |
| 30 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 15256 times.
|
15256 | assert(!manifestations.empty()); |
| 31 | |||
| 32 | // Save substantiations in declaration node | ||
| 33 | 15256 | Function *manifestationPtr = nullptr; | |
| 34 |
2/2✓ Branch 24 → 16 taken 16292 times.
✓ Branch 24 → 25 taken 15254 times.
|
31546 | for (const Function &manifestation : manifestations) { |
| 35 |
2/2✓ Branch 17 → 18 taken 16290 times.
✓ Branch 17 → 45 taken 2 times.
|
16292 | manifestationPtr = insertSubstantiation(insertScope, manifestation, baseFunction.declNode); |
| 36 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 16290 times.
|
16290 | assert(manifestationPtr != nullptr); |
| 37 |
1/2✓ Branch 20 → 21 taken 16290 times.
✗ Branch 20 → 22 not taken.
|
16290 | if (nodeFunctionList) |
| 38 |
1/2✓ Branch 21 → 22 taken 16290 times.
✗ Branch 21 → 45 not taken.
|
16290 | nodeFunctionList->push_back(manifestationPtr); |
| 39 | } | ||
| 40 | |||
| 41 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 15254 times.
|
15254 | if (!nodeFunctionList) |
| 42 | ✗ | return manifestationPtr; | |
| 43 | |||
| 44 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 15254 times.
|
15254 | assert(!nodeFunctionList->empty()); |
| 45 | 15254 | return nodeFunctionList->front(); | |
| 46 | 15258 | } | |
| 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 | 15256 | 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 3624 times.
✓ Branch 3 → 6 taken 11632 times.
|
15256 | if (baseFunction.paramList.empty()) { |
| 67 |
1/2✓ Branch 4 → 5 taken 3624 times.
✗ Branch 4 → 39 not taken.
|
3624 | manifestations.push_back(baseFunction); |
| 68 | 3624 | return; | |
| 69 | } | ||
| 70 | |||
| 71 | 11632 | ParamList currentFunctionParamTypes; | |
| 72 |
1/2✓ Branch 7 → 8 taken 11632 times.
✗ Branch 7 → 37 not taken.
|
11632 | currentFunctionParamTypes.reserve(baseFunction.paramList.size()); |
| 73 | 11632 | bool metFirstOptionalParam = false; | |
| 74 |
1/2✓ Branch 8 → 9 taken 11632 times.
✗ Branch 8 → 37 not taken.
|
11632 | Function manifestation = baseFunction; |
| 75 | |||
| 76 | // Loop over all parameters | ||
| 77 |
2/2✓ Branch 24 → 11 taken 18169 times.
✓ Branch 24 → 25 taken 11632 times.
|
29801 | for (const auto &[qualType, isOptional] : baseFunction.paramList) { |
| 78 | // Check if we have a mandatory parameter | ||
| 79 |
2/2✓ Branch 12 → 13 taken 17133 times.
✓ Branch 12 → 15 taken 1036 times.
|
18169 | if (!isOptional) { |
| 80 |
1/2✓ Branch 13 → 14 taken 17133 times.
✗ Branch 13 → 32 not taken.
|
17133 | currentFunctionParamTypes.push_back({qualType, /*optional=*/false}); |
| 81 | 17133 | continue; | |
| 82 | } | ||
| 83 | |||
| 84 | // Add substantiation without the optional parameter | ||
| 85 |
2/2✓ Branch 15 → 16 taken 1019 times.
✓ Branch 15 → 19 taken 17 times.
|
1036 | if (!metFirstOptionalParam) { |
| 86 |
1/2✓ Branch 16 → 17 taken 1019 times.
✗ Branch 16 → 34 not taken.
|
1019 | manifestation.paramList = currentFunctionParamTypes; |
| 87 |
1/2✓ Branch 17 → 18 taken 1019 times.
✗ Branch 17 → 34 not taken.
|
1019 | manifestations.push_back(manifestation); |
| 88 | // Now we cannot accept mandatory parameters anymore | ||
| 89 | 1019 | metFirstOptionalParam = true; | |
| 90 | } | ||
| 91 | |||
| 92 | // Add substantiation with the optional parameter | ||
| 93 |
1/2✓ Branch 19 → 20 taken 1036 times.
✗ Branch 19 → 33 not taken.
|
1036 | currentFunctionParamTypes.push_back({qualType, /*optional=*/false}); |
| 94 |
1/2✓ Branch 20 → 21 taken 1036 times.
✗ Branch 20 → 34 not taken.
|
1036 | manifestation.paramList = currentFunctionParamTypes; |
| 95 |
1/2✓ Branch 21 → 22 taken 1036 times.
✗ Branch 21 → 34 not taken.
|
1036 | manifestations.push_back(manifestation); |
| 96 | } | ||
| 97 | |||
| 98 | // Ensure at least once manifestation | ||
| 99 |
2/2✓ Branch 26 → 27 taken 10613 times.
✓ Branch 26 → 28 taken 1019 times.
|
11632 | if (manifestations.empty()) |
| 100 |
1/2✓ Branch 27 → 28 taken 10613 times.
✗ Branch 27 → 35 not taken.
|
10613 | manifestations.push_back(baseFunction); |
| 101 | 11632 | } | |
| 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 | 19596 | Function *FunctionManager::insertSubstantiation(Scope *insertScope, const Function &newManifestation, const ASTNode *declNode) { | |
| 111 |
2/4✓ Branch 2 → 3 taken 19596 times.
✗ Branch 2 → 70 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 19596 times.
|
19596 | assert(newManifestation.hasSubstantiatedParams()); |
| 112 | |||
| 113 |
1/2✓ Branch 5 → 6 taken 19596 times.
✗ Branch 5 → 70 not taken.
|
19596 | const std::string signature = newManifestation.getSignature(true, true, false); |
| 114 | |||
| 115 | // Check if the function exists already | ||
| 116 |
5/8✓ Branch 6 → 7 taken 19596 times.
✗ Branch 6 → 59 not taken.
✓ Branch 7 → 8 taken 19596 times.
✗ Branch 7 → 59 not taken.
✓ Branch 8 → 9 taken 19596 times.
✗ Branch 8 → 59 not taken.
✓ Branch 30 → 10 taken 308732 times.
✓ Branch 30 → 31 taken 19594 times.
|
328326 | for (const auto &manifestations : insertScope->functions | std::views::values) { |
| 117 |
3/4✓ Branch 11 → 12 taken 308732 times.
✗ Branch 11 → 59 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 28 taken 308730 times.
|
308732 | 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 19594 times.
✗ Branch 31 → 65 not taken.
✓ Branch 32 → 33 taken 19594 times.
✗ Branch 32 → 62 not taken.
✓ Branch 33 → 34 taken 19594 times.
✗ Branch 33 → 60 not taken.
|
19594 | const std::string fctId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn(); |
| 125 |
2/4✓ Branch 36 → 37 taken 19594 times.
✗ Branch 36 → 66 not taken.
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 19594 times.
|
19594 | assert(insertScope->functions.contains(fctId)); |
| 126 |
1/2✓ Branch 39 → 40 taken 19594 times.
✗ Branch 39 → 66 not taken.
|
19594 | FunctionManifestationList &manifestationList = insertScope->functions.at(fctId); |
| 127 | |||
| 128 | // Add substantiated function | ||
| 129 |
1/2✓ Branch 40 → 41 taken 19594 times.
✗ Branch 40 → 66 not taken.
|
19594 | manifestationList.emplace(signature, newManifestation); |
| 130 |
1/2✓ Branch 41 → 42 taken 19594 times.
✗ Branch 41 → 66 not taken.
|
39188 | return &manifestationList.at(signature); |
| 131 | 19596 | } | |
| 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 | 9060 | 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 9060 times.
✗ Branch 2 → 65 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 9060 times.
|
9060 | assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT})); |
| 146 | |||
| 147 | // Do cache lookup | ||
| 148 | 9060 | const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, {}); | |
| 149 |
3/4✓ Branch 8 → 9 taken 9060 times.
✗ Branch 8 → 66 not taken.
✓ Branch 11 → 12 taken 2198 times.
✓ Branch 11 → 14 taken 6862 times.
|
9060 | if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) { |
| 150 | 2198 | lookupCacheHits++; | |
| 151 | 2198 | return it->second; | |
| 152 | } | ||
| 153 | 6862 | lookupCacheMisses++; | |
| 154 | |||
| 155 | 2817 | const auto pred = [&](const Arg &arg) { return arg.first.hasAnyGenericParts(); }; | |
| 156 |
5/8✓ Branch 14 → 16 taken 6862 times.
✗ Branch 14 → 75 not taken.
✓ Branch 16 → 17 taken 6581 times.
✓ Branch 16 → 20 taken 281 times.
✓ Branch 17 → 18 taken 6581 times.
✗ Branch 17 → 75 not taken.
✓ Branch 18 → 19 taken 6581 times.
✗ Branch 18 → 20 not taken.
|
6862 | const bool requestedFullySubstantiated = !reqThisType.hasAnyGenericParts() && std::ranges::none_of(reqArgs, pred); |
| 157 | |||
| 158 | // Loop over function registry to find functions, that match the requirements of the call | ||
| 159 | 6862 | std::vector<const Function *> matches; | |
| 160 |
2/2✓ Branch 55 → 23 taken 56524 times.
✓ Branch 55 → 56 taken 6862 times.
|
63386 | for (const auto &[defCodeLocStr, manifestations] : matchScope->functions) { |
| 161 |
2/2✓ Branch 52 → 28 taken 66662 times.
✓ Branch 52 → 53 taken 22377 times.
|
89039 | for (const auto &[signature, presetFunction] : manifestations) { |
| 162 |
2/4✓ Branch 31 → 32 taken 66662 times.
✗ Branch 31 → 70 not taken.
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 66662 times.
|
66662 | assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point |
| 163 | |||
| 164 | // - search for concrete fct: Only match against fully substantiated versions to prevent double matching of a function | ||
| 165 | // - search for generic fct: Only match against generic preset functions | ||
| 166 |
3/4✓ Branch 34 → 35 taken 66662 times.
✗ Branch 34 → 70 not taken.
✓ Branch 35 → 36 taken 25959 times.
✓ Branch 35 → 37 taken 40703 times.
|
66662 | if (presetFunction.isFullySubstantiated() != requestedFullySubstantiated) |
| 167 | 32515 | continue; | |
| 168 | |||
| 169 | // Copy the function to be able to substantiate types | ||
| 170 |
1/2✓ Branch 37 → 38 taken 40703 times.
✗ Branch 37 → 70 not taken.
|
40703 | Function candidate = presetFunction; |
| 171 | |||
| 172 | // Create empty type mapping | ||
| 173 | 40703 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 174 | |||
| 175 | 40703 | bool forceSubstantiation = false; | |
| 176 |
1/2✓ Branch 38 → 39 taken 40703 times.
✗ Branch 38 → 68 not taken.
|
40703 | const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping, |
| 177 | strictQualifierMatching, forceSubstantiation, nullptr); | ||
| 178 |
2/2✓ Branch 39 → 40 taken 32976 times.
✓ Branch 39 → 41 taken 7727 times.
|
40703 | if (matchResult == MatchResult::SKIP_FUNCTION) |
| 179 | 32976 | break; // Leave the whole function | |
| 180 |
2/2✓ Branch 41 → 42 taken 6556 times.
✓ Branch 41 → 43 taken 1171 times.
|
7727 | if (matchResult == MatchResult::SKIP_MANIFESTATION) |
| 181 | 6556 | continue; // Leave this manifestation and try the next one | |
| 182 | |||
| 183 | // Add to matches | ||
| 184 |
3/6✓ Branch 43 → 44 taken 1171 times.
✗ Branch 43 → 67 not taken.
✓ Branch 44 → 45 taken 1171 times.
✗ Branch 44 → 67 not taken.
✓ Branch 45 → 46 taken 1171 times.
✗ Branch 45 → 67 not taken.
|
1171 | matches.push_back(&matchScope->functions.at(defCodeLocStr).at(signature)); |
| 185 | |||
| 186 | 1171 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 187 |
2/2✓ Branch 48 → 49 taken 6556 times.
✓ Branch 48 → 50 taken 34147 times.
|
40703 | } |
| 188 | } | ||
| 189 | |||
| 190 | // Return the very match or a nullptr | ||
| 191 |
2/2✓ Branch 57 → 58 taken 1171 times.
✓ Branch 57 → 60 taken 5691 times.
|
6862 | return !matches.empty() ? matches.front() : nullptr; |
| 192 | 6862 | } | |
| 193 | |||
| 194 | /** | ||
| 195 | * Check if there is a function in the scope, fulfilling all given requirements and if found, return it. | ||
| 196 | * If more than one function matches the requirement, an error gets thrown. | ||
| 197 | * | ||
| 198 | * @param matchScope Scope to match against | ||
| 199 | * @param reqName Function name requirement | ||
| 200 | * @param reqThisType This type requirement | ||
| 201 | * @param reqArgs Argument requirement | ||
| 202 | * @param templateTypeHints Template type requirement | ||
| 203 | * @param strictQualifierMatching Match argument and this type qualifiers strictly | ||
| 204 | * @param callNode Call AST node for printing error messages | ||
| 205 | * @return Matched function or nullptr | ||
| 206 | */ | ||
| 207 | 87367 | Function *FunctionManager::match(Scope *matchScope, const std::string &reqName, const QualType &reqThisType, | |
| 208 | const ArgList &reqArgs, const QualTypeList &templateTypeHints, bool strictQualifierMatching, | ||
| 209 | const ASTNode *callNode) { | ||
| 210 |
2/4✓ Branch 2 → 3 taken 87367 times.
✗ Branch 2 → 172 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 87367 times.
|
87367 | assert(reqThisType.isOneOf({TY_DYN, TY_STRUCT, TY_INTERFACE})); |
| 211 | |||
| 212 | // Do cache lookup | ||
| 213 | 87367 | const uint64_t cacheKey = getCacheKey(matchScope, reqName, reqThisType, reqArgs, templateTypeHints); | |
| 214 |
3/4✓ Branch 6 → 7 taken 87367 times.
✗ Branch 6 → 173 not taken.
✓ Branch 9 → 10 taken 14343 times.
✓ Branch 9 → 12 taken 73024 times.
|
87367 | if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) { |
| 215 | 14343 | lookupCacheHits++; | |
| 216 | 14343 | return it->second; | |
| 217 | } | ||
| 218 | 73024 | lookupCacheMisses++; | |
| 219 | |||
| 220 | // Loop over function registry to find functions, that match the requirements of the call | ||
| 221 | 73024 | std::vector<Function *> matches; | |
| 222 |
2/2✓ Branch 140 → 15 taken 1001193 times.
✓ Branch 140 → 141 taken 73023 times.
|
1074216 | for (auto &[fctId, manifestations] : matchScope->functions) { |
| 223 |
2/2✓ Branch 137 → 20 taken 1013137 times.
✓ Branch 137 → 138 taken 53663 times.
|
1066800 | for (const auto &[signature, presetFunction] : manifestations) { |
| 224 |
2/4✓ Branch 23 → 24 taken 1013137 times.
✗ Branch 23 → 196 not taken.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 1013137 times.
|
1013137 | assert(presetFunction.hasSubstantiatedParams()); // No optional params are allowed at this point |
| 225 | |||
| 226 | // Skip generic and newly inserted substantiations to prevent double matching of a function | ||
| 227 |
6/8✓ Branch 26 → 27 taken 1013137 times.
✗ Branch 26 → 196 not taken.
✓ Branch 27 → 28 taken 1003519 times.
✓ Branch 27 → 29 taken 9618 times.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 1003519 times.
✓ Branch 31 → 32 taken 9618 times.
✓ Branch 31 → 33 taken 1003519 times.
|
1013137 | if (presetFunction.isGenericSubstantiation() || presetFunction.isNewlyInserted) |
| 228 | 65607 | continue; | |
| 229 | |||
| 230 | // Copy the function to be able to substantiate types | ||
| 231 |
1/2✓ Branch 33 → 34 taken 1003519 times.
✗ Branch 33 → 196 not taken.
|
1003519 | Function candidate = presetFunction; |
| 232 | |||
| 233 | // Prepare type mapping, based on the given initial type mapping | ||
| 234 | 1003519 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 235 | 1003519 | typeMapping.clear(); | |
| 236 |
2/2✓ Branch 44 → 36 taken 42413 times.
✓ Branch 44 → 45 taken 1003519 times.
|
1045932 | for (size_t i = 0; i < std::min(templateTypeHints.size(), candidate.templateTypes.size()); i++) { |
| 237 |
2/4✓ Branch 36 → 37 taken 42413 times.
✗ Branch 36 → 194 not taken.
✓ Branch 37 → 38 taken 42413 times.
✗ Branch 37 → 194 not taken.
|
42413 | const std::string &typeName = candidate.templateTypes.at(i).getSubType(); |
| 238 |
1/2✓ Branch 38 → 39 taken 42413 times.
✗ Branch 38 → 194 not taken.
|
42413 | const QualType &templateType = templateTypeHints.at(i); |
| 239 |
1/2✓ Branch 39 → 40 taken 42413 times.
✗ Branch 39 → 194 not taken.
|
42413 | typeMapping.emplace(typeName, templateType); |
| 240 | } | ||
| 241 | |||
| 242 | 1003519 | bool forceSubstantiation = false; | |
| 243 |
2/2✓ Branch 45 → 46 taken 1003518 times.
✓ Branch 45 → 194 taken 1 time.
|
1003519 | const MatchResult matchResult = matchManifestation(candidate, matchScope, reqName, reqThisType, reqArgs, typeMapping, |
| 244 | strictQualifierMatching, forceSubstantiation, callNode); | ||
| 245 |
2/2✓ Branch 46 → 47 taken 943827 times.
✓ Branch 46 → 48 taken 59691 times.
|
1003518 | if (matchResult == MatchResult::SKIP_FUNCTION) |
| 246 | 943827 | break; // Leave the whole function | |
| 247 |
2/2✓ Branch 48 → 49 taken 49641 times.
✓ Branch 48 → 50 taken 10050 times.
|
59691 | if (matchResult == MatchResult::SKIP_MANIFESTATION) |
| 248 | 49641 | continue; // Leave this manifestation and try the next one | |
| 249 | |||
| 250 | // We found a match! -> Set the actual candidate and its entry to used | ||
| 251 | 10050 | candidate.used = true; | |
| 252 | 10050 | candidate.entry->used = true; | |
| 253 | |||
| 254 | // Check if the function is generic needs to be substantiated | ||
| 255 |
6/6✓ Branch 51 → 52 taken 6440 times.
✓ Branch 51 → 54 taken 3610 times.
✓ Branch 52 → 53 taken 6348 times.
✓ Branch 52 → 54 taken 92 times.
✓ Branch 55 → 56 taken 6348 times.
✓ Branch 55 → 68 taken 3702 times.
|
10050 | if (presetFunction.templateTypes.empty() && !forceSubstantiation) { |
| 256 |
5/10✓ Branch 56 → 57 taken 6348 times.
✗ Branch 56 → 174 not taken.
✓ Branch 57 → 58 taken 6348 times.
✗ Branch 57 → 62 not taken.
✓ Branch 58 → 59 taken 6348 times.
✗ Branch 58 → 174 not taken.
✓ Branch 59 → 60 taken 6348 times.
✗ Branch 59 → 174 not taken.
✓ Branch 60 → 61 taken 6348 times.
✗ Branch 60 → 62 not taken.
|
6348 | assert(matchScope->functions.contains(fctId) && matchScope->functions.at(fctId).contains(signature)); |
| 257 |
2/4✓ Branch 63 → 64 taken 6348 times.
✗ Branch 63 → 174 not taken.
✓ Branch 64 → 65 taken 6348 times.
✗ Branch 64 → 174 not taken.
|
6348 | Function *match = &matchScope->functions.at(fctId).at(signature); |
| 258 | 6348 | match->used = true; | |
| 259 |
1/2✓ Branch 65 → 66 taken 6348 times.
✗ Branch 65 → 174 not taken.
|
6348 | matches.push_back(match); |
| 260 | 6348 | continue; // Match was successful -> match the next function | |
| 261 | 6348 | } | |
| 262 | |||
| 263 | // Check if we already have this manifestation and can simply re-use it | ||
| 264 |
1/2✓ Branch 68 → 69 taken 3702 times.
✗ Branch 68 → 194 not taken.
|
3702 | const std::string newSignature = candidate.getSignature(true, true, false); |
| 265 |
3/4✓ Branch 69 → 70 taken 3702 times.
✗ Branch 69 → 176 not taken.
✓ Branch 72 → 73 taken 398 times.
✓ Branch 72 → 77 taken 3304 times.
|
3702 | if (const auto it = manifestations.find(newSignature); it != manifestations.end()) { |
| 266 | 398 | it->second.used = true; | |
| 267 |
1/2✓ Branch 75 → 76 taken 398 times.
✗ Branch 75 → 175 not taken.
|
398 | matches.push_back(&it->second); |
| 268 | 398 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 269 | } | ||
| 270 | |||
| 271 | // Insert the substantiated version if required | ||
| 272 |
1/2✓ Branch 77 → 78 taken 3304 times.
✗ Branch 77 → 192 not taken.
|
3304 | Function *substantiatedFunction = insertSubstantiation(matchScope, candidate, presetFunction.declNode); |
| 273 |
2/4✓ Branch 78 → 79 taken 3304 times.
✗ Branch 78 → 192 not taken.
✓ Branch 79 → 80 taken 3304 times.
✗ Branch 79 → 192 not taken.
|
3304 | substantiatedFunction->genericPreset = &matchScope->functions.at(fctId).at(signature); |
| 274 | 3304 | substantiatedFunction->alreadyTypeChecked = false; | |
| 275 |
2/4✓ Branch 80 → 81 taken 3304 times.
✗ Branch 80 → 192 not taken.
✓ Branch 81 → 82 taken 3304 times.
✗ Branch 81 → 192 not taken.
|
3304 | substantiatedFunction->declNode->getFctManifestations(reqName)->push_back(substantiatedFunction); |
| 276 | 3304 | substantiatedFunction->isNewlyInserted = true; // To not iterate over it in the same matching | |
| 277 | |||
| 278 | // Copy function entry | ||
| 279 |
1/2✓ Branch 82 → 83 taken 3304 times.
✗ Branch 82 → 192 not taken.
|
3304 | const std::string newScopeName = substantiatedFunction->getScopeName(); |
| 280 |
1/2✓ Branch 83 → 84 taken 3304 times.
✗ Branch 83 → 190 not taken.
|
3304 | matchScope->lookupStrict(presetFunction.entry->name)->used = true; |
| 281 |
1/2✓ Branch 86 → 87 taken 3304 times.
✗ Branch 86 → 190 not taken.
|
3304 | substantiatedFunction->entry = matchScope->symbolTable.copySymbol(presetFunction.entry->name, newScopeName); |
| 282 |
1/2✗ Branch 87 → 88 not taken.
✓ Branch 87 → 89 taken 3304 times.
|
3304 | assert(substantiatedFunction->entry != nullptr); |
| 283 | |||
| 284 | // Copy function scope | ||
| 285 |
1/2✓ Branch 89 → 90 taken 3304 times.
✗ Branch 89 → 190 not taken.
|
3304 | const std::string oldScopeName = presetFunction.getScopeName(); |
| 286 |
1/2✓ Branch 90 → 91 taken 3304 times.
✗ Branch 90 → 188 not taken.
|
3304 | Scope *childScope = matchScope->copyChildScope(oldScopeName, newScopeName); |
| 287 |
1/2✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 3304 times.
|
3304 | assert(childScope != nullptr); |
| 288 | 3304 | childScope->isGenericScope = false; | |
| 289 | 3304 | substantiatedFunction->bodyScope = childScope; | |
| 290 | |||
| 291 | // Insert symbols for generic type names with concrete types into the child block | ||
| 292 |
2/2✓ Branch 103 → 95 taken 4577 times.
✓ Branch 103 → 104 taken 3304 times.
|
7881 | for (const auto &[typeName, concreteType] : substantiatedFunction->typeMapping) |
| 293 |
2/4✓ Branch 98 → 99 taken 4577 times.
✗ Branch 98 → 179 not taken.
✓ Branch 99 → 100 taken 4577 times.
✗ Branch 99 → 177 not taken.
|
4577 | childScope->insertGenericType(typeName, GenericType(concreteType)); |
| 294 | |||
| 295 | // Substantiate the 'this' entry in the new function scope | ||
| 296 |
5/6✓ Branch 107 → 108 taken 2119 times.
✓ Branch 107 → 111 taken 1185 times.
✓ Branch 109 → 110 taken 2119 times.
✗ Branch 109 → 111 not taken.
✓ Branch 112 → 113 taken 2119 times.
✓ Branch 112 → 126 taken 1185 times.
|
3304 | if (presetFunction.isMethod() && !presetFunction.templateTypes.empty()) { |
| 297 |
1/2✓ Branch 115 → 116 taken 2119 times.
✗ Branch 115 → 183 not taken.
|
6357 | SymbolTableEntry *thisEntry = childScope->lookupStrict(THIS_VARIABLE_NAME); |
| 298 |
1/2✗ Branch 121 → 122 not taken.
✓ Branch 121 → 123 taken 2119 times.
|
2119 | assert(thisEntry != nullptr); |
| 299 |
2/4✓ Branch 123 → 124 taken 2119 times.
✗ Branch 123 → 187 not taken.
✓ Branch 124 → 125 taken 2119 times.
✗ Branch 124 → 187 not taken.
|
2119 | thisEntry->updateType(candidate.thisType.toPtr(callNode), /*overwriteExistingType=*/true); |
| 300 | } | ||
| 301 | |||
| 302 | // Add to matched functions | ||
| 303 |
1/2✓ Branch 126 → 127 taken 3304 times.
✗ Branch 126 → 188 not taken.
|
3304 | matches.push_back(substantiatedFunction); |
| 304 | |||
| 305 | 3304 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 306 |
2/2✓ Branch 133 → 134 taken 55989 times.
✓ Branch 133 → 135 taken 947529 times.
|
1007221 | } |
| 307 | } | ||
| 308 | |||
| 309 | // If no matches were found, return a nullptr | ||
| 310 |
2/2✓ Branch 142 → 143 taken 62974 times.
✓ Branch 142 → 144 taken 10049 times.
|
73023 | if (matches.empty()) |
| 311 | 62974 | return nullptr; | |
| 312 | |||
| 313 | // Check if more than one function matches the requirements | ||
| 314 |
2/2✓ Branch 145 → 146 taken 1 time.
✓ Branch 145 → 165 taken 10048 times.
|
10049 | if (matches.size() > 1) { |
| 315 |
1/2✓ Branch 146 → 147 taken 1 time.
✗ Branch 146 → 211 not taken.
|
1 | std::stringstream errorMessage; |
| 316 |
3/6✓ Branch 147 → 148 taken 1 time.
✗ Branch 147 → 209 not taken.
✓ Branch 148 → 149 taken 1 time.
✗ Branch 148 → 209 not taken.
✓ Branch 149 → 150 taken 1 time.
✗ Branch 149 → 209 not taken.
|
1 | errorMessage << "The function/procedure '" << reqName << "' is ambiguous. All of the following match the requested criteria:"; |
| 317 |
2/2✓ Branch 159 → 152 taken 2 times.
✓ Branch 159 → 160 taken 1 time.
|
3 | for (const Function *match : matches) |
| 318 |
3/6✓ Branch 153 → 154 taken 2 times.
✗ Branch 153 → 202 not taken.
✓ Branch 154 → 155 taken 2 times.
✗ Branch 154 → 201 not taken.
✓ Branch 155 → 156 taken 2 times.
✗ Branch 155 → 199 not taken.
|
2 | errorMessage << "\n " << match->getSignature(); |
| 319 |
2/4✓ Branch 161 → 162 taken 1 time.
✗ Branch 161 → 206 not taken.
✓ Branch 162 → 163 taken 1 time.
✗ Branch 162 → 203 not taken.
|
1 | throw SemanticError(callNode, FUNCTION_AMBIGUITY, errorMessage.str()); |
| 320 | 1 | } | |
| 321 | 10048 | Function *matchedFunction = matches.front(); | |
| 322 | 10048 | matchedFunction->isNewlyInserted = false; | |
| 323 | |||
| 324 | // Insert into cache | ||
| 325 |
1/2✓ Branch 166 → 167 taken 10048 times.
✗ Branch 166 → 212 not taken.
|
10048 | lookupCache[cacheKey] = matchedFunction; |
| 326 | |||
| 327 | // Trigger revisit in type checker if required | ||
| 328 |
1/2✓ Branch 167 → 168 taken 10048 times.
✗ Branch 167 → 212 not taken.
|
10048 | TypeChecker::requestRevisitIfRequired(matchedFunction); |
| 329 | |||
| 330 | // Return the very match | ||
| 331 | 10048 | return matchedFunction; | |
| 332 | 73024 | } | |
| 333 | |||
| 334 | 1044222 | MatchResult FunctionManager::matchManifestation(Function &candidate, Scope *&matchScope, const std::string &reqName, | |
| 335 | const QualType &reqThisType, const ArgList &reqArgs, TypeMapping &typeMapping, | ||
| 336 | bool strictQualifierMatching, bool &forceSubstantiation, | ||
| 337 | const ASTNode *callNode) { | ||
| 338 | // Check name requirement | ||
| 339 |
2/2✓ Branch 3 → 4 taken 976803 times.
✓ Branch 3 → 5 taken 67419 times.
|
1044222 | if (!matchName(candidate, reqName)) |
| 340 | 976803 | return MatchResult::SKIP_FUNCTION; // Leave the whole manifestation list, because all have the same name | |
| 341 | |||
| 342 | // Check 'this' type requirement | ||
| 343 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 67419 times.
|
67419 | if (!matchThisType(candidate, reqThisType, typeMapping, strictQualifierMatching, callNode)) |
| 344 | ✗ | return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one | |
| 345 | |||
| 346 | // Check arg types requirement | ||
| 347 |
2/2✓ Branch 9 → 10 taken 56196 times.
✓ Branch 9 → 11 taken 11222 times.
|
67419 | if (!matchArgTypes(candidate, reqArgs, typeMapping, strictQualifierMatching, forceSubstantiation, callNode)) |
| 348 | 56196 | return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one | |
| 349 | |||
| 350 | // Check if there are unresolved generic types | ||
| 351 |
2/2✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 11221 times.
|
11222 | if (typeMapping.size() < candidate.templateTypes.size()) |
| 352 | 1 | return MatchResult::SKIP_MANIFESTATION; // Leave this manifestation and try the next one | |
| 353 | |||
| 354 | // Substantiate return type | ||
| 355 | 11221 | substantiateReturnType(candidate, typeMapping, callNode); | |
| 356 | |||
| 357 | // Set the match scope to the scope of the concrete substantiation | ||
| 358 | 11221 | const QualType &thisType = candidate.thisType; | |
| 359 |
2/2✓ Branch 17 → 18 taken 6204 times.
✓ Branch 17 → 25 taken 5017 times.
|
11221 | if (!thisType.is(TY_DYN)) { |
| 360 | // If we only have the generic struct scope, lookup the concrete manifestation scope | ||
| 361 |
2/2✓ Branch 18 → 19 taken 75 times.
✓ Branch 18 → 23 taken 6129 times.
|
6204 | if (matchScope->isGenericScope) { |
| 362 | 75 | const Struct *spiceStruct = thisType.getStruct(candidate.declNode); | |
| 363 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 75 times.
|
75 | assert(spiceStruct != nullptr); |
| 364 | 75 | matchScope = spiceStruct->scope; | |
| 365 | } | ||
| 366 |
1/2✓ Branch 23 → 24 taken 6204 times.
✗ Branch 23 → 27 not taken.
|
6204 | candidate.thisType = candidate.thisType.getWithBodyScope(matchScope); |
| 367 | } | ||
| 368 | |||
| 369 | 11221 | return MatchResult::MATCHED; | |
| 370 | } | ||
| 371 | |||
| 372 | /** | ||
| 373 | * Checks if the matching candidate fulfills the name requirement | ||
| 374 | * | ||
| 375 | * @param candidate Matching candidate function | ||
| 376 | * @param reqName Requested function name | ||
| 377 | * @return Fulfilled or not | ||
| 378 | */ | ||
| 379 | 1044222 | bool FunctionManager::matchName(const Function &candidate, const std::string &reqName) { return candidate.name == reqName; } | |
| 380 | |||
| 381 | /** | ||
| 382 | * Checks if the matching candidate fulfills the 'this' type requirement | ||
| 383 | * | ||
| 384 | * @param candidate Matching candidate function | ||
| 385 | * @param reqThisType Requested 'this' type | ||
| 386 | * @param typeMapping Concrete template type mapping | ||
| 387 | * @param strictQualifierMatching Match qualifiers strictly | ||
| 388 | * @param callNode Call AST node for printing error messages | ||
| 389 | * @return Fulfilled or not | ||
| 390 | */ | ||
| 391 | 67419 | bool FunctionManager::matchThisType(Function &candidate, const QualType &reqThisType, TypeMapping &typeMapping, | |
| 392 | bool strictQualifierMatching, const ASTNode *callNode) { | ||
| 393 | 67419 | QualType &candidateThisType = candidate.thisType; | |
| 394 | |||
| 395 | // Shortcut for procedures | ||
| 396 |
7/10✓ Branch 2 → 3 taken 67419 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 47644 times.
✓ Branch 3 → 7 taken 19775 times.
✓ Branch 4 → 5 taken 47644 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 47644 times.
✗ Branch 5 → 7 not taken.
✓ Branch 8 → 9 taken 47644 times.
✓ Branch 8 → 10 taken 19775 times.
|
67419 | if (candidateThisType.is(TY_DYN) && reqThisType.is(TY_DYN)) |
| 397 | 47644 | return true; | |
| 398 | |||
| 399 | // Give the type matcher a way to retrieve instances of GenericType by their name | ||
| 400 | 42788 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 401 | 3238 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 402 | 19775 | }; | |
| 403 | |||
| 404 | // Check if the requested 'this' type matches the candidate 'this' type. The type mapping may be extended | ||
| 405 |
2/4✓ Branch 11 → 12 taken 19775 times.
✗ Branch 11 → 21 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 19775 times.
|
19775 | if (!TypeMatcher::matchRequestedToCandidateType(candidateThisType, reqThisType, typeMapping, genericTypeResolver, |
| 406 | strictQualifierMatching)) | ||
| 407 | ✗ | return false; | |
| 408 | |||
| 409 | // Substantiate the candidate param type, based on the type mapping | ||
| 410 |
3/4✓ Branch 14 → 15 taken 19775 times.
✗ Branch 14 → 21 not taken.
✓ Branch 15 → 16 taken 3426 times.
✓ Branch 15 → 17 taken 16349 times.
|
19775 | if (candidateThisType.hasAnyGenericParts()) |
| 411 |
1/2✓ Branch 16 → 17 taken 3426 times.
✗ Branch 16 → 21 not taken.
|
3426 | TypeMatcher::substantiateTypeWithTypeMapping(candidateThisType, typeMapping, callNode); |
| 412 | |||
| 413 | 19775 | return true; | |
| 414 | 19775 | } | |
| 415 | |||
| 416 | /** | ||
| 417 | * Checks if the matching candidate fulfills the argument types requirement | ||
| 418 | * | ||
| 419 | * @param candidate Matching candidate function | ||
| 420 | * @param reqArgs Requested argument types | ||
| 421 | * @param typeMapping Concrete template type mapping | ||
| 422 | * @param strictQualifierMatching Match qualifiers strictly | ||
| 423 | * @param needsSubstantiation We want to create a substantiation after successfully matching | ||
| 424 | * @param callNode Call AST node for printing error messages | ||
| 425 | * @return Fulfilled or not | ||
| 426 | */ | ||
| 427 | 67419 | bool FunctionManager::matchArgTypes(Function &candidate, const ArgList &reqArgs, TypeMapping &typeMapping, | |
| 428 | bool strictQualifierMatching, bool &needsSubstantiation, const ASTNode *callNode) { | ||
| 429 | 67419 | std::vector<Param> &candidateParamList = candidate.paramList; | |
| 430 | |||
| 431 | // If the number of arguments does not match with the number of params, the matching fails | ||
| 432 |
6/6✓ Branch 2 → 3 taken 67329 times.
✓ Branch 2 → 7 taken 90 times.
✓ Branch 5 → 6 taken 8259 times.
✓ Branch 5 → 7 taken 59070 times.
✓ Branch 8 → 9 taken 8259 times.
✓ Branch 8 → 10 taken 59160 times.
|
67419 | if (!candidate.isVararg && reqArgs.size() != candidateParamList.size()) |
| 433 | 8259 | return false; | |
| 434 | // In the case of a vararg function, we only disallow fewer arguments than parameters | ||
| 435 |
4/6✓ Branch 10 → 11 taken 90 times.
✓ Branch 10 → 15 taken 59070 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 90 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 59160 times.
|
59160 | if (candidate.isVararg && reqArgs.size() < candidateParamList.size()) |
| 436 | ✗ | return false; | |
| 437 | |||
| 438 | // Give the type matcher a way to retrieve instances of GenericType by their name | ||
| 439 | 124600 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 440 | 6280 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 441 | 59160 | }; | |
| 442 | |||
| 443 | // Loop over all parameters | ||
| 444 |
2/2✓ Branch 66 → 20 taken 65286 times.
✓ Branch 66 → 67 taken 11222 times.
|
76508 | for (size_t i = 0; i < reqArgs.size(); i++) { |
| 445 | // In the case of a vararg function candidate, we can accept additional arguments, that are not defined in the candidate, | ||
| 446 | // but we need to modify the candidate param list to accept them | ||
| 447 |
6/6✓ Branch 20 → 21 taken 361 times.
✓ Branch 20 → 24 taken 64925 times.
✓ Branch 22 → 23 taken 94 times.
✓ Branch 22 → 24 taken 267 times.
✓ Branch 25 → 26 taken 94 times.
✓ Branch 25 → 29 taken 65192 times.
|
65286 | if (candidate.isVararg && i >= candidateParamList.size()) { |
| 448 |
2/4✓ Branch 26 → 27 taken 94 times.
✗ Branch 26 → 71 not taken.
✓ Branch 27 → 28 taken 94 times.
✗ Branch 27 → 71 not taken.
|
94 | candidateParamList.push_back(Param(reqArgs.at(i).first, false)); |
| 449 | 94 | needsSubstantiation = true; // We need to modify the candidate param types | |
| 450 | 94 | continue; | |
| 451 | } | ||
| 452 | |||
| 453 | // Retrieve actual and requested types | ||
| 454 |
2/4✓ Branch 29 → 30 taken 65192 times.
✗ Branch 29 → 84 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 65192 times.
|
65192 | assert(!candidateParamList.at(i).isOptional); |
| 455 |
1/2✓ Branch 32 → 33 taken 65192 times.
✗ Branch 32 → 84 not taken.
|
65192 | QualType &candidateType = candidateParamList.at(i).qualType; |
| 456 |
1/2✓ Branch 33 → 34 taken 65192 times.
✗ Branch 33 → 84 not taken.
|
65192 | const auto &[requestedType, isArgTemporary] = reqArgs.at(i); |
| 457 | |||
| 458 | // Check if the requested param type matches the candidate param type. The type mapping may be extended | ||
| 459 |
3/4✓ Branch 36 → 37 taken 65192 times.
✗ Branch 36 → 84 not taken.
✓ Branch 37 → 38 taken 47937 times.
✓ Branch 37 → 39 taken 17255 times.
|
65192 | if (!TypeMatcher::matchRequestedToCandidateType(candidateType, requestedType, typeMapping, genericTypeResolver, |
| 460 | strictQualifierMatching)) | ||
| 461 | 47937 | return false; | |
| 462 | |||
| 463 | // Substantiate the candidate param type, based on the type mapping | ||
| 464 |
3/4✓ Branch 39 → 40 taken 17255 times.
✗ Branch 39 → 84 not taken.
✓ Branch 40 → 41 taken 5015 times.
✓ Branch 40 → 42 taken 12240 times.
|
17255 | if (candidateType.hasAnyGenericParts()) |
| 465 |
1/2✓ Branch 41 → 42 taken 5015 times.
✗ Branch 41 → 84 not taken.
|
5015 | TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, callNode); |
| 466 | |||
| 467 | // Check if we try to bind a non-ref temporary to a non-const ref parameter | ||
| 468 |
3/4✓ Branch 42 → 43 taken 17255 times.
✗ Branch 42 → 84 not taken.
✓ Branch 43 → 44 taken 1 time.
✓ Branch 43 → 54 taken 17254 times.
|
17255 | if (!candidateType.canBind(requestedType, isArgTemporary)) { |
| 469 |
1/2✓ Branch 44 → 45 taken 1 time.
✗ Branch 44 → 53 not taken.
|
1 | if (callNode) |
| 470 |
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"); |
| 471 | ✗ | return false; | |
| 472 | } | ||
| 473 | |||
| 474 | // If we have a function/procedure type we need to take care of the information, if it takes captures | ||
| 475 |
9/12✓ Branch 54 → 55 taken 17254 times.
✗ Branch 54 → 81 not taken.
✓ Branch 55 → 56 taken 17254 times.
✗ Branch 55 → 81 not taken.
✓ Branch 56 → 57 taken 33 times.
✓ Branch 56 → 60 taken 17221 times.
✓ Branch 57 → 58 taken 33 times.
✗ Branch 57 → 81 not taken.
✓ Branch 58 → 59 taken 3 times.
✓ Branch 58 → 60 taken 30 times.
✓ Branch 61 → 62 taken 3 times.
✓ Branch 61 → 64 taken 17251 times.
|
17254 | if (requestedType.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE}) && requestedType.hasLambdaCaptures()) { |
| 476 |
1/2✓ Branch 62 → 63 taken 3 times.
✗ Branch 62 → 83 not taken.
|
3 | candidateType = candidateType.getWithLambdaCaptures(); |
| 477 | 3 | needsSubstantiation = true; | |
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | 11222 | return true; | |
| 482 | 59160 | } | |
| 483 | |||
| 484 | /** | ||
| 485 | * Substantiates the candidate return type, based on the given type mapping | ||
| 486 | * | ||
| 487 | * @param candidate Matching candidate function | ||
| 488 | * @param typeMapping Concrete template type mapping | ||
| 489 | * @param callNode AST node for error messages | ||
| 490 | */ | ||
| 491 | 11221 | void FunctionManager::substantiateReturnType(Function &candidate, const TypeMapping &typeMapping, const ASTNode *callNode) { | |
| 492 |
2/2✓ Branch 3 → 4 taken 795 times.
✓ Branch 3 → 5 taken 10426 times.
|
11221 | if (candidate.returnType.hasAnyGenericParts()) |
| 493 | 795 | TypeMatcher::substantiateTypeWithTypeMapping(candidate.returnType, typeMapping, callNode); | |
| 494 | 11221 | } | |
| 495 | |||
| 496 | /** | ||
| 497 | * Searches the candidate template types for a generic type object with a certain name and return it | ||
| 498 | * | ||
| 499 | * @param candidate Matching candidate function | ||
| 500 | * @param templateTypeName Template type name | ||
| 501 | * @return Generic type object | ||
| 502 | */ | ||
| 503 | 9518 | const GenericType *FunctionManager::getGenericTypeOfCandidateByName(const Function &candidate, | |
| 504 | const std::string &templateTypeName) { | ||
| 505 |
1/2✓ Branch 11 → 4 taken 11001 times.
✗ Branch 11 → 12 not taken.
|
11001 | for (const GenericType &templateType : candidate.templateTypes) { |
| 506 |
3/4✓ Branch 5 → 6 taken 11001 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 9518 times.
✓ Branch 7 → 9 taken 1483 times.
|
11001 | if (templateType.getSubType() == templateTypeName) |
| 507 | 9518 | return &templateType; | |
| 508 | } | ||
| 509 | ✗ | return nullptr; | |
| 510 | } | ||
| 511 | |||
| 512 | /** | ||
| 513 | * Calculate the cache key for the function lookup cache | ||
| 514 | * | ||
| 515 | * @param scope Scope to match against | ||
| 516 | * @param name Function name requirement | ||
| 517 | * @param thisType This type requirement | ||
| 518 | * @param args Argument requirement | ||
| 519 | * @param templateTypes Template type requirement | ||
| 520 | * @return Cache key | ||
| 521 | */ | ||
| 522 | 96427 | uint64_t FunctionManager::getCacheKey(const Scope *scope, const std::string &name, const QualType &thisType, const ArgList &args, | |
| 523 | const QualTypeList &templateTypes) { | ||
| 524 | 96427 | uint64_t hash = 0; | |
| 525 | 96427 | hashCombine64(hash, hashPointer(scope)); | |
| 526 | 96427 | hashCombine64(hash, std::hash<std::string>{}(name)); | |
| 527 | 96427 | hashCombine64(hash, std::hash<QualType>{}(thisType)); | |
| 528 |
2/2✓ Branch 19 → 10 taken 153768 times.
✓ Branch 19 → 20 taken 96427 times.
|
250195 | for (const auto &[first, second] : args) { |
| 529 | 153768 | hashCombine64(hash, std::hash<QualType>{}(first)); | |
| 530 | 153768 | hashCombine64(hash, std::hash<bool>{}(second)); | |
| 531 | } | ||
| 532 | 96427 | hashCombine64(hash, hashVector(templateTypes)); | |
| 533 | 96427 | return hash; | |
| 534 | } | ||
| 535 | |||
| 536 | 989 | bool FunctionManager::hasCtor(const Scope *matchScope, bool lookForCopyCtor) { | |
| 537 |
5/8✓ Branch 2 → 3 taken 989 times.
✗ Branch 2 → 38 not taken.
✓ Branch 3 → 4 taken 989 times.
✗ Branch 3 → 38 not taken.
✓ Branch 4 → 5 taken 989 times.
✗ Branch 4 → 38 not taken.
✓ Branch 33 → 6 taken 5428 times.
✓ Branch 33 → 34 taken 519 times.
|
5947 | for (const auto &manifestations : matchScope->functions | std::views::values) { |
| 538 |
5/8✓ Branch 7 → 8 taken 5428 times.
✗ Branch 7 → 37 not taken.
✓ Branch 8 → 9 taken 5428 times.
✗ Branch 8 → 37 not taken.
✓ Branch 9 → 10 taken 5428 times.
✗ Branch 9 → 37 not taken.
✓ Branch 30 → 11 taken 5790 times.
✓ Branch 30 → 31 taken 4958 times.
|
10748 | for (const auto &function : manifestations | std::views::values) { |
| 539 | // If it is no ctor, skip it | ||
| 540 |
3/4✓ Branch 12 → 13 taken 5790 times.
✗ Branch 12 → 37 not taken.
✓ Branch 13 → 14 taken 3863 times.
✓ Branch 13 → 15 taken 1927 times.
|
5790 | if (function.name != CTOR_FUNCTION_NAME) |
| 541 | 3863 | continue; | |
| 542 | // If we don't search for a copy ctor and got none -> we found one | ||
| 543 | // If we search for a copy ctor and got one -> we found one | ||
| 544 |
6/8✓ Branch 16 → 17 taken 983 times.
✓ Branch 16 → 25 taken 944 times.
✓ Branch 17 → 18 taken 983 times.
✗ Branch 17 → 36 not taken.
✓ Branch 18 → 19 taken 983 times.
✗ Branch 18 → 36 not taken.
✓ Branch 19 → 20 taken 396 times.
✓ Branch 19 → 25 taken 587 times.
|
2323 | const bool isCopyCtor = function.paramList.size() == 1 && function.paramList.at(0).qualType.isConstRef() && |
| 545 |
5/8✓ Branch 20 → 21 taken 396 times.
✗ Branch 20 → 36 not taken.
✓ Branch 21 → 22 taken 396 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 396 times.
✗ Branch 22 → 36 not taken.
✓ Branch 23 → 24 taken 256 times.
✓ Branch 23 → 25 taken 140 times.
|
396 | function.paramList.at(0).qualType.getBase() == function.thisType; |
| 546 |
2/2✓ Branch 26 → 27 taken 470 times.
✓ Branch 26 → 28 taken 1457 times.
|
1927 | if (isCopyCtor == lookForCopyCtor) |
| 547 | 470 | return true; | |
| 548 | } | ||
| 549 | } | ||
| 550 | 519 | return false; | |
| 551 | } | ||
| 552 | |||
| 553 | 315 | bool FunctionManager::hasAnyNonCopyCtor(const Scope *matchScope) { return hasCtor(matchScope, false); } | |
| 554 | |||
| 555 | 674 | bool FunctionManager::hasCopyCtor(const Scope *matchScope) { return hasCtor(matchScope, true); } | |
| 556 | |||
| 557 | 4669 | bool FunctionManager::hasDtor(const Scope *matchScope) { | |
| 558 |
5/8✓ Branch 2 → 3 taken 4669 times.
✗ Branch 2 → 24 not taken.
✓ Branch 3 → 4 taken 4669 times.
✗ Branch 3 → 24 not taken.
✓ Branch 4 → 5 taken 4669 times.
✗ Branch 4 → 24 not taken.
✓ Branch 20 → 6 taken 33504 times.
✓ Branch 20 → 21 taken 2776 times.
|
36280 | for (const auto &manifestations : matchScope->functions | std::views::values) |
| 559 |
5/8✓ Branch 7 → 8 taken 33504 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 33504 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 33504 times.
✗ Branch 9 → 23 not taken.
✓ Branch 17 → 11 taken 37935 times.
✓ Branch 17 → 18 taken 31611 times.
|
69546 | for (const auto &function : manifestations | std::views::values) |
| 560 |
3/4✓ Branch 12 → 13 taken 37935 times.
✗ Branch 12 → 23 not taken.
✓ Branch 13 → 14 taken 1893 times.
✓ Branch 13 → 15 taken 36042 times.
|
37935 | if (function.name == DTOR_FUNCTION_NAME) |
| 561 | 1893 | return true; | |
| 562 | 2776 | return false; | |
| 563 | } | ||
| 564 | |||
| 565 | /** | ||
| 566 | * Clear the lookup cache | ||
| 567 | */ | ||
| 568 | 467 | void FunctionManager::cleanup() { | |
| 569 | 467 | lookupCache.clear(); | |
| 570 | 467 | lookupCacheHits = 0; | |
| 571 | 467 | lookupCacheMisses = 0; | |
| 572 | 467 | } | |
| 573 | |||
| 574 | /** | ||
| 575 | * Dump usage statistics for the lookup cache | ||
| 576 | */ | ||
| 577 | 230 | std::string FunctionManager::dumpLookupCacheStatistics() { | |
| 578 |
1/2✓ Branch 2 → 3 taken 230 times.
✗ Branch 2 → 22 not taken.
|
230 | std::stringstream stats; |
| 579 |
2/4✓ Branch 3 → 4 taken 230 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 230 times.
✗ Branch 4 → 20 not taken.
|
230 | stats << "FunctionManager lookup cache statistics:" << std::endl; |
| 580 |
3/6✓ Branch 5 → 6 taken 230 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 230 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 230 times.
✗ Branch 8 → 20 not taken.
|
230 | stats << " lookup cache entries: " << lookupCache.size() << std::endl; |
| 581 |
3/6✓ Branch 9 → 10 taken 230 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 230 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 230 times.
✗ Branch 11 → 20 not taken.
|
230 | stats << " lookup cache hits: " << lookupCacheHits << std::endl; |
| 582 |
3/6✓ Branch 12 → 13 taken 230 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 230 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 230 times.
✗ Branch 14 → 20 not taken.
|
230 | stats << " lookup cache misses: " << lookupCacheMisses << std::endl; |
| 583 |
1/2✓ Branch 15 → 16 taken 230 times.
✗ Branch 15 → 20 not taken.
|
460 | return stats.str(); |
| 584 | 230 | } | |
| 585 | |||
| 586 | } // namespace spice::compiler | ||
| 587 |