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