GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeCheckerTopLevelDefinitionsCheck.cpp
Date: 2025-10-09 06:28:01
Coverage Exec Excl Total
Lines: 100.0% 121 0 121
Functions: 100.0% 4 0 4
Branches: 67.0% 146 0 218

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "TypeChecker.h"
4
5 #include <SourceFile.h>
6 #include <exception/SemanticError.h>
7 #include <symboltablebuilder/SymbolTableBuilder.h>
8 #include <typechecker/TypeMatcher.h>
9
10 namespace spice::compiler {
11
12 409 std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) {
13 // Skip if already type-checked
14
2/2
✓ Branch 2 → 3 taken 43 times.
✓ Branch 2 → 5 taken 366 times.
409 if (typeCheckedMainFct)
15
1/2
✓ Branch 3 → 4 taken 43 times.
✗ Branch 3 → 11 not taken.
43 return nullptr;
16
17 366 node->resizeToNumberOfManifestations(1);
18
19 // Change to function body scope
20 366 currentScope = node->bodyScope;
21 // Visit statements in new scope
22
2/2
✓ Branch 6 → 7 taken 338 times.
✓ Branch 6 → 12 taken 28 times.
366 visit(node->body);
23 // Leave main function body scope
24 338 currentScope = rootScope;
25
26 // Set to type-checked
27 338 typeCheckedMainFct = true;
28
1/2
✓ Branch 8 → 9 taken 338 times.
✗ Branch 8 → 13 not taken.
338 return nullptr;
29 }
30
31 15881 std::any TypeChecker::visitFctDefCheck(FctDefNode *node) {
32 15881 node->resizeToNumberOfManifestations(node->manifestations.size());
33 15881 manIdx = 0; // Reset the manifestation index
34
35 // Get all manifestations for this function definition
36
2/2
✓ Branch 50 → 6 taken 19706 times.
✓ Branch 50 → 51 taken 15879 times.
35585 for (Function *manifestation : node->manifestations) {
37 // Skip non-substantiated or already checked functions
38
7/8
✓ Branch 7 → 8 taken 19706 times.
✗ Branch 7 → 68 not taken.
✓ Branch 8 → 9 taken 15516 times.
✓ Branch 8 → 10 taken 4190 times.
✓ Branch 9 → 10 taken 8367 times.
✓ Branch 9 → 11 taken 7149 times.
✓ Branch 12 → 13 taken 12557 times.
✓ Branch 12 → 14 taken 7149 times.
19706 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
39 12557 manIdx++; // Increase the manifestation index
40 12557 continue;
41 }
42
43 // Change scope to concrete struct specialization scope
44
2/2
✓ Branch 14 → 15 taken 3299 times.
✓ Branch 14 → 22 taken 3850 times.
7149 if (node->isMethod) {
45
2/4
✓ Branch 15 → 16 taken 3299 times.
✗ Branch 15 → 59 not taken.
✓ Branch 16 → 17 taken 3299 times.
✗ Branch 16 → 59 not taken.
3299 const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes());
46
2/4
✓ Branch 17 → 18 taken 3299 times.
✗ Branch 17 → 56 not taken.
✓ Branch 18 → 19 taken 3299 times.
✗ Branch 18 → 54 not taken.
3299 changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT);
47 3299 }
48
49 // Change to function scope
50
1/2
✓ Branch 22 → 23 taken 7149 times.
✗ Branch 22 → 68 not taken.
7149 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
51
52 // Mount type mapping for this manifestation
53
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 7149 times.
7149 assert(typeMapping.empty());
54
1/2
✓ Branch 26 → 27 taken 7149 times.
✗ Branch 26 → 68 not taken.
7149 typeMapping = manifestation->typeMapping;
55
56 // Set return type to the result variable
57
1/2
✓ Branch 29 → 30 taken 7149 times.
✗ Branch 29 → 62 not taken.
21447 SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
58
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 7149 times.
7149 assert(resultVarEntry != nullptr);
59
1/2
✓ Branch 37 → 38 taken 7149 times.
✗ Branch 37 → 68 not taken.
7149 resultVarEntry->updateType(manifestation->returnType, false);
60 7149 resultVarEntry->used = true;
61
62 // Visit parameters
63 // This happens once in the type checker prepare stage. This second time is only required if we have a generic function
64
2/2
✓ Branch 38 → 39 taken 5675 times.
✓ Branch 38 → 42 taken 1474 times.
7149 if (node->hasParams)
65
1/2
✓ Branch 39 → 40 taken 5675 times.
✗ Branch 39 → 66 not taken.
5675 visit(node->paramLst);
66
67 // Visit statements in new scope
68
2/2
✓ Branch 42 → 43 taken 7147 times.
✓ Branch 42 → 67 taken 2 times.
7149 visit(node->body);
69
70 // Clear type mapping
71 7147 typeMapping.clear();
72
73 // Change to root scope
74 7147 currentScope = rootScope;
75
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 7147 times.
7147 assert(currentScope->type == ScopeType::GLOBAL);
76
77 // Do not type-check this manifestation again
78 7147 manifestation->alreadyTypeChecked = true;
79
80 7147 manIdx++; // Increase the manifestation index
81 }
82 15879 manIdx = 0; // Reset the manifestation index
83
84
1/2
✓ Branch 51 → 52 taken 15879 times.
✗ Branch 51 → 69 not taken.
15879 return nullptr;
85 }
86
87 7963 std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) {
88 7963 node->resizeToNumberOfManifestations(node->manifestations.size());
89 7963 manIdx = 0; // Reset the manifestation index
90
91 // Get all manifestations for this procedure definition
92
2/2
✓ Branch 43 → 6 taken 9859 times.
✓ Branch 43 → 44 taken 7963 times.
17822 for (Function *manifestation : node->manifestations) {
93 // Skip non-substantiated or already checked procedures
94
7/8
✓ Branch 7 → 8 taken 9859 times.
✗ Branch 7 → 55 not taken.
✓ Branch 8 → 9 taken 5650 times.
✓ Branch 8 → 10 taken 4209 times.
✓ Branch 9 → 10 taken 2698 times.
✓ Branch 9 → 11 taken 2952 times.
✓ Branch 12 → 13 taken 6907 times.
✓ Branch 12 → 14 taken 2952 times.
9859 if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) {
95 6907 manIdx++; // Increase the manifestation index
96 6907 continue;
97 }
98
99 // Change scope to concrete struct specialization scope
100
2/2
✓ Branch 14 → 15 taken 2708 times.
✓ Branch 14 → 22 taken 244 times.
2952 if (node->isMethod) {
101
2/4
✓ Branch 15 → 16 taken 2708 times.
✗ Branch 15 → 52 not taken.
✓ Branch 16 → 17 taken 2708 times.
✗ Branch 16 → 52 not taken.
2708 const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes());
102
2/4
✓ Branch 17 → 18 taken 2708 times.
✗ Branch 17 → 49 not taken.
✓ Branch 18 → 19 taken 2708 times.
✗ Branch 18 → 47 not taken.
2708 changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT);
103 2708 }
104
105 // Change to procedure scope
106
1/2
✓ Branch 22 → 23 taken 2952 times.
✗ Branch 22 → 55 not taken.
2952 changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY);
107
108 // Mount type mapping for this manifestation
109
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 2952 times.
2952 assert(typeMapping.empty());
110
1/2
✓ Branch 26 → 27 taken 2952 times.
✗ Branch 26 → 55 not taken.
2952 typeMapping = manifestation->typeMapping;
111
112 // Visit parameters
113 // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure
114
2/2
✓ Branch 27 → 28 taken 2045 times.
✓ Branch 27 → 31 taken 907 times.
2952 if (node->hasParams)
115
1/2
✓ Branch 28 → 29 taken 2045 times.
✗ Branch 28 → 53 not taken.
2045 visit(node->paramLst);
116
117 // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required
118
2/2
✓ Branch 31 → 32 taken 1261 times.
✓ Branch 31 → 33 taken 1691 times.
2952 if (node->isCtor)
119
1/2
✓ Branch 32 → 33 taken 1261 times.
✗ Branch 32 → 55 not taken.
1261 createCtorBodyPreamble(node->scope);
120
121 // Visit statements in new scope
122
1/2
✓ Branch 33 → 34 taken 2952 times.
✗ Branch 33 → 54 not taken.
2952 visit(node->body);
123
124 // Clear type mapping
125 2952 typeMapping.clear();
126
127 // Change to root scope
128 2952 currentScope = rootScope;
129
2/4
✓ Branch 36 → 37 taken 2952 times.
✗ Branch 36 → 39 not taken.
✓ Branch 37 → 38 taken 2952 times.
✗ Branch 37 → 39 not taken.
2952 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
130
131 // Do not type-check this manifestation again
132 2952 manifestation->alreadyTypeChecked = true;
133
134 2952 manIdx++; // Increase the manifestation index
135 }
136 7963 manIdx = 0; // Reset the manifestation index
137
138
1/2
✓ Branch 44 → 45 taken 7963 times.
✗ Branch 44 → 56 not taken.
7963 return nullptr;
139 }
140
141 1115 std::any TypeChecker::visitStructDefCheck(StructDefNode *node) {
142 1115 node->resizeToNumberOfManifestations(node->structManifestations.size());
143 1115 manIdx = 0; // Reset the manifestation index
144
145 // Get all manifestations for this procedure definition
146
2/2
✓ Branch 146 → 6 taken 1874 times.
✓ Branch 146 → 147 taken 1115 times.
2989 for (Struct *manifestation : node->structManifestations) {
147 // Skip non-substantiated or already checked procedures
148
3/4
✓ Branch 7 → 8 taken 1874 times.
✗ Branch 7 → 228 not taken.
✓ Branch 8 → 9 taken 556 times.
✓ Branch 8 → 10 taken 1318 times.
1874 if (!manifestation->isFullySubstantiated()) {
149 556 manIdx++; // Increase the manifestation index
150 556 continue;
151 }
152
153 // Change to struct scope
154
1/2
✓ Branch 10 → 11 taken 1318 times.
✗ Branch 10 → 228 not taken.
1318 changeToScope(manifestation->scope, ScopeType::STRUCT);
155
156 // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations
157
2/2
✓ Branch 20 → 13 taken 3141 times.
✓ Branch 20 → 21 taken 1318 times.
4459 for (const FieldNode *field : node->fields)
158
2/2
✓ Branch 14 → 15 taken 537 times.
✓ Branch 14 → 18 taken 2604 times.
3141 if (field->defaultValue != nullptr)
159
1/2
✓ Branch 15 → 16 taken 537 times.
✗ Branch 15 → 150 not taken.
537 visit(field->defaultValue);
160
161 // Build struct type
162
1/2
✓ Branch 21 → 22 taken 1318 times.
✗ Branch 21 → 228 not taken.
1318 const QualType structType = manifestation->entry->getQualType();
163
164 // Check if the struct implements all methods of all attached interfaces
165 1318 size_t vtableIndex = 0;
166
2/2
✓ Branch 100 → 24 taken 350 times.
✓ Branch 100 → 101 taken 1318 times.
1668 for (const QualType &interfaceType : manifestation->interfaceTypes) {
167
1/2
✓ Branch 25 → 26 taken 350 times.
✗ Branch 25 → 195 not taken.
350 const Interface *interface = interfaceType.getInterface(node);
168
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 350 times.
350 assert(interface != nullptr);
169
170 // Check for all methods, that it is implemented by the struct
171
2/2
✓ Branch 97 → 30 taken 880 times.
✓ Branch 97 → 98 taken 350 times.
1230 for (const Function *expMethod : interface->methods) {
172
1/2
✓ Branch 31 → 32 taken 880 times.
✗ Branch 31 → 193 not taken.
880 const std::string methodName = expMethod->name;
173
1/2
✓ Branch 32 → 33 taken 880 times.
✗ Branch 32 → 191 not taken.
880 QualTypeList params = expMethod->getParamTypes();
174 880 QualType returnType = expMethod->returnType;
175
176 // Substantiate param and return types
177
1/2
✓ Branch 33 → 34 taken 880 times.
✗ Branch 33 → 189 not taken.
880 TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node);
178
3/4
✓ Branch 34 → 35 taken 880 times.
✗ Branch 34 → 189 not taken.
✓ Branch 35 → 36 taken 498 times.
✓ Branch 35 → 37 taken 382 times.
880 if (returnType.hasAnyGenericParts())
179
1/2
✓ Branch 36 → 37 taken 498 times.
✗ Branch 36 → 189 not taken.
498 TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node);
180
181 // Build args list
182 880 ArgList args;
183
1/2
✓ Branch 38 → 39 taken 880 times.
✗ Branch 38 → 187 not taken.
880 args.reserve(params.size());
184
2/2
✓ Branch 45 → 41 taken 14 times.
✓ Branch 45 → 46 taken 880 times.
894 for (const QualType &param : params)
185
1/2
✓ Branch 42 → 43 taken 14 times.
✗ Branch 42 → 152 not taken.
14 args.emplace_back(param, nullptr);
186
187 // Search for method that has the required signature
188
1/2
✓ Branch 47 → 48 taken 880 times.
✗ Branch 47 → 154 not taken.
880 Function *spiceFunction = FunctionManager::match(this, currentScope, methodName, structType, args, {}, true, node);
189
2/2
✓ Branch 49 → 50 taken 4 times.
✓ Branch 49 → 62 taken 876 times.
880 if (spiceFunction == nullptr) {
190
1/2
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 157 not taken.
4 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
191
4/8
✓ Branch 50 → 51 taken 4 times.
✗ Branch 50 → 169 not taken.
✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 165 not taken.
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 163 not taken.
✓ Branch 53 → 54 taken 4 times.
✗ Branch 53 → 161 not taken.
8 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
192
1/2
✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 159 not taken.
4 "'. The signature does not match.");
193 4 continue;
194 }
195
196 // Check return type
197
5/6
✓ Branch 62 → 63 taken 876 times.
✗ Branch 62 → 187 not taken.
✓ Branch 63 → 64 taken 152 times.
✓ Branch 63 → 67 taken 724 times.
✓ Branch 68 → 69 taken 2 times.
✓ Branch 68 → 81 taken 874 times.
1028 if (spiceFunction->returnType != returnType &&
198
3/4
✓ Branch 64 → 65 taken 152 times.
✗ Branch 64 → 187 not taken.
✓ Branch 65 → 66 taken 2 times.
✓ Branch 65 → 67 taken 150 times.
152 !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) {
199
1/2
✓ Branch 74 → 75 taken 2 times.
✗ Branch 74 → 172 not taken.
2 softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED,
200
4/8
✓ Branch 69 → 70 taken 2 times.
✗ Branch 69 → 184 not taken.
✓ Branch 70 → 71 taken 2 times.
✗ Branch 70 → 180 not taken.
✓ Branch 71 → 72 taken 2 times.
✗ Branch 71 → 178 not taken.
✓ Branch 72 → 73 taken 2 times.
✗ Branch 72 → 176 not taken.
4 "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() +
201
1/2
✓ Branch 73 → 74 taken 2 times.
✗ Branch 73 → 174 not taken.
2 "'. The return type does not match.");
202 2 continue;
203 }
204 // Set to virtual, since it overrides the interface method
205 874 spiceFunction->isVirtual = true;
206 874 spiceFunction->vtableIndex = vtableIndex++;
207
6/6
✓ Branch 83 → 84 taken 874 times.
✓ Branch 83 → 85 taken 6 times.
✓ Branch 87 → 88 taken 874 times.
✓ Branch 87 → 89 taken 6 times.
✓ Branch 91 → 92 taken 874 times.
✓ Branch 91 → 94 taken 6 times.
892 }
208 }
209
210 // Generate default ctor body if required
211
2/4
✓ Branch 104 → 105 taken 1318 times.
✗ Branch 104 → 198 not taken.
✓ Branch 105 → 106 taken 1318 times.
✗ Branch 105 → 196 not taken.
3954 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true);
212
4/4
✓ Branch 109 → 110 taken 589 times.
✓ Branch 109 → 112 taken 729 times.
✓ Branch 110 → 111 taken 89 times.
✓ Branch 110 → 112 taken 500 times.
1318 if (ctorFunc != nullptr && ctorFunc->implicitDefault)
213
1/2
✓ Branch 111 → 112 taken 89 times.
✗ Branch 111 → 228 not taken.
89 createCtorBodyPreamble(ctorFunc->bodyScope);
214
215 // Generate default copy ctor body if required
216
2/4
✓ Branch 112 → 113 taken 1318 times.
✗ Branch 112 → 209 not taken.
✓ Branch 116 → 117 taken 1318 times.
✗ Branch 116 → 205 not taken.
3954 const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}};
217
2/4
✓ Branch 120 → 121 taken 1318 times.
✗ Branch 120 → 213 not taken.
✓ Branch 121 → 122 taken 1318 times.
✗ Branch 121 → 211 not taken.
1318 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true);
218
4/4
✓ Branch 124 → 125 taken 424 times.
✓ Branch 124 → 127 taken 894 times.
✓ Branch 125 → 126 taken 139 times.
✓ Branch 125 → 127 taken 285 times.
1318 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault)
219
1/2
✓ Branch 126 → 127 taken 139 times.
✗ Branch 126 → 226 not taken.
139 createCopyCtorBodyPreamble(copyCtorFunc->bodyScope);
220
221 // Generate default dtor body if required
222
2/4
✓ Branch 130 → 131 taken 1318 times.
✗ Branch 130 → 219 not taken.
✓ Branch 131 → 132 taken 1318 times.
✗ Branch 131 → 217 not taken.
3954 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true);
223
4/4
✓ Branch 135 → 136 taken 495 times.
✓ Branch 135 → 138 taken 823 times.
✓ Branch 136 → 137 taken 223 times.
✓ Branch 136 → 138 taken 272 times.
1318 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
224
1/2
✓ Branch 137 → 138 taken 223 times.
✗ Branch 137 → 226 not taken.
223 createDtorBodyPreamble(dtorFunc->bodyScope);
225
226 // Return to the root scope
227 1318 currentScope = rootScope;
228
2/4
✓ Branch 138 → 139 taken 1318 times.
✗ Branch 138 → 141 not taken.
✓ Branch 139 → 140 taken 1318 times.
✗ Branch 139 → 141 not taken.
1318 assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL);
229
230 1318 manIdx++; // Increase the manifestation index
231 1318 }
232 1115 manIdx = 0; // Reset the manifestation index
233
234
1/2
✓ Branch 147 → 148 taken 1115 times.
✗ Branch 147 → 230 not taken.
1115 return nullptr;
235 }
236
237 } // namespace spice::compiler
238