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 <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 442 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
17 // Skip if already type-checked
18
2/2
✓ Branch 2 → 3 taken 51 times.
✓ Branch 2 → 5 taken 391 times.
442 if (typeCheckedMainFct)
19
1/2
✓ Branch 3 → 4 taken 51 times.
✗ Branch 3 → 11 not taken.
51 return nullptr;
20
21 391 node->resizeToNumberOfManifestations(1);
22
23 // Change to function body scope
24 391 currentScope = node->bodyScope;
25 // Visit statements in new scope
26
2/2
✓ Branch 6 → 7 taken 361 times.
✓ Branch 6 → 12 taken 30 times.
391 visit(node->body);
27 // Leave main function body scope
28 361 currentScope = rootScope;
29
30 // Set to type-checked
31 361 typeCheckedMainFct = true;
32
1/2
✓ Branch 8 → 9 taken 361 times.
✗ Branch 8 → 13 not taken.
361 return nullptr;
33 }
34
35 17425 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
36 17425 node->resizeToNumberOfManifestations(node->manifestations.size());
37 17425 manIdx = 0; // Reset the manifestation index
38
39 // Get all manifestations for this function definition
40
2/2
✓ Branch 48 → 6 taken 21569 times.
✓ Branch 48 → 49 taken 17423 times.
38992 for (Function *manifestation : node->manifestations) {
41 // Skip non-substantiated or already checked functions
42
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) {
43 13809 manIdx++; // Increase the manifestation index
44 13809 continue;
45 }
46
47 // Change scope to concrete struct specialization scope
48
2/2
✓ Branch 14 → 15 taken 3598 times.
✓ Branch 14 → 20 taken 4162 times.
7760 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 7760 times.
✗ Branch 20 → 63 not taken.
7760 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 7760 times.
7760 assert(typeMapping.empty());
58
1/2
✓ Branch 24 → 25 taken 7760 times.
✗ Branch 24 → 63 not taken.
7760 typeMapping = manifestation->typeMapping;
59
60 // Set return type to the result variable
61
1/2
✓ Branch 27 → 28 taken 7760 times.
✗ Branch 27 → 57 not taken.
23280 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
62
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 7760 times.
7760 assert(resultVarEntry != nullptr);
63
1/2
✓ Branch 35 → 36 taken 7760 times.
✗ Branch 35 → 63 not taken.
7760 resultVarEntry->updateType(manifestation->returnType, false);
64 7760 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 6132 times.
✓ Branch 36 → 40 taken 1628 times.
7760 if (node->hasParams)
69
1/2
✓ Branch 37 → 38 taken 6132 times.
✗ Branch 37 → 61 not taken.
6132 visit(node->paramLst);
70
71 // Visit statements in new scope
72
2/2
✓ Branch 40 → 41 taken 7758 times.
✓ Branch 40 → 62 taken 2 times.
7760 visit(node->body);
73
74 // Clear type mapping
75 7758 typeMapping.clear();
76
77 // Change to root scope
78 7758 currentScope = rootScope;
79
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 7758 times.
7758 assert(currentScope->type == ScopeType::GLOBAL);
80
81 // Do not type-check this manifestation again
82 7758 manifestation->alreadyTypeChecked = true;
83
84 7758 manIdx++; // Increase the manifestation index
85 }
86 17423 manIdx = 0; // Reset the manifestation index
87
88
1/2
✓ Branch 49 → 50 taken 17423 times.
✗ Branch 49 → 64 not taken.
17423 return nullptr;
89 }
90
91 9074 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
92 9074 node->resizeToNumberOfManifestations(node->manifestations.size());
93 9074 manIdx = 0; // Reset the manifestation index
94
95 // Get all manifestations for this procedure definition
96
2/2
✓ Branch 41 → 6 taken 12188 times.
✓ Branch 41 → 42 taken 9074 times.
21262 for (Function *manifestation : node->manifestations) {
97 // Skip non-substantiated or already checked procedures
98
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) {
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 2963 times.
✓ Branch 14 → 20 taken 1031 times.
3994 if (node->isMethod) {
105
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());
106
1/2
✓ Branch 17 → 18 taken 2963 times.
✗ Branch 17 → 45 not taken.
2963 changeToScope(scopeName, ScopeType::STRUCT);
107 2963 }
108
109 // Change to procedure scope
110
1/2
✓ Branch 20 → 21 taken 3994 times.
✗ Branch 20 → 50 not taken.
3994 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 3994 times.
3994 assert(typeMapping.empty());
114
1/2
✓ Branch 24 → 25 taken 3994 times.
✗ Branch 24 → 50 not taken.
3994 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 2957 times.
✓ Branch 25 → 29 taken 1037 times.
3994 if (node->hasParams)
119
1/2
✓ Branch 26 → 27 taken 2957 times.
✗ Branch 26 → 48 not taken.
2957 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 1388 times.
✓ Branch 29 → 31 taken 2606 times.
3994 if (node->isCtor)
123
1/2
✓ Branch 30 → 31 taken 1388 times.
✗ Branch 30 → 50 not taken.
1388 createCtorBodyPreamble(node->scope);
124
125 // Visit statements in new scope
126
1/2
✓ Branch 31 → 32 taken 3994 times.
✗ Branch 31 → 49 not taken.
3994 visit(node->body);
127
128 // Clear type mapping
129 3994 typeMapping.clear();
130
131 // Change to root scope
132 3994 currentScope = rootScope;
133
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);
134
135 // Do not type-check this manifestation again
136 3994 manifestation->alreadyTypeChecked = true;
137
138 3994 manIdx++; // Increase the manifestation index
139 }
140 9074 manIdx = 0; // Reset the manifestation index
141
142
1/2
✓ Branch 42 → 43 taken 9074 times.
✗ Branch 42 → 51 not taken.
9074 return nullptr;
143 }
144
145 1219 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
146 1219 node->resizeToNumberOfManifestations(node->structManifestations.size());
147 1219 manIdx = 0; // Reset the manifestation index
148
149 // Get all manifestations for this procedure definition
150
2/2
✓ Branch 159 → 6 taken 2038 times.
✓ Branch 159 → 160 taken 1219 times.
3257 for (const Struct *manifestation : node->structManifestations) {
151 // Skip non-substantiated or already checked procedures
152
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()) {
153 600 manIdx++; // Increase the manifestation index
154 600 continue;
155 }
156
157 // Change to struct scope
158
1/2
✓ Branch 10 → 11 taken 1438 times.
✗ Branch 10 → 242 not taken.
1438 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 3389 times.
✓ Branch 26 → 27 taken 1438 times.
4827 for (const FieldNode *field : node->fields) {
162
2/2
✓ Branch 14 → 15 taken 575 times.
✓ Branch 14 → 24 taken 2814 times.
3389 if (field->defaultValue != nullptr) {
163
1/2
✓ Branch 15 → 16 taken 575 times.
✗ Branch 15 → 163 not taken.
575 visit(field->defaultValue);
164
1/2
✓ Branch 17 → 18 taken 575 times.
✗ Branch 17 → 165 not taken.
575 SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName);
165
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 575 times.
575 assert(fieldEntry != nullptr);
166
1/2
✓ Branch 22 → 23 taken 575 times.
✗ Branch 22 → 164 not taken.
575 fieldEntry->updateState(INITIALIZED, field);
167 }
168 }
169
170 // Build struct type
171
1/2
✓ Branch 27 → 28 taken 1438 times.
✗ Branch 27 → 242 not taken.
1438 const QualType structType = manifestation->entry->getQualType();
172
173 // Check if the struct implements all methods of all attached interfaces
174 1438 size_t vtableIndex = 0;
175
2/2
✓ Branch 106 → 30 taken 384 times.
✓ Branch 106 → 107 taken 1438 times.
1822 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
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() +
201
1/2
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 173 not taken.
4 "'. The signature does not match.");
202 4 continue;
203 }
204
205 // Check return type
206
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 &&
207
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)) {
208
1/2
✓ Branch 80 → 81 taken 2 times.
✗ Branch 80 → 186 not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
209
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() +
210
1/2
✓ Branch 79 → 80 taken 2 times.
✗ Branch 79 → 188 not taken.
2 "'. The return type does not match.");
211 2 continue;
212 }
213 // Set to virtual, since it overrides the interface method
214 956 spiceFunction->isVirtual = true;
215 956 spiceFunction->vtableIndex = vtableIndex++;
216
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 }
217 }
218
219 // Check default ctor body if required
220
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);
221
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) {
222
1/2
✓ Branch 117 → 118 taken 104 times.
✗ Branch 117 → 242 not taken.
104 createCtorBodyPreamble(ctorFunc->bodyScope);
223
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);
224 }
225
226 // Check default copy ctor body if required
227
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 */}};
228
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);
229
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) {
230
1/2
✓ Branch 135 → 136 taken 149 times.
✗ Branch 135 → 240 not taken.
149 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
231
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);
232 }
233
234 // Check default dtor body if required
235
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);
236
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)
237
1/2
✓ Branch 149 → 150 taken 231 times.
✗ Branch 149 → 240 not taken.
231 createDtorBodyPreamble(dtorFunc->bodyScope);
238
239 // Reset field symbols to declared state for the next manifestation
240
1/2
✓ Branch 150 → 151 taken 1438 times.
✗ Branch 150 → 240 not taken.
1438 manifestation->resetFieldSymbolsToDeclared(node);
241
242 // Return to the root scope
243 1438 currentScope = rootScope;
244
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);
245
246 1438 manIdx++; // Increase the manifestation index
247 1438 }
248 1219 manIdx = 0; // Reset the manifestation index
249
250
1/2
✓ Branch 160 → 161 taken 1219 times.
✗ Branch 160 → 244 not taken.
1219 return nullptr;
251 }
252
253 } // namespace spice::compiler
254