GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.7% 101 / 0 / 109
Functions: 92.3% 12 / 0 / 13
Branches: 62.6% 149 / 0 / 238

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 10219 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 10219 times.
✗ Branch 4 → 7 not taken.
10219 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 10219 }
17
18 7206 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 7206 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 7206 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 2765 times.
✓ Branch 2 → 4 taken 4441 times.
7206 if (isPrepare)
25 2765 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 7159 times.
✓ Branch 4 → 29 taken 47 times.
7206 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 2750 times.
✓ Branch 6 → 25 taken 4409 times.
7159 if (isPrepare)
33
3/4
✓ Branch 7 → 8 taken 2750 times.
✗ Branch 7 → 32 not taken.
✓ Branch 22 → 10 taken 3279 times.
✓ Branch 22 → 23 taken 2750 times.
8779 for (Struct *manifestation : rootScope->getAllStructManifestationsInDeclarationOrder())
34
1/2
✓ Branch 12 → 13 taken 3279 times.
✗ Branch 12 → 30 not taken.
6029 createImplicitDefaultMembers(*manifestation, node);
35
36
1/2
✓ Branch 25 → 26 taken 7159 times.
✗ Branch 25 → 33 not taken.
14318 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 98 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 8 times.
✓ Branch 2 → 13 taken 90 times.
✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 8 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 8 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 8 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 8 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 8 times.
✓ Branch 15 → 16 taken 8 times.
✓ Branch 15 → 17 taken 90 times.
✓ Branch 17 → 18 taken 8 times.
✓ Branch 17 → 20 taken 90 times.
✓ Branch 20 → 21 taken 8 times.
✓ Branch 20 → 22 taken 90 times.
✓ Branch 22 → 23 taken 8 times.
✓ Branch 22 → 25 taken 90 times.
✓ Branch 25 → 26 taken 90 times.
✓ Branch 25 → 27 taken 8 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.
130 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
52 90 return true; // Not violated
53
54 // If we don't have any captures, we can return early
55 8 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
56
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 8 times.
8 if (captures.empty())
57 return true; // Not violated
58
59 // Check for the capture rules
60 8 if (const Capture &capture = captures.begin()->second;
61
8/8
✓ Branch 33 → 34 taken 5 times.
✓ Branch 33 → 39 taken 3 times.
✓ Branch 36 → 37 taken 3 times.
✓ Branch 36 → 39 taken 2 times.
✓ Branch 38 → 39 taken 1 time.
✓ Branch 38 → 40 taken 2 times.
✓ Branch 41 → 42 taken 6 times.
✓ Branch 41 → 51 taken 2 times.
8 captures.size() > 1 || !capture.capturedSymbol->getQualType().isPtr() || capture.getMode() != BY_VALUE) {
62 6 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 6 times.
✗ Branch 44 → 73 not taken.
✓ Branch 45 → 46 taken 6 times.
✗ Branch 45 → 71 not taken.
12 const CompilerWarning warning(node->codeLoc, ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION, warningMessage);
66
1/2
✓ Branch 48 → 49 taken 6 times.
✗ Branch 48 → 77 not taken.
6 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
67 6 }
68
69 8 return false; // Violated
70 }
71
72 47 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
73
1/2
✓ Branch 2 → 3 taken 47 times.
✗ Branch 2 → 42 not taken.
47 Scope *matchScope = thisType.getBodyScope();
74
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 47 times.
47 assert(matchScope != nullptr);
75
2/4
✓ Branch 5 → 6 taken 47 times.
✗ Branch 5 → 28 not taken.
✓ Branch 9 → 10 taken 47 times.
✗ Branch 9 → 24 not taken.
94 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 47 times.
✗ Branch 12 → 36 not taken.
✓ Branch 15 → 16 taken 47 times.
✗ Branch 15 → 32 not taken.
✓ Branch 16 → 17 taken 47 times.
✗ Branch 16 → 30 not taken.
188 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType.toNonConst(), args, {}, true, node);
78 47 }
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 978639 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
89 // Skip all types, except structs
90
3/4
✓ Branch 2 → 3 taken 978639 times.
✗ Branch 2 → 62 not taken.
✓ Branch 3 → 4 taken 841278 times.
✓ Branch 3 → 5 taken 137361 times.
978639 if (!symbolType.isBase(TY_STRUCT))
91 841278 return symbolType;
92
93 // If the target scope is in the current source file, we can return the symbol type as is
94 137361 SourceFile *targetSourceFile = targetScope->sourceFile;
95
2/2
✓ Branch 5 → 6 taken 27372 times.
✓ Branch 5 → 7 taken 109989 times.
137361 if (targetSourceFile == sourceFile)
96 27372 return symbolType;
97
98 // Match the scope of the symbol type against all scopes in the name registry of the target file
99
5/8
✓ Branch 7 → 8 taken 109989 times.
✗ Branch 7 → 58 not taken.
✓ Branch 8 → 9 taken 109989 times.
✗ Branch 8 → 58 not taken.
✓ Branch 9 → 10 taken 109989 times.
✗ Branch 9 → 58 not taken.
✓ Branch 41 → 11 taken 15362840 times.
✓ Branch 41 → 42 taken 9856 times.
15372696 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
100
7/10
✓ Branch 12 → 13 taken 15362840 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 15362840 times.
✗ Branch 13 → 58 not taken.
✓ Branch 14 → 15 taken 15362840 times.
✗ Branch 14 → 58 not taken.
✓ Branch 15 → 16 taken 2174223 times.
✓ Branch 15 → 17 taken 13188617 times.
✓ Branch 18 → 19 taken 2174223 times.
✓ Branch 18 → 39 taken 13188617 times.
15362840 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
101
3/4
✓ Branch 19 → 20 taken 2174223 times.
✗ Branch 19 → 57 not taken.
✓ Branch 37 → 22 taken 3034809 times.
✓ Branch 37 → 38 taken 2074090 times.
7283122 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
102
4/6
✓ Branch 24 → 25 taken 3034809 times.
✗ Branch 24 → 56 not taken.
✓ Branch 25 → 26 taken 3034809 times.
✗ Branch 25 → 56 not taken.
✓ Branch 26 → 27 taken 100133 times.
✓ Branch 26 → 28 taken 2934676 times.
3034809 if (manifestation->scope == symbolType.getBase().getBodyScope())
103 100133 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 9856 times.
✗ Branch 42 → 59 not taken.
✓ Branch 43 → 44 taken 9856 times.
✗ Branch 43 → 59 not taken.
✓ Branch 44 → 45 taken 9856 times.
✗ Branch 44 → 59 not taken.
9856 const std::string structName = symbolType.getBase().getSubType();
108
1/2
✓ Branch 45 → 46 taken 9856 times.
✗ Branch 45 → 60 not taken.
9856 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 39 times.
✓ Branch 46 → 48 taken 9817 times.
9856 if (origRegistryEntry == nullptr)
113 39 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 9817 times.
✗ Branch 48 → 60 not taken.
✓ Branch 49 → 50 taken 4048 times.
✓ Branch 49 → 51 taken 5769 times.
9817 if (targetSourceFile->exportedNameRegistry.contains(structName))
119 4048 return symbolType;
120 5769 const uint64_t targetTypeId = origRegistryEntry->typeId;
121 5769 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
122
1/2
✓ Branch 51 → 52 taken 5769 times.
✗ Branch 51 → 60 not taken.
5769 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
123
124 5769 return symbolType;
125 9856 }
126
127 25125 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
128 // Skip all types, except structs
129
3/4
✓ Branch 2 → 3 taken 25125 times.
✗ Branch 2 → 57 not taken.
✓ Branch 3 → 4 taken 2169 times.
✓ Branch 3 → 5 taken 22956 times.
25125 if (!symbolType.isBase(TY_STRUCT))
130 2169 return symbolType;
131
132 // If the given source file is in the current one, we can return the symbol type as is
133 22956 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
134
2/2
✓ Branch 5 → 6 taken 4811 times.
✓ Branch 5 → 7 taken 18145 times.
22956 if (sourceSourceFile == sourceFile)
135 4811 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 18145 times.
✗ Branch 7 → 57 not taken.
18145 const QualType baseType = symbolType.getBase();
139
5/8
✓ Branch 8 → 9 taken 18145 times.
✗ Branch 8 → 56 not taken.
✓ Branch 9 → 10 taken 18145 times.
✗ Branch 9 → 56 not taken.
✓ Branch 10 → 11 taken 18145 times.
✗ Branch 10 → 56 not taken.
✓ Branch 41 → 12 taken 3091851 times.
✓ Branch 41 → 42 taken 218 times.
3092069 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
140
7/10
✓ Branch 13 → 14 taken 3091851 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 3091851 times.
✗ Branch 14 → 56 not taken.
✓ Branch 15 → 16 taken 3091851 times.
✗ Branch 15 → 56 not taken.
✓ Branch 16 → 17 taken 537023 times.
✓ Branch 16 → 18 taken 2554828 times.
✓ Branch 19 → 20 taken 537023 times.
✓ Branch 19 → 39 taken 2554828 times.
3091851 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
141
3/4
✓ Branch 20 → 21 taken 537023 times.
✗ Branch 20 → 55 not taken.
✓ Branch 37 → 23 taken 604955 times.
✓ Branch 37 → 38 taken 519096 times.
1661074 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
142
3/4
✓ Branch 25 → 26 taken 604955 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 17927 times.
✓ Branch 26 → 28 taken 587028 times.
604955 if (manifestation->scope == baseType.getBodyScope())
143 17927 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 218 times.
✗ Branch 42 → 57 not taken.
✓ Branch 43 → 44 taken 218 times.
✗ Branch 43 → 57 not taken.
218 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 218 times.
218 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 218 times.
✗ Branch 46 → 57 not taken.
✓ Branch 47 → 48 taken 218 times.
✗ Branch 47 → 57 not taken.
✓ Branch 48 → 49 taken 16 times.
✓ Branch 48 → 50 taken 202 times.
218 if (sourceFile->exportedNameRegistry.contains(baseType.getSubType()))
157 16 return symbolType;
158 202 const uint64_t typeId = origRegistryEntry->typeId;
159 202 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
160
2/4
✓ Branch 50 → 51 taken 202 times.
✗ Branch 50 → 57 not taken.
✓ Branch 51 → 52 taken 202 times.
✗ Branch 51 → 57 not taken.
202 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
161
162 202 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 6206 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
172
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 6206 times.
6206 assert(node->getOpFctPointers()->size() > manIdx);
173 6206 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 109024 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
182
4/4
✓ Branch 2 → 3 taken 108213 times.
✓ Branch 2 → 5 taken 811 times.
✓ Branch 3 → 4 taken 71631 times.
✓ Branch 3 → 5 taken 36582 times.
109024 if (fct && !fct->alreadyTypeChecked)
183 71631 fct->entry->scope->sourceFile->reVisitRequested = true;
184 109024 }
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 141994 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
192
2/2
✓ Branch 18 → 4 taken 416916 times.
✓ Branch 18 → 19 taken 129418 times.
546334 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
193
8/10
✓ Branch 7 → 8 taken 416916 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 22278 times.
✓ Branch 8 → 12 taken 394638 times.
✓ Branch 9 → 10 taken 22278 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 12576 times.
✓ Branch 10 → 12 taken 9702 times.
✓ Branch 13 → 14 taken 12576 times.
✓ Branch 13 → 16 taken 404340 times.
416916 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
194
1/2
✓ Branch 14 → 15 taken 12576 times.
✗ Branch 14 → 20 not taken.
12576 sourceFile->requestRuntimeModule(runtimeModule);
195 12576 break;
196 }
197 }
198 141994 }
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 34571 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
206
2/2
✓ Branch 18 → 4 taken 454267 times.
✓ Branch 18 → 19 taken 30542 times.
484809 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
207
8/10
✓ Branch 7 → 8 taken 454267 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 4101 times.
✓ Branch 8 → 12 taken 450166 times.
✓ Branch 9 → 10 taken 4101 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 4029 times.
✓ Branch 10 → 12 taken 72 times.
✓ Branch 13 → 14 taken 4029 times.
✓ Branch 13 → 16 taken 450238 times.
454267 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
208
1/2
✓ Branch 14 → 15 taken 4029 times.
✗ Branch 14 → 20 not taken.
4029 sourceFile->requestRuntimeModule(runtimeModule);
209 4029 break;
210 }
211 }
212 34571 }
213
214 /**
215 * Add a soft error to the error list
216 */
217 25 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
218 25 resourceManager.errorManager.addSoftError(node, errorType, message);
219 25 }
220
221 35 bool TypeChecker::isCopyCtorCall(const FctCallNode *node, const QualType &thisType) const {
222
1/2
✓ Branch 2 → 3 taken 35 times.
✗ Branch 2 → 14 not taken.
35 const FctCallNode::FctCallData &data = node->data.at(manIdx);
223
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 33 times.
35 if (data.args.size() != 2)
224 2 return false;
225
2/4
✓ Branch 7 → 8 taken 33 times.
✗ Branch 7 → 13 not taken.
✓ Branch 8 → 9 taken 33 times.
✗ Branch 8 → 13 not taken.
33 const QualType &secondArgType = data.args.back().first.removeReferenceWrapper().toNonConst();
226
1/2
✓ Branch 9 → 10 taken 33 times.
✗ Branch 9 → 14 not taken.
33 return thisType.matches(secondArgType, false, false, true);
227 }
228
229 } // namespace spice::compiler
230