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 8029 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 8029 times.
✗ Branch 4 → 7 not taken.
8029 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 8029 }
17
18 5532 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 5532 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 5532 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 2245 times.
✓ Branch 2 → 4 taken 3287 times.
5532 if (isPrepare)
25 2245 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 5486 times.
✓ Branch 4 → 32 taken 46 times.
5532 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 2231 times.
✓ Branch 6 → 28 taken 3255 times.
5486 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 2231 times.
✗ Branch 7 → 36 not taken.
2231 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 25 → 10 taken 2640 times.
✓ Branch 25 → 26 taken 2231 times.
7102 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 2640 times.
✗ Branch 12 → 33 not taken.
2640 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 13 → 14 taken 2640 times.
✗ Branch 13 → 33 not taken.
2640 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 14 → 15 taken 2640 times.
✗ Branch 14 → 33 not taken.
2640 createDefaultMoveCtorIfRequired(*manifestation, manifestation->scope);
38
1/2
✓ Branch 15 → 16 taken 2640 times.
✗ Branch 15 → 33 not taken.
2640 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
39 }
40 2231 }
41
42
1/2
✓ Branch 28 → 29 taken 5486 times.
✗ Branch 28 → 37 not taken.
10972 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 58 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 52 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 52 times.
✓ Branch 17 → 18 taken 6 times.
✓ Branch 17 → 20 taken 52 times.
✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 22 taken 52 times.
✓ Branch 22 → 23 taken 6 times.
✓ Branch 22 → 25 taken 52 times.
✓ Branch 25 → 26 taken 52 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.
82 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
58 52 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 637088 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
93 // Skip all types, except structs
94
3/4
✓ Branch 2 → 3 taken 637088 times.
✗ Branch 2 → 62 not taken.
✓ Branch 3 → 4 taken 544531 times.
✓ Branch 3 → 5 taken 92557 times.
637088 if (!symbolType.isBase(TY_STRUCT))
95 544531 return symbolType;
96
97 // If the target scope is in the current source file, we can return the symbol type as is
98 92557 SourceFile *targetSourceFile = targetScope->sourceFile;
99
2/2
✓ Branch 5 → 6 taken 21409 times.
✓ Branch 5 → 7 taken 71148 times.
92557 if (targetSourceFile == sourceFile)
100 21409 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 71148 times.
✗ Branch 7 → 58 not taken.
✓ Branch 8 → 9 taken 71148 times.
✗ Branch 8 → 58 not taken.
✓ Branch 9 → 10 taken 71148 times.
✗ Branch 9 → 58 not taken.
✓ Branch 41 → 11 taken 9476674 times.
✓ Branch 41 → 42 taken 5319 times.
9481993 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
104
7/10
✓ Branch 12 → 13 taken 9476674 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 9476674 times.
✗ Branch 13 → 58 not taken.
✓ Branch 14 → 15 taken 9476674 times.
✗ Branch 14 → 58 not taken.
✓ Branch 15 → 16 taken 989327 times.
✓ Branch 15 → 17 taken 8487347 times.
✓ Branch 18 → 19 taken 989327 times.
✓ Branch 18 → 39 taken 8487347 times.
9476674 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
105
3/4
✓ Branch 19 → 20 taken 989327 times.
✗ Branch 19 → 57 not taken.
✓ Branch 37 → 22 taken 1532456 times.
✓ Branch 37 → 38 taken 923498 times.
3445281 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
106
4/6
✓ Branch 24 → 25 taken 1532456 times.
✗ Branch 24 → 56 not taken.
✓ Branch 25 → 26 taken 1532456 times.
✗ Branch 25 → 56 not taken.
✓ Branch 26 → 27 taken 65829 times.
✓ Branch 26 → 28 taken 1466627 times.
1532456 if (manifestation->scope == symbolType.getBase().getBodyScope())
107 65829 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 5319 times.
✗ Branch 42 → 59 not taken.
✓ Branch 43 → 44 taken 5319 times.
✗ Branch 43 → 59 not taken.
✓ Branch 44 → 45 taken 5319 times.
✗ Branch 44 → 59 not taken.
5319 const std::string structName = symbolType.getBase().getSubType();
112
1/2
✓ Branch 45 → 46 taken 5319 times.
✗ Branch 45 → 60 not taken.
5319 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 23 times.
✓ Branch 46 → 48 taken 5296 times.
5319 if (origRegistryEntry == nullptr)
117 23 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 5296 times.
✗ Branch 48 → 60 not taken.
✓ Branch 49 → 50 taken 742 times.
✓ Branch 49 → 51 taken 4554 times.
5296 if (targetSourceFile->exportedNameRegistry.contains(structName))
123 742 return symbolType;
124 4554 const uint64_t targetTypeId = origRegistryEntry->typeId;
125 4554 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
126
1/2
✓ Branch 51 → 52 taken 4554 times.
✗ Branch 51 → 60 not taken.
4554 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
127
128 4554 return symbolType;
129 5319 }
130
131 19232 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
132 // Skip all types, except structs
133
3/4
✓ Branch 2 → 3 taken 19232 times.
✗ Branch 2 → 57 not taken.
✓ Branch 3 → 4 taken 1579 times.
✓ Branch 3 → 5 taken 17653 times.
19232 if (!symbolType.isBase(TY_STRUCT))
134 1579 return symbolType;
135
136 // If the given source file is in the current one, we can return the symbol type as is
137 17653 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
138
2/2
✓ Branch 5 → 6 taken 4477 times.
✓ Branch 5 → 7 taken 13176 times.
17653 if (sourceSourceFile == sourceFile)
139 4477 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 13176 times.
✗ Branch 7 → 57 not taken.
13176 const QualType baseType = symbolType.getBase();
143
5/8
✓ Branch 8 → 9 taken 13176 times.
✗ Branch 8 → 56 not taken.
✓ Branch 9 → 10 taken 13176 times.
✗ Branch 9 → 56 not taken.
✓ Branch 10 → 11 taken 13176 times.
✗ Branch 10 → 56 not taken.
✓ Branch 41 → 12 taken 2098867 times.
✓ Branch 41 → 42 taken 186 times.
2099053 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
144
7/10
✓ Branch 13 → 14 taken 2098867 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 2098867 times.
✗ Branch 14 → 56 not taken.
✓ Branch 15 → 16 taken 2098867 times.
✗ Branch 15 → 56 not taken.
✓ Branch 16 → 17 taken 252402 times.
✓ Branch 16 → 18 taken 1846465 times.
✓ Branch 19 → 20 taken 252402 times.
✓ Branch 19 → 39 taken 1846465 times.
2098867 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
145
3/4
✓ Branch 20 → 21 taken 252402 times.
✗ Branch 20 → 55 not taken.
✓ Branch 37 → 23 taken 311860 times.
✓ Branch 37 → 38 taken 239412 times.
803674 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
146
3/4
✓ Branch 25 → 26 taken 311860 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 12990 times.
✓ Branch 26 → 28 taken 298870 times.
311860 if (manifestation->scope == baseType.getBodyScope())
147 12990 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 186 times.
✗ Branch 42 → 57 not taken.
✓ Branch 43 → 44 taken 186 times.
✗ Branch 43 → 57 not taken.
186 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 186 times.
186 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 186 times.
✗ Branch 46 → 57 not taken.
✓ Branch 47 → 48 taken 186 times.
✗ Branch 47 → 57 not taken.
✓ Branch 48 → 49 taken 16 times.
✓ Branch 48 → 50 taken 170 times.
186 if (sourceFile->exportedNameRegistry.contains(baseType.getSubType()))
161 16 return symbolType;
162 170 const uint64_t typeId = origRegistryEntry->typeId;
163 170 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
164
2/4
✓ Branch 50 → 51 taken 170 times.
✗ Branch 50 → 57 not taken.
✓ Branch 51 → 52 taken 170 times.
✗ Branch 51 → 57 not taken.
170 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
165
166 170 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 4283 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
176
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 4283 times.
4283 assert(node->getOpFctPointers()->size() > manIdx);
177 4283 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 82181 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
186
4/4
✓ Branch 2 → 3 taken 81887 times.
✓ Branch 2 → 5 taken 294 times.
✓ Branch 3 → 4 taken 53284 times.
✓ Branch 3 → 5 taken 28603 times.
82181 if (fct && !fct->alreadyTypeChecked)
187 53284 fct->entry->scope->sourceFile->reVisitRequested = true;
188 82181 }
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 104136 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
196
2/2
✓ Branch 18 → 4 taken 305775 times.
✓ Branch 18 → 19 taken 96065 times.
401840 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
197
8/10
✓ Branch 7 → 8 taken 305775 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 17199 times.
✓ Branch 8 → 12 taken 288576 times.
✓ Branch 9 → 10 taken 17199 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 8071 times.
✓ Branch 10 → 12 taken 9128 times.
✓ Branch 13 → 14 taken 8071 times.
✓ Branch 13 → 16 taken 297704 times.
305775 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
198
1/2
✓ Branch 14 → 15 taken 8071 times.
✗ Branch 14 → 20 not taken.
8071 sourceFile->requestRuntimeModule(runtimeModule);
199 8071 break;
200 }
201 }
202 104136 }
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 25085 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
210
2/2
✓ Branch 18 → 4 taken 336832 times.
✓ Branch 18 → 19 taken 23178 times.
360010 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
211
8/10
✓ Branch 7 → 8 taken 336832 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 1947 times.
✓ Branch 8 → 12 taken 334885 times.
✓ Branch 9 → 10 taken 1947 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 1907 times.
✓ Branch 10 → 12 taken 40 times.
✓ Branch 13 → 14 taken 1907 times.
✓ Branch 13 → 16 taken 334925 times.
336832 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
212
1/2
✓ Branch 14 → 15 taken 1907 times.
✗ Branch 14 → 20 not taken.
1907 sourceFile->requestRuntimeModule(runtimeModule);
213 1907 break;
214 }
215 }
216 25085 }
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