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 8499 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 8499 times.
✗ Branch 4 → 7 not taken.
8499 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 8499 }
17
18 5880 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 5880 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 5880 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 2382 times.
✓ Branch 2 → 4 taken 3498 times.
5880 if (isPrepare)
25 2382 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 5834 times.
✓ Branch 4 → 32 taken 46 times.
5880 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 2368 times.
✓ Branch 6 → 28 taken 3466 times.
5834 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 2368 times.
✗ Branch 7 → 36 not taken.
2368 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 25 → 10 taken 2805 times.
✓ Branch 25 → 26 taken 2368 times.
7541 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 2805 times.
✗ Branch 12 → 33 not taken.
2805 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 13 → 14 taken 2805 times.
✗ Branch 13 → 33 not taken.
2805 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 14 → 15 taken 2805 times.
✗ Branch 14 → 33 not taken.
2805 createDefaultMoveCtorIfRequired(*manifestation, manifestation->scope);
38
1/2
✓ Branch 15 → 16 taken 2805 times.
✗ Branch 15 → 33 not taken.
2805 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
39 }
40 2368 }
41
42
1/2
✓ Branch 28 → 29 taken 5834 times.
✗ Branch 28 → 37 not taken.
11668 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 62 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 56 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 56 times.
✓ Branch 17 → 18 taken 6 times.
✓ Branch 17 → 20 taken 56 times.
✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 22 taken 56 times.
✓ Branch 22 → 23 taken 6 times.
✓ Branch 22 → 25 taken 56 times.
✓ Branch 25 → 26 taken 56 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.
86 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
58 56 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 700601 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
93 // Skip all types, except structs
94
3/4
✓ Branch 2 → 3 taken 700601 times.
✗ Branch 2 → 62 not taken.
✓ Branch 3 → 4 taken 592656 times.
✓ Branch 3 → 5 taken 107945 times.
700601 if (!symbolType.isBase(TY_STRUCT))
95 592656 return symbolType;
96
97 // If the target scope is in the current source file, we can return the symbol type as is
98 107945 SourceFile *targetSourceFile = targetScope->sourceFile;
99
2/2
✓ Branch 5 → 6 taken 22775 times.
✓ Branch 5 → 7 taken 85170 times.
107945 if (targetSourceFile == sourceFile)
100 22775 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 85170 times.
✗ Branch 7 → 58 not taken.
✓ Branch 8 → 9 taken 85170 times.
✗ Branch 8 → 58 not taken.
✓ Branch 9 → 10 taken 85170 times.
✗ Branch 9 → 58 not taken.
✓ Branch 41 → 11 taken 11174425 times.
✓ Branch 41 → 42 taken 5670 times.
11180095 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
104
7/10
✓ Branch 12 → 13 taken 11174425 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 11174425 times.
✗ Branch 13 → 58 not taken.
✓ Branch 14 → 15 taken 11174425 times.
✗ Branch 14 → 58 not taken.
✓ Branch 15 → 16 taken 1129015 times.
✓ Branch 15 → 17 taken 10045410 times.
✓ Branch 18 → 19 taken 1129015 times.
✓ Branch 18 → 39 taken 10045410 times.
11174425 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
105
3/4
✓ Branch 19 → 20 taken 1129015 times.
✗ Branch 19 → 57 not taken.
✓ Branch 37 → 22 taken 1748821 times.
✓ Branch 37 → 38 taken 1049515 times.
3927351 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
106
4/6
✓ Branch 24 → 25 taken 1748821 times.
✗ Branch 24 → 56 not taken.
✓ Branch 25 → 26 taken 1748821 times.
✗ Branch 25 → 56 not taken.
✓ Branch 26 → 27 taken 79500 times.
✓ Branch 26 → 28 taken 1669321 times.
1748821 if (manifestation->scope == symbolType.getBase().getBodyScope())
107 79500 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 5670 times.
✗ Branch 42 → 59 not taken.
✓ Branch 43 → 44 taken 5670 times.
✗ Branch 43 → 59 not taken.
✓ Branch 44 → 45 taken 5670 times.
✗ Branch 44 → 59 not taken.
5670 const std::string structName = symbolType.getBase().getSubType();
112
1/2
✓ Branch 45 → 46 taken 5670 times.
✗ Branch 45 → 60 not taken.
5670 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 5644 times.
5670 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 5644 times.
✗ Branch 48 → 60 not taken.
✓ Branch 49 → 50 taken 747 times.
✓ Branch 49 → 51 taken 4897 times.
5644 if (targetSourceFile->exportedNameRegistry.contains(structName))
123 747 return symbolType;
124 4897 const uint64_t targetTypeId = origRegistryEntry->typeId;
125 4897 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
126
1/2
✓ Branch 51 → 52 taken 4897 times.
✗ Branch 51 → 60 not taken.
4897 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
127
128 4897 return symbolType;
129 5670 }
130
131 22596 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
132 // Skip all types, except structs
133
3/4
✓ Branch 2 → 3 taken 22596 times.
✗ Branch 2 → 57 not taken.
✓ Branch 3 → 4 taken 1722 times.
✓ Branch 3 → 5 taken 20874 times.
22596 if (!symbolType.isBase(TY_STRUCT))
134 1722 return symbolType;
135
136 // If the given source file is in the current one, we can return the symbol type as is
137 20874 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
138
2/2
✓ Branch 5 → 6 taken 4691 times.
✓ Branch 5 → 7 taken 16183 times.
20874 if (sourceSourceFile == sourceFile)
139 4691 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 16183 times.
✗ Branch 7 → 57 not taken.
16183 const QualType baseType = symbolType.getBase();
143
5/8
✓ Branch 8 → 9 taken 16183 times.
✗ Branch 8 → 56 not taken.
✓ Branch 9 → 10 taken 16183 times.
✗ Branch 9 → 56 not taken.
✓ Branch 10 → 11 taken 16183 times.
✗ Branch 10 → 56 not taken.
✓ Branch 41 → 12 taken 3231498 times.
✓ Branch 41 → 42 taken 207 times.
3231705 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
144
7/10
✓ Branch 13 → 14 taken 3231498 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 3231498 times.
✗ Branch 14 → 56 not taken.
✓ Branch 15 → 16 taken 3231498 times.
✗ Branch 15 → 56 not taken.
✓ Branch 16 → 17 taken 476205 times.
✓ Branch 16 → 18 taken 2755293 times.
✓ Branch 19 → 20 taken 476205 times.
✓ Branch 19 → 39 taken 2755293 times.
3231498 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
145
3/4
✓ Branch 20 → 21 taken 476205 times.
✗ Branch 20 → 55 not taken.
✓ Branch 37 → 23 taken 544168 times.
✓ Branch 37 → 38 taken 460229 times.
1480602 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
146
3/4
✓ Branch 25 → 26 taken 544168 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 15976 times.
✓ Branch 26 → 28 taken 528192 times.
544168 if (manifestation->scope == baseType.getBodyScope())
147 15976 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 5510 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
176
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 5510 times.
5510 assert(node->getOpFctPointers()->size() > manIdx);
177 5510 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 88810 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
186
4/4
✓ Branch 2 → 3 taken 88508 times.
✓ Branch 2 → 5 taken 302 times.
✓ Branch 3 → 4 taken 57393 times.
✓ Branch 3 → 5 taken 31115 times.
88810 if (fct && !fct->alreadyTypeChecked)
187 57393 fct->entry->scope->sourceFile->reVisitRequested = true;
188 88810 }
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 114973 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
196
2/2
✓ Branch 18 → 4 taken 338044 times.
✓ Branch 18 → 19 taken 106024 times.
444068 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
197
8/10
✓ Branch 7 → 8 taken 338044 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 18516 times.
✓ Branch 8 → 12 taken 319528 times.
✓ Branch 9 → 10 taken 18516 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 8949 times.
✓ Branch 10 → 12 taken 9567 times.
✓ Branch 13 → 14 taken 8949 times.
✓ Branch 13 → 16 taken 329095 times.
338044 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
198
1/2
✓ Branch 14 → 15 taken 8949 times.
✗ Branch 14 → 20 not taken.
8949 sourceFile->requestRuntimeModule(runtimeModule);
199 8949 break;
200 }
201 }
202 114973 }
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 27453 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
210
2/2
✓ Branch 18 → 4 taken 368304 times.
✓ Branch 18 → 19 taken 25348 times.
393652 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
211
8/10
✓ Branch 7 → 8 taken 368304 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 2145 times.
✓ Branch 8 → 12 taken 366159 times.
✓ Branch 9 → 10 taken 2145 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2105 times.
✓ Branch 10 → 12 taken 40 times.
✓ Branch 13 → 14 taken 2105 times.
✓ Branch 13 → 16 taken 366199 times.
368304 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
212
1/2
✓ Branch 14 → 15 taken 2105 times.
✗ Branch 14 → 20 not taken.
2105 sourceFile->requestRuntimeModule(runtimeModule);
213 2105 break;
214 }
215 }
216 27453 }
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