src/typechecker/TypeMatcher.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeMatcher.h" | ||
| 4 | |||
| 5 | #include <exception/SemanticError.h> | ||
| 6 | #include <model/GenericType.h> | ||
| 7 | #include <model/Interface.h> | ||
| 8 | #include <model/Struct.h> | ||
| 9 | #include <symboltablebuilder/Scope.h> | ||
| 10 | |||
| 11 | namespace spice::compiler { | ||
| 12 | |||
| 13 | 3725 | bool TypeMatcher::matchRequestedToCandidateTypes(const QualTypeList &candidateTypes, const QualTypeList &reqTypes, | |
| 14 | TypeMapping &typeMapping, ResolverFct &resolverFct, bool strictQualifiers) { | ||
| 15 | // Check if the size of template types matches | ||
| 16 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3725 times.
|
3725 | if (reqTypes.size() != candidateTypes.size()) |
| 17 | ✗ | return false; | |
| 18 | |||
| 19 | // Loop through both lists at the same time and match each pair of template types individually | ||
| 20 |
2/2✓ Branch 14 → 7 taken 4624 times.
✓ Branch 14 → 15 taken 3709 times.
|
8333 | for (size_t i = 0; i < candidateTypes.size(); i++) { |
| 21 | 4624 | const QualType &reqType = reqTypes.at(i); | |
| 22 | 4624 | const QualType &candidateType = candidateTypes.at(i); | |
| 23 | |||
| 24 | // Match the pair of template types | ||
| 25 |
2/2✓ Branch 10 → 11 taken 16 times.
✓ Branch 10 → 12 taken 4608 times.
|
4624 | if (!matchRequestedToCandidateType(candidateType, reqType, typeMapping, resolverFct, strictQualifiers)) |
| 26 | 16 | return false; | |
| 27 | } | ||
| 28 | 3709 | return true; | |
| 29 | } | ||
| 30 | |||
| 31 | 91395 | bool TypeMatcher::matchRequestedToCandidateType(QualType candidateType, QualType requestedType, TypeMapping &typeMapping, | |
| 32 | ResolverFct &resolverFct, bool strictQualifierMatching) { | ||
| 33 | // Unwrap as far as possible and remove reference wrappers if possible | ||
| 34 | 91395 | QualType::unwrapBothWithRefWrappers(candidateType, requestedType); | |
| 35 | |||
| 36 | // If the candidate does not contain any generic parts, we can simply check for type equality | ||
| 37 |
2/2✓ Branch 4 → 5 taken 64175 times.
✓ Branch 4 → 10 taken 27220 times.
|
91395 | if (!candidateType.hasAnyGenericParts()) { |
| 38 | // Check if the right one is a struct that implements the interface on the left | ||
| 39 |
2/2✓ Branch 6 → 7 taken 4 times.
✓ Branch 6 → 8 taken 64171 times.
|
64175 | if (candidateType.matchesInterfaceImplementedByStruct(requestedType)) |
| 40 | 4 | return true; | |
| 41 | // Normal equality check | ||
| 42 | 64171 | return candidateType.matches(requestedType, true, !strictQualifierMatching, true); | |
| 43 | } | ||
| 44 | |||
| 45 | // Check if the candidate type itself is generic | ||
| 46 |
2/2✓ Branch 11 → 12 taken 14839 times.
✓ Branch 11 → 83 taken 12381 times.
|
27220 | if (candidateType.isBase(TY_GENERIC)) { // The candidate type itself is generic |
| 47 |
1/2✓ Branch 12 → 13 taken 14839 times.
✗ Branch 12 → 118 not taken.
|
14839 | const QualType candidateBaseType = candidateType.getBase(); |
| 48 |
1/2✓ Branch 13 → 14 taken 14839 times.
✗ Branch 13 → 118 not taken.
|
14839 | const std::string &genericTypeName = candidateBaseType.getSubType(); |
| 49 | |||
| 50 | // Check if we know the concrete type for that generic type name already | ||
| 51 |
1/2✓ Branch 14 → 15 taken 14839 times.
✗ Branch 14 → 118 not taken.
|
14839 | const auto it = typeMapping.find(genericTypeName); |
| 52 |
2/2✓ Branch 17 → 18 taken 3684 times.
✓ Branch 17 → 40 taken 11155 times.
|
14839 | if (it != typeMapping.end()) { // This is a known generic type |
| 53 | 3684 | QualType knownConcreteType = it->second; | |
| 54 | |||
| 55 | // Merge qualifiers of candidate type and known concrete type together | ||
| 56 |
1/2✓ Branch 21 → 22 taken 3684 times.
✗ Branch 21 → 113 not taken.
|
3684 | const TypeQualifiers mergedQualifiers = knownConcreteType.getQualifiers().merge(candidateType.getQualifiers()); |
| 57 | 3684 | knownConcreteType.setQualifiers(mergedQualifiers); | |
| 58 | |||
| 59 | // Remove reference wrapper of candidate type if required | ||
| 60 |
3/4✓ Branch 23 → 24 taken 3684 times.
✗ Branch 23 → 113 not taken.
✓ Branch 24 → 25 taken 3497 times.
✓ Branch 24 → 27 taken 187 times.
|
3684 | if (!requestedType.isRef()) |
| 61 |
1/2✓ Branch 25 → 26 taken 3497 times.
✗ Branch 25 → 110 not taken.
|
3497 | knownConcreteType = knownConcreteType.removeReferenceWrapper(); |
| 62 | |||
| 63 |
6/10✓ Branch 27 → 28 taken 3684 times.
✗ Branch 27 → 113 not taken.
✓ Branch 28 → 29 taken 187 times.
✓ Branch 28 → 32 taken 3497 times.
✓ Branch 29 → 30 taken 187 times.
✗ Branch 29 → 113 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 187 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 37 taken 3684 times.
|
3684 | if (requestedType.isRef() && !knownConcreteType.isRef()) |
| 64 | ✗ | requestedType = requestedType.getContained().toNonConst(); | |
| 65 | |||
| 66 | // Check if the known concrete type matches the requested type | ||
| 67 |
1/2✓ Branch 37 → 38 taken 3684 times.
✗ Branch 37 → 113 not taken.
|
3684 | return knownConcreteType.matches(requestedType, true, !strictQualifierMatching, true); |
| 68 | } else { // This is an unknown generic type | ||
| 69 | // Retrieve generic candidate type by its name | ||
| 70 |
1/2✓ Branch 40 → 41 taken 11155 times.
✗ Branch 40 → 117 not taken.
|
11155 | const GenericType *genericCandidateBaseType = resolverFct(genericTypeName); |
| 71 |
1/2✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 11155 times.
|
11155 | assert(genericCandidateBaseType != nullptr); |
| 72 | |||
| 73 | // Check if the requested type fulfills all conditions of the generic candidate type | ||
| 74 | 11155 | QualType newBaseType; | |
| 75 |
3/4✓ Branch 43 → 44 taken 11155 times.
✗ Branch 43 → 117 not taken.
✓ Branch 44 → 45 taken 3681 times.
✓ Branch 44 → 46 taken 7474 times.
|
11155 | if (!genericCandidateBaseType->checkConditionsOf(requestedType, newBaseType, true, !strictQualifierMatching)) |
| 76 | 3681 | return false; | |
| 77 | |||
| 78 |
1/2✓ Branch 46 → 47 taken 7474 times.
✗ Branch 46 → 117 not taken.
|
7474 | QualType substantiatedCandidateType = candidateType.replaceBaseType(newBaseType); |
| 79 | |||
| 80 | // Remove reference wrapper of candidate type if required | ||
| 81 |
3/4✓ Branch 47 → 48 taken 7474 times.
✗ Branch 47 → 117 not taken.
✓ Branch 48 → 49 taken 7109 times.
✓ Branch 48 → 51 taken 365 times.
|
7474 | if (!requestedType.isRef()) |
| 82 |
1/2✓ Branch 49 → 50 taken 7109 times.
✗ Branch 49 → 114 not taken.
|
7109 | substantiatedCandidateType = substantiatedCandidateType.removeReferenceWrapper(); |
| 83 | |||
| 84 |
8/10✓ Branch 51 → 52 taken 7474 times.
✗ Branch 51 → 117 not taken.
✓ Branch 52 → 53 taken 365 times.
✓ Branch 52 → 56 taken 7109 times.
✓ Branch 53 → 54 taken 365 times.
✗ Branch 53 → 117 not taken.
✓ Branch 54 → 55 taken 9 times.
✓ Branch 54 → 56 taken 356 times.
✓ Branch 57 → 58 taken 9 times.
✓ Branch 57 → 61 taken 7465 times.
|
7474 | if (requestedType.isRef() && !substantiatedCandidateType.isRef()) |
| 85 |
2/4✓ Branch 58 → 59 taken 9 times.
✗ Branch 58 → 115 not taken.
✓ Branch 59 → 60 taken 9 times.
✗ Branch 59 → 115 not taken.
|
9 | requestedType = requestedType.getContained().toNonConst(); |
| 86 | |||
| 87 |
3/4✓ Branch 61 → 62 taken 7474 times.
✗ Branch 61 → 117 not taken.
✓ Branch 62 → 63 taken 1 time.
✓ Branch 62 → 64 taken 7473 times.
|
7474 | if (!substantiatedCandidateType.matches(requestedType, true, !strictQualifierMatching, true)) |
| 88 | 1 | return false; | |
| 89 | |||
| 90 | // Zero out all qualifiers in the requested type, that are present in the candidate type | ||
| 91 | // This is to set all qualifiers that are not present in the candidate type to the generic type replacement | ||
| 92 |
1/2✓ Branch 66 → 67 taken 7473 times.
✗ Branch 66 → 117 not taken.
|
7473 | requestedType.getQualifiers().eraseWithMask(substantiatedCandidateType.getQualifiers()); |
| 93 | |||
| 94 | // Add to type mapping | ||
| 95 |
3/4✓ Branch 67 → 68 taken 7473 times.
✗ Branch 67 → 117 not taken.
✓ Branch 68 → 69 taken 1473 times.
✓ Branch 68 → 70 taken 6000 times.
|
7473 | const QualType newMappingType = requestedType.hasAnyGenericParts() ? candidateBaseType : newBaseType; |
| 96 |
6/10✓ Branch 71 → 72 taken 7473 times.
✗ Branch 71 → 117 not taken.
✓ Branch 72 → 73 taken 6000 times.
✓ Branch 72 → 79 taken 1473 times.
✓ Branch 73 → 74 taken 6000 times.
✗ Branch 73 → 117 not taken.
✓ Branch 74 → 75 taken 6000 times.
✗ Branch 74 → 79 not taken.
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 6000 times.
|
7473 | assert(newMappingType.is(TY_GENERIC) || newMappingType.is(TY_INVALID) || |
| 97 | newMappingType.getQualifiers().isSigned != newMappingType.getQualifiers().isUnsigned); | ||
| 98 |
1/2✓ Branch 79 → 80 taken 7473 times.
✗ Branch 79 → 117 not taken.
|
7473 | typeMapping.emplace(genericTypeName, newMappingType); |
| 99 | |||
| 100 | 7473 | return true; // The type was successfully matched, by enriching the type mapping | |
| 101 | } | ||
| 102 | } else { // The candidate type itself is non-generic, but one or several template or param types are | ||
| 103 | // Check if supertype and subtype are equal | ||
| 104 |
2/2✓ Branch 85 → 86 taken 8411 times.
✓ Branch 85 → 87 taken 3970 times.
|
12381 | if (requestedType.getSuperType() != candidateType.getSuperType()) |
| 105 | 8411 | return false; | |
| 106 | |||
| 107 | // If we have a function/procedure type, check the param and return types. Otherwise, check the template types | ||
| 108 |
3/4✓ Branch 87 → 88 taken 3970 times.
✗ Branch 87 → 119 not taken.
✓ Branch 88 → 89 taken 3 times.
✓ Branch 88 → 94 taken 3967 times.
|
3970 | if (candidateType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) { |
| 109 | // Check param and return types | ||
| 110 | 3 | const QualTypeList &candidatePRTypes = candidateType.getFunctionParamAndReturnTypes(); | |
| 111 | 3 | const QualTypeList &requestedPRTypes = requestedType.getFunctionParamAndReturnTypes(); | |
| 112 |
1/2✗ Branch 92 → 93 not taken.
✓ Branch 92 → 108 taken 3 times.
|
3 | if (!matchRequestedToCandidateTypes(candidatePRTypes, requestedPRTypes, typeMapping, resolverFct, strictQualifierMatching)) |
| 113 | ✗ | return false; | |
| 114 | } else { | ||
| 115 |
2/2✓ Branch 97 → 98 taken 245 times.
✓ Branch 97 → 99 taken 3722 times.
|
3967 | if (requestedType.getSubType() != candidateType.getSubType()) |
| 116 | 245 | return false; | |
| 117 |
1/2✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 3722 times.
|
3722 | if (requestedType.getBodyScope()->parent != candidateType.getBodyScope()->parent) |
| 118 | ✗ | return false; | |
| 119 | |||
| 120 | // Check template types | ||
| 121 | 3722 | const QualTypeList &candidateTTypes = candidateType.getTemplateTypes(); | |
| 122 | 3722 | const QualTypeList &requestedTTypes = requestedType.getTemplateTypes(); | |
| 123 |
2/2✓ Branch 106 → 107 taken 16 times.
✓ Branch 106 → 108 taken 3706 times.
|
3722 | if (!matchRequestedToCandidateTypes(candidateTTypes, requestedTTypes, typeMapping, resolverFct, strictQualifierMatching)) |
| 124 | 16 | return false; | |
| 125 | } | ||
| 126 | |||
| 127 | 3709 | return true; // All requested template types match to their respective candidate template type -> successfully matched | |
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | 1433 | void TypeMatcher::substantiateTypesWithTypeMapping(QualTypeList &qualTypes, const TypeMapping &typeMapping, const ASTNode *node) { | |
| 132 |
2/2✓ Branch 18 → 4 taken 203 times.
✓ Branch 18 → 19 taken 1433 times.
|
3069 | for (QualType &qualType : qualTypes) |
| 133 |
3/4✓ Branch 6 → 7 taken 203 times.
✗ Branch 6 → 20 not taken.
✓ Branch 7 → 8 taken 189 times.
✓ Branch 7 → 9 taken 14 times.
|
203 | if (qualType.hasAnyGenericParts()) |
| 134 |
1/2✓ Branch 8 → 9 taken 189 times.
✗ Branch 8 → 20 not taken.
|
189 | substantiateTypeWithTypeMapping(qualType, typeMapping, node); |
| 135 | 1433 | } | |
| 136 | |||
| 137 | 21334 | void TypeMatcher::substantiateTypeWithTypeMapping(QualType &type, const TypeMapping &typeMapping, // NOLINT(*-no-recursion) | |
| 138 | const ASTNode *node) { | ||
| 139 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 21334 times.
|
21334 | assert(type.hasAnyGenericParts()); |
| 140 | |||
| 141 | // Check if the type itself is generic | ||
| 142 |
2/2✓ Branch 6 → 7 taken 15541 times.
✓ Branch 6 → 17 taken 5793 times.
|
21334 | if (type.isBase(TY_GENERIC)) { // The symbol type itself is generic |
| 143 |
3/6✓ Branch 7 → 8 taken 15541 times.
✗ Branch 7 → 104 not taken.
✓ Branch 8 → 9 taken 15541 times.
✗ Branch 8 → 104 not taken.
✓ Branch 9 → 10 taken 15541 times.
✗ Branch 9 → 104 not taken.
|
15541 | const std::string genericTypeName = type.getBase().getSubType(); |
| 144 |
2/4✓ Branch 10 → 11 taken 15541 times.
✗ Branch 10 → 106 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 15541 times.
|
15541 | assert(typeMapping.contains(genericTypeName)); |
| 145 |
1/2✓ Branch 13 → 14 taken 15541 times.
✗ Branch 13 → 106 not taken.
|
15541 | const QualType &replacementType = typeMapping.at(genericTypeName); |
| 146 |
1/2✓ Branch 14 → 15 taken 15541 times.
✗ Branch 14 → 105 not taken.
|
15541 | type = type.replaceBaseType(replacementType); |
| 147 |
4/6✓ Branch 17 → 18 taken 5793 times.
✗ Branch 17 → 110 not taken.
✓ Branch 18 → 19 taken 5793 times.
✗ Branch 18 → 109 not taken.
✓ Branch 19 → 20 taken 21 times.
✓ Branch 19 → 42 taken 5772 times.
|
21334 | } else if (type.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE})) { // The base type is a function or procedure |
| 148 | // Substantiate each param or return type | ||
| 149 |
2/4✓ Branch 20 → 21 taken 21 times.
✗ Branch 20 → 115 not taken.
✓ Branch 21 → 22 taken 21 times.
✗ Branch 21 → 115 not taken.
|
21 | QualTypeList paramAndReturnTypes = type.getFunctionParamAndReturnTypes(); |
| 150 |
2/2✓ Branch 38 → 24 taken 44 times.
✓ Branch 38 → 39 taken 21 times.
|
86 | for (QualType ¶mOrReturnType : paramAndReturnTypes) |
| 151 |
3/4✓ Branch 26 → 27 taken 44 times.
✗ Branch 26 → 111 not taken.
✓ Branch 27 → 28 taken 21 times.
✓ Branch 27 → 29 taken 23 times.
|
44 | if (paramOrReturnType.hasAnyGenericParts()) |
| 152 |
1/2✓ Branch 28 → 29 taken 21 times.
✗ Branch 28 → 111 not taken.
|
21 | substantiateTypeWithTypeMapping(paramOrReturnType, typeMapping, node); |
| 153 | // Attach the list of concrete param types to the symbol type | ||
| 154 |
1/2✓ Branch 39 → 40 taken 21 times.
✗ Branch 39 → 112 not taken.
|
21 | type = type.getWithFunctionParamAndReturnTypes(paramAndReturnTypes); |
| 155 | 21 | } else { // The base type is a struct or interface | |
| 156 |
3/6✓ Branch 42 → 43 taken 5772 times.
✗ Branch 42 → 117 not taken.
✓ Branch 43 → 44 taken 5772 times.
✗ Branch 43 → 116 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 5772 times.
|
5772 | assert(type.getBase().isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 157 | // Substantiate each template type | ||
| 158 |
1/2✓ Branch 46 → 47 taken 5772 times.
✗ Branch 46 → 148 not taken.
|
5772 | const QualType baseType = type.getBase(); |
| 159 |
2/4✓ Branch 47 → 48 taken 5772 times.
✗ Branch 47 → 148 not taken.
✓ Branch 48 → 49 taken 5772 times.
✗ Branch 48 → 148 not taken.
|
5772 | QualTypeList templateTypes = baseType.getTemplateTypes(); |
| 160 |
2/2✓ Branch 65 → 51 taken 7526 times.
✓ Branch 65 → 66 taken 5772 times.
|
19070 | for (QualType &templateType : templateTypes) |
| 161 |
3/4✓ Branch 53 → 54 taken 7526 times.
✗ Branch 53 → 118 not taken.
✓ Branch 54 → 55 taken 7137 times.
✓ Branch 54 → 56 taken 389 times.
|
7526 | if (templateType.hasAnyGenericParts()) |
| 162 |
1/2✓ Branch 55 → 56 taken 7137 times.
✗ Branch 55 → 118 not taken.
|
7137 | substantiateTypeWithTypeMapping(templateType, typeMapping, node); |
| 163 | // Attach the list of concrete template types to the symbol type | ||
| 164 |
1/2✓ Branch 66 → 67 taken 5772 times.
✗ Branch 66 → 119 not taken.
|
5772 | type = type.getWithBaseTemplateTypes(templateTypes); |
| 165 | // Lookup the scope of the concrete struct or interface type | ||
| 166 | // Only do this, if the struct or interface is not self-referencing, because in that case we'd end up in an infinite recursion | ||
| 167 |
3/4✓ Branch 67 → 68 taken 5772 times.
✗ Branch 67 → 146 not taken.
✓ Branch 68 → 69 taken 5366 times.
✓ Branch 68 → 101 taken 406 times.
|
5772 | if (!baseType.isSelfReferencingStructType()) { |
| 168 |
3/4✓ Branch 69 → 70 taken 5366 times.
✗ Branch 69 → 146 not taken.
✓ Branch 70 → 71 taken 4847 times.
✓ Branch 70 → 86 taken 519 times.
|
5366 | if (baseType.is(TY_STRUCT)) { // Struct |
| 169 |
1/2✓ Branch 71 → 72 taken 4847 times.
✗ Branch 71 → 146 not taken.
|
4847 | const Struct *spiceStruct = baseType.getStruct(node, templateTypes); |
| 170 |
1/2✗ Branch 72 → 73 not taken.
✓ Branch 72 → 84 taken 4847 times.
|
4847 | if (!spiceStruct) { |
| 171 | ✗ | assert(node != nullptr); | |
| 172 | ✗ | const std::string signature = Struct::getSignature(baseType.getSubType(), templateTypes); | |
| 173 | ✗ | throw SemanticError(node, UNKNOWN_DATATYPE, "Could not find struct '" + signature + "'"); | |
| 174 | ✗ | } | |
| 175 |
1/2✓ Branch 84 → 85 taken 4847 times.
✗ Branch 84 → 132 not taken.
|
4847 | type = type.getWithBodyScope(spiceStruct->scope); |
| 176 | } else { // Interface | ||
| 177 |
1/2✓ Branch 86 → 87 taken 519 times.
✗ Branch 86 → 146 not taken.
|
519 | const Interface *spiceInterface = baseType.getInterface(node, templateTypes); |
| 178 |
1/2✗ Branch 87 → 88 not taken.
✓ Branch 87 → 99 taken 519 times.
|
519 | if (!spiceInterface) { |
| 179 | ✗ | assert(node != nullptr); | |
| 180 | ✗ | const std::string signature = Interface::getSignature(baseType.getSubType(), templateTypes); | |
| 181 | ✗ | throw SemanticError(node, UNKNOWN_DATATYPE, "Could not find interface '" + signature + "'"); | |
| 182 | ✗ | } | |
| 183 |
1/2✓ Branch 99 → 100 taken 519 times.
✗ Branch 99 → 145 not taken.
|
519 | type = type.getWithBodyScope(spiceInterface->scope); |
| 184 | } | ||
| 185 | } | ||
| 186 | 5772 | } | |
| 187 | 21334 | } | |
| 188 | |||
| 189 | } // namespace spice::compiler | ||
| 190 |