GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 126 / 0 / 126
Functions: 100.0% 4 / 0 / 4
Branches: 66.1% 152 / 0 / 230

src/typechecker/TypeCheckerTopLevelDefinitionsCheck.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "TypeChecker.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTNodes.h>
7 #include <exception/SemanticError.h>
8 #include <model/Interface.h>
9 #include <symboltablebuilder/Scope.h>
10 #include <symboltablebuilder/SymbolTableBuilder.h>
11 #include <typechecker/FunctionManager.h>
12 #include <typechecker/TypeMatcher.h>
13
14 namespace spice::compiler {
15
16 464 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
17 // Skip if already type-checked
18
2/2
✓ Branch 2 → 3 taken 53 times.
✓ Branch 2 → 5 taken 411 times.
464 if (typeCheckedMainFct)
19
1/2
✓ Branch 3 → 4 taken 53 times.
✗ Branch 3 → 11 not taken.
53 return nullptr;
20
21 411 node->resizeToNumberOfManifestations(1);
22
23 // Change to function body scope
24 411 currentScope = node->bodyScope;
25 // Visit statements in new scope
26
2/2
✓ Branch 6 → 7 taken 381 times.
✓ Branch 6 → 12 taken 30 times.
411 visit(node->body);
27 // Leave main function body scope
28 381 currentScope = rootScope;
29
30 // Set to type-checked
31 381 typeCheckedMainFct = true;
32
1/2
✓ Branch 8 → 9 taken 381 times.
✗ Branch 8 → 13 not taken.
381 return nullptr;
33 }
34
35 18806 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
36 18806 node->resizeToNumberOfManifestations(node->manifestations.size());
37 18806 manIdx = 0; // Reset the manifestation index
38
39 // Get all manifestations for this function definition
40
2/2
✓ Branch 48 → 6 taken 23600 times.
✓ Branch 48 → 49 taken 18804 times.
42404 for (Function *manifestation : node->manifestations) {
41 // Skip non-substantiated or already checked functions
42
7/8
✓ Branch 7 → 8 taken 23600 times.
✗ Branch 7 → 63 not taken.
✓ Branch 8 → 9 taken 18529 times.
✓ Branch 8 → 10 taken 5071 times.
✓ Branch 9 → 10 taken 10132 times.
✓ Branch 9 → 11 taken 8397 times.
✓ Branch 12 → 13 taken 15203 times.
✓ Branch 12 → 14 taken 8397 times.
23600 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
43 15203 manIdx++; // Increase the manifestation index
44 15203 continue;
45 }
46
47 // Change scope to concrete struct specialization scope
48
2/2
✓ Branch 14 → 15 taken 3868 times.
✓ Branch 14 → 20 taken 4529 times.
8397 if (node->isMethod) {
49
2/4
✓ Branch 15 → 16 taken 3868 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 3868 times.
✗ Branch 16 → 54 not taken.
3868 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
50
1/2
✓ Branch 17 → 18 taken 3868 times.
✗ Branch 17 → 52 not taken.
3868 changeToScope(scopeName, ScopeType::STRUCT);
51 3868 }
52
53 // Change to function scope
54
1/2
✓ Branch 20 → 21 taken 8397 times.
✗ Branch 20 → 63 not taken.
8397 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
55
56 // Mount type mapping for this manifestation
57
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 8397 times.
8397 assert(typeMapping.empty());
58
1/2
✓ Branch 24 → 25 taken 8397 times.
✗ Branch 24 → 63 not taken.
8397 typeMapping = manifestation->typeMapping;
59
60 // Set return type to the result variable
61
1/2
✓ Branch 27 → 28 taken 8397 times.
✗ Branch 27 → 57 not taken.
25191 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
62
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 8397 times.
8397 assert(resultVarEntry != nullptr);
63
1/2
✓ Branch 35 → 36 taken 8397 times.
✗ Branch 35 → 63 not taken.
8397 resultVarEntry->updateType(manifestation->returnType, false);
64 8397 resultVarEntry->used = true;
65
66 // Visit parameters
67 // This happens once in the type checker prepare stage. This second time is only required if we have a generic function
68
2/2
✓ Branch 36 → 37 taken 6638 times.
✓ Branch 36 → 40 taken 1759 times.
8397 if (node->hasParams)
69
1/2
✓ Branch 37 → 38 taken 6638 times.
✗ Branch 37 → 61 not taken.
6638 visit(node->paramLst);
70
71 // Visit statements in new scope
72
2/2
✓ Branch 40 → 41 taken 8395 times.
✓ Branch 40 → 62 taken 2 times.
8397 visit(node->body);
73
74 // Clear type mapping
75 8395 typeMapping.clear();
76
77 // Change to root scope
78 8395 currentScope = rootScope;
79
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 8395 times.
8395 assert(currentScope->type == ScopeType::GLOBAL);
80
81 // Do not type-check this manifestation again
82 8395 manifestation->alreadyTypeChecked = true;
83
84 8395 manIdx++; // Increase the manifestation index
85 }
86 18804 manIdx = 0; // Reset the manifestation index
87
88
1/2
✓ Branch 49 → 50 taken 18804 times.
✗ Branch 49 → 64 not taken.
18804 return nullptr;
89 }
90
91 10235 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
92 10235 node->resizeToNumberOfManifestations(node->manifestations.size());
93 10235 manIdx = 0; // Reset the manifestation index
94
95 // Get all manifestations for this procedure definition
96
2/2
✓ Branch 41 → 6 taken 13796 times.
✓ Branch 41 → 42 taken 10235 times.
24031 for (Function *manifestation : node->manifestations) {
97 // Skip non-substantiated or already checked procedures
98
7/8
✓ Branch 7 → 8 taken 13796 times.
✗ Branch 7 → 50 not taken.
✓ Branch 8 → 9 taken 8347 times.
✓ Branch 8 → 10 taken 5449 times.
✓ Branch 9 → 10 taken 3931 times.
✓ Branch 9 → 11 taken 4416 times.
✓ Branch 12 → 13 taken 9380 times.
✓ Branch 12 → 14 taken 4416 times.
13796 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
99 9380 manIdx++; // Increase the manifestation index
100 9380 continue;
101 }
102
103 // Change scope to concrete struct specialization scope
104
2/2
✓ Branch 14 → 15 taken 3254 times.
✓ Branch 14 → 20 taken 1162 times.
4416 if (node->isMethod) {
105
2/4
✓ Branch 15 → 16 taken 3254 times.
✗ Branch 15 → 47 not taken.
✓ Branch 16 → 17 taken 3254 times.
✗ Branch 16 → 47 not taken.
3254 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
106
1/2
✓ Branch 17 → 18 taken 3254 times.
✗ Branch 17 → 45 not taken.
3254 changeToScope(scopeName, ScopeType::STRUCT);
107 3254 }
108
109 // Change to procedure scope
110
1/2
✓ Branch 20 → 21 taken 4416 times.
✗ Branch 20 → 50 not taken.
4416 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
111
112 // Mount type mapping for this manifestation
113
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 4416 times.
4416 assert(typeMapping.empty());
114
1/2
✓ Branch 24 → 25 taken 4416 times.
✗ Branch 24 → 50 not taken.
4416 typeMapping = manifestation->typeMapping;
115
116 // Visit parameters
117 // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure
118
2/2
✓ Branch 25 → 26 taken 3298 times.
✓ Branch 25 → 29 taken 1118 times.
4416 if (node->hasParams)
119
1/2
✓ Branch 26 → 27 taken 3298 times.
✗ Branch 26 → 48 not taken.
3298 visit(node->paramLst);
120
121 // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required
122
2/2
✓ Branch 29 → 30 taken 1530 times.
✓ Branch 29 → 31 taken 2886 times.
4416 if (node->isCtor)
123
1/2
✓ Branch 30 → 31 taken 1530 times.
✗ Branch 30 → 50 not taken.
1530 createCtorBodyPreamble(node->scope);
124
125 // Visit statements in new scope
126
1/2
✓ Branch 31 → 32 taken 4416 times.
✗ Branch 31 → 49 not taken.
4416 visit(node->body);
127
128 // Clear type mapping
129 4416 typeMapping.clear();
130
131 // Change to root scope
132 4416 currentScope = rootScope;
133
2/4
✓ Branch 34 → 35 taken 4416 times.
✗ Branch 34 → 37 not taken.
✓ Branch 35 → 36 taken 4416 times.
✗ Branch 35 → 37 not taken.
4416 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
134
135 // Do not type-check this manifestation again
136 4416 manifestation->alreadyTypeChecked = true;
137
138 4416 manIdx++; // Increase the manifestation index
139 }
140 10235 manIdx = 0; // Reset the manifestation index
141
142
1/2
✓ Branch 42 → 43 taken 10235 times.
✗ Branch 42 → 51 not taken.
10235 return nullptr;
143 }
144
145 1356 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
146 1356 node->resizeToNumberOfManifestations(node->structManifestations.size());
147 1356 manIdx = 0; // Reset the manifestation index
148
149 // Get all manifestations for this procedure definition
150
2/2
✓ Branch 159 → 6 taken 2362 times.
✓ Branch 159 → 160 taken 1356 times.
3718 for (const Struct *manifestation : node->structManifestations) {
151 // Skip non-substantiated or already checked procedures
152
3/4
✓ Branch 7 → 8 taken 2362 times.
✗ Branch 7 → 242 not taken.
✓ Branch 8 → 9 taken 691 times.
✓ Branch 8 → 10 taken 1671 times.
2362 if (!manifestation->isFullySubstantiated()) {
153 691 manIdx++; // Increase the manifestation index
154 691 continue;
155 }
156
157 // Change to struct scope
158
1/2
✓ Branch 10 → 11 taken 1671 times.
✗ Branch 10 → 242 not taken.
1671 changeToScope(manifestation->scope, ScopeType::STRUCT);
159
160 // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations
161
2/2
✓ Branch 26 → 13 taken 3902 times.
✓ Branch 26 → 27 taken 1671 times.
5573 for (const FieldNode *field : node->fields) {
162
2/2
✓ Branch 14 → 15 taken 716 times.
✓ Branch 14 → 24 taken 3186 times.
3902 if (field->defaultValue != nullptr) {
163
1/2
✓ Branch 15 → 16 taken 716 times.
✗ Branch 15 → 163 not taken.
716 visit(field->defaultValue);
164
1/2
✓ Branch 17 → 18 taken 716 times.
✗ Branch 17 → 165 not taken.
716 SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName);
165
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 716 times.
716 assert(fieldEntry != nullptr);
166
1/2
✓ Branch 22 → 23 taken 716 times.
✗ Branch 22 → 164 not taken.
716 fieldEntry->updateState(INITIALIZED, field);
167 }
168 }
169
170 // Build struct type
171
1/2
✓ Branch 27 → 28 taken 1671 times.
✗ Branch 27 → 242 not taken.
1671 const QualType structType = manifestation->entry->getQualType();
172
173 // Check if the struct implements all methods of all attached interfaces
174 1671 size_t vtableIndex = 0;
175
2/2
✓ Branch 106 → 30 taken 498 times.
✓ Branch 106 → 107 taken 1671 times.
2169 for (const QualType &interfaceType : manifestation->interfaceTypes) {
176
1/2
✓ Branch 31 → 32 taken 498 times.
✗ Branch 31 → 209 not taken.
498 const Interface *interface = interfaceType.getInterface(node);
177
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 498 times.
498 assert(interface != nullptr);
178
179 // Check for all methods, that it is implemented by the struct
180
2/2
✓ Branch 103 → 36 taken 1244 times.
✓ Branch 103 → 104 taken 498 times.
1742 for (const Function *expMethod : interface->methods) {
181
1/2
✓ Branch 37 → 38 taken 1244 times.
✗ Branch 37 → 207 not taken.
1244 const std::string methodName = expMethod->name;
182
1/2
✓ Branch 38 → 39 taken 1244 times.
✗ Branch 38 → 205 not taken.
1244 QualTypeList params = expMethod->getParamTypes();
183 1244 QualType returnType = expMethod->returnType;
184
185 // Substantiate param and return types
186
1/2
✓ Branch 39 → 40 taken 1244 times.
✗ Branch 39 → 203 not taken.
1244 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
187
3/4
✓ Branch 40 → 41 taken 1244 times.
✗ Branch 40 → 203 not taken.
✓ Branch 41 → 42 taken 711 times.
✓ Branch 41 → 43 taken 533 times.
1244 if (returnType.hasAnyGenericParts())
188
1/2
✓ Branch 42 → 43 taken 711 times.
✗ Branch 42 → 203 not taken.
711 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
189
190 // Build args list
191 1244 ArgList args;
192
1/2
✓ Branch 44 → 45 taken 1244 times.
✗ Branch 44 → 201 not taken.
1244 args.reserve(params.size());
193
2/2
✓ Branch 51 → 47 taken 14 times.
✓ Branch 51 → 52 taken 1244 times.
1258 for (const QualType &param : params)
194
1/2
✓ Branch 48 → 49 taken 14 times.
✗ Branch 48 → 166 not taken.
14 args.emplace_back(param, nullptr);
195
196 // Search for method that has the required signature
197
1/2
✓ Branch 53 → 54 taken 1244 times.
✗ Branch 53 → 168 not taken.
1244 Function *spiceFunction = FunctionManager::match(currentScope, methodName, structType, args, {}, true, node);
198
2/2
✓ Branch 55 → 56 taken 4 times.
✓ Branch 55 → 68 taken 1240 times.
1244 if (spiceFunction == nullptr) {
199
1/2
✓ Branch 61 → 62 taken 4 times.
✗ Branch 61 → 171 not taken.
4 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
200
5/10
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 183 not taken.
✓ Branch 57 → 58 taken 4 times.
✗ Branch 57 → 179 not taken.
✓ Branch 58 → 59 taken 4 times.
✗ Branch 58 → 177 not taken.
✓ Branch 59 → 60 taken 4 times.
✗ Branch 59 → 175 not taken.
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 173 not taken.
8 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() + "'.");
201 4 continue;
202 }
203
204 // Check return type
205
5/6
✓ Branch 68 → 69 taken 1240 times.
✗ Branch 68 → 201 not taken.
✓ Branch 69 → 70 taken 221 times.
✓ Branch 69 → 73 taken 1019 times.
✓ Branch 74 → 75 taken 2 times.
✓ Branch 74 → 87 taken 1238 times.
1461 if (spiceFunction->returnType != returnType &&
206
3/4
✓ Branch 70 → 71 taken 221 times.
✗ Branch 70 → 201 not taken.
✓ Branch 71 → 72 taken 2 times.
✓ Branch 71 → 73 taken 219 times.
221 !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) {
207
1/2
✓ Branch 80 → 81 taken 2 times.
✗ Branch 80 → 186 not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
208
4/8
✓ Branch 75 → 76 taken 2 times.
✗ Branch 75 → 198 not taken.
✓ Branch 76 → 77 taken 2 times.
✗ Branch 76 → 194 not taken.
✓ Branch 77 → 78 taken 2 times.
✗ Branch 77 → 192 not taken.
✓ Branch 78 → 79 taken 2 times.
✗ Branch 78 → 190 not taken.
4 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
209
1/2
✓ Branch 79 → 80 taken 2 times.
✗ Branch 79 → 188 not taken.
2 "'. The return type does not match.");
210 2 continue;
211 }
212 // Set to virtual, since it overrides the interface method
213 1238 spiceFunction->isVirtual = true;
214 1238 spiceFunction->vtableIndex = vtableIndex++;
215
6/6
✓ Branch 89 → 90 taken 1238 times.
✓ Branch 89 → 91 taken 6 times.
✓ Branch 93 → 94 taken 1238 times.
✓ Branch 93 → 95 taken 6 times.
✓ Branch 97 → 98 taken 1238 times.
✓ Branch 97 → 100 taken 6 times.
1256 }
216 }
217
218 // Check default ctor body if required
219
2/4
✓ Branch 110 → 111 taken 1671 times.
✗ Branch 110 → 212 not taken.
✓ Branch 111 → 112 taken 1671 times.
✗ Branch 111 → 210 not taken.
5013 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
220
4/4
✓ Branch 115 → 116 taken 730 times.
✓ Branch 115 → 121 taken 941 times.
✓ Branch 116 → 117 taken 114 times.
✓ Branch 116 → 121 taken 616 times.
1671 if (ctorFunc != nullptr && ctorFunc->implicitDefault) {
221
1/2
✓ Branch 117 → 118 taken 114 times.
✗ Branch 117 → 242 not taken.
114 createCtorBodyPreamble(ctorFunc->bodyScope);
222
2/4
✓ Branch 118 → 119 taken 114 times.
✗ Branch 118 → 242 not taken.
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 121 taken 114 times.
114 assert(manifestation->areAllFieldsInitialized() == nullptr);
223 }
224
225 // Check default copy ctor body if required
226
2/4
✓ Branch 121 → 122 taken 1671 times.
✗ Branch 121 → 223 not taken.
✓ Branch 125 → 126 taken 1671 times.
✗ Branch 125 → 219 not taken.
5013 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
227
2/4
✓ Branch 129 → 130 taken 1671 times.
✗ Branch 129 → 227 not taken.
✓ Branch 130 → 131 taken 1671 times.
✗ Branch 130 → 225 not taken.
1671 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
228
4/4
✓ Branch 133 → 134 taken 510 times.
✓ Branch 133 → 139 taken 1161 times.
✓ Branch 134 → 135 taken 164 times.
✓ Branch 134 → 139 taken 346 times.
1671 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) {
229
1/2
✓ Branch 135 → 136 taken 164 times.
✗ Branch 135 → 240 not taken.
164 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
230
2/4
✓ Branch 136 → 137 taken 164 times.
✗ Branch 136 → 240 not taken.
✗ Branch 137 → 138 not taken.
✓ Branch 137 → 139 taken 164 times.
164 assert(manifestation->areAllFieldsInitialized() == nullptr);
231 }
232
233 // Check default dtor body if required
234
2/4
✓ Branch 142 → 143 taken 1671 times.
✗ Branch 142 → 233 not taken.
✓ Branch 143 → 144 taken 1671 times.
✗ Branch 143 → 231 not taken.
5013 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
235
4/4
✓ Branch 147 → 148 taken 616 times.
✓ Branch 147 → 150 taken 1055 times.
✓ Branch 148 → 149 taken 287 times.
✓ Branch 148 → 150 taken 329 times.
1671 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
236
1/2
✓ Branch 149 → 150 taken 287 times.
✗ Branch 149 → 240 not taken.
287 createDtorBodyPreamble(dtorFunc->bodyScope);
237
238 // Reset field symbols to declared state for the next manifestation
239
1/2
✓ Branch 150 → 151 taken 1671 times.
✗ Branch 150 → 240 not taken.
1671 manifestation->resetFieldSymbolsToDeclared(node);
240
241 // Return to the root scope
242 1671 currentScope = rootScope;
243
2/4
✓ Branch 151 → 152 taken 1671 times.
✗ Branch 151 → 154 not taken.
✓ Branch 152 → 153 taken 1671 times.
✗ Branch 152 → 154 not taken.
1671 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
244
245 1671 manIdx++; // Increase the manifestation index
246 1671 }
247 1356 manIdx = 0; // Reset the manifestation index
248
249
1/2
✓ Branch 160 → 161 taken 1356 times.
✗ Branch 160 → 244 not taken.
1356 return nullptr;
250 }
251
252 } // namespace spice::compiler
253