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 448 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
17 // Skip if already type-checked
18
2/2
✓ Branch 2 → 3 taken 52 times.
✓ Branch 2 → 5 taken 396 times.
448 if (typeCheckedMainFct)
19
1/2
✓ Branch 3 → 4 taken 52 times.
✗ Branch 3 → 11 not taken.
52 return nullptr;
20
21 396 node->resizeToNumberOfManifestations(1);
22
23 // Change to function body scope
24 396 currentScope = node->bodyScope;
25 // Visit statements in new scope
26
2/2
✓ Branch 6 → 7 taken 366 times.
✓ Branch 6 → 12 taken 30 times.
396 visit(node->body);
27 // Leave main function body scope
28 366 currentScope = rootScope;
29
30 // Set to type-checked
31 366 typeCheckedMainFct = true;
32
1/2
✓ Branch 8 → 9 taken 366 times.
✗ Branch 8 → 13 not taken.
366 return nullptr;
33 }
34
35 17435 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
36 17435 node->resizeToNumberOfManifestations(node->manifestations.size());
37 17435 manIdx = 0; // Reset the manifestation index
38
39 // Get all manifestations for this function definition
40
2/2
✓ Branch 48 → 6 taken 21582 times.
✓ Branch 48 → 49 taken 17433 times.
39015 for (Function *manifestation : node->manifestations) {
41 // Skip non-substantiated or already checked functions
42
7/8
✓ Branch 7 → 8 taken 21582 times.
✗ Branch 7 → 63 not taken.
✓ Branch 8 → 9 taken 16918 times.
✓ Branch 8 → 10 taken 4664 times.
✓ Branch 9 → 10 taken 9147 times.
✓ Branch 9 → 11 taken 7771 times.
✓ Branch 12 → 13 taken 13811 times.
✓ Branch 12 → 14 taken 7771 times.
21582 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
43 13811 manIdx++; // Increase the manifestation index
44 13811 continue;
45 }
46
47 // Change scope to concrete struct specialization scope
48
2/2
✓ Branch 14 → 15 taken 3598 times.
✓ Branch 14 → 20 taken 4173 times.
7771 if (node->isMethod) {
49
2/4
✓ Branch 15 → 16 taken 3598 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 3598 times.
✗ Branch 16 → 54 not taken.
3598 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
50
1/2
✓ Branch 17 → 18 taken 3598 times.
✗ Branch 17 → 52 not taken.
3598 changeToScope(scopeName, ScopeType::STRUCT);
51 3598 }
52
53 // Change to function scope
54
1/2
✓ Branch 20 → 21 taken 7771 times.
✗ Branch 20 → 63 not taken.
7771 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 7771 times.
7771 assert(typeMapping.empty());
58
1/2
✓ Branch 24 → 25 taken 7771 times.
✗ Branch 24 → 63 not taken.
7771 typeMapping = manifestation->typeMapping;
59
60 // Set return type to the result variable
61
1/2
✓ Branch 27 → 28 taken 7771 times.
✗ Branch 27 → 57 not taken.
23313 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
62
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 7771 times.
7771 assert(resultVarEntry != nullptr);
63
1/2
✓ Branch 35 → 36 taken 7771 times.
✗ Branch 35 → 63 not taken.
7771 resultVarEntry->updateType(manifestation->returnType, false);
64 7771 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 6140 times.
✓ Branch 36 → 40 taken 1631 times.
7771 if (node->hasParams)
69
1/2
✓ Branch 37 → 38 taken 6140 times.
✗ Branch 37 → 61 not taken.
6140 visit(node->paramLst);
70
71 // Visit statements in new scope
72
2/2
✓ Branch 40 → 41 taken 7769 times.
✓ Branch 40 → 62 taken 2 times.
7771 visit(node->body);
73
74 // Clear type mapping
75 7769 typeMapping.clear();
76
77 // Change to root scope
78 7769 currentScope = rootScope;
79
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 7769 times.
7769 assert(currentScope->type == ScopeType::GLOBAL);
80
81 // Do not type-check this manifestation again
82 7769 manifestation->alreadyTypeChecked = true;
83
84 7769 manIdx++; // Increase the manifestation index
85 }
86 17433 manIdx = 0; // Reset the manifestation index
87
88
1/2
✓ Branch 49 → 50 taken 17433 times.
✗ Branch 49 → 64 not taken.
17433 return nullptr;
89 }
90
91 9081 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
92 9081 node->resizeToNumberOfManifestations(node->manifestations.size());
93 9081 manIdx = 0; // Reset the manifestation index
94
95 // Get all manifestations for this procedure definition
96
2/2
✓ Branch 41 → 6 taken 12195 times.
✓ Branch 41 → 42 taken 9081 times.
21276 for (Function *manifestation : node->manifestations) {
97 // Skip non-substantiated or already checked procedures
98
7/8
✓ Branch 7 → 8 taken 12195 times.
✗ Branch 7 → 50 not taken.
✓ Branch 8 → 9 taken 7389 times.
✓ Branch 8 → 10 taken 4806 times.
✓ Branch 9 → 10 taken 3388 times.
✓ Branch 9 → 11 taken 4001 times.
✓ Branch 12 → 13 taken 8194 times.
✓ Branch 12 → 14 taken 4001 times.
12195 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
99 8194 manIdx++; // Increase the manifestation index
100 8194 continue;
101 }
102
103 // Change scope to concrete struct specialization scope
104
2/2
✓ Branch 14 → 15 taken 2969 times.
✓ Branch 14 → 20 taken 1032 times.
4001 if (node->isMethod) {
105
2/4
✓ Branch 15 → 16 taken 2969 times.
✗ Branch 15 → 47 not taken.
✓ Branch 16 → 17 taken 2969 times.
✗ Branch 16 → 47 not taken.
2969 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
106
1/2
✓ Branch 17 → 18 taken 2969 times.
✗ Branch 17 → 45 not taken.
2969 changeToScope(scopeName, ScopeType::STRUCT);
107 2969 }
108
109 // Change to procedure scope
110
1/2
✓ Branch 20 → 21 taken 4001 times.
✗ Branch 20 → 50 not taken.
4001 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 4001 times.
4001 assert(typeMapping.empty());
114
1/2
✓ Branch 24 → 25 taken 4001 times.
✗ Branch 24 → 50 not taken.
4001 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 2959 times.
✓ Branch 25 → 29 taken 1042 times.
4001 if (node->hasParams)
119
1/2
✓ Branch 26 → 27 taken 2959 times.
✗ Branch 26 → 48 not taken.
2959 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 1392 times.
✓ Branch 29 → 31 taken 2609 times.
4001 if (node->isCtor)
123
1/2
✓ Branch 30 → 31 taken 1392 times.
✗ Branch 30 → 50 not taken.
1392 createCtorBodyPreamble(node->scope);
124
125 // Visit statements in new scope
126
1/2
✓ Branch 31 → 32 taken 4001 times.
✗ Branch 31 → 49 not taken.
4001 visit(node->body);
127
128 // Clear type mapping
129 4001 typeMapping.clear();
130
131 // Change to root scope
132 4001 currentScope = rootScope;
133
2/4
✓ Branch 34 → 35 taken 4001 times.
✗ Branch 34 → 37 not taken.
✓ Branch 35 → 36 taken 4001 times.
✗ Branch 35 → 37 not taken.
4001 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
134
135 // Do not type-check this manifestation again
136 4001 manifestation->alreadyTypeChecked = true;
137
138 4001 manIdx++; // Increase the manifestation index
139 }
140 9081 manIdx = 0; // Reset the manifestation index
141
142
1/2
✓ Branch 42 → 43 taken 9081 times.
✗ Branch 42 → 51 not taken.
9081 return nullptr;
143 }
144
145 1221 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
146 1221 node->resizeToNumberOfManifestations(node->structManifestations.size());
147 1221 manIdx = 0; // Reset the manifestation index
148
149 // Get all manifestations for this procedure definition
150
2/2
✓ Branch 159 → 6 taken 2040 times.
✓ Branch 159 → 160 taken 1221 times.
3261 for (const Struct *manifestation : node->structManifestations) {
151 // Skip non-substantiated or already checked procedures
152
3/4
✓ Branch 7 → 8 taken 2040 times.
✗ Branch 7 → 242 not taken.
✓ Branch 8 → 9 taken 600 times.
✓ Branch 8 → 10 taken 1440 times.
2040 if (!manifestation->isFullySubstantiated()) {
153 600 manIdx++; // Increase the manifestation index
154 600 continue;
155 }
156
157 // Change to struct scope
158
1/2
✓ Branch 10 → 11 taken 1440 times.
✗ Branch 10 → 242 not taken.
1440 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 3391 times.
✓ Branch 26 → 27 taken 1440 times.
4831 for (const FieldNode *field : node->fields) {
162
2/2
✓ Branch 14 → 15 taken 576 times.
✓ Branch 14 → 24 taken 2815 times.
3391 if (field->defaultValue != nullptr) {
163
1/2
✓ Branch 15 → 16 taken 576 times.
✗ Branch 15 → 163 not taken.
576 visit(field->defaultValue);
164
1/2
✓ Branch 17 → 18 taken 576 times.
✗ Branch 17 → 165 not taken.
576 SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName);
165
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 576 times.
576 assert(fieldEntry != nullptr);
166
1/2
✓ Branch 22 → 23 taken 576 times.
✗ Branch 22 → 164 not taken.
576 fieldEntry->updateState(INITIALIZED, field);
167 }
168 }
169
170 // Build struct type
171
1/2
✓ Branch 27 → 28 taken 1440 times.
✗ Branch 27 → 242 not taken.
1440 const QualType structType = manifestation->entry->getQualType();
172
173 // Check if the struct implements all methods of all attached interfaces
174 1440 size_t vtableIndex = 0;
175
2/2
✓ Branch 106 → 30 taken 384 times.
✓ Branch 106 → 107 taken 1440 times.
1824 for (const QualType &interfaceType : manifestation->interfaceTypes) {
176
1/2
✓ Branch 31 → 32 taken 384 times.
✗ Branch 31 → 209 not taken.
384 const Interface *interface = interfaceType.getInterface(node);
177
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 384 times.
384 assert(interface != nullptr);
178
179 // Check for all methods, that it is implemented by the struct
180
2/2
✓ Branch 103 → 36 taken 962 times.
✓ Branch 103 → 104 taken 384 times.
1346 for (const Function *expMethod : interface->methods) {
181
1/2
✓ Branch 37 → 38 taken 962 times.
✗ Branch 37 → 207 not taken.
962 const std::string methodName = expMethod->name;
182
1/2
✓ Branch 38 → 39 taken 962 times.
✗ Branch 38 → 205 not taken.
962 QualTypeList params = expMethod->getParamTypes();
183 962 QualType returnType = expMethod->returnType;
184
185 // Substantiate param and return types
186
1/2
✓ Branch 39 → 40 taken 962 times.
✗ Branch 39 → 203 not taken.
962 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
187
3/4
✓ Branch 40 → 41 taken 962 times.
✗ Branch 40 → 203 not taken.
✓ Branch 41 → 42 taken 546 times.
✓ Branch 41 → 43 taken 416 times.
962 if (returnType.hasAnyGenericParts())
188
1/2
✓ Branch 42 → 43 taken 546 times.
✗ Branch 42 → 203 not taken.
546 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
189
190 // Build args list
191 962 ArgList args;
192
1/2
✓ Branch 44 → 45 taken 962 times.
✗ Branch 44 → 201 not taken.
962 args.reserve(params.size());
193
2/2
✓ Branch 51 → 47 taken 14 times.
✓ Branch 51 → 52 taken 962 times.
976 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 962 times.
✗ Branch 53 → 168 not taken.
962 Function *spiceFunction = FunctionManager::match(currentScope, methodName, structType, args, {}, true, node);
198
2/2
✓ Branch 55 → 56 taken 4 times.
✓ Branch 55 → 68 taken 958 times.
962 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 958 times.
✗ Branch 68 → 201 not taken.
✓ Branch 69 → 70 taken 168 times.
✓ Branch 69 → 73 taken 790 times.
✓ Branch 74 → 75 taken 2 times.
✓ Branch 74 → 87 taken 956 times.
1126 if (spiceFunction->returnType != returnType &&
206
3/4
✓ Branch 70 → 71 taken 168 times.
✗ Branch 70 → 201 not taken.
✓ Branch 71 → 72 taken 2 times.
✓ Branch 71 → 73 taken 166 times.
168 !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 956 spiceFunction->isVirtual = true;
214 956 spiceFunction->vtableIndex = vtableIndex++;
215
6/6
✓ Branch 89 → 90 taken 956 times.
✓ Branch 89 → 91 taken 6 times.
✓ Branch 93 → 94 taken 956 times.
✓ Branch 93 → 95 taken 6 times.
✓ Branch 97 → 98 taken 956 times.
✓ Branch 97 → 100 taken 6 times.
974 }
216 }
217
218 // Check default ctor body if required
219
2/4
✓ Branch 110 → 111 taken 1440 times.
✗ Branch 110 → 212 not taken.
✓ Branch 111 → 112 taken 1440 times.
✗ Branch 111 → 210 not taken.
4320 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
220
4/4
✓ Branch 115 → 116 taken 658 times.
✓ Branch 115 → 121 taken 782 times.
✓ Branch 116 → 117 taken 104 times.
✓ Branch 116 → 121 taken 554 times.
1440 if (ctorFunc != nullptr && ctorFunc->implicitDefault) {
221
1/2
✓ Branch 117 → 118 taken 104 times.
✗ Branch 117 → 242 not taken.
104 createCtorBodyPreamble(ctorFunc->bodyScope);
222
2/4
✓ Branch 118 → 119 taken 104 times.
✗ Branch 118 → 242 not taken.
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 121 taken 104 times.
104 assert(manifestation->areAllFieldsInitialized() == nullptr);
223 }
224
225 // Check default copy ctor body if required
226
2/4
✓ Branch 121 → 122 taken 1440 times.
✗ Branch 121 → 223 not taken.
✓ Branch 125 → 126 taken 1440 times.
✗ Branch 125 → 219 not taken.
4320 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
227
2/4
✓ Branch 129 → 130 taken 1440 times.
✗ Branch 129 → 227 not taken.
✓ Branch 130 → 131 taken 1440 times.
✗ Branch 130 → 225 not taken.
1440 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
228
4/4
✓ Branch 133 → 134 taken 469 times.
✓ Branch 133 → 139 taken 971 times.
✓ Branch 134 → 135 taken 149 times.
✓ Branch 134 → 139 taken 320 times.
1440 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) {
229
1/2
✓ Branch 135 → 136 taken 149 times.
✗ Branch 135 → 240 not taken.
149 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
230
2/4
✓ Branch 136 → 137 taken 149 times.
✗ Branch 136 → 240 not taken.
✗ Branch 137 → 138 not taken.
✓ Branch 137 → 139 taken 149 times.
149 assert(manifestation->areAllFieldsInitialized() == nullptr);
231 }
232
233 // Check default dtor body if required
234
2/4
✓ Branch 142 → 143 taken 1440 times.
✗ Branch 142 → 233 not taken.
✓ Branch 143 → 144 taken 1440 times.
✗ Branch 143 → 231 not taken.
4320 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
235
4/4
✓ Branch 147 → 148 taken 535 times.
✓ Branch 147 → 150 taken 905 times.
✓ Branch 148 → 149 taken 231 times.
✓ Branch 148 → 150 taken 304 times.
1440 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
236
1/2
✓ Branch 149 → 150 taken 231 times.
✗ Branch 149 → 240 not taken.
231 createDtorBodyPreamble(dtorFunc->bodyScope);
237
238 // Reset field symbols to declared state for the next manifestation
239
1/2
✓ Branch 150 → 151 taken 1440 times.
✗ Branch 150 → 240 not taken.
1440 manifestation->resetFieldSymbolsToDeclared(node);
240
241 // Return to the root scope
242 1440 currentScope = rootScope;
243
2/4
✓ Branch 151 → 152 taken 1440 times.
✗ Branch 151 → 154 not taken.
✓ Branch 152 → 153 taken 1440 times.
✗ Branch 152 → 154 not taken.
1440 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
244
245 1440 manIdx++; // Increase the manifestation index
246 1440 }
247 1221 manIdx = 0; // Reset the manifestation index
248
249
1/2
✓ Branch 160 → 161 taken 1221 times.
✗ Branch 160 → 244 not taken.
1221 return nullptr;
250 }
251
252 } // namespace spice::compiler
253