| 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 <symboltablebuilder/SymbolTableBuilder.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | 3510 | TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode) | |
| 13 |
1/2✓ Branch 4 → 5 taken 3510 times.
✗ Branch 4 → 7 not taken.
|
3510 | : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) { |
| 14 | 3510 | } | |
| 15 | |||
| 16 | 2749 | std::any TypeChecker::visitEntry(EntryNode *node) { | |
| 17 | // Initialize | ||
| 18 | 2749 | currentScope = rootScope; | |
| 19 | |||
| 20 | // Initialize AST nodes with size of 1 | ||
| 21 | 2749 | const bool isPrepare = typeCheckerMode == TC_MODE_PRE; | |
| 22 |
2/2✓ Branch 2 → 3 taken 1158 times.
✓ Branch 2 → 4 taken 1591 times.
|
2749 | if (isPrepare) |
| 23 | 1158 | node->resizeToNumberOfManifestations(1); | |
| 24 | |||
| 25 | // Visit children | ||
| 26 |
2/2✓ Branch 4 → 5 taken 2705 times.
✓ Branch 4 → 22 taken 44 times.
|
2749 | visitChildren(node); |
| 27 | |||
| 28 | // Check which implicit structures we need for each struct, defined in this source file | ||
| 29 |
2/2✓ Branch 6 → 7 taken 1144 times.
✓ Branch 6 → 19 taken 1561 times.
|
2705 | if (isPrepare) { |
| 30 |
1/2✓ Branch 7 → 8 taken 1144 times.
✗ Branch 7 → 26 not taken.
|
1144 | const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder(); |
| 31 |
2/2✓ Branch 16 → 10 taken 702 times.
✓ Branch 16 → 17 taken 1144 times.
|
1846 | for (const Struct *manifestation : manifestations) { |
| 32 | // Check if we need to create a default ctor, copy ctor or dtor | ||
| 33 |
1/2✓ Branch 11 → 12 taken 702 times.
✗ Branch 11 → 23 not taken.
|
702 | createDefaultCtorIfRequired(*manifestation, manifestation->scope); |
| 34 |
1/2✓ Branch 12 → 13 taken 702 times.
✗ Branch 12 → 23 not taken.
|
702 | createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope); |
| 35 |
1/2✓ Branch 13 → 14 taken 702 times.
✗ Branch 13 → 23 not taken.
|
702 | createDefaultDtorIfRequired(*manifestation, manifestation->scope); |
| 36 | } | ||
| 37 | 1144 | } | |
| 38 | |||
| 39 |
1/2✓ Branch 19 → 20 taken 2705 times.
✗ Branch 19 → 27 not taken.
|
2705 | return nullptr; |
| 40 | } | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Check if the capture rules for async lambdas are enforced if the async attribute is set | ||
| 44 | * | ||
| 45 | * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the | ||
| 46 | * fat pointer and can ensure, that no stack variable is referenced inside the lambda. | ||
| 47 | * | ||
| 48 | * @param node Lambda base node | ||
| 49 | * @param attrs Lambda attributes | ||
| 50 | * @return False if the rules are violated, true otherwise | ||
| 51 | */ | ||
| 52 | 37 | bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const { | |
| 53 | // If the async attribute is not set, we can return early | ||
| 54 |
18/32✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 13 taken 21 times.
✓ Branch 5 → 6 taken 16 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 16 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 16 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 16 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 16 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 16 times.
✓ Branch 15 → 16 taken 16 times.
✓ Branch 15 → 17 taken 21 times.
✓ Branch 17 → 18 taken 16 times.
✓ Branch 17 → 20 taken 21 times.
✓ Branch 20 → 21 taken 16 times.
✓ Branch 20 → 22 taken 21 times.
✓ Branch 22 → 23 taken 16 times.
✓ Branch 22 → 25 taken 21 times.
✓ Branch 25 → 26 taken 21 times.
✓ Branch 25 → 27 taken 16 times.
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 60 not taken.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 69 not taken.
|
101 | if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue) |
| 55 | 21 | return true; // Not violated | |
| 56 | |||
| 57 | // If we don't have any captures, we can return early | ||
| 58 | 16 | const CaptureMap &captures = node->bodyScope->symbolTable.captures; | |
| 59 |
2/2✓ Branch 28 → 29 taken 10 times.
✓ Branch 28 → 30 taken 6 times.
|
16 | if (captures.empty()) |
| 60 | 10 | return true; // Not violated | |
| 61 | |||
| 62 | // Check for the capture rules | ||
| 63 | 6 | if (const Capture &capture = captures.begin()->second; | |
| 64 |
8/8✓ Branch 33 → 34 taken 4 times.
✓ Branch 33 → 39 taken 2 times.
✓ Branch 36 → 37 taken 2 times.
✓ Branch 36 → 39 taken 2 times.
✓ Branch 38 → 39 taken 1 time.
✓ Branch 38 → 40 taken 1 time.
✓ Branch 41 → 42 taken 5 times.
✓ Branch 41 → 51 taken 1 time.
|
6 | captures.size() > 1 || !capture.capturedSymbol->getQualType().isPtr() || capture.getMode() != BY_VALUE) { |
| 65 | 5 | const auto warningMessage = | |
| 66 | "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead " | ||
| 67 | "to bugs due to references, outliving the validity scope of the referenced variable."; | ||
| 68 |
2/4✓ Branch 44 → 45 taken 5 times.
✗ Branch 44 → 73 not taken.
✓ Branch 45 → 46 taken 5 times.
✗ Branch 45 → 71 not taken.
|
5 | const CompilerWarning warning(node->codeLoc, ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION, warningMessage); |
| 69 |
1/2✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 77 not taken.
|
5 | currentScope->sourceFile->compilerOutput.warnings.push_back(warning); |
| 70 | 5 | } | |
| 71 | |||
| 72 | 6 | return false; // Violated | |
| 73 | } | ||
| 74 | |||
| 75 | 24 | Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const { | |
| 76 |
1/2✓ Branch 2 → 3 taken 24 times.
✗ Branch 2 → 40 not taken.
|
24 | Scope *matchScope = thisType.getBodyScope(); |
| 77 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24 times.
|
24 | assert(matchScope != nullptr); |
| 78 |
2/4✓ Branch 5 → 6 taken 24 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 24 times.
✗ Branch 9 → 23 not taken.
|
48 | const ArgList args = {{thisType.toConstRef(node), false}}; |
| 79 |
2/4✓ Branch 14 → 15 taken 24 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 24 times.
✗ Branch 15 → 29 not taken.
|
96 | return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node); |
| 80 | 24 | } | |
| 81 | |||
| 82 | 137326 | QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const { | |
| 83 | // Skip all types, except structs | ||
| 84 |
3/4✓ Branch 2 → 3 taken 137326 times.
✗ Branch 2 → 50 not taken.
✓ Branch 3 → 4 taken 124497 times.
✓ Branch 3 → 5 taken 12829 times.
|
137326 | if (!symbolType.isBase(TY_STRUCT)) |
| 85 | 124497 | return symbolType; | |
| 86 | |||
| 87 | // If the target scope is in the current source file, we can return the symbol type as is | ||
| 88 | 12829 | SourceFile *targetSourceFile = targetScope->sourceFile; | |
| 89 |
2/2✓ Branch 5 → 6 taken 5738 times.
✓ Branch 5 → 7 taken 7091 times.
|
12829 | if (targetSourceFile == sourceFile) |
| 90 | 5738 | return symbolType; | |
| 91 | |||
| 92 | // Match the scope of the symbol type against all scopes in the name registry of the target file | ||
| 93 |
5/8✓ Branch 7 → 8 taken 7091 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 7091 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 7091 times.
✗ Branch 9 → 46 not taken.
✓ Branch 33 → 11 taken 159215 times.
✓ Branch 33 → 34 taken 309 times.
|
159524 | for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values) |
| 94 |
7/10✓ Branch 12 → 13 taken 159215 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 159215 times.
✗ Branch 13 → 46 not taken.
✓ Branch 14 → 15 taken 159215 times.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 18195 times.
✓ Branch 15 → 17 taken 141020 times.
✓ Branch 18 → 19 taken 18195 times.
✓ Branch 18 → 31 taken 141020 times.
|
159215 | if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT)) |
| 95 |
3/4✓ Branch 19 → 20 taken 18195 times.
✗ Branch 19 → 45 not taken.
✓ Branch 29 → 22 taken 34735 times.
✓ Branch 29 → 30 taken 11413 times.
|
46148 | for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations()) |
| 96 |
4/6✓ Branch 23 → 24 taken 34735 times.
✗ Branch 23 → 44 not taken.
✓ Branch 24 → 25 taken 34735 times.
✗ Branch 24 → 44 not taken.
✓ Branch 25 → 26 taken 6782 times.
✓ Branch 25 → 27 taken 27953 times.
|
34735 | if (manifestation->scope == symbolType.getBase().getBodyScope()) |
| 97 | 6782 | return symbolType; | |
| 98 | |||
| 99 | // The target file does not know about the struct at all | ||
| 100 | // -> show it how to find the struct | ||
| 101 |
3/6✓ Branch 34 → 35 taken 309 times.
✗ Branch 34 → 47 not taken.
✓ Branch 35 → 36 taken 309 times.
✗ Branch 35 → 47 not taken.
✓ Branch 36 → 37 taken 309 times.
✗ Branch 36 → 47 not taken.
|
309 | const std::string structName = symbolType.getBase().getSubType(); |
| 102 |
1/2✓ Branch 37 → 38 taken 309 times.
✗ Branch 37 → 48 not taken.
|
309 | const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName); |
| 103 |
1/2✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 309 times.
|
309 | assert(origRegistryEntry != nullptr); |
| 104 | 309 | const uint64_t targetTypeId = origRegistryEntry->typeId; | |
| 105 | 309 | SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry; | |
| 106 |
1/2✓ Branch 40 → 41 taken 309 times.
✗ Branch 40 → 48 not taken.
|
309 | targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false); |
| 107 | |||
| 108 | 309 | return symbolType; | |
| 109 | 309 | } | |
| 110 | |||
| 111 | 4554 | QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const { | |
| 112 | // Skip all types, except structs | ||
| 113 |
3/4✓ Branch 2 → 3 taken 4554 times.
✗ Branch 2 → 45 not taken.
✓ Branch 3 → 4 taken 559 times.
✓ Branch 3 → 5 taken 3995 times.
|
4554 | if (!symbolType.isBase(TY_STRUCT)) |
| 114 | 559 | return symbolType; | |
| 115 | |||
| 116 | // If the given source file is in the current one, we can return the symbol type as is | ||
| 117 | 3995 | const SourceFile *sourceSourceFile = sourceScope->sourceFile; | |
| 118 |
2/2✓ Branch 5 → 6 taken 1313 times.
✓ Branch 5 → 7 taken 2682 times.
|
3995 | if (sourceSourceFile == sourceFile) |
| 119 | 1313 | return symbolType; | |
| 120 | |||
| 121 | // Match the scope of the symbol type against all scopes in the name registry of this source file | ||
| 122 |
1/2✓ Branch 7 → 8 taken 2682 times.
✗ Branch 7 → 45 not taken.
|
2682 | const QualType baseType = symbolType.getBase(); |
| 123 |
5/8✓ Branch 8 → 9 taken 2682 times.
✗ Branch 8 → 44 not taken.
✓ Branch 9 → 10 taken 2682 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 2682 times.
✗ Branch 10 → 44 not taken.
✓ Branch 33 → 12 taken 107543 times.
✓ Branch 33 → 34 taken 62 times.
|
107605 | for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values) |
| 124 |
7/10✓ Branch 13 → 14 taken 107543 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 107543 times.
✗ Branch 14 → 44 not taken.
✓ Branch 15 → 16 taken 107543 times.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 7847 times.
✓ Branch 16 → 18 taken 99696 times.
✓ Branch 19 → 20 taken 7847 times.
✓ Branch 19 → 31 taken 99696 times.
|
107543 | if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT)) |
| 125 |
3/4✓ Branch 20 → 21 taken 7847 times.
✗ Branch 20 → 43 not taken.
✓ Branch 29 → 23 taken 13463 times.
✓ Branch 29 → 30 taken 5227 times.
|
18690 | for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations()) |
| 126 |
3/4✓ Branch 24 → 25 taken 13463 times.
✗ Branch 24 → 43 not taken.
✓ Branch 25 → 26 taken 2620 times.
✓ Branch 25 → 27 taken 10843 times.
|
13463 | if (manifestation->scope == baseType.getBodyScope()) |
| 127 | 2620 | return symbolType; | |
| 128 | |||
| 129 | // This source file does not know about the struct at all | ||
| 130 | // -> show it how to find the struct | ||
| 131 |
2/4✓ Branch 34 → 35 taken 62 times.
✗ Branch 34 → 45 not taken.
✓ Branch 35 → 36 taken 62 times.
✗ Branch 35 → 45 not taken.
|
62 | const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType()); |
| 132 |
1/2✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 62 times.
|
62 | assert(origRegistryEntry != nullptr); |
| 133 | 62 | const uint64_t typeId = origRegistryEntry->typeId; | |
| 134 | 62 | SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry; | |
| 135 |
2/4✓ Branch 38 → 39 taken 62 times.
✗ Branch 38 → 45 not taken.
✓ Branch 39 → 40 taken 62 times.
✗ Branch 39 → 45 not taken.
|
62 | sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false); |
| 136 | |||
| 137 | 62 | return symbolType; | |
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Returns the operator function list for the current manifestation and the given node | ||
| 142 | * | ||
| 143 | * @param node Node to retrieve the op fct pointer list from | ||
| 144 | * @return Op fct pointer list | ||
| 145 | */ | ||
| 146 | 889 | std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const { | |
| 147 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 889 times.
|
889 | assert(node->getOpFctPointers()->size() > manIdx); |
| 148 | 889 | return node->getOpFctPointers()->at(manIdx); | |
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Check if a function has been type-checked already. If not, request a revisit | ||
| 153 | * | ||
| 154 | * @param fct Function to check | ||
| 155 | */ | ||
| 156 | 24808 | void TypeChecker::requestRevisitIfRequired(const Function *fct) { | |
| 157 |
4/4✓ Branch 2 → 3 taken 24714 times.
✓ Branch 2 → 5 taken 94 times.
✓ Branch 3 → 4 taken 13937 times.
✓ Branch 3 → 5 taken 10777 times.
|
24808 | if (fct && !fct->alreadyTypeChecked) |
| 158 | 13937 | fct->entry->scope->sourceFile->reVisitRequested = true; | |
| 159 | 24808 | } | |
| 160 | |||
| 161 | /** | ||
| 162 | * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module. | ||
| 163 | * | ||
| 164 | * @param typeName Given type name | ||
| 165 | */ | ||
| 166 | 31625 | void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const { | |
| 167 |
2/2✓ Branch 18 → 4 taken 92594 times.
✓ Branch 18 → 19 taken 29075 times.
|
121669 | for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) { |
| 168 |
8/10✓ Branch 7 → 8 taken 92594 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 7667 times.
✓ Branch 8 → 12 taken 84927 times.
✓ Branch 9 → 10 taken 7667 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2550 times.
✓ Branch 10 → 12 taken 5117 times.
✓ Branch 13 → 14 taken 2550 times.
✓ Branch 13 → 16 taken 90044 times.
|
92594 | if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) { |
| 169 |
1/2✓ Branch 14 → 15 taken 2550 times.
✗ Branch 14 → 20 not taken.
|
2550 | sourceFile->requestRuntimeModule(runtimeModule); |
| 170 | 2550 | break; | |
| 171 | } | ||
| 172 | } | ||
| 173 | 31625 | } | |
| 174 | |||
| 175 | /** | ||
| 176 | * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module. | ||
| 177 | * | ||
| 178 | * @param functionName Given function name | ||
| 179 | */ | ||
| 180 | 9351 | void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const { | |
| 181 |
2/2✓ Branch 18 → 4 taken 143317 times.
✓ Branch 18 → 19 taken 8746 times.
|
152063 | for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) { |
| 182 |
8/10✓ Branch 7 → 8 taken 143317 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 632 times.
✓ Branch 8 → 12 taken 142685 times.
✓ Branch 9 → 10 taken 632 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 605 times.
✓ Branch 10 → 12 taken 27 times.
✓ Branch 13 → 14 taken 605 times.
✓ Branch 13 → 16 taken 142712 times.
|
143317 | if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) { |
| 183 |
1/2✓ Branch 14 → 15 taken 605 times.
✗ Branch 14 → 20 not taken.
|
605 | sourceFile->requestRuntimeModule(runtimeModule); |
| 184 | 605 | break; | |
| 185 | } | ||
| 186 | } | ||
| 187 | 9351 | } | |
| 188 | |||
| 189 | /** | ||
| 190 | * Add a soft error to the error list | ||
| 191 | */ | ||
| 192 | 25 | void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const { | |
| 193 | 25 | resourceManager.errorManager.addSoftError(node, errorType, message); | |
| 194 | 25 | } | |
| 195 | |||
| 196 | } // namespace spice::compiler | ||
| 197 |