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 | 5784 | std::any TypeChecker::visitUnsafeBlock(UnsafeBlockNode *node) { | |
| 16 | // Change to unsafe block body scope | ||
| 17 |
2/4✓ Branch 2 → 3 taken 5784 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 5784 times.
✗ Branch 3 → 12 not taken.
|
5784 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::UNSAFE_BODY); |
| 18 | |||
| 19 | // Visit body | ||
| 20 |
1/2✓ Branch 5 → 6 taken 5784 times.
✗ Branch 5 → 16 not taken.
|
5784 | visit(node->body); |
| 21 | |||
| 22 |
1/2✓ Branch 7 → 8 taken 5784 times.
✗ Branch 7 → 17 not taken.
|
11568 | return nullptr; |
| 23 | 5784 | } | |
| 24 | |||
| 25 | 2673 | std::any TypeChecker::visitForLoop(ForLoopNode *node) { | |
| 26 | // Change to for body scope | ||
| 27 |
2/4✓ Branch 2 → 3 taken 2673 times.
✗ Branch 2 → 39 not taken.
✓ Branch 3 → 4 taken 2673 times.
✗ Branch 3 → 37 not taken.
|
2673 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOR_BODY); |
| 28 | |||
| 29 | // Visit loop variable declaration | ||
| 30 |
1/2✓ Branch 5 → 6 taken 2673 times.
✗ Branch 5 → 41 not taken.
|
2673 | visit(node->initDecl); |
| 31 | |||
| 32 | // Visit condition | ||
| 33 |
2/4✓ Branch 7 → 8 taken 2673 times.
✗ Branch 7 → 44 not taken.
✓ Branch 8 → 9 taken 2673 times.
✗ Branch 8 → 42 not taken.
|
2673 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condAssign)).type; |
| 34 |
2/6✓ Branch 10 → 11 taken 2673 times.
✗ Branch 10 → 57 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 15 taken 2673 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 46 not taken.
|
2673 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 35 | // Check if condition evaluates to bool | ||
| 36 |
3/4✓ Branch 15 → 16 taken 2673 times.
✗ Branch 15 → 57 not taken.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 27 taken 2672 times.
|
2673 | if (!conditionType.is(TY_BOOL)) |
| 37 |
4/8✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 47 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 53 not taken.
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 53 not taken.
|
4 | 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 27 → 28 taken 2672 times.
✗ Branch 27 → 54 not taken.
|
2672 | visit(node->incAssign); |
| 41 | |||
| 42 | // Visit body | ||
| 43 |
1/2✓ Branch 29 → 30 taken 2672 times.
✗ Branch 29 → 55 not taken.
|
2672 | visit(node->body); |
| 44 | |||
| 45 |
1/2✓ Branch 31 → 32 taken 2672 times.
✗ Branch 31 → 56 not taken.
|
5344 | return nullptr; |
| 46 | 2673 | } | |
| 47 | |||
| 48 | 591 | std::any TypeChecker::visitForeachLoop(ForeachLoopNode *node) { | |
| 49 | // Visit iterator assignment | ||
| 50 | 591 | ExprNode *iteratorNode = node->iteratorAssign; | |
| 51 |
2/4✓ Branch 2 → 3 taken 591 times.
✗ Branch 2 → 217 not taken.
✓ Branch 3 → 4 taken 591 times.
✗ Branch 3 → 215 not taken.
|
591 | QualType iteratorOrIterableType = std::any_cast<ExprResult>(visit(iteratorNode)).type; |
| 52 |
2/6✓ Branch 5 → 6 taken 591 times.
✗ Branch 5 → 363 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 591 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 219 not taken.
|
591 | HANDLE_UNRESOLVED_TYPE_PTR(iteratorOrIterableType) |
| 53 |
1/2✓ Branch 10 → 11 taken 591 times.
✗ Branch 10 → 220 not taken.
|
591 | iteratorOrIterableType = iteratorOrIterableType.removeReferenceWrapper(); |
| 54 | |||
| 55 | // Retrieve iterator type | ||
| 56 | 591 | QualType iteratorType = iteratorOrIterableType; | |
| 57 | |||
| 58 |
3/4✓ Branch 11 → 12 taken 591 times.
✗ Branch 11 → 363 not taken.
✓ Branch 12 → 13 taken 558 times.
✓ Branch 12 → 76 taken 33 times.
|
591 | if (iteratorOrIterableType.isIterable(node)) { |
| 59 | 558 | const QualType &iterableType = iteratorOrIterableType; | |
| 60 |
3/4✓ Branch 13 → 14 taken 558 times.
✗ Branch 13 → 363 not taken.
✓ Branch 14 → 15 taken 19 times.
✓ Branch 14 → 52 taken 539 times.
|
558 | if (iteratorOrIterableType.isArray()) { // Array |
| 61 |
2/4✓ Branch 17 → 18 taken 19 times.
✗ Branch 17 → 223 not taken.
✓ Branch 18 → 19 taken 19 times.
✗ Branch 18 → 221 not taken.
|
38 | const NameRegistryEntry *nameRegistryEntry = sourceFile->getNameRegistryEntry(ARRAY_ITERATOR_NAME); |
| 62 |
2/2✓ Branch 21 → 22 taken 1 time.
✓ Branch 21 → 31 taken 18 times.
|
19 | if (!nameRegistryEntry) { |
| 63 |
2/4✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 229 not taken.
✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 227 not taken.
|
1 | softError(node, UNKNOWN_DATATYPE, "Forgot to import \"std/iterator/array-iterator\"?"); |
| 64 |
1/2✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 233 not taken.
|
2 | return nullptr; |
| 65 | } | ||
| 66 | 18 | nameRegistryEntry->targetEntry->used = nameRegistryEntry->importEntry->used = true; | |
| 67 | 18 | Scope *matchScope = nameRegistryEntry->targetScope->parent; | |
| 68 |
1/2✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 18 times.
|
18 | assert(matchScope->type == ScopeType::GLOBAL); |
| 69 |
1/2✓ Branch 33 → 34 taken 18 times.
✗ Branch 33 → 251 not taken.
|
18 | QualType unsignedLongType(TY_LONG); |
| 70 |
1/2✓ Branch 34 → 35 taken 18 times.
✗ Branch 34 → 251 not taken.
|
18 | unsignedLongType.makeUnsigned(true); |
| 71 |
1/2✓ Branch 39 → 40 taken 18 times.
✗ Branch 39 → 234 not taken.
|
36 | const ArgList argTypes = {Arg(iterableType, false), Arg(unsignedLongType, false)}; |
| 72 |
1/2✓ Branch 41 → 42 taken 18 times.
✗ Branch 41 → 249 not taken.
|
18 | const QualType thisType(TY_DYN); |
| 73 |
2/4✓ Branch 45 → 46 taken 18 times.
✗ Branch 45 → 242 not taken.
✓ Branch 46 → 47 taken 18 times.
✗ Branch 46 → 240 not taken.
|
54 | node->getIteratorFct = FunctionManager::match(matchScope, "iterate", thisType, argTypes, {}, true, iteratorNode); |
| 74 | 18 | } else { // Struct, implementing Iterator interface | |
| 75 |
1/2✓ Branch 52 → 53 taken 539 times.
✗ Branch 52 → 363 not taken.
|
539 | Scope *matchScope = iterableType.getBodyScope(); |
| 76 |
2/4✓ Branch 57 → 58 taken 539 times.
✗ Branch 57 → 254 not taken.
✓ Branch 58 → 59 taken 539 times.
✗ Branch 58 → 252 not taken.
|
1617 | node->getIteratorFct = FunctionManager::match(matchScope, "getIterator", iterableType, {}, {}, true, iteratorNode); |
| 77 | } | ||
| 78 |
2/2✓ Branch 64 → 65 taken 1 time.
✓ Branch 64 → 73 taken 556 times.
|
557 | if (node->getIteratorFct == nullptr) |
| 79 |
2/4✓ Branch 68 → 69 taken 1 time.
✗ Branch 68 → 267 not taken.
✓ Branch 69 → 70 taken 1 time.
✗ Branch 69 → 264 not taken.
|
3 | throw SemanticError(iteratorNode, INVALID_ITERATOR, "No getIterator() function found for the given iterable type"); |
| 80 | |||
| 81 | 556 | iteratorType = QualType(node->getIteratorFct->returnType); | |
| 82 | // Add anonymous symbol to keep track of dtor call, if non-trivially destructible | ||
| 83 |
3/4✓ Branch 73 → 74 taken 556 times.
✗ Branch 73 → 363 not taken.
✓ Branch 74 → 75 taken 1 time.
✓ Branch 74 → 76 taken 555 times.
|
556 | if (!iteratorType.isTriviallyDestructible(iteratorNode)) |
| 84 |
1/2✓ Branch 75 → 76 taken 1 time.
✗ Branch 75 → 363 not taken.
|
1 | currentScope->symbolTable.insertAnonymous(iteratorType, iteratorNode); |
| 85 | } | ||
| 86 | |||
| 87 | // Change to foreach body scope | ||
| 88 |
2/4✓ Branch 76 → 77 taken 589 times.
✗ Branch 76 → 275 not taken.
✓ Branch 77 → 78 taken 589 times.
✗ Branch 77 → 273 not taken.
|
589 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOREACH_BODY); |
| 89 | |||
| 90 | // Check iterator type | ||
| 91 |
3/4✓ Branch 79 → 80 taken 589 times.
✗ Branch 79 → 361 not taken.
✓ Branch 80 → 81 taken 1 time.
✓ Branch 80 → 89 taken 588 times.
|
589 | if (!iteratorType.isIterator(node)) { |
| 92 | 1 | const std::string errMsg = | |
| 93 | "Can only iterate over arrays or data structures, inheriting from IIterator or IIterable. You provided " + | ||
| 94 |
2/4✓ Branch 81 → 82 taken 1 time.
✗ Branch 81 → 279 not taken.
✓ Branch 82 → 83 taken 1 time.
✗ Branch 82 → 277 not taken.
|
1 | iteratorType.getName(false, true); |
| 95 |
1/2✓ Branch 84 → 85 taken 1 time.
✗ Branch 84 → 281 not taken.
|
1 | softError(node->iteratorAssign, OPERATOR_WRONG_DATA_TYPE, errMsg); |
| 96 |
1/2✓ Branch 85 → 86 taken 1 time.
✗ Branch 85 → 280 not taken.
|
1 | return nullptr; |
| 97 | 1 | } | |
| 98 |
1/2✓ Branch 89 → 90 taken 588 times.
✗ Branch 89 → 361 not taken.
|
588 | const QualTypeList &iteratorTemplateTypes = iteratorType.getTemplateTypes(); |
| 99 |
2/2✓ Branch 91 → 92 taken 1 time.
✓ Branch 91 → 102 taken 587 times.
|
588 | if (iteratorTemplateTypes.empty()) |
| 100 |
4/8✓ Branch 94 → 95 taken 1 time.
✗ Branch 94 → 286 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 284 not taken.
✓ Branch 98 → 99 taken 1 time.
✗ Branch 98 → 290 not taken.
✓ Branch 99 → 100 taken 1 time.
✗ Branch 99 → 290 not taken.
|
4 | SOFT_ERROR_ER(node->iteratorAssign, INVALID_ITERATOR, |
| 101 | "Iterator has no generic arguments so that the item type could not be inferred") | ||
| 102 | |||
| 103 | 587 | const bool hasIdx = node->idxVarDecl; | |
| 104 |
2/2✓ Branch 102 → 103 taken 118 times.
✓ Branch 102 → 124 taken 469 times.
|
587 | if (hasIdx) { |
| 105 | // Visit index declaration or assignment | ||
| 106 |
2/4✓ Branch 103 → 104 taken 118 times.
✗ Branch 103 → 293 not taken.
✓ Branch 104 → 105 taken 118 times.
✗ Branch 104 → 291 not taken.
|
118 | auto indexType = std::any_cast<QualType>(visit(node->idxVarDecl)); |
| 107 |
2/6✓ Branch 106 → 107 taken 118 times.
✗ Branch 106 → 302 not taken.
✗ Branch 107 → 108 not taken.
✓ Branch 107 → 111 taken 118 times.
✗ Branch 108 → 109 not taken.
✗ Branch 108 → 294 not taken.
|
118 | HANDLE_UNRESOLVED_TYPE_PTR(indexType) |
| 108 | // Check if index type is int | ||
| 109 |
3/4✓ Branch 111 → 112 taken 118 times.
✗ Branch 111 → 302 not taken.
✓ Branch 112 → 113 taken 1 time.
✓ Branch 112 → 122 taken 117 times.
|
118 | if (!indexType.is(TY_LONG)) |
| 110 |
5/10✓ Branch 113 → 114 taken 1 time.
✗ Branch 113 → 299 not taken.
✓ Branch 114 → 115 taken 1 time.
✗ Branch 114 → 297 not taken.
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 295 not taken.
✓ Branch 118 → 119 taken 1 time.
✗ Branch 118 → 301 not taken.
✓ Branch 119 → 120 taken 1 time.
✗ Branch 119 → 301 not taken.
|
2 | 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 124 → 125 taken 586 times.
✗ Branch 124 → 361 not taken.
|
586 | Scope *matchScope = iteratorType.getBodyScope(); |
| 116 | 586 | QualType iteratorItemType; | |
| 117 |
2/2✓ Branch 125 → 126 taken 117 times.
✓ Branch 125 → 143 taken 469 times.
|
586 | if (hasIdx) { |
| 118 |
2/4✓ Branch 130 → 131 taken 117 times.
✗ Branch 130 → 305 not taken.
✓ Branch 131 → 132 taken 117 times.
✗ Branch 131 → 303 not taken.
|
351 | node->getIdxFct = FunctionManager::match(matchScope, "getIdx", iteratorType, {}, {}, false, node); |
| 119 |
1/4✗ Branch 136 → 137 not taken.
✓ Branch 136 → 140 taken 117 times.
✗ Branch 137 → 138 not taken.
✗ Branch 137 → 315 not taken.
|
117 | RETURN_NULLPTR_IF_NULLPTR(node->getIdxFct); |
| 120 |
1/2✓ Branch 140 → 141 taken 117 times.
✗ Branch 140 → 361 not taken.
|
117 | iteratorItemType = node->getIdxFct->returnType.getTemplateTypes().back(); |
| 121 | } else { | ||
| 122 |
2/4✓ Branch 147 → 148 taken 469 times.
✗ Branch 147 → 318 not taken.
✓ Branch 148 → 149 taken 469 times.
✗ Branch 148 → 316 not taken.
|
1407 | node->getFct = FunctionManager::match(matchScope, "get", iteratorType, {}, {}, false, node); |
| 123 |
1/4✗ Branch 153 → 154 not taken.
✓ Branch 153 → 157 taken 469 times.
✗ Branch 154 → 155 not taken.
✗ Branch 154 → 328 not taken.
|
469 | RETURN_NULLPTR_IF_NULLPTR(node->getFct); |
| 124 | 469 | iteratorItemType = node->getFct->returnType; | |
| 125 | } | ||
| 126 |
2/4✓ Branch 162 → 163 taken 586 times.
✗ Branch 162 → 331 not taken.
✓ Branch 163 → 164 taken 586 times.
✗ Branch 163 → 329 not taken.
|
1758 | node->isValidFct = FunctionManager::match(matchScope, "isValid", iteratorType, {}, {}, false, node); |
| 127 |
1/4✗ Branch 168 → 169 not taken.
✓ Branch 168 → 172 taken 586 times.
✗ Branch 169 → 170 not taken.
✗ Branch 169 → 341 not taken.
|
586 | RETURN_NULLPTR_IF_NULLPTR(node->isValidFct); |
| 128 |
2/4✓ Branch 176 → 177 taken 586 times.
✗ Branch 176 → 344 not taken.
✓ Branch 177 → 178 taken 586 times.
✗ Branch 177 → 342 not taken.
|
1758 | node->nextFct = FunctionManager::match(matchScope, "next", iteratorType, {}, {}, false, node); |
| 129 |
1/4✗ Branch 182 → 183 not taken.
✓ Branch 182 → 186 taken 586 times.
✗ Branch 183 → 184 not taken.
✗ Branch 183 → 354 not taken.
|
586 | RETURN_NULLPTR_IF_NULLPTR(node->nextFct); |
| 130 | |||
| 131 | // Retrieve item variable entry | ||
| 132 |
1/2✓ Branch 186 → 187 taken 586 times.
✗ Branch 186 → 361 not taken.
|
586 | SymbolTableEntry *itemVarSymbol = currentScope->lookupStrict(node->itemVarDecl->varName); |
| 133 |
1/2✗ Branch 189 → 190 not taken.
✓ Branch 189 → 191 taken 586 times.
|
586 | assert(itemVarSymbol != nullptr); |
| 134 | |||
| 135 | // Check type of the item | ||
| 136 |
2/4✓ Branch 191 → 192 taken 586 times.
✗ Branch 191 → 357 not taken.
✓ Branch 192 → 193 taken 586 times.
✗ Branch 192 → 355 not taken.
|
586 | auto itemType = std::any_cast<QualType>(visit(node->itemVarDecl)); |
| 137 |
2/6✓ Branch 194 → 195 taken 586 times.
✗ Branch 194 → 361 not taken.
✗ Branch 195 → 196 not taken.
✓ Branch 195 → 199 taken 586 times.
✗ Branch 196 → 197 not taken.
✗ Branch 196 → 358 not taken.
|
586 | HANDLE_UNRESOLVED_TYPE_PTR(itemType) |
| 138 |
3/4✓ Branch 199 → 200 taken 586 times.
✗ Branch 199 → 361 not taken.
✓ Branch 200 → 201 taken 5 times.
✓ Branch 200 → 203 taken 581 times.
|
586 | if (itemType.is(TY_DYN)) { // Perform type inference |
| 139 | // Update evaluated symbol type of the declaration data type | ||
| 140 |
1/2✓ Branch 201 → 202 taken 5 times.
✗ Branch 201 → 361 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 | 586 | const ExprResult itemResult = {itemType, itemVarSymbol}; | |
| 147 | 586 | const ExprResult iteratorItemResult = {iteratorItemType, nullptr /* always a temporary */}; | |
| 148 | 585 | const auto [_, copyCtor] = | |
| 149 |
2/2✓ Branch 203 → 204 taken 585 times.
✓ Branch 203 → 361 taken 1 time.
|
586 | opRuleManager.getAssignResultType(node->itemVarDecl, itemResult, iteratorItemResult, true, false, ERROR_FOREACH_ITEM); |
| 150 | 585 | node->calledItemCopyCtor = copyCtor; | |
| 151 | |||
| 152 | // Update type of item | ||
| 153 |
1/2✓ Branch 206 → 207 taken 585 times.
✗ Branch 206 → 361 not taken.
|
585 | itemVarSymbol->updateType(itemType, true); |
| 154 | |||
| 155 | // Visit body | ||
| 156 |
1/2✓ Branch 207 → 208 taken 585 times.
✗ Branch 207 → 359 not taken.
|
585 | visit(node->body); |
| 157 | |||
| 158 |
1/2✓ Branch 209 → 210 taken 585 times.
✗ Branch 209 → 360 not taken.
|
1170 | return nullptr; |
| 159 | 589 | } | |
| 160 | |||
| 161 | 1606 | std::any TypeChecker::visitWhileLoop(WhileLoopNode *node) { | |
| 162 | // Change to while body scope | ||
| 163 |
2/4✓ Branch 2 → 3 taken 1606 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 1606 times.
✗ Branch 3 → 33 not taken.
|
1606 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY); |
| 164 | |||
| 165 | // Visit condition | ||
| 166 |
3/4✓ Branch 5 → 6 taken 1605 times.
✓ Branch 5 → 39 taken 1 time.
✓ Branch 6 → 7 taken 1605 times.
✗ Branch 6 → 37 not taken.
|
1606 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 167 |
2/6✓ Branch 8 → 9 taken 1605 times.
✗ Branch 8 → 51 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 13 taken 1605 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 41 not taken.
|
1605 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 168 | // Check if condition evaluates to bool | ||
| 169 |
3/4✓ Branch 13 → 14 taken 1605 times.
✗ Branch 13 → 51 not taken.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 25 taken 1604 times.
|
1605 | if (!conditionType.is(TY_BOOL)) |
| 170 |
4/8✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 44 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 42 not taken.
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 48 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 48 not taken.
|
4 | 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 25 → 26 taken 1604 times.
✗ Branch 25 → 49 not taken.
|
1604 | visit(node->body); |
| 174 | |||
| 175 |
1/2✓ Branch 27 → 28 taken 1604 times.
✗ Branch 27 → 50 not taken.
|
3208 | return nullptr; |
| 176 | 1606 | } | |
| 177 | |||
| 178 | 16 | std::any TypeChecker::visitDoWhileLoop(DoWhileLoopNode *node) { | |
| 179 | // Change to while body scope | ||
| 180 |
2/4✓ Branch 2 → 3 taken 16 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 16 times.
✗ Branch 3 → 33 not taken.
|
16 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY); |
| 181 | |||
| 182 | // Visit body | ||
| 183 |
1/2✓ Branch 5 → 6 taken 16 times.
✗ Branch 5 → 37 not taken.
|
16 | visit(node->body); |
| 184 | |||
| 185 | // Visit condition | ||
| 186 |
2/4✓ Branch 7 → 8 taken 16 times.
✗ Branch 7 → 40 not taken.
✓ Branch 8 → 9 taken 16 times.
✗ Branch 8 → 38 not taken.
|
16 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 187 |
2/6✓ Branch 10 → 11 taken 16 times.
✗ Branch 10 → 51 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 15 taken 16 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 42 not taken.
|
16 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 188 | // Check if condition evaluates to bool | ||
| 189 |
3/4✓ Branch 15 → 16 taken 16 times.
✗ Branch 15 → 51 not taken.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 27 taken 15 times.
|
16 | if (!conditionType.is(TY_BOOL)) |
| 190 |
4/8✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 45 not taken.
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 43 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 49 not taken.
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 49 not taken.
|
4 | SOFT_ERROR_ER(node->condition, CONDITION_MUST_BE_BOOL, "Do-While loop condition must be of type bool") |
| 191 | |||
| 192 |
1/2✓ Branch 27 → 28 taken 15 times.
✗ Branch 27 → 50 not taken.
|
30 | return nullptr; |
| 193 | 16 | } | |
| 194 | |||
| 195 | 12186 | std::any TypeChecker::visitIfStmt(IfStmtNode *node) { | |
| 196 | // Change to then body scope | ||
| 197 |
2/4✓ Branch 2 → 3 taken 12186 times.
✗ Branch 2 → 72 not taken.
✓ Branch 3 → 4 taken 12186 times.
✗ Branch 3 → 70 not taken.
|
12186 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY); |
| 198 | |||
| 199 | // Visit condition | ||
| 200 |
3/4✓ Branch 5 → 6 taken 12184 times.
✓ Branch 5 → 76 taken 2 times.
✓ Branch 6 → 7 taken 12184 times.
✗ Branch 6 → 74 not taken.
|
12186 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 201 |
4/6✓ Branch 8 → 9 taken 12184 times.
✗ Branch 8 → 90 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 13 taken 12183 times.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 78 not taken.
|
12185 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 202 | // Check if condition evaluates to bool | ||
| 203 |
3/4✓ Branch 13 → 14 taken 12183 times.
✗ Branch 13 → 90 not taken.
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 25 taken 12181 times.
|
12183 | if (!conditionType.is(TY_BOOL)) |
| 204 |
4/8✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 81 not taken.
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 79 not taken.
✓ Branch 21 → 22 taken 2 times.
✗ Branch 21 → 85 not taken.
✓ Branch 22 → 23 taken 2 times.
✗ Branch 22 → 85 not taken.
|
8 | 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 25 → 26 taken 12181 times.
✗ Branch 25 → 27 not taken.
|
12181 | const auto *assignExprNode = dynamic_cast<AssignExprNode *>(node->condition); |
| 208 |
3/4✓ Branch 28 → 29 taken 1 time.
✓ Branch 28 → 32 taken 12180 times.
✓ Branch 29 → 30 taken 1 time.
✗ Branch 29 → 32 not taken.
|
12181 | if (assignExprNode && assignExprNode->op == AssignExprNode::AssignOp::OP_ASSIGN) |
| 209 |
1/2✓ Branch 30 → 31 taken 1 time.
✗ Branch 30 → 86 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 32 → 33 taken 12181 times.
✗ Branch 32 → 90 not taken.
|
12181 | const bool constantCondition = node->condition->hasCompileTimeValue(manIdx); |
| 215 |
5/6✓ Branch 33 → 34 taken 86 times.
✓ Branch 33 → 36 taken 12095 times.
✓ Branch 34 → 35 taken 86 times.
✗ Branch 34 → 90 not taken.
✓ Branch 35 → 36 taken 62 times.
✓ Branch 35 → 37 taken 24 times.
|
12181 | node->compileThenBranch[manIdx] = !constantCondition || node->condition->getCompileTimeValue(manIdx).boolValue; |
| 216 |
5/6✓ Branch 40 → 41 taken 86 times.
✓ Branch 40 → 43 taken 12095 times.
✓ Branch 41 → 42 taken 86 times.
✗ Branch 41 → 90 not taken.
✓ Branch 42 → 43 taken 24 times.
✓ Branch 42 → 44 taken 62 times.
|
12181 | node->compileElseBranch[manIdx] = !constantCondition || !node->condition->getCompileTimeValue(manIdx).boolValue; |
| 217 | |||
| 218 | // Visit body | ||
| 219 |
2/2✓ Branch 49 → 50 taken 12157 times.
✓ Branch 49 → 53 taken 24 times.
|
12181 | if (node->compileThenBranch[manIdx]) |
| 220 |
1/2✓ Branch 50 → 51 taken 12157 times.
✗ Branch 50 → 87 not taken.
|
12157 | visit(node->thenBody); |
| 221 | |||
| 222 | // Leave then body scope | ||
| 223 |
1/2✓ Branch 53 → 54 taken 12181 times.
✗ Branch 53 → 90 not taken.
|
12181 | scopeHandle.leaveScopeEarly(); |
| 224 | |||
| 225 | // Visit else statement if existing | ||
| 226 |
6/6✓ Branch 56 → 57 taken 12119 times.
✓ Branch 56 → 59 taken 62 times.
✓ Branch 57 → 58 taken 864 times.
✓ Branch 57 → 59 taken 11255 times.
✓ Branch 60 → 61 taken 864 times.
✓ Branch 60 → 64 taken 11317 times.
|
12181 | if (node->compileElseBranch[manIdx] && node->elseStmt) |
| 227 |
1/2✓ Branch 61 → 62 taken 864 times.
✗ Branch 61 → 88 not taken.
|
864 | visit(node->elseStmt); |
| 228 | |||
| 229 |
1/2✓ Branch 64 → 65 taken 12181 times.
✗ Branch 64 → 89 not taken.
|
24362 | return nullptr; |
| 230 | 12186 | } | |
| 231 | |||
| 232 | 864 | 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 339 times.
✓ Branch 2 → 8 taken 525 times.
|
864 | if (node->isElseIf) { |
| 235 |
1/2✓ Branch 3 → 4 taken 339 times.
✗ Branch 3 → 18 not taken.
|
339 | visit(node->ifStmt); |
| 236 |
1/2✓ Branch 5 → 6 taken 339 times.
✗ Branch 5 → 19 not taken.
|
678 | return nullptr; |
| 237 | } | ||
| 238 | |||
| 239 | // Change to else body scope | ||
| 240 |
2/4✓ Branch 8 → 9 taken 525 times.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 525 times.
✗ Branch 9 → 20 not taken.
|
525 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY); |
| 241 | |||
| 242 | // Visit body | ||
| 243 |
1/2✓ Branch 11 → 12 taken 525 times.
✗ Branch 11 → 24 not taken.
|
525 | visit(node->body); |
| 244 | |||
| 245 |
1/2✓ Branch 13 → 14 taken 525 times.
✗ Branch 13 → 25 not taken.
|
525 | return nullptr; |
| 246 | 525 | } | |
| 247 | |||
| 248 | 85 | std::any TypeChecker::visitSwitchStmt(SwitchStmtNode *node) { | |
| 249 | // Check expression type | ||
| 250 |
2/4✓ Branch 2 → 3 taken 85 times.
✗ Branch 2 → 74 not taken.
✓ Branch 3 → 4 taken 85 times.
✗ Branch 3 → 72 not taken.
|
85 | const QualType exprType = std::any_cast<ExprResult>(visit(node->assignExpr)).type; |
| 251 |
2/6✓ Branch 5 → 6 taken 85 times.
✗ Branch 5 → 101 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 85 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 76 not taken.
|
85 | HANDLE_UNRESOLVED_TYPE_PTR(exprType) |
| 252 |
3/4✓ Branch 10 → 11 taken 85 times.
✗ Branch 10 → 77 not taken.
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 22 taken 84 times.
|
85 | if (!exprType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL})) |
| 253 |
4/8✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 80 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 78 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 84 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 84 not taken.
|
4 | 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 22 → 23 taken 84 times.
✗ Branch 22 → 85 not taken.
|
84 | visitChildren(node); |
| 258 | |||
| 259 | // Check if case constant types match switch expression type | ||
| 260 |
2/2✓ Branch 66 → 26 taken 662 times.
✓ Branch 66 → 67 taken 82 times.
|
828 | for (const CaseBranchNode *caseBranchNode : node->caseBranches) |
| 261 |
2/2✓ Branch 56 → 30 taken 847 times.
✓ Branch 56 → 57 taken 660 times.
|
2169 | for (CaseConstantNode *constantNode : caseBranchNode->caseConstants) { |
| 262 |
2/4✓ Branch 32 → 33 taken 847 times.
✗ Branch 32 → 88 not taken.
✓ Branch 33 → 34 taken 847 times.
✗ Branch 33 → 86 not taken.
|
847 | const QualType constantType = std::any_cast<ExprResult>(visit(constantNode)).type; |
| 263 |
3/4✓ Branch 35 → 36 taken 847 times.
✗ Branch 35 → 97 not taken.
✓ Branch 36 → 37 taken 2 times.
✓ Branch 36 → 47 taken 845 times.
|
847 | if (!constantType.matches(exprType, false, true, true)) |
| 264 |
4/8✓ Branch 39 → 40 taken 2 times.
✗ Branch 39 → 92 not taken.
✓ Branch 40 → 41 taken 2 times.
✗ Branch 40 → 90 not taken.
✓ Branch 43 → 44 taken 2 times.
✗ Branch 43 → 96 not taken.
✓ Branch 44 → 45 taken 2 times.
✗ Branch 44 → 96 not taken.
|
8 | SOFT_ERROR_ER(constantNode, SWITCH_CASE_TYPE_MISMATCH, "Case value type does not match the switch expression type") |
| 265 | } | ||
| 266 | |||
| 267 |
1/2✓ Branch 67 → 68 taken 82 times.
✗ Branch 67 → 100 not taken.
|
164 | return nullptr; |
| 268 | } | ||
| 269 | |||
| 270 | 662 | std::any TypeChecker::visitCaseBranch(CaseBranchNode *node) { | |
| 271 | // Change to case body scope | ||
| 272 |
2/4✓ Branch 2 → 3 taken 662 times.
✗ Branch 2 → 30 not taken.
✓ Branch 3 → 4 taken 662 times.
✗ Branch 3 → 28 not taken.
|
662 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::CASE_BODY); |
| 273 | |||
| 274 | // Visit constant list | ||
| 275 |
2/2✓ Branch 20 → 7 taken 847 times.
✓ Branch 20 → 21 taken 662 times.
|
2171 | for (CaseConstantNode *constant : node->caseConstants) |
| 276 |
1/2✓ Branch 9 → 10 taken 847 times.
✗ Branch 9 → 32 not taken.
|
847 | visit(constant); |
| 277 | |||
| 278 | // Visit body | ||
| 279 |
1/2✓ Branch 21 → 22 taken 662 times.
✗ Branch 21 → 34 not taken.
|
662 | visit(node->body); |
| 280 | |||
| 281 |
1/2✓ Branch 23 → 24 taken 662 times.
✗ Branch 23 → 35 not taken.
|
1324 | return nullptr; |
| 282 | 662 | } | |
| 283 | |||
| 284 | 1694 | 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 1658 times.
|
1694 | 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 254 times.
✓ Branch 5 → 11 taken 1404 times.
|
1658 | if (node->identifierFragments.size() == 1) |
| 291 | 508 | 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 702 times.
✓ Branch 11 → 24 taken 956 times.
|
1658 | if (!node->entry) { |
| 295 | 702 | const NameRegistryEntry *registryEntry = sourceFile->getNameRegistryEntry(node->fqIdentifier); | |
| 296 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 23 taken 702 times.
|
702 | if (!registryEntry) |
| 297 | ✗ | SOFT_ERROR_ER(node, REFERENCED_UNDEFINED_VARIABLE, "The variable '" + node->fqIdentifier + "' could not be found") | |
| 298 | 702 | node->entry = registryEntry->targetEntry; | |
| 299 | } | ||
| 300 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 1658 times.
|
1658 | assert(node->entry != nullptr); |
| 301 | |||
| 302 | // Check for the correct type | ||
| 303 | 1658 | const QualType &qualType = node->entry->getQualType(); | |
| 304 |
3/4✓ Branch 27 → 28 taken 1658 times.
✗ Branch 27 → 51 not taken.
✓ Branch 28 → 29 taken 2 times.
✓ Branch 28 → 39 taken 1656 times.
|
1658 | 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.
|
8 | 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 1656 times.
✗ Branch 39 → 59 not taken.
✓ Branch 40 → 41 taken 1656 times.
✗ Branch 40 → 59 not taken.
|
3312 | return ExprResult{node->setEvaluatedSymbolType(qualType, manIdx)}; |
| 308 | } | ||
| 309 | |||
| 310 | 69 | std::any TypeChecker::visitDefaultBranch(DefaultBranchNode *node) { | |
| 311 | // Change to default body scope | ||
| 312 |
2/4✓ Branch 2 → 3 taken 69 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 69 times.
✗ Branch 3 → 12 not taken.
|
69 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::DEFAULT_BODY); |
| 313 | |||
| 314 | // Visit body | ||
| 315 |
1/2✓ Branch 5 → 6 taken 69 times.
✗ Branch 5 → 16 not taken.
|
69 | visit(node->body); |
| 316 | |||
| 317 |
1/2✓ Branch 7 → 8 taken 69 times.
✗ Branch 7 → 17 not taken.
|
138 | return nullptr; |
| 318 | 69 | } | |
| 319 | |||
| 320 | 35 | std::any TypeChecker::visitAnonymousBlockStmt(AnonymousBlockStmtNode *node) { | |
| 321 | // Change to anonymous scope body scope | ||
| 322 |
2/4✓ Branch 2 → 3 taken 35 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 35 times.
✗ Branch 3 → 12 not taken.
|
35 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::ANONYMOUS_BLOCK_BODY); |
| 323 | |||
| 324 | // Visit body | ||
| 325 |
1/2✓ Branch 5 → 6 taken 35 times.
✗ Branch 5 → 16 not taken.
|
35 | visit(node->body); |
| 326 | |||
| 327 |
1/2✓ Branch 7 → 8 taken 35 times.
✗ Branch 7 → 17 not taken.
|
70 | return nullptr; |
| 328 | 35 | } | |
| 329 | |||
| 330 | } // namespace spice::compiler | ||
| 331 |