GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 95.0% 419 / 0 / 441
Functions: 100.0% 17 / 0 / 17
Branches: 56.9% 602 / 0 / 1058

src/typechecker/TypeCheckerTopLevelDefinitionsPrepare.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "TypeChecker.h"
4
5 #include <unordered_set>
6
7 #include <SourceFile.h>
8 #include <ast/Attributes.h>
9 #include <global/GlobalResourceManager.h>
10 #include <global/TypeRegistry.h>
11 #include <model/GenericType.h>
12 #include <model/Interface.h>
13 #include <symboltablebuilder/Scope.h>
14 #include <symboltablebuilder/SymbolTableBuilder.h>
15 #include <typechecker/FunctionManager.h>
16 #include <typechecker/InterfaceManager.h>
17 #include <typechecker/MacroDefs.h>
18 #include <typechecker/StructManager.h>
19
20 namespace spice::compiler {
21
22 567 std::any TypeChecker::visitMainFctDefPrepare(MainFctDefNode *node) {
23 // Mark unreachable statements
24 567 bool returnsOnAllControlPaths = true;
25
1/2
✓ Branch 2 → 3 taken 567 times.
✗ Branch 2 → 77 not taken.
567 node->returnsOnAllControlPaths(&returnsOnAllControlPaths, manIdx);
26
27 // Retrieve return type
28
1/2
✓ Branch 3 → 4 taken 567 times.
✗ Branch 3 → 77 not taken.
567 const QualType returnType(TY_INT);
29
30 // Change to function body scope
31 567 currentScope = node->bodyScope;
32
33 // Set type of 'result' variable to int
34
1/2
✓ Branch 6 → 7 taken 567 times.
✗ Branch 6 → 56 not taken.
1701 SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
35
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 567 times.
567 assert(resultEntry != nullptr);
36
1/2
✓ Branch 14 → 15 taken 567 times.
✗ Branch 14 → 77 not taken.
567 resultEntry->updateType(returnType, false);
37 567 resultEntry->used = true;
38
39 // Retrieve param types
40 567 QualTypeList paramTypes;
41
2/2
✓ Branch 15 → 16 taken 7 times.
✓ Branch 15 → 36 taken 560 times.
567 if (node->takesArgs) {
42
2/4
✓ Branch 16 → 17 taken 7 times.
✗ Branch 16 → 62 not taken.
✓ Branch 17 → 18 taken 7 times.
✗ Branch 17 → 60 not taken.
7 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
43
2/2
✓ Branch 33 → 21 taken 14 times.
✓ Branch 33 → 34 taken 7 times.
28 for (const auto &[name, qualType, isOptional] : namedParamList)
44
1/2
✓ Branch 23 → 24 taken 14 times.
✗ Branch 23 → 63 not taken.
14 paramTypes.push_back(qualType);
45 7 }
46
47 // Prepare type of function
48
2/4
✓ Branch 36 → 37 taken 567 times.
✗ Branch 36 → 67 not taken.
✓ Branch 37 → 38 taken 567 times.
✗ Branch 37 → 67 not taken.
567 const QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes);
49
50 // Update main function symbol type
51
1/2
✓ Branch 40 → 41 taken 567 times.
✗ Branch 40 → 70 not taken.
1701 SymbolTableEntry *functionEntry = rootScope->lookupStrict(MAIN_FUNCTION_NAME);
52
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 567 times.
567 assert(functionEntry != nullptr);
53
1/2
✓ Branch 48 → 49 taken 567 times.
✗ Branch 48 → 75 not taken.
567 functionEntry->updateType(functionType, false);
54 567 functionEntry->used = true;
55
56 // Leave main function body scope
57 567 currentScope = rootScope;
58
59
1/2
✓ Branch 49 → 50 taken 567 times.
✗ Branch 49 → 74 not taken.
1134 return nullptr;
60 567 }
61
62 23638 std::any TypeChecker::visitFctDefPrepare(FctDefNode *node) {
63 // Check if name is dtor
64
3/4
✓ Branch 2 → 3 taken 23638 times.
✗ Branch 2 → 424 not taken.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 13 taken 23637 times.
23638 if (node->name->name == DTOR_FUNCTION_NAME)
65
3/6
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 281 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 279 not taken.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 285 not taken.
4 SOFT_ERROR_BOOL(node, DTOR_MUST_BE_PROCEDURE, "Destructors are not allowed to be of type function")
66
67 // Check if all control paths in the function return
68 23637 bool doSetPredecessorsUnreachable = true;
69
3/4
✓ Branch 13 → 14 taken 23637 times.
✗ Branch 13 → 424 not taken.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 24 taken 23636 times.
23637 if (!node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable, manIdx))
70
3/6
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 288 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 286 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 292 not taken.
4 SOFT_ERROR_BOOL(node, MISSING_RETURN_STMT, "Not all control paths of this function have a return statement")
71
72 // Change to function scope
73 23636 currentScope = node->scope;
74
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 23636 times.
23636 assert(currentScope->type == ScopeType::FUNC_PROC_BODY);
75
76 // Retrieve function template types
77 23636 std::vector<GenericType> usedGenericTypes;
78
2/2
✓ Branch 26 → 27 taken 2588 times.
✓ Branch 26 → 65 taken 21048 times.
23636 if (node->hasTemplateTypes) {
79
2/2
✓ Branch 63 → 29 taken 3133 times.
✓ Branch 63 → 64 taken 2587 times.
8308 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
80 // Visit template type
81
2/4
✓ Branch 31 → 32 taken 3133 times.
✗ Branch 31 → 295 not taken.
✓ Branch 32 → 33 taken 3133 times.
✗ Branch 32 → 293 not taken.
3133 auto templateType = std::any_cast<QualType>(visit(dataType));
82
2/4
✓ Branch 34 → 35 taken 3133 times.
✗ Branch 34 → 305 not taken.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 3133 times.
3133 if (templateType.is(TY_UNRESOLVED))
83 continue;
84 // Check if it is a generic type
85
3/4
✓ Branch 37 → 38 taken 3133 times.
✗ Branch 37 → 305 not taken.
✓ Branch 38 → 39 taken 1 time.
✓ Branch 38 → 47 taken 3132 times.
3133 if (!templateType.is(TY_GENERIC))
86
2/4
✓ Branch 42 → 43 taken 1 time.
✗ Branch 42 → 299 not taken.
✓ Branch 43 → 44 taken 1 time.
✗ Branch 43 → 296 not taken.
3 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
87 // Convert generic symbol type to generic type
88
2/4
✓ Branch 47 → 48 taken 3132 times.
✗ Branch 47 → 305 not taken.
✓ Branch 48 → 49 taken 3132 times.
✗ Branch 48 → 305 not taken.
3132 GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType());
89
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 3132 times.
3132 assert(genericType != nullptr);
90
1/2
✓ Branch 51 → 52 taken 3132 times.
✗ Branch 51 → 305 not taken.
3132 usedGenericTypes.push_back(*genericType);
91 }
92 }
93
94 // Retrieve 'this' type
95
1/2
✓ Branch 65 → 66 taken 23635 times.
✗ Branch 65 → 422 not taken.
23635 QualType thisType(TY_DYN); // If the function is not a method, the default this type is TY_DYN
96
2/2
✓ Branch 66 → 67 taken 11607 times.
✓ Branch 66 → 105 taken 12028 times.
23635 if (node->isMethod) {
97 11607 Scope *structParentScope = node->structScope->parent;
98
1/2
✓ Branch 67 → 68 taken 11607 times.
✗ Branch 67 → 315 not taken.
11607 SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName);
99
1/2
✗ Branch 70 → 71 not taken.
✓ Branch 70 → 72 taken 11607 times.
11607 assert(structEntry != nullptr);
100 // Set struct to used
101 11607 structEntry->used = true;
102 // Get type and ptr type
103
1/2
✓ Branch 72 → 73 taken 11607 times.
✗ Branch 72 → 315 not taken.
11607 thisType = structEntry->getQualType();
104
1/2
✓ Branch 73 → 74 taken 11607 times.
✗ Branch 73 → 315 not taken.
11607 const QualType thisPtrType = thisType.toPtr(node);
105 // Collect template types of 'this' type
106
3/4
✓ Branch 74 → 75 taken 11607 times.
✗ Branch 74 → 308 not taken.
✓ Branch 92 → 77 taken 4129 times.
✓ Branch 92 → 93 taken 11607 times.
27343 for (const QualType &templateType : thisType.getTemplateTypes()) {
107 991 const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; };
108
3/4
✓ Branch 79 → 80 taken 4129 times.
✗ Branch 79 → 307 not taken.
✓ Branch 80 → 81 taken 4107 times.
✓ Branch 80 → 82 taken 22 times.
4129 if (std::ranges::none_of(usedGenericTypes, lambda))
109
1/2
✓ Branch 81 → 82 taken 4107 times.
✗ Branch 81 → 307 not taken.
4107 usedGenericTypes.emplace_back(templateType);
110 4129 usedGenericTypes.back().used = true;
111 }
112
113 // Set type of 'this' variable
114
1/2
✓ Branch 95 → 96 taken 11607 times.
✗ Branch 95 → 311 not taken.
34821 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
115
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 11607 times.
11607 assert(thisEntry != nullptr);
116
1/2
✓ Branch 103 → 104 taken 11607 times.
✗ Branch 103 → 315 not taken.
11607 thisEntry->updateType(thisPtrType, false);
117 }
118
119 // Visit parameters
120 23635 QualTypeList paramTypes;
121 23635 ParamList paramList;
122
2/2
✓ Branch 105 → 106 taken 17843 times.
✓ Branch 105 → 152 taken 5792 times.
23635 if (node->hasParams) {
123 17843 std::vector<const char *> paramNames;
124 // Visit param list to retrieve the param names
125
2/4
✓ Branch 106 → 107 taken 17843 times.
✗ Branch 106 → 318 not taken.
✓ Branch 107 → 108 taken 17843 times.
✗ Branch 107 → 316 not taken.
17843 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
126
2/2
✓ Branch 140 → 111 taken 26877 times.
✓ Branch 140 → 141 taken 17841 times.
62561 for (const auto &[name, qualType, isOptional] : namedParamList) {
127
1/2
✓ Branch 113 → 114 taken 26877 times.
✗ Branch 113 → 330 not taken.
26877 paramNames.push_back(name);
128
2/6
✓ Branch 114 → 115 taken 26877 times.
✗ Branch 114 → 330 not taken.
✗ Branch 115 → 116 not taken.
✓ Branch 115 → 119 taken 26877 times.
✗ Branch 116 → 117 not taken.
✗ Branch 116 → 319 not taken.
26877 HANDLE_UNRESOLVED_TYPE_PTR(qualType);
129
1/2
✓ Branch 119 → 120 taken 26877 times.
✗ Branch 119 → 330 not taken.
26877 paramTypes.push_back(qualType);
130
1/2
✓ Branch 120 → 121 taken 26877 times.
✗ Branch 120 → 320 not taken.
26877 paramList.push_back({qualType, isOptional});
131 // Check if the type is present in the template for generic types
132
3/4
✓ Branch 121 → 122 taken 26877 times.
✗ Branch 121 → 330 not taken.
✓ Branch 122 → 123 taken 2 times.
✓ Branch 122 → 131 taken 26875 times.
26877 if (!qualType.isCoveredByGenericTypeList(usedGenericTypes))
133 throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE,
134
2/4
✓ Branch 126 → 127 taken 2 times.
✗ Branch 126 → 324 not taken.
✓ Branch 127 → 128 taken 2 times.
✗ Branch 127 → 321 not taken.
6 "Generic param type not included in the template type list of the function");
135 }
136
2/4
✓ Branch 143 → 144 taken 17841 times.
✗ Branch 143 → 145 not taken.
✓ Branch 148 → 149 taken 17841 times.
✗ Branch 148 → 151 not taken.
35686 }
137
138 // Retrieve return type
139
2/4
✓ Branch 152 → 153 taken 23633 times.
✗ Branch 152 → 338 not taken.
✓ Branch 153 → 154 taken 23633 times.
✗ Branch 153 → 336 not taken.
23633 auto returnType = std::any_cast<QualType>(visit(node->returnType));
140
2/6
✓ Branch 155 → 156 taken 23633 times.
✗ Branch 155 → 418 not taken.
✗ Branch 156 → 157 not taken.
✓ Branch 156 → 160 taken 23633 times.
✗ Branch 157 → 158 not taken.
✗ Branch 157 → 339 not taken.
23633 HANDLE_UNRESOLVED_TYPE_PTR(returnType)
141
3/4
✓ Branch 160 → 161 taken 23633 times.
✗ Branch 160 → 418 not taken.
✓ Branch 161 → 162 taken 1 time.
✓ Branch 161 → 171 taken 23632 times.
23633 if (returnType.is(TY_DYN))
142
3/6
✓ Branch 164 → 165 taken 1 time.
✗ Branch 164 → 342 not taken.
✓ Branch 165 → 166 taken 1 time.
✗ Branch 165 → 340 not taken.
✓ Branch 168 → 169 taken 1 time.
✗ Branch 168 → 346 not taken.
4 SOFT_ERROR_BOOL(node, UNEXPECTED_DYN_TYPE, "Dyn return types are not allowed")
143
3/4
✓ Branch 171 → 172 taken 23632 times.
✗ Branch 171 → 418 not taken.
✓ Branch 172 → 173 taken 1 time.
✓ Branch 172 → 182 taken 23631 times.
23632 if (!returnType.isCoveredByGenericTypeList(usedGenericTypes))
144
3/6
✓ Branch 175 → 176 taken 1 time.
✗ Branch 175 → 349 not taken.
✓ Branch 176 → 177 taken 1 time.
✗ Branch 176 → 347 not taken.
✓ Branch 179 → 180 taken 1 time.
✗ Branch 179 → 353 not taken.
4 SOFT_ERROR_BOOL(node->returnType, GENERIC_TYPE_NOT_IN_TEMPLATE,
145 "Generic return type not included in the template type list of the function")
146
147 // Leave function body scope
148 23631 currentScope = node->scope->parent;
149
3/4
✓ Branch 182 → 183 taken 11607 times.
✓ Branch 182 → 185 taken 12024 times.
✗ Branch 183 → 184 not taken.
✓ Branch 183 → 185 taken 11607 times.
23631 assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT);
150
151 // Prepare type of function
152
2/4
✓ Branch 185 → 186 taken 23631 times.
✗ Branch 185 → 354 not taken.
✓ Branch 186 → 187 taken 23631 times.
✗ Branch 186 → 354 not taken.
23631 QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes);
153 23631 functionType.setQualifiers(node->qualifiers);
154
155 // Update type of function entry
156
1/2
✓ Branch 188 → 189 taken 23631 times.
✗ Branch 188 → 357 not taken.
47262 SymbolTableEntry *functionEntry = currentScope->lookupStrict(node->getSymbolTableEntryName());
157
1/2
✗ Branch 193 → 194 not taken.
✓ Branch 193 → 195 taken 23631 times.
23631 assert(functionEntry != nullptr);
158
1/2
✓ Branch 195 → 196 taken 23631 times.
✗ Branch 195 → 418 not taken.
23631 functionEntry->updateType(functionType, false);
159
160 // Build function object
161
4/8
✓ Branch 196 → 197 taken 23631 times.
✗ Branch 196 → 366 not taken.
✓ Branch 197 → 198 taken 23631 times.
✗ Branch 197 → 363 not taken.
✓ Branch 198 → 199 taken 23631 times.
✗ Branch 198 → 360 not taken.
✓ Branch 199 → 200 taken 23631 times.
✗ Branch 199 → 358 not taken.
23631 Function spiceFunc(node->name->name, functionEntry, thisType, returnType, paramList, usedGenericTypes, node);
162 23631 spiceFunc.bodyScope = node->scope;
163
2/2
✓ Branch 203 → 204 taken 23630 times.
✓ Branch 203 → 416 taken 1 time.
23631 FunctionManager::insert(currentScope, spiceFunc, &node->manifestations);
164
165 // Check function attributes
166
2/2
✓ Branch 204 → 205 taken 787 times.
✓ Branch 204 → 252 taken 22843 times.
23630 if (node->attrs) {
167 787 const AttrLstNode *attrLst = node->attrs->attrLst;
168 787 Function *firstManifestation = node->manifestations.front();
169
4/6
✓ Branch 208 → 209 taken 787 times.
✗ Branch 208 → 369 not taken.
✓ Branch 209 → 210 taken 787 times.
✗ Branch 209 → 367 not taken.
✓ Branch 212 → 213 taken 1 time.
✓ Branch 212 → 214 taken 786 times.
2361 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
170 1 firstManifestation->mangleFunctionName = value->boolValue;
171
3/6
✓ Branch 216 → 217 taken 787 times.
✗ Branch 216 → 375 not taken.
✓ Branch 217 → 218 taken 787 times.
✗ Branch 217 → 373 not taken.
✗ Branch 220 → 221 not taken.
✓ Branch 220 → 223 taken 787 times.
2361 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
172 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
173 firstManifestation->predefinedMangledName = stringValue;
174 }
175
6/8
✓ Branch 225 → 226 taken 787 times.
✗ Branch 225 → 381 not taken.
✓ Branch 226 → 227 taken 787 times.
✗ Branch 226 → 379 not taken.
✓ Branch 229 → 230 taken 111 times.
✓ Branch 229 → 252 taken 676 times.
✓ Branch 230 → 231 taken 110 times.
✓ Branch 230 → 252 taken 1 time.
2361 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_TEST); value && value->boolValue) {
176 // Make sure that the function has the correct signature
177
2/2
✓ Branch 231 → 232 taken 1 time.
✓ Branch 231 → 240 taken 109 times.
110 if (node->hasParams)
178
2/4
✓ Branch 235 → 236 taken 1 time.
✗ Branch 235 → 388 not taken.
✓ Branch 236 → 237 taken 1 time.
✗ Branch 236 → 385 not taken.
3 throw SemanticError(node->paramLst, TEST_FUNCTION_WITH_PARAMS, "Test function may not have parameters");
179
3/4
✓ Branch 240 → 241 taken 109 times.
✗ Branch 240 → 416 not taken.
✓ Branch 241 → 242 taken 1 time.
✓ Branch 241 → 250 taken 108 times.
109 if (!returnType.is(TY_BOOL))
180
2/4
✓ Branch 245 → 246 taken 1 time.
✗ Branch 245 → 397 not taken.
✓ Branch 246 → 247 taken 1 time.
✗ Branch 246 → 394 not taken.
3 throw SemanticError(node->returnType, TEST_FUNCTION_WRONG_RETURN_TYPE, "Test function must return a bool");
181 // Add to test function list
182 108 firstManifestation->entry->used = true; // Avoid printing unused warnings
183 108 firstManifestation->used = true; // Always keep test functions, because they are called implicitly by the test main
184
1/2
✓ Branch 251 → 252 taken 108 times.
✗ Branch 251 → 416 not taken.
108 sourceFile->testFunctions.push_back(node->manifestations.front());
185 }
186 }
187
188 // Duplicate / rename the original child scope to reflect the substantiated versions of the function
189
2/2
✓ Branch 262 → 253 taken 1916 times.
✓ Branch 262 → 263 taken 23628 times.
25544 for (size_t i = 1; i < node->manifestations.size(); i++) {
190
4/8
✓ Branch 253 → 254 taken 1916 times.
✗ Branch 253 → 408 not taken.
✓ Branch 254 → 255 taken 1916 times.
✗ Branch 254 → 408 not taken.
✓ Branch 255 → 256 taken 1916 times.
✗ Branch 255 → 405 not taken.
✓ Branch 256 → 257 taken 1916 times.
✗ Branch 256 → 403 not taken.
1916 Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getScopeName());
191
1/2
✓ Branch 259 → 260 taken 1916 times.
✗ Branch 259 → 416 not taken.
1916 node->manifestations.at(i)->bodyScope = scope;
192 }
193
3/6
✓ Branch 264 → 265 taken 23628 times.
✗ Branch 264 → 414 not taken.
✓ Branch 265 → 266 taken 23628 times.
✗ Branch 265 → 411 not taken.
✓ Branch 266 → 267 taken 23628 times.
✗ Branch 266 → 409 not taken.
23628 currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getScopeName());
194
195 // Change to the root scope
196 23628 currentScope = rootScope;
197
1/2
✗ Branch 269 → 270 not taken.
✓ Branch 269 → 271 taken 23628 times.
23628 assert(currentScope->type == ScopeType::GLOBAL);
198
199
1/2
✓ Branch 271 → 272 taken 23628 times.
✗ Branch 271 → 415 not taken.
23628 return nullptr;
200 23649 }
201
202 12908 std::any TypeChecker::visitProcDefPrepare(ProcDefNode *node) {
203 // Mark unreachable statements
204 12908 bool doSetPredecessorsUnreachable = true;
205
1/2
✓ Branch 2 → 3 taken 12908 times.
✗ Branch 2 → 316 not taken.
12908 node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable, manIdx);
206
207 // Check if dtor and has params
208
7/8
✓ Branch 3 → 4 taken 9935 times.
✓ Branch 3 → 7 taken 2973 times.
✓ Branch 4 → 5 taken 9935 times.
✗ Branch 4 → 316 not taken.
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 9934 times.
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 17 taken 12907 times.
12908 if (node->hasParams && node->name->name == DTOR_FUNCTION_NAME)
209
2/4
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 219 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 216 not taken.
3 throw SemanticError(node, DTOR_WITH_PARAMS, "It is not allowed to specify parameters for destructors");
210
211 // Change to procedure scope
212 12907 currentScope = node->scope;
213
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 12907 times.
12907 assert(currentScope->type == ScopeType::FUNC_PROC_BODY);
214
215 // Retrieve procedure template types
216 12907 std::vector<GenericType> usedGenericTypes;
217
2/2
✓ Branch 19 → 20 taken 2547 times.
✓ Branch 19 → 58 taken 10360 times.
12907 if (node->hasTemplateTypes) {
218
2/2
✓ Branch 56 → 22 taken 2872 times.
✓ Branch 56 → 57 taken 2546 times.
7965 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
219 // Visit template type
220
2/4
✓ Branch 24 → 25 taken 2872 times.
✗ Branch 24 → 227 not taken.
✓ Branch 25 → 26 taken 2872 times.
✗ Branch 25 → 225 not taken.
2872 auto templateType = std::any_cast<QualType>(visit(dataType));
221
2/4
✓ Branch 27 → 28 taken 2872 times.
✗ Branch 27 → 237 not taken.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 2872 times.
2872 if (templateType.is(TY_UNRESOLVED))
222 continue;
223 // Check if it is a generic type
224
3/4
✓ Branch 30 → 31 taken 2872 times.
✗ Branch 30 → 237 not taken.
✓ Branch 31 → 32 taken 1 time.
✓ Branch 31 → 40 taken 2871 times.
2872 if (!templateType.is(TY_GENERIC))
225
2/4
✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 231 not taken.
✓ Branch 36 → 37 taken 1 time.
✗ Branch 36 → 228 not taken.
3 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
226 // Convert generic symbol type to generic type
227
2/4
✓ Branch 40 → 41 taken 2871 times.
✗ Branch 40 → 237 not taken.
✓ Branch 41 → 42 taken 2871 times.
✗ Branch 41 → 237 not taken.
2871 const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType());
228
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 2871 times.
2871 assert(genericType != nullptr);
229
1/2
✓ Branch 44 → 45 taken 2871 times.
✗ Branch 44 → 237 not taken.
2871 usedGenericTypes.push_back(*genericType);
230 }
231 }
232
233 // Retrieve 'this' type
234
1/2
✓ Branch 58 → 59 taken 12906 times.
✗ Branch 58 → 314 not taken.
12906 QualType thisType(TY_DYN); // If the procedure is not a method, the default this type is TY_DYN
235
2/2
✓ Branch 59 → 60 taken 10922 times.
✓ Branch 59 → 98 taken 1984 times.
12906 if (node->isMethod) {
236 10922 Scope *structParentScope = node->structScope->parent;
237
1/2
✓ Branch 60 → 61 taken 10922 times.
✗ Branch 60 → 247 not taken.
10922 SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName);
238
1/2
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 10922 times.
10922 assert(structEntry != nullptr);
239 // Set struct to used
240 10922 structEntry->used = true;
241 // Get type and ptr type
242
1/2
✓ Branch 65 → 66 taken 10922 times.
✗ Branch 65 → 247 not taken.
10922 thisType = structEntry->getQualType();
243
1/2
✓ Branch 66 → 67 taken 10922 times.
✗ Branch 66 → 247 not taken.
10922 const QualType thisPtrType = thisType.toPtr(node);
244 // Collect template types of 'this' type
245
3/4
✓ Branch 67 → 68 taken 10922 times.
✗ Branch 67 → 240 not taken.
✓ Branch 85 → 70 taken 4022 times.
✓ Branch 85 → 86 taken 10922 times.
25866 for (const QualType &templateType : thisType.getTemplateTypes()) {
246 1296 const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; };
247
3/4
✓ Branch 72 → 73 taken 4022 times.
✗ Branch 72 → 239 not taken.
✓ Branch 73 → 74 taken 3733 times.
✓ Branch 73 → 75 taken 289 times.
4022 if (std::ranges::none_of(usedGenericTypes, lambda))
248
1/2
✓ Branch 74 → 75 taken 3733 times.
✗ Branch 74 → 239 not taken.
3733 usedGenericTypes.emplace_back(templateType);
249 4022 usedGenericTypes.back().used = true;
250 }
251
252 // Set type of 'this' variable
253
1/2
✓ Branch 88 → 89 taken 10922 times.
✗ Branch 88 → 243 not taken.
32766 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
254
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 96 taken 10922 times.
10922 assert(thisEntry != nullptr);
255
1/2
✓ Branch 96 → 97 taken 10922 times.
✗ Branch 96 → 247 not taken.
10922 thisEntry->updateType(thisPtrType, false);
256 }
257
258 // Visit parameters
259 12906 QualTypeList paramTypes;
260 12906 ParamList paramList;
261
2/2
✓ Branch 98 → 99 taken 9934 times.
✓ Branch 98 → 145 taken 2972 times.
12906 if (node->hasParams) {
262 9934 std::vector<const char *> paramNames;
263 // Visit param list to retrieve the param names
264
2/4
✓ Branch 99 → 100 taken 9934 times.
✗ Branch 99 → 250 not taken.
✓ Branch 100 → 101 taken 9934 times.
✗ Branch 100 → 248 not taken.
9934 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
265
2/2
✓ Branch 133 → 104 taken 13571 times.
✓ Branch 133 → 134 taken 9932 times.
33437 for (const auto &[name, qualType, isOptional] : namedParamList) {
266
1/2
✓ Branch 106 → 107 taken 13571 times.
✗ Branch 106 → 262 not taken.
13571 paramNames.push_back(name);
267
1/2
✓ Branch 107 → 108 taken 13571 times.
✗ Branch 107 → 262 not taken.
13571 paramTypes.push_back(qualType);
268
4/6
✓ Branch 108 → 109 taken 13571 times.
✗ Branch 108 → 262 not taken.
✓ Branch 109 → 110 taken 1 time.
✓ Branch 109 → 113 taken 13570 times.
✓ Branch 110 → 111 taken 1 time.
✗ Branch 110 → 251 not taken.
13572 HANDLE_UNRESOLVED_TYPE_PTR(qualType);
269
1/2
✓ Branch 113 → 114 taken 13570 times.
✗ Branch 113 → 252 not taken.
13570 paramList.push_back({qualType, isOptional});
270 // Check if the type is present in the template for generic types
271
3/4
✓ Branch 114 → 115 taken 13570 times.
✗ Branch 114 → 262 not taken.
✓ Branch 115 → 116 taken 1 time.
✓ Branch 115 → 124 taken 13569 times.
13570 if (!qualType.isCoveredByGenericTypeList(usedGenericTypes))
272 throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE,
273
2/4
✓ Branch 119 → 120 taken 1 time.
✗ Branch 119 → 256 not taken.
✓ Branch 120 → 121 taken 1 time.
✗ Branch 120 → 253 not taken.
3 "Generic param type not included in the template type list of the procedure");
274 }
275
4/4
✓ Branch 136 → 137 taken 9932 times.
✓ Branch 136 → 138 taken 1 time.
✓ Branch 141 → 142 taken 9932 times.
✓ Branch 141 → 144 taken 1 time.
19868 }
276
277 // Leave procedure body scope
278 12904 currentScope = node->scope->parent;
279
3/4
✓ Branch 145 → 146 taken 10922 times.
✓ Branch 145 → 148 taken 1982 times.
✗ Branch 146 → 147 not taken.
✓ Branch 146 → 148 taken 10922 times.
12904 assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT);
280
281 // Prepare type of procedure
282
3/6
✓ Branch 148 → 149 taken 12904 times.
✗ Branch 148 → 269 not taken.
✓ Branch 149 → 150 taken 12904 times.
✗ Branch 149 → 268 not taken.
✓ Branch 150 → 151 taken 12904 times.
✗ Branch 150 → 268 not taken.
12904 QualType procedureType = QualType(TY_PROCEDURE).getWithFunctionParamAndReturnTypes(QualType(TY_DYN), paramTypes);
283 12904 procedureType.setQualifiers(node->qualifiers);
284
285 // Update type of procedure entry
286
1/2
✓ Branch 152 → 153 taken 12904 times.
✗ Branch 152 → 272 not taken.
25808 SymbolTableEntry *procedureEntry = currentScope->lookupStrict(node->getSymbolTableEntryName());
287
1/2
✗ Branch 157 → 158 not taken.
✓ Branch 157 → 159 taken 12904 times.
12904 assert(procedureEntry != nullptr);
288
1/2
✓ Branch 159 → 160 taken 12904 times.
✗ Branch 159 → 310 not taken.
12904 procedureEntry->updateType(procedureType, false);
289
290 // Build procedure object
291
5/10
✓ Branch 160 → 161 taken 12904 times.
✗ Branch 160 → 282 not taken.
✓ Branch 161 → 162 taken 12904 times.
✗ Branch 161 → 279 not taken.
✓ Branch 162 → 163 taken 12904 times.
✗ Branch 162 → 276 not taken.
✓ Branch 163 → 164 taken 12904 times.
✗ Branch 163 → 275 not taken.
✓ Branch 164 → 165 taken 12904 times.
✗ Branch 164 → 273 not taken.
12904 Function spiceProc(node->name->name, procedureEntry, thisType, QualType(TY_DYN), paramList, usedGenericTypes, node);
292 12904 spiceProc.bodyScope = node->scope;
293
2/2
✓ Branch 168 → 169 taken 12903 times.
✓ Branch 168 → 308 taken 1 time.
12904 FunctionManager::insert(currentScope, spiceProc, &node->manifestations);
294
295 // Check procedure attributes
296
1/2
✗ Branch 169 → 170 not taken.
✓ Branch 169 → 189 taken 12903 times.
12903 if (node->attrs) {
297 const AttrLstNode *attrLst = node->attrs->attrLst;
298 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
299 node->manifestations.front()->mangleFunctionName = value->boolValue;
300 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
301 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
302 node->manifestations.front()->predefinedMangledName = stringValue;
303 }
304 }
305
306 // Duplicate / rename the original child scope to reflect the substantiated versions of the procedure
307
2/2
✓ Branch 199 → 190 taken 618 times.
✓ Branch 199 → 200 taken 12903 times.
13521 for (size_t i = 1; i < node->manifestations.size(); i++) {
308
4/8
✓ Branch 190 → 191 taken 618 times.
✗ Branch 190 → 300 not taken.
✓ Branch 191 → 192 taken 618 times.
✗ Branch 191 → 300 not taken.
✓ Branch 192 → 193 taken 618 times.
✗ Branch 192 → 297 not taken.
✓ Branch 193 → 194 taken 618 times.
✗ Branch 193 → 295 not taken.
618 Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getScopeName());
309
1/2
✓ Branch 196 → 197 taken 618 times.
✗ Branch 196 → 308 not taken.
618 node->manifestations.at(i)->bodyScope = scope;
310 }
311
3/6
✓ Branch 201 → 202 taken 12903 times.
✗ Branch 201 → 306 not taken.
✓ Branch 202 → 203 taken 12903 times.
✗ Branch 202 → 303 not taken.
✓ Branch 203 → 204 taken 12903 times.
✗ Branch 203 → 301 not taken.
12903 currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getScopeName());
312
313 // Change to the root scope
314 12903 currentScope = rootScope;
315
1/2
✗ Branch 206 → 207 not taken.
✓ Branch 206 → 208 taken 12903 times.
12903 assert(currentScope->type == ScopeType::GLOBAL);
316
317
1/2
✓ Branch 208 → 209 taken 12903 times.
✗ Branch 208 → 307 not taken.
12903 return nullptr;
318 12912 }
319
320 /**
321 * Determine whether a field type (transitively) contains a struct with the given origin body scope by value, which would
322 * give the origin struct an infinite size. Only by-value struct fields are followed; pointers and references break the
323 * cycle and have a fixed size. Struct types that are not manifested yet (e.g. still being prepared as part of a circular
324 * import) end a branch - such a cycle is still detected once the last struct in it is prepared, when all the others are
325 * manifested.
326 *
327 * @param fieldType Type of the field to inspect
328 * @param originScope Body scope of the struct whose infinite size we are checking for
329 * @param node Accessing AST node (for struct lookup diagnostics)
330 * @param visited Set of already-visited struct scopes, to keep the walk finite
331 * @return true if the origin struct is reachable by value, i.e. it has infinite size
332 */
333 21674 static bool fieldContainsStructByValue(const QualType &fieldType, const Scope *originScope, const ASTNode *node,
334 std::unordered_set<const Scope *> &visited) {
335
3/4
✓ Branch 2 → 3 taken 21674 times.
✗ Branch 2 → 22 not taken.
✓ Branch 3 → 4 taken 15864 times.
✓ Branch 3 → 5 taken 5810 times.
21674 if (!fieldType.is(TY_STRUCT))
336 15864 return false;
337
1/2
✓ Branch 5 → 6 taken 5810 times.
✗ Branch 5 → 22 not taken.
5810 const Scope *fieldScope = fieldType.getBodyScope();
338
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 5809 times.
5810 if (fieldScope == originScope)
339 1 return true;
340
6/8
✓ Branch 8 → 9 taken 5809 times.
✗ Branch 8 → 11 not taken.
✓ Branch 9 → 10 taken 5809 times.
✗ Branch 9 → 22 not taken.
✓ Branch 10 → 11 taken 105 times.
✓ Branch 10 → 12 taken 5704 times.
✓ Branch 13 → 14 taken 105 times.
✓ Branch 13 → 15 taken 5704 times.
5809 if (fieldScope == nullptr || !visited.insert(fieldScope).second)
341 105 return false;
342
1/2
✓ Branch 15 → 16 taken 5704 times.
✗ Branch 15 → 22 not taken.
5704 const Struct *spiceStruct = fieldType.getStruct(node);
343
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 5703 times.
5704 if (spiceStruct == nullptr)
344 1 return false; // Not manifested yet; the cycle is caught once the last struct in it is prepared
345
1/2
✓ Branch 18 → 19 taken 5703 times.
✗ Branch 18 → 22 not taken.
5703 return std::ranges::any_of(spiceStruct->fieldTypes, [&](const QualType &memberType) {
346 15420 return fieldContainsStructByValue(memberType, originScope, node, visited);
347 5703 });
348 }
349
350 3277 std::any TypeChecker::visitStructDefPrepare(StructDefNode *node) {
351 3277 QualTypeList usedTemplateTypes;
352 3277 std::vector<GenericType> templateTypesGeneric;
353
354 // Retrieve struct template types
355
2/2
✓ Branch 2 → 3 taken 736 times.
✓ Branch 2 → 46 taken 2541 times.
3277 if (node->hasTemplateTypes) {
356
1/2
✓ Branch 4 → 5 taken 736 times.
✗ Branch 4 → 318 not taken.
736 usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size());
357
1/2
✓ Branch 6 → 7 taken 736 times.
✗ Branch 6 → 318 not taken.
736 templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size());
358
2/2
✓ Branch 44 → 9 taken 1030 times.
✓ Branch 44 → 45 taken 736 times.
2502 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
359 // Visit template type
360
2/4
✓ Branch 11 → 12 taken 1030 times.
✗ Branch 11 → 220 not taken.
✓ Branch 12 → 13 taken 1030 times.
✗ Branch 12 → 218 not taken.
1030 auto templateType = std::any_cast<QualType>(visit(dataType));
361
2/4
✓ Branch 14 → 15 taken 1030 times.
✗ Branch 14 → 230 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1030 times.
1030 if (templateType.is(TY_UNRESOLVED))
362 continue;
363 // Check if it is a generic type
364
2/4
✓ Branch 17 → 18 taken 1030 times.
✗ Branch 17 → 230 not taken.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 27 taken 1030 times.
1030 if (!templateType.is(TY_GENERIC))
365 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
366 // Convert generic symbol type to generic type
367
2/4
✓ Branch 27 → 28 taken 1030 times.
✗ Branch 27 → 230 not taken.
✓ Branch 28 → 29 taken 1030 times.
✗ Branch 28 → 230 not taken.
1030 GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType());
368
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 1030 times.
1030 assert(genericType != nullptr);
369
1/2
✓ Branch 31 → 32 taken 1030 times.
✗ Branch 31 → 230 not taken.
1030 usedTemplateTypes.push_back(*genericType);
370
1/2
✓ Branch 32 → 33 taken 1030 times.
✗ Branch 32 → 230 not taken.
1030 templateTypesGeneric.push_back(*genericType);
371 }
372 }
373
374 // Retrieve implemented interfaces
375 3277 QualTypeList interfaceTypes;
376
2/2
✓ Branch 46 → 47 taken 378 times.
✓ Branch 46 → 107 taken 2899 times.
3277 if (node->hasInterfaces) {
377
1/2
✓ Branch 48 → 49 taken 378 times.
✗ Branch 48 → 316 not taken.
378 interfaceTypes.reserve(node->interfaceTypeLst->dataTypes.size());
378
2/2
✓ Branch 105 → 51 taken 378 times.
✓ Branch 105 → 106 taken 377 times.
1133 for (DataTypeNode *interfaceNode : node->interfaceTypeLst->dataTypes) {
379 // Visit interface type
380
2/4
✓ Branch 53 → 54 taken 378 times.
✗ Branch 53 → 234 not taken.
✓ Branch 54 → 55 taken 378 times.
✗ Branch 54 → 232 not taken.
378 auto interfaceType = std::any_cast<QualType>(visit(interfaceNode));
381
2/4
✓ Branch 56 → 57 taken 378 times.
✗ Branch 56 → 256 not taken.
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 378 times.
378 if (interfaceType.is(TY_UNRESOLVED))
382 continue;
383 // Check if it is an interface type
384
2/4
✓ Branch 59 → 60 taken 378 times.
✗ Branch 59 → 256 not taken.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 68 taken 378 times.
378 if (!interfaceType.is(TY_INTERFACE))
385 throw SemanticError(interfaceNode, EXPECTED_INTERFACE_TYPE,
386 "Expected interface type, got " + interfaceType.getName(false));
387 // Check for visibility
388
9/12
✓ Branch 68 → 69 taken 378 times.
✗ Branch 68 → 256 not taken.
✓ Branch 69 → 70 taken 378 times.
✗ Branch 69 → 256 not taken.
✓ Branch 70 → 71 taken 339 times.
✓ Branch 70 → 74 taken 39 times.
✓ Branch 71 → 72 taken 339 times.
✗ Branch 71 → 256 not taken.
✓ Branch 72 → 73 taken 1 time.
✓ Branch 72 → 74 taken 338 times.
✓ Branch 75 → 76 taken 1 time.
✓ Branch 75 → 84 taken 377 times.
378 if (interfaceType.getBodyScope()->isImportedBy(rootScope) && !interfaceType.isPublic())
389 throw SemanticError(node, INSUFFICIENT_VISIBILITY,
390
4/8
✓ Branch 77 → 78 taken 1 time.
✗ Branch 77 → 249 not taken.
✓ Branch 78 → 79 taken 1 time.
✗ Branch 78 → 249 not taken.
✓ Branch 79 → 80 taken 1 time.
✗ Branch 79 → 247 not taken.
✓ Branch 80 → 81 taken 1 time.
✗ Branch 80 → 244 not taken.
1 "Cannot access interface '" + interfaceType.getSubType() + "' due to its private visibility");
391 // Add to interface types
392
1/2
✓ Branch 84 → 85 taken 377 times.
✗ Branch 84 → 256 not taken.
377 interfaceTypes.push_back(interfaceType);
393 // Update the type of the entry for that interface field
394 377 const std::string &interfaceName = interfaceNode->baseDataType->customDataType->typeNameFragments.back();
395
1/2
✓ Branch 86 → 87 taken 377 times.
✗ Branch 86 → 255 not taken.
754 SymbolTableEntry *interfaceEntry = node->structScope->lookupStrict("this." + interfaceName);
396
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 377 times.
377 assert(interfaceEntry != nullptr);
397
1/2
✓ Branch 93 → 94 taken 377 times.
✗ Branch 93 → 256 not taken.
377 interfaceEntry->updateType(interfaceType, false);
398 }
399 }
400
401 // Update type of struct entry
402
1/2
✗ Branch 107 → 108 not taken.
✓ Branch 107 → 109 taken 3276 times.
3276 assert(node->entry != nullptr);
403 3276 const TypeChainElementData data = {.bodyScope = node->structScope};
404
1/2
✓ Branch 109 → 110 taken 3276 times.
✗ Branch 109 → 316 not taken.
3276 const Type *type = TypeRegistry::getOrInsert(TY_STRUCT, node->structName, node->typeId, data, usedTemplateTypes);
405 // If the entry was implicitly forward-declared because it is referenced across a circular import
406 // (assignDeferredOpaqueType), the existing type was already set to the opaque struct type. Overwriting it with the
407 // identical interned type is expected here.
408
2/4
✓ Branch 110 → 111 taken 3276 times.
✗ Branch 110 → 316 not taken.
✓ Branch 111 → 112 taken 3276 times.
✗ Branch 111 → 316 not taken.
3276 const bool overwrite = node->entry->getQualType().is(TY_STRUCT);
409
2/4
✓ Branch 112 → 113 taken 3276 times.
✗ Branch 112 → 258 not taken.
✓ Branch 113 → 114 taken 3276 times.
✗ Branch 113 → 258 not taken.
3276 node->entry->updateType(QualType(type, node->qualifiers), overwrite);
410
411 // Change to struct scope
412 3276 currentScope = node->structScope;
413
1/2
✗ Branch 114 → 115 not taken.
✓ Branch 114 → 116 taken 3276 times.
3276 assert(currentScope->type == ScopeType::STRUCT);
414
415 // Retrieve field types
416 3276 QualTypeList fieldTypes;
417
1/2
✓ Branch 117 → 118 taken 3276 times.
✗ Branch 117 → 314 not taken.
3276 fieldTypes.reserve(node->fields.size());
418 // Shared across all fields: the origin (node->structScope) is constant, so a struct already proven not to reach it
419 // via one field need not be re-walked for another.
420 3276 std::unordered_set<const Scope *> visitedScopes;
421
2/2
✓ Branch 165 → 121 taken 6256 times.
✓ Branch 165 → 166 taken 3272 times.
12804 for (FieldNode *field : node->fields) {
422 // Visit field type
423
2/4
✓ Branch 123 → 124 taken 6256 times.
✗ Branch 123 → 261 not taken.
✓ Branch 124 → 125 taken 6256 times.
✗ Branch 124 → 259 not taken.
6256 auto fieldType = std::any_cast<QualType>(visit(field));
424
3/4
✓ Branch 126 → 127 taken 6256 times.
✗ Branch 126 → 280 not taken.
✓ Branch 127 → 128 taken 2 times.
✓ Branch 127 → 129 taken 6254 times.
6256 if (fieldType.is(TY_UNRESOLVED))
425
1/2
✗ Branch 128 → 129 not taken.
✓ Branch 128 → 280 taken 2 times.
2 sourceFile->checkForSoftErrors(); // We get into trouble if we continue without the field type -> abort
426
427 // Check for struct with infinite size. This happens if a struct (transitively, through by-value struct fields)
428 // contains itself - either directly (struct A has a field of type A) or through a cycle of structs in mutually
429 // importing files (A has a field of type B and B has a field of type A).
430
3/4
✓ Branch 129 → 130 taken 6254 times.
✗ Branch 129 → 280 not taken.
✓ Branch 130 → 131 taken 1 time.
✓ Branch 130 → 139 taken 6253 times.
6254 if (fieldContainsStructByValue(fieldType, node->structScope, field, visitedScopes))
431
2/4
✓ Branch 134 → 135 taken 1 time.
✗ Branch 134 → 265 not taken.
✓ Branch 135 → 136 taken 1 time.
✗ Branch 135 → 262 not taken.
3 throw SemanticError(field, STRUCT_INFINITE_SIZE, "Struct with infinite size detected");
432
433 // Add to field types
434
1/2
✓ Branch 139 → 140 taken 6253 times.
✗ Branch 139 → 280 not taken.
6253 fieldTypes.push_back(fieldType);
435
436 // Update type of field entry
437
1/2
✓ Branch 140 → 141 taken 6253 times.
✗ Branch 140 → 280 not taken.
6253 SymbolTableEntry *fieldEntry = currentScope->lookupStrict(field->fieldName);
438
1/2
✗ Branch 143 → 144 not taken.
✓ Branch 143 → 145 taken 6253 times.
6253 assert(fieldEntry != nullptr);
439
1/2
✓ Branch 145 → 146 taken 6253 times.
✗ Branch 145 → 280 not taken.
6253 fieldEntry->updateType(fieldType, false);
440
441 // Check if the template type list contains this type
442
3/4
✓ Branch 146 → 147 taken 6253 times.
✗ Branch 146 → 280 not taken.
✓ Branch 147 → 148 taken 1 time.
✓ Branch 147 → 156 taken 6252 times.
6253 if (!fieldType.isCoveredByGenericTypeList(templateTypesGeneric))
443
2/4
✓ Branch 151 → 152 taken 1 time.
✗ Branch 151 → 274 not taken.
✓ Branch 152 → 153 taken 1 time.
✗ Branch 152 → 271 not taken.
3 throw SemanticError(field->dataType, GENERIC_TYPE_NOT_IN_TEMPLATE, "Generic field type not included in struct template");
444 }
445
446 // Change to the root scope
447 3272 currentScope = rootScope;
448
1/2
✗ Branch 166 → 167 not taken.
✓ Branch 166 → 168 taken 3272 times.
3272 assert(currentScope->type == ScopeType::GLOBAL);
449
450 // Build struct object
451
5/10
✓ Branch 168 → 169 taken 3272 times.
✗ Branch 168 → 293 not taken.
✓ Branch 169 → 170 taken 3272 times.
✗ Branch 169 → 290 not taken.
✓ Branch 170 → 171 taken 3272 times.
✗ Branch 170 → 287 not taken.
✓ Branch 171 → 172 taken 3272 times.
✗ Branch 171 → 284 not taken.
✓ Branch 172 → 173 taken 3272 times.
✗ Branch 172 → 282 not taken.
3272 Struct spiceStruct(node->structName, node->entry, node->structScope, fieldTypes, templateTypesGeneric, interfaceTypes, node);
452
1/2
✓ Branch 177 → 178 taken 3272 times.
✗ Branch 177 → 310 not taken.
3272 StructManager::insert(currentScope, spiceStruct, &node->structManifestations);
453 3272 spiceStruct.scope = node->structScope;
454
455 // Request RTTI runtime if the struct is polymorphic
456 3272 node->emitVTable |= node->hasInterfaces;
457
12/18
✓ Branch 178 → 179 taken 125 times.
✓ Branch 178 → 185 taken 3147 times.
✓ Branch 181 → 182 taken 125 times.
✗ Branch 181 → 294 not taken.
✓ Branch 182 → 183 taken 125 times.
✗ Branch 182 → 294 not taken.
✓ Branch 183 → 184 taken 124 times.
✓ Branch 183 → 185 taken 1 time.
✓ Branch 186 → 187 taken 125 times.
✓ Branch 186 → 188 taken 3147 times.
✓ Branch 188 → 189 taken 125 times.
✓ Branch 188 → 191 taken 3147 times.
✓ Branch 191 → 192 taken 124 times.
✓ Branch 191 → 199 taken 3148 times.
✗ Branch 294 → 295 not taken.
✗ Branch 294 → 296 not taken.
✗ Branch 298 → 299 not taken.
✗ Branch 298 → 301 not taken.
3522 if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_EMIT_VTABLE))
458
2/4
✓ Branch 194 → 195 taken 124 times.
✗ Branch 194 → 305 not taken.
✓ Branch 195 → 196 taken 124 times.
✗ Branch 195 → 303 not taken.
372 node->emitVTable |= node->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_EMIT_VTABLE)->boolValue;
459
7/8
✓ Branch 199 → 200 taken 501 times.
✓ Branch 199 → 205 taken 2771 times.
✓ Branch 200 → 201 taken 501 times.
✗ Branch 200 → 310 not taken.
✓ Branch 203 → 204 taken 378 times.
✓ Branch 203 → 205 taken 123 times.
✓ Branch 206 → 207 taken 378 times.
✓ Branch 206 → 208 taken 2894 times.
3773 if (node->emitVTable && !sourceFile->isRttiRT())
460
1/2
✓ Branch 207 → 208 taken 378 times.
✗ Branch 207 → 310 not taken.
378 sourceFile->requestRuntimeModule(RTTI_RT);
461
462
1/2
✓ Branch 208 → 209 taken 3272 times.
✗ Branch 208 → 309 not taken.
6544 return nullptr;
463 3295 }
464
465 403 std::any TypeChecker::visitInterfaceDefPrepare(InterfaceDefNode *node) {
466 403 QualTypeList usedTemplateTypes;
467 403 std::vector<GenericType> templateTypesGeneric;
468
469 // Retrieve interface template types
470
2/2
✓ Branch 2 → 3 taken 191 times.
✓ Branch 2 → 47 taken 212 times.
403 if (node->hasTemplateTypes) {
471
1/2
✓ Branch 4 → 5 taken 191 times.
✗ Branch 4 → 178 not taken.
191 usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size());
472
1/2
✓ Branch 6 → 7 taken 191 times.
✗ Branch 6 → 178 not taken.
191 templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size());
473
2/2
✓ Branch 45 → 9 taken 191 times.
✓ Branch 45 → 46 taken 191 times.
573 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
474 // Visit template type
475
2/4
✓ Branch 11 → 12 taken 191 times.
✗ Branch 11 → 144 not taken.
✓ Branch 12 → 13 taken 191 times.
✗ Branch 12 → 142 not taken.
191 auto templateType = std::any_cast<QualType>(visit(dataType));
476
2/6
✓ Branch 14 → 15 taken 191 times.
✗ Branch 14 → 152 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 19 taken 191 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 145 not taken.
191 HANDLE_UNRESOLVED_TYPE_PTR(templateType)
477 // Check if it is a generic type
478
2/4
✓ Branch 19 → 20 taken 191 times.
✗ Branch 19 → 152 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 28 taken 191 times.
191 if (!templateType.is(TY_GENERIC)) {
479 softError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
480 continue;
481 }
482 // Convert generic symbol type to generic type
483
2/4
✓ Branch 28 → 29 taken 191 times.
✗ Branch 28 → 152 not taken.
✓ Branch 29 → 30 taken 191 times.
✗ Branch 29 → 152 not taken.
191 const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType());
484
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 191 times.
191 assert(genericType != nullptr);
485
1/2
✓ Branch 32 → 33 taken 191 times.
✗ Branch 32 → 152 not taken.
191 usedTemplateTypes.push_back(*genericType);
486
1/2
✓ Branch 33 → 34 taken 191 times.
✗ Branch 33 → 152 not taken.
191 templateTypesGeneric.push_back(*genericType);
487 }
488 }
489
490 // Update type of interface entry
491 403 const TypeChainElementData data = {.bodyScope = node->interfaceScope};
492
1/2
✓ Branch 47 → 48 taken 403 times.
✗ Branch 47 → 178 not taken.
403 const Type *type = TypeRegistry::getOrInsert(TY_INTERFACE, node->interfaceName, node->typeId, data, usedTemplateTypes);
493
1/2
✓ Branch 48 → 49 taken 403 times.
✗ Branch 48 → 178 not taken.
403 const QualType interfaceType(type, node->qualifiers);
494
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 403 times.
403 assert(node->entry != nullptr);
495 // If the entry was implicitly forward-declared because it is referenced across a circular import
496 // (assignDeferredOpaqueType), the existing type was already set to the opaque interface type. Overwriting it with the
497 // identical interned type is expected here.
498
2/4
✓ Branch 51 → 52 taken 403 times.
✗ Branch 51 → 178 not taken.
✓ Branch 52 → 53 taken 403 times.
✗ Branch 52 → 178 not taken.
403 const bool overwrite = node->entry->getQualType().is(TY_INTERFACE);
499
1/2
✓ Branch 53 → 54 taken 403 times.
✗ Branch 53 → 178 not taken.
403 node->entry->updateType(interfaceType, overwrite);
500
501 // Change to interface scope
502 403 currentScope = node->interfaceScope;
503
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 403 times.
403 assert(currentScope->type == ScopeType::INTERFACE);
504
505 // Visit methods
506 403 size_t vtableIndex = 0;
507 403 std::vector<Function *> methods;
508
1/2
✓ Branch 57 → 58 taken 403 times.
✗ Branch 57 → 176 not taken.
403 methods.reserve(node->signatures.size());
509
2/2
✓ Branch 118 → 60 taken 2438 times.
✓ Branch 118 → 119 taken 402 times.
3243 for (SignatureNode *signature : node->signatures) {
510
2/4
✓ Branch 62 → 63 taken 2438 times.
✗ Branch 62 → 156 not taken.
✓ Branch 63 → 64 taken 2438 times.
✗ Branch 63 → 154 not taken.
2438 const auto method = std::any_cast<std::vector<Function *> *>(visit(signature));
511
2/2
✓ Branch 65 → 66 taken 1 time.
✓ Branch 65 → 69 taken 2437 times.
2438 if (!method)
512
1/2
✓ Branch 66 → 67 taken 1 time.
✗ Branch 66 → 157 not taken.
2 return nullptr;
513
514 // Set 'this' type
515
2/2
✓ Branch 101 → 71 taken 2437 times.
✓ Branch 101 → 102 taken 2437 times.
7311 for (Function *m : *method) {
516 2437 m->isVirtual = true; // Interface methods are always virtual
517 2437 m->vtableIndex = vtableIndex;
518 2437 m->thisType = interfaceType;
519
520 // Merge the enclosing interface's own template types into the method's template types, mirroring how struct
521 // methods inherit the struct's generic params in visitFctDefPrepare. Without this, a method that does not
522 // redeclare its own template list (e.g. isValid() inside IIterator<T>) ends up with an empty templateTypes
523 // list, even though its thisType references the interface's generic type - so generic type resolution fails
524 // while matching the 'this' type of a call through an interface reference.
525
3/4
✓ Branch 73 → 74 taken 2437 times.
✗ Branch 73 → 159 not taken.
✓ Branch 91 → 76 taken 489 times.
✓ Branch 91 → 92 taken 2437 times.
5363 for (const QualType &templateType : interfaceType.getTemplateTypes()) {
526 288 const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; };
527
3/4
✓ Branch 78 → 79 taken 489 times.
✗ Branch 78 → 158 not taken.
✓ Branch 79 → 80 taken 201 times.
✓ Branch 79 → 81 taken 288 times.
489 if (std::ranges::none_of(m->templateTypes, lambda))
528
1/2
✓ Branch 80 → 81 taken 201 times.
✗ Branch 80 → 158 not taken.
201 m->templateTypes.emplace_back(templateType);
529 489 m->templateTypes.back().used = true;
530 }
531 }
532
533
1/2
✓ Branch 108 → 109 taken 2437 times.
✗ Branch 108 → 161 not taken.
4874 methods.insert(methods.end(), method->begin(), method->end());
534 2437 vtableIndex++;
535 }
536
537 // Change to root scope
538 402 currentScope = rootScope;
539
1/2
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 121 taken 402 times.
402 assert(currentScope->type == ScopeType::GLOBAL);
540
541 // Build interface object
542
4/8
✓ Branch 121 → 122 taken 402 times.
✗ Branch 121 → 172 not taken.
✓ Branch 122 → 123 taken 402 times.
✗ Branch 122 → 169 not taken.
✓ Branch 123 → 124 taken 402 times.
✗ Branch 123 → 166 not taken.
✓ Branch 124 → 125 taken 402 times.
✗ Branch 124 → 164 not taken.
402 Interface spiceInterface(node->interfaceName, node->entry, node->interfaceScope, methods, templateTypesGeneric, node);
543
1/2
✓ Branch 128 → 129 taken 402 times.
✗ Branch 128 → 174 not taken.
402 InterfaceManager::insert(currentScope, spiceInterface, &node->interfaceManifestations);
544 402 spiceInterface.scope = node->interfaceScope;
545
546 // Request RTTI runtime, that is always required when dealing with interfaces due to polymorphism
547
2/4
✓ Branch 129 → 130 taken 402 times.
✗ Branch 129 → 174 not taken.
✓ Branch 132 → 133 taken 402 times.
✗ Branch 132 → 134 not taken.
804 if (!sourceFile->isRttiRT())
548
1/2
✓ Branch 133 → 134 taken 402 times.
✗ Branch 133 → 174 not taken.
402 sourceFile->requestRuntimeModule(RTTI_RT);
549
550
1/2
✓ Branch 134 → 135 taken 402 times.
✗ Branch 134 → 173 not taken.
402 return nullptr;
551 403 }
552
553 382 std::any TypeChecker::visitEnumDefPrepare(EnumDefNode *node) {
554 // Update type of enum entry
555 382 const TypeChainElementData data = {.bodyScope = node->enumScope};
556
1/2
✓ Branch 3 → 4 taken 382 times.
✗ Branch 3 → 88 not taken.
382 const Type *type = TypeRegistry::getOrInsert(TY_ENUM, node->enumName, node->typeId, data, {});
557
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 382 times.
382 assert(node->entry != nullptr);
558 // The enum may already carry its opaque type if it was referenced across a circular import before being prepared
559 // (assignDeferredOpaqueType). Overwriting it with the identical interned type is expected here.
560
2/4
✓ Branch 7 → 8 taken 382 times.
✗ Branch 7 → 109 not taken.
✓ Branch 8 → 9 taken 382 times.
✗ Branch 8 → 109 not taken.
382 const bool overwrite = node->entry->getQualType().is(TY_ENUM);
561
2/4
✓ Branch 9 → 10 taken 382 times.
✗ Branch 9 → 91 not taken.
✓ Branch 10 → 11 taken 382 times.
✗ Branch 10 → 91 not taken.
382 node->entry->updateType(QualType(type, node->qualifiers), overwrite);
562
563 // Change to enum scope
564 382 currentScope = node->enumScope;
565
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 382 times.
382 assert(currentScope->type == ScopeType::ENUM);
566
567 // Loop through all items with values
568 382 std::vector<std::string> names;
569 382 std::vector<uint32_t> values;
570
2/2
✓ Branch 45 → 15 taken 5024 times.
✓ Branch 45 → 46 taken 382 times.
5788 for (const EnumItemNode *enumItem : node->itemLst->items) {
571 // Save name
572
1/2
✓ Branch 17 → 18 taken 5024 times.
✗ Branch 17 → 100 not taken.
5024 names.push_back(enumItem->itemName);
573 // Check for duplicate value
574
2/2
✓ Branch 18 → 19 taken 2642 times.
✓ Branch 18 → 36 taken 2382 times.
5024 if (enumItem->hasValue) {
575
3/4
✓ Branch 20 → 21 taken 2642 times.
✗ Branch 20 → 92 not taken.
✓ Branch 27 → 28 taken 1 time.
✓ Branch 27 → 35 taken 2641 times.
5284 if (std::ranges::find(values, enumItem->itemValue) != values.end()) {
576
2/4
✓ Branch 30 → 31 taken 1 time.
✗ Branch 30 → 96 not taken.
✓ Branch 31 → 32 taken 1 time.
✗ Branch 31 → 94 not taken.
1 softError(enumItem, DUPLICATE_ENUM_ITEM_VALUE, "Duplicate enum item value, please use another");
577 1 continue;
578 }
579
1/2
✓ Branch 35 → 36 taken 2641 times.
✗ Branch 35 → 100 not taken.
2641 values.push_back(enumItem->itemValue);
580 }
581 }
582
583 // Loop through all items without values
584 382 uint32_t nextValue = 0;
585
1/2
✓ Branch 46 → 47 taken 382 times.
✗ Branch 46 → 105 not taken.
382 const QualType intSymbolType(TY_INT);
586
2/2
✓ Branch 79 → 49 taken 5024 times.
✓ Branch 79 → 80 taken 382 times.
5788 for (EnumItemNode *enumItem : node->itemLst->items) {
587 // Update type of enum item entry
588
1/2
✓ Branch 51 → 52 taken 5024 times.
✗ Branch 51 → 103 not taken.
5024 SymbolTableEntry *itemEntry = currentScope->lookupStrict(enumItem->itemName);
589
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 5024 times.
5024 assert(itemEntry != nullptr);
590
1/2
✓ Branch 56 → 57 taken 5024 times.
✗ Branch 56 → 103 not taken.
5024 itemEntry->updateType(intSymbolType, false);
591 // Fill in value if not filled yet
592
2/2
✓ Branch 57 → 58 taken 2382 times.
✓ Branch 57 → 70 taken 2642 times.
5024 if (!enumItem->hasValue) {
593
3/4
✓ Branch 61 → 62 taken 4528 times.
✗ Branch 61 → 101 not taken.
✓ Branch 68 → 59 taken 2146 times.
✓ Branch 68 → 69 taken 2382 times.
9056 while (std::ranges::find(values, nextValue) != values.end())
594 2146 nextValue++;
595 2382 enumItem->itemValue = nextValue;
596
1/2
✓ Branch 69 → 70 taken 2382 times.
✗ Branch 69 → 103 not taken.
2382 values.push_back(nextValue);
597 }
598 }
599
600 // Change to root scope
601 382 currentScope = rootScope;
602
1/2
✗ Branch 80 → 81 not taken.
✓ Branch 80 → 82 taken 382 times.
382 assert(currentScope->type == ScopeType::GLOBAL);
603
604
1/2
✓ Branch 82 → 83 taken 382 times.
✗ Branch 82 → 104 not taken.
764 return nullptr;
605 382 }
606
607 2258 std::any TypeChecker::visitGenericTypeDefPrepare(GenericTypeDefNode *node) {
608 // Retrieve type conditions
609 2258 QualTypeList typeConditions;
610
1/2
✓ Branch 3 → 4 taken 2258 times.
✗ Branch 3 → 70 not taken.
2258 typeConditions.reserve(node->typeAltsLst->dataTypes.size());
611
2/2
✓ Branch 26 → 6 taken 3989 times.
✓ Branch 26 → 27 taken 2258 times.
8505 for (const auto &typeAlt : node->typeAltsLst->dataTypes) {
612
2/4
✓ Branch 8 → 9 taken 3989 times.
✗ Branch 8 → 58 not taken.
✓ Branch 9 → 10 taken 3989 times.
✗ Branch 9 → 56 not taken.
3989 auto typeCondition = std::any_cast<QualType>(visit(typeAlt));
613
2/6
✓ Branch 11 → 12 taken 3989 times.
✗ Branch 11 → 60 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 16 taken 3989 times.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 59 not taken.
3989 HANDLE_UNRESOLVED_TYPE_PTR(typeCondition)
614
1/2
✓ Branch 16 → 17 taken 3989 times.
✗ Branch 16 → 60 not taken.
3989 typeConditions.push_back(typeCondition);
615 }
616
617 // Add generic type to the scope
618
2/4
✓ Branch 27 → 28 taken 2258 times.
✗ Branch 27 → 64 not taken.
✓ Branch 28 → 29 taken 2258 times.
✗ Branch 28 → 62 not taken.
2258 const GenericType genericType(node->typeName, typeConditions);
619
1/2
✓ Branch 30 → 31 taken 2258 times.
✗ Branch 30 → 68 not taken.
2258 rootScope->insertGenericType(node->typeName, genericType);
620
621 // Check if only one type condition is set
622
7/8
✓ Branch 32 → 33 taken 1058 times.
✓ Branch 32 → 37 taken 1200 times.
✓ Branch 34 → 35 taken 1058 times.
✗ Branch 34 → 68 not taken.
✓ Branch 35 → 36 taken 1 time.
✓ Branch 35 → 37 taken 1057 times.
✓ Branch 38 → 39 taken 1 time.
✓ Branch 38 → 41 taken 2257 times.
2258 if (typeConditions.size() == 1 && !typeConditions.front().is(TY_DYN))
623
1/2
✓ Branch 39 → 40 taken 1 time.
✗ Branch 39 → 65 not taken.
1 sourceFile->compilerOutput.warnings.emplace_back(node->typeAltsLst->codeLoc, SINGLE_GENERIC_TYPE_CONDITION,
624 "Generic type is locked to one type");
625
626 // Check if the list contains the dyn type, along with other types
627
1/2
✓ Branch 41 → 42 taken 2258 times.
✗ Branch 41 → 68 not taken.
6245 const bool containsDynType = std::ranges::any_of(typeConditions, [&](const QualType &type) { return type.is(TY_DYN); });
628
6/6
✓ Branch 42 → 43 taken 1059 times.
✓ Branch 42 → 46 taken 1199 times.
✓ Branch 44 → 45 taken 2 times.
✓ Branch 44 → 46 taken 1057 times.
✓ Branch 47 → 48 taken 2 times.
✓ Branch 47 → 50 taken 2256 times.
2258 if (containsDynType && typeConditions.size() > 1)
629 2 sourceFile->compilerOutput.warnings.emplace_back(
630
1/2
✓ Branch 48 → 49 taken 2 times.
✗ Branch 48 → 66 not taken.
2 node->typeAltsLst->codeLoc, INEFFECTIVE_GENERIC_TYPE_CONDITION,
631 "One or several type conditions are superfluous, because the dyn type condition is given");
632
633
1/2
✓ Branch 50 → 51 taken 2258 times.
✗ Branch 50 → 67 not taken.
2258 return nullptr;
634 2258 }
635
636 351 std::any TypeChecker::visitAliasDefPrepare(AliasDefNode *node) {
637
2/4
✓ Branch 2 → 3 taken 351 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 351 times.
✗ Branch 3 → 5 not taken.
351 assert(node->entry != nullptr && node->aliasedTypeContainerEntry != nullptr);
638
639 // The alias may already have been prepared on demand because it was referenced across a circular import
640 // (assignDeferredOpaqueType). In that case there is nothing left to do.
641
4/6
✓ Branch 6 → 7 taken 351 times.
✗ Branch 6 → 43 not taken.
✓ Branch 7 → 8 taken 351 times.
✗ Branch 7 → 43 not taken.
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 12 taken 350 times.
351 if (node->entry->getQualType().is(TY_ALIAS))
642
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 32 not taken.
2 return nullptr;
643
644 // Ensure the aliased-type subtree is sized for manifestations before it is visited below. On the normal path
645 // visitEntry already sized the whole file to 1, so this is a no-op; on the on-demand path (a cross-import reference
646 // reaching this alias before its own file's visitEntry ran) it establishes the precondition setEvaluatedSymbolType
647 // requires.
648
1/2
✓ Branch 12 → 13 taken 350 times.
✗ Branch 12 → 43 not taken.
350 node->dataType->resizeToNumberOfManifestations(1);
649
650 // Update type of alias entry
651
1/2
✓ Branch 14 → 15 taken 350 times.
✗ Branch 14 → 33 not taken.
350 const Type *type = TypeRegistry::getOrInsert(TY_ALIAS, node->aliasName, node->typeId, {}, {});
652
2/4
✓ Branch 16 → 17 taken 350 times.
✗ Branch 16 → 37 not taken.
✓ Branch 17 → 18 taken 350 times.
✗ Branch 17 → 37 not taken.
350 node->entry->updateType(QualType(type, node->qualifiers), false);
653
654 // Update type of the aliased type container entry
655
2/4
✓ Branch 18 → 19 taken 350 times.
✗ Branch 18 → 40 not taken.
✓ Branch 19 → 20 taken 350 times.
✗ Branch 19 → 38 not taken.
350 const auto aliasedType = std::any_cast<QualType>(visit(node->dataType));
656
4/6
✓ Branch 21 → 22 taken 350 times.
✗ Branch 21 → 43 not taken.
✓ Branch 22 → 23 taken 1 time.
✓ Branch 22 → 26 taken 349 times.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 41 not taken.
351 HANDLE_UNRESOLVED_TYPE_PTR(aliasedType)
657
1/2
✓ Branch 26 → 27 taken 349 times.
✗ Branch 26 → 43 not taken.
349 node->aliasedTypeContainerEntry->updateType(aliasedType, false);
658 349 node->aliasedTypeContainerEntry->used = true; // The container type is always used per default
659
660
1/2
✓ Branch 27 → 28 taken 349 times.
✗ Branch 27 → 42 not taken.
698 return nullptr;
661 }
662
663 2738 std::any TypeChecker::visitGlobalVarDefPrepare(GlobalVarDefNode *node) {
664 // Insert variable name to symbol table
665
2/4
✓ Branch 2 → 3 taken 2738 times.
✗ Branch 2 → 87 not taken.
✓ Branch 3 → 4 taken 2738 times.
✗ Branch 3 → 85 not taken.
2738 auto globalVarType = std::any_cast<QualType>(visit(node->dataType));
666
2/6
✓ Branch 5 → 6 taken 2738 times.
✗ Branch 5 → 133 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 2738 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 88 not taken.
2738 HANDLE_UNRESOLVED_TYPE_PTR(globalVarType)
667
668
2/2
✓ Branch 10 → 11 taken 2736 times.
✓ Branch 10 → 40 taken 2 times.
2738 if (node->constant) { // Variable is initialized here
669
2/4
✓ Branch 11 → 12 taken 2736 times.
✗ Branch 11 → 91 not taken.
✓ Branch 12 → 13 taken 2736 times.
✗ Branch 12 → 89 not taken.
2736 const QualType rhsType = std::any_cast<ExprResult>(visit(node->constant)).type;
670
2/6
✓ Branch 14 → 15 taken 2736 times.
✗ Branch 14 → 110 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 19 taken 2736 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 93 not taken.
2736 HANDLE_UNRESOLVED_TYPE_PTR(rhsType)
671
3/4
✓ Branch 19 → 20 taken 2736 times.
✗ Branch 19 → 110 not taken.
✓ Branch 20 → 21 taken 1 time.
✓ Branch 20 → 22 taken 2735 times.
2736 if (globalVarType.is(TY_DYN)) { // Perform type inference
672 1 globalVarType = rhsType;
673
3/4
✓ Branch 22 → 23 taken 2735 times.
✗ Branch 22 → 110 not taken.
✓ Branch 23 → 24 taken 2 times.
✓ Branch 23 → 38 taken 2733 times.
2735 } else if (!globalVarType.matches(rhsType, false, true, true)) { // Check if types are matching
674
7/14
✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 107 not taken.
✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 102 not taken.
✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 100 not taken.
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 98 not taken.
✓ Branch 28 → 29 taken 2 times.
✗ Branch 28 → 96 not taken.
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 94 not taken.
✓ Branch 35 → 36 taken 2 times.
✗ Branch 35 → 109 not taken.
4 SOFT_ERROR_BOOL(node->constant, OPERATOR_WRONG_DATA_TYPE,
675 "Expected " + globalVarType.getName(false) + ", but got " + rhsType.getName(false))
676 }
677 }
678
679 // Check if the type is still missing
680
3/4
✓ Branch 40 → 41 taken 2736 times.
✗ Branch 40 → 133 not taken.
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 51 taken 2735 times.
2736 if (globalVarType.is(TY_DYN))
681
3/6
✓ Branch 44 → 45 taken 1 time.
✗ Branch 44 → 113 not taken.
✓ Branch 45 → 46 taken 1 time.
✗ Branch 45 → 111 not taken.
✓ Branch 48 → 49 taken 1 time.
✗ Branch 48 → 117 not taken.
4 SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_TYPE_DYN, "Global variables must have an explicit data type")
682
683 // Check if we would need to insert instructions in the global scope to initialize the variable
684
2/4
✓ Branch 51 → 52 taken 2735 times.
✗ Branch 51 → 133 not taken.
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 62 taken 2735 times.
2735 if (!globalVarType.isPrimitive())
685 SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_INVALID_TYPE, "Spice does only support global variables of primitive type")
686
687 // Update type of global var entry
688
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 2735 times.
2735 assert(node->entry != nullptr);
689
1/2
✓ Branch 64 → 65 taken 2735 times.
✗ Branch 64 → 133 not taken.
2735 node->entry->updateType(globalVarType, false);
690
691 // Check if a value is attached
692
6/8
✓ Branch 65 → 66 taken 1 time.
✓ Branch 65 → 69 taken 2734 times.
✓ Branch 66 → 67 taken 1 time.
✗ Branch 66 → 133 not taken.
✓ Branch 67 → 68 taken 1 time.
✗ Branch 67 → 69 not taken.
✓ Branch 70 → 71 taken 1 time.
✓ Branch 70 → 80 taken 2734 times.
2735 if (!node->constant && globalVarType.isConst())
693
3/6
✓ Branch 73 → 74 taken 1 time.
✗ Branch 73 → 127 not taken.
✓ Branch 74 → 75 taken 1 time.
✗ Branch 74 → 125 not taken.
✓ Branch 77 → 78 taken 1 time.
✗ Branch 77 → 131 not taken.
4 SOFT_ERROR_BOOL(node, GLOBAL_CONST_WITHOUT_VALUE, "You must specify a value for constant global variables")
694
695
1/2
✓ Branch 80 → 81 taken 2734 times.
✗ Branch 80 → 132 not taken.
5468 return nullptr;
696 }
697
698 3977 std::any TypeChecker::visitExtDeclPrepare(ExtDeclNode *node) {
699 // Collect argument types
700 3977 QualTypeList argTypes;
701 3977 ParamList argList;
702
2/2
✓ Branch 2 → 3 taken 3768 times.
✓ Branch 2 → 41 taken 209 times.
3977 if (node->hasArgs) {
703
1/2
✓ Branch 4 → 5 taken 3768 times.
✗ Branch 4 → 168 not taken.
3768 argList.reserve(node->argTypeLst->typeLst->dataTypes.size());
704
2/2
✓ Branch 39 → 7 taken 8291 times.
✓ Branch 39 → 40 taken 3768 times.
15827 for (DataTypeNode *arg : node->argTypeLst->typeLst->dataTypes) {
705 // Visit argument
706
2/4
✓ Branch 9 → 10 taken 8291 times.
✗ Branch 9 → 113 not taken.
✓ Branch 10 → 11 taken 8291 times.
✗ Branch 10 → 111 not taken.
8291 auto argType = std::any_cast<QualType>(visit(arg));
707
2/6
✓ Branch 12 → 13 taken 8291 times.
✗ Branch 12 → 122 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 17 taken 8291 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 114 not taken.
8291 HANDLE_UNRESOLVED_TYPE_PTR(argType)
708 // Check if the argument type is 'dyn'
709
3/4
✓ Branch 17 → 18 taken 8291 times.
✗ Branch 17 → 122 not taken.
✓ Branch 18 → 19 taken 1 time.
✓ Branch 18 → 26 taken 8290 times.
8291 if (argType.is(TY_DYN)) {
710
2/4
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 117 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 115 not taken.
1 softError(arg, UNEXPECTED_DYN_TYPE, "Dyn data type is not allowed as arg type for external functions");
711 1 continue;
712 }
713 // Save argument
714
1/2
✓ Branch 26 → 27 taken 8290 times.
✗ Branch 26 → 122 not taken.
8290 argTypes.push_back(argType);
715
1/2
✓ Branch 27 → 28 taken 8290 times.
✗ Branch 27 → 121 not taken.
8290 argList.push_back({argType, false});
716 }
717 }
718
719 // Retrieve return type
720
1/2
✓ Branch 41 → 42 taken 3977 times.
✗ Branch 41 → 168 not taken.
3977 QualType returnType(TY_DYN);
721 3977 const bool isFunction = node->returnType;
722
2/2
✓ Branch 42 → 43 taken 3166 times.
✓ Branch 42 → 62 taken 811 times.
3977 if (isFunction) { // External function
723
2/4
✓ Branch 43 → 44 taken 3166 times.
✗ Branch 43 → 126 not taken.
✓ Branch 44 → 45 taken 3166 times.
✗ Branch 44 → 124 not taken.
3166 returnType = std::any_cast<QualType>(visit(node->returnType));
724
2/6
✓ Branch 46 → 47 taken 3166 times.
✗ Branch 46 → 168 not taken.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 51 taken 3166 times.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 128 not taken.
3166 HANDLE_UNRESOLVED_TYPE_PTR(returnType)
725 // Check if return type is dyn
726
3/4
✓ Branch 51 → 52 taken 3166 times.
✗ Branch 51 → 168 not taken.
✓ Branch 52 → 53 taken 1 time.
✓ Branch 52 → 62 taken 3165 times.
3166 if (returnType.is(TY_DYN))
727
3/6
✓ Branch 55 → 56 taken 1 time.
✗ Branch 55 → 131 not taken.
✓ Branch 56 → 57 taken 1 time.
✗ Branch 56 → 129 not taken.
✓ Branch 59 → 60 taken 1 time.
✗ Branch 59 → 135 not taken.
4 SOFT_ERROR_BOOL(node->returnType, UNEXPECTED_DYN_TYPE, "dyn is not allowed as return type for external functions")
728 }
729
730 // Add function to current scope
731
4/8
✓ Branch 63 → 64 taken 3976 times.
✗ Branch 63 → 142 not taken.
✓ Branch 64 → 65 taken 3976 times.
✗ Branch 64 → 139 not taken.
✓ Branch 65 → 66 taken 3976 times.
✗ Branch 65 → 138 not taken.
✓ Branch 66 → 67 taken 3976 times.
✗ Branch 66 → 136 not taken.
3976 const Function spiceFunc(node->extFunctionName, node->entry, QualType(TY_DYN), returnType, argList, {}, node);
732
1/2
✓ Branch 70 → 71 taken 3976 times.
✗ Branch 70 → 166 not taken.
3976 node->extFunction = FunctionManager::insert(currentScope, spiceFunc, &node->extFunctionManifestations);
733 3976 node->extFunction->mangleFunctionName = false;
734 3976 node->extFunction->alreadyTypeChecked = true;
735
4/4
✓ Branch 71 → 72 taken 3768 times.
✓ Branch 71 → 74 taken 208 times.
✓ Branch 72 → 73 taken 66 times.
✓ Branch 72 → 74 taken 3702 times.
3976 node->extFunction->isVararg = node->argTypeLst && node->argTypeLst->hasEllipsis;
736
737 // Check procedure attributes
738
2/2
✓ Branch 75 → 76 taken 1 time.
✓ Branch 75 → 93 taken 3975 times.
3976 if (node->attrs) {
739 1 const AttrLstNode *attrLst = node->attrs->attrLst;
740
3/6
✓ Branch 78 → 79 taken 1 time.
✗ Branch 78 → 148 not taken.
✓ Branch 79 → 80 taken 1 time.
✗ Branch 79 → 146 not taken.
✗ Branch 82 → 83 not taken.
✓ Branch 82 → 84 taken 1 time.
3 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
741 node->extFunction->mangleFunctionName = value->boolValue;
742
3/6
✓ Branch 86 → 87 taken 1 time.
✗ Branch 86 → 154 not taken.
✓ Branch 87 → 88 taken 1 time.
✗ Branch 87 → 152 not taken.
✓ Branch 90 → 91 taken 1 time.
✗ Branch 90 → 93 not taken.
3 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
743
1/2
✓ Branch 91 → 92 taken 1 time.
✗ Branch 91 → 166 not taken.
1 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
744
1/2
✓ Branch 92 → 93 taken 1 time.
✗ Branch 92 → 166 not taken.
1 node->extFunction->predefinedMangledName = stringValue;
745 }
746 }
747
748 // Prepare ext function type
749
2/2
✓ Branch 93 → 94 taken 3165 times.
✓ Branch 93 → 95 taken 811 times.
3976 const SuperType superType = isFunction ? TY_FUNCTION : TY_PROCEDURE;
750
2/4
✓ Branch 96 → 97 taken 3976 times.
✗ Branch 96 → 158 not taken.
✓ Branch 97 → 98 taken 3976 times.
✗ Branch 97 → 158 not taken.
3976 const QualType extFunctionType = QualType(superType).getWithFunctionParamAndReturnTypes(returnType, argTypes);
751
752 // Set type of external function
753
1/2
✓ Branch 98 → 99 taken 3976 times.
✗ Branch 98 → 166 not taken.
3976 node->entry->updateType(extFunctionType, false);
754
755 // Rename the original child scope to reflect the substantiated versions of the external function
756
3/6
✓ Branch 99 → 100 taken 3976 times.
✗ Branch 99 → 164 not taken.
✓ Branch 100 → 101 taken 3976 times.
✗ Branch 100 → 161 not taken.
✓ Branch 101 → 102 taken 3976 times.
✗ Branch 101 → 159 not taken.
3976 currentScope->renameChildScope(node->getScopeId(), spiceFunc.getScopeName());
757
758
1/2
✓ Branch 104 → 105 taken 3976 times.
✗ Branch 104 → 165 not taken.
3976 return nullptr;
759 3977 }
760
761 2729 std::any TypeChecker::visitImportDefPrepare(ImportDefNode *node) {
762 // Set entry to import type
763
1/2
✓ Branch 2 → 3 taken 2729 times.
✗ Branch 2 → 11 not taken.
2729 const QualType importType(TY_IMPORT, node->importName);
764
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2729 times.
2729 assert(node->entry != nullptr);
765
1/2
✓ Branch 5 → 6 taken 2729 times.
✗ Branch 5 → 11 not taken.
2729 node->entry->updateType(importType, false);
766
767
1/2
✓ Branch 6 → 7 taken 2729 times.
✗ Branch 6 → 10 not taken.
5458 return nullptr;
768 }
769
770 } // namespace spice::compiler
771