GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 90.7% 98 / 0 / 108
Functions: 92.3% 12 / 0 / 13
Branches: 60.2% 142 / 0 / 236

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 66627 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 66627 times.
✗ Branch 4 → 7 not taken.
66627 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 66627 }
17
18 30286 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 30286 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 30286 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 11016 times.
✓ Branch 2 → 4 taken 19270 times.
30286 if (isPrepare)
25 11016 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 30176 times.
✓ Branch 4 → 29 taken 110 times.
30286 visitChildren(node);
29
30 // Check which implicit structures we need for each struct, defined in this source file. Manifestations that are
31 // substantiated after this point are decided on directly at their creation (see createImplicitDefaultMembers).
32
2/2
✓ Branch 6 → 7 taken 10976 times.
✓ Branch 6 → 25 taken 19200 times.
30176 if (isPrepare)
33
3/4
✓ Branch 7 → 8 taken 10976 times.
✗ Branch 7 → 32 not taken.
✓ Branch 22 → 10 taken 15363 times.
✓ Branch 22 → 23 taken 10976 times.
37315 for (Struct *manifestation : rootScope->getAllStructManifestationsInDeclarationOrder())
34
1/2
✓ Branch 12 → 13 taken 15363 times.
✗ Branch 12 → 30 not taken.
26339 createImplicitDefaultMembers(*manifestation, node);
35
36
1/2
✓ Branch 25 → 26 taken 30176 times.
✗ Branch 25 → 33 not taken.
60352 return nullptr;
37 }
38
39 /**
40 * Check if the capture rules for async lambdas are enforced if the async attribute is set
41 *
42 * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the
43 * fat pointer and can ensure, that no stack variable is referenced inside the lambda.
44 *
45 * @param node Lambda base node
46 * @param attrs Lambda attributes
47 * @return False if the rules are violated, true otherwise
48 */
49 1755 bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const {
50 // If the async attribute is not set, we can return early
51
18/32
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 13 taken 1739 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 1739 times.
✓ Branch 17 → 18 taken 16 times.
✓ Branch 17 → 20 taken 1739 times.
✓ Branch 20 → 21 taken 16 times.
✓ Branch 20 → 22 taken 1739 times.
✓ Branch 22 → 23 taken 16 times.
✓ Branch 22 → 25 taken 1739 times.
✓ Branch 25 → 26 taken 1739 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.
1819 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
52 1739 return true; // Not violated
53
54 // If we don't have any captures, we can return early
55 16 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
56
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 16 times.
16 if (captures.empty())
57 ✗ return true; // Not violated
58
59 // Check for the capture rules
60 16 if (const Capture &capture = captures.begin()->second;
61
8/8
✓ Branch 33 → 34 taken 10 times.
✓ Branch 33 → 39 taken 6 times.
✓ Branch 36 → 37 taken 6 times.
✓ Branch 36 → 39 taken 4 times.
✓ Branch 38 → 39 taken 2 times.
✓ Branch 38 → 40 taken 4 times.
✓ Branch 41 → 42 taken 12 times.
✓ Branch 41 → 51 taken 4 times.
16 captures.size() > 1 || !capture.capturedSymbol->getQualType().isPtr() || capture.getMode() != BY_VALUE) {
62 12 const auto warningMessage =
63 "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead "
64 "to bugs due to references, outliving the validity scope of the referenced variable.";
65
2/4
✓ Branch 44 → 45 taken 12 times.
✗ Branch 44 → 73 not taken.
✓ Branch 45 → 46 taken 12 times.
✗ Branch 45 → 71 not taken.
24 const CompilerWarning warning(node->codeLoc, ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION, warningMessage);
66
1/2
✓ Branch 48 → 49 taken 12 times.
✗ Branch 48 → 77 not taken.
12 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
67 12 }
68
69 16 return false; // Violated
70 }
71
72 2180 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
73
1/2
✓ Branch 2 → 3 taken 2180 times.
✗ Branch 2 → 42 not taken.
2180 Scope *matchScope = thisType.getBodyScope();
74
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2180 times.
2180 assert(matchScope != nullptr);
75
2/4
✓ Branch 5 → 6 taken 2180 times.
✗ Branch 5 → 28 not taken.
✓ Branch 9 → 10 taken 2180 times.
✗ Branch 9 → 24 not taken.
4360 const ArgList args = {{thisType.toConstRef(node), false}};
76 // The new instance being constructed is never const, regardless of whether the copy source (thisType) is const
77
3/6
✓ Branch 12 → 13 taken 2180 times.
✗ Branch 12 → 36 not taken.
✓ Branch 15 → 16 taken 2180 times.
✗ Branch 15 → 32 not taken.
✓ Branch 16 → 17 taken 2180 times.
✗ Branch 16 → 30 not taken.
8720 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType.toNonConst(), args, {}, true, node);
78 2180 }
79
80 ✗ Function *TypeChecker::matchMoveCtor(const QualType &thisType, const ASTNode *node) const {
81 ✗ Scope *matchScope = thisType.getBodyScope();
82 ✗ assert(matchScope != nullptr);
83 ✗ const ArgList args = {{thisType.toNonConst().toRef(node), false}};
84 // The new instance being constructed is never const, regardless of whether the move source (thisType) is const
85 ✗ return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType.toNonConst(), args, {}, true, node);
86 ✗ }
87
88 49423071 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
89 // Skip all types, except structs
90
3/4
✓ Branch 2 → 3 taken 49423071 times.
✗ Branch 2 → 62 not taken.
✓ Branch 3 → 4 taken 32583971 times.
✓ Branch 3 → 5 taken 16839100 times.
49423071 if (!symbolType.isBase(TY_STRUCT))
91 32583971 return symbolType;
92
93 // If the target scope is in the current source file, we can return the symbol type as is
94 16839100 SourceFile *targetSourceFile = targetScope->sourceFile;
95
2/2
✓ Branch 5 → 6 taken 647378 times.
✓ Branch 5 → 7 taken 16191722 times.
16839100 if (targetSourceFile == sourceFile)
96 647378 return symbolType;
97
98 // Match the scope of the symbol type against all scopes in the name registry of the target file
99 ✗ for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
100 ✗ if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
101
3/4
✓ Branch 19 → 20 taken 895640518 times.
✗ Branch 19 → 57 not taken.
✓ Branch 37 → 22 taken 1492927445 times.
✓ Branch 37 → 38 taken 879871200 times.
3268439163 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
102
4/6
✓ Branch 24 → 25 taken 1492927445 times.
✗ Branch 24 → 56 not taken.
✓ Branch 25 → 26 taken 1492927445 times.
✗ Branch 25 → 56 not taken.
✓ Branch 26 → 27 taken 15769318 times.
✓ Branch 26 → 28 taken 1477158127 times.
1492927445 if (manifestation->scope == symbolType.getBase().getBodyScope())
103 15769318 return symbolType;
104
105 // The target file does not know about the struct at all
106 // -> show it how to find the struct
107
3/6
✓ Branch 42 → 43 taken 422404 times.
✗ Branch 42 → 59 not taken.
✓ Branch 43 → 44 taken 422404 times.
✗ Branch 43 → 59 not taken.
✓ Branch 44 → 45 taken 422404 times.
✗ Branch 44 → 59 not taken.
422404 const std::string structName = symbolType.getBase().getSubType();
108
1/2
✓ Branch 45 → 46 taken 422404 times.
✗ Branch 45 → 60 not taken.
422404 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
109 // If even this file does not know the struct by its unqualified name (deep transitive import), there is
110 // nothing to copy over. Skip teaching the target file; the type identity itself is unaffected, and member
111 // access falls back to the resolved body scope.
112
2/2
✓ Branch 46 → 47 taken 32214 times.
✓ Branch 46 → 48 taken 390190 times.
422404 if (origRegistryEntry == nullptr)
113 32214 return symbolType;
114 // Do not clobber an entry the target file already has under this name (e.g. its OWN same-named struct, like
115 // llvm's `Function` vs the model's `Function`). Teaching uses keepNewOnCollision=false, which would otherwise
116 // ERASE the target's existing entry and break resolution of its own type. The struct's QualType identity is
117 // carried by pointer regardless, so a name that is already taken does not need (re-)teaching here.
118
3/4
✓ Branch 48 → 49 taken 390190 times.
✗ Branch 48 → 60 not taken.
✓ Branch 49 → 50 taken 127644 times.
✓ Branch 49 → 51 taken 262546 times.
390190 if (targetSourceFile->exportedNameRegistry.contains(structName))
119 127644 return symbolType;
120 262546 const uint64_t targetTypeId = origRegistryEntry->typeId;
121 262546 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
122
1/2
✓ Branch 51 → 52 taken 262546 times.
✗ Branch 51 → 60 not taken.
262546 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
123
124 262546 return symbolType;
125 422404 }
126
127 499559 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
128 // Skip all types, except structs
129
3/4
✓ Branch 2 → 3 taken 499559 times.
✗ Branch 2 → 57 not taken.
✓ Branch 3 → 4 taken 23404 times.
✓ Branch 3 → 5 taken 476155 times.
499559 if (!symbolType.isBase(TY_STRUCT))
130 23404 return symbolType;
131
132 // If the given source file is in the current one, we can return the symbol type as is
133 476155 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
134
2/2
✓ Branch 5 → 6 taken 40036 times.
✓ Branch 5 → 7 taken 436119 times.
476155 if (sourceSourceFile == sourceFile)
135 40036 return symbolType;
136
137 // Match the scope of the symbol type against all scopes in the name registry of this source file
138
1/2
✓ Branch 7 → 8 taken 436119 times.
✗ Branch 7 → 57 not taken.
436119 const QualType baseType = symbolType.getBase();
139
5/8
✓ Branch 8 → 9 taken 436119 times.
✗ Branch 8 → 56 not taken.
✓ Branch 9 → 10 taken 436119 times.
✗ Branch 9 → 56 not taken.
✓ Branch 10 → 11 taken 436119 times.
✗ Branch 10 → 56 not taken.
✓ Branch 41 → 12 taken 302784083 times.
✓ Branch 41 → 42 taken 1174 times.
302785257 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
140
7/10
✓ Branch 13 → 14 taken 302784083 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 302784083 times.
✗ Branch 14 → 56 not taken.
✓ Branch 15 → 16 taken 302784083 times.
✗ Branch 15 → 56 not taken.
✓ Branch 16 → 17 taken 35345085 times.
✓ Branch 16 → 18 taken 267438998 times.
✓ Branch 19 → 20 taken 35345085 times.
✓ Branch 19 → 39 taken 267438998 times.
302784083 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
141
3/4
✓ Branch 20 → 21 taken 35345085 times.
✗ Branch 20 → 55 not taken.
✓ Branch 37 → 23 taken 46926256 times.
✓ Branch 37 → 38 taken 34910140 times.
117181481 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
142
3/4
✓ Branch 25 → 26 taken 46926256 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 434945 times.
✓ Branch 26 → 28 taken 46491311 times.
46926256 if (manifestation->scope == baseType.getBodyScope())
143 434945 return symbolType;
144
145 // This source file does not know about the struct at all
146 // -> show it how to find the struct
147
2/4
✓ Branch 42 → 43 taken 1174 times.
✗ Branch 42 → 57 not taken.
✓ Branch 43 → 44 taken 1174 times.
✗ Branch 43 → 57 not taken.
1174 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
148 // If even the source file does not know the struct by its unqualified name (deep transitive import), there is
149 // nothing to copy over. Skip teaching this file; the type identity itself is unaffected, and member access
150 // falls back to the resolved body scope.
151
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 1174 times.
1174 if (origRegistryEntry == nullptr)
152 ✗ return symbolType;
153 // Do not clobber an entry this file already has under this name (see mapLocalTypeToImportedScopeType): teaching
154 // with keepNewOnCollision=false would ERASE this file's own same-named struct entry. The QualType identity is
155 // carried by pointer, so an already-taken name needs no (re-)teaching here.
156
4/6
✓ Branch 46 → 47 taken 1174 times.
✗ Branch 46 → 57 not taken.
✓ Branch 47 → 48 taken 1174 times.
✗ Branch 47 → 57 not taken.
✓ Branch 48 → 49 taken 250 times.
✓ Branch 48 → 50 taken 924 times.
1174 if (sourceFile->exportedNameRegistry.contains(baseType.getSubType()))
157 250 return symbolType;
158 924 const uint64_t typeId = origRegistryEntry->typeId;
159 924 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
160
2/4
✓ Branch 50 → 51 taken 924 times.
✗ Branch 50 → 57 not taken.
✓ Branch 51 → 52 taken 924 times.
✗ Branch 51 → 57 not taken.
924 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
161
162 924 return symbolType;
163 }
164
165 /**
166 * Returns the operator function list for the current manifestation and the given node
167 *
168 * @param node Node to retrieve the op fct pointer list from
169 * @return Op fct pointer list
170 */
171 145508 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
172
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 145508 times.
145508 assert(node->getOpFctPointers()->size() > manIdx);
173 145508 return node->getOpFctPointers()->at(manIdx);
174 }
175
176 /**
177 * Check if a function has been type-checked already. If not, request a revisit
178 *
179 * @param fct Function to check
180 */
181 1404983 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
182
4/4
✓ Branch 2 → 3 taken 1398589 times.
✓ Branch 2 → 5 taken 6394 times.
✓ Branch 3 → 4 taken 954655 times.
✓ Branch 3 → 5 taken 443934 times.
1404983 if (fct && !fct->alreadyTypeChecked)
183 954655 fct->entry->scope->sourceFile->reVisitRequested = true;
184 1404983 }
185
186 /**
187 * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module.
188 *
189 * @param typeName Given type name
190 */
191 1696620 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
192
2/2
✓ Branch 18 → 4 taken 5031033 times.
✓ Branch 18 → 19 taken 1481367 times.
6512400 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
193
8/10
✓ Branch 7 → 8 taken 5031033 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 238459 times.
✓ Branch 8 → 12 taken 4792574 times.
✓ Branch 9 → 10 taken 238459 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 215253 times.
✓ Branch 10 → 12 taken 23206 times.
✓ Branch 13 → 14 taken 215253 times.
✓ Branch 13 → 16 taken 4815780 times.
5031033 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
194
1/2
✓ Branch 14 → 15 taken 215253 times.
✗ Branch 14 → 20 not taken.
215253 sourceFile->requestRuntimeModule(runtimeModule);
195 215253 break;
196 }
197 }
198 1696620 }
199
200 /**
201 * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module.
202 *
203 * @param functionName Given function name
204 */
205 305178 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
206
2/2
✓ Branch 18 → 4 taken 5199870 times.
✓ Branch 18 → 19 taken 267748 times.
5467618 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
207
8/10
✓ Branch 7 → 8 taken 5199870 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 38514 times.
✓ Branch 8 → 12 taken 5161356 times.
✓ Branch 9 → 10 taken 38514 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 37430 times.
✓ Branch 10 → 12 taken 1084 times.
✓ Branch 13 → 14 taken 37430 times.
✓ Branch 13 → 16 taken 5162440 times.
5199870 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
208
1/2
✓ Branch 14 → 15 taken 37430 times.
✗ Branch 14 → 20 not taken.
37430 sourceFile->requestRuntimeModule(runtimeModule);
209 37430 break;
210 }
211 }
212 305178 }
213
214 /**
215 * Add a soft error to the error list
216 */
217 56 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
218 56 resourceManager.errorManager.addSoftError(node, errorType, message);
219 56 }
220
221 946 bool TypeChecker::isCopyCtorCall(const ArgList &ctorArgs, const QualType &thisType) const {
222
2/2
✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 5 taken 942 times.
946 if (ctorArgs.size() != 1)
223 4 return false;
224
2/4
✓ Branch 6 → 7 taken 942 times.
✗ Branch 6 → 12 not taken.
✓ Branch 7 → 8 taken 942 times.
✗ Branch 7 → 12 not taken.
942 const QualType &argType = ctorArgs.front().first.removeReferenceWrapper().toNonConst();
225
1/2
✓ Branch 8 → 9 taken 942 times.
✗ Branch 8 → 13 not taken.
942 return thisType.matches(argType, false, false, true);
226 }
227
228 } // namespace spice::compiler
229