GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeCheckerCheck.cpp
Date: 2025-02-05 01:09:36
Exec Total Coverage
Lines: 124 124 100.0%
Functions: 4 4 100.0%
Branches: 150 226 66.4%

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "TypeChecker.h"
4
5 #include <SourceFile.h>
6 #include <exception/SemanticError.h>
7 #include <symboltablebuilder/SymbolTableBuilder.h>
8 #include <typechecker/TypeMatcher.h>
9
10 namespace spice::compiler {
11
12 392 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
13 // Skip if already type-checked
14
2/2
✓ Branch 0 (2→3) taken 40 times.
✓ Branch 1 (2→5) taken 352 times.
392 if (typeCheckedMainFct)
15
1/2
✓ Branch 0 (3→4) taken 40 times.
✗ Branch 1 (3→11) not taken.
40 return nullptr;
16
17 352 node->resizeToNumberOfManifestations(1);
18
19 // Change to function body scope
20 352 currentScope = node->bodyScope;
21 // Visit statements in new scope
22
2/2
✓ Branch 0 (6→7) taken 324 times.
✓ Branch 1 (6→12) taken 28 times.
352 visit(node->body);
23 // Leave main function body scope
24 324 currentScope = rootScope;
25
26 // Set to type-checked
27 324 typeCheckedMainFct = true;
28
1/2
✓ Branch 0 (8→9) taken 324 times.
✗ Branch 1 (8→13) not taken.
324 return nullptr;
29 }
30
31 12588 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
32 12588 node->resizeToNumberOfManifestations(node->manifestations.size());
33 12588 manIdx = 0; // Reset the manifestation index
34
35 // Get all manifestations for this function definition
36
2/2
✓ Branch 0 (50→6) taken 15729 times.
✓ Branch 1 (50→51) taken 12586 times.
28315 for (Function *manifestation : node->manifestations) {
37 // Skip non-substantiated or already checked functions
38
7/8
✓ Branch 0 (7→8) taken 15729 times.
✗ Branch 1 (7→68) not taken.
✓ Branch 2 (8→9) taken 12514 times.
✓ Branch 3 (8→10) taken 3215 times.
✓ Branch 4 (9→10) taken 6732 times.
✓ Branch 5 (9→11) taken 5782 times.
✓ Branch 6 (12→13) taken 9947 times.
✓ Branch 7 (12→14) taken 5782 times.
15729 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
39 9947 manIdx++; // Increase the manifestation index
40 9947 continue;
41 }
42
43 // Change scope to concrete struct specialization scope
44
2/2
✓ Branch 0 (14→15) taken 3030 times.
✓ Branch 1 (14→22) taken 2752 times.
5782 if (node->isMethod) {
45
2/4
✓ Branch 0 (15→16) taken 3030 times.
✗ Branch 1 (15→59) not taken.
✓ Branch 2 (16→17) taken 3030 times.
✗ Branch 3 (16→59) not taken.
3030 const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes());
46
2/4
✓ Branch 0 (17→18) taken 3030 times.
✗ Branch 1 (17→56) not taken.
✓ Branch 2 (18→19) taken 3030 times.
✗ Branch 3 (18→54) not taken.
3030 changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT);
47 3030 }
48
49 // Change to function scope
50
1/2
✓ Branch 0 (22→23) taken 5782 times.
✗ Branch 1 (22→68) not taken.
5782 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
51
52 // Mount type mapping for this manifestation
53
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 5782 times.
5782 assert(typeMapping.empty());
54
1/2
✓ Branch 0 (26→27) taken 5782 times.
✗ Branch 1 (26→68) not taken.
5782 typeMapping = manifestation->typeMapping;
55
56 // Set return type to the result variable
57
1/2
✓ Branch 0 (29→30) taken 5782 times.
✗ Branch 1 (29→62) not taken.
17346 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
58
1/2
✗ Branch 0 (35→36) not taken.
✓ Branch 1 (35→37) taken 5782 times.
5782 assert(resultVarEntry != nullptr);
59
1/2
✓ Branch 0 (37→38) taken 5782 times.
✗ Branch 1 (37→68) not taken.
5782 resultVarEntry->updateType(manifestation->returnType, false);
60 5782 resultVarEntry->used = true;
61
62 // Visit parameters
63 // This happens once in the type checker prepare stage. This second time is only required if we have a generic function
64
2/2
✓ Branch 0 (38→39) taken 4485 times.
✓ Branch 1 (38→42) taken 1297 times.
5782 if (node->hasParams)
65
1/2
✓ Branch 0 (39→40) taken 4485 times.
✗ Branch 1 (39→66) not taken.
4485 visit(node->paramLst);
66
67 // Visit statements in new scope
68
2/2
✓ Branch 0 (42→43) taken 5780 times.
✓ Branch 1 (42→67) taken 2 times.
5782 visit(node->body);
69
70 // Clear type mapping
71 5780 typeMapping.clear();
72
73 // Change to root scope
74 5780 currentScope = rootScope;
75
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 5780 times.
5780 assert(currentScope->type == ScopeType::GLOBAL);
76
77 // Do not type-check this manifestation again
78 5780 manifestation->alreadyTypeChecked = true;
79
80 5780 manIdx++; // Increase the manifestation index
81 }
82 12586 manIdx = 0; // Reset the manifestation index
83
84
1/2
✓ Branch 0 (51→52) taken 12586 times.
✗ Branch 1 (51→69) not taken.
12586 return nullptr;
85 }
86
87 6011 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
88 6011 node->resizeToNumberOfManifestations(node->manifestations.size());
89 6011 manIdx = 0; // Reset the manifestation index
90
91 // Get all manifestations for this procedure definition
92
2/2
✓ Branch 0 (43→6) taken 7563 times.
✓ Branch 1 (43→44) taken 6011 times.
13574 for (Function *manifestation : node->manifestations) {
93 // Skip non-substantiated or already checked procedures
94
7/8
✓ Branch 0 (7→8) taken 7563 times.
✗ Branch 1 (7→55) not taken.
✓ Branch 2 (8→9) taken 5024 times.
✓ Branch 3 (8→10) taken 2539 times.
✓ Branch 4 (9→10) taken 2399 times.
✓ Branch 5 (9→11) taken 2625 times.
✓ Branch 6 (12→13) taken 4938 times.
✓ Branch 7 (12→14) taken 2625 times.
7563 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
95 4938 manIdx++; // Increase the manifestation index
96 4938 continue;
97 }
98
99 // Change scope to concrete struct specialization scope
100
2/2
✓ Branch 0 (14→15) taken 2415 times.
✓ Branch 1 (14→22) taken 210 times.
2625 if (node->isMethod) {
101
2/4
✓ Branch 0 (15→16) taken 2415 times.
✗ Branch 1 (15→52) not taken.
✓ Branch 2 (16→17) taken 2415 times.
✗ Branch 3 (16→52) not taken.
2415 const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes());
102
2/4
✓ Branch 0 (17→18) taken 2415 times.
✗ Branch 1 (17→49) not taken.
✓ Branch 2 (18→19) taken 2415 times.
✗ Branch 3 (18→47) not taken.
2415 changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT);
103 2415 }
104
105 // Change to procedure scope
106
1/2
✓ Branch 0 (22→23) taken 2625 times.
✗ Branch 1 (22→55) not taken.
2625 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
107
108 // Mount type mapping for this manifestation
109
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 2625 times.
2625 assert(typeMapping.empty());
110
1/2
✓ Branch 0 (26→27) taken 2625 times.
✗ Branch 1 (26→55) not taken.
2625 typeMapping = manifestation->typeMapping;
111
112 // Visit parameters
113 // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure
114
2/2
✓ Branch 0 (27→28) taken 1808 times.
✓ Branch 1 (27→31) taken 817 times.
2625 if (node->hasParams)
115
1/2
✓ Branch 0 (28→29) taken 1808 times.
✗ Branch 1 (28→53) not taken.
1808 visit(node->paramLst);
116
117 // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required
118
2/2
✓ Branch 0 (31→32) taken 1131 times.
✓ Branch 1 (31→33) taken 1494 times.
2625 if (node->isCtor)
119
1/2
✓ Branch 0 (32→33) taken 1131 times.
✗ Branch 1 (32→55) not taken.
1131 createCtorBodyPreamble(node->scope);
120
121 // Visit statements in new scope
122
1/2
✓ Branch 0 (33→34) taken 2625 times.
✗ Branch 1 (33→54) not taken.
2625 visit(node->body);
123
124 // Clear type mapping
125 2625 typeMapping.clear();
126
127 // Change to root scope
128 2625 currentScope = rootScope;
129
2/4
✓ Branch 0 (36→37) taken 2625 times.
✗ Branch 1 (36→39) not taken.
✓ Branch 2 (37→38) taken 2625 times.
✗ Branch 3 (37→39) not taken.
2625 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
130
131 // Do not type-check this manifestation again
132 2625 manifestation->alreadyTypeChecked = true;
133
134 2625 manIdx++; // Increase the manifestation index
135 }
136 6011 manIdx = 0; // Reset the manifestation index
137
138
1/2
✓ Branch 0 (44→45) taken 6011 times.
✗ Branch 1 (44→56) not taken.
6011 return nullptr;
139 }
140
141 941 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
142 941 node->resizeToNumberOfManifestations(node->structManifestations.size());
143 941 manIdx = 0; // Reset the manifestation index
144
145 // Get all manifestations for this procedure definition
146
2/2
✓ Branch 0 (151→6) taken 1493 times.
✓ Branch 1 (151→152) taken 941 times.
2434 for (Struct *manifestation : node->structManifestations) {
147 // Skip non-substantiated or already checked procedures
148
3/4
✓ Branch 0 (7→8) taken 1493 times.
✗ Branch 1 (7→236) not taken.
✓ Branch 2 (8→9) taken 371 times.
✓ Branch 3 (8→10) taken 1122 times.
1493 if (!manifestation->isFullySubstantiated()) {
149 371 manIdx++; // Increase the manifestation index
150 371 continue;
151 }
152
153 // Change to struct scope
154
1/2
✓ Branch 0 (10→11) taken 1122 times.
✗ Branch 1 (10→236) not taken.
1122 changeToScope(manifestation->scope, ScopeType::STRUCT);
155
156 // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations
157
2/2
✓ Branch 0 (20→13) taken 2689 times.
✓ Branch 1 (20→21) taken 1122 times.
3811 for (const FieldNode *field : node->fields)
158
2/2
✓ Branch 0 (14→15) taken 423 times.
✓ Branch 1 (14→18) taken 2266 times.
2689 if (field->defaultValue != nullptr)
159
1/2
✓ Branch 0 (15→16) taken 423 times.
✗ Branch 1 (15→155) not taken.
423 visit(field->defaultValue);
160
161 // Build struct type
162
1/2
✓ Branch 0 (21→22) taken 1122 times.
✗ Branch 1 (21→236) not taken.
1122 const QualType structType = manifestation->entry->getQualType();
163
164 // Check if the struct implements all methods of all attached interfaces
165 1122 size_t vtableIndex = 0;
166
2/2
✓ Branch 0 (105→24) taken 264 times.
✓ Branch 1 (105→106) taken 1122 times.
1386 for (const QualType &interfaceType : manifestation->interfaceTypes) {
167 // Retrieve interface instance
168
2/4
✓ Branch 0 (25→26) taken 264 times.
✗ Branch 1 (25→202) not taken.
✓ Branch 2 (26→27) taken 264 times.
✗ Branch 3 (26→202) not taken.
264 const std::string interfaceName = interfaceType.getSubType();
169
1/2
✓ Branch 0 (27→28) taken 264 times.
✗ Branch 1 (27→200) not taken.
264 Scope *matchScope = interfaceType.getBodyScope()->parent;
170
2/4
✓ Branch 0 (28→29) taken 264 times.
✗ Branch 1 (28→200) not taken.
✓ Branch 2 (29→30) taken 264 times.
✗ Branch 3 (29→200) not taken.
264 const Interface *interface = InterfaceManager::match(matchScope, interfaceName, interfaceType.getTemplateTypes(), node);
171
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 264 times.
264 assert(interface != nullptr);
172
173 // Check for all methods, that it is implemented by the struct
174
2/2
✓ Branch 0 (101→34) taken 668 times.
✓ Branch 1 (101→102) taken 264 times.
932 for (const Function *expMethod : interface->methods) {
175
1/2
✓ Branch 0 (35→36) taken 668 times.
✗ Branch 1 (35→198) not taken.
668 const std::string methodName = expMethod->name;
176
1/2
✓ Branch 0 (36→37) taken 668 times.
✗ Branch 1 (36→196) not taken.
668 QualTypeList params = expMethod->getParamTypes();
177 668 QualType returnType = expMethod->returnType;
178
179 // Substantiate param and return types
180
1/2
✓ Branch 0 (37→38) taken 668 times.
✗ Branch 1 (37→194) not taken.
668 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
181
3/4
✓ Branch 0 (38→39) taken 668 times.
✗ Branch 1 (38→194) not taken.
✓ Branch 2 (39→40) taken 372 times.
✓ Branch 3 (39→41) taken 296 times.
668 if (returnType.hasAnyGenericParts())
182
1/2
✓ Branch 0 (40→41) taken 372 times.
✗ Branch 1 (40→194) not taken.
372 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
183
184 // Build args list
185 668 ArgList args;
186
1/2
✓ Branch 0 (42→43) taken 668 times.
✗ Branch 1 (42→192) not taken.
668 args.reserve(params.size());
187
2/2
✓ Branch 0 (49→45) taken 14 times.
✓ Branch 1 (49→50) taken 668 times.
682 for (const QualType &param : params)
188
1/2
✓ Branch 0 (46→47) taken 14 times.
✗ Branch 1 (46→157) not taken.
14 args.emplace_back(param, nullptr);
189
190 // Search for method that has the required signature
191
1/2
✓ Branch 0 (51→52) taken 668 times.
✗ Branch 1 (51→159) not taken.
668 Function *spiceFunction = FunctionManager::match(this, currentScope, methodName, structType, args, {}, true, node);
192
2/2
✓ Branch 0 (53→54) taken 4 times.
✓ Branch 1 (53→66) taken 664 times.
668 if (spiceFunction == nullptr) {
193
1/2
✓ Branch 0 (59→60) taken 4 times.
✗ Branch 1 (59→162) not taken.
4 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
194
4/8
✓ Branch 0 (54→55) taken 4 times.
✗ Branch 1 (54→174) not taken.
✓ Branch 2 (55→56) taken 4 times.
✗ Branch 3 (55→170) not taken.
✓ Branch 4 (56→57) taken 4 times.
✗ Branch 5 (56→168) not taken.
✓ Branch 6 (57→58) taken 4 times.
✗ Branch 7 (57→166) not taken.
8 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
195
1/2
✓ Branch 0 (58→59) taken 4 times.
✗ Branch 1 (58→164) not taken.
4 "'. The signature does not match.");
196 4 continue;
197 }
198
199 // Check return type
200
5/6
✓ Branch 0 (66→67) taken 664 times.
✗ Branch 1 (66→192) not taken.
✓ Branch 2 (67→68) taken 110 times.
✓ Branch 3 (67→71) taken 554 times.
✓ Branch 4 (72→73) taken 2 times.
✓ Branch 5 (72→85) taken 662 times.
774 if (spiceFunction->returnType != returnType &&
201
3/4
✓ Branch 0 (68→69) taken 110 times.
✗ Branch 1 (68→192) not taken.
✓ Branch 2 (69→70) taken 2 times.
✓ Branch 3 (69→71) taken 108 times.
110 !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) {
202
1/2
✓ Branch 0 (78→79) taken 2 times.
✗ Branch 1 (78→177) not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
203
4/8
✓ Branch 0 (73→74) taken 2 times.
✗ Branch 1 (73→189) not taken.
✓ Branch 2 (74→75) taken 2 times.
✗ Branch 3 (74→185) not taken.
✓ Branch 4 (75→76) taken 2 times.
✗ Branch 5 (75→183) not taken.
✓ Branch 6 (76→77) taken 2 times.
✗ Branch 7 (76→181) not taken.
4 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
204
1/2
✓ Branch 0 (77→78) taken 2 times.
✗ Branch 1 (77→179) not taken.
2 "'. The return type does not match.");
205 2 continue;
206 }
207 // Set to virtual, since it overrides the interface method
208 662 spiceFunction->isVirtual = true;
209 662 spiceFunction->vtableIndex = vtableIndex++;
210
6/6
✓ Branch 0 (87→88) taken 662 times.
✓ Branch 1 (87→89) taken 6 times.
✓ Branch 2 (91→92) taken 662 times.
✓ Branch 3 (91→93) taken 6 times.
✓ Branch 4 (95→96) taken 662 times.
✓ Branch 5 (95→98) taken 6 times.
680 }
211 264 }
212
213 // Generate default ctor body if required
214
2/4
✓ Branch 0 (109→110) taken 1122 times.
✗ Branch 1 (109→206) not taken.
✓ Branch 2 (110→111) taken 1122 times.
✗ Branch 3 (110→204) not taken.
3366 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
215
4/4
✓ Branch 0 (114→115) taken 517 times.
✓ Branch 1 (114→117) taken 605 times.
✓ Branch 2 (115→116) taken 67 times.
✓ Branch 3 (115→117) taken 450 times.
1122 if (ctorFunc != nullptr && ctorFunc->implicitDefault)
216
1/2
✓ Branch 0 (116→117) taken 67 times.
✗ Branch 1 (116→236) not taken.
67 createCtorBodyPreamble(ctorFunc->bodyScope);
217
218 // Generate default copy ctor body if required
219
2/4
✓ Branch 0 (117→118) taken 1122 times.
✗ Branch 1 (117→217) not taken.
✓ Branch 2 (121→122) taken 1122 times.
✗ Branch 3 (121→213) not taken.
3366 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
220
2/4
✓ Branch 0 (125→126) taken 1122 times.
✗ Branch 1 (125→221) not taken.
✓ Branch 2 (126→127) taken 1122 times.
✗ Branch 3 (126→219) not taken.
1122 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
221
4/4
✓ Branch 0 (129→130) taken 397 times.
✓ Branch 1 (129→132) taken 725 times.
✓ Branch 2 (130→131) taken 133 times.
✓ Branch 3 (130→132) taken 264 times.
1122 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault)
222
1/2
✓ Branch 0 (131→132) taken 133 times.
✗ Branch 1 (131→234) not taken.
133 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
223
224 // Generate default dtor body if required
225
2/4
✓ Branch 0 (135→136) taken 1122 times.
✗ Branch 1 (135→227) not taken.
✓ Branch 2 (136→137) taken 1122 times.
✗ Branch 3 (136→225) not taken.
3366 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
226
4/4
✓ Branch 0 (140→141) taken 436 times.
✓ Branch 1 (140→143) taken 686 times.
✓ Branch 2 (141→142) taken 183 times.
✓ Branch 3 (141→143) taken 253 times.
1122 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
227
1/2
✓ Branch 0 (142→143) taken 183 times.
✗ Branch 1 (142→234) not taken.
183 createDtorBodyPreamble(dtorFunc->bodyScope);
228
229 // Return to the root scope
230 1122 currentScope = rootScope;
231
2/4
✓ Branch 0 (143→144) taken 1122 times.
✗ Branch 1 (143→146) not taken.
✓ Branch 2 (144→145) taken 1122 times.
✗ Branch 3 (144→146) not taken.
1122 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
232
233 1122 manIdx++; // Increase the manifestation index
234 1122 }
235 941 manIdx = 0; // Reset the manifestation index
236
237
1/2
✓ Branch 0 (152→153) taken 941 times.
✗ Branch 1 (152→238) not taken.
941 return nullptr;
238 }
239
240 } // namespace spice::compiler
241