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 466 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
17 // Skip if already type-checked
18
2/2
✓ Branch 2 → 3 taken 54 times.
✓ Branch 2 → 6 taken 412 times.
466 if (typeCheckedMainFct)
19
1/2
✓ Branch 3 → 4 taken 54 times.
✗ Branch 3 → 13 not taken.
108 return nullptr;
20
21 412 node->resizeToNumberOfManifestations(1);
22
23 // Change to function body scope
24 412 currentScope = node->bodyScope;
25 // Visit statements in new scope
26
2/2
✓ Branch 7 → 8 taken 382 times.
✓ Branch 7 → 14 taken 30 times.
412 visit(node->body);
27 // Leave main function body scope
28 382 currentScope = rootScope;
29
30 // Set to type-checked
31 382 typeCheckedMainFct = true;
32
1/2
✓ Branch 9 → 10 taken 382 times.
✗ Branch 9 → 15 not taken.
764 return nullptr;
33 }
34
35 18834 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
36 18834 node->resizeToNumberOfManifestations(node->manifestations.size());
37 18834 manIdx = 0; // Reset the manifestation index
38
39 // Get all manifestations for this function definition
40
2/2
✓ Branch 56 → 6 taken 23632 times.
✓ Branch 56 → 57 taken 18832 times.
61298 for (Function *manifestation : node->manifestations) {
41 // Skip non-substantiated or already checked functions
42
7/8
✓ Branch 8 → 9 taken 23632 times.
✗ Branch 8 → 72 not taken.
✓ Branch 9 → 10 taken 18542 times.
✓ Branch 9 → 11 taken 5090 times.
✓ Branch 10 → 11 taken 10134 times.
✓ Branch 10 → 12 taken 8408 times.
✓ Branch 13 → 14 taken 15224 times.
✓ Branch 13 → 15 taken 8408 times.
23632 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
43 15224 manIdx++; // Increase the manifestation index
44 15224 continue;
45 }
46
47 // Change scope to concrete struct specialization scope
48
2/2
✓ Branch 15 → 16 taken 3870 times.
✓ Branch 15 → 21 taken 4538 times.
8408 if (node->isMethod) {
49
2/4
✓ Branch 16 → 17 taken 3870 times.
✗ Branch 16 → 63 not taken.
✓ Branch 17 → 18 taken 3870 times.
✗ Branch 17 → 63 not taken.
3870 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
50
1/2
✓ Branch 18 → 19 taken 3870 times.
✗ Branch 18 → 61 not taken.
3870 changeToScope(scopeName, ScopeType::STRUCT);
51 3870 }
52
53 // Change to function scope
54
1/2
✓ Branch 21 → 22 taken 8408 times.
✗ Branch 21 → 72 not taken.
8408 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
55
56 // Mount type mapping for this manifestation
57
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 8408 times.
8408 assert(typeMapping.empty());
58
1/2
✓ Branch 25 → 26 taken 8408 times.
✗ Branch 25 → 72 not taken.
8408 typeMapping = manifestation->typeMapping;
59
60 // Set return type to the result variable
61
1/2
✓ Branch 28 → 29 taken 8408 times.
✗ Branch 28 → 66 not taken.
25224 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
62
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 8408 times.
8408 assert(resultVarEntry != nullptr);
63
1/2
✓ Branch 36 → 37 taken 8408 times.
✗ Branch 36 → 72 not taken.
8408 resultVarEntry->updateType(manifestation->returnType, false);
64 8408 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 37 → 38 taken 6646 times.
✓ Branch 37 → 41 taken 1762 times.
8408 if (node->hasParams)
69
1/2
✓ Branch 38 → 39 taken 6646 times.
✗ Branch 38 → 70 not taken.
6646 visit(node->paramLst);
70
71 // Visit statements in new scope
72
2/2
✓ Branch 41 → 42 taken 8406 times.
✓ Branch 41 → 71 taken 2 times.
8408 visit(node->body);
73
74 // Clear type mapping
75 8406 typeMapping.clear();
76
77 // Change to root scope
78 8406 currentScope = rootScope;
79
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 8406 times.
8406 assert(currentScope->type == ScopeType::GLOBAL);
80
81 // Do not type-check this manifestation again
82 8406 manifestation->alreadyTypeChecked = true;
83
84 8406 manIdx++; // Increase the manifestation index
85 }
86 18832 manIdx = 0; // Reset the manifestation index
87
88
1/2
✓ Branch 57 → 58 taken 18832 times.
✗ Branch 57 → 73 not taken.
37664 return nullptr;
89 }
90
91 10260 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
92 10260 node->resizeToNumberOfManifestations(node->manifestations.size());
93 10260 manIdx = 0; // Reset the manifestation index
94
95 // Get all manifestations for this procedure definition
96
2/2
✓ Branch 49 → 6 taken 13824 times.
✓ Branch 49 → 50 taken 10260 times.
34344 for (Function *manifestation : node->manifestations) {
97 // Skip non-substantiated or already checked procedures
98
7/8
✓ Branch 8 → 9 taken 13824 times.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 8369 times.
✓ Branch 9 → 11 taken 5455 times.
✓ Branch 10 → 11 taken 3932 times.
✓ Branch 10 → 12 taken 4437 times.
✓ Branch 13 → 14 taken 9387 times.
✓ Branch 13 → 15 taken 4437 times.
13824 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
99 9387 manIdx++; // Increase the manifestation index
100 9387 continue;
101 }
102
103 // Change scope to concrete struct specialization scope
104
2/2
✓ Branch 15 → 16 taken 3273 times.
✓ Branch 15 → 21 taken 1164 times.
4437 if (node->isMethod) {
105
2/4
✓ Branch 16 → 17 taken 3273 times.
✗ Branch 16 → 56 not taken.
✓ Branch 17 → 18 taken 3273 times.
✗ Branch 17 → 56 not taken.
3273 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
106
1/2
✓ Branch 18 → 19 taken 3273 times.
✗ Branch 18 → 54 not taken.
3273 changeToScope(scopeName, ScopeType::STRUCT);
107 3273 }
108
109 // Change to procedure scope
110
1/2
✓ Branch 21 → 22 taken 4437 times.
✗ Branch 21 → 59 not taken.
4437 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
111
112 // Mount type mapping for this manifestation
113
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 4437 times.
4437 assert(typeMapping.empty());
114
1/2
✓ Branch 25 → 26 taken 4437 times.
✗ Branch 25 → 59 not taken.
4437 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 26 → 27 taken 3308 times.
✓ Branch 26 → 30 taken 1129 times.
4437 if (node->hasParams)
119
1/2
✓ Branch 27 → 28 taken 3308 times.
✗ Branch 27 → 57 not taken.
3308 visit(node->paramLst);
120
121 // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required
122
2/2
✓ Branch 30 → 31 taken 1540 times.
✓ Branch 30 → 32 taken 2897 times.
4437 if (node->isCtor)
123
1/2
✓ Branch 31 → 32 taken 1540 times.
✗ Branch 31 → 59 not taken.
1540 createCtorBodyPreamble(node->scope);
124
125 // Visit statements in new scope
126
1/2
✓ Branch 32 → 33 taken 4437 times.
✗ Branch 32 → 58 not taken.
4437 visit(node->body);
127
128 // Clear type mapping
129 4437 typeMapping.clear();
130
131 // Change to root scope
132 4437 currentScope = rootScope;
133
2/4
✓ Branch 35 → 36 taken 4437 times.
✗ Branch 35 → 38 not taken.
✓ Branch 36 → 37 taken 4437 times.
✗ Branch 36 → 38 not taken.
4437 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
134
135 // Do not type-check this manifestation again
136 4437 manifestation->alreadyTypeChecked = true;
137
138 4437 manIdx++; // Increase the manifestation index
139 }
140 10260 manIdx = 0; // Reset the manifestation index
141
142
1/2
✓ Branch 50 → 51 taken 10260 times.
✗ Branch 50 → 60 not taken.
20520 return nullptr;
143 }
144
145 1365 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
146 1365 node->resizeToNumberOfManifestations(node->structManifestations.size());
147 1365 manIdx = 0; // Reset the manifestation index
148
149 // Get all manifestations for this procedure definition
150
2/2
✓ Branch 202 → 6 taken 2373 times.
✓ Branch 202 → 203 taken 1365 times.
5103 for (const Struct *manifestation : node->structManifestations) {
151 // Skip non-substantiated or already checked procedures
152
3/4
✓ Branch 8 → 9 taken 2373 times.
✗ Branch 8 → 286 not taken.
✓ Branch 9 → 10 taken 693 times.
✓ Branch 9 → 11 taken 1680 times.
2373 if (!manifestation->isFullySubstantiated()) {
153 693 manIdx++; // Increase the manifestation index
154 693 continue;
155 }
156
157 // Change to struct scope
158
1/2
✓ Branch 11 → 12 taken 1680 times.
✗ Branch 11 → 286 not taken.
1680 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 35 → 14 taken 3919 times.
✓ Branch 35 → 36 taken 1680 times.
7279 for (const FieldNode *field : node->fields) {
162
2/2
✓ Branch 16 → 17 taken 719 times.
✓ Branch 16 → 26 taken 3200 times.
3919 if (field->defaultValue != nullptr) {
163
1/2
✓ Branch 17 → 18 taken 719 times.
✗ Branch 17 → 207 not taken.
719 visit(field->defaultValue);
164
1/2
✓ Branch 19 → 20 taken 719 times.
✗ Branch 19 → 209 not taken.
719 SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName);
165
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 719 times.
719 assert(fieldEntry != nullptr);
166
1/2
✓ Branch 24 → 25 taken 719 times.
✗ Branch 24 → 208 not taken.
719 fieldEntry->updateState(INITIALIZED, field);
167 }
168 }
169
170 // Build struct type
171
1/2
✓ Branch 36 → 37 taken 1680 times.
✗ Branch 36 → 286 not taken.
1680 const QualType structType = manifestation->entry->getQualType();
172
173 // Check if the struct implements all methods of all attached interfaces
174 1680 size_t vtableIndex = 0;
175
2/2
✓ Branch 141 → 39 taken 498 times.
✓ Branch 141 → 142 taken 1680 times.
3858 for (const QualType &interfaceType : manifestation->interfaceTypes) {
176
1/2
✓ Branch 41 → 42 taken 498 times.
✗ Branch 41 → 253 not taken.
498 const Interface *interface = interfaceType.getInterface(node);
177
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 498 times.
498 assert(interface != nullptr);
178
179 // Check for all methods, that it is implemented by the struct
180
2/2
✓ Branch 131 → 46 taken 1244 times.
✓ Branch 131 → 132 taken 498 times.
2240 for (const Function *expMethod : interface->methods) {
181
1/2
✓ Branch 48 → 49 taken 1244 times.
✗ Branch 48 → 251 not taken.
1244 const std::string methodName = expMethod->name;
182
1/2
✓ Branch 49 → 50 taken 1244 times.
✗ Branch 49 → 249 not taken.
1244 QualTypeList params = expMethod->getParamTypes();
183 1244 QualType returnType = expMethod->returnType;
184
185 // Substantiate param and return types
186
1/2
✓ Branch 50 → 51 taken 1244 times.
✗ Branch 50 → 247 not taken.
1244 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
187
3/4
✓ Branch 51 → 52 taken 1244 times.
✗ Branch 51 → 247 not taken.
✓ Branch 52 → 53 taken 711 times.
✓ Branch 52 → 54 taken 533 times.
1244 if (returnType.hasAnyGenericParts())
188
1/2
✓ Branch 53 → 54 taken 711 times.
✗ Branch 53 → 247 not taken.
711 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
189
190 // Build args list
191 1244 ArgList args;
192
1/2
✓ Branch 55 → 56 taken 1244 times.
✗ Branch 55 → 245 not taken.
1244 args.reserve(params.size());
193
2/2
✓ Branch 70 → 58 taken 14 times.
✓ Branch 70 → 71 taken 1244 times.
2502 for (const QualType &param : params)
194
1/2
✓ Branch 60 → 61 taken 14 times.
✗ Branch 60 → 210 not taken.
14 args.emplace_back(param, nullptr);
195
196 // Search for method that has the required signature
197
1/2
✓ Branch 72 → 73 taken 1244 times.
✗ Branch 72 → 212 not taken.
1244 Function *spiceFunction = FunctionManager::match(currentScope, methodName, structType, args, {}, true, node);
198
2/2
✓ Branch 74 → 75 taken 4 times.
✓ Branch 74 → 87 taken 1240 times.
1244 if (spiceFunction == nullptr) {
199
1/2
✓ Branch 80 → 81 taken 4 times.
✗ Branch 80 → 215 not taken.
4 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
200
5/10
✓ Branch 75 → 76 taken 4 times.
✗ Branch 75 → 227 not taken.
✓ Branch 76 → 77 taken 4 times.
✗ Branch 76 → 223 not taken.
✓ Branch 77 → 78 taken 4 times.
✗ Branch 77 → 221 not taken.
✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 219 not taken.
✓ Branch 79 → 80 taken 4 times.
✗ Branch 79 → 217 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 87 → 88 taken 1240 times.
✗ Branch 87 → 245 not taken.
✓ Branch 88 → 89 taken 221 times.
✓ Branch 88 → 92 taken 1019 times.
✓ Branch 93 → 94 taken 2 times.
✓ Branch 93 → 106 taken 1238 times.
1461 if (spiceFunction->returnType != returnType &&
206
3/4
✓ Branch 89 → 90 taken 221 times.
✗ Branch 89 → 245 not taken.
✓ Branch 90 → 91 taken 2 times.
✓ Branch 90 → 92 taken 219 times.
221 !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) {
207
1/2
✓ Branch 99 → 100 taken 2 times.
✗ Branch 99 → 230 not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
208
4/8
✓ Branch 94 → 95 taken 2 times.
✗ Branch 94 → 242 not taken.
✓ Branch 95 → 96 taken 2 times.
✗ Branch 95 → 238 not taken.
✓ Branch 96 → 97 taken 2 times.
✗ Branch 96 → 236 not taken.
✓ Branch 97 → 98 taken 2 times.
✗ Branch 97 → 234 not taken.
4 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
209
1/2
✓ Branch 98 → 99 taken 2 times.
✗ Branch 98 → 232 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 108 → 109 taken 1238 times.
✓ Branch 108 → 110 taken 6 times.
✓ Branch 113 → 114 taken 1238 times.
✓ Branch 113 → 115 taken 6 times.
✓ Branch 118 → 119 taken 1238 times.
✓ Branch 118 → 121 taken 6 times.
3732 }
216 }
217
218 // Check default ctor body if required
219
2/4
✓ Branch 145 → 146 taken 1680 times.
✗ Branch 145 → 256 not taken.
✓ Branch 146 → 147 taken 1680 times.
✗ Branch 146 → 254 not taken.
5040 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
220
4/4
✓ Branch 150 → 151 taken 734 times.
✓ Branch 150 → 156 taken 946 times.
✓ Branch 151 → 152 taken 116 times.
✓ Branch 151 → 156 taken 618 times.
1680 if (ctorFunc != nullptr && ctorFunc->implicitDefault) {
221
1/2
✓ Branch 152 → 153 taken 116 times.
✗ Branch 152 → 286 not taken.
116 createCtorBodyPreamble(ctorFunc->bodyScope);
222
2/4
✓ Branch 153 → 154 taken 116 times.
✗ Branch 153 → 286 not taken.
✗ Branch 154 → 155 not taken.
✓ Branch 154 → 156 taken 116 times.
116 assert(manifestation->areAllFieldsInitialized() == nullptr);
223 }
224
225 // Check default copy ctor body if required
226
2/4
✓ Branch 156 → 157 taken 1680 times.
✗ Branch 156 → 267 not taken.
✓ Branch 160 → 161 taken 1680 times.
✗ Branch 160 → 263 not taken.
5040 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
227
2/4
✓ Branch 164 → 165 taken 1680 times.
✗ Branch 164 → 271 not taken.
✓ Branch 165 → 166 taken 1680 times.
✗ Branch 165 → 269 not taken.
1680 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
228
4/4
✓ Branch 168 → 169 taken 513 times.
✓ Branch 168 → 174 taken 1167 times.
✓ Branch 169 → 170 taken 166 times.
✓ Branch 169 → 174 taken 347 times.
1680 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) {
229
1/2
✓ Branch 170 → 171 taken 166 times.
✗ Branch 170 → 284 not taken.
166 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
230
2/4
✓ Branch 171 → 172 taken 166 times.
✗ Branch 171 → 284 not taken.
✗ Branch 172 → 173 not taken.
✓ Branch 172 → 174 taken 166 times.
166 assert(manifestation->areAllFieldsInitialized() == nullptr);
231 }
232
233 // Check default dtor body if required
234
2/4
✓ Branch 177 → 178 taken 1680 times.
✗ Branch 177 → 277 not taken.
✓ Branch 178 → 179 taken 1680 times.
✗ Branch 178 → 275 not taken.
5040 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
235
4/4
✓ Branch 182 → 183 taken 620 times.
✓ Branch 182 → 185 taken 1060 times.
✓ Branch 183 → 184 taken 289 times.
✓ Branch 183 → 185 taken 331 times.
1680 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
236
1/2
✓ Branch 184 → 185 taken 289 times.
✗ Branch 184 → 284 not taken.
289 createDtorBodyPreamble(dtorFunc->bodyScope);
237
238 // Reset field symbols to declared state for the next manifestation
239
1/2
✓ Branch 185 → 186 taken 1680 times.
✗ Branch 185 → 284 not taken.
1680 manifestation->resetFieldSymbolsToDeclared(node);
240
241 // Return to the root scope
242 1680 currentScope = rootScope;
243
2/4
✓ Branch 186 → 187 taken 1680 times.
✗ Branch 186 → 189 not taken.
✓ Branch 187 → 188 taken 1680 times.
✗ Branch 187 → 189 not taken.
1680 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
244
245 1680 manIdx++; // Increase the manifestation index
246 1680 }
247 1365 manIdx = 0; // Reset the manifestation index
248
249
1/2
✓ Branch 203 → 204 taken 1365 times.
✗ Branch 203 → 288 not taken.
2730 return nullptr;
250 }
251
252 } // namespace spice::compiler
253