GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 99.4% 177 / 0 / 178
Functions: 100.0% 12 / 0 / 12
Branches: 54.4% 257 / 0 / 472

src/typechecker/TypeCheckerControlStructures.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 <global/GlobalResourceManager.h>
8 #include <symboltablebuilder/Scope.h>
9 #include <symboltablebuilder/ScopeHandle.h>
10 #include <typechecker/FunctionManager.h>
11 #include <typechecker/MacroDefs.h>
12
13 namespace spice::compiler {
14
15 2637 std::any TypeChecker::visitUnsafeBlock(UnsafeBlockNode *node) {
16 // Change to unsafe block body scope
17
2/4
✓ Branch 2 → 3 taken 2637 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 2637 times.
✗ Branch 3 → 11 not taken.
2637 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::UNSAFE_BODY);
18
19 // Visit body
20
1/2
✓ Branch 5 → 6 taken 2637 times.
✗ Branch 5 → 15 not taken.
2637 visit(node->body);
21
22
1/2
✓ Branch 7 → 8 taken 2637 times.
✗ Branch 7 → 16 not taken.
5274 return nullptr;
23 2637 }
24
25 1498 std::any TypeChecker::visitForLoop(ForLoopNode *node) {
26 // Change to for body scope
27
2/4
✓ Branch 2 → 3 taken 1498 times.
✗ Branch 2 → 37 not taken.
✓ Branch 3 → 4 taken 1498 times.
✗ Branch 3 → 35 not taken.
1498 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOR_BODY);
28
29 // Visit loop variable declaration
30
1/2
✓ Branch 5 → 6 taken 1498 times.
✗ Branch 5 → 39 not taken.
1498 visit(node->initDecl);
31
32 // Visit condition
33
2/4
✓ Branch 7 → 8 taken 1498 times.
✗ Branch 7 → 42 not taken.
✓ Branch 8 → 9 taken 1498 times.
✗ Branch 8 → 40 not taken.
1498 const QualType conditionType = std::any_cast<ExprResult>(visit(node->condAssign)).type;
34
2/6
✓ Branch 10 → 11 taken 1498 times.
✗ Branch 10 → 55 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 1498 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 44 not taken.
1498 HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
35 // Check if condition evaluates to bool
36
3/4
✓ Branch 14 → 15 taken 1498 times.
✗ Branch 14 → 55 not taken.
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 26 taken 1497 times.
1498 if (!conditionType.is(TY_BOOL))
37
4/8
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 47 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 45 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 51 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 51 not taken.
3 SOFT_ERROR_ER(node->condAssign, CONDITION_MUST_BE_BOOL, "For loop condition must be of type bool")
38
39 // Visit incrementer
40
1/2
✓ Branch 26 → 27 taken 1497 times.
✗ Branch 26 → 52 not taken.
1497 visit(node->incAssign);
41
42 // Visit body
43
1/2
✓ Branch 28 → 29 taken 1497 times.
✗ Branch 28 → 53 not taken.
1497 visit(node->body);
44
45
1/2
✓ Branch 30 → 31 taken 1497 times.
✗ Branch 30 → 54 not taken.
1497 return nullptr;
46 1498 }
47
48 136 std::any TypeChecker::visitForeachLoop(ForeachLoopNode *node) {
49 // Visit iterator assignment
50 136 ExprNode *iteratorNode = node->iteratorAssign;
51
2/4
✓ Branch 2 → 3 taken 136 times.
✗ Branch 2 → 207 not taken.
✓ Branch 3 → 4 taken 136 times.
✗ Branch 3 → 205 not taken.
136 QualType iteratorOrIterableType = std::any_cast<ExprResult>(visit(iteratorNode)).type;
52
2/6
✓ Branch 5 → 6 taken 136 times.
✗ Branch 5 → 353 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 136 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 209 not taken.
136 HANDLE_UNRESOLVED_TYPE_PTR(iteratorOrIterableType)
53
1/2
✓ Branch 9 → 10 taken 136 times.
✗ Branch 9 → 210 not taken.
136 iteratorOrIterableType = iteratorOrIterableType.removeReferenceWrapper();
54
55 // Retrieve iterator type
56 136 QualType iteratorType = iteratorOrIterableType;
57
58
3/4
✓ Branch 10 → 11 taken 136 times.
✗ Branch 10 → 353 not taken.
✓ Branch 11 → 12 taken 112 times.
✓ Branch 11 → 74 taken 24 times.
136 if (iteratorOrIterableType.isIterable(node)) {
59 112 const QualType &iterableType = iteratorOrIterableType;
60
3/4
✓ Branch 12 → 13 taken 112 times.
✗ Branch 12 → 353 not taken.
✓ Branch 13 → 14 taken 8 times.
✓ Branch 13 → 50 taken 104 times.
112 if (iteratorOrIterableType.isArray()) { // Array
61
2/4
✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 213 not taken.
✓ Branch 17 → 18 taken 8 times.
✗ Branch 17 → 211 not taken.
16 const NameRegistryEntry *nameRegistryEntry = sourceFile->getNameRegistryEntry(ARRAY_ITERATOR_NAME);
62
2/2
✓ Branch 20 → 21 taken 1 time.
✓ Branch 20 → 29 taken 7 times.
8 if (!nameRegistryEntry) {
63
2/4
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 219 not taken.
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 217 not taken.
1 softError(node, UNKNOWN_DATATYPE, "Forgot to import \"std/iterator/array-iterator\"?");
64
1/2
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 223 not taken.
1 return nullptr;
65 }
66 7 nameRegistryEntry->targetEntry->used = nameRegistryEntry->importEntry->used = true;
67 7 Scope *matchScope = nameRegistryEntry->targetScope->parent;
68
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 7 times.
7 assert(matchScope->type == ScopeType::GLOBAL);
69
1/2
✓ Branch 31 → 32 taken 7 times.
✗ Branch 31 → 241 not taken.
7 QualType unsignedLongType(TY_LONG);
70
1/2
✓ Branch 32 → 33 taken 7 times.
✗ Branch 32 → 241 not taken.
7 unsignedLongType.makeUnsigned(true);
71
1/2
✓ Branch 37 → 38 taken 7 times.
✗ Branch 37 → 224 not taken.
14 const ArgList argTypes = {Arg(iterableType, false), Arg(unsignedLongType, false)};
72
1/2
✓ Branch 39 → 40 taken 7 times.
✗ Branch 39 → 239 not taken.
7 const QualType thisType(TY_DYN);
73
2/4
✓ Branch 43 → 44 taken 7 times.
✗ Branch 43 → 232 not taken.
✓ Branch 44 → 45 taken 7 times.
✗ Branch 44 → 230 not taken.
21 node->getIteratorFct = FunctionManager::match(matchScope, "iterate", thisType, argTypes, {}, true, iteratorNode);
74 7 } else { // Struct, implementing Iterator interface
75
1/2
✓ Branch 50 → 51 taken 104 times.
✗ Branch 50 → 353 not taken.
104 Scope *matchScope = iterableType.getBodyScope();
76
2/4
✓ Branch 55 → 56 taken 104 times.
✗ Branch 55 → 244 not taken.
✓ Branch 56 → 57 taken 104 times.
✗ Branch 56 → 242 not taken.
312 node->getIteratorFct = FunctionManager::match(matchScope, "getIterator", iterableType, {}, {}, true, iteratorNode);
77 }
78
2/2
✓ Branch 62 → 63 taken 1 time.
✓ Branch 62 → 71 taken 110 times.
111 if (node->getIteratorFct == nullptr)
79
2/4
✓ Branch 66 → 67 taken 1 time.
✗ Branch 66 → 257 not taken.
✓ Branch 67 → 68 taken 1 time.
✗ Branch 67 → 254 not taken.
3 throw SemanticError(iteratorNode, INVALID_ITERATOR, "No getIterator() function found for the given iterable type");
80
81 110 iteratorType = QualType(node->getIteratorFct->returnType);
82 // Add anonymous symbol to keep track of dtor call, if non-trivially destructible
83
3/4
✓ Branch 71 → 72 taken 110 times.
✗ Branch 71 → 353 not taken.
✓ Branch 72 → 73 taken 1 time.
✓ Branch 72 → 74 taken 109 times.
110 if (!iteratorType.isTriviallyDestructible(iteratorNode))
84
1/2
✓ Branch 73 → 74 taken 1 time.
✗ Branch 73 → 353 not taken.
1 currentScope->symbolTable.insertAnonymous(iteratorType, iteratorNode);
85 }
86
87 // Change to foreach body scope
88
2/4
✓ Branch 74 → 75 taken 134 times.
✗ Branch 74 → 265 not taken.
✓ Branch 75 → 76 taken 134 times.
✗ Branch 75 → 263 not taken.
134 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOREACH_BODY);
89
90 // Check iterator type
91
3/4
✓ Branch 77 → 78 taken 134 times.
✗ Branch 77 → 351 not taken.
✓ Branch 78 → 79 taken 1 time.
✓ Branch 78 → 86 taken 133 times.
134 if (!iteratorType.isIterator(node)) {
92 const std::string errMsg =
93 "Can only iterate over arrays or data structures, inheriting from IIterator or IIterable. You provided " +
94
2/4
✓ Branch 79 → 80 taken 1 time.
✗ Branch 79 → 269 not taken.
✓ Branch 80 → 81 taken 1 time.
✗ Branch 80 → 267 not taken.
1 iteratorType.getName(false, true);
95
1/2
✓ Branch 82 → 83 taken 1 time.
✗ Branch 82 → 271 not taken.
1 softError(node->iteratorAssign, OPERATOR_WRONG_DATA_TYPE, errMsg);
96
1/2
✓ Branch 83 → 84 taken 1 time.
✗ Branch 83 → 270 not taken.
1 return nullptr;
97 1 }
98
1/2
✓ Branch 86 → 87 taken 133 times.
✗ Branch 86 → 351 not taken.
133 const QualTypeList &iteratorTemplateTypes = iteratorType.getTemplateTypes();
99
2/2
✓ Branch 88 → 89 taken 1 time.
✓ Branch 88 → 99 taken 132 times.
133 if (iteratorTemplateTypes.empty())
100
4/8
✓ Branch 91 → 92 taken 1 time.
✗ Branch 91 → 276 not taken.
✓ Branch 92 → 93 taken 1 time.
✗ Branch 92 → 274 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 280 not taken.
✓ Branch 96 → 97 taken 1 time.
✗ Branch 96 → 280 not taken.
3 SOFT_ERROR_ER(node->iteratorAssign, INVALID_ITERATOR,
101 "Iterator has no generic arguments so that the item type could not be inferred")
102
103 132 const bool hasIdx = node->idxVarDecl;
104
2/2
✓ Branch 99 → 100 taken 8 times.
✓ Branch 99 → 120 taken 124 times.
132 if (hasIdx) {
105 // Visit index declaration or assignment
106
2/4
✓ Branch 100 → 101 taken 8 times.
✗ Branch 100 → 283 not taken.
✓ Branch 101 → 102 taken 8 times.
✗ Branch 101 → 281 not taken.
8 auto indexType = std::any_cast<QualType>(visit(node->idxVarDecl));
107
2/6
✓ Branch 103 → 104 taken 8 times.
✗ Branch 103 → 292 not taken.
✗ Branch 104 → 105 not taken.
✓ Branch 104 → 107 taken 8 times.
✗ Branch 105 → 106 not taken.
✗ Branch 105 → 284 not taken.
8 HANDLE_UNRESOLVED_TYPE_PTR(indexType)
108 // Check if index type is int
109
3/4
✓ Branch 107 → 108 taken 8 times.
✗ Branch 107 → 292 not taken.
✓ Branch 108 → 109 taken 1 time.
✓ Branch 108 → 118 taken 7 times.
8 if (!indexType.is(TY_LONG))
110
5/10
✓ Branch 109 → 110 taken 1 time.
✗ Branch 109 → 289 not taken.
✓ Branch 110 → 111 taken 1 time.
✗ Branch 110 → 287 not taken.
✓ Branch 111 → 112 taken 1 time.
✗ Branch 111 → 285 not taken.
✓ Branch 114 → 115 taken 1 time.
✗ Branch 114 → 291 not taken.
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 291 not taken.
1 SOFT_ERROR_ER(node->idxVarDecl, FOREACH_IDX_NOT_LONG,
111 "Index in foreach loop must be of type long. You provided " + indexType.getName(false))
112 }
113
114 // Retrieve .get(), .getIdx(), .isValid() and .next() functions
115
1/2
✓ Branch 120 → 121 taken 131 times.
✗ Branch 120 → 351 not taken.
131 Scope *matchScope = iteratorType.getBodyScope();
116 131 QualType iteratorItemType;
117
2/2
✓ Branch 121 → 122 taken 7 times.
✓ Branch 121 → 138 taken 124 times.
131 if (hasIdx) {
118
2/4
✓ Branch 126 → 127 taken 7 times.
✗ Branch 126 → 295 not taken.
✓ Branch 127 → 128 taken 7 times.
✗ Branch 127 → 293 not taken.
21 node->getIdxFct = FunctionManager::match(matchScope, "getIdx", iteratorType, {}, {}, false, node);
119
1/4
✗ Branch 132 → 133 not taken.
✓ Branch 132 → 135 taken 7 times.
✗ Branch 133 → 134 not taken.
✗ Branch 133 → 305 not taken.
7 RETURN_NULLPTR_IF_NULLPTR(node->getIdxFct);
120
1/2
✓ Branch 135 → 136 taken 7 times.
✗ Branch 135 → 351 not taken.
7 iteratorItemType = node->getIdxFct->returnType.getTemplateTypes().back();
121 } else {
122
2/4
✓ Branch 142 → 143 taken 124 times.
✗ Branch 142 → 308 not taken.
✓ Branch 143 → 144 taken 124 times.
✗ Branch 143 → 306 not taken.
372 node->getFct = FunctionManager::match(matchScope, "get", iteratorType, {}, {}, false, node);
123
1/4
✗ Branch 148 → 149 not taken.
✓ Branch 148 → 151 taken 124 times.
✗ Branch 149 → 150 not taken.
✗ Branch 149 → 318 not taken.
124 RETURN_NULLPTR_IF_NULLPTR(node->getFct);
124 124 iteratorItemType = node->getFct->returnType;
125 }
126
2/4
✓ Branch 156 → 157 taken 131 times.
✗ Branch 156 → 321 not taken.
✓ Branch 157 → 158 taken 131 times.
✗ Branch 157 → 319 not taken.
393 node->isValidFct = FunctionManager::match(matchScope, "isValid", iteratorType, {}, {}, false, node);
127
1/4
✗ Branch 162 → 163 not taken.
✓ Branch 162 → 165 taken 131 times.
✗ Branch 163 → 164 not taken.
✗ Branch 163 → 331 not taken.
131 RETURN_NULLPTR_IF_NULLPTR(node->isValidFct);
128
2/4
✓ Branch 169 → 170 taken 131 times.
✗ Branch 169 → 334 not taken.
✓ Branch 170 → 171 taken 131 times.
✗ Branch 170 → 332 not taken.
393 node->nextFct = FunctionManager::match(matchScope, "next", iteratorType, {}, {}, false, node);
129
1/4
✗ Branch 175 → 176 not taken.
✓ Branch 175 → 178 taken 131 times.
✗ Branch 176 → 177 not taken.
✗ Branch 176 → 344 not taken.
131 RETURN_NULLPTR_IF_NULLPTR(node->nextFct);
130
131 // Retrieve item variable entry
132
1/2
✓ Branch 178 → 179 taken 131 times.
✗ Branch 178 → 351 not taken.
131 SymbolTableEntry *itemVarSymbol = currentScope->lookupStrict(node->itemVarDecl->varName);
133
1/2
✗ Branch 181 → 182 not taken.
✓ Branch 181 → 183 taken 131 times.
131 assert(itemVarSymbol != nullptr);
134
135 // Check type of the item
136
2/4
✓ Branch 183 → 184 taken 131 times.
✗ Branch 183 → 347 not taken.
✓ Branch 184 → 185 taken 131 times.
✗ Branch 184 → 345 not taken.
131 auto itemType = std::any_cast<QualType>(visit(node->itemVarDecl));
137
2/6
✓ Branch 186 → 187 taken 131 times.
✗ Branch 186 → 351 not taken.
✗ Branch 187 → 188 not taken.
✓ Branch 187 → 190 taken 131 times.
✗ Branch 188 → 189 not taken.
✗ Branch 188 → 348 not taken.
131 HANDLE_UNRESOLVED_TYPE_PTR(itemType)
138
3/4
✓ Branch 190 → 191 taken 131 times.
✗ Branch 190 → 351 not taken.
✓ Branch 191 → 192 taken 5 times.
✓ Branch 191 → 194 taken 126 times.
131 if (itemType.is(TY_DYN)) { // Perform type inference
139 // Update evaluated symbol type of the declaration data type
140
1/2
✓ Branch 192 → 193 taken 5 times.
✗ Branch 192 → 351 not taken.
5 node->itemVarDecl->dataType->setEvaluatedSymbolType(iteratorItemType, manIdx);
141 // Update item type
142 5 itemType = iteratorItemType;
143 }
144
145 // Check result type. This is important, also for copy ctor calls, etc.
146 131 const ExprResult itemResult = {itemType, itemVarSymbol};
147 131 const ExprResult iteratorItemResult = {iteratorItemType, nullptr /* always a temporary */};
148 130 const auto [_, copyCtor] =
149
2/2
✓ Branch 194 → 195 taken 130 times.
✓ Branch 194 → 351 taken 1 time.
131 opRuleManager.getAssignResultType(node->itemVarDecl, itemResult, iteratorItemResult, true, false, ERROR_FOREACH_ITEM);
150 130 node->calledItemCopyCtor = copyCtor;
151
152 // Update type of item
153
1/2
✓ Branch 197 → 198 taken 130 times.
✗ Branch 197 → 351 not taken.
130 itemVarSymbol->updateType(itemType, true);
154
155 // Visit body
156
1/2
✓ Branch 198 → 199 taken 130 times.
✗ Branch 198 → 349 not taken.
130 visit(node->body);
157
158
1/2
✓ Branch 200 → 201 taken 130 times.
✗ Branch 200 → 350 not taken.
130 return nullptr;
159 134 }
160
161 861 std::any TypeChecker::visitWhileLoop(WhileLoopNode *node) {
162 // Change to while body scope
163
2/4
✓ Branch 2 → 3 taken 861 times.
✗ Branch 2 → 33 not taken.
✓ Branch 3 → 4 taken 861 times.
✗ Branch 3 → 31 not taken.
861 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY);
164
165 // Visit condition
166
3/4
✓ Branch 5 → 6 taken 860 times.
✓ Branch 5 → 37 taken 1 time.
✓ Branch 6 → 7 taken 860 times.
✗ Branch 6 → 35 not taken.
861 const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type;
167
2/6
✓ Branch 8 → 9 taken 860 times.
✗ Branch 8 → 49 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 860 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 39 not taken.
860 HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
168 // Check if condition evaluates to bool
169
3/4
✓ Branch 12 → 13 taken 860 times.
✗ Branch 12 → 49 not taken.
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 24 taken 859 times.
860 if (!conditionType.is(TY_BOOL))
170
4/8
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 42 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 40 not taken.
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 46 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 46 not taken.
3 SOFT_ERROR_ER(node->condition, CONDITION_MUST_BE_BOOL, "While loop condition must be of type bool")
171
172 // Visit body
173
1/2
✓ Branch 24 → 25 taken 859 times.
✗ Branch 24 → 47 not taken.
859 visit(node->body);
174
175
1/2
✓ Branch 26 → 27 taken 859 times.
✗ Branch 26 → 48 not taken.
859 return nullptr;
176 861 }
177
178 8 std::any TypeChecker::visitDoWhileLoop(DoWhileLoopNode *node) {
179 // Change to while body scope
180
2/4
✓ Branch 2 → 3 taken 8 times.
✗ Branch 2 → 33 not taken.
✓ Branch 3 → 4 taken 8 times.
✗ Branch 3 → 31 not taken.
8 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY);
181
182 // Visit body
183
1/2
✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 35 not taken.
8 visit(node->body);
184
185 // Visit condition
186
2/4
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 38 not taken.
✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 36 not taken.
8 const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type;
187
2/6
✓ Branch 10 → 11 taken 8 times.
✗ Branch 10 → 49 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 8 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 40 not taken.
8 HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
188 // Check if condition evaluates to bool
189
3/4
✓ Branch 14 → 15 taken 8 times.
✗ Branch 14 → 49 not taken.
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 26 taken 7 times.
8 if (!conditionType.is(TY_BOOL))
190
4/8
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 43 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 41 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 47 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 47 not taken.
3 SOFT_ERROR_ER(node->condition, CONDITION_MUST_BE_BOOL, "Do-While loop condition must be of type bool")
191
192
1/2
✓ Branch 26 → 27 taken 7 times.
✗ Branch 26 → 48 not taken.
7 return nullptr;
193 8 }
194
195 4901 std::any TypeChecker::visitIfStmt(IfStmtNode *node) {
196 // Change to then body scope
197
2/4
✓ Branch 2 → 3 taken 4901 times.
✗ Branch 2 → 59 not taken.
✓ Branch 3 → 4 taken 4901 times.
✗ Branch 3 → 57 not taken.
4901 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY);
198
199 // Visit condition
200
3/4
✓ Branch 5 → 6 taken 4899 times.
✓ Branch 5 → 63 taken 2 times.
✓ Branch 6 → 7 taken 4899 times.
✗ Branch 6 → 61 not taken.
4901 const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type;
201
4/6
✓ Branch 8 → 9 taken 4899 times.
✗ Branch 8 → 77 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 12 taken 4898 times.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 65 not taken.
4899 HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
202 // Check if condition evaluates to bool
203
3/4
✓ Branch 12 → 13 taken 4898 times.
✗ Branch 12 → 77 not taken.
✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 24 taken 4896 times.
4898 if (!conditionType.is(TY_BOOL))
204
4/8
✓ Branch 16 → 17 taken 2 times.
✗ Branch 16 → 68 not taken.
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 66 not taken.
✓ Branch 20 → 21 taken 2 times.
✗ Branch 20 → 72 not taken.
✓ Branch 21 → 22 taken 2 times.
✗ Branch 21 → 72 not taken.
6 SOFT_ERROR_ER(node->condition, CONDITION_MUST_BE_BOOL, "If condition must be of type bool")
205
206 // Warning for bool assignment
207
1/2
✓ Branch 24 → 25 taken 4896 times.
✗ Branch 24 → 26 not taken.
4896 const auto *assignExprNode = dynamic_cast<AssignExprNode *>(node->condition);
208
3/4
✓ Branch 27 → 28 taken 1 time.
✓ Branch 27 → 31 taken 4895 times.
✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 31 not taken.
4896 if (assignExprNode && assignExprNode->op == AssignExprNode::AssignOp::OP_ASSIGN)
209
1/2
✓ Branch 29 → 30 taken 1 time.
✗ Branch 29 → 73 not taken.
1 sourceFile->compilerOutput.warnings.emplace_back(node->condition->codeLoc, BOOL_ASSIGN_AS_CONDITION,
210 "If you want to compare the values, use '=='");
211
212 // Update the information, if one of the branches should be skipped.
213 // This is important to check again here, because the constness of the condition can have changed after type checking.
214
1/2
✓ Branch 31 → 32 taken 4896 times.
✗ Branch 31 → 77 not taken.
4896 const bool constantCondition = node->condition->hasCompileTimeValue(manIdx);
215
5/6
✓ Branch 32 → 33 taken 17 times.
✓ Branch 32 → 35 taken 4879 times.
✓ Branch 33 → 34 taken 17 times.
✗ Branch 33 → 77 not taken.
✓ Branch 34 → 35 taken 13 times.
✓ Branch 34 → 36 taken 4 times.
4896 node->compileThenBranch = !constantCondition || node->condition->getCompileTimeValue(manIdx).boolValue;
216
5/6
✓ Branch 37 → 38 taken 17 times.
✓ Branch 37 → 40 taken 4879 times.
✓ Branch 38 → 39 taken 17 times.
✗ Branch 38 → 77 not taken.
✓ Branch 39 → 40 taken 4 times.
✓ Branch 39 → 41 taken 13 times.
4896 node->compileElseBranch = !constantCondition || !node->condition->getCompileTimeValue(manIdx).boolValue;
217
218 // Visit body
219
2/2
✓ Branch 42 → 43 taken 4892 times.
✓ Branch 42 → 46 taken 4 times.
4896 if (node->compileThenBranch)
220
1/2
✓ Branch 43 → 44 taken 4892 times.
✗ Branch 43 → 74 not taken.
4892 visit(node->thenBody);
221
222 // Leave then body scope
223
1/2
✓ Branch 46 → 47 taken 4896 times.
✗ Branch 46 → 77 not taken.
4896 scopeHandle.leaveScopeEarly();
224
225 // Visit else statement if existing
226
4/4
✓ Branch 47 → 48 taken 4883 times.
✓ Branch 47 → 52 taken 13 times.
✓ Branch 48 → 49 taken 208 times.
✓ Branch 48 → 52 taken 4675 times.
4896 if (node->compileElseBranch && node->elseStmt)
227
1/2
✓ Branch 49 → 50 taken 208 times.
✗ Branch 49 → 75 not taken.
208 visit(node->elseStmt);
228
229
1/2
✓ Branch 52 → 53 taken 4896 times.
✗ Branch 52 → 76 not taken.
4896 return nullptr;
230 4901 }
231
232 208 std::any TypeChecker::visitElseStmt(ElseStmtNode *node) {
233 // Visit if statement in the case of an else if branch
234
2/2
✓ Branch 2 → 3 taken 63 times.
✓ Branch 2 → 7 taken 145 times.
208 if (node->isElseIf) {
235
1/2
✓ Branch 3 → 4 taken 63 times.
✗ Branch 3 → 16 not taken.
63 visit(node->ifStmt);
236
1/2
✓ Branch 5 → 6 taken 63 times.
✗ Branch 5 → 17 not taken.
63 return nullptr;
237 }
238
239 // Change to else body scope
240
2/4
✓ Branch 7 → 8 taken 145 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 145 times.
✗ Branch 8 → 18 not taken.
145 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY);
241
242 // Visit body
243
1/2
✓ Branch 10 → 11 taken 145 times.
✗ Branch 10 → 22 not taken.
145 visit(node->body);
244
245
1/2
✓ Branch 12 → 13 taken 145 times.
✗ Branch 12 → 23 not taken.
145 return nullptr;
246 145 }
247
248 12 std::any TypeChecker::visitSwitchStmt(SwitchStmtNode *node) {
249 // Check expression type
250
2/4
✓ Branch 2 → 3 taken 12 times.
✗ Branch 2 → 56 not taken.
✓ Branch 3 → 4 taken 12 times.
✗ Branch 3 → 54 not taken.
12 const QualType exprType = std::any_cast<ExprResult>(visit(node->assignExpr)).type;
251
2/6
✓ Branch 5 → 6 taken 12 times.
✗ Branch 5 → 83 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 12 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 58 not taken.
12 HANDLE_UNRESOLVED_TYPE_PTR(exprType)
252
3/4
✓ Branch 9 → 10 taken 12 times.
✗ Branch 9 → 59 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 21 taken 11 times.
12 if (!exprType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL}))
253
4/8
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 62 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 60 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 66 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 66 not taken.
3 SOFT_ERROR_ER(node->assignExpr, SWITCH_EXPR_MUST_BE_PRIMITIVE,
254 "Switch expression must be of int, short, long, byte, char or bool type")
255
256 // Visit children
257
1/2
✓ Branch 21 → 22 taken 11 times.
✗ Branch 21 → 67 not taken.
11 visitChildren(node);
258
259 // Check if case constant types match switch expression type
260
2/2
✓ Branch 49 → 25 taken 53 times.
✓ Branch 49 → 50 taken 9 times.
62 for (const CaseBranchNode *caseBranchNode : node->caseBranches)
261
2/2
✓ Branch 46 → 28 taken 70 times.
✓ Branch 46 → 47 taken 51 times.
121 for (CaseConstantNode *constantNode : caseBranchNode->caseConstants) {
262
2/4
✓ Branch 29 → 30 taken 70 times.
✗ Branch 29 → 70 not taken.
✓ Branch 30 → 31 taken 70 times.
✗ Branch 30 → 68 not taken.
70 const QualType constantType = std::any_cast<ExprResult>(visit(constantNode)).type;
263
3/4
✓ Branch 32 → 33 taken 70 times.
✗ Branch 32 → 79 not taken.
✓ Branch 33 → 34 taken 2 times.
✓ Branch 33 → 44 taken 68 times.
70 if (!constantType.matches(exprType, false, true, true))
264
4/8
✓ Branch 36 → 37 taken 2 times.
✗ Branch 36 → 74 not taken.
✓ Branch 37 → 38 taken 2 times.
✗ Branch 37 → 72 not taken.
✓ Branch 40 → 41 taken 2 times.
✗ Branch 40 → 78 not taken.
✓ Branch 41 → 42 taken 2 times.
✗ Branch 41 → 78 not taken.
6 SOFT_ERROR_ER(constantNode, SWITCH_CASE_TYPE_MISMATCH, "Case value type does not match the switch expression type")
265 }
266
267
1/2
✓ Branch 50 → 51 taken 9 times.
✗ Branch 50 → 82 not taken.
9 return nullptr;
268 }
269
270 53 std::any TypeChecker::visitCaseBranch(CaseBranchNode *node) {
271 // Change to case body scope
272
2/4
✓ Branch 2 → 3 taken 53 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 53 times.
✗ Branch 3 → 19 not taken.
53 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::CASE_BODY);
273
274 // Visit constant list
275
2/2
✓ Branch 12 → 7 taken 70 times.
✓ Branch 12 → 13 taken 53 times.
123 for (CaseConstantNode *constant : node->caseConstants)
276
1/2
✓ Branch 8 → 9 taken 70 times.
✗ Branch 8 → 23 not taken.
70 visit(constant);
277
278 // Visit body
279
1/2
✓ Branch 13 → 14 taken 53 times.
✗ Branch 13 → 25 not taken.
53 visit(node->body);
280
281
1/2
✓ Branch 15 → 16 taken 53 times.
✗ Branch 15 → 26 not taken.
106 return nullptr;
282 53 }
283
284 140 std::any TypeChecker::visitCaseConstant(CaseConstantNode *node) {
285 // If we have a normal constant, we can take the symbol type from there
286
2/2
✓ Branch 2 → 3 taken 36 times.
✓ Branch 2 → 4 taken 104 times.
140 if (node->constant)
287 36 return visit(node->constant);
288
289 // Check if a local or global variable can be found by searching for the name
290
2/2
✓ Branch 5 → 6 taken 30 times.
✓ Branch 5 → 11 taken 74 times.
104 if (node->identifierFragments.size() == 1)
291 60 node->entry = currentScope->lookup(node->identifierFragments.back());
292
293 // If no local or global was found, search in the name registry
294
2/2
✓ Branch 11 → 12 taken 37 times.
✓ Branch 11 → 24 taken 67 times.
104 if (!node->entry) {
295 37 const NameRegistryEntry *registryEntry = sourceFile->getNameRegistryEntry(node->fqIdentifier);
296
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 23 taken 37 times.
37 if (!registryEntry)
297 SOFT_ERROR_ER(node, REFERENCED_UNDEFINED_VARIABLE, "The variable '" + node->fqIdentifier + "' could not be found")
298 37 node->entry = registryEntry->targetEntry;
299 }
300
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 104 times.
104 assert(node->entry != nullptr);
301
302 // Check for the correct type
303 104 const QualType &qualType = node->entry->getQualType();
304
3/4
✓ Branch 27 → 28 taken 104 times.
✗ Branch 27 → 51 not taken.
✓ Branch 28 → 29 taken 2 times.
✓ Branch 28 → 39 taken 102 times.
104 if (!qualType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL}))
305
4/8
✓ Branch 31 → 32 taken 2 times.
✗ Branch 31 → 54 not taken.
✓ Branch 32 → 33 taken 2 times.
✗ Branch 32 → 52 not taken.
✓ Branch 35 → 36 taken 2 times.
✗ Branch 35 → 58 not taken.
✓ Branch 36 → 37 taken 2 times.
✗ Branch 36 → 58 not taken.
6 SOFT_ERROR_ER(node, CASE_CONSTANT_INVALID_TYPE, "Case constants must be of type int, short, long, byte, char, bool or enum")
306
307
2/4
✓ Branch 39 → 40 taken 102 times.
✗ Branch 39 → 59 not taken.
✓ Branch 40 → 41 taken 102 times.
✗ Branch 40 → 59 not taken.
102 return ExprResult{node->setEvaluatedSymbolType(qualType, manIdx)};
308 }
309
310 6 std::any TypeChecker::visitDefaultBranch(DefaultBranchNode *node) {
311 // Change to default body scope
312
2/4
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 11 not taken.
6 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::DEFAULT_BODY);
313
314 // Visit body
315
1/2
✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 15 not taken.
6 visit(node->body);
316
317
1/2
✓ Branch 7 → 8 taken 6 times.
✗ Branch 7 → 16 not taken.
12 return nullptr;
318 6 }
319
320 33 std::any TypeChecker::visitAnonymousBlockStmt(AnonymousBlockStmtNode *node) {
321 // Change to anonymous scope body scope
322
2/4
✓ Branch 2 → 3 taken 33 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 33 times.
✗ Branch 3 → 11 not taken.
33 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::ANONYMOUS_BLOCK_BODY);
323
324 // Visit body
325
1/2
✓ Branch 5 → 6 taken 33 times.
✗ Branch 5 → 15 not taken.
33 visit(node->body);
326
327
1/2
✓ Branch 7 → 8 taken 33 times.
✗ Branch 7 → 16 not taken.
66 return nullptr;
328 33 }
329
330 } // namespace spice::compiler
331