GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.9% 94 / 0 / 95
Functions: 100.0% 11 / 0 / 11
Branches: 65.7% 134 / 0 / 204

src/typechecker/TypeChecker.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 <symboltablebuilder/Scope.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/FunctionManager.h>
11
12 namespace spice::compiler {
13
14 3597 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 3597 times.
✗ Branch 4 → 7 not taken.
3597 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 3597 }
17
18 2847 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 2847 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 2847 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 1197 times.
✓ Branch 2 → 4 taken 1650 times.
2847 if (isPrepare)
25 1197 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 2801 times.
✓ Branch 4 → 22 taken 46 times.
2847 visitChildren(node);
29
30 // Check which implicit structures we need for each struct, defined in this source file
31
2/2
✓ Branch 6 → 7 taken 1183 times.
✓ Branch 6 → 19 taken 1618 times.
2801 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 1183 times.
✗ Branch 7 → 26 not taken.
1183 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 16 → 10 taken 724 times.
✓ Branch 16 → 17 taken 1183 times.
1907 for (const Struct *manifestation : manifestations) {
34 // Check if we need to create a default ctor, copy ctor or dtor
35
1/2
✓ Branch 11 → 12 taken 724 times.
✗ Branch 11 → 23 not taken.
724 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 12 → 13 taken 724 times.
✗ Branch 12 → 23 not taken.
724 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 13 → 14 taken 724 times.
✗ Branch 13 → 23 not taken.
724 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
38 }
39 1183 }
40
41
1/2
✓ Branch 19 → 20 taken 2801 times.
✗ Branch 19 → 27 not taken.
2801 return nullptr;
42 }
43
44 /**
45 * Check if the capture rules for async lambdas are enforced if the async attribute is set
46 *
47 * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the
48 * fat pointer and can ensure, that no stack variable is referenced inside the lambda.
49 *
50 * @param node Lambda base node
51 * @param attrs Lambda attributes
52 * @return False if the rules are violated, true otherwise
53 */
54 34 bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const {
55 // If the async attribute is not set, we can return early
56
18/32
✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 13 taken 29 times.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 5 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 5 times.
✓ Branch 15 → 16 taken 5 times.
✓ Branch 15 → 17 taken 29 times.
✓ Branch 17 → 18 taken 5 times.
✓ Branch 17 → 20 taken 29 times.
✓ Branch 20 → 21 taken 5 times.
✓ Branch 20 → 22 taken 29 times.
✓ Branch 22 → 23 taken 5 times.
✓ Branch 22 → 25 taken 29 times.
✓ Branch 25 → 26 taken 29 times.
✓ Branch 25 → 27 taken 5 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.
54 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
57 29 return true; // Not violated
58
59 // If we don't have any captures, we can return early
60 5 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
61
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 5 times.
5 if (captures.empty())
62 return true; // Not violated
63
64 // Check for the capture rules
65 5 if (const Capture &capture = captures.begin()->second;
66
6/8
✓ Branch 33 → 34 taken 3 times.
✓ Branch 33 → 39 taken 2 times.
✓ Branch 36 → 37 taken 1 time.
✓ Branch 36 → 39 taken 2 times.
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 40 not taken.
✓ Branch 41 → 42 taken 5 times.
✗ Branch 41 → 51 not taken.
5 captures.size() > 1 || !capture.capturedSymbol->getQualType().isPtr() || capture.getMode() != BY_VALUE) {
67 5 const auto warningMessage =
68 "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead "
69 "to bugs due to references, outliving the validity scope of the referenced variable.";
70
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);
71
1/2
✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 77 not taken.
5 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
72 5 }
73
74 5 return false; // Violated
75 }
76
77 26 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
78
1/2
✓ Branch 2 → 3 taken 26 times.
✗ Branch 2 → 40 not taken.
26 Scope *matchScope = thisType.getBodyScope();
79
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 26 times.
26 assert(matchScope != nullptr);
80
2/4
✓ Branch 5 → 6 taken 26 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 26 times.
✗ Branch 9 → 23 not taken.
52 const ArgList args = {{thisType.toConstRef(node), false}};
81
2/4
✓ Branch 14 → 15 taken 26 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 26 times.
✗ Branch 15 → 29 not taken.
104 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
82 26 }
83
84 147909 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
85 // Skip all types, except structs
86
3/4
✓ Branch 2 → 3 taken 147909 times.
✗ Branch 2 → 50 not taken.
✓ Branch 3 → 4 taken 134904 times.
✓ Branch 3 → 5 taken 13005 times.
147909 if (!symbolType.isBase(TY_STRUCT))
87 134904 return symbolType;
88
89 // If the target scope is in the current source file, we can return the symbol type as is
90 13005 SourceFile *targetSourceFile = targetScope->sourceFile;
91
2/2
✓ Branch 5 → 6 taken 5932 times.
✓ Branch 5 → 7 taken 7073 times.
13005 if (targetSourceFile == sourceFile)
92 5932 return symbolType;
93
94 // Match the scope of the symbol type against all scopes in the name registry of the target file
95
5/8
✓ Branch 7 → 8 taken 7073 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 7073 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 7073 times.
✗ Branch 9 → 46 not taken.
✓ Branch 33 → 11 taken 159301 times.
✓ Branch 33 → 34 taken 317 times.
159618 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
96
7/10
✓ Branch 12 → 13 taken 159301 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 159301 times.
✗ Branch 13 → 46 not taken.
✓ Branch 14 → 15 taken 159301 times.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 18137 times.
✓ Branch 15 → 17 taken 141164 times.
✓ Branch 18 → 19 taken 18137 times.
✓ Branch 18 → 31 taken 141164 times.
159301 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
97
3/4
✓ Branch 19 → 20 taken 18137 times.
✗ Branch 19 → 45 not taken.
✓ Branch 29 → 22 taken 34662 times.
✓ Branch 29 → 30 taken 11381 times.
46043 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
98
4/6
✓ Branch 23 → 24 taken 34662 times.
✗ Branch 23 → 44 not taken.
✓ Branch 24 → 25 taken 34662 times.
✗ Branch 24 → 44 not taken.
✓ Branch 25 → 26 taken 6756 times.
✓ Branch 25 → 27 taken 27906 times.
34662 if (manifestation->scope == symbolType.getBase().getBodyScope())
99 6756 return symbolType;
100
101 // The target file does not know about the struct at all
102 // -> show it how to find the struct
103
3/6
✓ Branch 34 → 35 taken 317 times.
✗ Branch 34 → 47 not taken.
✓ Branch 35 → 36 taken 317 times.
✗ Branch 35 → 47 not taken.
✓ Branch 36 → 37 taken 317 times.
✗ Branch 36 → 47 not taken.
317 const std::string structName = symbolType.getBase().getSubType();
104
1/2
✓ Branch 37 → 38 taken 317 times.
✗ Branch 37 → 48 not taken.
317 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
105
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 317 times.
317 assert(origRegistryEntry != nullptr);
106 317 const uint64_t targetTypeId = origRegistryEntry->typeId;
107 317 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
108
1/2
✓ Branch 40 → 41 taken 317 times.
✗ Branch 40 → 48 not taken.
317 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
109
110 317 return symbolType;
111 317 }
112
113 4598 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
114 // Skip all types, except structs
115
3/4
✓ Branch 2 → 3 taken 4598 times.
✗ Branch 2 → 45 not taken.
✓ Branch 3 → 4 taken 582 times.
✓ Branch 3 → 5 taken 4016 times.
4598 if (!symbolType.isBase(TY_STRUCT))
116 582 return symbolType;
117
118 // If the given source file is in the current one, we can return the symbol type as is
119 4016 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
120
2/2
✓ Branch 5 → 6 taken 1361 times.
✓ Branch 5 → 7 taken 2655 times.
4016 if (sourceSourceFile == sourceFile)
121 1361 return symbolType;
122
123 // Match the scope of the symbol type against all scopes in the name registry of this source file
124
1/2
✓ Branch 7 → 8 taken 2655 times.
✗ Branch 7 → 45 not taken.
2655 const QualType baseType = symbolType.getBase();
125
5/8
✓ Branch 8 → 9 taken 2655 times.
✗ Branch 8 → 44 not taken.
✓ Branch 9 → 10 taken 2655 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 2655 times.
✗ Branch 10 → 44 not taken.
✓ Branch 33 → 12 taken 106709 times.
✓ Branch 33 → 34 taken 60 times.
106769 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
126
7/10
✓ Branch 13 → 14 taken 106709 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 106709 times.
✗ Branch 14 → 44 not taken.
✓ Branch 15 → 16 taken 106709 times.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 7773 times.
✓ Branch 16 → 18 taken 98936 times.
✓ Branch 19 → 20 taken 7773 times.
✓ Branch 19 → 31 taken 98936 times.
106709 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
127
3/4
✓ Branch 20 → 21 taken 7773 times.
✗ Branch 20 → 43 not taken.
✓ Branch 29 → 23 taken 13331 times.
✓ Branch 29 → 30 taken 5178 times.
18509 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
128
3/4
✓ Branch 24 → 25 taken 13331 times.
✗ Branch 24 → 43 not taken.
✓ Branch 25 → 26 taken 2595 times.
✓ Branch 25 → 27 taken 10736 times.
13331 if (manifestation->scope == baseType.getBodyScope())
129 2595 return symbolType;
130
131 // This source file does not know about the struct at all
132 // -> show it how to find the struct
133
2/4
✓ Branch 34 → 35 taken 60 times.
✗ Branch 34 → 45 not taken.
✓ Branch 35 → 36 taken 60 times.
✗ Branch 35 → 45 not taken.
60 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
134
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 60 times.
60 assert(origRegistryEntry != nullptr);
135 60 const uint64_t typeId = origRegistryEntry->typeId;
136 60 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
137
2/4
✓ Branch 38 → 39 taken 60 times.
✗ Branch 38 → 45 not taken.
✓ Branch 39 → 40 taken 60 times.
✗ Branch 39 → 45 not taken.
60 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
138
139 60 return symbolType;
140 }
141
142 /**
143 * Returns the operator function list for the current manifestation and the given node
144 *
145 * @param node Node to retrieve the op fct pointer list from
146 * @return Op fct pointer list
147 */
148 911 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
149
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 911 times.
911 assert(node->getOpFctPointers()->size() > manIdx);
150 911 return node->getOpFctPointers()->at(manIdx);
151 }
152
153 /**
154 * Check if a function has been type-checked already. If not, request a revisit
155 *
156 * @param fct Function to check
157 */
158 27786 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
159
4/4
✓ Branch 2 → 3 taken 27691 times.
✓ Branch 2 → 5 taken 95 times.
✓ Branch 3 → 4 taken 16428 times.
✓ Branch 3 → 5 taken 11263 times.
27786 if (fct && !fct->alreadyTypeChecked)
160 16428 fct->entry->scope->sourceFile->reVisitRequested = true;
161 27786 }
162
163 /**
164 * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module.
165 *
166 * @param typeName Given type name
167 */
168 36290 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
169
2/2
✓ Branch 18 → 4 taken 106608 times.
✓ Branch 18 → 19 taken 33746 times.
140354 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
170
8/10
✓ Branch 7 → 8 taken 106608 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 7910 times.
✓ Branch 8 → 12 taken 98698 times.
✓ Branch 9 → 10 taken 7910 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2544 times.
✓ Branch 10 → 12 taken 5366 times.
✓ Branch 13 → 14 taken 2544 times.
✓ Branch 13 → 16 taken 104064 times.
106608 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
171
1/2
✓ Branch 14 → 15 taken 2544 times.
✗ Branch 14 → 20 not taken.
2544 sourceFile->requestRuntimeModule(runtimeModule);
172 2544 break;
173 }
174 }
175 36290 }
176
177 /**
178 * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module.
179 *
180 * @param functionName Given function name
181 */
182 11036 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
183
2/2
✓ Branch 18 → 4 taken 169361 times.
✓ Branch 18 → 19 taken 10447 times.
179808 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
184
8/10
✓ Branch 7 → 8 taken 169361 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 616 times.
✓ Branch 8 → 12 taken 168745 times.
✓ Branch 9 → 10 taken 616 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 589 times.
✓ Branch 10 → 12 taken 27 times.
✓ Branch 13 → 14 taken 589 times.
✓ Branch 13 → 16 taken 168772 times.
169361 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
185
1/2
✓ Branch 14 → 15 taken 589 times.
✗ Branch 14 → 20 not taken.
589 sourceFile->requestRuntimeModule(runtimeModule);
186 589 break;
187 }
188 }
189 11036 }
190
191 /**
192 * Add a soft error to the error list
193 */
194 25 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
195 25 resourceManager.errorManager.addSoftError(node, errorType, message);
196 25 }
197
198 } // namespace spice::compiler
199