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