| 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 | 355 | std::any TypeChecker::visitMainFctDefPrepare(MainFctDefNode *node) { | |
| 15 | // Mark unreachable statements | ||
| 16 | 355 | bool returnsOnAllControlPaths = true; | |
| 17 |
1/2✓ Branch 0 (2→3) taken 355 times.
✗ Branch 1 (2→68) not taken.
|
355 | node->returnsOnAllControlPaths(&returnsOnAllControlPaths); |
| 18 | |||
| 19 | // Retrieve return type | ||
| 20 |
1/2✓ Branch 0 (3→4) taken 355 times.
✗ Branch 1 (3→68) not taken.
|
355 | const QualType returnType(TY_INT); |
| 21 | |||
| 22 | // Change to function body scope | ||
| 23 | 355 | currentScope = node->bodyScope; | |
| 24 | |||
| 25 | // Set type of 'result' variable to int | ||
| 26 |
1/2✓ Branch 0 (6→7) taken 355 times.
✗ Branch 1 (6→47) not taken.
|
1065 | SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); |
| 27 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 355 times.
|
355 | assert(resultEntry != nullptr); |
| 28 |
1/2✓ Branch 0 (14→15) taken 355 times.
✗ Branch 1 (14→68) not taken.
|
355 | resultEntry->updateType(returnType, false); |
| 29 | 355 | resultEntry->used = true; | |
| 30 | |||
| 31 | // Retrieve param types | ||
| 32 | 355 | QualTypeList paramTypes; | |
| 33 |
2/2✓ Branch 0 (15→16) taken 4 times.
✓ Branch 1 (15→28) taken 351 times.
|
355 | if (node->takesArgs) { |
| 34 |
2/4✓ Branch 0 (16→17) taken 4 times.
✗ Branch 1 (16→53) not taken.
✓ Branch 2 (17→18) taken 4 times.
✗ Branch 3 (17→51) not taken.
|
4 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); |
| 35 |
2/2✓ Branch 0 (25→21) taken 8 times.
✓ Branch 1 (25→26) taken 4 times.
|
12 | for (const auto &[name, qualType, isOptional] : namedParamList) |
| 36 |
1/2✓ Branch 0 (22→23) taken 8 times.
✗ Branch 1 (22→54) not taken.
|
8 | paramTypes.push_back(qualType); |
| 37 | 4 | } | |
| 38 | |||
| 39 | // Prepare type of function | ||
| 40 |
2/4✓ Branch 0 (28→29) taken 355 times.
✗ Branch 1 (28→58) not taken.
✓ Branch 2 (29→30) taken 355 times.
✗ Branch 3 (29→58) not taken.
|
355 | const QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes); |
| 41 | |||
| 42 | // Update main function symbol type | ||
| 43 |
1/2✓ Branch 0 (32→33) taken 355 times.
✗ Branch 1 (32→61) not taken.
|
1065 | SymbolTableEntry *functionEntry = rootScope->lookupStrict(MAIN_FUNCTION_NAME); |
| 44 |
1/2✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 355 times.
|
355 | assert(functionEntry != nullptr); |
| 45 |
1/2✓ Branch 0 (40→41) taken 355 times.
✗ Branch 1 (40→66) not taken.
|
355 | functionEntry->updateType(functionType, false); |
| 46 | 355 | functionEntry->used = true; | |
| 47 | |||
| 48 | // Leave main function body scope | ||
| 49 | 355 | currentScope = rootScope; | |
| 50 | |||
| 51 |
1/2✓ Branch 0 (41→42) taken 355 times.
✗ Branch 1 (41→65) not taken.
|
710 | return nullptr; |
| 52 | 355 | } | |
| 53 | |||
| 54 | 6005 | std::any TypeChecker::visitFctDefPrepare(FctDefNode *node) { | |
| 55 | // Check if name is dtor | ||
| 56 |
3/4✓ Branch 0 (2→3) taken 6005 times.
✗ Branch 1 (2→376) not taken.
✓ Branch 2 (3→4) taken 1 times.
✓ Branch 3 (3→12) taken 6004 times.
|
6005 | if (node->name->name == DTOR_FUNCTION_NAME) |
| 57 |
3/6✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→236) not taken.
✓ Branch 2 (7→8) taken 1 times.
✗ Branch 3 (7→234) not taken.
✓ Branch 4 (10→11) taken 1 times.
✗ Branch 5 (10→240) 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 | 6004 | bool doSetPredecessorsUnreachable = true; | |
| 61 |
3/4✓ Branch 0 (12→13) taken 6004 times.
✗ Branch 1 (12→376) not taken.
✓ Branch 2 (13→14) taken 1 times.
✓ Branch 3 (13→22) taken 6003 times.
|
6004 | if (!node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable)) |
| 62 |
3/6✓ Branch 0 (16→17) taken 1 times.
✗ Branch 1 (16→243) not taken.
✓ Branch 2 (17→18) taken 1 times.
✗ Branch 3 (17→241) not taken.
✓ Branch 4 (20→21) taken 1 times.
✗ Branch 5 (20→247) 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 | 6003 | currentScope = node->scope; | |
| 66 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 6003 times.
|
6003 | assert(currentScope->type == ScopeType::FUNC_PROC_BODY); |
| 67 | |||
| 68 | // Retrieve function template types | ||
| 69 | 6003 | std::vector<GenericType> usedGenericTypes; | |
| 70 |
2/2✓ Branch 0 (24→25) taken 784 times.
✓ Branch 1 (24→54) taken 5219 times.
|
6003 | if (node->hasTemplateTypes) { |
| 71 |
2/2✓ Branch 0 (52→27) taken 945 times.
✓ Branch 1 (52→53) taken 783 times.
|
1728 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { |
| 72 | // Visit template type | ||
| 73 |
2/4✓ Branch 0 (28→29) taken 945 times.
✗ Branch 1 (28→250) not taken.
✓ Branch 2 (29→30) taken 945 times.
✗ Branch 3 (29→248) not taken.
|
945 | auto templateType = std::any_cast<QualType>(visit(dataType)); |
| 74 |
2/4✓ Branch 0 (31→32) taken 945 times.
✗ Branch 1 (31→260) not taken.
✗ Branch 2 (32→33) not taken.
✓ Branch 3 (32→34) taken 945 times.
|
945 | if (templateType.is(TY_UNRESOLVED)) |
| 75 | ✗ | continue; | |
| 76 | // Check if it is a generic type | ||
| 77 |
3/4✓ Branch 0 (34→35) taken 945 times.
✗ Branch 1 (34→260) not taken.
✓ Branch 2 (35→36) taken 1 times.
✓ Branch 3 (35→44) taken 944 times.
|
945 | if (!templateType.is(TY_GENERIC)) |
| 78 |
2/4✓ Branch 0 (39→40) taken 1 times.
✗ Branch 1 (39→254) not taken.
✓ Branch 2 (40→41) taken 1 times.
✗ Branch 3 (40→251) 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 0 (44→45) taken 944 times.
✗ Branch 1 (44→260) not taken.
✓ Branch 2 (45→46) taken 944 times.
✗ Branch 3 (45→260) not taken.
|
944 | GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); |
| 81 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 944 times.
|
944 | assert(genericType != nullptr); |
| 82 |
1/2✓ Branch 0 (48→49) taken 944 times.
✗ Branch 1 (48→260) not taken.
|
944 | usedGenericTypes.push_back(*genericType); |
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | // Retrieve 'this' type | ||
| 87 |
1/2✓ Branch 0 (54→55) taken 6002 times.
✗ Branch 1 (54→374) not taken.
|
6002 | QualType thisType(TY_DYN); // If the function is not a method, the default this type is TY_DYN |
| 88 | 6002 | QualType thisPtrType = thisType; | |
| 89 |
2/2✓ Branch 0 (55→56) taken 2671 times.
✓ Branch 1 (55→85) taken 3331 times.
|
6002 | if (node->isMethod) { |
| 90 | 2671 | Scope *structParentScope = node->structScope->parent; | |
| 91 |
1/2✓ Branch 0 (56→57) taken 2671 times.
✗ Branch 1 (56→374) not taken.
|
2671 | SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName); |
| 92 |
1/2✗ Branch 0 (59→60) not taken.
✓ Branch 1 (59→61) taken 2671 times.
|
2671 | assert(structEntry != nullptr); |
| 93 | // Set struct to used | ||
| 94 | 2671 | structEntry->used = true; | |
| 95 | // Get type and ptr type | ||
| 96 |
1/2✓ Branch 0 (61→62) taken 2671 times.
✗ Branch 1 (61→374) not taken.
|
2671 | thisType = structEntry->getQualType(); |
| 97 |
1/2✓ Branch 0 (62→63) taken 2671 times.
✗ Branch 1 (62→262) not taken.
|
2671 | thisPtrType = thisType.toPtr(node); |
| 98 | // Collect template types of 'this' type | ||
| 99 |
3/4✓ Branch 0 (63→64) taken 2671 times.
✗ Branch 1 (63→264) not taken.
✓ Branch 2 (73→66) taken 875 times.
✓ Branch 3 (73→74) taken 2671 times.
|
3546 | for (const QualType &templateType : thisType.getTemplateTypes()) { |
| 100 | 200 | const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; }; | |
| 101 |
3/4✓ Branch 0 (67→68) taken 875 times.
✗ Branch 1 (67→263) not taken.
✓ Branch 2 (68→69) taken 871 times.
✓ Branch 3 (68→70) taken 4 times.
|
875 | if (std::ranges::none_of(usedGenericTypes, lambda)) |
| 102 |
1/2✓ Branch 0 (69→70) taken 871 times.
✗ Branch 1 (69→263) not taken.
|
871 | usedGenericTypes.emplace_back(templateType); |
| 103 | 875 | usedGenericTypes.back().used = true; | |
| 104 | } | ||
| 105 | |||
| 106 | // Set type of 'this' variable | ||
| 107 |
1/2✓ Branch 0 (76→77) taken 2671 times.
✗ Branch 1 (76→267) not taken.
|
8013 | SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 108 |
1/2✗ Branch 0 (82→83) not taken.
✓ Branch 1 (82→84) taken 2671 times.
|
2671 | assert(thisEntry != nullptr); |
| 109 |
1/2✓ Branch 0 (84→85) taken 2671 times.
✗ Branch 1 (84→374) not taken.
|
2671 | thisEntry->updateType(thisPtrType, false); |
| 110 | } | ||
| 111 | |||
| 112 | // Visit parameters | ||
| 113 | 6002 | QualTypeList paramTypes; | |
| 114 | 6002 | ParamList paramList; | |
| 115 |
2/2✓ Branch 0 (85→86) taken 4579 times.
✓ Branch 1 (85→111) taken 1423 times.
|
6002 | if (node->hasParams) { |
| 116 | 4579 | std::vector<const char *> paramNames; | |
| 117 | // Visit param list to retrieve the param names | ||
| 118 |
2/4✓ Branch 0 (86→87) taken 4579 times.
✗ Branch 1 (86→273) not taken.
✓ Branch 2 (87→88) taken 4579 times.
✗ Branch 3 (87→271) not taken.
|
4579 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); |
| 119 |
2/2✓ Branch 0 (107→91) taken 7691 times.
✓ Branch 1 (107→108) taken 4577 times.
|
12268 | for (const auto &[name, qualType, isOptional] : namedParamList) { |
| 120 |
1/2✓ Branch 0 (92→93) taken 7691 times.
✗ Branch 1 (92→284) not taken.
|
7691 | paramNames.push_back(name); |
| 121 |
1/2✓ Branch 0 (93→94) taken 7691 times.
✗ Branch 1 (93→284) not taken.
|
7691 | paramTypes.push_back(qualType); |
| 122 |
1/2✓ Branch 0 (94→95) taken 7691 times.
✗ Branch 1 (94→274) not taken.
|
7691 | paramList.push_back({qualType, isOptional}); |
| 123 | // Check if the type is present in the template for generic types | ||
| 124 |
3/4✓ Branch 0 (95→96) taken 7691 times.
✗ Branch 1 (95→284) not taken.
✓ Branch 2 (96→97) taken 2 times.
✓ Branch 3 (96→105) taken 7689 times.
|
7691 | if (!qualType.isCoveredByGenericTypeList(usedGenericTypes)) |
| 125 | ✗ | throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE, | |
| 126 |
2/4✓ Branch 0 (100→101) taken 2 times.
✗ Branch 1 (100→278) not taken.
✓ Branch 2 (101→102) taken 2 times.
✗ Branch 3 (101→275) not taken.
|
6 | "Generic param type not included in the template type list of the function"); |
| 127 | } | ||
| 128 | 4581 | } | |
| 129 | |||
| 130 | // Retrieve return type | ||
| 131 |
2/4✓ Branch 0 (111→112) taken 6000 times.
✗ Branch 1 (111→292) not taken.
✓ Branch 2 (112→113) taken 6000 times.
✗ Branch 3 (112→290) not taken.
|
6000 | auto returnType = std::any_cast<QualType>(visit(node->returnType)); |
| 132 |
2/6✓ Branch 0 (114→115) taken 6000 times.
✗ Branch 1 (114→370) not taken.
✗ Branch 2 (115→116) not taken.
✓ Branch 3 (115→118) taken 6000 times.
✗ Branch 4 (116→117) not taken.
✗ Branch 5 (116→293) not taken.
|
6000 | HANDLE_UNRESOLVED_TYPE_PTR(returnType) |
| 133 |
3/4✓ Branch 0 (118→119) taken 6000 times.
✗ Branch 1 (118→370) not taken.
✓ Branch 2 (119→120) taken 1 times.
✓ Branch 3 (119→128) taken 5999 times.
|
6000 | if (returnType.is(TY_DYN)) |
| 134 |
3/6✓ Branch 0 (122→123) taken 1 times.
✗ Branch 1 (122→296) not taken.
✓ Branch 2 (123→124) taken 1 times.
✗ Branch 3 (123→294) not taken.
✓ Branch 4 (126→127) taken 1 times.
✗ Branch 5 (126→300) not taken.
|
3 | SOFT_ERROR_BOOL(node, UNEXPECTED_DYN_TYPE, "Dyn return types are not allowed") |
| 135 |
3/4✓ Branch 0 (128→129) taken 5999 times.
✗ Branch 1 (128→370) not taken.
✓ Branch 2 (129→130) taken 1 times.
✓ Branch 3 (129→138) taken 5998 times.
|
5999 | if (!returnType.isCoveredByGenericTypeList(usedGenericTypes)) |
| 136 |
3/6✓ Branch 0 (132→133) taken 1 times.
✗ Branch 1 (132→303) not taken.
✓ Branch 2 (133→134) taken 1 times.
✗ Branch 3 (133→301) not taken.
✓ Branch 4 (136→137) taken 1 times.
✗ Branch 5 (136→307) 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 | 5998 | currentScope = node->scope->parent; | |
| 141 |
3/4✓ Branch 0 (138→139) taken 2671 times.
✓ Branch 1 (138→141) taken 3327 times.
✗ Branch 2 (139→140) not taken.
✓ Branch 3 (139→141) taken 2671 times.
|
5998 | assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT); |
| 142 | |||
| 143 | // Prepare type of function | ||
| 144 |
2/4✓ Branch 0 (141→142) taken 5998 times.
✗ Branch 1 (141→308) not taken.
✓ Branch 2 (142→143) taken 5998 times.
✗ Branch 3 (142→308) not taken.
|
5998 | QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes); |
| 145 | 5998 | functionType.setQualifiers(node->qualifiers); | |
| 146 | |||
| 147 | // Update type of function entry | ||
| 148 |
1/2✓ Branch 0 (144→145) taken 5998 times.
✗ Branch 1 (144→311) not taken.
|
11996 | SymbolTableEntry *functionEntry = currentScope->lookupStrict(node->getSymbolTableEntryName()); |
| 149 |
1/2✗ Branch 0 (149→150) not taken.
✓ Branch 1 (149→151) taken 5998 times.
|
5998 | assert(functionEntry != nullptr); |
| 150 |
1/2✓ Branch 0 (151→152) taken 5998 times.
✗ Branch 1 (151→370) not taken.
|
5998 | functionEntry->updateType(functionType, false); |
| 151 | |||
| 152 | // Build function object | ||
| 153 |
3/6✓ Branch 0 (152→153) taken 5998 times.
✗ Branch 1 (152→318) not taken.
✓ Branch 2 (153→154) taken 5998 times.
✗ Branch 3 (153→315) not taken.
✓ Branch 4 (154→155) taken 5998 times.
✗ Branch 5 (154→312) not taken.
|
5998 | Function spiceFunc(node->name->name, functionEntry, thisType, returnType, paramList, usedGenericTypes, node); |
| 154 | 5998 | spiceFunc.bodyScope = node->scope; | |
| 155 |
2/2✓ Branch 0 (159→160) taken 5997 times.
✓ Branch 1 (159→368) taken 1 times.
|
5998 | FunctionManager::insert(currentScope, spiceFunc, &node->manifestations); |
| 156 | |||
| 157 | // Check function attributes | ||
| 158 |
2/2✓ Branch 0 (160→161) taken 323 times.
✓ Branch 1 (160→208) taken 5674 times.
|
5997 | if (node->attrs) { |
| 159 | 323 | const AttrLstNode *attrLst = node->attrs->attrLst; | |
| 160 | 323 | Function *firstManifestation = node->manifestations.front(); | |
| 161 |
3/6✓ Branch 0 (164→165) taken 323 times.
✗ Branch 1 (164→321) not taken.
✓ Branch 2 (165→166) taken 323 times.
✗ Branch 3 (165→319) not taken.
✗ Branch 4 (168→169) not taken.
✓ Branch 5 (168→170) taken 323 times.
|
969 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE)) |
| 162 | ✗ | firstManifestation->mangleFunctionName = value->boolValue; | |
| 163 |
3/6✓ Branch 0 (172→173) taken 323 times.
✗ Branch 1 (172→327) not taken.
✓ Branch 2 (173→174) taken 323 times.
✗ Branch 3 (173→325) not taken.
✗ Branch 4 (176→177) not taken.
✓ Branch 5 (176→179) taken 323 times.
|
969 | 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 0 (181→182) taken 323 times.
✗ Branch 1 (181→333) not taken.
✓ Branch 2 (182→183) taken 323 times.
✗ Branch 3 (182→331) not taken.
✓ Branch 4 (185→186) taken 12 times.
✓ Branch 5 (185→208) taken 311 times.
✓ Branch 6 (186→187) taken 11 times.
✓ Branch 7 (186→208) taken 1 times.
|
969 | 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 0 (187→188) taken 1 times.
✓ Branch 1 (187→196) taken 10 times.
|
11 | if (node->hasParams) |
| 170 |
2/4✓ Branch 0 (191→192) taken 1 times.
✗ Branch 1 (191→340) not taken.
✓ Branch 2 (192→193) taken 1 times.
✗ Branch 3 (192→337) not taken.
|
3 | throw SemanticError(node->paramLst, TEST_FUNCTION_WITH_PARAMS, "Test function may not have parameters"); |
| 171 |
3/4✓ Branch 0 (196→197) taken 10 times.
✗ Branch 1 (196→368) not taken.
✓ Branch 2 (197→198) taken 1 times.
✓ Branch 3 (197→206) taken 9 times.
|
10 | if (!returnType.is(TY_BOOL)) |
| 172 |
2/4✓ Branch 0 (201→202) taken 1 times.
✗ Branch 1 (201→349) not taken.
✓ Branch 2 (202→203) taken 1 times.
✗ Branch 3 (202→346) 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 0 (207→208) taken 9 times.
✗ Branch 1 (207→368) 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 0 (218→209) taken 602 times.
✓ Branch 1 (218→219) taken 5995 times.
|
6597 | for (size_t i = 1; i < node->manifestations.size(); i++) { |
| 182 |
4/8✓ Branch 0 (209→210) taken 602 times.
✗ Branch 1 (209→360) not taken.
✓ Branch 2 (210→211) taken 602 times.
✗ Branch 3 (210→360) not taken.
✓ Branch 4 (211→212) taken 602 times.
✗ Branch 5 (211→357) not taken.
✓ Branch 6 (212→213) taken 602 times.
✗ Branch 7 (212→355) not taken.
|
602 | Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false)); |
| 183 |
1/2✓ Branch 0 (215→216) taken 602 times.
✗ Branch 1 (215→368) not taken.
|
602 | node->manifestations.at(i)->bodyScope = scope; |
| 184 | } | ||
| 185 |
3/6✓ Branch 0 (220→221) taken 5995 times.
✗ Branch 1 (220→366) not taken.
✓ Branch 2 (221→222) taken 5995 times.
✗ Branch 3 (221→363) not taken.
✓ Branch 4 (222→223) taken 5995 times.
✗ Branch 5 (222→361) not taken.
|
5995 | currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false)); |
| 186 | |||
| 187 | // Change to the root scope | ||
| 188 | 5995 | currentScope = rootScope; | |
| 189 |
1/2✗ Branch 0 (225→226) not taken.
✓ Branch 1 (225→227) taken 5995 times.
|
5995 | assert(currentScope->type == ScopeType::GLOBAL); |
| 190 | |||
| 191 |
1/2✓ Branch 0 (227→228) taken 5995 times.
✗ Branch 1 (227→367) not taken.
|
5995 | return nullptr; |
| 192 | 6016 | } | |
| 193 | |||
| 194 | 2950 | std::any TypeChecker::visitProcDefPrepare(ProcDefNode *node) { | |
| 195 | // Mark unreachable statements | ||
| 196 | 2950 | bool doSetPredecessorsUnreachable = true; | |
| 197 |
1/2✓ Branch 0 (2→3) taken 2950 times.
✗ Branch 1 (2→273) not taken.
|
2950 | node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable); |
| 198 | |||
| 199 | // Check if dtor and has params | ||
| 200 |
7/8✓ Branch 0 (3→4) taken 2093 times.
✓ Branch 1 (3→7) taken 857 times.
✓ Branch 2 (4→5) taken 2093 times.
✗ Branch 3 (4→273) not taken.
✓ Branch 4 (5→6) taken 1 times.
✓ Branch 5 (5→7) taken 2092 times.
✓ Branch 6 (8→9) taken 1 times.
✓ Branch 7 (8→17) taken 2949 times.
|
2950 | if (node->hasParams && node->name->name == DTOR_FUNCTION_NAME) |
| 201 |
2/4✓ Branch 0 (12→13) taken 1 times.
✗ Branch 1 (12→179) not taken.
✓ Branch 2 (13→14) taken 1 times.
✗ Branch 3 (13→176) 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 | 2949 | currentScope = node->scope; | |
| 205 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 2949 times.
|
2949 | assert(currentScope->type == ScopeType::FUNC_PROC_BODY); |
| 206 | |||
| 207 | // Retrieve procedure template types | ||
| 208 | 2949 | std::vector<GenericType> usedGenericTypes; | |
| 209 |
2/2✓ Branch 0 (19→20) taken 573 times.
✓ Branch 1 (19→49) taken 2376 times.
|
2949 | if (node->hasTemplateTypes) { |
| 210 |
2/2✓ Branch 0 (47→22) taken 637 times.
✓ Branch 1 (47→48) taken 572 times.
|
1209 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { |
| 211 | // Visit template type | ||
| 212 |
2/4✓ Branch 0 (23→24) taken 637 times.
✗ Branch 1 (23→187) not taken.
✓ Branch 2 (24→25) taken 637 times.
✗ Branch 3 (24→185) not taken.
|
637 | auto templateType = std::any_cast<QualType>(visit(dataType)); |
| 213 |
2/4✓ Branch 0 (26→27) taken 637 times.
✗ Branch 1 (26→197) not taken.
✗ Branch 2 (27→28) not taken.
✓ Branch 3 (27→29) taken 637 times.
|
637 | if (templateType.is(TY_UNRESOLVED)) |
| 214 | ✗ | continue; | |
| 215 | // Check if it is a generic type | ||
| 216 |
3/4✓ Branch 0 (29→30) taken 637 times.
✗ Branch 1 (29→197) not taken.
✓ Branch 2 (30→31) taken 1 times.
✓ Branch 3 (30→39) taken 636 times.
|
637 | if (!templateType.is(TY_GENERIC)) |
| 217 |
2/4✓ Branch 0 (34→35) taken 1 times.
✗ Branch 1 (34→191) not taken.
✓ Branch 2 (35→36) taken 1 times.
✗ Branch 3 (35→188) 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 0 (39→40) taken 636 times.
✗ Branch 1 (39→197) not taken.
✓ Branch 2 (40→41) taken 636 times.
✗ Branch 3 (40→197) not taken.
|
636 | const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); |
| 220 |
1/2✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→43) taken 636 times.
|
636 | assert(genericType != nullptr); |
| 221 |
1/2✓ Branch 0 (43→44) taken 636 times.
✗ Branch 1 (43→197) not taken.
|
636 | usedGenericTypes.push_back(*genericType); |
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | // Retrieve 'this' type | ||
| 226 |
1/2✓ Branch 0 (49→50) taken 2948 times.
✗ Branch 1 (49→271) not taken.
|
2948 | QualType thisType(TY_DYN); // If the procedure is not a method, the default this type is TY_DYN |
| 227 | 2948 | QualType thisPtrType = thisType; | |
| 228 |
2/2✓ Branch 0 (50→51) taken 2454 times.
✓ Branch 1 (50→80) taken 494 times.
|
2948 | if (node->isMethod) { |
| 229 | 2454 | Scope *structParentScope = node->structScope->parent; | |
| 230 |
1/2✓ Branch 0 (51→52) taken 2454 times.
✗ Branch 1 (51→271) not taken.
|
2454 | SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName); |
| 231 |
1/2✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→56) taken 2454 times.
|
2454 | assert(structEntry != nullptr); |
| 232 | // Set struct to used | ||
| 233 | 2454 | structEntry->used = true; | |
| 234 | // Get type and ptr type | ||
| 235 |
1/2✓ Branch 0 (56→57) taken 2454 times.
✗ Branch 1 (56→271) not taken.
|
2454 | thisType = structEntry->getQualType(); |
| 236 |
1/2✓ Branch 0 (57→58) taken 2454 times.
✗ Branch 1 (57→199) not taken.
|
2454 | thisPtrType = thisType.toPtr(node); |
| 237 | // Collect template types of 'this' type | ||
| 238 |
3/4✓ Branch 0 (58→59) taken 2454 times.
✗ Branch 1 (58→201) not taken.
✓ Branch 2 (68→61) taken 839 times.
✓ Branch 3 (68→69) taken 2454 times.
|
3293 | for (const QualType &templateType : thisType.getTemplateTypes()) { |
| 239 | 260 | const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; }; | |
| 240 |
3/4✓ Branch 0 (62→63) taken 839 times.
✗ Branch 1 (62→200) not taken.
✓ Branch 2 (63→64) taken 778 times.
✓ Branch 3 (63→65) taken 61 times.
|
839 | if (std::ranges::none_of(usedGenericTypes, lambda)) |
| 241 |
1/2✓ Branch 0 (64→65) taken 778 times.
✗ Branch 1 (64→200) not taken.
|
778 | usedGenericTypes.emplace_back(templateType); |
| 242 | 839 | usedGenericTypes.back().used = true; | |
| 243 | } | ||
| 244 | |||
| 245 | // Set type of 'this' variable | ||
| 246 |
1/2✓ Branch 0 (71→72) taken 2454 times.
✗ Branch 1 (71→204) not taken.
|
7362 | SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 247 |
1/2✗ Branch 0 (77→78) not taken.
✓ Branch 1 (77→79) taken 2454 times.
|
2454 | assert(thisEntry != nullptr); |
| 248 |
1/2✓ Branch 0 (79→80) taken 2454 times.
✗ Branch 1 (79→271) not taken.
|
2454 | thisEntry->updateType(thisPtrType, false); |
| 249 | } | ||
| 250 | |||
| 251 | // Visit parameters | ||
| 252 | 2948 | QualTypeList paramTypes; | |
| 253 | 2948 | ParamList paramList; | |
| 254 |
2/2✓ Branch 0 (80→81) taken 2092 times.
✓ Branch 1 (80→106) taken 856 times.
|
2948 | if (node->hasParams) { |
| 255 | 2092 | std::vector<const char *> paramNames; | |
| 256 | // Visit param list to retrieve the param names | ||
| 257 |
2/4✓ Branch 0 (81→82) taken 2092 times.
✗ Branch 1 (81→210) not taken.
✓ Branch 2 (82→83) taken 2092 times.
✗ Branch 3 (82→208) not taken.
|
2092 | auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst)); |
| 258 |
2/2✓ Branch 0 (102→86) taken 2609 times.
✓ Branch 1 (102→103) taken 2091 times.
|
4700 | for (const auto &[name, qualType, isOptional] : namedParamList) { |
| 259 |
1/2✓ Branch 0 (87→88) taken 2609 times.
✗ Branch 1 (87→221) not taken.
|
2609 | paramNames.push_back(name); |
| 260 |
1/2✓ Branch 0 (88→89) taken 2609 times.
✗ Branch 1 (88→221) not taken.
|
2609 | paramTypes.push_back(qualType); |
| 261 |
1/2✓ Branch 0 (89→90) taken 2609 times.
✗ Branch 1 (89→211) not taken.
|
2609 | paramList.push_back({qualType, isOptional}); |
| 262 | // Check if the type is present in the template for generic types | ||
| 263 |
3/4✓ Branch 0 (90→91) taken 2609 times.
✗ Branch 1 (90→221) not taken.
✓ Branch 2 (91→92) taken 1 times.
✓ Branch 3 (91→100) taken 2608 times.
|
2609 | if (!qualType.isCoveredByGenericTypeList(usedGenericTypes)) |
| 264 | ✗ | throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE, | |
| 265 |
2/4✓ Branch 0 (95→96) taken 1 times.
✗ Branch 1 (95→215) not taken.
✓ Branch 2 (96→97) taken 1 times.
✗ Branch 3 (96→212) not taken.
|
3 | "Generic param type not included in the template type list of the procedure"); |
| 266 | } | ||
| 267 | 2093 | } | |
| 268 | |||
| 269 | // Leave procedure body scope | ||
| 270 | 2947 | currentScope = node->scope->parent; | |
| 271 |
3/4✓ Branch 0 (106→107) taken 2454 times.
✓ Branch 1 (106→109) taken 493 times.
✗ Branch 2 (107→108) not taken.
✓ Branch 3 (107→109) taken 2454 times.
|
2947 | assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT); |
| 272 | |||
| 273 | // Prepare type of procedure | ||
| 274 |
3/6✓ Branch 0 (109→110) taken 2947 times.
✗ Branch 1 (109→228) not taken.
✓ Branch 2 (110→111) taken 2947 times.
✗ Branch 3 (110→227) not taken.
✓ Branch 4 (111→112) taken 2947 times.
✗ Branch 5 (111→227) not taken.
|
2947 | QualType procedureType = QualType(TY_PROCEDURE).getWithFunctionParamAndReturnTypes(QualType(TY_DYN), paramTypes); |
| 275 | 2947 | procedureType.setQualifiers(node->qualifiers); | |
| 276 | |||
| 277 | // Update type of procedure entry | ||
| 278 |
1/2✓ Branch 0 (113→114) taken 2947 times.
✗ Branch 1 (113→231) not taken.
|
5894 | SymbolTableEntry *procedureEntry = currentScope->lookupStrict(node->getSymbolTableEntryName()); |
| 279 |
1/2✗ Branch 0 (118→119) not taken.
✓ Branch 1 (118→120) taken 2947 times.
|
2947 | assert(procedureEntry != nullptr); |
| 280 |
1/2✓ Branch 0 (120→121) taken 2947 times.
✗ Branch 1 (120→267) not taken.
|
2947 | procedureEntry->updateType(procedureType, false); |
| 281 | |||
| 282 | // Build procedure object | ||
| 283 |
4/8✓ Branch 0 (121→122) taken 2947 times.
✗ Branch 1 (121→239) not taken.
✓ Branch 2 (122→123) taken 2947 times.
✗ Branch 3 (122→236) not taken.
✓ Branch 4 (123→124) taken 2947 times.
✗ Branch 5 (123→233) not taken.
✓ Branch 6 (124→125) taken 2947 times.
✗ Branch 7 (124→232) not taken.
|
2947 | Function spiceProc(node->name->name, procedureEntry, thisType, QualType(TY_DYN), paramList, usedGenericTypes, node); |
| 284 | 2947 | spiceProc.bodyScope = node->scope; | |
| 285 |
2/2✓ Branch 0 (129→130) taken 2946 times.
✓ Branch 1 (129→265) taken 1 times.
|
2947 | FunctionManager::insert(currentScope, spiceProc, &node->manifestations); |
| 286 | |||
| 287 | // Check procedure attributes | ||
| 288 |
1/2✗ Branch 0 (130→131) not taken.
✓ Branch 1 (130→150) taken 2946 times.
|
2946 | 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 0 (160→151) taken 140 times.
✓ Branch 1 (160→161) taken 2946 times.
|
3086 | for (size_t i = 1; i < node->manifestations.size(); i++) { |
| 300 |
4/8✓ Branch 0 (151→152) taken 140 times.
✗ Branch 1 (151→257) not taken.
✓ Branch 2 (152→153) taken 140 times.
✗ Branch 3 (152→257) not taken.
✓ Branch 4 (153→154) taken 140 times.
✗ Branch 5 (153→254) not taken.
✓ Branch 6 (154→155) taken 140 times.
✗ Branch 7 (154→252) not taken.
|
140 | Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false)); |
| 301 |
1/2✓ Branch 0 (157→158) taken 140 times.
✗ Branch 1 (157→265) not taken.
|
140 | node->manifestations.at(i)->bodyScope = scope; |
| 302 | } | ||
| 303 |
3/6✓ Branch 0 (162→163) taken 2946 times.
✗ Branch 1 (162→263) not taken.
✓ Branch 2 (163→164) taken 2946 times.
✗ Branch 3 (163→260) not taken.
✓ Branch 4 (164→165) taken 2946 times.
✗ Branch 5 (164→258) not taken.
|
2946 | currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false)); |
| 304 | |||
| 305 | // Change to the root scope | ||
| 306 | 2946 | currentScope = rootScope; | |
| 307 |
1/2✗ Branch 0 (167→168) not taken.
✓ Branch 1 (167→169) taken 2946 times.
|
2946 | assert(currentScope->type == ScopeType::GLOBAL); |
| 308 | |||
| 309 |
1/2✓ Branch 0 (169→170) taken 2946 times.
✗ Branch 1 (169→264) not taken.
|
5892 | return nullptr; |
| 310 | 2954 | } | |
| 311 | |||
| 312 | 585 | std::any TypeChecker::visitStructDefPrepare(StructDefNode *node) { | |
| 313 | 585 | QualTypeList usedTemplateTypes; | |
| 314 | 585 | std::vector<GenericType> templateTypesGeneric; | |
| 315 | |||
| 316 | // Retrieve struct template types | ||
| 317 |
2/2✓ Branch 0 (2→3) taken 180 times.
✓ Branch 1 (2→37) taken 405 times.
|
585 | if (node->hasTemplateTypes) { |
| 318 |
1/2✓ Branch 0 (4→5) taken 180 times.
✗ Branch 1 (4→288) not taken.
|
180 | usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size()); |
| 319 |
1/2✓ Branch 0 (6→7) taken 180 times.
✗ Branch 1 (6→288) not taken.
|
180 | templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size()); |
| 320 |
2/2✓ Branch 0 (35→9) taken 244 times.
✓ Branch 1 (35→36) taken 180 times.
|
424 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { |
| 321 | // Visit template type | ||
| 322 |
2/4✓ Branch 0 (10→11) taken 244 times.
✗ Branch 1 (10→194) not taken.
✓ Branch 2 (11→12) taken 244 times.
✗ Branch 3 (11→192) not taken.
|
244 | auto templateType = std::any_cast<QualType>(visit(dataType)); |
| 323 |
2/4✓ Branch 0 (13→14) taken 244 times.
✗ Branch 1 (13→204) not taken.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→16) taken 244 times.
|
244 | if (templateType.is(TY_UNRESOLVED)) |
| 324 | ✗ | continue; | |
| 325 | // Check if it is a generic type | ||
| 326 |
2/4✓ Branch 0 (16→17) taken 244 times.
✗ Branch 1 (16→204) not taken.
✗ Branch 2 (17→18) not taken.
✓ Branch 3 (17→26) taken 244 times.
|
244 | 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 0 (26→27) taken 244 times.
✗ Branch 1 (26→204) not taken.
✓ Branch 2 (27→28) taken 244 times.
✗ Branch 3 (27→204) not taken.
|
244 | GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); |
| 330 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 244 times.
|
244 | assert(genericType != nullptr); |
| 331 |
1/2✓ Branch 0 (30→31) taken 244 times.
✗ Branch 1 (30→204) not taken.
|
244 | usedTemplateTypes.push_back(*genericType); |
| 332 |
1/2✓ Branch 0 (31→32) taken 244 times.
✗ Branch 1 (31→204) not taken.
|
244 | templateTypesGeneric.push_back(*genericType); |
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | // Retrieve implemented interfaces | ||
| 337 | 585 | QualTypeList interfaceTypes; | |
| 338 |
2/2✓ Branch 0 (37→38) taken 93 times.
✓ Branch 1 (37→89) taken 492 times.
|
585 | if (node->hasInterfaces) { |
| 339 |
1/2✓ Branch 0 (39→40) taken 93 times.
✗ Branch 1 (39→286) not taken.
|
93 | interfaceTypes.reserve(node->interfaceTypeLst->dataTypes.size()); |
| 340 |
2/2✓ Branch 0 (87→42) taken 93 times.
✓ Branch 1 (87→88) taken 92 times.
|
185 | for (DataTypeNode *interfaceNode : node->interfaceTypeLst->dataTypes) { |
| 341 | // Visit interface type | ||
| 342 |
2/4✓ Branch 0 (43→44) taken 93 times.
✗ Branch 1 (43→208) not taken.
✓ Branch 2 (44→45) taken 93 times.
✗ Branch 3 (44→206) not taken.
|
93 | auto interfaceType = std::any_cast<QualType>(visit(interfaceNode)); |
| 343 |
2/4✓ Branch 0 (46→47) taken 93 times.
✗ Branch 1 (46→230) not taken.
✗ Branch 2 (47→48) not taken.
✓ Branch 3 (47→49) taken 93 times.
|
93 | if (interfaceType.is(TY_UNRESOLVED)) |
| 344 | ✗ | continue; | |
| 345 | // Check if it is an interface type | ||
| 346 |
2/4✓ Branch 0 (49→50) taken 93 times.
✗ Branch 1 (49→230) not taken.
✗ Branch 2 (50→51) not taken.
✓ Branch 3 (50→58) taken 93 times.
|
93 | 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 0 (58→59) taken 93 times.
✗ Branch 1 (58→230) not taken.
✓ Branch 2 (59→60) taken 93 times.
✗ Branch 3 (59→230) not taken.
✓ Branch 4 (60→61) taken 79 times.
✓ Branch 5 (60→64) taken 14 times.
✓ Branch 6 (61→62) taken 79 times.
✗ Branch 7 (61→230) not taken.
✓ Branch 8 (62→63) taken 1 times.
✓ Branch 9 (62→64) taken 78 times.
✓ Branch 10 (65→66) taken 1 times.
✓ Branch 11 (65→74) taken 92 times.
|
93 | if (interfaceType.getBodyScope()->isImportedBy(rootScope) && !interfaceType.isPublic()) |
| 351 | ✗ | throw SemanticError(node, INSUFFICIENT_VISIBILITY, | |
| 352 |
4/8✓ Branch 0 (67→68) taken 1 times.
✗ Branch 1 (67→223) not taken.
✓ Branch 2 (68→69) taken 1 times.
✗ Branch 3 (68→223) not taken.
✓ Branch 4 (69→70) taken 1 times.
✗ Branch 5 (69→221) not taken.
✓ Branch 6 (70→71) taken 1 times.
✗ Branch 7 (70→218) not taken.
|
1 | "Cannot access interface '" + interfaceType.getSubType() + "' due to its private visibility"); |
| 353 | // Add to interface types | ||
| 354 |
1/2✓ Branch 0 (74→75) taken 92 times.
✗ Branch 1 (74→230) not taken.
|
92 | interfaceTypes.push_back(interfaceType); |
| 355 | // Update the type of the entry for that interface field | ||
| 356 | 92 | const std::string &interfaceName = interfaceNode->baseDataType->customDataType->typeNameFragments.back(); | |
| 357 |
1/2✓ Branch 0 (76→77) taken 92 times.
✗ Branch 1 (76→229) not taken.
|
184 | SymbolTableEntry *interfaceEntry = node->structScope->lookupStrict("this." + interfaceName); |
| 358 |
1/2✗ Branch 0 (81→82) not taken.
✓ Branch 1 (81→83) taken 92 times.
|
92 | assert(interfaceEntry != nullptr); |
| 359 |
1/2✓ Branch 0 (83→84) taken 92 times.
✗ Branch 1 (83→230) not taken.
|
92 | interfaceEntry->updateType(interfaceType, false); |
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | // Update type of struct entry | ||
| 364 |
1/2✗ Branch 0 (89→90) not taken.
✓ Branch 1 (89→91) taken 584 times.
|
584 | assert(node->entry != nullptr); |
| 365 | 584 | const TypeChainElementData data = {.bodyScope = node->structScope}; | |
| 366 |
1/2✓ Branch 0 (91→92) taken 584 times.
✗ Branch 1 (91→286) not taken.
|
584 | const Type *type = TypeRegistry::getOrInsert(TY_STRUCT, node->structName, node->typeId, data, usedTemplateTypes); |
| 367 |
2/4✓ Branch 0 (92→93) taken 584 times.
✗ Branch 1 (92→232) not taken.
✓ Branch 2 (93→94) taken 584 times.
✗ Branch 3 (93→232) not taken.
|
584 | node->entry->updateType(QualType(type, node->qualifiers), false); |
| 368 | |||
| 369 | // Change to struct scope | ||
| 370 | 584 | currentScope = node->structScope; | |
| 371 |
1/2✗ Branch 0 (94→95) not taken.
✓ Branch 1 (94→96) taken 584 times.
|
584 | assert(currentScope->type == ScopeType::STRUCT); |
| 372 | |||
| 373 | // Retrieve field types | ||
| 374 | 584 | QualTypeList fieldTypes; | |
| 375 |
1/2✓ Branch 0 (97→98) taken 584 times.
✗ Branch 1 (97→284) not taken.
|
584 | fieldTypes.reserve(node->fields.size()); |
| 376 |
2/2✓ Branch 0 (141→100) taken 1260 times.
✓ Branch 1 (141→142) taken 581 times.
|
1841 | for (FieldNode *field : node->fields) { |
| 377 | // Visit field type | ||
| 378 |
2/4✓ Branch 0 (101→102) taken 1260 times.
✗ Branch 1 (101→235) not taken.
✓ Branch 2 (102→103) taken 1260 times.
✗ Branch 3 (102→233) not taken.
|
1260 | auto fieldType = std::any_cast<QualType>(visit(field)); |
| 379 |
3/4✓ Branch 0 (104→105) taken 1260 times.
✗ Branch 1 (104→254) not taken.
✓ Branch 2 (105→106) taken 2 times.
✓ Branch 3 (105→107) taken 1258 times.
|
1260 | if (fieldType.is(TY_UNRESOLVED)) |
| 380 |
1/2✗ Branch 0 (106→107) not taken.
✓ Branch 1 (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 0 (107→108) taken 1258 times.
✗ Branch 1 (107→254) not taken.
✓ Branch 2 (108→109) taken 131 times.
✓ Branch 3 (108→112) taken 1127 times.
✓ Branch 4 (109→110) taken 131 times.
✗ Branch 5 (109→254) not taken.
✗ Branch 6 (110→111) not taken.
✓ Branch 7 (110→112) taken 131 times.
✗ Branch 8 (113→114) not taken.
✓ Branch 9 (113→122) taken 1258 times.
|
1258 | 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 0 (122→123) taken 1258 times.
✗ Branch 1 (122→254) not taken.
|
1258 | fieldTypes.push_back(fieldType); |
| 389 | |||
| 390 | // Update type of field entry | ||
| 391 |
1/2✓ Branch 0 (123→124) taken 1258 times.
✗ Branch 1 (123→254) not taken.
|
1258 | SymbolTableEntry *fieldEntry = currentScope->lookupStrict(field->fieldName); |
| 392 |
1/2✗ Branch 0 (126→127) not taken.
✓ Branch 1 (126→128) taken 1258 times.
|
1258 | assert(fieldEntry != nullptr); |
| 393 |
1/2✓ Branch 0 (128→129) taken 1258 times.
✗ Branch 1 (128→254) not taken.
|
1258 | fieldEntry->updateType(fieldType, false); |
| 394 | |||
| 395 | // Check if the template type list contains this type | ||
| 396 |
3/4✓ Branch 0 (129→130) taken 1258 times.
✗ Branch 1 (129→254) not taken.
✓ Branch 2 (130→131) taken 1 times.
✓ Branch 3 (130→139) taken 1257 times.
|
1258 | if (!fieldType.isCoveredByGenericTypeList(templateTypesGeneric)) |
| 397 |
2/4✓ Branch 0 (134→135) taken 1 times.
✗ Branch 1 (134→248) not taken.
✓ Branch 2 (135→136) taken 1 times.
✗ Branch 3 (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 | 581 | currentScope = rootScope; | |
| 402 |
1/2✗ Branch 0 (142→143) not taken.
✓ Branch 1 (142→144) taken 581 times.
|
581 | assert(currentScope->type == ScopeType::GLOBAL); |
| 403 | |||
| 404 | // Build struct object | ||
| 405 |
4/8✓ Branch 0 (144→145) taken 581 times.
✗ Branch 1 (144→265) not taken.
✓ Branch 2 (145→146) taken 581 times.
✗ Branch 3 (145→262) not taken.
✓ Branch 4 (146→147) taken 581 times.
✗ Branch 5 (146→259) not taken.
✓ Branch 6 (147→148) taken 581 times.
✗ Branch 7 (147→256) not taken.
|
581 | Struct spiceStruct(node->structName, node->entry, node->structScope, fieldTypes, templateTypesGeneric, interfaceTypes, node); |
| 406 |
1/2✓ Branch 0 (153→154) taken 581 times.
✗ Branch 1 (153→282) not taken.
|
581 | StructManager::insert(currentScope, spiceStruct, &node->structManifestations); |
| 407 | 581 | spiceStruct.scope = node->structScope; | |
| 408 | |||
| 409 | // Request RTTI runtime if the struct is polymorphic | ||
| 410 | 581 | node->emitVTable |= node->hasInterfaces; | |
| 411 |
12/18✓ Branch 0 (154→155) taken 53 times.
✓ Branch 1 (154→161) taken 528 times.
✓ Branch 2 (157→158) taken 53 times.
✗ Branch 3 (157→266) not taken.
✓ Branch 4 (158→159) taken 53 times.
✗ Branch 5 (158→266) not taken.
✓ Branch 6 (159→160) taken 52 times.
✓ Branch 7 (159→161) taken 1 times.
✓ Branch 8 (162→163) taken 53 times.
✓ Branch 9 (162→164) taken 528 times.
✓ Branch 10 (164→165) taken 53 times.
✓ Branch 11 (164→167) taken 528 times.
✓ Branch 12 (167→168) taken 52 times.
✓ Branch 13 (167→175) taken 529 times.
✗ Branch 14 (266→267) not taken.
✗ Branch 15 (266→268) not taken.
✗ Branch 16 (270→271) not taken.
✗ Branch 17 (270→273) not taken.
|
687 | if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_EMIT_VTABLE)) |
| 412 |
2/4✓ Branch 0 (170→171) taken 52 times.
✗ Branch 1 (170→277) not taken.
✓ Branch 2 (171→172) taken 52 times.
✗ Branch 3 (171→275) not taken.
|
156 | node->emitVTable |= node->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_EMIT_VTABLE)->boolValue; |
| 413 |
7/8✓ Branch 0 (175→176) taken 144 times.
✓ Branch 1 (175→181) taken 437 times.
✓ Branch 2 (176→177) taken 144 times.
✗ Branch 3 (176→282) not taken.
✓ Branch 4 (179→180) taken 93 times.
✓ Branch 5 (179→181) taken 51 times.
✓ Branch 6 (182→183) taken 93 times.
✓ Branch 7 (182→184) taken 488 times.
|
725 | if (node->emitVTable && !sourceFile->isRttiRT()) |
| 414 |
1/2✓ Branch 0 (183→184) taken 93 times.
✗ Branch 1 (183→282) not taken.
|
93 | sourceFile->requestRuntimeModule(RTTI_RT); |
| 415 | |||
| 416 |
1/2✓ Branch 0 (184→185) taken 581 times.
✗ Branch 1 (184→281) not taken.
|
1162 | return nullptr; |
| 417 | 596 | } | |
| 418 | |||
| 419 | 80 | std::any TypeChecker::visitInterfaceDefPrepare(InterfaceDefNode *node) { | |
| 420 | 80 | QualTypeList usedTemplateTypes; | |
| 421 | 80 | std::vector<GenericType> templateTypesGeneric; | |
| 422 | |||
| 423 | // Retrieve interface template types | ||
| 424 |
2/2✓ Branch 0 (2→3) taken 60 times.
✓ Branch 1 (2→37) taken 20 times.
|
80 | if (node->hasTemplateTypes) { |
| 425 |
1/2✓ Branch 0 (4→5) taken 60 times.
✗ Branch 1 (4→122) not taken.
|
60 | usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size()); |
| 426 |
1/2✓ Branch 0 (6→7) taken 60 times.
✗ Branch 1 (6→122) not taken.
|
60 | templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size()); |
| 427 |
2/2✓ Branch 0 (35→9) taken 60 times.
✓ Branch 1 (35→36) taken 60 times.
|
120 | for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) { |
| 428 | // Visit template type | ||
| 429 |
2/4✓ Branch 0 (10→11) taken 60 times.
✗ Branch 1 (10→93) not taken.
✓ Branch 2 (11→12) taken 60 times.
✗ Branch 3 (11→91) not taken.
|
60 | auto templateType = std::any_cast<QualType>(visit(dataType)); |
| 430 |
2/6✓ Branch 0 (13→14) taken 60 times.
✗ Branch 1 (13→101) not taken.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→17) taken 60 times.
✗ Branch 4 (15→16) not taken.
✗ Branch 5 (15→94) not taken.
|
60 | HANDLE_UNRESOLVED_TYPE_PTR(templateType) |
| 431 | // Check if it is a generic type | ||
| 432 |
2/4✓ Branch 0 (17→18) taken 60 times.
✗ Branch 1 (17→101) not taken.
✗ Branch 2 (18→19) not taken.
✓ Branch 3 (18→26) taken 60 times.
|
60 | 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 0 (26→27) taken 60 times.
✗ Branch 1 (26→101) not taken.
✓ Branch 2 (27→28) taken 60 times.
✗ Branch 3 (27→101) not taken.
|
60 | const GenericType *genericType = rootScope->lookupGenericTypeStrict(templateType.getSubType()); |
| 438 |
1/2✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 60 times.
|
60 | assert(genericType != nullptr); |
| 439 |
1/2✓ Branch 0 (30→31) taken 60 times.
✗ Branch 1 (30→101) not taken.
|
60 | usedTemplateTypes.push_back(*genericType); |
| 440 |
1/2✓ Branch 0 (31→32) taken 60 times.
✗ Branch 1 (31→101) not taken.
|
60 | templateTypesGeneric.push_back(*genericType); |
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | // Update type of interface entry | ||
| 445 | 80 | const TypeChainElementData data = {.bodyScope = node->interfaceScope}; | |
| 446 |
1/2✓ Branch 0 (37→38) taken 80 times.
✗ Branch 1 (37→122) not taken.
|
80 | const Type *type = TypeRegistry::getOrInsert(TY_INTERFACE, node->interfaceName, node->typeId, data, usedTemplateTypes); |
| 447 |
1/2✓ Branch 0 (38→39) taken 80 times.
✗ Branch 1 (38→122) not taken.
|
80 | const QualType interfaceType(type, node->qualifiers); |
| 448 |
1/2✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 80 times.
|
80 | assert(node->entry != nullptr); |
| 449 |
1/2✓ Branch 0 (41→42) taken 80 times.
✗ Branch 1 (41→122) not taken.
|
80 | node->entry->updateType(interfaceType, false); |
| 450 | |||
| 451 | // Change to interface scope | ||
| 452 | 80 | currentScope = node->interfaceScope; | |
| 453 |
1/2✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→44) taken 80 times.
|
80 | assert(currentScope->type == ScopeType::INTERFACE); |
| 454 | |||
| 455 | // Visit methods | ||
| 456 | 80 | size_t vtableIndex = 0; | |
| 457 | 80 | std::vector<Function *> methods; | |
| 458 |
1/2✓ Branch 0 (45→46) taken 80 times.
✗ Branch 1 (45→120) not taken.
|
80 | methods.reserve(node->signatures.size()); |
| 459 |
2/2✓ Branch 0 (68→48) taken 186 times.
✓ Branch 1 (68→69) taken 79 times.
|
265 | for (SignatureNode *signature : node->signatures) { |
| 460 |
2/4✓ Branch 0 (49→50) taken 186 times.
✗ Branch 1 (49→105) not taken.
✓ Branch 2 (50→51) taken 186 times.
✗ Branch 3 (50→103) not taken.
|
186 | const auto method = std::any_cast<std::vector<Function *> *>(visit(signature)); |
| 461 |
2/2✓ Branch 0 (52→53) taken 1 times.
✓ Branch 1 (52→55) taken 185 times.
|
186 | if (!method) |
| 462 |
1/2✓ Branch 0 (53→54) taken 1 times.
✗ Branch 1 (53→106) not taken.
|
1 | return nullptr; |
| 463 | |||
| 464 | // Set 'this' type | ||
| 465 |
2/2✓ Branch 0 (60→57) taken 185 times.
✓ Branch 1 (60→61) taken 185 times.
|
370 | for (Function *m : *method) { |
| 466 | 185 | m->isVirtual = true; // Interface methods are always virtual | |
| 467 | 185 | m->vtableIndex = vtableIndex; | |
| 468 | 185 | m->thisType = interfaceType; | |
| 469 | } | ||
| 470 | |||
| 471 |
1/2✓ Branch 0 (65→66) taken 185 times.
✗ Branch 1 (65→107) not taken.
|
185 | methods.insert(methods.end(), method->begin(), method->end()); |
| 472 | 185 | vtableIndex++; | |
| 473 | } | ||
| 474 | |||
| 475 | // Change to root scope | ||
| 476 | 79 | currentScope = rootScope; | |
| 477 |
1/2✗ Branch 0 (69→70) not taken.
✓ Branch 1 (69→71) taken 79 times.
|
79 | assert(currentScope->type == ScopeType::GLOBAL); |
| 478 | |||
| 479 | // Build interface object | ||
| 480 |
3/6✓ Branch 0 (71→72) taken 79 times.
✗ Branch 1 (71→116) not taken.
✓ Branch 2 (72→73) taken 79 times.
✗ Branch 3 (72→113) not taken.
✓ Branch 4 (73→74) taken 79 times.
✗ Branch 5 (73→110) not taken.
|
79 | Interface spiceInterface(node->interfaceName, node->entry, node->interfaceScope, methods, templateTypesGeneric, node); |
| 481 |
1/2✓ Branch 0 (78→79) taken 79 times.
✗ Branch 1 (78→118) not taken.
|
79 | InterfaceManager::insert(currentScope, spiceInterface, &node->interfaceManifestations); |
| 482 | 79 | spiceInterface.scope = node->interfaceScope; | |
| 483 | |||
| 484 | // Request RTTI runtime, that is always required when dealing with interfaces due to polymorphism | ||
| 485 |
2/4✓ Branch 0 (79→80) taken 79 times.
✗ Branch 1 (79→118) not taken.
✓ Branch 2 (82→83) taken 79 times.
✗ Branch 3 (82→84) not taken.
|
158 | if (!sourceFile->isRttiRT()) |
| 486 |
1/2✓ Branch 0 (83→84) taken 79 times.
✗ Branch 1 (83→118) not taken.
|
79 | sourceFile->requestRuntimeModule(RTTI_RT); |
| 487 | |||
| 488 |
1/2✓ Branch 0 (84→85) taken 79 times.
✗ Branch 1 (84→117) not taken.
|
79 | return nullptr; |
| 489 | 80 | } | |
| 490 | |||
| 491 | 63 | std::any TypeChecker::visitEnumDefPrepare(EnumDefNode *node) { | |
| 492 | // Update type of enum entry | ||
| 493 | 63 | const TypeChainElementData data = {.bodyScope = node->enumScope}; | |
| 494 |
1/2✓ Branch 0 (3→4) taken 63 times.
✗ Branch 1 (3→59) not taken.
|
63 | const Type *type = TypeRegistry::getOrInsert(TY_ENUM, node->enumName, node->typeId, data, {}); |
| 495 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 63 times.
|
63 | assert(node->entry != nullptr); |
| 496 |
2/4✓ Branch 0 (7→8) taken 63 times.
✗ Branch 1 (7→62) not taken.
✓ Branch 2 (8→9) taken 63 times.
✗ Branch 3 (8→62) not taken.
|
63 | node->entry->updateType(QualType(type, node->qualifiers), false); |
| 497 | |||
| 498 | // Change to enum scope | ||
| 499 | 63 | currentScope = node->enumScope; | |
| 500 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 63 times.
|
63 | assert(currentScope->type == ScopeType::ENUM); |
| 501 | |||
| 502 | // Loop through all items with values | ||
| 503 | 63 | std::vector<std::string> names; | |
| 504 | 63 | std::vector<uint32_t> values; | |
| 505 |
2/2✓ Branch 0 (30→13) taken 732 times.
✓ Branch 1 (30→31) taken 63 times.
|
795 | for (const EnumItemNode *enumItem : node->itemLst->items) { |
| 506 | // Save name | ||
| 507 |
1/2✓ Branch 0 (14→15) taken 732 times.
✗ Branch 1 (14→71) not taken.
|
732 | names.push_back(enumItem->itemName); |
| 508 | // Check for duplicate value | ||
| 509 |
2/2✓ Branch 0 (15→16) taken 408 times.
✓ Branch 1 (15→28) taken 324 times.
|
732 | if (enumItem->hasValue) { |
| 510 |
3/4✓ Branch 0 (17→18) taken 408 times.
✗ Branch 1 (17→63) not taken.
✓ Branch 2 (19→20) taken 1 times.
✓ Branch 3 (19→27) taken 407 times.
|
408 | if (std::ranges::find(values, enumItem->itemValue) != values.end()) { |
| 511 |
2/4✓ Branch 0 (22→23) taken 1 times.
✗ Branch 1 (22→67) not taken.
✓ Branch 2 (23→24) taken 1 times.
✗ Branch 3 (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 0 (27→28) taken 407 times.
✗ Branch 1 (27→71) not taken.
|
407 | values.push_back(enumItem->itemValue); |
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | // Loop through all items without values | ||
| 519 | 63 | uint32_t nextValue = 0; | |
| 520 |
1/2✓ Branch 0 (31→32) taken 63 times.
✗ Branch 1 (31→76) not taken.
|
63 | const QualType intSymbolType(TY_INT); |
| 521 |
2/2✓ Branch 0 (51→34) taken 732 times.
✓ Branch 1 (51→52) taken 63 times.
|
795 | for (EnumItemNode *enumItem : node->itemLst->items) { |
| 522 | // Update type of enum item entry | ||
| 523 |
1/2✓ Branch 0 (35→36) taken 732 times.
✗ Branch 1 (35→74) not taken.
|
732 | SymbolTableEntry *itemEntry = currentScope->lookupStrict(enumItem->itemName); |
| 524 |
1/2✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 732 times.
|
732 | assert(itemEntry != nullptr); |
| 525 |
1/2✓ Branch 0 (40→41) taken 732 times.
✗ Branch 1 (40→74) not taken.
|
732 | itemEntry->updateType(intSymbolType, false); |
| 526 | // Fill in value if not filled yet | ||
| 527 |
2/2✓ Branch 0 (41→42) taken 324 times.
✓ Branch 1 (41→49) taken 408 times.
|
732 | if (!enumItem->hasValue) { |
| 528 |
3/4✓ Branch 0 (45→46) taken 610 times.
✗ Branch 1 (45→72) not taken.
✓ Branch 2 (47→43) taken 286 times.
✓ Branch 3 (47→48) taken 324 times.
|
610 | while (std::ranges::find(values, nextValue) != values.end()) |
| 529 | 286 | nextValue++; | |
| 530 | 324 | enumItem->itemValue = nextValue; | |
| 531 |
1/2✓ Branch 0 (48→49) taken 324 times.
✗ Branch 1 (48→74) not taken.
|
324 | values.push_back(nextValue); |
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | // Change to root scope | ||
| 536 | 63 | currentScope = rootScope; | |
| 537 |
1/2✗ Branch 0 (52→53) not taken.
✓ Branch 1 (52→54) taken 63 times.
|
63 | assert(currentScope->type == ScopeType::GLOBAL); |
| 538 | |||
| 539 |
1/2✓ Branch 0 (54→55) taken 63 times.
✗ Branch 1 (54→75) not taken.
|
126 | return nullptr; |
| 540 | 63 | } | |
| 541 | |||
| 542 | 759 | std::any TypeChecker::visitGenericTypeDefPrepare(GenericTypeDefNode *node) { | |
| 543 | // Retrieve type conditions | ||
| 544 | 759 | QualTypeList typeConditions; | |
| 545 |
1/2✓ Branch 0 (3→4) taken 759 times.
✗ Branch 1 (3→52) not taken.
|
759 | typeConditions.reserve(node->typeAltsLst->dataTypes.size()); |
| 546 |
2/2✓ Branch 0 (19→6) taken 1467 times.
✓ Branch 1 (19→20) taken 759 times.
|
2226 | for (const auto &typeAlt : node->typeAltsLst->dataTypes) { |
| 547 |
2/4✓ Branch 0 (7→8) taken 1467 times.
✗ Branch 1 (7→41) not taken.
✓ Branch 2 (8→9) taken 1467 times.
✗ Branch 3 (8→39) not taken.
|
1467 | auto typeCondition = std::any_cast<QualType>(visit(typeAlt)); |
| 548 |
2/6✓ Branch 0 (10→11) taken 1467 times.
✗ Branch 1 (10→43) not taken.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→14) taken 1467 times.
✗ Branch 4 (12→13) not taken.
✗ Branch 5 (12→42) not taken.
|
1467 | HANDLE_UNRESOLVED_TYPE_PTR(typeCondition) |
| 549 |
3/4✓ Branch 0 (14→15) taken 1467 times.
✗ Branch 1 (14→43) not taken.
✓ Branch 2 (15→16) taken 1194 times.
✓ Branch 3 (15→17) taken 273 times.
|
1467 | if (!typeCondition.is(TY_DYN)) |
| 550 |
1/2✓ Branch 0 (16→17) taken 1194 times.
✗ Branch 1 (16→43) not taken.
|
1194 | typeConditions.push_back(typeCondition); |
| 551 | } | ||
| 552 | |||
| 553 | // Add generic type to the scope | ||
| 554 |
2/4✓ Branch 0 (20→21) taken 759 times.
✗ Branch 1 (20→47) not taken.
✓ Branch 2 (21→22) taken 759 times.
✗ Branch 3 (21→45) not taken.
|
759 | const GenericType genericType(node->typeName, typeConditions); |
| 555 |
1/2✓ Branch 0 (23→24) taken 759 times.
✗ Branch 1 (23→50) not taken.
|
759 | rootScope->insertGenericType(node->typeName, genericType); |
| 556 | |||
| 557 | // Check if only one type condition is set | ||
| 558 |
6/8✓ Branch 0 (25→26) taken 1 times.
✓ Branch 1 (25→30) taken 758 times.
✓ Branch 2 (27→28) taken 1 times.
✗ Branch 3 (27→50) not taken.
✓ Branch 4 (28→29) taken 1 times.
✗ Branch 5 (28→30) not taken.
✓ Branch 6 (31→32) taken 1 times.
✓ Branch 7 (31→34) taken 758 times.
|
759 | if (typeConditions.size() == 1 && !typeConditions.front().is(TY_DYN)) |
| 559 |
1/2✓ Branch 0 (32→33) taken 1 times.
✗ Branch 1 (32→48) not taken.
|
1 | sourceFile->compilerOutput.warnings.emplace_back(node->typeAltsLst->codeLoc, SINGLE_GENERIC_TYPE_CONDITION, |
| 560 | "Generic type is locked to one type"); | ||
| 561 | |||
| 562 |
1/2✓ Branch 0 (34→35) taken 759 times.
✗ Branch 1 (34→49) not taken.
|
759 | return nullptr; |
| 563 | 759 | } | |
| 564 | |||
| 565 | 48 | std::any TypeChecker::visitAliasDefPrepare(AliasDefNode *node) { | |
| 566 |
2/4✓ Branch 0 (2→3) taken 48 times.
✗ Branch 1 (2→5) not taken.
✓ Branch 2 (3→4) taken 48 times.
✗ Branch 3 (3→5) not taken.
|
48 | assert(node->entry != nullptr && node->aliasedTypeContainerEntry != nullptr); |
| 567 | |||
| 568 | // Update type of alias entry | ||
| 569 |
1/2✓ Branch 0 (7→8) taken 48 times.
✗ Branch 1 (7→23) not taken.
|
48 | const Type *type = TypeRegistry::getOrInsert(TY_ALIAS, node->aliasName, node->typeId, {}, {}); |
| 570 |
2/4✓ Branch 0 (9→10) taken 48 times.
✗ Branch 1 (9→27) not taken.
✓ Branch 2 (10→11) taken 48 times.
✗ Branch 3 (10→27) not taken.
|
48 | node->entry->updateType(QualType(type, node->qualifiers), false); |
| 571 | |||
| 572 | // Update type of the aliased type container entry | ||
| 573 |
2/4✓ Branch 0 (11→12) taken 48 times.
✗ Branch 1 (11→30) not taken.
✓ Branch 2 (12→13) taken 48 times.
✗ Branch 3 (12→28) not taken.
|
48 | const auto aliasedType = std::any_cast<QualType>(visit(node->dataType)); |
| 574 |
4/6✓ Branch 0 (14→15) taken 48 times.
✗ Branch 1 (14→33) not taken.
✓ Branch 2 (15→16) taken 1 times.
✓ Branch 3 (15→18) taken 47 times.
✓ Branch 4 (16→17) taken 1 times.
✗ Branch 5 (16→31) not taken.
|
48 | HANDLE_UNRESOLVED_TYPE_PTR(aliasedType) |
| 575 |
1/2✓ Branch 0 (18→19) taken 47 times.
✗ Branch 1 (18→33) not taken.
|
47 | node->aliasedTypeContainerEntry->updateType(aliasedType, false); |
| 576 | 47 | node->aliasedTypeContainerEntry->used = true; // The container type is always used per default | |
| 577 | |||
| 578 |
1/2✓ Branch 0 (19→20) taken 47 times.
✗ Branch 1 (19→32) not taken.
|
47 | return nullptr; |
| 579 | } | ||
| 580 | |||
| 581 | 760 | std::any TypeChecker::visitGlobalVarDefPrepare(GlobalVarDefNode *node) { | |
| 582 | // Insert variable name to symbol table | ||
| 583 |
2/4✓ Branch 0 (2→3) taken 760 times.
✗ Branch 1 (2→80) not taken.
✓ Branch 2 (3→4) taken 760 times.
✗ Branch 3 (3→78) not taken.
|
760 | auto globalVarType = std::any_cast<QualType>(visit(node->dataType)); |
| 584 |
2/6✓ Branch 0 (5→6) taken 760 times.
✗ Branch 1 (5→126) not taken.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→9) taken 760 times.
✗ Branch 4 (7→8) not taken.
✗ Branch 5 (7→81) not taken.
|
760 | HANDLE_UNRESOLVED_TYPE_PTR(globalVarType) |
| 585 | |||
| 586 |
2/2✓ Branch 0 (9→10) taken 758 times.
✓ Branch 1 (9→37) taken 2 times.
|
760 | if (node->constant) { // Variable is initialized here |
| 587 |
2/4✓ Branch 0 (10→11) taken 758 times.
✗ Branch 1 (10→84) not taken.
✓ Branch 2 (11→12) taken 758 times.
✗ Branch 3 (11→82) not taken.
|
758 | const QualType rhsType = std::any_cast<ExprResult>(visit(node->constant)).type; |
| 588 |
2/6✓ Branch 0 (13→14) taken 758 times.
✗ Branch 1 (13→103) not taken.
✗ Branch 2 (14→15) not taken.
✓ Branch 3 (14→17) taken 758 times.
✗ Branch 4 (15→16) not taken.
✗ Branch 5 (15→86) not taken.
|
758 | HANDLE_UNRESOLVED_TYPE_PTR(rhsType) |
| 589 |
3/4✓ Branch 0 (17→18) taken 758 times.
✗ Branch 1 (17→103) not taken.
✓ Branch 2 (18→19) taken 1 times.
✓ Branch 3 (18→20) taken 757 times.
|
758 | if (globalVarType.is(TY_DYN)) { // Perform type inference |
| 590 | 1 | globalVarType = rhsType; | |
| 591 |
3/4✓ Branch 0 (20→21) taken 757 times.
✗ Branch 1 (20→103) not taken.
✓ Branch 2 (21→22) taken 2 times.
✓ Branch 3 (21→35) taken 755 times.
|
757 | } else if (!globalVarType.matches(rhsType, false, true, true)) { // Check if types are matching |
| 592 |
7/14✓ Branch 0 (22→23) taken 2 times.
✗ Branch 1 (22→100) not taken.
✓ Branch 2 (23→24) taken 2 times.
✗ Branch 3 (23→95) not taken.
✓ Branch 4 (24→25) taken 2 times.
✗ Branch 5 (24→93) not taken.
✓ Branch 6 (25→26) taken 2 times.
✗ Branch 7 (25→91) not taken.
✓ Branch 8 (26→27) taken 2 times.
✗ Branch 9 (26→89) not taken.
✓ Branch 10 (27→28) taken 2 times.
✗ Branch 11 (27→87) not taken.
✓ Branch 12 (33→34) taken 2 times.
✗ Branch 13 (33→102) not taken.
|
2 | SOFT_ERROR_BOOL(node->constant, OPERATOR_WRONG_DATA_TYPE, |
| 593 | "Expected " + globalVarType.getName(false) + ", but got " + rhsType.getName(false)) | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | // Check if the type is still missing | ||
| 598 |
3/4✓ Branch 0 (37→38) taken 758 times.
✗ Branch 1 (37→126) not taken.
✓ Branch 2 (38→39) taken 1 times.
✓ Branch 3 (38→47) taken 757 times.
|
758 | if (globalVarType.is(TY_DYN)) |
| 599 |
3/6✓ Branch 0 (41→42) taken 1 times.
✗ Branch 1 (41→106) not taken.
✓ Branch 2 (42→43) taken 1 times.
✗ Branch 3 (42→104) not taken.
✓ Branch 4 (45→46) taken 1 times.
✗ Branch 5 (45→110) not taken.
|
3 | SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_TYPE_DYN, "Global variables must have an explicit data type") |
| 600 | |||
| 601 | // Check if we would need to insert instructions in the global scope to initialize the variable | ||
| 602 |
2/4✓ Branch 0 (47→48) taken 757 times.
✗ Branch 1 (47→126) not taken.
✗ Branch 2 (48→49) not taken.
✓ Branch 3 (48→57) taken 757 times.
|
757 | if (!globalVarType.isPrimitive()) |
| 603 | ✗ | SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_INVALID_TYPE, "Spice does only support global variables of primitive type") | |
| 604 | |||
| 605 | // Update type of global var entry | ||
| 606 |
1/2✗ Branch 0 (57→58) not taken.
✓ Branch 1 (57→59) taken 757 times.
|
757 | assert(node->entry != nullptr); |
| 607 |
1/2✓ Branch 0 (59→60) taken 757 times.
✗ Branch 1 (59→126) not taken.
|
757 | node->entry->updateType(globalVarType, false); |
| 608 | |||
| 609 | // Check if a value is attached | ||
| 610 |
6/8✓ Branch 0 (60→61) taken 1 times.
✓ Branch 1 (60→64) taken 756 times.
✓ Branch 2 (61→62) taken 1 times.
✗ Branch 3 (61→126) not taken.
✓ Branch 4 (62→63) taken 1 times.
✗ Branch 5 (62→64) not taken.
✓ Branch 6 (65→66) taken 1 times.
✓ Branch 7 (65→74) taken 756 times.
|
757 | if (!node->constant && globalVarType.isConst()) |
| 611 |
3/6✓ Branch 0 (68→69) taken 1 times.
✗ Branch 1 (68→120) not taken.
✓ Branch 2 (69→70) taken 1 times.
✗ Branch 3 (69→118) not taken.
✓ Branch 4 (72→73) taken 1 times.
✗ Branch 5 (72→124) not taken.
|
3 | SOFT_ERROR_BOOL(node, GLOBAL_CONST_WITHOUT_VALUE, "You must specify a value for constant global variables") |
| 612 | |||
| 613 |
1/2✓ Branch 0 (74→75) taken 756 times.
✗ Branch 1 (74→125) not taken.
|
756 | return nullptr; |
| 614 | } | ||
| 615 | |||
| 616 | 851 | std::any TypeChecker::visitExtDeclPrepare(ExtDeclNode *node) { | |
| 617 | // Collect argument types | ||
| 618 | 851 | QualTypeList argTypes; | |
| 619 | 851 | ParamList argList; | |
| 620 |
2/2✓ Branch 0 (2→3) taken 811 times.
✓ Branch 1 (2→31) taken 40 times.
|
851 | if (node->hasArgs) { |
| 621 |
1/2✓ Branch 0 (4→5) taken 811 times.
✗ Branch 1 (4→149) not taken.
|
811 | argList.reserve(node->argTypeLst->dataTypes.size()); |
| 622 |
2/2✓ Branch 0 (29→7) taken 1666 times.
✓ Branch 1 (29→30) taken 811 times.
|
2477 | for (DataTypeNode *arg : node->argTypeLst->dataTypes) { |
| 623 | // Visit argument | ||
| 624 |
2/4✓ Branch 0 (8→9) taken 1666 times.
✗ Branch 1 (8→96) not taken.
✓ Branch 2 (9→10) taken 1666 times.
✗ Branch 3 (9→94) not taken.
|
1666 | auto argType = std::any_cast<QualType>(visit(arg)); |
| 625 |
2/6✓ Branch 0 (11→12) taken 1666 times.
✗ Branch 1 (11→105) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 1666 times.
✗ Branch 4 (13→14) not taken.
✗ Branch 5 (13→97) not taken.
|
1666 | HANDLE_UNRESOLVED_TYPE_PTR(argType) |
| 626 | // Check if the argument type is 'dyn' | ||
| 627 |
3/4✓ Branch 0 (15→16) taken 1666 times.
✗ Branch 1 (15→105) not taken.
✓ Branch 2 (16→17) taken 1 times.
✓ Branch 3 (16→24) taken 1665 times.
|
1666 | if (argType.is(TY_DYN)) { |
| 628 |
2/4✓ Branch 0 (19→20) taken 1 times.
✗ Branch 1 (19→100) not taken.
✓ Branch 2 (20→21) taken 1 times.
✗ Branch 3 (20→98) not taken.
|
1 | softError(arg, UNEXPECTED_DYN_TYPE, "Dyn data type is not allowed as arg type for external functions"); |
| 629 | 1 | continue; | |
| 630 | } | ||
| 631 | // Save argument | ||
| 632 |
1/2✓ Branch 0 (24→25) taken 1665 times.
✗ Branch 1 (24→105) not taken.
|
1665 | argTypes.push_back(argType); |
| 633 |
1/2✓ Branch 0 (25→26) taken 1665 times.
✗ Branch 1 (25→104) not taken.
|
1665 | argList.push_back({argType, false}); |
| 634 | } | ||
| 635 | } | ||
| 636 | |||
| 637 | // Retrieve return type | ||
| 638 |
1/2✓ Branch 0 (31→32) taken 851 times.
✗ Branch 1 (31→149) not taken.
|
851 | QualType returnType(TY_DYN); |
| 639 | 851 | const bool isFunction = node->returnType; | |
| 640 |
2/2✓ Branch 0 (32→33) taken 553 times.
✓ Branch 1 (32→50) taken 298 times.
|
851 | if (isFunction) { // External function |
| 641 |
2/4✓ Branch 0 (33→34) taken 553 times.
✗ Branch 1 (33→109) not taken.
✓ Branch 2 (34→35) taken 553 times.
✗ Branch 3 (34→107) not taken.
|
553 | returnType = std::any_cast<QualType>(visit(node->returnType)); |
| 642 |
2/6✓ Branch 0 (36→37) taken 553 times.
✗ Branch 1 (36→149) not taken.
✗ Branch 2 (37→38) not taken.
✓ Branch 3 (37→40) taken 553 times.
✗ Branch 4 (38→39) not taken.
✗ Branch 5 (38→111) not taken.
|
553 | HANDLE_UNRESOLVED_TYPE_PTR(returnType) |
| 643 | // Check if return type is dyn | ||
| 644 |
3/4✓ Branch 0 (40→41) taken 553 times.
✗ Branch 1 (40→149) not taken.
✓ Branch 2 (41→42) taken 1 times.
✓ Branch 3 (41→50) taken 552 times.
|
553 | if (returnType.is(TY_DYN)) |
| 645 |
3/6✓ Branch 0 (44→45) taken 1 times.
✗ Branch 1 (44→114) not taken.
✓ Branch 2 (45→46) taken 1 times.
✗ Branch 3 (45→112) not taken.
✓ Branch 4 (48→49) taken 1 times.
✗ Branch 5 (48→118) not taken.
|
3 | SOFT_ERROR_BOOL(node->returnType, UNEXPECTED_DYN_TYPE, "dyn is not allowed as return type for external functions") |
| 646 | } | ||
| 647 | |||
| 648 | // Add function to current scope | ||
| 649 |
3/6✓ Branch 0 (51→52) taken 850 times.
✗ Branch 1 (51→123) not taken.
✓ Branch 2 (52→53) taken 850 times.
✗ Branch 3 (52→120) not taken.
✓ Branch 4 (53→54) taken 850 times.
✗ Branch 5 (53→119) not taken.
|
850 | const Function spiceFunc(node->extFunctionName, node->entry, QualType(TY_DYN), returnType, argList, {}, node); |
| 650 |
1/2✓ Branch 0 (58→59) taken 850 times.
✗ Branch 1 (58→147) not taken.
|
850 | node->extFunction = FunctionManager::insert(currentScope, spiceFunc, &node->extFunctionManifestations); |
| 651 | 850 | node->extFunction->mangleFunctionName = false; | |
| 652 | 850 | node->extFunction->alreadyTypeChecked = true; | |
| 653 | |||
| 654 | // Check procedure attributes | ||
| 655 |
2/2✓ Branch 0 (59→60) taken 1 times.
✓ Branch 1 (59→77) taken 849 times.
|
850 | if (node->attrs) { |
| 656 | 1 | const AttrLstNode *attrLst = node->attrs->attrLst; | |
| 657 |
3/6✓ Branch 0 (62→63) taken 1 times.
✗ Branch 1 (62→129) not taken.
✓ Branch 2 (63→64) taken 1 times.
✗ Branch 3 (63→127) not taken.
✗ Branch 4 (66→67) not taken.
✓ Branch 5 (66→68) taken 1 times.
|
3 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE)) |
| 658 | ✗ | node->extFunction->mangleFunctionName = value->boolValue; | |
| 659 |
3/6✓ Branch 0 (70→71) taken 1 times.
✗ Branch 1 (70→135) not taken.
✓ Branch 2 (71→72) taken 1 times.
✗ Branch 3 (71→133) not taken.
✓ Branch 4 (74→75) taken 1 times.
✗ Branch 5 (74→77) not taken.
|
3 | if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) { |
| 660 |
1/2✓ Branch 0 (75→76) taken 1 times.
✗ Branch 1 (75→147) not taken.
|
1 | const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset); |
| 661 |
1/2✓ Branch 0 (76→77) taken 1 times.
✗ Branch 1 (76→147) not taken.
|
1 | node->extFunction->predefinedMangledName = stringValue; |
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 665 | // Prepare ext function type | ||
| 666 |
2/2✓ Branch 0 (77→78) taken 552 times.
✓ Branch 1 (77→79) taken 298 times.
|
850 | const SuperType superType = isFunction ? TY_FUNCTION : TY_PROCEDURE; |
| 667 |
2/4✓ Branch 0 (80→81) taken 850 times.
✗ Branch 1 (80→139) not taken.
✓ Branch 2 (81→82) taken 850 times.
✗ Branch 3 (81→139) not taken.
|
850 | const QualType extFunctionType = QualType(superType).getWithFunctionParamAndReturnTypes(returnType, argTypes); |
| 668 | |||
| 669 | // Set type of external function | ||
| 670 |
1/2✓ Branch 0 (82→83) taken 850 times.
✗ Branch 1 (82→147) not taken.
|
850 | node->entry->updateType(extFunctionType, false); |
| 671 | |||
| 672 | // Rename the original child scope to reflect the substantiated versions of the external function | ||
| 673 |
3/6✓ Branch 0 (83→84) taken 850 times.
✗ Branch 1 (83→145) not taken.
✓ Branch 2 (84→85) taken 850 times.
✗ Branch 3 (84→142) not taken.
✓ Branch 4 (85→86) taken 850 times.
✗ Branch 5 (85→140) not taken.
|
850 | currentScope->renameChildScope(node->getScopeId(), spiceFunc.getSignature(false)); |
| 674 | |||
| 675 |
1/2✓ Branch 0 (88→89) taken 850 times.
✗ Branch 1 (88→146) not taken.
|
850 | return nullptr; |
| 676 | 851 | } | |
| 677 | |||
| 678 | 466 | std::any TypeChecker::visitImportDefPrepare(ImportDefNode *node) { | |
| 679 | // Set entry to import type | ||
| 680 |
1/2✓ Branch 0 (2→3) taken 466 times.
✗ Branch 1 (2→10) not taken.
|
466 | const QualType importType(TY_IMPORT, node->importName); |
| 681 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 466 times.
|
466 | assert(node->entry != nullptr); |
| 682 |
1/2✓ Branch 0 (5→6) taken 466 times.
✗ Branch 1 (5→10) not taken.
|
466 | node->entry->updateType(importType, false); |
| 683 | |||
| 684 |
1/2✓ Branch 0 (6→7) taken 466 times.
✗ Branch 1 (6→9) not taken.
|
466 | return nullptr; |
| 685 | } | ||
| 686 | |||
| 687 | } // namespace spice::compiler | ||
| 688 |