GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 88.8% 95 / 0 / 107
Functions: 100.0% 4 / 0 / 4
Branches: 57.6% 129 / 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 11081 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 11081 times.
11081 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 13431 times.
✓ Branch 14 → 15 taken 11044 times.
24475 for (size_t i = 0; i < candidateTypes.size(); i++) {
21 13431 const QualType &reqType = reqTypes.at(i);
22 13431 const QualType &candidateType = candidateTypes.at(i);
23
24 // Match the pair of template types
25
2/2
✓ Branch 10 → 11 taken 37 times.
✓ Branch 10 → 12 taken 13394 times.
13431 if (!matchRequestedToCandidateType(candidateType, reqType, typeMapping, resolverFct, strictQualifiers))
26 37 return false;
27 }
28 11044 return true;
29 }
30
31 192297 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 192297 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 128340 times.
✓ Branch 4 → 13 taken 63957 times.
192297 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 11 times.
✓ Branch 6 → 8 taken 128329 times.
128340 if (candidateType.matchesInterfaceImplementedByStruct(requestedType))
40 11 return true;
41 // Normal equality check
42
2/2
✓ Branch 9 → 10 taken 58407 times.
✓ Branch 9 → 11 taken 69922 times.
128329 if (candidateType.matches(requestedType, true, !strictQualifierMatching, true))
43 58407 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 69922 return candidateType.matchesComposedBaseOfStruct(requestedType);
47 }
48
49 // Check if the candidate type itself is generic
50
2/2
✓ Branch 14 → 15 taken 30762 times.
✓ Branch 14 → 86 taken 33195 times.
63957 if (candidateType.isBase(TY_GENERIC)) { // The candidate type itself is generic
51
1/2
✓ Branch 15 → 16 taken 30762 times.
✗ Branch 15 → 121 not taken.
30762 const QualType candidateBaseType = candidateType.getBase();
52
1/2
✓ Branch 16 → 17 taken 30762 times.
✗ Branch 16 → 121 not taken.
30762 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 30762 times.
✗ Branch 17 → 121 not taken.
30762 const auto it = typeMapping.find(genericTypeName);
56
2/2
✓ Branch 20 → 21 taken 7121 times.
✓ Branch 20 → 43 taken 23641 times.
30762 if (it != typeMapping.end()) { // This is a known generic type
57 7121 QualType knownConcreteType = it->second;
58
59 // Merge qualifiers of candidate type and known concrete type together
60
1/2
✓ Branch 24 → 25 taken 7121 times.
✗ Branch 24 → 116 not taken.
7121 const TypeQualifiers mergedQualifiers = knownConcreteType.getQualifiers().merge(candidateType.getQualifiers());
61 7121 knownConcreteType.setQualifiers(mergedQualifiers);
62
63 // Remove reference wrapper of candidate type if required
64
3/4
✓ Branch 26 → 27 taken 7121 times.
✗ Branch 26 → 116 not taken.
✓ Branch 27 → 28 taken 6462 times.
✓ Branch 27 → 30 taken 659 times.
7121 if (!requestedType.isRef())
65
1/2
✓ Branch 28 → 29 taken 6462 times.
✗ Branch 28 → 113 not taken.
6462 knownConcreteType = knownConcreteType.removeReferenceWrapper();
66
67
6/10
✓ Branch 30 → 31 taken 7121 times.
✗ Branch 30 → 116 not taken.
✓ Branch 31 → 32 taken 659 times.
✓ Branch 31 → 35 taken 6462 times.
✓ Branch 32 → 33 taken 659 times.
✗ Branch 32 → 116 not taken.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 659 times.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 40 taken 7121 times.
7121 if (requestedType.isRef() && !knownConcreteType.isRef())
68 requestedType = requestedType.getContained().toNonConst();
69
70 // Check if the known concrete type matches the requested type
71
1/2
✓ Branch 40 → 41 taken 7121 times.
✗ Branch 40 → 116 not taken.
7121 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 23641 times.
✗ Branch 43 → 120 not taken.
23641 const GenericType *genericCandidateBaseType = resolverFct(genericTypeName);
75
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 23641 times.
23641 assert(genericCandidateBaseType != nullptr);
76
77 // Check if the requested type fulfills all conditions of the generic candidate type
78 23641 QualType newBaseType;
79
3/4
✓ Branch 46 → 47 taken 23641 times.
✗ Branch 46 → 120 not taken.
✓ Branch 47 → 48 taken 5792 times.
✓ Branch 47 → 49 taken 17849 times.
23641 if (!genericCandidateBaseType->checkConditionsOf(requestedType, newBaseType, true, !strictQualifierMatching))
80 5792 return false;
81
82
1/2
✓ Branch 49 → 50 taken 17849 times.
✗ Branch 49 → 120 not taken.
17849 QualType substantiatedCandidateType = candidateType.replaceBaseType(newBaseType);
83
84 // Remove reference wrapper of candidate type if required
85
3/4
✓ Branch 50 → 51 taken 17849 times.
✗ Branch 50 → 120 not taken.
✓ Branch 51 → 52 taken 17012 times.
✓ Branch 51 → 54 taken 837 times.
17849 if (!requestedType.isRef())
86
1/2
✓ Branch 52 → 53 taken 17012 times.
✗ Branch 52 → 117 not taken.
17012 substantiatedCandidateType = substantiatedCandidateType.removeReferenceWrapper();
87
88
8/10
✓ Branch 54 → 55 taken 17849 times.
✗ Branch 54 → 120 not taken.
✓ Branch 55 → 56 taken 837 times.
✓ Branch 55 → 59 taken 17012 times.
✓ Branch 56 → 57 taken 837 times.
✗ Branch 56 → 120 not taken.
✓ Branch 57 → 58 taken 14 times.
✓ Branch 57 → 59 taken 823 times.
✓ Branch 60 → 61 taken 14 times.
✓ Branch 60 → 64 taken 17835 times.
17849 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 17849 times.
✗ Branch 64 → 120 not taken.
✓ Branch 65 → 66 taken 1 time.
✓ Branch 65 → 67 taken 17848 times.
17849 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 17848 times.
✗ Branch 69 → 120 not taken.
17848 requestedType.getQualifiers().eraseWithMask(substantiatedCandidateType.getQualifiers());
97
98 // Add to type mapping
99
3/4
✓ Branch 70 → 71 taken 17848 times.
✗ Branch 70 → 120 not taken.
✓ Branch 71 → 72 taken 2889 times.
✓ Branch 71 → 73 taken 14959 times.
17848 const QualType newMappingType = requestedType.hasAnyGenericParts() ? candidateBaseType : newBaseType;
100
6/10
✓ Branch 74 → 75 taken 17848 times.
✗ Branch 74 → 120 not taken.
✓ Branch 75 → 76 taken 14959 times.
✓ Branch 75 → 82 taken 2889 times.
✓ Branch 76 → 77 taken 14959 times.
✗ Branch 76 → 120 not taken.
✓ Branch 77 → 78 taken 14959 times.
✗ Branch 77 → 82 not taken.
✗ Branch 80 → 81 not taken.
✓ Branch 80 → 82 taken 14959 times.
17848 assert(newMappingType.is(TY_GENERIC) || newMappingType.is(TY_INVALID) ||
101 newMappingType.getQualifiers().isSigned != newMappingType.getQualifiers().isUnsigned);
102
1/2
✓ Branch 82 → 83 taken 17848 times.
✗ Branch 82 → 120 not taken.
17848 typeMapping.emplace(genericTypeName, newMappingType);
103
104 17848 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 21350 times.
✓ Branch 88 → 90 taken 11845 times.
33195 if (requestedType.getSuperType() != candidateType.getSuperType())
109 21350 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 11845 times.
✗ Branch 90 → 122 not taken.
✓ Branch 91 → 92 taken 5 times.
✓ Branch 91 → 97 taken 11840 times.
11845 if (candidateType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
113 // Check param and return types
114 5 const QualTypeList &candidatePRTypes = candidateType.getFunctionParamAndReturnTypes();
115 5 const QualTypeList &requestedPRTypes = requestedType.getFunctionParamAndReturnTypes();
116
1/2
✗ Branch 95 → 96 not taken.
✓ Branch 95 → 111 taken 5 times.
5 if (!matchRequestedToCandidateTypes(candidatePRTypes, requestedPRTypes, typeMapping, resolverFct, strictQualifierMatching))
117 return false;
118 } else {
119
2/2
✓ Branch 100 → 101 taken 764 times.
✓ Branch 100 → 102 taken 11076 times.
11840 if (requestedType.getSubType() != candidateType.getSubType())
120 764 return false;
121
1/2
✗ Branch 104 → 105 not taken.
✓ Branch 104 → 106 taken 11076 times.
11076 if (requestedType.getBodyScope()->parent != candidateType.getBodyScope()->parent)
122 return false;
123
124 // Check template types
125 11076 const QualTypeList &candidateTTypes = candidateType.getTemplateTypes();
126 11076 const QualTypeList &requestedTTypes = requestedType.getTemplateTypes();
127
2/2
✓ Branch 109 → 110 taken 37 times.
✓ Branch 109 → 111 taken 11039 times.
11076 if (!matchRequestedToCandidateTypes(candidateTTypes, requestedTTypes, typeMapping, resolverFct, strictQualifierMatching))
128 37 return false;
129 }
130
131 11044 return true; // All requested template types match to their respective candidate template type -> successfully matched
132 }
133 }
134
135 5086 void TypeMatcher::substantiateTypesWithTypeMapping(QualTypeList &qualTypes, const TypeMapping &typeMapping, const ASTNode *node) {
136
2/2
✓ Branch 18 → 4 taken 826 times.
✓ Branch 18 → 19 taken 5086 times.
10998 for (QualType &qualType : qualTypes)
137
3/4
✓ Branch 6 → 7 taken 826 times.
✗ Branch 6 → 20 not taken.
✓ Branch 7 → 8 taken 661 times.
✓ Branch 7 → 9 taken 165 times.
826 if (qualType.hasAnyGenericParts())
138
1/2
✓ Branch 8 → 9 taken 661 times.
✗ Branch 8 → 20 not taken.
661 substantiateTypeWithTypeMapping(qualType, typeMapping, node);
139 5086 }
140
141 54129 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 54129 times.
54129 assert(type.hasAnyGenericParts());
144
145 // Check if the type itself is generic
146
2/2
✓ Branch 6 → 7 taken 37460 times.
✓ Branch 6 → 17 taken 16669 times.
54129 if (type.isBase(TY_GENERIC)) { // The symbol type itself is generic
147
3/6
✓ Branch 7 → 8 taken 37460 times.
✗ Branch 7 → 104 not taken.
✓ Branch 8 → 9 taken 37460 times.
✗ Branch 8 → 104 not taken.
✓ Branch 9 → 10 taken 37460 times.
✗ Branch 9 → 104 not taken.
37460 const std::string genericTypeName = type.getBase().getSubType();
148
2/4
✓ Branch 10 → 11 taken 37460 times.
✗ Branch 10 → 106 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 37460 times.
37460 assert(typeMapping.contains(genericTypeName));
149
1/2
✓ Branch 13 → 14 taken 37460 times.
✗ Branch 13 → 106 not taken.
37460 const QualType &replacementType = typeMapping.at(genericTypeName);
150
1/2
✓ Branch 14 → 15 taken 37460 times.
✗ Branch 14 → 105 not taken.
37460 type = type.replaceBaseType(replacementType);
151
4/6
✓ Branch 17 → 18 taken 16669 times.
✗ Branch 17 → 110 not taken.
✓ Branch 18 → 19 taken 16669 times.
✗ Branch 18 → 109 not taken.
✓ Branch 19 → 20 taken 47 times.
✓ Branch 19 → 42 taken 16622 times.
54129 } 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 47 times.
✗ Branch 20 → 115 not taken.
✓ Branch 21 → 22 taken 47 times.
✗ Branch 21 → 115 not taken.
47 QualTypeList paramAndReturnTypes = type.getFunctionParamAndReturnTypes();
154
2/2
✓ Branch 38 → 24 taken 96 times.
✓ Branch 38 → 39 taken 47 times.
190 for (QualType &paramOrReturnType : paramAndReturnTypes)
155
3/4
✓ Branch 26 → 27 taken 96 times.
✗ Branch 26 → 111 not taken.
✓ Branch 27 → 28 taken 47 times.
✓ Branch 27 → 29 taken 49 times.
96 if (paramOrReturnType.hasAnyGenericParts())
156
1/2
✓ Branch 28 → 29 taken 47 times.
✗ Branch 28 → 111 not taken.
47 substantiateTypeWithTypeMapping(paramOrReturnType, typeMapping, node);
157 // Attach the list of concrete param types to the symbol type
158
1/2
✓ Branch 39 → 40 taken 47 times.
✗ Branch 39 → 112 not taken.
47 type = type.getWithFunctionParamAndReturnTypes(paramAndReturnTypes);
159 47 } else { // The base type is a struct or interface
160
3/6
✓ Branch 42 → 43 taken 16622 times.
✗ Branch 42 → 117 not taken.
✓ Branch 43 → 44 taken 16622 times.
✗ Branch 43 → 116 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 16622 times.
16622 assert(type.getBase().isOneOf({TY_STRUCT, TY_INTERFACE}));
161 // Substantiate each template type
162
1/2
✓ Branch 46 → 47 taken 16622 times.
✗ Branch 46 → 148 not taken.
16622 const QualType baseType = type.getBase();
163
2/4
✓ Branch 47 → 48 taken 16622 times.
✗ Branch 47 → 148 not taken.
✓ Branch 48 → 49 taken 16622 times.
✗ Branch 48 → 148 not taken.
16622 QualTypeList templateTypes = baseType.getTemplateTypes();
164
2/2
✓ Branch 65 → 51 taken 21381 times.
✓ Branch 65 → 66 taken 16622 times.
54625 for (QualType &templateType : templateTypes)
165
3/4
✓ Branch 53 → 54 taken 21381 times.
✗ Branch 53 → 118 not taken.
✓ Branch 54 → 55 taken 20137 times.
✓ Branch 54 → 56 taken 1244 times.
21381 if (templateType.hasAnyGenericParts())
166
1/2
✓ Branch 55 → 56 taken 20137 times.
✗ Branch 55 → 118 not taken.
20137 substantiateTypeWithTypeMapping(templateType, typeMapping, node);
167 // Attach the list of concrete template types to the symbol type
168
1/2
✓ Branch 66 → 67 taken 16622 times.
✗ Branch 66 → 119 not taken.
16622 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 16622 times.
✗ Branch 67 → 146 not taken.
✓ Branch 68 → 69 taken 15798 times.
✓ Branch 68 → 101 taken 824 times.
16622 if (!baseType.isSelfReferencingStructType()) {
172
3/4
✓ Branch 69 → 70 taken 15798 times.
✗ Branch 69 → 146 not taken.
✓ Branch 70 → 71 taken 14409 times.
✓ Branch 70 → 86 taken 1389 times.
15798 if (baseType.is(TY_STRUCT)) { // Struct
173
1/2
✓ Branch 71 → 72 taken 14409 times.
✗ Branch 71 → 146 not taken.
14409 const Struct *spiceStruct = baseType.getStruct(node, templateTypes);
174
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 84 taken 14409 times.
14409 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 14409 times.
✗ Branch 84 → 132 not taken.
14409 type = type.getWithBodyScope(spiceStruct->scope);
180 } else { // Interface
181
1/2
✓ Branch 86 → 87 taken 1389 times.
✗ Branch 86 → 146 not taken.
1389 const Interface *spiceInterface = baseType.getInterface(node, templateTypes);
182
1/2
✗ Branch 87 → 88 not taken.
✓ Branch 87 → 99 taken 1389 times.
1389 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 1389 times.
✗ Branch 99 → 145 not taken.
1389 type = type.getWithBodyScope(spiceInterface->scope);
188 }
189 }
190 16622 }
191 54129 }
192
193 } // namespace spice::compiler
194