| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeChecker.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/Attributes.h> | ||
| 7 | #include <global/GlobalResourceManager.h> | ||
| 8 | #include <global/TypeRegistry.h> | ||
| 9 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
| 10 | #include <typechecker/MacroDefs.h> | ||
| 11 | |||
| 12 | namespace spice::compiler { | ||
| 13 | |||
| 14 | 372 | std::any TypeChecker::visitMainFctDefPrepare(MainFctDefNode *node) { | |
| 15 | // Mark unreachable statements | ||
| 16 | 372 | bool returnsOnAllControlPaths = true; | |
| 17 | 1/2✓ Branch 2 → 3 taken 372 times. ✗ Branch 2 → 68 not taken. | 372 | node->returnsOnAllControlPaths(&returnsOnAllControlPaths); | 
| 18 | |||
| 19 | // Retrieve return type | ||
| 20 | 1/2✓ Branch 3 → 4 taken 372 times. ✗ Branch 3 → 68 not taken. | 372 | const QualType returnType(TY_INT); | 
| 21 | |||
| 22 | // Change to function body scope | ||
| 23 | 372 | currentScope = node->bodyScope; | |
| 24 | |||
| 25 | // Set type of 'result' variable to int | ||
| 26 | 1/2✓ Branch 6 → 7 taken 372 times. ✗ Branch 6 → 47 not taken. | 1116 | SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); | 
| 27 | 1/2✗ Branch 12 → 13 not taken. ✓ Branch 12 → 14 taken 372 times. | 372 | assert(resultEntry != nullptr); | 
| 28 | 1/2✓ Branch 14 → 15 taken 372 times. ✗ Branch 14 → 68 not taken. | 372 | resultEntry->updateType(returnType, false); | 
| 29 | 372 | resultEntry->used = true; | |
| 30 | |||
| 31 | // Retrieve param types | ||
| 32 | 372 | QualTypeList paramTypes; | |
| 33 | 2/2✓ Branch 15 → 16 taken 4 times. ✓ Branch 15 → 28 taken 368 times. | 372 | if (node->takesArgs) { | 
| 34 | 2/4✓ Branch 16 → 17 taken 4 times. ✗ Branch 16 → 53 not taken. ✓ Branch 17 → 18 taken 4 times. ✗ Branch 17 → 51 not taken. | 4 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); | 
| 35 | 2/2✓ Branch 25 → 21 taken 8 times. ✓ Branch 25 → 26 taken 4 times. | 12 | for (const auto &[name, qualType, isOptional] : namedParamList) | 
| 36 | 1/2✓ Branch 22 → 23 taken 8 times. ✗ Branch 22 → 54 not taken. | 8 | paramTypes.push_back(qualType); | 
| 37 | 4 | } | |
| 38 | |||
| 39 | // Prepare type of function | ||
| 40 | 2/4✓ Branch 28 → 29 taken 372 times. ✗ Branch 28 → 58 not taken. ✓ Branch 29 → 30 taken 372 times. ✗ Branch 29 → 58 not taken. | 372 | const QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes); | 
| 41 | |||
| 42 | // Update main function symbol type | ||
| 43 | 1/2✓ Branch 32 → 33 taken 372 times. ✗ Branch 32 → 61 not taken. | 1116 | SymbolTableEntry *functionEntry = rootScope->lookupStrict(MAIN_FUNCTION_NAME); | 
| 44 | 1/2✗ Branch 38 → 39 not taken. ✓ Branch 38 → 40 taken 372 times. | 372 | assert(functionEntry != nullptr); | 
| 45 | 1/2✓ Branch 40 → 41 taken 372 times. ✗ Branch 40 → 66 not taken. | 372 | functionEntry->updateType(functionType, false); | 
| 46 | 372 | functionEntry->used = true; | |
| 47 | |||
| 48 | // Leave main function body scope | ||
| 49 | 372 | currentScope = rootScope; | |
| 50 | |||
| 51 | 1/2✓ Branch 41 → 42 taken 372 times. ✗ Branch 41 → 65 not taken. | 744 | return nullptr; | 
| 52 | 372 | } | |
| 53 | |||
| 54 | 7593 | std::any TypeChecker::visitFctDefPrepare(FctDefNode *node) { | |
| 55 | // Check if name is dtor | ||
| 56 | 3/4✓ Branch 2 → 3 taken 7593 times. ✗ Branch 2 → 389 not taken. ✓ Branch 3 → 4 taken 1 time. ✓ Branch 3 → 12 taken 7592 times. | 7593 | if (node->name->name == DTOR_FUNCTION_NAME) | 
| 57 | 3/6✓ Branch 6 → 7 taken 1 time. ✗ Branch 6 → 248 not taken. ✓ Branch 7 → 8 taken 1 time. ✗ Branch 7 → 246 not taken. ✓ Branch 10 → 11 taken 1 time. ✗ Branch 10 → 252 not taken. | 3 | SOFT_ERROR_BOOL(node, DTOR_MUST_BE_PROCEDURE, "Destructors are not allowed to be of type function") | 
| 58 | |||
| 59 | // Check if all control paths in the function return | ||
| 60 | 7592 | bool doSetPredecessorsUnreachable = true; | |
| 61 | 3/4✓ Branch 12 → 13 taken 7592 times. ✗ Branch 12 → 389 not taken. ✓ Branch 13 → 14 taken 1 time. ✓ Branch 13 → 22 taken 7591 times. | 7592 | if (!node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable)) | 
| 62 | 3/6✓ Branch 16 → 17 taken 1 time. ✗ Branch 16 → 255 not taken. ✓ Branch 17 → 18 taken 1 time. ✗ Branch 17 → 253 not taken. ✓ Branch 20 → 21 taken 1 time. ✗ Branch 20 → 259 not taken. | 3 | SOFT_ERROR_BOOL(node, MISSING_RETURN_STMT, "Not all control paths of this function have a return statement") | 
| 63 | |||
| 64 | // Change to function scope | ||
| 65 | 7591 | currentScope = node->scope; | |
| 66 | 1/2✗ Branch 22 → 23 not taken. ✓ Branch 22 → 24 taken 7591 times. | 7591 | assert(currentScope->type == ScopeType::FUNC_PROC_BODY); | 
| 67 | |||
| 68 | // Retrieve function template types | ||
| 69 | 7591 | std::vector<GenericType> usedGenericTypes; | |
| 70 | 2/2✓ Branch 24 → 25 taken 1008 times. ✓ Branch 24 → 54 taken 6583 times. | 7591 | if (node->hasTemplateTypes) { | 
| 71 | 2/2✓ Branch 52 → 27 taken 1200 times. ✓ Branch 52 → 53 taken 1007 times. | 2207 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { | 
| 72 | // Visit template type | ||
| 73 | 2/4✓ Branch 28 → 29 taken 1200 times. ✗ Branch 28 → 262 not taken. ✓ Branch 29 → 30 taken 1200 times. ✗ Branch 29 → 260 not taken. | 1200 | auto templateType = std::any_cast<QualType>(visit(dataType)); | 
| 74 | 2/4✓ Branch 31 → 32 taken 1200 times. ✗ Branch 31 → 272 not taken. ✗ Branch 32 → 33 not taken. ✓ Branch 32 → 34 taken 1200 times. | 1200 | if (templateType.is(TY_UNRESOLVED)) | 
| 75 | ✗ | continue; | |
| 76 | // Check if it is a generic type | ||
| 77 | 3/4✓ Branch 34 → 35 taken 1200 times. ✗ Branch 34 → 272 not taken. ✓ Branch 35 → 36 taken 1 time. ✓ Branch 35 → 44 taken 1199 times. | 1200 | if (!templateType.is(TY_GENERIC)) | 
| 78 | 2/4✓ Branch 39 → 40 taken 1 time. ✗ Branch 39 → 266 not taken. ✓ Branch 40 → 41 taken 1 time. ✗ Branch 40 → 263 not taken. | 3 | throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types"); | 
| 79 | // Convert generic symbol type to generic type | ||
| 80 | 2/4✓ Branch 44 → 45 taken 1199 times. ✗ Branch 44 → 272 not taken. ✓ Branch 45 → 46 taken 1199 times. ✗ Branch 45 → 272 not taken. | 1199 | GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); | 
| 81 | 1/2✗ Branch 46 → 47 not taken. ✓ Branch 46 → 48 taken 1199 times. | 1199 | assert(genericType != nullptr); | 
| 82 | 1/2✓ Branch 48 → 49 taken 1199 times. ✗ Branch 48 → 272 not taken. | 1199 | usedGenericTypes.push_back(*genericType); | 
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | // Retrieve 'this' type | ||
| 87 | 1/2✓ Branch 54 → 55 taken 7590 times. ✗ Branch 54 → 387 not taken. | 7590 | QualType thisType(TY_DYN); // If the function is not a method, the default this type is TY_DYN | 
| 88 | 2/2✓ Branch 55 → 56 taken 3053 times. ✓ Branch 55 → 86 taken 4537 times. | 7590 | if (node->isMethod) { | 
| 89 | 3053 | Scope *structParentScope = node->structScope->parent; | |
| 90 | 1/2✓ Branch 56 → 57 taken 3053 times. ✗ Branch 56 → 282 not taken. | 3053 | SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName); | 
| 91 | 1/2✗ Branch 59 → 60 not taken. ✓ Branch 59 → 61 taken 3053 times. | 3053 | assert(structEntry != nullptr); | 
| 92 | // Set struct to used | ||
| 93 | 3053 | structEntry->used = true; | |
| 94 | // Get type and ptr type | ||
| 95 | 1/2✓ Branch 61 → 62 taken 3053 times. ✗ Branch 61 → 282 not taken. | 3053 | thisType = structEntry->getQualType(); | 
| 96 | 1/2✓ Branch 62 → 63 taken 3053 times. ✗ Branch 62 → 282 not taken. | 3053 | const QualType thisPtrType = thisType.toPtr(node); | 
| 97 | // Collect template types of 'this' type | ||
| 98 | 3/4✓ Branch 63 → 64 taken 3053 times. ✗ Branch 63 → 275 not taken. ✓ Branch 73 → 66 taken 1225 times. ✓ Branch 73 → 74 taken 3053 times. | 4278 | for (const QualType &templateType : thisType.getTemplateTypes()) { | 
| 99 | 290 | const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; }; | |
| 100 | 3/4✓ Branch 67 → 68 taken 1225 times. ✗ Branch 67 → 274 not taken. ✓ Branch 68 → 69 taken 1221 times. ✓ Branch 68 → 70 taken 4 times. | 1225 | if (std::ranges::none_of(usedGenericTypes, lambda)) | 
| 101 | 1/2✓ Branch 69 → 70 taken 1221 times. ✗ Branch 69 → 274 not taken. | 1221 | usedGenericTypes.emplace_back(templateType); | 
| 102 | 1225 | usedGenericTypes.back().used = true; | |
| 103 | } | ||
| 104 | |||
| 105 | // Set type of 'this' variable | ||
| 106 | 1/2✓ Branch 76 → 77 taken 3053 times. ✗ Branch 76 → 278 not taken. | 9159 | SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); | 
| 107 | 1/2✗ Branch 82 → 83 not taken. ✓ Branch 82 → 84 taken 3053 times. | 3053 | assert(thisEntry != nullptr); | 
| 108 | 1/2✓ Branch 84 → 85 taken 3053 times. ✗ Branch 84 → 282 not taken. | 3053 | thisEntry->updateType(thisPtrType, false); | 
| 109 | } | ||
| 110 | |||
| 111 | // Visit parameters | ||
| 112 | 7590 | QualTypeList paramTypes; | |
| 113 | 7590 | ParamList paramList; | |
| 114 | 2/2✓ Branch 86 → 87 taken 5867 times. ✓ Branch 86 → 123 taken 1723 times. | 7590 | if (node->hasParams) { | 
| 115 | 5867 | std::vector<const char *> paramNames; | |
| 116 | // Visit param list to retrieve the param names | ||
| 117 | 2/4✓ Branch 87 → 88 taken 5867 times. ✗ Branch 87 → 285 not taken. ✓ Branch 88 → 89 taken 5867 times. ✗ Branch 88 → 283 not taken. | 5867 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); | 
| 118 | 2/2✓ Branch 112 → 92 taken 9268 times. ✓ Branch 112 → 113 taken 5865 times. | 15133 | for (const auto &[name, qualType, isOptional] : namedParamList) { | 
| 119 | 1/2✓ Branch 93 → 94 taken 9268 times. ✗ Branch 93 → 297 not taken. | 9268 | paramNames.push_back(name); | 
| 120 | 2/6✓ Branch 94 → 95 taken 9268 times. ✗ Branch 94 → 297 not taken. ✗ Branch 95 → 96 not taken. ✓ Branch 95 → 98 taken 9268 times. ✗ Branch 96 → 97 not taken. ✗ Branch 96 → 286 not taken. | 9268 | HANDLE_UNRESOLVED_TYPE_PTR(qualType); | 
| 121 | 1/2✓ Branch 98 → 99 taken 9268 times. ✗ Branch 98 → 297 not taken. | 9268 | paramTypes.push_back(qualType); | 
| 122 | 1/2✓ Branch 99 → 100 taken 9268 times. ✗ Branch 99 → 287 not taken. | 9268 | paramList.push_back({qualType, isOptional}); | 
| 123 | // Check if the type is present in the template for generic types | ||
| 124 | 3/4✓ Branch 100 → 101 taken 9268 times. ✗ Branch 100 → 297 not taken. ✓ Branch 101 → 102 taken 2 times. ✓ Branch 101 → 110 taken 9266 times. | 9268 | if (!qualType.isCoveredByGenericTypeList(usedGenericTypes)) | 
| 125 | ✗ | throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE, | |
| 126 | 2/4✓ Branch 105 → 106 taken 2 times. ✗ Branch 105 → 291 not taken. ✓ Branch 106 → 107 taken 2 times. ✗ Branch 106 → 288 not taken. | 6 | "Generic param type not included in the template type list of the function"); | 
| 127 | } | ||
| 128 | 2/4✓ Branch 115 → 116 taken 5865 times. ✗ Branch 115 → 117 not taken. ✓ Branch 119 → 120 taken 5865 times. ✗ Branch 119 → 122 not taken. | 5869 | } | 
| 129 | |||
| 130 | // Retrieve return type | ||
| 131 | 2/4✓ Branch 123 → 124 taken 7588 times. ✗ Branch 123 → 305 not taken. ✓ Branch 124 → 125 taken 7588 times. ✗ Branch 124 → 303 not taken. | 7588 | auto returnType = std::any_cast<QualType>(visit(node->returnType)); | 
| 132 | 2/6✓ Branch 126 → 127 taken 7588 times. ✗ Branch 126 → 383 not taken. ✗ Branch 127 → 128 not taken. ✓ Branch 127 → 130 taken 7588 times. ✗ Branch 128 → 129 not taken. ✗ Branch 128 → 306 not taken. | 7588 | HANDLE_UNRESOLVED_TYPE_PTR(returnType) | 
| 133 | 3/4✓ Branch 130 → 131 taken 7588 times. ✗ Branch 130 → 383 not taken. ✓ Branch 131 → 132 taken 1 time. ✓ Branch 131 → 140 taken 7587 times. | 7588 | if (returnType.is(TY_DYN)) | 
| 134 | 3/6✓ Branch 134 → 135 taken 1 time. ✗ Branch 134 → 309 not taken. ✓ Branch 135 → 136 taken 1 time. ✗ Branch 135 → 307 not taken. ✓ Branch 138 → 139 taken 1 time. ✗ Branch 138 → 313 not taken. | 3 | SOFT_ERROR_BOOL(node, UNEXPECTED_DYN_TYPE, "Dyn return types are not allowed") | 
| 135 | 3/4✓ Branch 140 → 141 taken 7587 times. ✗ Branch 140 → 383 not taken. ✓ Branch 141 → 142 taken 1 time. ✓ Branch 141 → 150 taken 7586 times. | 7587 | if (!returnType.isCoveredByGenericTypeList(usedGenericTypes)) | 
| 136 | 3/6✓ Branch 144 → 145 taken 1 time. ✗ Branch 144 → 316 not taken. ✓ Branch 145 → 146 taken 1 time. ✗ Branch 145 → 314 not taken. ✓ Branch 148 → 149 taken 1 time. ✗ Branch 148 → 320 not taken. | 3 | SOFT_ERROR_BOOL(node->returnType, GENERIC_TYPE_NOT_IN_TEMPLATE, | 
| 137 | "Generic return type not included in the template type list of the function") | ||
| 138 | |||
| 139 | // Leave function body scope | ||
| 140 | 7586 | currentScope = node->scope->parent; | |
| 141 | 3/4✓ Branch 150 → 151 taken 3053 times. ✓ Branch 150 → 153 taken 4533 times. ✗ Branch 151 → 152 not taken. ✓ Branch 151 → 153 taken 3053 times. | 7586 | assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT); | 
| 142 | |||
| 143 | // Prepare type of function | ||
| 144 | 2/4✓ Branch 153 → 154 taken 7586 times. ✗ Branch 153 → 321 not taken. ✓ Branch 154 → 155 taken 7586 times. ✗ Branch 154 → 321 not taken. | 7586 | QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes); | 
| 145 | 7586 | functionType.setQualifiers(node->qualifiers); | |
| 146 | |||
| 147 | // Update type of function entry | ||
| 148 | 1/2✓ Branch 156 → 157 taken 7586 times. ✗ Branch 156 → 324 not taken. | 15172 | SymbolTableEntry *functionEntry = currentScope->lookupStrict(node->getSymbolTableEntryName()); | 
| 149 | 1/2✗ Branch 161 → 162 not taken. ✓ Branch 161 → 163 taken 7586 times. | 7586 | assert(functionEntry != nullptr); | 
| 150 | 1/2✓ Branch 163 → 164 taken 7586 times. ✗ Branch 163 → 383 not taken. | 7586 | functionEntry->updateType(functionType, false); | 
| 151 | |||
| 152 | // Build function object | ||
| 153 | 3/6✓ Branch 164 → 165 taken 7586 times. ✗ Branch 164 → 331 not taken. ✓ Branch 165 → 166 taken 7586 times. ✗ Branch 165 → 328 not taken. ✓ Branch 166 → 167 taken 7586 times. ✗ Branch 166 → 325 not taken. | 7586 | Function spiceFunc(node->name->name, functionEntry, thisType, returnType, paramList, usedGenericTypes, node); | 
| 154 | 7586 | spiceFunc.bodyScope = node->scope; | |
| 155 | 2/2✓ Branch 171 → 172 taken 7585 times. ✓ Branch 171 → 381 taken 1 time. | 7586 | FunctionManager::insert(currentScope, spiceFunc, &node->manifestations); | 
| 156 | |||
| 157 | // Check function attributes | ||
| 158 | 2/2✓ Branch 172 → 173 taken 309 times. ✓ Branch 172 → 220 taken 7276 times. | 7585 | if (node->attrs) { | 
| 159 | 309 | const AttrLstNode *attrLst = node->attrs->attrLst; | |
| 160 | 309 | Function *firstManifestation = node->manifestations.front(); | |
| 161 | 3/6✓ Branch 176 → 177 taken 309 times. ✗ Branch 176 → 334 not taken. ✓ Branch 177 → 178 taken 309 times. ✗ Branch 177 → 332 not taken. ✗ Branch 180 → 181 not taken. ✓ Branch 180 → 182 taken 309 times. | 927 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE)) | 
| 162 | ✗ | firstManifestation->mangleFunctionName = value->boolValue; | |
| 163 | 3/6✓ Branch 184 → 185 taken 309 times. ✗ Branch 184 → 340 not taken. ✓ Branch 185 → 186 taken 309 times. ✗ Branch 185 → 338 not taken. ✗ Branch 188 → 189 not taken. ✓ Branch 188 → 191 taken 309 times. | 927 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) { | 
| 164 | ✗ | const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset); | |
| 165 | ✗ | firstManifestation->predefinedMangledName = stringValue; | |
| 166 | } | ||
| 167 | 6/8✓ Branch 193 → 194 taken 309 times. ✗ Branch 193 → 346 not taken. ✓ Branch 194 → 195 taken 309 times. ✗ Branch 194 → 344 not taken. ✓ Branch 197 → 198 taken 12 times. ✓ Branch 197 → 220 taken 297 times. ✓ Branch 198 → 199 taken 11 times. ✓ Branch 198 → 220 taken 1 time. | 927 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_TEST); value && value->boolValue) { | 
| 168 | // Make sure that the function has the correct signature | ||
| 169 | 2/2✓ Branch 199 → 200 taken 1 time. ✓ Branch 199 → 208 taken 10 times. | 11 | if (node->hasParams) | 
| 170 | 2/4✓ Branch 203 → 204 taken 1 time. ✗ Branch 203 → 353 not taken. ✓ Branch 204 → 205 taken 1 time. ✗ Branch 204 → 350 not taken. | 3 | throw SemanticError(node->paramLst, TEST_FUNCTION_WITH_PARAMS, "Test function may not have parameters"); | 
| 171 | 3/4✓ Branch 208 → 209 taken 10 times. ✗ Branch 208 → 381 not taken. ✓ Branch 209 → 210 taken 1 time. ✓ Branch 209 → 218 taken 9 times. | 10 | if (!returnType.is(TY_BOOL)) | 
| 172 | 2/4✓ Branch 213 → 214 taken 1 time. ✗ Branch 213 → 362 not taken. ✓ Branch 214 → 215 taken 1 time. ✗ Branch 214 → 359 not taken. | 3 | throw SemanticError(node->returnType, TEST_FUNCTION_WRONG_RETURN_TYPE, "Test function must return a bool"); | 
| 173 | // Add to test function list | ||
| 174 | 9 | firstManifestation->entry->used = true; // Avoid printing unused warnings | |
| 175 | 9 | firstManifestation->used = true; // Always keep test functions, because they are called implicitly by the test main | |
| 176 | 1/2✓ Branch 219 → 220 taken 9 times. ✗ Branch 219 → 381 not taken. | 9 | sourceFile->testFunctions.push_back(node->manifestations.front()); | 
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | // Duplicate / rename the original child scope to reflect the substantiated versions of the function | ||
| 181 | 2/2✓ Branch 230 → 221 taken 700 times. ✓ Branch 230 → 231 taken 7583 times. | 8283 | for (size_t i = 1; i < node->manifestations.size(); i++) { | 
| 182 | 4/8✓ Branch 221 → 222 taken 700 times. ✗ Branch 221 → 373 not taken. ✓ Branch 222 → 223 taken 700 times. ✗ Branch 222 → 373 not taken. ✓ Branch 223 → 224 taken 700 times. ✗ Branch 223 → 370 not taken. ✓ Branch 224 → 225 taken 700 times. ✗ Branch 224 → 368 not taken. | 700 | Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false)); | 
| 183 | 1/2✓ Branch 227 → 228 taken 700 times. ✗ Branch 227 → 381 not taken. | 700 | node->manifestations.at(i)->bodyScope = scope; | 
| 184 | } | ||
| 185 | 3/6✓ Branch 232 → 233 taken 7583 times. ✗ Branch 232 → 379 not taken. ✓ Branch 233 → 234 taken 7583 times. ✗ Branch 233 → 376 not taken. ✓ Branch 234 → 235 taken 7583 times. ✗ Branch 234 → 374 not taken. | 7583 | currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false)); | 
| 186 | |||
| 187 | // Change to the root scope | ||
| 188 | 7583 | currentScope = rootScope; | |
| 189 | 1/2✗ Branch 237 → 238 not taken. ✓ Branch 237 → 239 taken 7583 times. | 7583 | assert(currentScope->type == ScopeType::GLOBAL); | 
| 190 | |||
| 191 | 1/2✓ Branch 239 → 240 taken 7583 times. ✗ Branch 239 → 380 not taken. | 7583 | return nullptr; | 
| 192 | 7604 | } | |
| 193 | |||
| 194 | 3784 | std::any TypeChecker::visitProcDefPrepare(ProcDefNode *node) { | |
| 195 | // Mark unreachable statements | ||
| 196 | 3784 | bool doSetPredecessorsUnreachable = true; | |
| 197 | 1/2✓ Branch 2 → 3 taken 3784 times. ✗ Branch 2 → 286 not taken. | 3784 | node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable); | 
| 198 | |||
| 199 | // Check if dtor and has params | ||
| 200 | 7/8✓ Branch 3 → 4 taken 2801 times. ✓ Branch 3 → 7 taken 983 times. ✓ Branch 4 → 5 taken 2801 times. ✗ Branch 4 → 286 not taken. ✓ Branch 5 → 6 taken 1 time. ✓ Branch 5 → 7 taken 2800 times. ✓ Branch 8 → 9 taken 1 time. ✓ Branch 8 → 17 taken 3783 times. | 3784 | if (node->hasParams && node->name->name == DTOR_FUNCTION_NAME) | 
| 201 | 2/4✓ Branch 12 → 13 taken 1 time. ✗ Branch 12 → 191 not taken. ✓ Branch 13 → 14 taken 1 time. ✗ Branch 13 → 188 not taken. | 3 | throw SemanticError(node, DTOR_WITH_PARAMS, "It is not allowed to specify parameters for destructors"); | 
| 202 | |||
| 203 | // Change to procedure scope | ||
| 204 | 3783 | currentScope = node->scope; | |
| 205 | 1/2✗ Branch 17 → 18 not taken. ✓ Branch 17 → 19 taken 3783 times. | 3783 | assert(currentScope->type == ScopeType::FUNC_PROC_BODY); | 
| 206 | |||
| 207 | // Retrieve procedure template types | ||
| 208 | 3783 | std::vector<GenericType> usedGenericTypes; | |
| 209 | 2/2✓ Branch 19 → 20 taken 1021 times. ✓ Branch 19 → 49 taken 2762 times. | 3783 | if (node->hasTemplateTypes) { | 
| 210 | 2/2✓ Branch 47 → 22 taken 1108 times. ✓ Branch 47 → 48 taken 1020 times. | 2128 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { | 
| 211 | // Visit template type | ||
| 212 | 2/4✓ Branch 23 → 24 taken 1108 times. ✗ Branch 23 → 199 not taken. ✓ Branch 24 → 25 taken 1108 times. ✗ Branch 24 → 197 not taken. | 1108 | auto templateType = std::any_cast<QualType>(visit(dataType)); | 
| 213 | 2/4✓ Branch 26 → 27 taken 1108 times. ✗ Branch 26 → 209 not taken. ✗ Branch 27 → 28 not taken. ✓ Branch 27 → 29 taken 1108 times. | 1108 | if (templateType.is(TY_UNRESOLVED)) | 
| 214 | ✗ | continue; | |
| 215 | // Check if it is a generic type | ||
| 216 | 3/4✓ Branch 29 → 30 taken 1108 times. ✗ Branch 29 → 209 not taken. ✓ Branch 30 → 31 taken 1 time. ✓ Branch 30 → 39 taken 1107 times. | 1108 | if (!templateType.is(TY_GENERIC)) | 
| 217 | 2/4✓ Branch 34 → 35 taken 1 time. ✗ Branch 34 → 203 not taken. ✓ Branch 35 → 36 taken 1 time. ✗ Branch 35 → 200 not taken. | 3 | throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types"); | 
| 218 | // Convert generic symbol type to generic type | ||
| 219 | 2/4✓ Branch 39 → 40 taken 1107 times. ✗ Branch 39 → 209 not taken. ✓ Branch 40 → 41 taken 1107 times. ✗ Branch 40 → 209 not taken. | 1107 | const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); | 
| 220 | 1/2✗ Branch 41 → 42 not taken. ✓ Branch 41 → 43 taken 1107 times. | 1107 | assert(genericType != nullptr); | 
| 221 | 1/2✓ Branch 43 → 44 taken 1107 times. ✗ Branch 43 → 209 not taken. | 1107 | usedGenericTypes.push_back(*genericType); | 
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | // Retrieve 'this' type | ||
| 226 | 1/2✓ Branch 49 → 50 taken 3782 times. ✗ Branch 49 → 284 not taken. | 3782 | QualType thisType(TY_DYN); // If the procedure is not a method, the default this type is TY_DYN | 
| 227 | 2/2✓ Branch 50 → 51 taken 3194 times. ✓ Branch 50 → 81 taken 588 times. | 3782 | if (node->isMethod) { | 
| 228 | 3194 | Scope *structParentScope = node->structScope->parent; | |
| 229 | 1/2✓ Branch 51 → 52 taken 3194 times. ✗ Branch 51 → 219 not taken. | 3194 | SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName); | 
| 230 | 1/2✗ Branch 54 → 55 not taken. ✓ Branch 54 → 56 taken 3194 times. | 3194 | assert(structEntry != nullptr); | 
| 231 | // Set struct to used | ||
| 232 | 3194 | structEntry->used = true; | |
| 233 | // Get type and ptr type | ||
| 234 | 1/2✓ Branch 56 → 57 taken 3194 times. ✗ Branch 56 → 219 not taken. | 3194 | thisType = structEntry->getQualType(); | 
| 235 | 1/2✓ Branch 57 → 58 taken 3194 times. ✗ Branch 57 → 219 not taken. | 3194 | const QualType thisPtrType = thisType.toPtr(node); | 
| 236 | // Collect template types of 'this' type | ||
| 237 | 3/4✓ Branch 58 → 59 taken 3194 times. ✗ Branch 58 → 212 not taken. ✓ Branch 68 → 61 taken 1125 times. ✓ Branch 68 → 69 taken 3194 times. | 4319 | for (const QualType &templateType : thisType.getTemplateTypes()) { | 
| 238 | 356 | const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; }; | |
| 239 | 3/4✓ Branch 62 → 63 taken 1125 times. ✗ Branch 62 → 211 not taken. ✓ Branch 63 → 64 taken 1037 times. ✓ Branch 63 → 65 taken 88 times. | 1125 | if (std::ranges::none_of(usedGenericTypes, lambda)) | 
| 240 | 1/2✓ Branch 64 → 65 taken 1037 times. ✗ Branch 64 → 211 not taken. | 1037 | usedGenericTypes.emplace_back(templateType); | 
| 241 | 1125 | usedGenericTypes.back().used = true; | |
| 242 | } | ||
| 243 | |||
| 244 | // Set type of 'this' variable | ||
| 245 | 1/2✓ Branch 71 → 72 taken 3194 times. ✗ Branch 71 → 215 not taken. | 9582 | SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); | 
| 246 | 1/2✗ Branch 77 → 78 not taken. ✓ Branch 77 → 79 taken 3194 times. | 3194 | assert(thisEntry != nullptr); | 
| 247 | 1/2✓ Branch 79 → 80 taken 3194 times. ✗ Branch 79 → 219 not taken. | 3194 | thisEntry->updateType(thisPtrType, false); | 
| 248 | } | ||
| 249 | |||
| 250 | // Visit parameters | ||
| 251 | 3782 | QualTypeList paramTypes; | |
| 252 | 3782 | ParamList paramList; | |
| 253 | 2/2✓ Branch 81 → 82 taken 2800 times. ✓ Branch 81 → 118 taken 982 times. | 3782 | if (node->hasParams) { | 
| 254 | 2800 | std::vector<const char *> paramNames; | |
| 255 | // Visit param list to retrieve the param names | ||
| 256 | 2/4✓ Branch 82 → 83 taken 2800 times. ✗ Branch 82 → 222 not taken. ✓ Branch 83 → 84 taken 2800 times. ✗ Branch 83 → 220 not taken. | 2800 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); | 
| 257 | 2/2✓ Branch 107 → 87 taken 3768 times. ✓ Branch 107 → 108 taken 2798 times. | 6566 | for (const auto &[name, qualType, isOptional] : namedParamList) { | 
| 258 | 1/2✓ Branch 88 → 89 taken 3768 times. ✗ Branch 88 → 234 not taken. | 3768 | paramNames.push_back(name); | 
| 259 | 1/2✓ Branch 89 → 90 taken 3768 times. ✗ Branch 89 → 234 not taken. | 3768 | paramTypes.push_back(qualType); | 
| 260 | 4/6✓ Branch 90 → 91 taken 3768 times. ✗ Branch 90 → 234 not taken. ✓ Branch 91 → 92 taken 1 time. ✓ Branch 91 → 94 taken 3767 times. ✓ Branch 92 → 93 taken 1 time. ✗ Branch 92 → 223 not taken. | 3768 | HANDLE_UNRESOLVED_TYPE_PTR(qualType); | 
| 261 | 1/2✓ Branch 94 → 95 taken 3767 times. ✗ Branch 94 → 224 not taken. | 3767 | paramList.push_back({qualType, isOptional}); | 
| 262 | // Check if the type is present in the template for generic types | ||
| 263 | 3/4✓ Branch 95 → 96 taken 3767 times. ✗ Branch 95 → 234 not taken. ✓ Branch 96 → 97 taken 1 time. ✓ Branch 96 → 105 taken 3766 times. | 3767 | if (!qualType.isCoveredByGenericTypeList(usedGenericTypes)) | 
| 264 | ✗ | throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE, | |
| 265 | 2/4✓ Branch 100 → 101 taken 1 time. ✗ Branch 100 → 228 not taken. ✓ Branch 101 → 102 taken 1 time. ✗ Branch 101 → 225 not taken. | 3 | "Generic param type not included in the template type list of the procedure"); | 
| 266 | } | ||
| 267 | 4/4✓ Branch 110 → 111 taken 2798 times. ✓ Branch 110 → 112 taken 1 time. ✓ Branch 114 → 115 taken 2798 times. ✓ Branch 114 → 117 taken 1 time. | 2802 | } | 
| 268 | |||
| 269 | // Leave procedure body scope | ||
| 270 | 3780 | currentScope = node->scope->parent; | |
| 271 | 3/4✓ Branch 118 → 119 taken 3194 times. ✓ Branch 118 → 121 taken 586 times. ✗ Branch 119 → 120 not taken. ✓ Branch 119 → 121 taken 3194 times. | 3780 | assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT); | 
| 272 | |||
| 273 | // Prepare type of procedure | ||
| 274 | 3/6✓ Branch 121 → 122 taken 3780 times. ✗ Branch 121 → 241 not taken. ✓ Branch 122 → 123 taken 3780 times. ✗ Branch 122 → 240 not taken. ✓ Branch 123 → 124 taken 3780 times. ✗ Branch 123 → 240 not taken. | 3780 | QualType procedureType = QualType(TY_PROCEDURE).getWithFunctionParamAndReturnTypes(QualType(TY_DYN), paramTypes); | 
| 275 | 3780 | procedureType.setQualifiers(node->qualifiers); | |
| 276 | |||
| 277 | // Update type of procedure entry | ||
| 278 | 1/2✓ Branch 125 → 126 taken 3780 times. ✗ Branch 125 → 244 not taken. | 7560 | SymbolTableEntry *procedureEntry = currentScope->lookupStrict(node->getSymbolTableEntryName()); | 
| 279 | 1/2✗ Branch 130 → 131 not taken. ✓ Branch 130 → 132 taken 3780 times. | 3780 | assert(procedureEntry != nullptr); | 
| 280 | 1/2✓ Branch 132 → 133 taken 3780 times. ✗ Branch 132 → 280 not taken. | 3780 | procedureEntry->updateType(procedureType, false); | 
| 281 | |||
| 282 | // Build procedure object | ||
| 283 | 4/8✓ Branch 133 → 134 taken 3780 times. ✗ Branch 133 → 252 not taken. ✓ Branch 134 → 135 taken 3780 times. ✗ Branch 134 → 249 not taken. ✓ Branch 135 → 136 taken 3780 times. ✗ Branch 135 → 246 not taken. ✓ Branch 136 → 137 taken 3780 times. ✗ Branch 136 → 245 not taken. | 3780 | Function spiceProc(node->name->name, procedureEntry, thisType, QualType(TY_DYN), paramList, usedGenericTypes, node); | 
| 284 | 3780 | spiceProc.bodyScope = node->scope; | |
| 285 | 2/2✓ Branch 141 → 142 taken 3779 times. ✓ Branch 141 → 278 taken 1 time. | 3780 | FunctionManager::insert(currentScope, spiceProc, &node->manifestations); | 
| 286 | |||
| 287 | // Check procedure attributes | ||
| 288 | 1/2✗ Branch 142 → 143 not taken. ✓ Branch 142 → 162 taken 3779 times. | 3779 | if (node->attrs) { | 
| 289 | ✗ | const AttrLstNode *attrLst = node->attrs->attrLst; | |
| 290 | ✗ | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE)) | |
| 291 | ✗ | node->manifestations.front()->mangleFunctionName = value->boolValue; | |
| 292 | ✗ | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) { | |
| 293 | ✗ | const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset); | |
| 294 | ✗ | node->manifestations.front()->predefinedMangledName = stringValue; | |
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | // Duplicate / rename the original child scope to reflect the substantiated versions of the procedure | ||
| 299 | 2/2✓ Branch 172 → 163 taken 170 times. ✓ Branch 172 → 173 taken 3779 times. | 3949 | for (size_t i = 1; i < node->manifestations.size(); i++) { | 
| 300 | 4/8✓ Branch 163 → 164 taken 170 times. ✗ Branch 163 → 270 not taken. ✓ Branch 164 → 165 taken 170 times. ✗ Branch 164 → 270 not taken. ✓ Branch 165 → 166 taken 170 times. ✗ Branch 165 → 267 not taken. ✓ Branch 166 → 167 taken 170 times. ✗ Branch 166 → 265 not taken. | 170 | Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false)); | 
| 301 | 1/2✓ Branch 169 → 170 taken 170 times. ✗ Branch 169 → 278 not taken. | 170 | node->manifestations.at(i)->bodyScope = scope; | 
| 302 | } | ||
| 303 | 3/6✓ Branch 174 → 175 taken 3779 times. ✗ Branch 174 → 276 not taken. ✓ Branch 175 → 176 taken 3779 times. ✗ Branch 175 → 273 not taken. ✓ Branch 176 → 177 taken 3779 times. ✗ Branch 176 → 271 not taken. | 3779 | currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false)); | 
| 304 | |||
| 305 | // Change to the root scope | ||
| 306 | 3779 | currentScope = rootScope; | |
| 307 | 1/2✗ Branch 179 → 180 not taken. ✓ Branch 179 → 181 taken 3779 times. | 3779 | assert(currentScope->type == ScopeType::GLOBAL); | 
| 308 | |||
| 309 | 1/2✓ Branch 181 → 182 taken 3779 times. ✗ Branch 181 → 277 not taken. | 3779 | return nullptr; | 
| 310 | 3788 | } | |
| 311 | |||
| 312 | 679 | std::any TypeChecker::visitStructDefPrepare(StructDefNode *node) { | |
| 313 | 679 | QualTypeList usedTemplateTypes; | |
| 314 | 679 | std::vector<GenericType> templateTypesGeneric; | |
| 315 | |||
| 316 | // Retrieve struct template types | ||
| 317 | 2/2✓ Branch 2 → 3 taken 241 times. ✓ Branch 2 → 37 taken 438 times. | 679 | if (node->hasTemplateTypes) { | 
| 318 | 1/2✓ Branch 4 → 5 taken 241 times. ✗ Branch 4 → 288 not taken. | 241 | usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size()); | 
| 319 | 1/2✓ Branch 6 → 7 taken 241 times. ✗ Branch 6 → 288 not taken. | 241 | templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size()); | 
| 320 | 2/2✓ Branch 35 → 9 taken 327 times. ✓ Branch 35 → 36 taken 241 times. | 568 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { | 
| 321 | // Visit template type | ||
| 322 | 2/4✓ Branch 10 → 11 taken 327 times. ✗ Branch 10 → 194 not taken. ✓ Branch 11 → 12 taken 327 times. ✗ Branch 11 → 192 not taken. | 327 | auto templateType = std::any_cast<QualType>(visit(dataType)); | 
| 323 | 2/4✓ Branch 13 → 14 taken 327 times. ✗ Branch 13 → 204 not taken. ✗ Branch 14 → 15 not taken. ✓ Branch 14 → 16 taken 327 times. | 327 | if (templateType.is(TY_UNRESOLVED)) | 
| 324 | ✗ | continue; | |
| 325 | // Check if it is a generic type | ||
| 326 | 2/4✓ Branch 16 → 17 taken 327 times. ✗ Branch 16 → 204 not taken. ✗ Branch 17 → 18 not taken. ✓ Branch 17 → 26 taken 327 times. | 327 | if (!templateType.is(TY_GENERIC)) | 
| 327 | ✗ | throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types"); | |
| 328 | // Convert generic symbol type to generic type | ||
| 329 | 2/4✓ Branch 26 → 27 taken 327 times. ✗ Branch 26 → 204 not taken. ✓ Branch 27 → 28 taken 327 times. ✗ Branch 27 → 204 not taken. | 327 | GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); | 
| 330 | 1/2✗ Branch 28 → 29 not taken. ✓ Branch 28 → 30 taken 327 times. | 327 | assert(genericType != nullptr); | 
| 331 | 1/2✓ Branch 30 → 31 taken 327 times. ✗ Branch 30 → 204 not taken. | 327 | usedTemplateTypes.push_back(*genericType); | 
| 332 | 1/2✓ Branch 31 → 32 taken 327 times. ✗ Branch 31 → 204 not taken. | 327 | templateTypesGeneric.push_back(*genericType); | 
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | // Retrieve implemented interfaces | ||
| 337 | 679 | QualTypeList interfaceTypes; | |
| 338 | 2/2✓ Branch 37 → 38 taken 130 times. ✓ Branch 37 → 89 taken 549 times. | 679 | if (node->hasInterfaces) { | 
| 339 | 1/2✓ Branch 39 → 40 taken 130 times. ✗ Branch 39 → 286 not taken. | 130 | interfaceTypes.reserve(node->interfaceTypeLst->dataTypes.size()); | 
| 340 | 2/2✓ Branch 87 → 42 taken 130 times. ✓ Branch 87 → 88 taken 129 times. | 259 | for (DataTypeNode *interfaceNode : node->interfaceTypeLst->dataTypes) { | 
| 341 | // Visit interface type | ||
| 342 | 2/4✓ Branch 43 → 44 taken 130 times. ✗ Branch 43 → 208 not taken. ✓ Branch 44 → 45 taken 130 times. ✗ Branch 44 → 206 not taken. | 130 | auto interfaceType = std::any_cast<QualType>(visit(interfaceNode)); | 
| 343 | 2/4✓ Branch 46 → 47 taken 130 times. ✗ Branch 46 → 230 not taken. ✗ Branch 47 → 48 not taken. ✓ Branch 47 → 49 taken 130 times. | 130 | if (interfaceType.is(TY_UNRESOLVED)) | 
| 344 | ✗ | continue; | |
| 345 | // Check if it is an interface type | ||
| 346 | 2/4✓ Branch 49 → 50 taken 130 times. ✗ Branch 49 → 230 not taken. ✗ Branch 50 → 51 not taken. ✓ Branch 50 → 58 taken 130 times. | 130 | if (!interfaceType.is(TY_INTERFACE)) | 
| 347 | ✗ | throw SemanticError(interfaceNode, EXPECTED_INTERFACE_TYPE, | |
| 348 | ✗ | "Expected interface type, got " + interfaceType.getName(false)); | |
| 349 | // Check for visibility | ||
| 350 | 9/12✓ Branch 58 → 59 taken 130 times. ✗ Branch 58 → 230 not taken. ✓ Branch 59 → 60 taken 130 times. ✗ Branch 59 → 230 not taken. ✓ Branch 60 → 61 taken 116 times. ✓ Branch 60 → 64 taken 14 times. ✓ Branch 61 → 62 taken 116 times. ✗ Branch 61 → 230 not taken. ✓ Branch 62 → 63 taken 1 time. ✓ Branch 62 → 64 taken 115 times. ✓ Branch 65 → 66 taken 1 time. ✓ Branch 65 → 74 taken 129 times. | 130 | if (interfaceType.getBodyScope()->isImportedBy(rootScope) && !interfaceType.isPublic()) | 
| 351 | ✗ | throw SemanticError(node, INSUFFICIENT_VISIBILITY, | |
| 352 | 4/8✓ Branch 67 → 68 taken 1 time. ✗ Branch 67 → 223 not taken. ✓ Branch 68 → 69 taken 1 time. ✗ Branch 68 → 223 not taken. ✓ Branch 69 → 70 taken 1 time. ✗ Branch 69 → 221 not taken. ✓ Branch 70 → 71 taken 1 time. ✗ Branch 70 → 218 not taken. | 1 | "Cannot access interface '" + interfaceType.getSubType() + "' due to its private visibility"); | 
| 353 | // Add to interface types | ||
| 354 | 1/2✓ Branch 74 → 75 taken 129 times. ✗ Branch 74 → 230 not taken. | 129 | interfaceTypes.push_back(interfaceType); | 
| 355 | // Update the type of the entry for that interface field | ||
| 356 | 129 | const std::string &interfaceName = interfaceNode->baseDataType->customDataType->typeNameFragments.back(); | |
| 357 | 1/2✓ Branch 76 → 77 taken 129 times. ✗ Branch 76 → 229 not taken. | 258 | SymbolTableEntry *interfaceEntry = node->structScope->lookupStrict("this." + interfaceName); | 
| 358 | 1/2✗ Branch 81 → 82 not taken. ✓ Branch 81 → 83 taken 129 times. | 129 | assert(interfaceEntry != nullptr); | 
| 359 | 1/2✓ Branch 83 → 84 taken 129 times. ✗ Branch 83 → 230 not taken. | 129 | interfaceEntry->updateType(interfaceType, false); | 
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | // Update type of struct entry | ||
| 364 | 1/2✗ Branch 89 → 90 not taken. ✓ Branch 89 → 91 taken 678 times. | 678 | assert(node->entry != nullptr); | 
| 365 | 678 | const TypeChainElementData data = {.bodyScope = node->structScope}; | |
| 366 | 1/2✓ Branch 91 → 92 taken 678 times. ✗ Branch 91 → 286 not taken. | 678 | const Type *type = TypeRegistry::getOrInsert(TY_STRUCT, node->structName, node->typeId, data, usedTemplateTypes); | 
| 367 | 2/4✓ Branch 92 → 93 taken 678 times. ✗ Branch 92 → 232 not taken. ✓ Branch 93 → 94 taken 678 times. ✗ Branch 93 → 232 not taken. | 678 | node->entry->updateType(QualType(type, node->qualifiers), false); | 
| 368 | |||
| 369 | // Change to struct scope | ||
| 370 | 678 | currentScope = node->structScope; | |
| 371 | 1/2✗ Branch 94 → 95 not taken. ✓ Branch 94 → 96 taken 678 times. | 678 | assert(currentScope->type == ScopeType::STRUCT); | 
| 372 | |||
| 373 | // Retrieve field types | ||
| 374 | 678 | QualTypeList fieldTypes; | |
| 375 | 1/2✓ Branch 97 → 98 taken 678 times. ✗ Branch 97 → 284 not taken. | 678 | fieldTypes.reserve(node->fields.size()); | 
| 376 | 2/2✓ Branch 141 → 100 taken 1461 times. ✓ Branch 141 → 142 taken 675 times. | 2136 | for (FieldNode *field : node->fields) { | 
| 377 | // Visit field type | ||
| 378 | 2/4✓ Branch 101 → 102 taken 1461 times. ✗ Branch 101 → 235 not taken. ✓ Branch 102 → 103 taken 1461 times. ✗ Branch 102 → 233 not taken. | 1461 | auto fieldType = std::any_cast<QualType>(visit(field)); | 
| 379 | 3/4✓ Branch 104 → 105 taken 1461 times. ✗ Branch 104 → 254 not taken. ✓ Branch 105 → 106 taken 2 times. ✓ Branch 105 → 107 taken 1459 times. | 1461 | if (fieldType.is(TY_UNRESOLVED)) | 
| 380 | 1/2✗ Branch 106 → 107 not taken. ✓ Branch 106 → 254 taken 2 times. | 2 | sourceFile->checkForSoftErrors(); // We get into trouble if we continue without the field type -> abort | 
| 381 | |||
| 382 | // Check for struct with infinite size. | ||
| 383 | // This can happen if the struct A has a field with type A | ||
| 384 | 6/10✓ Branch 107 → 108 taken 1459 times. ✗ Branch 107 → 254 not taken. ✓ Branch 108 → 109 taken 163 times. ✓ Branch 108 → 112 taken 1296 times. ✓ Branch 109 → 110 taken 163 times. ✗ Branch 109 → 254 not taken. ✗ Branch 110 → 111 not taken. ✓ Branch 110 → 112 taken 163 times. ✗ Branch 113 → 114 not taken. ✓ Branch 113 → 122 taken 1459 times. | 1459 | if (fieldType.is(TY_STRUCT) && fieldType.getBodyScope() == node->structScope) | 
| 385 | ✗ | throw SemanticError(field, STRUCT_INFINITE_SIZE, "Struct with infinite size detected"); | |
| 386 | |||
| 387 | // Add to field types | ||
| 388 | 1/2✓ Branch 122 → 123 taken 1459 times. ✗ Branch 122 → 254 not taken. | 1459 | fieldTypes.push_back(fieldType); | 
| 389 | |||
| 390 | // Update type of field entry | ||
| 391 | 1/2✓ Branch 123 → 124 taken 1459 times. ✗ Branch 123 → 254 not taken. | 1459 | SymbolTableEntry *fieldEntry = currentScope->lookupStrict(field->fieldName); | 
| 392 | 1/2✗ Branch 126 → 127 not taken. ✓ Branch 126 → 128 taken 1459 times. | 1459 | assert(fieldEntry != nullptr); | 
| 393 | 1/2✓ Branch 128 → 129 taken 1459 times. ✗ Branch 128 → 254 not taken. | 1459 | fieldEntry->updateType(fieldType, false); | 
| 394 | |||
| 395 | // Check if the template type list contains this type | ||
| 396 | 3/4✓ Branch 129 → 130 taken 1459 times. ✗ Branch 129 → 254 not taken. ✓ Branch 130 → 131 taken 1 time. ✓ Branch 130 → 139 taken 1458 times. | 1459 | if (!fieldType.isCoveredByGenericTypeList(templateTypesGeneric)) | 
| 397 | 2/4✓ Branch 134 → 135 taken 1 time. ✗ Branch 134 → 248 not taken. ✓ Branch 135 → 136 taken 1 time. ✗ Branch 135 → 245 not taken. | 3 | throw SemanticError(field->dataType, GENERIC_TYPE_NOT_IN_TEMPLATE, "Generic field type not included in struct template"); | 
| 398 | } | ||
| 399 | |||
| 400 | // Change to the root scope | ||
| 401 | 675 | currentScope = rootScope; | |
| 402 | 1/2✗ Branch 142 → 143 not taken. ✓ Branch 142 → 144 taken 675 times. | 675 | assert(currentScope->type == ScopeType::GLOBAL); | 
| 403 | |||
| 404 | // Build struct object | ||
| 405 | 4/8✓ Branch 144 → 145 taken 675 times. ✗ Branch 144 → 265 not taken. ✓ Branch 145 → 146 taken 675 times. ✗ Branch 145 → 262 not taken. ✓ Branch 146 → 147 taken 675 times. ✗ Branch 146 → 259 not taken. ✓ Branch 147 → 148 taken 675 times. ✗ Branch 147 → 256 not taken. | 675 | Struct spiceStruct(node->structName, node->entry, node->structScope, fieldTypes, templateTypesGeneric, interfaceTypes, node); | 
| 406 | 1/2✓ Branch 153 → 154 taken 675 times. ✗ Branch 153 → 282 not taken. | 675 | StructManager::insert(currentScope, spiceStruct, &node->structManifestations); | 
| 407 | 675 | spiceStruct.scope = node->structScope; | |
| 408 | |||
| 409 | // Request RTTI runtime if the struct is polymorphic | ||
| 410 | 675 | node->emitVTable |= node->hasInterfaces; | |
| 411 | 12/18✓ Branch 154 → 155 taken 58 times. ✓ Branch 154 → 161 taken 617 times. ✓ Branch 157 → 158 taken 58 times. ✗ Branch 157 → 266 not taken. ✓ Branch 158 → 159 taken 58 times. ✗ Branch 158 → 266 not taken. ✓ Branch 159 → 160 taken 57 times. ✓ Branch 159 → 161 taken 1 time. ✓ Branch 162 → 163 taken 58 times. ✓ Branch 162 → 164 taken 617 times. ✓ Branch 164 → 165 taken 58 times. ✓ Branch 164 → 167 taken 617 times. ✓ Branch 167 → 168 taken 57 times. ✓ Branch 167 → 175 taken 618 times. ✗ Branch 266 → 267 not taken. ✗ Branch 266 → 268 not taken. ✗ Branch 270 → 271 not taken. ✗ Branch 270 → 273 not taken. | 791 | if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_EMIT_VTABLE)) | 
| 412 | 2/4✓ Branch 170 → 171 taken 57 times. ✗ Branch 170 → 277 not taken. ✓ Branch 171 → 172 taken 57 times. ✗ Branch 171 → 275 not taken. | 171 | node->emitVTable |= node->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_EMIT_VTABLE)->boolValue; | 
| 413 | 7/8✓ Branch 175 → 176 taken 186 times. ✓ Branch 175 → 181 taken 489 times. ✓ Branch 176 → 177 taken 186 times. ✗ Branch 176 → 282 not taken. ✓ Branch 179 → 180 taken 130 times. ✓ Branch 179 → 181 taken 56 times. ✓ Branch 182 → 183 taken 130 times. ✓ Branch 182 → 184 taken 545 times. | 861 | if (node->emitVTable && !sourceFile->isRttiRT()) | 
| 414 | 1/2✓ Branch 183 → 184 taken 130 times. ✗ Branch 183 → 282 not taken. | 130 | sourceFile->requestRuntimeModule(RTTI_RT); | 
| 415 | |||
| 416 | 1/2✓ Branch 184 → 185 taken 675 times. ✗ Branch 184 → 281 not taken. | 1350 | return nullptr; | 
| 417 | 690 | } | |
| 418 | |||
| 419 | 93 | std::any TypeChecker::visitInterfaceDefPrepare(InterfaceDefNode *node) { | |
| 420 | 93 | QualTypeList usedTemplateTypes; | |
| 421 | 93 | std::vector<GenericType> templateTypesGeneric; | |
| 422 | |||
| 423 | // Retrieve interface template types | ||
| 424 | 2/2✓ Branch 2 → 3 taken 70 times. ✓ Branch 2 → 37 taken 23 times. | 93 | if (node->hasTemplateTypes) { | 
| 425 | 1/2✓ Branch 4 → 5 taken 70 times. ✗ Branch 4 → 122 not taken. | 70 | usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size()); | 
| 426 | 1/2✓ Branch 6 → 7 taken 70 times. ✗ Branch 6 → 122 not taken. | 70 | templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size()); | 
| 427 | 2/2✓ Branch 35 → 9 taken 70 times. ✓ Branch 35 → 36 taken 70 times. | 140 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { | 
| 428 | // Visit template type | ||
| 429 | 2/4✓ Branch 10 → 11 taken 70 times. ✗ Branch 10 → 93 not taken. ✓ Branch 11 → 12 taken 70 times. ✗ Branch 11 → 91 not taken. | 70 | auto templateType = std::any_cast<QualType>(visit(dataType)); | 
| 430 | 2/6✓ Branch 13 → 14 taken 70 times. ✗ Branch 13 → 101 not taken. ✗ Branch 14 → 15 not taken. ✓ Branch 14 → 17 taken 70 times. ✗ Branch 15 → 16 not taken. ✗ Branch 15 → 94 not taken. | 70 | HANDLE_UNRESOLVED_TYPE_PTR(templateType) | 
| 431 | // Check if it is a generic type | ||
| 432 | 2/4✓ Branch 17 → 18 taken 70 times. ✗ Branch 17 → 101 not taken. ✗ Branch 18 → 19 not taken. ✓ Branch 18 → 26 taken 70 times. | 70 | if (!templateType.is(TY_GENERIC)) { | 
| 433 | ✗ | softError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types"); | |
| 434 | ✗ | continue; | |
| 435 | } | ||
| 436 | // Convert generic symbol type to generic type | ||
| 437 | 2/4✓ Branch 26 → 27 taken 70 times. ✗ Branch 26 → 101 not taken. ✓ Branch 27 → 28 taken 70 times. ✗ Branch 27 → 101 not taken. | 70 | const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); | 
| 438 | 1/2✗ Branch 28 → 29 not taken. ✓ Branch 28 → 30 taken 70 times. | 70 | assert(genericType != nullptr); | 
| 439 | 1/2✓ Branch 30 → 31 taken 70 times. ✗ Branch 30 → 101 not taken. | 70 | usedTemplateTypes.push_back(*genericType); | 
| 440 | 1/2✓ Branch 31 → 32 taken 70 times. ✗ Branch 31 → 101 not taken. | 70 | templateTypesGeneric.push_back(*genericType); | 
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | // Update type of interface entry | ||
| 445 | 93 | const TypeChainElementData data = {.bodyScope = node->interfaceScope}; | |
| 446 | 1/2✓ Branch 37 → 38 taken 93 times. ✗ Branch 37 → 122 not taken. | 93 | const Type *type = TypeRegistry::getOrInsert(TY_INTERFACE, node->interfaceName, node->typeId, data, usedTemplateTypes); | 
| 447 | 1/2✓ Branch 38 → 39 taken 93 times. ✗ Branch 38 → 122 not taken. | 93 | const QualType interfaceType(type, node->qualifiers); | 
| 448 | 1/2✗ Branch 39 → 40 not taken. ✓ Branch 39 → 41 taken 93 times. | 93 | assert(node->entry != nullptr); | 
| 449 | 1/2✓ Branch 41 → 42 taken 93 times. ✗ Branch 41 → 122 not taken. | 93 | node->entry->updateType(interfaceType, false); | 
| 450 | |||
| 451 | // Change to interface scope | ||
| 452 | 93 | currentScope = node->interfaceScope; | |
| 453 | 1/2✗ Branch 42 → 43 not taken. ✓ Branch 42 → 44 taken 93 times. | 93 | assert(currentScope->type == ScopeType::INTERFACE); | 
| 454 | |||
| 455 | // Visit methods | ||
| 456 | 93 | size_t vtableIndex = 0; | |
| 457 | 93 | std::vector<Function *> methods; | |
| 458 | 1/2✓ Branch 45 → 46 taken 93 times. ✗ Branch 45 → 120 not taken. | 93 | methods.reserve(node->signatures.size()); | 
| 459 | 2/2✓ Branch 68 → 48 taken 214 times. ✓ Branch 68 → 69 taken 92 times. | 306 | for (SignatureNode *signature : node->signatures) { | 
| 460 | 2/4✓ Branch 49 → 50 taken 214 times. ✗ Branch 49 → 105 not taken. ✓ Branch 50 → 51 taken 214 times. ✗ Branch 50 → 103 not taken. | 214 | const auto method = std::any_cast<std::vector<Function *> *>(visit(signature)); | 
| 461 | 2/2✓ Branch 52 → 53 taken 1 time. ✓ Branch 52 → 55 taken 213 times. | 214 | if (!method) | 
| 462 | 1/2✓ Branch 53 → 54 taken 1 time. ✗ Branch 53 → 106 not taken. | 1 | return nullptr; | 
| 463 | |||
| 464 | // Set 'this' type | ||
| 465 | 2/2✓ Branch 60 → 57 taken 213 times. ✓ Branch 60 → 61 taken 213 times. | 426 | for (Function *m : *method) { | 
| 466 | 213 | m->isVirtual = true; // Interface methods are always virtual | |
| 467 | 213 | m->vtableIndex = vtableIndex; | |
| 468 | 213 | m->thisType = interfaceType; | |
| 469 | } | ||
| 470 | |||
| 471 | 1/2✓ Branch 65 → 66 taken 213 times. ✗ Branch 65 → 107 not taken. | 213 | methods.insert(methods.end(), method->begin(), method->end()); | 
| 472 | 213 | vtableIndex++; | |
| 473 | } | ||
| 474 | |||
| 475 | // Change to root scope | ||
| 476 | 92 | currentScope = rootScope; | |
| 477 | 1/2✗ Branch 69 → 70 not taken. ✓ Branch 69 → 71 taken 92 times. | 92 | assert(currentScope->type == ScopeType::GLOBAL); | 
| 478 | |||
| 479 | // Build interface object | ||
| 480 | 3/6✓ Branch 71 → 72 taken 92 times. ✗ Branch 71 → 116 not taken. ✓ Branch 72 → 73 taken 92 times. ✗ Branch 72 → 113 not taken. ✓ Branch 73 → 74 taken 92 times. ✗ Branch 73 → 110 not taken. | 92 | Interface spiceInterface(node->interfaceName, node->entry, node->interfaceScope, methods, templateTypesGeneric, node); | 
| 481 | 1/2✓ Branch 78 → 79 taken 92 times. ✗ Branch 78 → 118 not taken. | 92 | InterfaceManager::insert(currentScope, spiceInterface, &node->interfaceManifestations); | 
| 482 | 92 | spiceInterface.scope = node->interfaceScope; | |
| 483 | |||
| 484 | // Request RTTI runtime, that is always required when dealing with interfaces due to polymorphism | ||
| 485 | 2/4✓ Branch 79 → 80 taken 92 times. ✗ Branch 79 → 118 not taken. ✓ Branch 82 → 83 taken 92 times. ✗ Branch 82 → 84 not taken. | 184 | if (!sourceFile->isRttiRT()) | 
| 486 | 1/2✓ Branch 83 → 84 taken 92 times. ✗ Branch 83 → 118 not taken. | 92 | sourceFile->requestRuntimeModule(RTTI_RT); | 
| 487 | |||
| 488 | 1/2✓ Branch 84 → 85 taken 92 times. ✗ Branch 84 → 117 not taken. | 92 | return nullptr; | 
| 489 | 93 | } | |
| 490 | |||
| 491 | 65 | std::any TypeChecker::visitEnumDefPrepare(EnumDefNode *node) { | |
| 492 | // Update type of enum entry | ||
| 493 | 65 | const TypeChainElementData data = {.bodyScope = node->enumScope}; | |
| 494 | 1/2✓ Branch 3 → 4 taken 65 times. ✗ Branch 3 → 59 not taken. | 65 | const Type *type = TypeRegistry::getOrInsert(TY_ENUM, node->enumName, node->typeId, data, {}); | 
| 495 | 1/2✗ Branch 5 → 6 not taken. ✓ Branch 5 → 7 taken 65 times. | 65 | assert(node->entry != nullptr); | 
| 496 | 2/4✓ Branch 7 → 8 taken 65 times. ✗ Branch 7 → 62 not taken. ✓ Branch 8 → 9 taken 65 times. ✗ Branch 8 → 62 not taken. | 65 | node->entry->updateType(QualType(type, node->qualifiers), false); | 
| 497 | |||
| 498 | // Change to enum scope | ||
| 499 | 65 | currentScope = node->enumScope; | |
| 500 | 1/2✗ Branch 9 → 10 not taken. ✓ Branch 9 → 11 taken 65 times. | 65 | assert(currentScope->type == ScopeType::ENUM); | 
| 501 | |||
| 502 | // Loop through all items with values | ||
| 503 | 65 | std::vector<std::string> names; | |
| 504 | 65 | std::vector<uint32_t> values; | |
| 505 | 2/2✓ Branch 30 → 13 taken 737 times. ✓ Branch 30 → 31 taken 65 times. | 802 | for (const EnumItemNode *enumItem : node->itemLst->items) { | 
| 506 | // Save name | ||
| 507 | 1/2✓ Branch 14 → 15 taken 737 times. ✗ Branch 14 → 71 not taken. | 737 | names.push_back(enumItem->itemName); | 
| 508 | // Check for duplicate value | ||
| 509 | 2/2✓ Branch 15 → 16 taken 409 times. ✓ Branch 15 → 28 taken 328 times. | 737 | if (enumItem->hasValue) { | 
| 510 | 3/4✓ Branch 17 → 18 taken 409 times. ✗ Branch 17 → 63 not taken. ✓ Branch 19 → 20 taken 1 time. ✓ Branch 19 → 27 taken 408 times. | 409 | if (std::ranges::find(values, enumItem->itemValue) != values.end()) { | 
| 511 | 2/4✓ Branch 22 → 23 taken 1 time. ✗ Branch 22 → 67 not taken. ✓ Branch 23 → 24 taken 1 time. ✗ Branch 23 → 65 not taken. | 1 | softError(enumItem, DUPLICATE_ENUM_ITEM_VALUE, "Duplicate enum item value, please use another"); | 
| 512 | 1 | continue; | |
| 513 | } | ||
| 514 | 1/2✓ Branch 27 → 28 taken 408 times. ✗ Branch 27 → 71 not taken. | 408 | values.push_back(enumItem->itemValue); | 
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | // Loop through all items without values | ||
| 519 | 65 | uint32_t nextValue = 0; | |
| 520 | 1/2✓ Branch 31 → 32 taken 65 times. ✗ Branch 31 → 76 not taken. | 65 | const QualType intSymbolType(TY_INT); | 
| 521 | 2/2✓ Branch 51 → 34 taken 737 times. ✓ Branch 51 → 52 taken 65 times. | 802 | for (EnumItemNode *enumItem : node->itemLst->items) { | 
| 522 | // Update type of enum item entry | ||
| 523 | 1/2✓ Branch 35 → 36 taken 737 times. ✗ Branch 35 → 74 not taken. | 737 | SymbolTableEntry *itemEntry = currentScope->lookupStrict(enumItem->itemName); | 
| 524 | 1/2✗ Branch 38 → 39 not taken. ✓ Branch 38 → 40 taken 737 times. | 737 | assert(itemEntry != nullptr); | 
| 525 | 1/2✓ Branch 40 → 41 taken 737 times. ✗ Branch 40 → 74 not taken. | 737 | itemEntry->updateType(intSymbolType, false); | 
| 526 | // Fill in value if not filled yet | ||
| 527 | 2/2✓ Branch 41 → 42 taken 328 times. ✓ Branch 41 → 49 taken 409 times. | 737 | if (!enumItem->hasValue) { | 
| 528 | 3/4✓ Branch 45 → 46 taken 616 times. ✗ Branch 45 → 72 not taken. ✓ Branch 47 → 43 taken 288 times. ✓ Branch 47 → 48 taken 328 times. | 616 | while (std::ranges::find(values, nextValue) != values.end()) | 
| 529 | 288 | nextValue++; | |
| 530 | 328 | enumItem->itemValue = nextValue; | |
| 531 | 1/2✓ Branch 48 → 49 taken 328 times. ✗ Branch 48 → 74 not taken. | 328 | values.push_back(nextValue); | 
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | // Change to root scope | ||
| 536 | 65 | currentScope = rootScope; | |
| 537 | 1/2✗ Branch 52 → 53 not taken. ✓ Branch 52 → 54 taken 65 times. | 65 | assert(currentScope->type == ScopeType::GLOBAL); | 
| 538 | |||
| 539 | 1/2✓ Branch 54 → 55 taken 65 times. ✗ Branch 54 → 75 not taken. | 130 | return nullptr; | 
| 540 | 65 | } | |
| 541 | |||
| 542 | 895 | std::any TypeChecker::visitGenericTypeDefPrepare(GenericTypeDefNode *node) { | |
| 543 | // Retrieve type conditions | ||
| 544 | 895 | QualTypeList typeConditions; | |
| 545 | 1/2✓ Branch 3 → 4 taken 895 times. ✗ Branch 3 → 60 not taken. | 895 | typeConditions.reserve(node->typeAltsLst->dataTypes.size()); | 
| 546 | 2/2✓ Branch 17 → 6 taken 1698 times. ✓ Branch 17 → 18 taken 895 times. | 2593 | for (const auto &typeAlt : node->typeAltsLst->dataTypes) { | 
| 547 | 2/4✓ Branch 7 → 8 taken 1698 times. ✗ Branch 7 → 48 not taken. ✓ Branch 8 → 9 taken 1698 times. ✗ Branch 8 → 46 not taken. | 1698 | auto typeCondition = std::any_cast<QualType>(visit(typeAlt)); | 
| 548 | 2/6✓ Branch 10 → 11 taken 1698 times. ✗ Branch 10 → 50 not taken. ✗ Branch 11 → 12 not taken. ✓ Branch 11 → 14 taken 1698 times. ✗ Branch 12 → 13 not taken. ✗ Branch 12 → 49 not taken. | 1698 | HANDLE_UNRESOLVED_TYPE_PTR(typeCondition) | 
| 549 | 1/2✓ Branch 14 → 15 taken 1698 times. ✗ Branch 14 → 50 not taken. | 1698 | typeConditions.push_back(typeCondition); | 
| 550 | } | ||
| 551 | |||
| 552 | // Add generic type to the scope | ||
| 553 | 2/4✓ Branch 18 → 19 taken 895 times. ✗ Branch 18 → 54 not taken. ✓ Branch 19 → 20 taken 895 times. ✗ Branch 19 → 52 not taken. | 895 | const GenericType genericType(node->typeName, typeConditions); | 
| 554 | 1/2✓ Branch 21 → 22 taken 895 times. ✗ Branch 21 → 58 not taken. | 895 | rootScope->insertGenericType(node->typeName, genericType); | 
| 555 | |||
| 556 | // Check if only one type condition is set | ||
| 557 | 7/8✓ Branch 23 → 24 taken 341 times. ✓ Branch 23 → 28 taken 554 times. ✓ Branch 25 → 26 taken 341 times. ✗ Branch 25 → 58 not taken. ✓ Branch 26 → 27 taken 2 times. ✓ Branch 26 → 28 taken 339 times. ✓ Branch 29 → 30 taken 2 times. ✓ Branch 29 → 32 taken 893 times. | 895 | if (typeConditions.size() == 1 && !typeConditions.front().is(TY_DYN)) | 
| 558 | 1/2✓ Branch 30 → 31 taken 2 times. ✗ Branch 30 → 55 not taken. | 2 | sourceFile->compilerOutput.warnings.emplace_back(node->typeAltsLst->codeLoc, SINGLE_GENERIC_TYPE_CONDITION, | 
| 559 | "Generic type is locked to one type"); | ||
| 560 | |||
| 561 | // Check if the list contains the dyn type, along with other types | ||
| 562 | 1/2✓ Branch 32 → 33 taken 895 times. ✗ Branch 32 → 58 not taken. | 2591 | const bool containsDynType = std::ranges::any_of(typeConditions, [&](const QualType &type) { return type.is(TY_DYN); }); | 
| 563 | 6/6✓ Branch 33 → 34 taken 341 times. ✓ Branch 33 → 37 taken 554 times. ✓ Branch 35 → 36 taken 2 times. ✓ Branch 35 → 37 taken 339 times. ✓ Branch 38 → 39 taken 2 times. ✓ Branch 38 → 41 taken 893 times. | 895 | if (containsDynType && typeConditions.size() > 1) | 
| 564 | 2 | sourceFile->compilerOutput.warnings.emplace_back( | |
| 565 | 1/2✓ Branch 39 → 40 taken 2 times. ✗ Branch 39 → 56 not taken. | 2 | node->typeAltsLst->codeLoc, INEFFECTIVE_GENERIC_TYPE_CONDITION, | 
| 566 | "One or several type conditions are superfluous, because the dyn type condition is given"); | ||
| 567 | |||
| 568 | 1/2✓ Branch 41 → 42 taken 895 times. ✗ Branch 41 → 57 not taken. | 895 | return nullptr; | 
| 569 | 895 | } | |
| 570 | |||
| 571 | 69 | std::any TypeChecker::visitAliasDefPrepare(AliasDefNode *node) { | |
| 572 | 2/4✓ Branch 2 → 3 taken 69 times. ✗ Branch 2 → 5 not taken. ✓ Branch 3 → 4 taken 69 times. ✗ Branch 3 → 5 not taken. | 69 | assert(node->entry != nullptr && node->aliasedTypeContainerEntry != nullptr); | 
| 573 | |||
| 574 | // Update type of alias entry | ||
| 575 | 1/2✓ Branch 7 → 8 taken 69 times. ✗ Branch 7 → 23 not taken. | 69 | const Type *type = TypeRegistry::getOrInsert(TY_ALIAS, node->aliasName, node->typeId, {}, {}); | 
| 576 | 2/4✓ Branch 9 → 10 taken 69 times. ✗ Branch 9 → 27 not taken. ✓ Branch 10 → 11 taken 69 times. ✗ Branch 10 → 27 not taken. | 69 | node->entry->updateType(QualType(type, node->qualifiers), false); | 
| 577 | |||
| 578 | // Update type of the aliased type container entry | ||
| 579 | 2/4✓ Branch 11 → 12 taken 69 times. ✗ Branch 11 → 30 not taken. ✓ Branch 12 → 13 taken 69 times. ✗ Branch 12 → 28 not taken. | 69 | const auto aliasedType = std::any_cast<QualType>(visit(node->dataType)); | 
| 580 | 4/6✓ Branch 14 → 15 taken 69 times. ✗ Branch 14 → 33 not taken. ✓ Branch 15 → 16 taken 1 time. ✓ Branch 15 → 18 taken 68 times. ✓ Branch 16 → 17 taken 1 time. ✗ Branch 16 → 31 not taken. | 69 | HANDLE_UNRESOLVED_TYPE_PTR(aliasedType) | 
| 581 | 1/2✓ Branch 18 → 19 taken 68 times. ✗ Branch 18 → 33 not taken. | 68 | node->aliasedTypeContainerEntry->updateType(aliasedType, false); | 
| 582 | 68 | node->aliasedTypeContainerEntry->used = true; // The container type is always used per default | |
| 583 | |||
| 584 | 1/2✓ Branch 19 → 20 taken 68 times. ✗ Branch 19 → 32 not taken. | 68 | return nullptr; | 
| 585 | } | ||
| 586 | |||
| 587 | 1170 | std::any TypeChecker::visitGlobalVarDefPrepare(GlobalVarDefNode *node) { | |
| 588 | // Insert variable name to symbol table | ||
| 589 | 2/4✓ Branch 2 → 3 taken 1170 times. ✗ Branch 2 → 80 not taken. ✓ Branch 3 → 4 taken 1170 times. ✗ Branch 3 → 78 not taken. | 1170 | auto globalVarType = std::any_cast<QualType>(visit(node->dataType)); | 
| 590 | 2/6✓ Branch 5 → 6 taken 1170 times. ✗ Branch 5 → 126 not taken. ✗ Branch 6 → 7 not taken. ✓ Branch 6 → 9 taken 1170 times. ✗ Branch 7 → 8 not taken. ✗ Branch 7 → 81 not taken. | 1170 | HANDLE_UNRESOLVED_TYPE_PTR(globalVarType) | 
| 591 | |||
| 592 | 2/2✓ Branch 9 → 10 taken 1168 times. ✓ Branch 9 → 37 taken 2 times. | 1170 | if (node->constant) { // Variable is initialized here | 
| 593 | 2/4✓ Branch 10 → 11 taken 1168 times. ✗ Branch 10 → 84 not taken. ✓ Branch 11 → 12 taken 1168 times. ✗ Branch 11 → 82 not taken. | 1168 | const QualType rhsType = std::any_cast<ExprResult>(visit(node->constant)).type; | 
| 594 | 2/6✓ Branch 13 → 14 taken 1168 times. ✗ Branch 13 → 103 not taken. ✗ Branch 14 → 15 not taken. ✓ Branch 14 → 17 taken 1168 times. ✗ Branch 15 → 16 not taken. ✗ Branch 15 → 86 not taken. | 1168 | HANDLE_UNRESOLVED_TYPE_PTR(rhsType) | 
| 595 | 3/4✓ Branch 17 → 18 taken 1168 times. ✗ Branch 17 → 103 not taken. ✓ Branch 18 → 19 taken 1 time. ✓ Branch 18 → 20 taken 1167 times. | 1168 | if (globalVarType.is(TY_DYN)) { // Perform type inference | 
| 596 | 1 | globalVarType = rhsType; | |
| 597 | 3/4✓ Branch 20 → 21 taken 1167 times. ✗ Branch 20 → 103 not taken. ✓ Branch 21 → 22 taken 2 times. ✓ Branch 21 → 35 taken 1165 times. | 1167 | } else if (!globalVarType.matches(rhsType, false, true, true)) { // Check if types are matching | 
| 598 | 7/14✓ Branch 22 → 23 taken 2 times. ✗ Branch 22 → 100 not taken. ✓ Branch 23 → 24 taken 2 times. ✗ Branch 23 → 95 not taken. ✓ Branch 24 → 25 taken 2 times. ✗ Branch 24 → 93 not taken. ✓ Branch 25 → 26 taken 2 times. ✗ Branch 25 → 91 not taken. ✓ Branch 26 → 27 taken 2 times. ✗ Branch 26 → 89 not taken. ✓ Branch 27 → 28 taken 2 times. ✗ Branch 27 → 87 not taken. ✓ Branch 33 → 34 taken 2 times. ✗ Branch 33 → 102 not taken. | 2 | SOFT_ERROR_BOOL(node->constant, OPERATOR_WRONG_DATA_TYPE, | 
| 599 | "Expected " + globalVarType.getName(false) + ", but got " + rhsType.getName(false)) | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | // Check if the type is still missing | ||
| 604 | 3/4✓ Branch 37 → 38 taken 1168 times. ✗ Branch 37 → 126 not taken. ✓ Branch 38 → 39 taken 1 time. ✓ Branch 38 → 47 taken 1167 times. | 1168 | if (globalVarType.is(TY_DYN)) | 
| 605 | 3/6✓ Branch 41 → 42 taken 1 time. ✗ Branch 41 → 106 not taken. ✓ Branch 42 → 43 taken 1 time. ✗ Branch 42 → 104 not taken. ✓ Branch 45 → 46 taken 1 time. ✗ Branch 45 → 110 not taken. | 3 | SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_TYPE_DYN, "Global variables must have an explicit data type") | 
| 606 | |||
| 607 | // Check if we would need to insert instructions in the global scope to initialize the variable | ||
| 608 | 2/4✓ Branch 47 → 48 taken 1167 times. ✗ Branch 47 → 126 not taken. ✗ Branch 48 → 49 not taken. ✓ Branch 48 → 57 taken 1167 times. | 1167 | if (!globalVarType.isPrimitive()) | 
| 609 | ✗ | SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_INVALID_TYPE, "Spice does only support global variables of primitive type") | |
| 610 | |||
| 611 | // Update type of global var entry | ||
| 612 | 1/2✗ Branch 57 → 58 not taken. ✓ Branch 57 → 59 taken 1167 times. | 1167 | assert(node->entry != nullptr); | 
| 613 | 1/2✓ Branch 59 → 60 taken 1167 times. ✗ Branch 59 → 126 not taken. | 1167 | node->entry->updateType(globalVarType, false); | 
| 614 | |||
| 615 | // Check if a value is attached | ||
| 616 | 6/8✓ Branch 60 → 61 taken 1 time. ✓ Branch 60 → 64 taken 1166 times. ✓ Branch 61 → 62 taken 1 time. ✗ Branch 61 → 126 not taken. ✓ Branch 62 → 63 taken 1 time. ✗ Branch 62 → 64 not taken. ✓ Branch 65 → 66 taken 1 time. ✓ Branch 65 → 74 taken 1166 times. | 1167 | if (!node->constant && globalVarType.isConst()) | 
| 617 | 3/6✓ Branch 68 → 69 taken 1 time. ✗ Branch 68 → 120 not taken. ✓ Branch 69 → 70 taken 1 time. ✗ Branch 69 → 118 not taken. ✓ Branch 72 → 73 taken 1 time. ✗ Branch 72 → 124 not taken. | 3 | SOFT_ERROR_BOOL(node, GLOBAL_CONST_WITHOUT_VALUE, "You must specify a value for constant global variables") | 
| 618 | |||
| 619 | 1/2✓ Branch 74 → 75 taken 1166 times. ✗ Branch 74 → 125 not taken. | 1166 | return nullptr; | 
| 620 | } | ||
| 621 | |||
| 622 | 982 | std::any TypeChecker::visitExtDeclPrepare(ExtDeclNode *node) { | |
| 623 | // Collect argument types | ||
| 624 | 982 | QualTypeList argTypes; | |
| 625 | 982 | ParamList argList; | |
| 626 | 2/2✓ Branch 2 → 3 taken 940 times. ✓ Branch 2 → 31 taken 42 times. | 982 | if (node->hasArgs) { | 
| 627 | 1/2✓ Branch 4 → 5 taken 940 times. ✗ Branch 4 → 153 not taken. | 940 | argList.reserve(node->argTypeLst->typeLst->dataTypes.size()); | 
| 628 | 2/2✓ Branch 29 → 7 taken 1930 times. ✓ Branch 29 → 30 taken 940 times. | 2870 | for (DataTypeNode *arg : node->argTypeLst->typeLst->dataTypes) { | 
| 629 | // Visit argument | ||
| 630 | 2/4✓ Branch 8 → 9 taken 1930 times. ✗ Branch 8 → 100 not taken. ✓ Branch 9 → 10 taken 1930 times. ✗ Branch 9 → 98 not taken. | 1930 | auto argType = std::any_cast<QualType>(visit(arg)); | 
| 631 | 2/6✓ Branch 11 → 12 taken 1930 times. ✗ Branch 11 → 109 not taken. ✗ Branch 12 → 13 not taken. ✓ Branch 12 → 15 taken 1930 times. ✗ Branch 13 → 14 not taken. ✗ Branch 13 → 101 not taken. | 1930 | HANDLE_UNRESOLVED_TYPE_PTR(argType) | 
| 632 | // Check if the argument type is 'dyn' | ||
| 633 | 3/4✓ Branch 15 → 16 taken 1930 times. ✗ Branch 15 → 109 not taken. ✓ Branch 16 → 17 taken 1 time. ✓ Branch 16 → 24 taken 1929 times. | 1930 | if (argType.is(TY_DYN)) { | 
| 634 | 2/4✓ Branch 19 → 20 taken 1 time. ✗ Branch 19 → 104 not taken. ✓ Branch 20 → 21 taken 1 time. ✗ Branch 20 → 102 not taken. | 1 | softError(arg, UNEXPECTED_DYN_TYPE, "Dyn data type is not allowed as arg type for external functions"); | 
| 635 | 1 | continue; | |
| 636 | } | ||
| 637 | // Save argument | ||
| 638 | 1/2✓ Branch 24 → 25 taken 1929 times. ✗ Branch 24 → 109 not taken. | 1929 | argTypes.push_back(argType); | 
| 639 | 1/2✓ Branch 25 → 26 taken 1929 times. ✗ Branch 25 → 108 not taken. | 1929 | argList.push_back({argType, false}); | 
| 640 | } | ||
| 641 | } | ||
| 642 | |||
| 643 | // Retrieve return type | ||
| 644 | 1/2✓ Branch 31 → 32 taken 982 times. ✗ Branch 31 → 153 not taken. | 982 | QualType returnType(TY_DYN); | 
| 645 | 982 | const bool isFunction = node->returnType; | |
| 646 | 2/2✓ Branch 32 → 33 taken 648 times. ✓ Branch 32 → 50 taken 334 times. | 982 | if (isFunction) { // External function | 
| 647 | 2/4✓ Branch 33 → 34 taken 648 times. ✗ Branch 33 → 113 not taken. ✓ Branch 34 → 35 taken 648 times. ✗ Branch 34 → 111 not taken. | 648 | returnType = std::any_cast<QualType>(visit(node->returnType)); | 
| 648 | 2/6✓ Branch 36 → 37 taken 648 times. ✗ Branch 36 → 153 not taken. ✗ Branch 37 → 38 not taken. ✓ Branch 37 → 40 taken 648 times. ✗ Branch 38 → 39 not taken. ✗ Branch 38 → 115 not taken. | 648 | HANDLE_UNRESOLVED_TYPE_PTR(returnType) | 
| 649 | // Check if return type is dyn | ||
| 650 | 3/4✓ Branch 40 → 41 taken 648 times. ✗ Branch 40 → 153 not taken. ✓ Branch 41 → 42 taken 1 time. ✓ Branch 41 → 50 taken 647 times. | 648 | if (returnType.is(TY_DYN)) | 
| 651 | 3/6✓ Branch 44 → 45 taken 1 time. ✗ Branch 44 → 118 not taken. ✓ Branch 45 → 46 taken 1 time. ✗ Branch 45 → 116 not taken. ✓ Branch 48 → 49 taken 1 time. ✗ Branch 48 → 122 not taken. | 3 | SOFT_ERROR_BOOL(node->returnType, UNEXPECTED_DYN_TYPE, "dyn is not allowed as return type for external functions") | 
| 652 | } | ||
| 653 | |||
| 654 | // Add function to current scope | ||
| 655 | 3/6✓ Branch 51 → 52 taken 981 times. ✗ Branch 51 → 127 not taken. ✓ Branch 52 → 53 taken 981 times. ✗ Branch 52 → 124 not taken. ✓ Branch 53 → 54 taken 981 times. ✗ Branch 53 → 123 not taken. | 981 | const Function spiceFunc(node->extFunctionName, node->entry, QualType(TY_DYN), returnType, argList, {}, node); | 
| 656 | 1/2✓ Branch 58 → 59 taken 981 times. ✗ Branch 58 → 151 not taken. | 981 | node->extFunction = FunctionManager::insert(currentScope, spiceFunc, &node->extFunctionManifestations); | 
| 657 | 981 | node->extFunction->mangleFunctionName = false; | |
| 658 | 981 | node->extFunction->alreadyTypeChecked = true; | |
| 659 | 4/4✓ Branch 59 → 60 taken 940 times. ✓ Branch 59 → 62 taken 41 times. ✓ Branch 60 → 61 taken 15 times. ✓ Branch 60 → 62 taken 925 times. | 981 | node->extFunction->isVararg = node->argTypeLst && node->argTypeLst->hasEllipsis; | 
| 660 | |||
| 661 | // Check procedure attributes | ||
| 662 | 2/2✓ Branch 63 → 64 taken 1 time. ✓ Branch 63 → 81 taken 980 times. | 981 | if (node->attrs) { | 
| 663 | 1 | const AttrLstNode *attrLst = node->attrs->attrLst; | |
| 664 | 3/6✓ Branch 66 → 67 taken 1 time. ✗ Branch 66 → 133 not taken. ✓ Branch 67 → 68 taken 1 time. ✗ Branch 67 → 131 not taken. ✗ Branch 70 → 71 not taken. ✓ Branch 70 → 72 taken 1 time. | 3 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE)) | 
| 665 | ✗ | node->extFunction->mangleFunctionName = value->boolValue; | |
| 666 | 3/6✓ Branch 74 → 75 taken 1 time. ✗ Branch 74 → 139 not taken. ✓ Branch 75 → 76 taken 1 time. ✗ Branch 75 → 137 not taken. ✓ Branch 78 → 79 taken 1 time. ✗ Branch 78 → 81 not taken. | 3 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) { | 
| 667 | 1/2✓ Branch 79 → 80 taken 1 time. ✗ Branch 79 → 151 not taken. | 1 | const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset); | 
| 668 | 1/2✓ Branch 80 → 81 taken 1 time. ✗ Branch 80 → 151 not taken. | 1 | node->extFunction->predefinedMangledName = stringValue; | 
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | // Prepare ext function type | ||
| 673 | 2/2✓ Branch 81 → 82 taken 647 times. ✓ Branch 81 → 83 taken 334 times. | 981 | const SuperType superType = isFunction ? TY_FUNCTION : TY_PROCEDURE; | 
| 674 | 2/4✓ Branch 84 → 85 taken 981 times. ✗ Branch 84 → 143 not taken. ✓ Branch 85 → 86 taken 981 times. ✗ Branch 85 → 143 not taken. | 981 | const QualType extFunctionType = QualType(superType).getWithFunctionParamAndReturnTypes(returnType, argTypes); | 
| 675 | |||
| 676 | // Set type of external function | ||
| 677 | 1/2✓ Branch 86 → 87 taken 981 times. ✗ Branch 86 → 151 not taken. | 981 | node->entry->updateType(extFunctionType, false); | 
| 678 | |||
| 679 | // Rename the original child scope to reflect the substantiated versions of the external function | ||
| 680 | 3/6✓ Branch 87 → 88 taken 981 times. ✗ Branch 87 → 149 not taken. ✓ Branch 88 → 89 taken 981 times. ✗ Branch 88 → 146 not taken. ✓ Branch 89 → 90 taken 981 times. ✗ Branch 89 → 144 not taken. | 981 | currentScope->renameChildScope(node->getScopeId(), spiceFunc.getSignature(false)); | 
| 681 | |||
| 682 | 1/2✓ Branch 92 → 93 taken 981 times. ✗ Branch 92 → 150 not taken. | 981 | return nullptr; | 
| 683 | 982 | } | |
| 684 | |||
| 685 | 594 | std::any TypeChecker::visitImportDefPrepare(ImportDefNode *node) { | |
| 686 | // Set entry to import type | ||
| 687 | 1/2✓ Branch 2 → 3 taken 594 times. ✗ Branch 2 → 10 not taken. | 594 | const QualType importType(TY_IMPORT, node->importName); | 
| 688 | 1/2✗ Branch 3 → 4 not taken. ✓ Branch 3 → 5 taken 594 times. | 594 | assert(node->entry != nullptr); | 
| 689 | 1/2✓ Branch 5 → 6 taken 594 times. ✗ Branch 5 → 10 not taken. | 594 | node->entry->updateType(importType, false); | 
| 690 | |||
| 691 | 1/2✓ Branch 6 → 7 taken 594 times. ✗ Branch 6 → 9 not taken. | 594 | return nullptr; | 
| 692 | } | ||
| 693 | |||
| 694 | } // namespace spice::compiler | ||
| 695 |