GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 99.0% 100 / 0 / 101
Functions: 100.0% 12 / 0 / 12
Branches: 66.4% 142 / 0 / 214

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 3966 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 3966 times.
✗ Branch 4 → 7 not taken.
3966 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 3966 }
17
18 3057 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 3057 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 3057 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 1275 times.
✓ Branch 2 → 4 taken 1782 times.
3057 if (isPrepare)
25 1275 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 3011 times.
✓ Branch 4 → 31 taken 46 times.
3057 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 1261 times.
✓ Branch 6 → 27 taken 1750 times.
3011 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 1261 times.
✗ Branch 7 → 35 not taken.
1261 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 24 → 10 taken 803 times.
✓ Branch 24 → 25 taken 1261 times.
3325 for (const Struct *manifestation : manifestations) {
34 // Check if we need to create a default ctor, copy ctor or dtor
35
1/2
✓ Branch 12 → 13 taken 803 times.
✗ Branch 12 → 32 not taken.
803 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 13 → 14 taken 803 times.
✗ Branch 13 → 32 not taken.
803 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 14 → 15 taken 803 times.
✗ Branch 14 → 32 not taken.
803 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
38 }
39 1261 }
40
41
1/2
✓ Branch 27 → 28 taken 3011 times.
✗ Branch 27 → 36 not taken.
6022 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 35 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 6 times.
✓ Branch 2 → 13 taken 29 times.
✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 6 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 6 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6 times.
✓ Branch 15 → 16 taken 6 times.
✓ Branch 15 → 17 taken 29 times.
✓ Branch 17 → 18 taken 6 times.
✓ Branch 17 → 20 taken 29 times.
✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 22 taken 29 times.
✓ Branch 22 → 23 taken 6 times.
✓ Branch 22 → 25 taken 29 times.
✓ Branch 25 → 26 taken 29 times.
✓ Branch 25 → 27 taken 6 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.
59 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 6 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
61
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 6 times.
6 if (captures.empty())
62 return true; // Not violated
63
64 // Check for the capture rules
65 6 if (const Capture &capture = captures.begin()->second;
66
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) {
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.
10 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 6 return false; // Violated
75 }
76
77 27 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
78
1/2
✓ Branch 2 → 3 taken 27 times.
✗ Branch 2 → 40 not taken.
27 Scope *matchScope = thisType.getBodyScope();
79
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 27 times.
27 assert(matchScope != nullptr);
80
2/4
✓ Branch 5 → 6 taken 27 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 27 times.
✗ Branch 9 → 23 not taken.
54 const ArgList args = {{thisType.toConstRef(node), false}};
81
2/4
✓ Branch 14 → 15 taken 27 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 27 times.
✗ Branch 15 → 29 not taken.
108 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
82 27 }
83
84 161069 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
85 // Skip all types, except structs
86
3/4
✓ Branch 2 → 3 taken 161069 times.
✗ Branch 2 → 58 not taken.
✓ Branch 3 → 4 taken 145727 times.
✓ Branch 3 → 5 taken 15342 times.
161069 if (!symbolType.isBase(TY_STRUCT))
87 145727 return symbolType;
88
89 // If the target scope is in the current source file, we can return the symbol type as is
90 15342 SourceFile *targetSourceFile = targetScope->sourceFile;
91
2/2
✓ Branch 5 → 6 taken 6598 times.
✓ Branch 5 → 7 taken 8744 times.
15342 if (targetSourceFile == sourceFile)
92 6598 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 8744 times.
✗ Branch 7 → 54 not taken.
✓ Branch 8 → 9 taken 8744 times.
✗ Branch 8 → 54 not taken.
✓ Branch 9 → 10 taken 8744 times.
✗ Branch 9 → 54 not taken.
✓ Branch 41 → 11 taken 205242 times.
✓ Branch 41 → 42 taken 396 times.
205638 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
96
7/10
✓ Branch 12 → 13 taken 205242 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 205242 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 205242 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 25083 times.
✓ Branch 15 → 17 taken 180159 times.
✓ Branch 18 → 19 taken 25083 times.
✓ Branch 18 → 39 taken 180159 times.
205242 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
97
3/4
✓ Branch 19 → 20 taken 25083 times.
✗ Branch 19 → 53 not taken.
✓ Branch 37 → 22 taken 50766 times.
✓ Branch 37 → 38 taken 16735 times.
92584 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
98
4/6
✓ Branch 24 → 25 taken 50766 times.
✗ Branch 24 → 52 not taken.
✓ Branch 25 → 26 taken 50766 times.
✗ Branch 25 → 52 not taken.
✓ Branch 26 → 27 taken 8348 times.
✓ Branch 26 → 28 taken 42418 times.
50766 if (manifestation->scope == symbolType.getBase().getBodyScope())
99 8348 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 42 → 43 taken 396 times.
✗ Branch 42 → 55 not taken.
✓ Branch 43 → 44 taken 396 times.
✗ Branch 43 → 55 not taken.
✓ Branch 44 → 45 taken 396 times.
✗ Branch 44 → 55 not taken.
396 const std::string structName = symbolType.getBase().getSubType();
104
1/2
✓ Branch 45 → 46 taken 396 times.
✗ Branch 45 → 56 not taken.
396 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
105
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 396 times.
396 assert(origRegistryEntry != nullptr);
106 396 const uint64_t targetTypeId = origRegistryEntry->typeId;
107 396 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
108
1/2
✓ Branch 48 → 49 taken 396 times.
✗ Branch 48 → 56 not taken.
396 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
109
110 396 return symbolType;
111 396 }
112
113 5020 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
114 // Skip all types, except structs
115
3/4
✓ Branch 2 → 3 taken 5020 times.
✗ Branch 2 → 53 not taken.
✓ Branch 3 → 4 taken 617 times.
✓ Branch 3 → 5 taken 4403 times.
5020 if (!symbolType.isBase(TY_STRUCT))
116 617 return symbolType;
117
118 // If the given source file is in the current one, we can return the symbol type as is
119 4403 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
120
2/2
✓ Branch 5 → 6 taken 1476 times.
✓ Branch 5 → 7 taken 2927 times.
4403 if (sourceSourceFile == sourceFile)
121 1476 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 2927 times.
✗ Branch 7 → 53 not taken.
2927 const QualType baseType = symbolType.getBase();
125
5/8
✓ Branch 8 → 9 taken 2927 times.
✗ Branch 8 → 52 not taken.
✓ Branch 9 → 10 taken 2927 times.
✗ Branch 9 → 52 not taken.
✓ Branch 10 → 11 taken 2927 times.
✗ Branch 10 → 52 not taken.
✓ Branch 41 → 12 taken 125184 times.
✓ Branch 41 → 42 taken 70 times.
125254 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
126
7/10
✓ Branch 13 → 14 taken 125184 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 125184 times.
✗ Branch 14 → 52 not taken.
✓ Branch 15 → 16 taken 125184 times.
✗ Branch 15 → 52 not taken.
✓ Branch 16 → 17 taken 9101 times.
✓ Branch 16 → 18 taken 116083 times.
✓ Branch 19 → 20 taken 9101 times.
✓ Branch 19 → 39 taken 116083 times.
125184 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
127
3/4
✓ Branch 20 → 21 taken 9101 times.
✗ Branch 20 → 51 not taken.
✓ Branch 37 → 23 taken 16464 times.
✓ Branch 37 → 38 taken 6244 times.
31809 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
128
3/4
✓ Branch 25 → 26 taken 16464 times.
✗ Branch 25 → 51 not taken.
✓ Branch 26 → 27 taken 2857 times.
✓ Branch 26 → 28 taken 13607 times.
16464 if (manifestation->scope == baseType.getBodyScope())
129 2857 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 42 → 43 taken 70 times.
✗ Branch 42 → 53 not taken.
✓ Branch 43 → 44 taken 70 times.
✗ Branch 43 → 53 not taken.
70 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
134
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 70 times.
70 assert(origRegistryEntry != nullptr);
135 70 const uint64_t typeId = origRegistryEntry->typeId;
136 70 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
137
2/4
✓ Branch 46 → 47 taken 70 times.
✗ Branch 46 → 53 not taken.
✓ Branch 47 → 48 taken 70 times.
✗ Branch 47 → 53 not taken.
70 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
138
139 70 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 1043 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
149
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1043 times.
1043 assert(node->getOpFctPointers()->size() > manIdx);
150 1043 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 30502 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
159
4/4
✓ Branch 2 → 3 taken 30388 times.
✓ Branch 2 → 5 taken 114 times.
✓ Branch 3 → 4 taken 17951 times.
✓ Branch 3 → 5 taken 12437 times.
30502 if (fct && !fct->alreadyTypeChecked)
160 17951 fct->entry->scope->sourceFile->reVisitRequested = true;
161 30502 }
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 40210 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
169
2/2
✓ Branch 18 → 4 taken 118078 times.
✓ Branch 18 → 19 taken 37420 times.
155498 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
170
8/10
✓ Branch 7 → 8 taken 118078 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 8953 times.
✓ Branch 8 → 12 taken 109125 times.
✓ Branch 9 → 10 taken 8953 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2790 times.
✓ Branch 10 → 12 taken 6163 times.
✓ Branch 13 → 14 taken 2790 times.
✓ Branch 13 → 16 taken 115288 times.
118078 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
171
1/2
✓ Branch 14 → 15 taken 2790 times.
✗ Branch 14 → 20 not taken.
2790 sourceFile->requestRuntimeModule(runtimeModule);
172 2790 break;
173 }
174 }
175 40210 }
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 12019 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
183
2/2
✓ Branch 18 → 4 taken 162864 times.
✓ Branch 18 → 19 taken 11361 times.
174225 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
184
8/10
✓ Branch 7 → 8 taken 162864 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 672 times.
✓ Branch 8 → 12 taken 162192 times.
✓ Branch 9 → 10 taken 672 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 658 times.
✓ Branch 10 → 12 taken 14 times.
✓ Branch 13 → 14 taken 658 times.
✓ Branch 13 → 16 taken 162206 times.
162864 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
185
1/2
✓ Branch 14 → 15 taken 658 times.
✗ Branch 14 → 20 not taken.
658 sourceFile->requestRuntimeModule(runtimeModule);
186 658 break;
187 }
188 }
189 12019 }
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 6 bool TypeChecker::isCopyCtorCall(const FctCallNode *node, const QualType &thisType) const {
199
1/2
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 14 not taken.
6 const FctCallNode::FctCallData &data = node->data.at(manIdx);
200
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 4 times.
6 if (data.args.size() != 2)
201 2 return false;
202
2/4
✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 13 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 13 not taken.
4 const QualType &secondArgType = data.args.back().first.removeReferenceWrapper().toNonConst();
203
1/2
✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 14 not taken.
4 return thisType.matches(secondArgType, false, false, true);
204 }
205
206 } // namespace spice::compiler
207