GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 127 / 0 / 127
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 <exception/SemanticError.h>
7 #include <global/GlobalResourceManager.h>
8 #include <symboltablebuilder/SymbolTableBuilder.h>
9 #include <typechecker/TypeMatcher.h>
10
11 namespace spice::compiler {
12
13 442 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
14 // Skip if already type-checked
15
2/2
✓ Branch 2 → 3 taken 51 times.
✓ Branch 2 → 5 taken 391 times.
442 if (typeCheckedMainFct)
16
1/2
✓ Branch 3 → 4 taken 51 times.
✗ Branch 3 → 11 not taken.
51 return nullptr;
17
18 391 node->resizeToNumberOfManifestations(1);
19
20 // Change to function body scope
21 391 currentScope = node->bodyScope;
22 // Visit statements in new scope
23
2/2
✓ Branch 6 → 7 taken 361 times.
✓ Branch 6 → 12 taken 30 times.
391 visit(node->body);
24 // Leave main function body scope
25 361 currentScope = rootScope;
26
27 // Set to type-checked
28 361 typeCheckedMainFct = true;
29
1/2
✓ Branch 8 → 9 taken 361 times.
✗ Branch 8 → 13 not taken.
361 return nullptr;
30 }
31
32 17425 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
33 17425 node->resizeToNumberOfManifestations(node->manifestations.size());
34 17425 manIdx = 0; // Reset the manifestation index
35
36 // Get all manifestations for this function definition
37
2/2
✓ Branch 48 → 6 taken 21569 times.
✓ Branch 48 → 49 taken 17423 times.
38992 for (Function *manifestation : node->manifestations) {
38 // Skip non-substantiated or already checked functions
39
7/8
✓ Branch 7 → 8 taken 21569 times.
✗ Branch 7 → 63 not taken.
✓ Branch 8 → 9 taken 16907 times.
✓ Branch 8 → 10 taken 4662 times.
✓ Branch 9 → 10 taken 9147 times.
✓ Branch 9 → 11 taken 7760 times.
✓ Branch 12 → 13 taken 13809 times.
✓ Branch 12 → 14 taken 7760 times.
21569 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
40 13809 manIdx++; // Increase the manifestation index
41 13809 continue;
42 }
43
44 // Change scope to concrete struct specialization scope
45
2/2
✓ Branch 14 → 15 taken 3598 times.
✓ Branch 14 → 20 taken 4162 times.
7760 if (node->isMethod) {
46
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());
47
1/2
✓ Branch 17 → 18 taken 3598 times.
✗ Branch 17 → 52 not taken.
3598 changeToScope(scopeName, ScopeType::STRUCT);
48 3598 }
49
50 // Change to function scope
51
1/2
✓ Branch 20 → 21 taken 7760 times.
✗ Branch 20 → 63 not taken.
7760 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
52
53 // Mount type mapping for this manifestation
54
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 7760 times.
7760 assert(typeMapping.empty());
55
1/2
✓ Branch 24 → 25 taken 7760 times.
✗ Branch 24 → 63 not taken.
7760 typeMapping = manifestation->typeMapping;
56
57 // Set return type to the result variable
58
1/2
✓ Branch 27 → 28 taken 7760 times.
✗ Branch 27 → 57 not taken.
23280 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
59
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 7760 times.
7760 assert(resultVarEntry != nullptr);
60
1/2
✓ Branch 35 → 36 taken 7760 times.
✗ Branch 35 → 63 not taken.
7760 resultVarEntry->updateType(manifestation->returnType, false);
61 7760 resultVarEntry->used = true;
62
63 // Visit parameters
64 // This happens once in the type checker prepare stage. This second time is only required if we have a generic function
65
2/2
✓ Branch 36 → 37 taken 6132 times.
✓ Branch 36 → 40 taken 1628 times.
7760 if (node->hasParams)
66
1/2
✓ Branch 37 → 38 taken 6132 times.
✗ Branch 37 → 61 not taken.
6132 visit(node->paramLst);
67
68 // Visit statements in new scope
69
2/2
✓ Branch 40 → 41 taken 7758 times.
✓ Branch 40 → 62 taken 2 times.
7760 visit(node->body);
70
71 // Clear type mapping
72 7758 typeMapping.clear();
73
74 // Change to root scope
75 7758 currentScope = rootScope;
76
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 7758 times.
7758 assert(currentScope->type == ScopeType::GLOBAL);
77
78 // Do not type-check this manifestation again
79 7758 manifestation->alreadyTypeChecked = true;
80
81 7758 manIdx++; // Increase the manifestation index
82 }
83 17423 manIdx = 0; // Reset the manifestation index
84
85
1/2
✓ Branch 49 → 50 taken 17423 times.
✗ Branch 49 → 64 not taken.
17423 return nullptr;
86 }
87
88 9074 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
89 9074 node->resizeToNumberOfManifestations(node->manifestations.size());
90 9074 manIdx = 0; // Reset the manifestation index
91
92 // Get all manifestations for this procedure definition
93
2/2
✓ Branch 41 → 6 taken 12188 times.
✓ Branch 41 → 42 taken 9074 times.
21262 for (Function *manifestation : node->manifestations) {
94 // Skip non-substantiated or already checked procedures
95
7/8
✓ Branch 7 → 8 taken 12188 times.
✗ Branch 7 → 50 not taken.
✓ Branch 8 → 9 taken 7382 times.
✓ Branch 8 → 10 taken 4806 times.
✓ Branch 9 → 10 taken 3388 times.
✓ Branch 9 → 11 taken 3994 times.
✓ Branch 12 → 13 taken 8194 times.
✓ Branch 12 → 14 taken 3994 times.
12188 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
96 8194 manIdx++; // Increase the manifestation index
97 8194 continue;
98 }
99
100 // Change scope to concrete struct specialization scope
101
2/2
✓ Branch 14 → 15 taken 2963 times.
✓ Branch 14 → 20 taken 1031 times.
3994 if (node->isMethod) {
102
2/4
✓ Branch 15 → 16 taken 2963 times.
✗ Branch 15 → 47 not taken.
✓ Branch 16 → 17 taken 2963 times.
✗ Branch 16 → 47 not taken.
2963 const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes());
103
1/2
✓ Branch 17 → 18 taken 2963 times.
✗ Branch 17 → 45 not taken.
2963 changeToScope(scopeName, ScopeType::STRUCT);
104 2963 }
105
106 // Change to procedure scope
107
1/2
✓ Branch 20 → 21 taken 3994 times.
✗ Branch 20 → 50 not taken.
3994 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
108
109 // Mount type mapping for this manifestation
110
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 3994 times.
3994 assert(typeMapping.empty());
111
1/2
✓ Branch 24 → 25 taken 3994 times.
✗ Branch 24 → 50 not taken.
3994 typeMapping = manifestation->typeMapping;
112
113 // Visit parameters
114 // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure
115
2/2
✓ Branch 25 → 26 taken 2957 times.
✓ Branch 25 → 29 taken 1037 times.
3994 if (node->hasParams)
116
1/2
✓ Branch 26 → 27 taken 2957 times.
✗ Branch 26 → 48 not taken.
2957 visit(node->paramLst);
117
118 // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required
119
2/2
✓ Branch 29 → 30 taken 1388 times.
✓ Branch 29 → 31 taken 2606 times.
3994 if (node->isCtor)
120
1/2
✓ Branch 30 → 31 taken 1388 times.
✗ Branch 30 → 50 not taken.
1388 createCtorBodyPreamble(node->scope);
121
122 // Visit statements in new scope
123
1/2
✓ Branch 31 → 32 taken 3994 times.
✗ Branch 31 → 49 not taken.
3994 visit(node->body);
124
125 // Clear type mapping
126 3994 typeMapping.clear();
127
128 // Change to root scope
129 3994 currentScope = rootScope;
130
2/4
✓ Branch 34 → 35 taken 3994 times.
✗ Branch 34 → 37 not taken.
✓ Branch 35 → 36 taken 3994 times.
✗ Branch 35 → 37 not taken.
3994 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
131
132 // Do not type-check this manifestation again
133 3994 manifestation->alreadyTypeChecked = true;
134
135 3994 manIdx++; // Increase the manifestation index
136 }
137 9074 manIdx = 0; // Reset the manifestation index
138
139
1/2
✓ Branch 42 → 43 taken 9074 times.
✗ Branch 42 → 51 not taken.
9074 return nullptr;
140 }
141
142 1219 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
143 1219 node->resizeToNumberOfManifestations(node->structManifestations.size());
144 1219 manIdx = 0; // Reset the manifestation index
145
146 // Get all manifestations for this procedure definition
147
2/2
✓ Branch 159 → 6 taken 2038 times.
✓ Branch 159 → 160 taken 1219 times.
3257 for (const Struct *manifestation : node->structManifestations) {
148 // Skip non-substantiated or already checked procedures
149
3/4
✓ Branch 7 → 8 taken 2038 times.
✗ Branch 7 → 242 not taken.
✓ Branch 8 → 9 taken 600 times.
✓ Branch 8 → 10 taken 1438 times.
2038 if (!manifestation->isFullySubstantiated()) {
150 600 manIdx++; // Increase the manifestation index
151 600 continue;
152 }
153
154 // Change to struct scope
155
1/2
✓ Branch 10 → 11 taken 1438 times.
✗ Branch 10 → 242 not taken.
1438 changeToScope(manifestation->scope, ScopeType::STRUCT);
156
157 // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations
158
2/2
✓ Branch 26 → 13 taken 3389 times.
✓ Branch 26 → 27 taken 1438 times.
4827 for (const FieldNode *field : node->fields) {
159
2/2
✓ Branch 14 → 15 taken 575 times.
✓ Branch 14 → 24 taken 2814 times.
3389 if (field->defaultValue != nullptr) {
160
1/2
✓ Branch 15 → 16 taken 575 times.
✗ Branch 15 → 163 not taken.
575 visit(field->defaultValue);
161
1/2
✓ Branch 17 → 18 taken 575 times.
✗ Branch 17 → 165 not taken.
575 SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName);
162
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 575 times.
575 assert(fieldEntry != nullptr);
163
1/2
✓ Branch 22 → 23 taken 575 times.
✗ Branch 22 → 164 not taken.
575 fieldEntry->updateState(INITIALIZED, field);
164 }
165 }
166
167 // Build struct type
168
1/2
✓ Branch 27 → 28 taken 1438 times.
✗ Branch 27 → 242 not taken.
1438 const QualType structType = manifestation->entry->getQualType();
169
170 // Check if the struct implements all methods of all attached interfaces
171 1438 size_t vtableIndex = 0;
172
2/2
✓ Branch 106 → 30 taken 384 times.
✓ Branch 106 → 107 taken 1438 times.
1822 for (const QualType &interfaceType : manifestation->interfaceTypes) {
173
1/2
✓ Branch 31 → 32 taken 384 times.
✗ Branch 31 → 209 not taken.
384 const Interface *interface = interfaceType.getInterface(node);
174
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 384 times.
384 assert(interface != nullptr);
175
176 // Check for all methods, that it is implemented by the struct
177
2/2
✓ Branch 103 → 36 taken 962 times.
✓ Branch 103 → 104 taken 384 times.
1346 for (const Function *expMethod : interface->methods) {
178
1/2
✓ Branch 37 → 38 taken 962 times.
✗ Branch 37 → 207 not taken.
962 const std::string methodName = expMethod->name;
179
1/2
✓ Branch 38 → 39 taken 962 times.
✗ Branch 38 → 205 not taken.
962 QualTypeList params = expMethod->getParamTypes();
180 962 QualType returnType = expMethod->returnType;
181
182 // Substantiate param and return types
183
1/2
✓ Branch 39 → 40 taken 962 times.
✗ Branch 39 → 203 not taken.
962 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
184
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())
185
1/2
✓ Branch 42 → 43 taken 546 times.
✗ Branch 42 → 203 not taken.
546 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
186
187 // Build args list
188 962 ArgList args;
189
1/2
✓ Branch 44 → 45 taken 962 times.
✗ Branch 44 → 201 not taken.
962 args.reserve(params.size());
190
2/2
✓ Branch 51 → 47 taken 14 times.
✓ Branch 51 → 52 taken 962 times.
976 for (const QualType &param : params)
191
1/2
✓ Branch 48 → 49 taken 14 times.
✗ Branch 48 → 166 not taken.
14 args.emplace_back(param, nullptr);
192
193 // Search for method that has the required signature
194
1/2
✓ Branch 53 → 54 taken 962 times.
✗ Branch 53 → 168 not taken.
962 Function *spiceFunction = FunctionManager::match(currentScope, methodName, structType, args, {}, true, node);
195
2/2
✓ Branch 55 → 56 taken 4 times.
✓ Branch 55 → 68 taken 958 times.
962 if (spiceFunction == nullptr) {
196
1/2
✓ Branch 61 → 62 taken 4 times.
✗ Branch 61 → 171 not taken.
4 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
197
4/8
✓ 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.
8 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
198
1/2
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 173 not taken.
4 "'. The signature does not match.");
199 4 continue;
200 }
201
202 // Check return type
203
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 &&
204
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)) {
205
1/2
✓ Branch 80 → 81 taken 2 times.
✗ Branch 80 → 186 not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
206
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() +
207
1/2
✓ Branch 79 → 80 taken 2 times.
✗ Branch 79 → 188 not taken.
2 "'. The return type does not match.");
208 2 continue;
209 }
210 // Set to virtual, since it overrides the interface method
211 956 spiceFunction->isVirtual = true;
212 956 spiceFunction->vtableIndex = vtableIndex++;
213
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 }
214 }
215
216 // Check default ctor body if required
217
2/4
✓ Branch 110 → 111 taken 1438 times.
✗ Branch 110 → 212 not taken.
✓ Branch 111 → 112 taken 1438 times.
✗ Branch 111 → 210 not taken.
4314 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
218
4/4
✓ Branch 115 → 116 taken 656 times.
✓ Branch 115 → 121 taken 782 times.
✓ Branch 116 → 117 taken 104 times.
✓ Branch 116 → 121 taken 552 times.
1438 if (ctorFunc != nullptr && ctorFunc->implicitDefault) {
219
1/2
✓ Branch 117 → 118 taken 104 times.
✗ Branch 117 → 242 not taken.
104 createCtorBodyPreamble(ctorFunc->bodyScope);
220
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);
221 }
222
223 // Check default copy ctor body if required
224
2/4
✓ Branch 121 → 122 taken 1438 times.
✗ Branch 121 → 223 not taken.
✓ Branch 125 → 126 taken 1438 times.
✗ Branch 125 → 219 not taken.
4314 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
225
2/4
✓ Branch 129 → 130 taken 1438 times.
✗ Branch 129 → 227 not taken.
✓ Branch 130 → 131 taken 1438 times.
✗ Branch 130 → 225 not taken.
1438 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
226
4/4
✓ Branch 133 → 134 taken 467 times.
✓ Branch 133 → 139 taken 971 times.
✓ Branch 134 → 135 taken 149 times.
✓ Branch 134 → 139 taken 318 times.
1438 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) {
227
1/2
✓ Branch 135 → 136 taken 149 times.
✗ Branch 135 → 240 not taken.
149 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
228
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);
229 }
230
231 // Check default dtor body if required
232
2/4
✓ Branch 142 → 143 taken 1438 times.
✗ Branch 142 → 233 not taken.
✓ Branch 143 → 144 taken 1438 times.
✗ Branch 143 → 231 not taken.
4314 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
233
4/4
✓ Branch 147 → 148 taken 533 times.
✓ Branch 147 → 150 taken 905 times.
✓ Branch 148 → 149 taken 231 times.
✓ Branch 148 → 150 taken 302 times.
1438 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
234
1/2
✓ Branch 149 → 150 taken 231 times.
✗ Branch 149 → 240 not taken.
231 createDtorBodyPreamble(dtorFunc->bodyScope);
235
236 // Reset field symbols to declared state for the next manifestation
237
1/2
✓ Branch 150 → 151 taken 1438 times.
✗ Branch 150 → 240 not taken.
1438 manifestation->resetFieldSymbolsToDeclared(node);
238
239 // Return to the root scope
240 1438 currentScope = rootScope;
241
2/4
✓ Branch 151 → 152 taken 1438 times.
✗ Branch 151 → 154 not taken.
✓ Branch 152 → 153 taken 1438 times.
✗ Branch 152 → 154 not taken.
1438 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
242
243 1438 manIdx++; // Increase the manifestation index
244 1438 }
245 1219 manIdx = 0; // Reset the manifestation index
246
247
1/2
✓ Branch 160 → 161 taken 1219 times.
✗ Branch 160 → 244 not taken.
1219 return nullptr;
248 }
249
250 } // namespace spice::compiler
251