GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeMatcher.cpp
Date: 2025-11-03 22:22:45
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-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 3132 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 3132 times.
3132 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 3887 times.
✓ Branch 14 → 15 taken 3120 times.
7007 for (size_t i = 0; i < candidateTypes.size(); i++) {
18 3887 const QualType &reqType = reqTypes.at(i);
19 3887 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 3875 times.
3887 if (!matchRequestedToCandidateType(candidateType, reqType, typeMapping, resolverFct, strictQualifiers))
23 12 return false;
24 }
25 3120 return true;
26 }
27
28 74312 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 74312 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 53924 times.
✓ Branch 4 → 10 taken 20388 times.
74312 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 53922 times.
53924 if (candidateType.matchesInterfaceImplementedByStruct(requestedType))
37 2 return true;
38 // Normal equality check
39 53922 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 10095 times.
✓ Branch 11 → 83 taken 10293 times.
20388 if (candidateType.isBase(TY_GENERIC)) { // The candidate type itself is generic
44
1/2
✓ Branch 12 → 13 taken 10095 times.
✗ Branch 12 → 121 not taken.
10095 const QualType candidateBaseType = candidateType.getBase();
45
1/2
✓ Branch 13 → 14 taken 10095 times.
✗ Branch 13 → 121 not taken.
10095 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 10095 times.
✗ Branch 14 → 121 not taken.
✓ Branch 15 → 16 taken 1740 times.
✓ Branch 15 → 38 taken 8355 times.
10095 if (typeMapping.contains(genericTypeName)) { // This is a known generic type
49
1/2
✓ Branch 16 → 17 taken 1740 times.
✗ Branch 16 → 113 not taken.
1740 QualType knownConcreteType = typeMapping.at(genericTypeName);
50
51 // Merge qualifiers of candidate type and known concrete type together
52
1/2
✓ Branch 19 → 20 taken 1740 times.
✗ Branch 19 → 113 not taken.
1740 const TypeQualifiers mergedQualifiers = knownConcreteType.getQualifiers().merge(candidateType.getQualifiers());
53 1740 knownConcreteType.setQualifiers(mergedQualifiers);
54
55 // Remove reference wrapper of candidate type if required
56
3/4
✓ Branch 21 → 22 taken 1740 times.
✗ Branch 21 → 113 not taken.
✓ Branch 22 → 23 taken 1578 times.
✓ Branch 22 → 25 taken 162 times.
1740 if (!requestedType.isRef())
57
1/2
✓ Branch 23 → 24 taken 1578 times.
✗ Branch 23 → 110 not taken.
1578 knownConcreteType = knownConcreteType.removeReferenceWrapper();
58
59
8/10
✓ Branch 25 → 26 taken 1740 times.
✗ Branch 25 → 113 not taken.
✓ Branch 26 → 27 taken 162 times.
✓ Branch 26 → 30 taken 1578 times.
✓ Branch 27 → 28 taken 162 times.
✗ Branch 27 → 113 not taken.
✓ Branch 28 → 29 taken 4 times.
✓ Branch 28 → 30 taken 158 times.
✓ Branch 31 → 32 taken 4 times.
✓ Branch 31 → 35 taken 1736 times.
1740 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 1740 times.
✗ Branch 35 → 113 not taken.
1740 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 8355 times.
✗ Branch 38 → 120 not taken.
8355 const GenericType *genericCandidateBaseType = resolverFct(genericTypeName);
67
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 8355 times.
8355 assert(genericCandidateBaseType != nullptr);
68
69 // Check if the requested type fulfills all conditions of the generic candidate type
70 8355 QualType newBaseType;
71
3/4
✓ Branch 41 → 42 taken 8355 times.
✗ Branch 41 → 120 not taken.
✓ Branch 42 → 43 taken 2957 times.
✓ Branch 42 → 44 taken 5398 times.
8355 if (!genericCandidateBaseType->checkConditionsOf(requestedType, newBaseType, true, !strictQualifierMatching))
72 2957 return false;
73
74
1/2
✓ Branch 44 → 45 taken 5398 times.
✗ Branch 44 → 120 not taken.
5398 QualType substantiatedCandidateType = candidateType.replaceBaseType(newBaseType);
75
76 // Remove reference wrapper of candidate type if required
77
3/4
✓ Branch 45 → 46 taken 5398 times.
✗ Branch 45 → 120 not taken.
✓ Branch 46 → 47 taken 5066 times.
✓ Branch 46 → 49 taken 332 times.
5398 if (!requestedType.isRef())
78
1/2
✓ Branch 47 → 48 taken 5066 times.
✗ Branch 47 → 114 not taken.
5066 substantiatedCandidateType = substantiatedCandidateType.removeReferenceWrapper();
79
80
8/10
✓ Branch 49 → 50 taken 5398 times.
✗ Branch 49 → 120 not taken.
✓ Branch 50 → 51 taken 332 times.
✓ Branch 50 → 54 taken 5066 times.
✓ Branch 51 → 52 taken 332 times.
✗ Branch 51 → 120 not taken.
✓ Branch 52 → 53 taken 5 times.
✓ Branch 52 → 54 taken 327 times.
✓ Branch 55 → 56 taken 5 times.
✓ Branch 55 → 59 taken 5393 times.
5398 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 5398 times.
✗ Branch 59 → 120 not taken.
✓ Branch 60 → 61 taken 1 time.
✓ Branch 60 → 62 taken 5397 times.
5398 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 5397 times.
✗ Branch 64 → 120 not taken.
5397 requestedType.getQualifiers().eraseWithMask(substantiatedCandidateType.getQualifiers());
89
90 // Add to type mapping
91
3/4
✓ Branch 65 → 66 taken 5397 times.
✗ Branch 65 → 120 not taken.
✓ Branch 66 → 67 taken 1208 times.
✓ Branch 66 → 68 taken 4189 times.
5397 const QualType newMappingType = requestedType.hasAnyGenericParts() ? candidateBaseType : newBaseType;
92
6/10
✓ Branch 69 → 70 taken 5397 times.
✗ Branch 69 → 120 not taken.
✓ Branch 70 → 71 taken 4189 times.
✓ Branch 70 → 77 taken 1208 times.
✓ Branch 71 → 72 taken 4189 times.
✗ Branch 71 → 120 not taken.
✓ Branch 72 → 73 taken 4189 times.
✗ Branch 72 → 77 not taken.
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 4189 times.
5397 assert(newMappingType.is(TY_GENERIC) || newMappingType.is(TY_INVALID) ||
93 newMappingType.getQualifiers().isSigned != newMappingType.getQualifiers().isUnsigned);
94
2/4
✓ Branch 77 → 78 taken 5397 times.
✗ Branch 77 → 119 not taken.
✓ Branch 78 → 79 taken 5397 times.
✗ Branch 78 → 117 not taken.
5397 typeMapping.insert({genericTypeName, newMappingType});
95
96 5397 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 6983 times.
✓ Branch 85 → 87 taken 3310 times.
10293 if (requestedType.getSuperType() != candidateType.getSuperType())
101 6983 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 3310 times.
✗ Branch 87 → 122 not taken.
✓ Branch 88 → 89 taken 1 time.
✓ Branch 88 → 94 taken 3309 times.
3310 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 178 times.
✓ Branch 97 → 99 taken 3131 times.
3309 if (requestedType.getSubType() != candidateType.getSubType())
112 178 return false;
113
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 3131 times.
3131 if (requestedType.getBodyScope()->parent != candidateType.getBodyScope()->parent)
114 return false;
115
116 // Check template types
117 3131 const QualTypeList &candidateTTypes = candidateType.getTemplateTypes();
118 3131 const QualTypeList &requestedTTypes = requestedType.getTemplateTypes();
119
2/2
✓ Branch 106 → 107 taken 12 times.
✓ Branch 106 → 108 taken 3119 times.
3131 if (!matchRequestedToCandidateTypes(candidateTTypes, requestedTTypes, typeMapping, resolverFct, strictQualifierMatching))
120 12 return false;
121 }
122
123 3120 return true; // All requested template types match to their respective candidate template type -> successfully matched
124 }
125 }
126
127 1056 void TypeMatcher::substantiateTypesWithTypeMapping(QualTypeList &qualTypes, const TypeMapping &typeMapping, const ASTNode *node) {
128
2/2
✓ Branch 10 → 4 taken 170 times.
✓ Branch 10 → 11 taken 1056 times.
1226 for (QualType &qualType : qualTypes)
129
3/4
✓ Branch 5 → 6 taken 170 times.
✗ Branch 5 → 12 not taken.
✓ Branch 6 → 7 taken 156 times.
✓ Branch 6 → 8 taken 14 times.
170 if (qualType.hasAnyGenericParts())
130
1/2
✓ Branch 7 → 8 taken 156 times.
✗ Branch 7 → 12 not taken.
156 substantiateTypeWithTypeMapping(qualType, typeMapping, node);
131 1056 }
132
133 15520 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 15520 times.
15520 assert(type.hasAnyGenericParts());
136
137 // Check if the type itself is generic
138
2/2
✓ Branch 6 → 7 taken 10762 times.
✓ Branch 6 → 17 taken 4758 times.
15520 if (type.isBase(TY_GENERIC)) { // The symbol type itself is generic
139
3/6
✓ Branch 7 → 8 taken 10762 times.
✗ Branch 7 → 88 not taken.
✓ Branch 8 → 9 taken 10762 times.
✗ Branch 8 → 88 not taken.
✓ Branch 9 → 10 taken 10762 times.
✗ Branch 9 → 88 not taken.
10762 const std::string genericTypeName = type.getBase().getSubType();
140
2/4
✓ Branch 10 → 11 taken 10762 times.
✗ Branch 10 → 90 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 10762 times.
10762 assert(typeMapping.contains(genericTypeName));
141
1/2
✓ Branch 13 → 14 taken 10762 times.
✗ Branch 13 → 90 not taken.
10762 const QualType &replacementType = typeMapping.at(genericTypeName);
142
1/2
✓ Branch 14 → 15 taken 10762 times.
✗ Branch 14 → 89 not taken.
10762 type = type.replaceBaseType(replacementType);
143
4/6
✓ Branch 17 → 18 taken 4758 times.
✗ Branch 17 → 94 not taken.
✓ Branch 18 → 19 taken 4758 times.
✗ Branch 18 → 93 not taken.
✓ Branch 19 → 20 taken 19 times.
✓ Branch 19 → 34 taken 4739 times.
15520 } 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 4739 times.
✗ Branch 34 → 101 not taken.
✓ Branch 35 → 36 taken 4739 times.
✗ Branch 35 → 100 not taken.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 4739 times.
4739 assert(type.getBase().isOneOf({TY_STRUCT, TY_INTERFACE}));
153 // Substantiate each template type
154
1/2
✓ Branch 38 → 39 taken 4739 times.
✗ Branch 38 → 132 not taken.
4739 const QualType baseType = type.getBase();
155
2/4
✓ Branch 39 → 40 taken 4739 times.
✗ Branch 39 → 132 not taken.
✓ Branch 40 → 41 taken 4739 times.
✗ Branch 40 → 132 not taken.
4739 QualTypeList templateTypes = baseType.getTemplateTypes();
156
2/2
✓ Branch 49 → 43 taken 6181 times.
✓ Branch 49 → 50 taken 4739 times.
10920 for (QualType &templateType : templateTypes)
157
3/4
✓ Branch 44 → 45 taken 6181 times.
✗ Branch 44 → 102 not taken.
✓ Branch 45 → 46 taken 5886 times.
✓ Branch 45 → 47 taken 295 times.
6181 if (templateType.hasAnyGenericParts())
158
1/2
✓ Branch 46 → 47 taken 5886 times.
✗ Branch 46 → 102 not taken.
5886 substantiateTypeWithTypeMapping(templateType, typeMapping, node);
159 // Attach the list of concrete template types to the symbol type
160
1/2
✓ Branch 50 → 51 taken 4739 times.
✗ Branch 50 → 103 not taken.
4739 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 4739 times.
✗ Branch 51 → 130 not taken.
✓ Branch 52 → 53 taken 4401 times.
✓ Branch 52 → 85 taken 338 times.
4739 if (!baseType.isSelfReferencingStructType()) {
164
3/4
✓ Branch 53 → 54 taken 4401 times.
✗ Branch 53 → 130 not taken.
✓ Branch 54 → 55 taken 3993 times.
✓ Branch 54 → 70 taken 408 times.
4401 if (baseType.is(TY_STRUCT)) { // Struct
165
1/2
✓ Branch 55 → 56 taken 3993 times.
✗ Branch 55 → 130 not taken.
3993 const Struct *spiceStruct = baseType.getStruct(node, templateTypes);
166
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 68 taken 3993 times.
3993 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 3993 times.
✗ Branch 68 → 116 not taken.
3993 type = type.getWithBodyScope(spiceStruct->scope);
172 } else { // Interface
173
1/2
✓ Branch 70 → 71 taken 408 times.
✗ Branch 70 → 130 not taken.
408 const Interface *spiceInterface = baseType.getInterface(node, templateTypes);
174
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 83 taken 408 times.
408 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 408 times.
✗ Branch 83 → 129 not taken.
408 type = type.getWithBodyScope(spiceInterface->scope);
180 }
181 }
182 4739 }
183 15520 }
184
185 } // namespace spice::compiler
186