GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 89.7% 96 / 0 / 107
Functions: 100.0% 4 / 0 / 4
Branches: 59.4% 133 / 0 / 224

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 15079 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 15079 times.
15079 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 18047 times.
✓ Branch 14 → 15 taken 15018 times.
33065 for (size_t i = 0; i < candidateTypes.size(); i++) {
21 18047 const QualType &reqType = reqTypes.at(i);
22 18047 const QualType &candidateType = candidateTypes.at(i);
23
24 // Match the pair of template types
25
2/2
✓ Branch 10 → 11 taken 61 times.
✓ Branch 10 → 12 taken 17986 times.
18047 if (!matchRequestedToCandidateType(candidateType, reqType, typeMapping, resolverFct, strictQualifiers))
26 61 return false;
27 }
28 15018 return true;
29 }
30
31 254443 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 254443 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 171441 times.
✓ Branch 4 → 13 taken 83002 times.
254443 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 17 times.
✓ Branch 6 → 8 taken 171424 times.
171441 if (candidateType.matchesInterfaceImplementedByStruct(requestedType))
40 17 return true;
41 // Normal equality check
42
2/2
✓ Branch 9 → 10 taken 85456 times.
✓ Branch 9 → 11 taken 85968 times.
171424 if (candidateType.matches(requestedType, true, !strictQualifierMatching, true))
43 85456 return true;
44 // As a fallback, check if the right one is a struct that composes the struct on the left as its
45 // first field. This is kept last so the common (non-composing) path does not pay for the lookup.
46 85968 return candidateType.matchesComposedBaseOfStruct(requestedType);
47 }
48
49 // Check if the candidate type itself is generic
50
2/2
✓ Branch 14 → 15 taken 38557 times.
✓ Branch 14 → 86 taken 44445 times.
83002 if (candidateType.isBase(TY_GENERIC)) { // The candidate type itself is generic
51
1/2
✓ Branch 15 → 16 taken 38557 times.
✗ Branch 15 → 121 not taken.
38557 const QualType candidateBaseType = candidateType.getBase();
52
1/2
✓ Branch 16 → 17 taken 38557 times.
✗ Branch 16 → 121 not taken.
38557 const std::string &genericTypeName = candidateBaseType.getSubType();
53
54 // Check if we know the concrete type for that generic type name already
55
1/2
✓ Branch 17 → 18 taken 38557 times.
✗ Branch 17 → 121 not taken.
38557 const auto it = typeMapping.find(genericTypeName);
56
2/2
✓ Branch 20 → 21 taken 9137 times.
✓ Branch 20 → 43 taken 29420 times.
38557 if (it != typeMapping.end()) { // This is a known generic type
57 9137 QualType knownConcreteType = it->second;
58
59 // Merge qualifiers of candidate type and known concrete type together
60
1/2
✓ Branch 24 → 25 taken 9137 times.
✗ Branch 24 → 116 not taken.
9137 const TypeQualifiers mergedQualifiers = knownConcreteType.getQualifiers().merge(candidateType.getQualifiers());
61 9137 knownConcreteType.setQualifiers(mergedQualifiers);
62
63 // Remove reference wrapper of candidate type if required
64
3/4
✓ Branch 26 → 27 taken 9137 times.
✗ Branch 26 → 116 not taken.
✓ Branch 27 → 28 taken 8148 times.
✓ Branch 27 → 30 taken 989 times.
9137 if (!requestedType.isRef())
65
1/2
✓ Branch 28 → 29 taken 8148 times.
✗ Branch 28 → 113 not taken.
8148 knownConcreteType = knownConcreteType.removeReferenceWrapper();
66
67
8/10
✓ Branch 30 → 31 taken 9137 times.
✗ Branch 30 → 116 not taken.
✓ Branch 31 → 32 taken 989 times.
✓ Branch 31 → 35 taken 8148 times.
✓ Branch 32 → 33 taken 989 times.
✗ Branch 32 → 116 not taken.
✓ Branch 33 → 34 taken 26 times.
✓ Branch 33 → 35 taken 963 times.
✓ Branch 36 → 37 taken 26 times.
✓ Branch 36 → 40 taken 9111 times.
9137 if (requestedType.isRef() && !knownConcreteType.isRef())
68
2/4
✓ Branch 37 → 38 taken 26 times.
✗ Branch 37 → 114 not taken.
✓ Branch 38 → 39 taken 26 times.
✗ Branch 38 → 114 not taken.
26 requestedType = requestedType.getContained().toNonConst();
69
70 // Check if the known concrete type matches the requested type
71
1/2
✓ Branch 40 → 41 taken 9137 times.
✗ Branch 40 → 116 not taken.
9137 return knownConcreteType.matches(requestedType, true, !strictQualifierMatching, true);
72 } else { // This is an unknown generic type
73 // Retrieve generic candidate type by its name
74
1/2
✓ Branch 43 → 44 taken 29420 times.
✗ Branch 43 → 120 not taken.
29420 const GenericType *genericCandidateBaseType = resolverFct(genericTypeName);
75
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 29420 times.
29420 assert(genericCandidateBaseType != nullptr);
76
77 // Check if the requested type fulfills all conditions of the generic candidate type
78 29420 QualType newBaseType;
79
3/4
✓ Branch 46 → 47 taken 29420 times.
✗ Branch 46 → 120 not taken.
✓ Branch 47 → 48 taken 6437 times.
✓ Branch 47 → 49 taken 22983 times.
29420 if (!genericCandidateBaseType->checkConditionsOf(requestedType, newBaseType, true, !strictQualifierMatching))
80 6437 return false;
81
82
1/2
✓ Branch 49 → 50 taken 22983 times.
✗ Branch 49 → 120 not taken.
22983 QualType substantiatedCandidateType = candidateType.replaceBaseType(newBaseType);
83
84 // Remove reference wrapper of candidate type if required
85
3/4
✓ Branch 50 → 51 taken 22983 times.
✗ Branch 50 → 120 not taken.
✓ Branch 51 → 52 taken 21926 times.
✓ Branch 51 → 54 taken 1057 times.
22983 if (!requestedType.isRef())
86
1/2
✓ Branch 52 → 53 taken 21926 times.
✗ Branch 52 → 117 not taken.
21926 substantiatedCandidateType = substantiatedCandidateType.removeReferenceWrapper();
87
88
8/10
✓ Branch 54 → 55 taken 22983 times.
✗ Branch 54 → 120 not taken.
✓ Branch 55 → 56 taken 1057 times.
✓ Branch 55 → 59 taken 21926 times.
✓ Branch 56 → 57 taken 1057 times.
✗ Branch 56 → 120 not taken.
✓ Branch 57 → 58 taken 14 times.
✓ Branch 57 → 59 taken 1043 times.
✓ Branch 60 → 61 taken 14 times.
✓ Branch 60 → 64 taken 22969 times.
22983 if (requestedType.isRef() && !substantiatedCandidateType.isRef())
89
2/4
✓ Branch 61 → 62 taken 14 times.
✗ Branch 61 → 118 not taken.
✓ Branch 62 → 63 taken 14 times.
✗ Branch 62 → 118 not taken.
14 requestedType = requestedType.getContained().toNonConst();
90
91
3/4
✓ Branch 64 → 65 taken 22983 times.
✗ Branch 64 → 120 not taken.
✓ Branch 65 → 66 taken 1 time.
✓ Branch 65 → 67 taken 22982 times.
22983 if (!substantiatedCandidateType.matches(requestedType, true, !strictQualifierMatching, true))
92 1 return false;
93
94 // Zero out all qualifiers in the requested type, that are present in the candidate type
95 // This is to set all qualifiers that are not present in the candidate type to the generic type replacement
96
1/2
✓ Branch 69 → 70 taken 22982 times.
✗ Branch 69 → 120 not taken.
22982 requestedType.getQualifiers().eraseWithMask(substantiatedCandidateType.getQualifiers());
97
98 // Add to type mapping
99
3/4
✓ Branch 70 → 71 taken 22982 times.
✗ Branch 70 → 120 not taken.
✓ Branch 71 → 72 taken 2093 times.
✓ Branch 71 → 73 taken 20889 times.
22982 const QualType newMappingType = requestedType.hasAnyGenericParts() ? candidateBaseType : newBaseType;
100
6/10
✓ Branch 74 → 75 taken 22982 times.
✗ Branch 74 → 120 not taken.
✓ Branch 75 → 76 taken 20889 times.
✓ Branch 75 → 82 taken 2093 times.
✓ Branch 76 → 77 taken 20889 times.
✗ Branch 76 → 120 not taken.
✓ Branch 77 → 78 taken 20889 times.
✗ Branch 77 → 82 not taken.
✗ Branch 80 → 81 not taken.
✓ Branch 80 → 82 taken 20889 times.
22982 assert(newMappingType.is(TY_GENERIC) || newMappingType.is(TY_INVALID) ||
101 newMappingType.getQualifiers().isSigned != newMappingType.getQualifiers().isUnsigned);
102
1/2
✓ Branch 82 → 83 taken 22982 times.
✗ Branch 82 → 120 not taken.
22982 typeMapping.emplace(genericTypeName, newMappingType);
103
104 22982 return true; // The type was successfully matched, by enriching the type mapping
105 }
106 } else { // The candidate type itself is non-generic, but one or several template or param types are
107 // Check if supertype and subtype are equal
108
2/2
✓ Branch 88 → 89 taken 28315 times.
✓ Branch 88 → 90 taken 16130 times.
44445 if (requestedType.getSuperType() != candidateType.getSuperType())
109 28315 return false;
110
111 // If we have a function/procedure type, check the param and return types. Otherwise, check the template types
112
3/4
✓ Branch 90 → 91 taken 16130 times.
✗ Branch 90 → 122 not taken.
✓ Branch 91 → 92 taken 8 times.
✓ Branch 91 → 97 taken 16122 times.
16130 if (candidateType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
113 // Check param and return types
114 8 const QualTypeList &candidatePRTypes = candidateType.getFunctionParamAndReturnTypes();
115 8 const QualTypeList &requestedPRTypes = requestedType.getFunctionParamAndReturnTypes();
116
1/2
✗ Branch 95 → 96 not taken.
✓ Branch 95 → 111 taken 8 times.
8 if (!matchRequestedToCandidateTypes(candidatePRTypes, requestedPRTypes, typeMapping, resolverFct, strictQualifierMatching))
117 return false;
118 } else {
119
2/2
✓ Branch 100 → 101 taken 1051 times.
✓ Branch 100 → 102 taken 15071 times.
16122 if (requestedType.getSubType() != candidateType.getSubType())
120 1051 return false;
121
1/2
✗ Branch 104 → 105 not taken.
✓ Branch 104 → 106 taken 15071 times.
15071 if (requestedType.getBodyScope()->parent != candidateType.getBodyScope()->parent)
122 return false;
123
124 // Check template types
125 15071 const QualTypeList &candidateTTypes = candidateType.getTemplateTypes();
126 15071 const QualTypeList &requestedTTypes = requestedType.getTemplateTypes();
127
2/2
✓ Branch 109 → 110 taken 61 times.
✓ Branch 109 → 111 taken 15010 times.
15071 if (!matchRequestedToCandidateTypes(candidateTTypes, requestedTTypes, typeMapping, resolverFct, strictQualifierMatching))
128 61 return false;
129 }
130
131 15018 return true; // All requested template types match to their respective candidate template type -> successfully matched
132 }
133 }
134
135 7749 void TypeMatcher::substantiateTypesWithTypeMapping(QualTypeList &qualTypes, const TypeMapping &typeMapping, const ASTNode *node) {
136
2/2
✓ Branch 18 → 4 taken 1723 times.
✓ Branch 18 → 19 taken 7749 times.
17221 for (QualType &qualType : qualTypes)
137
3/4
✓ Branch 6 → 7 taken 1723 times.
✗ Branch 6 → 20 not taken.
✓ Branch 7 → 8 taken 965 times.
✓ Branch 7 → 9 taken 758 times.
1723 if (qualType.hasAnyGenericParts())
138
1/2
✓ Branch 8 → 9 taken 965 times.
✗ Branch 8 → 20 not taken.
965 substantiateTypeWithTypeMapping(qualType, typeMapping, node);
139 7749 }
140
141 71392 void TypeMatcher::substantiateTypeWithTypeMapping(QualType &type, const TypeMapping &typeMapping, // NOLINT(*-no-recursion)
142 const ASTNode *node) {
143
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 71392 times.
71392 assert(type.hasAnyGenericParts());
144
145 // Check if the type itself is generic
146
2/2
✓ Branch 6 → 7 taken 48942 times.
✓ Branch 6 → 17 taken 22450 times.
71392 if (type.isBase(TY_GENERIC)) { // The symbol type itself is generic
147
3/6
✓ Branch 7 → 8 taken 48942 times.
✗ Branch 7 → 104 not taken.
✓ Branch 8 → 9 taken 48942 times.
✗ Branch 8 → 104 not taken.
✓ Branch 9 → 10 taken 48942 times.
✗ Branch 9 → 104 not taken.
48942 const std::string genericTypeName = type.getBase().getSubType();
148
2/4
✓ Branch 10 → 11 taken 48942 times.
✗ Branch 10 → 106 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 48942 times.
48942 assert(typeMapping.contains(genericTypeName));
149
1/2
✓ Branch 13 → 14 taken 48942 times.
✗ Branch 13 → 106 not taken.
48942 const QualType &replacementType = typeMapping.at(genericTypeName);
150
1/2
✓ Branch 14 → 15 taken 48942 times.
✗ Branch 14 → 105 not taken.
48942 type = type.replaceBaseType(replacementType);
151
4/6
✓ Branch 17 → 18 taken 22450 times.
✗ Branch 17 → 110 not taken.
✓ Branch 18 → 19 taken 22450 times.
✗ Branch 18 → 109 not taken.
✓ Branch 19 → 20 taken 39 times.
✓ Branch 19 → 42 taken 22411 times.
71392 } else if (type.getBase().isOneOf({TY_FUNCTION, TY_PROCEDURE})) { // The base type is a function or procedure
152 // Substantiate each param or return type
153
2/4
✓ Branch 20 → 21 taken 39 times.
✗ Branch 20 → 115 not taken.
✓ Branch 21 → 22 taken 39 times.
✗ Branch 21 → 115 not taken.
39 QualTypeList paramAndReturnTypes = type.getFunctionParamAndReturnTypes();
154
2/2
✓ Branch 38 → 24 taken 80 times.
✓ Branch 38 → 39 taken 39 times.
158 for (QualType &paramOrReturnType : paramAndReturnTypes)
155
3/4
✓ Branch 26 → 27 taken 80 times.
✗ Branch 26 → 111 not taken.
✓ Branch 27 → 28 taken 39 times.
✓ Branch 27 → 29 taken 41 times.
80 if (paramOrReturnType.hasAnyGenericParts())
156
1/2
✓ Branch 28 → 29 taken 39 times.
✗ Branch 28 → 111 not taken.
39 substantiateTypeWithTypeMapping(paramOrReturnType, typeMapping, node);
157 // Attach the list of concrete param types to the symbol type
158
1/2
✓ Branch 39 → 40 taken 39 times.
✗ Branch 39 → 112 not taken.
39 type = type.getWithFunctionParamAndReturnTypes(paramAndReturnTypes);
159 39 } else { // The base type is a struct or interface
160
3/6
✓ Branch 42 → 43 taken 22411 times.
✗ Branch 42 → 117 not taken.
✓ Branch 43 → 44 taken 22411 times.
✗ Branch 43 → 116 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 22411 times.
22411 assert(type.getBase().isOneOf({TY_STRUCT, TY_INTERFACE}));
161 // Substantiate each template type
162
1/2
✓ Branch 46 → 47 taken 22411 times.
✗ Branch 46 → 148 not taken.
22411 const QualType baseType = type.getBase();
163
2/4
✓ Branch 47 → 48 taken 22411 times.
✗ Branch 47 → 148 not taken.
✓ Branch 48 → 49 taken 22411 times.
✗ Branch 48 → 148 not taken.
22411 QualTypeList templateTypes = baseType.getTemplateTypes();
164
2/2
✓ Branch 65 → 51 taken 28555 times.
✓ Branch 65 → 66 taken 22411 times.
73377 for (QualType &templateType : templateTypes)
165
3/4
✓ Branch 53 → 54 taken 28555 times.
✗ Branch 53 → 118 not taken.
✓ Branch 54 → 55 taken 26824 times.
✓ Branch 54 → 56 taken 1731 times.
28555 if (templateType.hasAnyGenericParts())
166
1/2
✓ Branch 55 → 56 taken 26824 times.
✗ Branch 55 → 118 not taken.
26824 substantiateTypeWithTypeMapping(templateType, typeMapping, node);
167 // Attach the list of concrete template types to the symbol type
168
1/2
✓ Branch 66 → 67 taken 22411 times.
✗ Branch 66 → 119 not taken.
22411 type = type.getWithBaseTemplateTypes(templateTypes);
169 // Lookup the scope of the concrete struct or interface type
170 // Only do this, if the struct or interface is not self-referencing, because in that case we'd end up in an infinite recursion
171
3/4
✓ Branch 67 → 68 taken 22411 times.
✗ Branch 67 → 146 not taken.
✓ Branch 68 → 69 taken 21603 times.
✓ Branch 68 → 101 taken 808 times.
22411 if (!baseType.isSelfReferencingStructType()) {
172
3/4
✓ Branch 69 → 70 taken 21603 times.
✗ Branch 69 → 146 not taken.
✓ Branch 70 → 71 taken 19776 times.
✓ Branch 70 → 86 taken 1827 times.
21603 if (baseType.is(TY_STRUCT)) { // Struct
173
1/2
✓ Branch 71 → 72 taken 19776 times.
✗ Branch 71 → 146 not taken.
19776 const Struct *spiceStruct = baseType.getStruct(node, templateTypes);
174
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 84 taken 19776 times.
19776 if (!spiceStruct) {
175 assert(node != nullptr);
176 const std::string signature = Struct::getSignature(baseType.getSubType(), templateTypes);
177 throw SemanticError(node, UNKNOWN_DATATYPE, "Could not find struct '" + signature + "'");
178 }
179
1/2
✓ Branch 84 → 85 taken 19776 times.
✗ Branch 84 → 132 not taken.
19776 type = type.getWithBodyScope(spiceStruct->scope);
180 } else { // Interface
181
1/2
✓ Branch 86 → 87 taken 1827 times.
✗ Branch 86 → 146 not taken.
1827 const Interface *spiceInterface = baseType.getInterface(node, templateTypes);
182
1/2
✗ Branch 87 → 88 not taken.
✓ Branch 87 → 99 taken 1827 times.
1827 if (!spiceInterface) {
183 assert(node != nullptr);
184 const std::string signature = Interface::getSignature(baseType.getSubType(), templateTypes);
185 throw SemanticError(node, UNKNOWN_DATATYPE, "Could not find interface '" + signature + "'");
186 }
187
1/2
✓ Branch 99 → 100 taken 1827 times.
✗ Branch 99 → 145 not taken.
1827 type = type.getWithBodyScope(spiceInterface->scope);
188 }
189 }
190 22411 }
191 71392 }
192
193 } // namespace spice::compiler
194