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