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 | 20254 | std::any TypeChecker::visitUnsafeBlock(UnsafeBlockNode *node) { | |
| 16 | // Change to unsafe block body scope | ||
| 17 |
2/4✓ Branch 2 → 3 taken 20254 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 20254 times.
✗ Branch 3 → 12 not taken.
|
20254 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::UNSAFE_BODY); |
| 18 | |||
| 19 | // Visit body | ||
| 20 |
1/2✓ Branch 5 → 6 taken 20254 times.
✗ Branch 5 → 16 not taken.
|
20254 | visit(node->body); |
| 21 | |||
| 22 |
1/2✓ Branch 7 → 8 taken 20254 times.
✗ Branch 7 → 17 not taken.
|
40508 | return nullptr; |
| 23 | 20254 | } | |
| 24 | |||
| 25 | 8109 | std::any TypeChecker::visitForLoop(ForLoopNode *node) { | |
| 26 | // Change to for body scope | ||
| 27 |
2/4✓ Branch 2 → 3 taken 8109 times.
✗ Branch 2 → 39 not taken.
✓ Branch 3 → 4 taken 8109 times.
✗ Branch 3 → 37 not taken.
|
8109 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOR_BODY); |
| 28 | |||
| 29 | // Visit loop variable declaration | ||
| 30 |
1/2✓ Branch 5 → 6 taken 8109 times.
✗ Branch 5 → 41 not taken.
|
8109 | visit(node->initDecl); |
| 31 | |||
| 32 | // Visit condition | ||
| 33 |
2/4✓ Branch 7 → 8 taken 8109 times.
✗ Branch 7 → 44 not taken.
✓ Branch 8 → 9 taken 8109 times.
✗ Branch 8 → 42 not taken.
|
8109 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condAssign)).type; |
| 34 |
2/6✓ Branch 10 → 11 taken 8109 times.
✗ Branch 10 → 57 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 15 taken 8109 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 46 not taken.
|
8109 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 35 | // Check if condition evaluates to bool | ||
| 36 |
3/4✓ Branch 15 → 16 taken 8109 times.
✗ Branch 15 → 57 not taken.
✓ Branch 16 → 17 taken 2 times.
✓ Branch 16 → 27 taken 8107 times.
|
8109 | if (!conditionType.is(TY_BOOL)) |
| 37 |
4/8✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 2 times.
✗ Branch 20 → 47 not taken.
✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 53 not taken.
✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 53 not taken.
|
8 | 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 8107 times.
✗ Branch 27 → 54 not taken.
|
8107 | visit(node->incAssign); |
| 41 | |||
| 42 | // Visit body | ||
| 43 |
1/2✓ Branch 29 → 30 taken 8107 times.
✗ Branch 29 → 55 not taken.
|
8107 | visit(node->body); |
| 44 | |||
| 45 |
1/2✓ Branch 31 → 32 taken 8107 times.
✗ Branch 31 → 56 not taken.
|
16214 | return nullptr; |
| 46 | 8109 | } | |
| 47 | |||
| 48 | 968 | std::any TypeChecker::visitForeachLoop(ForeachLoopNode *node) { | |
| 49 | // Visit iterator assignment | ||
| 50 | 968 | ExprNode *iteratorNode = node->iteratorAssign; | |
| 51 |
2/4✓ Branch 2 → 3 taken 968 times.
✗ Branch 2 → 217 not taken.
✓ Branch 3 → 4 taken 968 times.
✗ Branch 3 → 215 not taken.
|
968 | QualType iteratorOrIterableType = std::any_cast<ExprResult>(visit(iteratorNode)).type; |
| 52 |
2/6✓ Branch 5 → 6 taken 968 times.
✗ Branch 5 → 363 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 968 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 219 not taken.
|
968 | HANDLE_UNRESOLVED_TYPE_PTR(iteratorOrIterableType) |
| 53 |
1/2✓ Branch 10 → 11 taken 968 times.
✗ Branch 10 → 220 not taken.
|
968 | iteratorOrIterableType = iteratorOrIterableType.removeReferenceWrapper(); |
| 54 | |||
| 55 | // Retrieve iterator type | ||
| 56 | 968 | QualType iteratorType = iteratorOrIterableType; | |
| 57 | |||
| 58 |
3/4✓ Branch 11 → 12 taken 968 times.
✗ Branch 11 → 363 not taken.
✓ Branch 12 → 13 taken 902 times.
✓ Branch 12 → 76 taken 66 times.
|
968 | if (iteratorOrIterableType.isIterable(node)) { |
| 59 | 902 | const QualType &iterableType = iteratorOrIterableType; | |
| 60 |
3/4✓ Branch 13 → 14 taken 902 times.
✗ Branch 13 → 363 not taken.
✓ Branch 14 → 15 taken 38 times.
✓ Branch 14 → 52 taken 864 times.
|
902 | if (iteratorOrIterableType.isArray()) { // Array |
| 61 |
2/4✓ Branch 17 → 18 taken 38 times.
✗ Branch 17 → 223 not taken.
✓ Branch 18 → 19 taken 38 times.
✗ Branch 18 → 221 not taken.
|
76 | const NameRegistryEntry *nameRegistryEntry = sourceFile->getNameRegistryEntry(ARRAY_ITERATOR_NAME); |
| 62 |
2/2✓ Branch 21 → 22 taken 2 times.
✓ Branch 21 → 31 taken 36 times.
|
38 | if (!nameRegistryEntry) { |
| 63 |
2/4✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 229 not taken.
✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 227 not taken.
|
2 | softError(node, UNKNOWN_DATATYPE, "Forgot to import \"std/iterator/array-iterator\"?"); |
| 64 |
1/2✓ Branch 28 → 29 taken 2 times.
✗ Branch 28 → 233 not taken.
|
4 | return nullptr; |
| 65 | } | ||
| 66 | 36 | nameRegistryEntry->targetEntry->used = nameRegistryEntry->importEntry->used = true; | |
| 67 | 36 | Scope *matchScope = nameRegistryEntry->targetScope->parent; | |
| 68 |
1/2✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 36 times.
|
36 | assert(matchScope->type == ScopeType::GLOBAL); |
| 69 |
1/2✓ Branch 33 → 34 taken 36 times.
✗ Branch 33 → 251 not taken.
|
36 | QualType unsignedLongType(TY_LONG); |
| 70 |
1/2✓ Branch 34 → 35 taken 36 times.
✗ Branch 34 → 251 not taken.
|
36 | unsignedLongType.makeUnsigned(true); |
| 71 |
1/2✓ Branch 39 → 40 taken 36 times.
✗ Branch 39 → 234 not taken.
|
72 | const ArgList argTypes = {Arg(iterableType, false), Arg(unsignedLongType, false)}; |
| 72 |
1/2✓ Branch 41 → 42 taken 36 times.
✗ Branch 41 → 249 not taken.
|
36 | const QualType thisType(TY_DYN); |
| 73 |
2/4✓ Branch 45 → 46 taken 36 times.
✗ Branch 45 → 242 not taken.
✓ Branch 46 → 47 taken 36 times.
✗ Branch 46 → 240 not taken.
|
108 | node->getIteratorFct = FunctionManager::match(matchScope, "iterate", thisType, argTypes, {}, true, iteratorNode); |
| 74 | 36 | } else { // Struct, implementing Iterator interface | |
| 75 |
1/2✓ Branch 52 → 53 taken 864 times.
✗ Branch 52 → 363 not taken.
|
864 | Scope *matchScope = iterableType.getBodyScope(); |
| 76 |
2/4✓ Branch 57 → 58 taken 864 times.
✗ Branch 57 → 254 not taken.
✓ Branch 58 → 59 taken 864 times.
✗ Branch 58 → 252 not taken.
|
2592 | node->getIteratorFct = FunctionManager::match(matchScope, "getIterator", iterableType, {}, {}, true, iteratorNode); |
| 77 | } | ||
| 78 |
2/2✓ Branch 64 → 65 taken 2 times.
✓ Branch 64 → 73 taken 898 times.
|
900 | if (node->getIteratorFct == nullptr) |
| 79 |
2/4✓ Branch 68 → 69 taken 2 times.
✗ Branch 68 → 267 not taken.
✓ Branch 69 → 70 taken 2 times.
✗ Branch 69 → 264 not taken.
|
6 | throw SemanticError(iteratorNode, INVALID_ITERATOR, "No getIterator() function found for the given iterable type"); |
| 80 | |||
| 81 | 898 | 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 898 times.
✗ Branch 73 → 363 not taken.
✓ Branch 74 → 75 taken 2 times.
✓ Branch 74 → 76 taken 896 times.
|
898 | if (!iteratorType.isTriviallyDestructible(iteratorNode)) |
| 84 |
1/2✓ Branch 75 → 76 taken 2 times.
✗ Branch 75 → 363 not taken.
|
2 | currentScope->symbolTable.insertAnonymous(iteratorType, iteratorNode); |
| 85 | } | ||
| 86 | |||
| 87 | // Change to foreach body scope | ||
| 88 |
2/4✓ Branch 76 → 77 taken 964 times.
✗ Branch 76 → 275 not taken.
✓ Branch 77 → 78 taken 964 times.
✗ Branch 77 → 273 not taken.
|
964 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOREACH_BODY); |
| 89 | |||
| 90 | // Check iterator type | ||
| 91 |
3/4✓ Branch 79 → 80 taken 964 times.
✗ Branch 79 → 361 not taken.
✓ Branch 80 → 81 taken 2 times.
✓ Branch 80 → 89 taken 962 times.
|
964 | if (!iteratorType.isIterator(node)) { |
| 92 | 2 | 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 2 times.
✗ Branch 81 → 279 not taken.
✓ Branch 82 → 83 taken 2 times.
✗ Branch 82 → 277 not taken.
|
2 | iteratorType.getName(false, true); |
| 95 |
1/2✓ Branch 84 → 85 taken 2 times.
✗ Branch 84 → 281 not taken.
|
2 | softError(node->iteratorAssign, OPERATOR_WRONG_DATA_TYPE, errMsg); |
| 96 |
1/2✓ Branch 85 → 86 taken 2 times.
✗ Branch 85 → 280 not taken.
|
2 | return nullptr; |
| 97 | 2 | } | |
| 98 |
1/2✓ Branch 89 → 90 taken 962 times.
✗ Branch 89 → 361 not taken.
|
962 | const QualTypeList &iteratorTemplateTypes = iteratorType.getTemplateTypes(); |
| 99 |
2/2✓ Branch 91 → 92 taken 2 times.
✓ Branch 91 → 102 taken 960 times.
|
962 | if (iteratorTemplateTypes.empty()) |
| 100 |
4/8✓ Branch 94 → 95 taken 2 times.
✗ Branch 94 → 286 not taken.
✓ Branch 95 → 96 taken 2 times.
✗ Branch 95 → 284 not taken.
✓ Branch 98 → 99 taken 2 times.
✗ Branch 98 → 290 not taken.
✓ Branch 99 → 100 taken 2 times.
✗ Branch 99 → 290 not taken.
|
8 | SOFT_ERROR_ER(node->iteratorAssign, INVALID_ITERATOR, |
| 101 | "Iterator has no generic arguments so that the item type could not be inferred") | ||
| 102 | |||
| 103 | 960 | const bool hasIdx = node->idxVarDecl; | |
| 104 |
2/2✓ Branch 102 → 103 taken 236 times.
✓ Branch 102 → 124 taken 724 times.
|
960 | if (hasIdx) { |
| 105 | // Visit index declaration or assignment | ||
| 106 |
2/4✓ Branch 103 → 104 taken 236 times.
✗ Branch 103 → 293 not taken.
✓ Branch 104 → 105 taken 236 times.
✗ Branch 104 → 291 not taken.
|
236 | auto indexType = std::any_cast<QualType>(visit(node->idxVarDecl)); |
| 107 |
2/6✓ Branch 106 → 107 taken 236 times.
✗ Branch 106 → 302 not taken.
✗ Branch 107 → 108 not taken.
✓ Branch 107 → 111 taken 236 times.
✗ Branch 108 → 109 not taken.
✗ Branch 108 → 294 not taken.
|
236 | HANDLE_UNRESOLVED_TYPE_PTR(indexType) |
| 108 | // Check if index type is int | ||
| 109 |
3/4✓ Branch 111 → 112 taken 236 times.
✗ Branch 111 → 302 not taken.
✓ Branch 112 → 113 taken 2 times.
✓ Branch 112 → 122 taken 234 times.
|
236 | if (!indexType.is(TY_LONG)) |
| 110 |
5/10✓ Branch 113 → 114 taken 2 times.
✗ Branch 113 → 299 not taken.
✓ Branch 114 → 115 taken 2 times.
✗ Branch 114 → 297 not taken.
✓ Branch 115 → 116 taken 2 times.
✗ Branch 115 → 295 not taken.
✓ Branch 118 → 119 taken 2 times.
✗ Branch 118 → 301 not taken.
✓ Branch 119 → 120 taken 2 times.
✗ Branch 119 → 301 not taken.
|
4 | 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 958 times.
✗ Branch 124 → 361 not taken.
|
958 | Scope *matchScope = iteratorType.getBodyScope(); |
| 116 | 958 | QualType iteratorItemType; | |
| 117 |
2/2✓ Branch 125 → 126 taken 234 times.
✓ Branch 125 → 143 taken 724 times.
|
958 | if (hasIdx) { |
| 118 |
2/4✓ Branch 130 → 131 taken 234 times.
✗ Branch 130 → 305 not taken.
✓ Branch 131 → 132 taken 234 times.
✗ Branch 131 → 303 not taken.
|
702 | node->getIdxFct = FunctionManager::match(matchScope, "getIdx", iteratorType, {}, {}, false, node); |
| 119 |
1/4✗ Branch 136 → 137 not taken.
✓ Branch 136 → 140 taken 234 times.
✗ Branch 137 → 138 not taken.
✗ Branch 137 → 315 not taken.
|
234 | RETURN_NULLPTR_IF_NULLPTR(node->getIdxFct); |
| 120 |
1/2✓ Branch 140 → 141 taken 234 times.
✗ Branch 140 → 361 not taken.
|
234 | iteratorItemType = node->getIdxFct->returnType.getTemplateTypes().back(); |
| 121 | } else { | ||
| 122 |
2/4✓ Branch 147 → 148 taken 724 times.
✗ Branch 147 → 318 not taken.
✓ Branch 148 → 149 taken 724 times.
✗ Branch 148 → 316 not taken.
|
2172 | node->getFct = FunctionManager::match(matchScope, "get", iteratorType, {}, {}, false, node); |
| 123 |
1/4✗ Branch 153 → 154 not taken.
✓ Branch 153 → 157 taken 724 times.
✗ Branch 154 → 155 not taken.
✗ Branch 154 → 328 not taken.
|
724 | RETURN_NULLPTR_IF_NULLPTR(node->getFct); |
| 124 | 724 | iteratorItemType = node->getFct->returnType; | |
| 125 | } | ||
| 126 |
2/4✓ Branch 162 → 163 taken 958 times.
✗ Branch 162 → 331 not taken.
✓ Branch 163 → 164 taken 958 times.
✗ Branch 163 → 329 not taken.
|
2874 | node->isValidFct = FunctionManager::match(matchScope, "isValid", iteratorType, {}, {}, false, node); |
| 127 |
1/4✗ Branch 168 → 169 not taken.
✓ Branch 168 → 172 taken 958 times.
✗ Branch 169 → 170 not taken.
✗ Branch 169 → 341 not taken.
|
958 | RETURN_NULLPTR_IF_NULLPTR(node->isValidFct); |
| 128 |
2/4✓ Branch 176 → 177 taken 958 times.
✗ Branch 176 → 344 not taken.
✓ Branch 177 → 178 taken 958 times.
✗ Branch 177 → 342 not taken.
|
2874 | node->nextFct = FunctionManager::match(matchScope, "next", iteratorType, {}, {}, false, node); |
| 129 |
1/4✗ Branch 182 → 183 not taken.
✓ Branch 182 → 186 taken 958 times.
✗ Branch 183 → 184 not taken.
✗ Branch 183 → 354 not taken.
|
958 | RETURN_NULLPTR_IF_NULLPTR(node->nextFct); |
| 130 | |||
| 131 | // Retrieve item variable entry | ||
| 132 |
1/2✓ Branch 186 → 187 taken 958 times.
✗ Branch 186 → 361 not taken.
|
958 | SymbolTableEntry *itemVarSymbol = currentScope->lookupStrict(node->itemVarDecl->varName); |
| 133 |
1/2✗ Branch 189 → 190 not taken.
✓ Branch 189 → 191 taken 958 times.
|
958 | assert(itemVarSymbol != nullptr); |
| 134 | |||
| 135 | // Check type of the item | ||
| 136 |
2/4✓ Branch 191 → 192 taken 958 times.
✗ Branch 191 → 357 not taken.
✓ Branch 192 → 193 taken 958 times.
✗ Branch 192 → 355 not taken.
|
958 | auto itemType = std::any_cast<QualType>(visit(node->itemVarDecl)); |
| 137 |
2/6✓ Branch 194 → 195 taken 958 times.
✗ Branch 194 → 361 not taken.
✗ Branch 195 → 196 not taken.
✓ Branch 195 → 199 taken 958 times.
✗ Branch 196 → 197 not taken.
✗ Branch 196 → 358 not taken.
|
958 | HANDLE_UNRESOLVED_TYPE_PTR(itemType) |
| 138 |
3/4✓ Branch 199 → 200 taken 958 times.
✗ Branch 199 → 361 not taken.
✓ Branch 200 → 201 taken 10 times.
✓ Branch 200 → 203 taken 948 times.
|
958 | 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 10 times.
✗ Branch 201 → 361 not taken.
|
10 | node->itemVarDecl->dataType->setEvaluatedSymbolType(iteratorItemType, manIdx); |
| 141 | // Update item type | ||
| 142 | 10 | itemType = iteratorItemType; | |
| 143 | } | ||
| 144 | |||
| 145 | // Check result type. This is important, also for copy ctor calls, etc. | ||
| 146 | 958 | const ExprResult itemResult = {itemType, itemVarSymbol}; | |
| 147 | 958 | const ExprResult iteratorItemResult = {iteratorItemType, nullptr /* always a temporary */}; | |
| 148 | 956 | const auto [_, copyCtor] = | |
| 149 |
2/2✓ Branch 203 → 204 taken 956 times.
✓ Branch 203 → 361 taken 2 times.
|
958 | opRuleManager.getAssignResultType(node->itemVarDecl, itemResult, iteratorItemResult, true, false, ERROR_FOREACH_ITEM); |
| 150 | 956 | node->calledItemCopyCtor = copyCtor; | |
| 151 | |||
| 152 | // Update type of item | ||
| 153 |
1/2✓ Branch 206 → 207 taken 956 times.
✗ Branch 206 → 361 not taken.
|
956 | itemVarSymbol->updateType(itemType, true); |
| 154 | |||
| 155 | // Visit body | ||
| 156 |
1/2✓ Branch 207 → 208 taken 956 times.
✗ Branch 207 → 359 not taken.
|
956 | visit(node->body); |
| 157 | |||
| 158 |
1/2✓ Branch 209 → 210 taken 956 times.
✗ Branch 209 → 360 not taken.
|
1912 | return nullptr; |
| 159 | 964 | } | |
| 160 | |||
| 161 | 2933 | std::any TypeChecker::visitWhileLoop(WhileLoopNode *node) { | |
| 162 | // Change to while body scope | ||
| 163 |
2/4✓ Branch 2 → 3 taken 2933 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 2933 times.
✗ Branch 3 → 33 not taken.
|
2933 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY); |
| 164 | |||
| 165 | // Visit condition | ||
| 166 |
3/4✓ Branch 5 → 6 taken 2931 times.
✓ Branch 5 → 39 taken 2 times.
✓ Branch 6 → 7 taken 2931 times.
✗ Branch 6 → 37 not taken.
|
2933 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 167 |
2/6✓ Branch 8 → 9 taken 2931 times.
✗ Branch 8 → 51 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 13 taken 2931 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 41 not taken.
|
2931 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 168 | // Check if condition evaluates to bool | ||
| 169 |
3/4✓ Branch 13 → 14 taken 2931 times.
✗ Branch 13 → 51 not taken.
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 25 taken 2929 times.
|
2931 | if (!conditionType.is(TY_BOOL)) |
| 170 |
4/8✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 44 not taken.
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 42 not taken.
✓ Branch 21 → 22 taken 2 times.
✗ Branch 21 → 48 not taken.
✓ Branch 22 → 23 taken 2 times.
✗ Branch 22 → 48 not taken.
|
8 | 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 2929 times.
✗ Branch 25 → 49 not taken.
|
2929 | visit(node->body); |
| 174 | |||
| 175 |
1/2✓ Branch 27 → 28 taken 2929 times.
✗ Branch 27 → 50 not taken.
|
5858 | return nullptr; |
| 176 | 2933 | } | |
| 177 | |||
| 178 | 33 | std::any TypeChecker::visitDoWhileLoop(DoWhileLoopNode *node) { | |
| 179 | // Change to while body scope | ||
| 180 |
2/4✓ Branch 2 → 3 taken 33 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 33 times.
✗ Branch 3 → 33 not taken.
|
33 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY); |
| 181 | |||
| 182 | // Visit body | ||
| 183 |
1/2✓ Branch 5 → 6 taken 33 times.
✗ Branch 5 → 37 not taken.
|
33 | visit(node->body); |
| 184 | |||
| 185 | // Visit condition | ||
| 186 |
2/4✓ Branch 7 → 8 taken 33 times.
✗ Branch 7 → 40 not taken.
✓ Branch 8 → 9 taken 33 times.
✗ Branch 8 → 38 not taken.
|
33 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 187 |
2/6✓ Branch 10 → 11 taken 33 times.
✗ Branch 10 → 51 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 15 taken 33 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 42 not taken.
|
33 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 188 | // Check if condition evaluates to bool | ||
| 189 |
3/4✓ Branch 15 → 16 taken 33 times.
✗ Branch 15 → 51 not taken.
✓ Branch 16 → 17 taken 2 times.
✓ Branch 16 → 27 taken 31 times.
|
33 | if (!conditionType.is(TY_BOOL)) |
| 190 |
4/8✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 45 not taken.
✓ Branch 20 → 21 taken 2 times.
✗ Branch 20 → 43 not taken.
✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 49 not taken.
✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 49 not taken.
|
8 | 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 31 times.
✗ Branch 27 → 50 not taken.
|
62 | return nullptr; |
| 193 | 33 | } | |
| 194 | |||
| 195 | 44958 | std::any TypeChecker::visitIfStmt(IfStmtNode *node) { | |
| 196 | // Change to then body scope | ||
| 197 |
2/4✓ Branch 2 → 3 taken 44958 times.
✗ Branch 2 → 72 not taken.
✓ Branch 3 → 4 taken 44958 times.
✗ Branch 3 → 70 not taken.
|
44958 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY); |
| 198 | |||
| 199 | // Visit condition | ||
| 200 |
3/4✓ Branch 5 → 6 taken 44954 times.
✓ Branch 5 → 76 taken 4 times.
✓ Branch 6 → 7 taken 44954 times.
✗ Branch 6 → 74 not taken.
|
44958 | const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition)).type; |
| 201 |
4/6✓ Branch 8 → 9 taken 44954 times.
✗ Branch 8 → 90 not taken.
✓ Branch 9 → 10 taken 2 times.
✓ Branch 9 → 13 taken 44952 times.
✓ Branch 10 → 11 taken 2 times.
✗ Branch 10 → 78 not taken.
|
44956 | HANDLE_UNRESOLVED_TYPE_PTR(conditionType) |
| 202 | // Check if condition evaluates to bool | ||
| 203 |
3/4✓ Branch 13 → 14 taken 44952 times.
✗ Branch 13 → 90 not taken.
✓ Branch 14 → 15 taken 4 times.
✓ Branch 14 → 25 taken 44948 times.
|
44952 | if (!conditionType.is(TY_BOOL)) |
| 204 |
4/8✓ Branch 17 → 18 taken 4 times.
✗ Branch 17 → 81 not taken.
✓ Branch 18 → 19 taken 4 times.
✗ Branch 18 → 79 not taken.
✓ Branch 21 → 22 taken 4 times.
✗ Branch 21 → 85 not taken.
✓ Branch 22 → 23 taken 4 times.
✗ Branch 22 → 85 not taken.
|
16 | 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 44948 times.
✗ Branch 25 → 27 not taken.
|
44948 | const auto *assignExprNode = dynamic_cast<AssignExprNode *>(node->condition); |
| 208 |
3/4✓ Branch 28 → 29 taken 2 times.
✓ Branch 28 → 32 taken 44946 times.
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 32 not taken.
|
44948 | if (assignExprNode && assignExprNode->op == AssignExprNode::AssignOp::OP_ASSIGN) |
| 209 |
1/2✓ Branch 30 → 31 taken 2 times.
✗ Branch 30 → 86 not taken.
|
2 | 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 44948 times.
✗ Branch 32 → 90 not taken.
|
44948 | const bool constantCondition = node->condition->hasCompileTimeValue(manIdx); |
| 215 |
5/6✓ Branch 33 → 34 taken 684 times.
✓ Branch 33 → 36 taken 44264 times.
✓ Branch 34 → 35 taken 684 times.
✗ Branch 34 → 90 not taken.
✓ Branch 35 → 36 taken 357 times.
✓ Branch 35 → 37 taken 327 times.
|
44948 | node->compileThenBranch[manIdx] = !constantCondition || node->condition->getCompileTimeValue(manIdx).boolValue; |
| 216 |
5/6✓ Branch 40 → 41 taken 684 times.
✓ Branch 40 → 43 taken 44264 times.
✓ Branch 41 → 42 taken 684 times.
✗ Branch 41 → 90 not taken.
✓ Branch 42 → 43 taken 327 times.
✓ Branch 42 → 44 taken 357 times.
|
44948 | node->compileElseBranch[manIdx] = !constantCondition || !node->condition->getCompileTimeValue(manIdx).boolValue; |
| 217 | |||
| 218 | // Visit body | ||
| 219 |
2/2✓ Branch 49 → 50 taken 44621 times.
✓ Branch 49 → 53 taken 327 times.
|
44948 | if (node->compileThenBranch[manIdx]) |
| 220 |
1/2✓ Branch 50 → 51 taken 44621 times.
✗ Branch 50 → 87 not taken.
|
44621 | visit(node->thenBody); |
| 221 | |||
| 222 | // Leave then body scope | ||
| 223 |
1/2✓ Branch 53 → 54 taken 44948 times.
✗ Branch 53 → 90 not taken.
|
44948 | scopeHandle.leaveScopeEarly(); |
| 224 | |||
| 225 | // Visit else statement if existing | ||
| 226 |
6/6✓ Branch 56 → 57 taken 44591 times.
✓ Branch 56 → 59 taken 357 times.
✓ Branch 57 → 58 taken 2803 times.
✓ Branch 57 → 59 taken 41788 times.
✓ Branch 60 → 61 taken 2803 times.
✓ Branch 60 → 64 taken 42145 times.
|
44948 | if (node->compileElseBranch[manIdx] && node->elseStmt) |
| 227 |
1/2✓ Branch 61 → 62 taken 2803 times.
✗ Branch 61 → 88 not taken.
|
2803 | visit(node->elseStmt); |
| 228 | |||
| 229 |
1/2✓ Branch 64 → 65 taken 44948 times.
✗ Branch 64 → 89 not taken.
|
89896 | return nullptr; |
| 230 | 44958 | } | |
| 231 | |||
| 232 | 2803 | 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 1131 times.
✓ Branch 2 → 8 taken 1672 times.
|
2803 | if (node->isElseIf) { |
| 235 |
1/2✓ Branch 3 → 4 taken 1131 times.
✗ Branch 3 → 18 not taken.
|
1131 | visit(node->ifStmt); |
| 236 |
1/2✓ Branch 5 → 6 taken 1131 times.
✗ Branch 5 → 19 not taken.
|
2262 | return nullptr; |
| 237 | } | ||
| 238 | |||
| 239 | // Change to else body scope | ||
| 240 |
2/4✓ Branch 8 → 9 taken 1672 times.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 1672 times.
✗ Branch 9 → 20 not taken.
|
1672 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY); |
| 241 | |||
| 242 | // Visit body | ||
| 243 |
1/2✓ Branch 11 → 12 taken 1672 times.
✗ Branch 11 → 24 not taken.
|
1672 | visit(node->body); |
| 244 | |||
| 245 |
1/2✓ Branch 13 → 14 taken 1672 times.
✗ Branch 13 → 25 not taken.
|
1672 | return nullptr; |
| 246 | 1672 | } | |
| 247 | |||
| 248 | 189 | std::any TypeChecker::visitSwitchStmt(SwitchStmtNode *node) { | |
| 249 | // Check expression type | ||
| 250 |
2/4✓ Branch 2 → 3 taken 189 times.
✗ Branch 2 → 74 not taken.
✓ Branch 3 → 4 taken 189 times.
✗ Branch 3 → 72 not taken.
|
189 | const QualType exprType = std::any_cast<ExprResult>(visit(node->assignExpr)).type; |
| 251 |
2/6✓ Branch 5 → 6 taken 189 times.
✗ Branch 5 → 101 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 189 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 76 not taken.
|
189 | HANDLE_UNRESOLVED_TYPE_PTR(exprType) |
| 252 |
3/4✓ Branch 10 → 11 taken 189 times.
✗ Branch 10 → 77 not taken.
✓ Branch 11 → 12 taken 2 times.
✓ Branch 11 → 22 taken 187 times.
|
189 | if (!exprType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL})) |
| 253 |
4/8✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 80 not taken.
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 78 not taken.
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 84 not taken.
✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 84 not taken.
|
8 | 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 187 times.
✗ Branch 22 → 85 not taken.
|
187 | visitChildren(node); |
| 258 | |||
| 259 | // Check if case constant types match switch expression type | ||
| 260 |
2/2✓ Branch 66 → 26 taken 1539 times.
✓ Branch 66 → 67 taken 183 times.
|
1909 | for (const CaseBranchNode *caseBranchNode : node->caseBranches) |
| 261 |
2/2✓ Branch 56 → 30 taken 1915 times.
✓ Branch 56 → 57 taken 1535 times.
|
4989 | for (CaseConstantNode *constantNode : caseBranchNode->caseConstants) { |
| 262 |
2/4✓ Branch 32 → 33 taken 1915 times.
✗ Branch 32 → 88 not taken.
✓ Branch 33 → 34 taken 1915 times.
✗ Branch 33 → 86 not taken.
|
1915 | const QualType constantType = std::any_cast<ExprResult>(visit(constantNode)).type; |
| 263 |
3/4✓ Branch 35 → 36 taken 1915 times.
✗ Branch 35 → 97 not taken.
✓ Branch 36 → 37 taken 4 times.
✓ Branch 36 → 47 taken 1911 times.
|
1915 | if (!constantType.matches(exprType, false, true, true)) |
| 264 |
4/8✓ Branch 39 → 40 taken 4 times.
✗ Branch 39 → 92 not taken.
✓ Branch 40 → 41 taken 4 times.
✗ Branch 40 → 90 not taken.
✓ Branch 43 → 44 taken 4 times.
✗ Branch 43 → 96 not taken.
✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 96 not taken.
|
16 | 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 183 times.
✗ Branch 67 → 100 not taken.
|
366 | return nullptr; |
| 268 | } | ||
| 269 | |||
| 270 | 1539 | std::any TypeChecker::visitCaseBranch(CaseBranchNode *node) { | |
| 271 | // Change to case body scope | ||
| 272 |
2/4✓ Branch 2 → 3 taken 1539 times.
✗ Branch 2 → 30 not taken.
✓ Branch 3 → 4 taken 1539 times.
✗ Branch 3 → 28 not taken.
|
1539 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::CASE_BODY); |
| 273 | |||
| 274 | // Visit constant list | ||
| 275 |
2/2✓ Branch 20 → 7 taken 1915 times.
✓ Branch 20 → 21 taken 1539 times.
|
4993 | for (CaseConstantNode *constant : node->caseConstants) |
| 276 |
1/2✓ Branch 9 → 10 taken 1915 times.
✗ Branch 9 → 32 not taken.
|
1915 | visit(constant); |
| 277 | |||
| 278 | // Visit body | ||
| 279 |
1/2✓ Branch 21 → 22 taken 1539 times.
✗ Branch 21 → 34 not taken.
|
1539 | visit(node->body); |
| 280 | |||
| 281 |
1/2✓ Branch 23 → 24 taken 1539 times.
✗ Branch 23 → 35 not taken.
|
3078 | return nullptr; |
| 282 | 1539 | } | |
| 283 | |||
| 284 | 3830 | 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 274 times.
✓ Branch 2 → 4 taken 3556 times.
|
3830 | if (node->constant) |
| 287 | 274 | 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 508 times.
✓ Branch 5 → 11 taken 3048 times.
|
3556 | if (node->identifierFragments.size() == 1) |
| 291 | 1016 | 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 1524 times.
✓ Branch 11 → 24 taken 2032 times.
|
3556 | if (!node->entry) { |
| 295 | 1524 | const NameRegistryEntry *registryEntry = sourceFile->getNameRegistryEntry(node->fqIdentifier); | |
| 296 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 23 taken 1524 times.
|
1524 | if (!registryEntry) |
| 297 | ✗ | SOFT_ERROR_ER(node, REFERENCED_UNDEFINED_VARIABLE, "The variable '" + node->fqIdentifier + "' could not be found") | |
| 298 | 1524 | node->entry = registryEntry->targetEntry; | |
| 299 | } | ||
| 300 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 3556 times.
|
3556 | assert(node->entry != nullptr); |
| 301 | |||
| 302 | // Check for the correct type | ||
| 303 | 3556 | const QualType &qualType = node->entry->getQualType(); | |
| 304 |
3/4✓ Branch 27 → 28 taken 3556 times.
✗ Branch 27 → 51 not taken.
✓ Branch 28 → 29 taken 4 times.
✓ Branch 28 → 39 taken 3552 times.
|
3556 | if (!qualType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL})) |
| 305 |
4/8✓ Branch 31 → 32 taken 4 times.
✗ Branch 31 → 54 not taken.
✓ Branch 32 → 33 taken 4 times.
✗ Branch 32 → 52 not taken.
✓ Branch 35 → 36 taken 4 times.
✗ Branch 35 → 58 not taken.
✓ Branch 36 → 37 taken 4 times.
✗ Branch 36 → 58 not taken.
|
16 | 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 3552 times.
✗ Branch 39 → 59 not taken.
✓ Branch 40 → 41 taken 3552 times.
✗ Branch 40 → 59 not taken.
|
7104 | return ExprResult{node->setEvaluatedSymbolType(qualType, manIdx)}; |
| 308 | } | ||
| 309 | |||
| 310 | 157 | std::any TypeChecker::visitDefaultBranch(DefaultBranchNode *node) { | |
| 311 | // Change to default body scope | ||
| 312 |
2/4✓ Branch 2 → 3 taken 157 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 157 times.
✗ Branch 3 → 12 not taken.
|
157 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::DEFAULT_BODY); |
| 313 | |||
| 314 | // Visit body | ||
| 315 |
1/2✓ Branch 5 → 6 taken 157 times.
✗ Branch 5 → 16 not taken.
|
157 | visit(node->body); |
| 316 | |||
| 317 |
1/2✓ Branch 7 → 8 taken 157 times.
✗ Branch 7 → 17 not taken.
|
314 | return nullptr; |
| 318 | 157 | } | |
| 319 | |||
| 320 | 123 | std::any TypeChecker::visitAnonymousBlockStmt(AnonymousBlockStmtNode *node) { | |
| 321 | // Change to anonymous scope body scope | ||
| 322 |
2/4✓ Branch 2 → 3 taken 123 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 123 times.
✗ Branch 3 → 12 not taken.
|
123 | ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::ANONYMOUS_BLOCK_BODY); |
| 323 | |||
| 324 | // Visit body | ||
| 325 |
1/2✓ Branch 5 → 6 taken 123 times.
✗ Branch 5 → 16 not taken.
|
123 | visit(node->body); |
| 326 | |||
| 327 |
1/2✓ Branch 7 → 8 taken 123 times.
✗ Branch 7 → 17 not taken.
|
246 | return nullptr; |
| 328 | 123 | } | |
| 329 | |||
| 330 | } // namespace spice::compiler | ||
| 331 |