GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeMatcher.cpp
Date: 2026-01-06 18:16:09
Coverage Exec Excl Total
Lines: 89.4% 93 0 104
Functions: 100.0% 4 0 4
Branches: 58.8% 133 0 226

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 <symboltablebuilder/Scope.h>
7
8 namespace spice::compiler {
9
10 3506 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 3506 times.
3506 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 4303 times.
✓ Branch 14 → 15 taken 3485 times.
7788 for (size_t i = 0; i < candidateTypes.size(); i++) {
18 4303 const QualType &reqType = reqTypes.at(i);
19 4303 const QualType &candidateType = candidateTypes.at(i);
20
21 // Match the pair of template types
22
2/2
✓ Branch 10 → 11 taken 21 times.
✓ Branch 10 → 12 taken 4282 times.
4303 if (!matchRequestedToCandidateType(candidateType, reqType, typeMapping, resolverFct, strictQualifiers))
23 21 return false;
24 }
25 3485 return true;
26 }
27
28 86429 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 86429 QualType::unwrapBothWithRefWrappers(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 61025 times.
✓ Branch 4 → 10 taken 25404 times.
86429 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 3 times.
✓ Branch 6 → 8 taken 61022 times.
61025 if (candidateType.matchesInterfaceImplementedByStruct(requestedType))
37 3 return true;
38 // Normal equality check
39 61022 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 13783 times.
✓ Branch 11 → 83 taken 11621 times.
25404 if (candidateType.isBase(TY_GENERIC)) { // The candidate type itself is generic
44
1/2
✓ Branch 12 → 13 taken 13783 times.
✗ Branch 12 → 121 not taken.
13783 const QualType candidateBaseType = candidateType.getBase();
45
1/2
✓ Branch 13 → 14 taken 13783 times.
✗ Branch 13 → 121 not taken.
13783 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 13783 times.
✗ Branch 14 → 121 not taken.
✓ Branch 15 → 16 taken 3417 times.
✓ Branch 15 → 38 taken 10366 times.
13783 if (typeMapping.contains(genericTypeName)) { // This is a known generic type
49
1/2
✓ Branch 16 → 17 taken 3417 times.
✗ Branch 16 → 113 not taken.
3417 QualType knownConcreteType = typeMapping.at(genericTypeName);
50
51 // Merge qualifiers of candidate type and known concrete type together
52
1/2
✓ Branch 19 → 20 taken 3417 times.
✗ Branch 19 → 113 not taken.
3417 const TypeQualifiers mergedQualifiers = knownConcreteType.getQualifiers().merge(candidateType.getQualifiers());
53 3417 knownConcreteType.setQualifiers(mergedQualifiers);
54
55 // Remove reference wrapper of candidate type if required
56
3/4
✓ Branch 21 → 22 taken 3417 times.
✗ Branch 21 → 113 not taken.
✓ Branch 22 → 23 taken 3246 times.
✓ Branch 22 → 25 taken 171 times.
3417 if (!requestedType.isRef())
57
1/2
✓ Branch 23 → 24 taken 3246 times.
✗ Branch 23 → 110 not taken.
3246 knownConcreteType = knownConcreteType.removeReferenceWrapper();
58
59
8/10
✓ Branch 25 → 26 taken 3417 times.
✗ Branch 25 → 113 not taken.
✓ Branch 26 → 27 taken 171 times.
✓ Branch 26 → 30 taken 3246 times.
✓ Branch 27 → 28 taken 171 times.
✗ Branch 27 → 113 not taken.
✓ Branch 28 → 29 taken 3 times.
✓ Branch 28 → 30 taken 168 times.
✓ Branch 31 → 32 taken 3 times.
✓ Branch 31 → 35 taken 3414 times.
3417 if (requestedType.isRef() && !knownConcreteType.isRef())
60
2/4
✓ Branch 32 → 33 taken 3 times.
✗ Branch 32 → 111 not taken.
✓ Branch 33 → 34 taken 3 times.
✗ Branch 33 → 111 not taken.
3 requestedType = requestedType.getContained().toNonConst();
61
62 // Check if the known concrete type matches the requested type
63
1/2
✓ Branch 35 → 36 taken 3417 times.
✗ Branch 35 → 113 not taken.
3417 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 10366 times.
✗ Branch 38 → 120 not taken.
10366 const GenericType *genericCandidateBaseType = resolverFct(genericTypeName);
67
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 10366 times.
10366 assert(genericCandidateBaseType != nullptr);
68
69 // Check if the requested type fulfills all conditions of the generic candidate type
70 10366 QualType newBaseType;
71
3/4
✓ Branch 41 → 42 taken 10366 times.
✗ Branch 41 → 120 not taken.
✓ Branch 42 → 43 taken 3256 times.
✓ Branch 42 → 44 taken 7110 times.
10366 if (!genericCandidateBaseType->checkConditionsOf(requestedType, newBaseType, true, !strictQualifierMatching))
72 3256 return false;
73
74
1/2
✓ Branch 44 → 45 taken 7110 times.
✗ Branch 44 → 120 not taken.
7110 QualType substantiatedCandidateType = candidateType.replaceBaseType(newBaseType);
75
76 // Remove reference wrapper of candidate type if required
77
3/4
✓ Branch 45 → 46 taken 7110 times.
✗ Branch 45 → 120 not taken.
✓ Branch 46 → 47 taken 6762 times.
✓ Branch 46 → 49 taken 348 times.
7110 if (!requestedType.isRef())
78
1/2
✓ Branch 47 → 48 taken 6762 times.
✗ Branch 47 → 114 not taken.
6762 substantiatedCandidateType = substantiatedCandidateType.removeReferenceWrapper();
79
80
8/10
✓ Branch 49 → 50 taken 7110 times.
✗ Branch 49 → 120 not taken.
✓ Branch 50 → 51 taken 348 times.
✓ Branch 50 → 54 taken 6762 times.
✓ Branch 51 → 52 taken 348 times.
✗ Branch 51 → 120 not taken.
✓ Branch 52 → 53 taken 9 times.
✓ Branch 52 → 54 taken 339 times.
✓ Branch 55 → 56 taken 9 times.
✓ Branch 55 → 59 taken 7101 times.
7110 if (requestedType.isRef() && !substantiatedCandidateType.isRef())
81
2/4
✓ Branch 56 → 57 taken 9 times.
✗ Branch 56 → 115 not taken.
✓ Branch 57 → 58 taken 9 times.
✗ Branch 57 → 115 not taken.
9 requestedType = requestedType.getContained().toNonConst();
82
83
3/4
✓ Branch 59 → 60 taken 7110 times.
✗ Branch 59 → 120 not taken.
✓ Branch 60 → 61 taken 1 time.
✓ Branch 60 → 62 taken 7109 times.
7110 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 7109 times.
✗ Branch 64 → 120 not taken.
7109 requestedType.getQualifiers().eraseWithMask(substantiatedCandidateType.getQualifiers());
89
90 // Add to type mapping
91
3/4
✓ Branch 65 → 66 taken 7109 times.
✗ Branch 65 → 120 not taken.
✓ Branch 66 → 67 taken 1585 times.
✓ Branch 66 → 68 taken 5524 times.
7109 const QualType newMappingType = requestedType.hasAnyGenericParts() ? candidateBaseType : newBaseType;
92
6/10
✓ Branch 69 → 70 taken 7109 times.
✗ Branch 69 → 120 not taken.
✓ Branch 70 → 71 taken 5524 times.
✓ Branch 70 → 77 taken 1585 times.
✓ Branch 71 → 72 taken 5524 times.
✗ Branch 71 → 120 not taken.
✓ Branch 72 → 73 taken 5524 times.
✗ Branch 72 → 77 not taken.
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 5524 times.
7109 assert(newMappingType.is(TY_GENERIC) || newMappingType.is(TY_INVALID) ||
93 newMappingType.getQualifiers().isSigned != newMappingType.getQualifiers().isUnsigned);
94
2/4
✓ Branch 77 → 78 taken 7109 times.
✗ Branch 77 → 119 not taken.
✓ Branch 78 → 79 taken 7109 times.
✗ Branch 78 → 117 not taken.
7109 typeMapping.insert({genericTypeName, newMappingType});
95
96 7109 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 7903 times.
✓ Branch 85 → 87 taken 3718 times.
11621 if (requestedType.getSuperType() != candidateType.getSuperType())
101 7903 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 3718 times.
✗ Branch 87 → 122 not taken.
✓ Branch 88 → 89 taken 1 time.
✓ Branch 88 → 94 taken 3717 times.
3718 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 212 times.
✓ Branch 97 → 99 taken 3505 times.
3717 if (requestedType.getSubType() != candidateType.getSubType())
112 212 return false;
113
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 3505 times.
3505 if (requestedType.getBodyScope()->parent != candidateType.getBodyScope()->parent)
114 return false;
115
116 // Check template types
117 3505 const QualTypeList &candidateTTypes = candidateType.getTemplateTypes();
118 3505 const QualTypeList &requestedTTypes = requestedType.getTemplateTypes();
119
2/2
✓ Branch 106 → 107 taken 21 times.
✓ Branch 106 → 108 taken 3484 times.
3505 if (!matchRequestedToCandidateTypes(candidateTTypes, requestedTTypes, typeMapping, resolverFct, strictQualifierMatching))
120 21 return false;
121 }
122
123 3485 return true; // All requested template types match to their respective candidate template type -> successfully matched
124 }
125 }
126
127 1128 void TypeMatcher::substantiateTypesWithTypeMapping(QualTypeList &qualTypes, const TypeMapping &typeMapping, const ASTNode *node) {
128
2/2
✓ Branch 10 → 4 taken 180 times.
✓ Branch 10 → 11 taken 1128 times.
1308 for (QualType &qualType : qualTypes)
129
3/4
✓ Branch 5 → 6 taken 180 times.
✗ Branch 5 → 12 not taken.
✓ Branch 6 → 7 taken 166 times.
✓ Branch 6 → 8 taken 14 times.
180 if (qualType.hasAnyGenericParts())
130
1/2
✓ Branch 7 → 8 taken 166 times.
✗ Branch 7 → 12 not taken.
166 substantiateTypeWithTypeMapping(qualType, typeMapping, node);
131 1128 }
132
133 19468 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 19468 times.
19468 assert(type.hasAnyGenericParts());
136
137 // Check if the type itself is generic
138
2/2
✓ Branch 6 → 7 taken 14273 times.
✓ Branch 6 → 17 taken 5195 times.
19468 if (type.isBase(TY_GENERIC)) { // The symbol type itself is generic
139
3/6
✓ Branch 7 → 8 taken 14273 times.
✗ Branch 7 → 88 not taken.
✓ Branch 8 → 9 taken 14273 times.
✗ Branch 8 → 88 not taken.
✓ Branch 9 → 10 taken 14273 times.
✗ Branch 9 → 88 not taken.
14273 const std::string genericTypeName = type.getBase().getSubType();
140
2/4
✓ Branch 10 → 11 taken 14273 times.
✗ Branch 10 → 90 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 14273 times.
14273 assert(typeMapping.contains(genericTypeName));
141
1/2
✓ Branch 13 → 14 taken 14273 times.
✗ Branch 13 → 90 not taken.
14273 const QualType &replacementType = typeMapping.at(genericTypeName);
142
1/2
✓ Branch 14 → 15 taken 14273 times.
✗ Branch 14 → 89 not taken.
14273 type = type.replaceBaseType(replacementType);
143
4/6
✓ Branch 17 → 18 taken 5195 times.
✗ Branch 17 → 94 not taken.
✓ Branch 18 → 19 taken 5195 times.
✗ Branch 18 → 93 not taken.
✓ Branch 19 → 20 taken 19 times.
✓ Branch 19 → 34 taken 5176 times.
19468 } 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 &paramOrReturnType : 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 5176 times.
✗ Branch 34 → 101 not taken.
✓ Branch 35 → 36 taken 5176 times.
✗ Branch 35 → 100 not taken.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 5176 times.
5176 assert(type.getBase().isOneOf({TY_STRUCT, TY_INTERFACE}));
153 // Substantiate each template type
154
1/2
✓ Branch 38 → 39 taken 5176 times.
✗ Branch 38 → 132 not taken.
5176 const QualType baseType = type.getBase();
155
2/4
✓ Branch 39 → 40 taken 5176 times.
✗ Branch 39 → 132 not taken.
✓ Branch 40 → 41 taken 5176 times.
✗ Branch 40 → 132 not taken.
5176 QualTypeList templateTypes = baseType.getTemplateTypes();
156
2/2
✓ Branch 49 → 43 taken 6678 times.
✓ Branch 49 → 50 taken 5176 times.
11854 for (QualType &templateType : templateTypes)
157
3/4
✓ Branch 44 → 45 taken 6678 times.
✗ Branch 44 → 102 not taken.
✓ Branch 45 → 46 taken 6365 times.
✓ Branch 45 → 47 taken 313 times.
6678 if (templateType.hasAnyGenericParts())
158
1/2
✓ Branch 46 → 47 taken 6365 times.
✗ Branch 46 → 102 not taken.
6365 substantiateTypeWithTypeMapping(templateType, typeMapping, node);
159 // Attach the list of concrete template types to the symbol type
160
1/2
✓ Branch 50 → 51 taken 5176 times.
✗ Branch 50 → 103 not taken.
5176 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 5176 times.
✗ Branch 51 → 130 not taken.
✓ Branch 52 → 53 taken 4838 times.
✓ Branch 52 → 85 taken 338 times.
5176 if (!baseType.isSelfReferencingStructType()) {
164
3/4
✓ Branch 53 → 54 taken 4838 times.
✗ Branch 53 → 130 not taken.
✓ Branch 54 → 55 taken 4400 times.
✓ Branch 54 → 70 taken 438 times.
4838 if (baseType.is(TY_STRUCT)) { // Struct
165
1/2
✓ Branch 55 → 56 taken 4400 times.
✗ Branch 55 → 130 not taken.
4400 const Struct *spiceStruct = baseType.getStruct(node, templateTypes);
166
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 68 taken 4400 times.
4400 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 4400 times.
✗ Branch 68 → 116 not taken.
4400 type = type.getWithBodyScope(spiceStruct->scope);
172 } else { // Interface
173
1/2
✓ Branch 70 → 71 taken 438 times.
✗ Branch 70 → 130 not taken.
438 const Interface *spiceInterface = baseType.getInterface(node, templateTypes);
174
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 83 taken 438 times.
438 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 438 times.
✗ Branch 83 → 129 not taken.
438 type = type.getWithBodyScope(spiceInterface->scope);
180 }
181 }
182 5176 }
183 19468 }
184
185 } // namespace spice::compiler
186