GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.0% 106 / 0 / 114
Functions: 92.3% 12 / 0 / 13
Branches: 62.9% 151 / 0 / 240

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 8471 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 8471 times.
✗ Branch 4 → 7 not taken.
8471 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 8471 }
17
18 5855 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 5855 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 5855 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 2372 times.
✓ Branch 2 → 4 taken 3483 times.
5855 if (isPrepare)
25 2372 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 5808 times.
✓ Branch 4 → 32 taken 47 times.
5855 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 2358 times.
✓ Branch 6 → 28 taken 3450 times.
5808 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 2358 times.
✗ Branch 7 → 36 not taken.
2358 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 25 → 10 taken 2800 times.
✓ Branch 25 → 26 taken 2358 times.
7516 for (const Struct *manifestation : manifestations) {
34 // Check if we need to create a default ctor, copy ctor, move ctor or dtor
35
1/2
✓ Branch 12 → 13 taken 2800 times.
✗ Branch 12 → 33 not taken.
2800 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 13 → 14 taken 2800 times.
✗ Branch 13 → 33 not taken.
2800 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 14 → 15 taken 2800 times.
✗ Branch 14 → 33 not taken.
2800 createDefaultMoveCtorIfRequired(*manifestation, manifestation->scope);
38
1/2
✓ Branch 15 → 16 taken 2800 times.
✗ Branch 15 → 33 not taken.
2800 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
39 }
40 2358 }
41
42
1/2
✓ Branch 28 → 29 taken 5808 times.
✗ Branch 28 → 37 not taken.
11616 return nullptr;
43 }
44
45 /**
46 * Check if the capture rules for async lambdas are enforced if the async attribute is set
47 *
48 * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the
49 * fat pointer and can ensure, that no stack variable is referenced inside the lambda.
50 *
51 * @param node Lambda base node
52 * @param attrs Lambda attributes
53 * @return False if the rules are violated, true otherwise
54 */
55 61 bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const {
56 // If the async attribute is not set, we can return early
57
18/32
✓ Branch 2 → 3 taken 6 times.
✓ Branch 2 → 13 taken 55 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 55 times.
✓ Branch 17 → 18 taken 6 times.
✓ Branch 17 → 20 taken 55 times.
✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 22 taken 55 times.
✓ Branch 22 → 23 taken 6 times.
✓ Branch 22 → 25 taken 55 times.
✓ Branch 25 → 26 taken 55 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.
85 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
58 55 return true; // Not violated
59
60 // If we don't have any captures, we can return early
61 6 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
62
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 6 times.
6 if (captures.empty())
63 return true; // Not violated
64
65 // Check for the capture rules
66 6 if (const Capture &capture = captures.begin()->second;
67
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) {
68 5 const auto warningMessage =
69 "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead "
70 "to bugs due to references, outliving the validity scope of the referenced variable.";
71
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);
72
1/2
✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 77 not taken.
5 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
73 5 }
74
75 6 return false; // Violated
76 }
77
78 70 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
79
1/2
✓ Branch 2 → 3 taken 70 times.
✗ Branch 2 → 40 not taken.
70 Scope *matchScope = thisType.getBodyScope();
80
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 70 times.
70 assert(matchScope != nullptr);
81
2/4
✓ Branch 5 → 6 taken 70 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 70 times.
✗ Branch 9 → 23 not taken.
140 const ArgList args = {{thisType.toConstRef(node), false}};
82
2/4
✓ Branch 14 → 15 taken 70 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 70 times.
✗ Branch 15 → 29 not taken.
280 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
83 70 }
84
85 Function *TypeChecker::matchMoveCtor(const QualType &thisType, const ASTNode *node) const {
86 Scope *matchScope = thisType.getBodyScope();
87 assert(matchScope != nullptr);
88 const ArgList args = {{thisType.toNonConst().toRef(node), false}};
89 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
90 }
91
92 689683 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
93 // Skip all types, except structs
94
3/4
✓ Branch 2 → 3 taken 689683 times.
✗ Branch 2 → 62 not taken.
✓ Branch 3 → 4 taken 587939 times.
✓ Branch 3 → 5 taken 101744 times.
689683 if (!symbolType.isBase(TY_STRUCT))
95 587939 return symbolType;
96
97 // If the target scope is in the current source file, we can return the symbol type as is
98 101744 SourceFile *targetSourceFile = targetScope->sourceFile;
99
2/2
✓ Branch 5 → 6 taken 22599 times.
✓ Branch 5 → 7 taken 79145 times.
101744 if (targetSourceFile == sourceFile)
100 22599 return symbolType;
101
102 // Match the scope of the symbol type against all scopes in the name registry of the target file
103
5/8
✓ Branch 7 → 8 taken 79145 times.
✗ Branch 7 → 58 not taken.
✓ Branch 8 → 9 taken 79145 times.
✗ Branch 8 → 58 not taken.
✓ Branch 9 → 10 taken 79145 times.
✗ Branch 9 → 58 not taken.
✓ Branch 41 → 11 taken 11022675 times.
✓ Branch 41 → 42 taken 5588 times.
11028263 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
104
7/10
✓ Branch 12 → 13 taken 11022675 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 11022675 times.
✗ Branch 13 → 58 not taken.
✓ Branch 14 → 15 taken 11022675 times.
✗ Branch 14 → 58 not taken.
✓ Branch 15 → 16 taken 1095933 times.
✓ Branch 15 → 17 taken 9926742 times.
✓ Branch 18 → 19 taken 1095933 times.
✓ Branch 18 → 39 taken 9926742 times.
11022675 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
105
3/4
✓ Branch 19 → 20 taken 1095933 times.
✗ Branch 19 → 57 not taken.
✓ Branch 37 → 22 taken 1714260 times.
✓ Branch 37 → 38 taken 1022376 times.
3832569 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
106
4/6
✓ Branch 24 → 25 taken 1714260 times.
✗ Branch 24 → 56 not taken.
✓ Branch 25 → 26 taken 1714260 times.
✗ Branch 25 → 56 not taken.
✓ Branch 26 → 27 taken 73557 times.
✓ Branch 26 → 28 taken 1640703 times.
1714260 if (manifestation->scope == symbolType.getBase().getBodyScope())
107 73557 return symbolType;
108
109 // The target file does not know about the struct at all
110 // -> show it how to find the struct
111
3/6
✓ Branch 42 → 43 taken 5588 times.
✗ Branch 42 → 59 not taken.
✓ Branch 43 → 44 taken 5588 times.
✗ Branch 43 → 59 not taken.
✓ Branch 44 → 45 taken 5588 times.
✗ Branch 44 → 59 not taken.
5588 const std::string structName = symbolType.getBase().getSubType();
112
1/2
✓ Branch 45 → 46 taken 5588 times.
✗ Branch 45 → 60 not taken.
5588 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
113 // If even this file does not know the struct by its unqualified name (deep transitive import), there is
114 // nothing to copy over. Skip teaching the target file; the type identity itself is unaffected, and member
115 // access falls back to the resolved body scope.
116
2/2
✓ Branch 46 → 47 taken 26 times.
✓ Branch 46 → 48 taken 5562 times.
5588 if (origRegistryEntry == nullptr)
117 26 return symbolType;
118 // Do not clobber an entry the target file already has under this name (e.g. its OWN same-named struct, like
119 // llvm's `Function` vs the model's `Function`). Teaching uses keepNewOnCollision=false, which would otherwise
120 // ERASE the target's existing entry and break resolution of its own type. The struct's QualType identity is
121 // carried by pointer regardless, so a name that is already taken does not need (re-)teaching here.
122
3/4
✓ Branch 48 → 49 taken 5562 times.
✗ Branch 48 → 60 not taken.
✓ Branch 49 → 50 taken 743 times.
✓ Branch 49 → 51 taken 4819 times.
5562 if (targetSourceFile->exportedNameRegistry.contains(structName))
123 743 return symbolType;
124 4819 const uint64_t targetTypeId = origRegistryEntry->typeId;
125 4819 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
126
1/2
✓ Branch 51 → 52 taken 4819 times.
✗ Branch 51 → 60 not taken.
4819 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
127
128 4819 return symbolType;
129 5588 }
130
131 20989 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
132 // Skip all types, except structs
133
3/4
✓ Branch 2 → 3 taken 20989 times.
✗ Branch 2 → 57 not taken.
✓ Branch 3 → 4 taken 1708 times.
✓ Branch 3 → 5 taken 19281 times.
20989 if (!symbolType.isBase(TY_STRUCT))
134 1708 return symbolType;
135
136 // If the given source file is in the current one, we can return the symbol type as is
137 19281 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
138
2/2
✓ Branch 5 → 6 taken 4674 times.
✓ Branch 5 → 7 taken 14607 times.
19281 if (sourceSourceFile == sourceFile)
139 4674 return symbolType;
140
141 // Match the scope of the symbol type against all scopes in the name registry of this source file
142
1/2
✓ Branch 7 → 8 taken 14607 times.
✗ Branch 7 → 57 not taken.
14607 const QualType baseType = symbolType.getBase();
143
5/8
✓ Branch 8 → 9 taken 14607 times.
✗ Branch 8 → 56 not taken.
✓ Branch 9 → 10 taken 14607 times.
✗ Branch 9 → 56 not taken.
✓ Branch 10 → 11 taken 14607 times.
✗ Branch 10 → 56 not taken.
✓ Branch 41 → 12 taken 2613748 times.
✓ Branch 41 → 42 taken 207 times.
2613955 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
144
7/10
✓ Branch 13 → 14 taken 2613748 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 2613748 times.
✗ Branch 14 → 56 not taken.
✓ Branch 15 → 16 taken 2613748 times.
✗ Branch 15 → 56 not taken.
✓ Branch 16 → 17 taken 338530 times.
✓ Branch 16 → 18 taken 2275218 times.
✓ Branch 19 → 20 taken 338530 times.
✓ Branch 19 → 39 taken 2275218 times.
2613748 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
145
3/4
✓ Branch 20 → 21 taken 338530 times.
✗ Branch 20 → 55 not taken.
✓ Branch 37 → 23 taken 403742 times.
✓ Branch 37 → 38 taken 324130 times.
1066402 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
146
3/4
✓ Branch 25 → 26 taken 403742 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 14400 times.
✓ Branch 26 → 28 taken 389342 times.
403742 if (manifestation->scope == baseType.getBodyScope())
147 14400 return symbolType;
148
149 // This source file does not know about the struct at all
150 // -> show it how to find the struct
151
2/4
✓ Branch 42 → 43 taken 207 times.
✗ Branch 42 → 57 not taken.
✓ Branch 43 → 44 taken 207 times.
✗ Branch 43 → 57 not taken.
207 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
152 // If even the source file does not know the struct by its unqualified name (deep transitive import), there is
153 // nothing to copy over. Skip teaching this file; the type identity itself is unaffected, and member access
154 // falls back to the resolved body scope.
155
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 207 times.
207 if (origRegistryEntry == nullptr)
156 return symbolType;
157 // Do not clobber an entry this file already has under this name (see mapLocalTypeToImportedScopeType): teaching
158 // with keepNewOnCollision=false would ERASE this file's own same-named struct entry. The QualType identity is
159 // carried by pointer, so an already-taken name needs no (re-)teaching here.
160
4/6
✓ Branch 46 → 47 taken 207 times.
✗ Branch 46 → 57 not taken.
✓ Branch 47 → 48 taken 207 times.
✗ Branch 47 → 57 not taken.
✓ Branch 48 → 49 taken 16 times.
✓ Branch 48 → 50 taken 191 times.
207 if (sourceFile->exportedNameRegistry.contains(baseType.getSubType()))
161 16 return symbolType;
162 191 const uint64_t typeId = origRegistryEntry->typeId;
163 191 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
164
2/4
✓ Branch 50 → 51 taken 191 times.
✗ Branch 50 → 57 not taken.
✓ Branch 51 → 52 taken 191 times.
✗ Branch 51 → 57 not taken.
191 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
165
166 191 return symbolType;
167 }
168
169 /**
170 * Returns the operator function list for the current manifestation and the given node
171 *
172 * @param node Node to retrieve the op fct pointer list from
173 * @return Op fct pointer list
174 */
175 4471 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
176
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 4471 times.
4471 assert(node->getOpFctPointers()->size() > manIdx);
177 4471 return node->getOpFctPointers()->at(manIdx);
178 }
179
180 /**
181 * Check if a function has been type-checked already. If not, request a revisit
182 *
183 * @param fct Function to check
184 */
185 87080 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
186
4/4
✓ Branch 2 → 3 taken 86777 times.
✓ Branch 2 → 5 taken 303 times.
✓ Branch 3 → 4 taken 57014 times.
✓ Branch 3 → 5 taken 29763 times.
87080 if (fct && !fct->alreadyTypeChecked)
187 57014 fct->entry->scope->sourceFile->reVisitRequested = true;
188 87080 }
189
190 /**
191 * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module.
192 *
193 * @param typeName Given type name
194 */
195 113528 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
196
2/2
✓ Branch 18 → 4 taken 333684 times.
✓ Branch 18 → 19 taken 105066 times.
438750 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
197
8/10
✓ Branch 7 → 8 taken 333684 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 18025 times.
✓ Branch 8 → 12 taken 315659 times.
✓ Branch 9 → 10 taken 18025 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 8462 times.
✓ Branch 10 → 12 taken 9563 times.
✓ Branch 13 → 14 taken 8462 times.
✓ Branch 13 → 16 taken 325222 times.
333684 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
198
1/2
✓ Branch 14 → 15 taken 8462 times.
✗ Branch 14 → 20 not taken.
8462 sourceFile->requestRuntimeModule(runtimeModule);
199 8462 break;
200 }
201 }
202 113528 }
203
204 /**
205 * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module.
206 *
207 * @param functionName Given function name
208 */
209 27213 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
210
2/2
✓ Branch 18 → 4 taken 364856 times.
✓ Branch 18 → 19 taken 25099 times.
389955 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
211
8/10
✓ Branch 7 → 8 taken 364856 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 2154 times.
✓ Branch 8 → 12 taken 362702 times.
✓ Branch 9 → 10 taken 2154 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2114 times.
✓ Branch 10 → 12 taken 40 times.
✓ Branch 13 → 14 taken 2114 times.
✓ Branch 13 → 16 taken 362742 times.
364856 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
212
1/2
✓ Branch 14 → 15 taken 2114 times.
✗ Branch 14 → 20 not taken.
2114 sourceFile->requestRuntimeModule(runtimeModule);
213 2114 break;
214 }
215 }
216 27213 }
217
218 /**
219 * Add a soft error to the error list
220 */
221 25 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
222 25 resourceManager.errorManager.addSoftError(node, errorType, message);
223 25 }
224
225 14 bool TypeChecker::isCopyCtorCall(const FctCallNode *node, const QualType &thisType) const {
226
1/2
✓ Branch 2 → 3 taken 14 times.
✗ Branch 2 → 14 not taken.
14 const FctCallNode::FctCallData &data = node->data.at(manIdx);
227
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 12 times.
14 if (data.args.size() != 2)
228 2 return false;
229
2/4
✓ Branch 7 → 8 taken 12 times.
✗ Branch 7 → 13 not taken.
✓ Branch 8 → 9 taken 12 times.
✗ Branch 8 → 13 not taken.
12 const QualType &secondArgType = data.args.back().first.removeReferenceWrapper().toNonConst();
230
1/2
✓ Branch 9 → 10 taken 12 times.
✗ Branch 9 → 14 not taken.
12 return thisType.matches(secondArgType, false, false, true);
231 }
232
233 } // namespace spice::compiler
234