GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.9% 94 / 0 / 95
Functions: 100.0% 11 / 0 / 11
Branches: 65.7% 134 / 0 / 204

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 3659 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
15
1/2
✓ Branch 4 → 5 taken 3659 times.
✗ Branch 4 → 7 not taken.
3659 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
16 3659 }
17
18 2899 std::any TypeChecker::visitEntry(EntryNode *node) {
19 // Initialize
20 2899 currentScope = rootScope;
21
22 // Initialize AST nodes with size of 1
23 2899 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
24
2/2
✓ Branch 2 → 3 taken 1217 times.
✓ Branch 2 → 4 taken 1682 times.
2899 if (isPrepare)
25 1217 node->resizeToNumberOfManifestations(1);
26
27 // Visit children
28
2/2
✓ Branch 4 → 5 taken 2853 times.
✓ Branch 4 → 22 taken 46 times.
2899 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 1203 times.
✓ Branch 6 → 19 taken 1650 times.
2853 if (isPrepare) {
32
1/2
✓ Branch 7 → 8 taken 1203 times.
✗ Branch 7 → 26 not taken.
1203 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
33
2/2
✓ Branch 16 → 10 taken 748 times.
✓ Branch 16 → 17 taken 1203 times.
1951 for (const Struct *manifestation : manifestations) {
34 // Check if we need to create a default ctor, copy ctor or dtor
35
1/2
✓ Branch 11 → 12 taken 748 times.
✗ Branch 11 → 23 not taken.
748 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
36
1/2
✓ Branch 12 → 13 taken 748 times.
✗ Branch 12 → 23 not taken.
748 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
37
1/2
✓ Branch 13 → 14 taken 748 times.
✗ Branch 13 → 23 not taken.
748 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
38 }
39 1203 }
40
41
1/2
✓ Branch 19 → 20 taken 2853 times.
✗ Branch 19 → 27 not taken.
2853 return nullptr;
42 }
43
44 /**
45 * Check if the capture rules for async lambdas are enforced if the async attribute is set
46 *
47 * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the
48 * fat pointer and can ensure, that no stack variable is referenced inside the lambda.
49 *
50 * @param node Lambda base node
51 * @param attrs Lambda attributes
52 * @return False if the rules are violated, true otherwise
53 */
54 34 bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const {
55 // If the async attribute is not set, we can return early
56
18/32
✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 13 taken 29 times.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 5 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 5 times.
✓ Branch 15 → 16 taken 5 times.
✓ Branch 15 → 17 taken 29 times.
✓ Branch 17 → 18 taken 5 times.
✓ Branch 17 → 20 taken 29 times.
✓ Branch 20 → 21 taken 5 times.
✓ Branch 20 → 22 taken 29 times.
✓ Branch 22 → 23 taken 5 times.
✓ Branch 22 → 25 taken 29 times.
✓ Branch 25 → 26 taken 29 times.
✓ Branch 25 → 27 taken 5 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.
54 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
57 29 return true; // Not violated
58
59 // If we don't have any captures, we can return early
60 5 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
61
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 5 times.
5 if (captures.empty())
62 return true; // Not violated
63
64 // Check for the capture rules
65 5 if (const Capture &capture = captures.begin()->second;
66
6/8
✓ Branch 33 → 34 taken 3 times.
✓ Branch 33 → 39 taken 2 times.
✓ Branch 36 → 37 taken 1 time.
✓ Branch 36 → 39 taken 2 times.
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 40 not taken.
✓ Branch 41 → 42 taken 5 times.
✗ Branch 41 → 51 not taken.
5 captures.size() > 1 || !capture.capturedSymbol->getQualType().isPtr() || capture.getMode() != BY_VALUE) {
67 5 const auto warningMessage =
68 "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead "
69 "to bugs due to references, outliving the validity scope of the referenced variable.";
70
2/4
✓ Branch 44 → 45 taken 5 times.
✗ Branch 44 → 73 not taken.
✓ Branch 45 → 46 taken 5 times.
✗ Branch 45 → 71 not taken.
5 const CompilerWarning warning(node->codeLoc, ASYNC_LAMBDA_CAPTURE_RULE_VIOLATION, warningMessage);
71
1/2
✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 77 not taken.
5 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
72 5 }
73
74 5 return false; // Violated
75 }
76
77 26 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
78
1/2
✓ Branch 2 → 3 taken 26 times.
✗ Branch 2 → 40 not taken.
26 Scope *matchScope = thisType.getBodyScope();
79
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 26 times.
26 assert(matchScope != nullptr);
80
2/4
✓ Branch 5 → 6 taken 26 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 26 times.
✗ Branch 9 → 23 not taken.
52 const ArgList args = {{thisType.toConstRef(node), false}};
81
2/4
✓ Branch 14 → 15 taken 26 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 26 times.
✗ Branch 15 → 29 not taken.
104 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
82 26 }
83
84 150788 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
85 // Skip all types, except structs
86
3/4
✓ Branch 2 → 3 taken 150788 times.
✗ Branch 2 → 50 not taken.
✓ Branch 3 → 4 taken 137476 times.
✓ Branch 3 → 5 taken 13312 times.
150788 if (!symbolType.isBase(TY_STRUCT))
87 137476 return symbolType;
88
89 // If the target scope is in the current source file, we can return the symbol type as is
90 13312 SourceFile *targetSourceFile = targetScope->sourceFile;
91
2/2
✓ Branch 5 → 6 taken 6068 times.
✓ Branch 5 → 7 taken 7244 times.
13312 if (targetSourceFile == sourceFile)
92 6068 return symbolType;
93
94 // Match the scope of the symbol type against all scopes in the name registry of the target file
95
5/8
✓ Branch 7 → 8 taken 7244 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 7244 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 7244 times.
✗ Branch 9 → 46 not taken.
✓ Branch 33 → 11 taken 162837 times.
✓ Branch 33 → 34 taken 321 times.
163158 for (const NameRegistryEntry &entry : targetSourceFile->exportedNameRegistry | std::views::values)
96
7/10
✓ Branch 12 → 13 taken 162837 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 162837 times.
✗ Branch 13 → 46 not taken.
✓ Branch 14 → 15 taken 162837 times.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 18589 times.
✓ Branch 15 → 17 taken 144248 times.
✓ Branch 18 → 19 taken 18589 times.
✓ Branch 18 → 31 taken 144248 times.
162837 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
97
3/4
✓ Branch 19 → 20 taken 18589 times.
✗ Branch 19 → 45 not taken.
✓ Branch 29 → 22 taken 35212 times.
✓ Branch 29 → 30 taken 11666 times.
46878 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
98
4/6
✓ Branch 23 → 24 taken 35212 times.
✗ Branch 23 → 44 not taken.
✓ Branch 24 → 25 taken 35212 times.
✗ Branch 24 → 44 not taken.
✓ Branch 25 → 26 taken 6923 times.
✓ Branch 25 → 27 taken 28289 times.
35212 if (manifestation->scope == symbolType.getBase().getBodyScope())
99 6923 return symbolType;
100
101 // The target file does not know about the struct at all
102 // -> show it how to find the struct
103
3/6
✓ Branch 34 → 35 taken 321 times.
✗ Branch 34 → 47 not taken.
✓ Branch 35 → 36 taken 321 times.
✗ Branch 35 → 47 not taken.
✓ Branch 36 → 37 taken 321 times.
✗ Branch 36 → 47 not taken.
321 const std::string structName = symbolType.getBase().getSubType();
104
1/2
✓ Branch 37 → 38 taken 321 times.
✗ Branch 37 → 48 not taken.
321 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
105
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 321 times.
321 assert(origRegistryEntry != nullptr);
106 321 const uint64_t targetTypeId = origRegistryEntry->typeId;
107 321 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
108
1/2
✓ Branch 40 → 41 taken 321 times.
✗ Branch 40 → 48 not taken.
321 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
109
110 321 return symbolType;
111 321 }
112
113 4719 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
114 // Skip all types, except structs
115
3/4
✓ Branch 2 → 3 taken 4719 times.
✗ Branch 2 → 45 not taken.
✓ Branch 3 → 4 taken 590 times.
✓ Branch 3 → 5 taken 4129 times.
4719 if (!symbolType.isBase(TY_STRUCT))
116 590 return symbolType;
117
118 // If the given source file is in the current one, we can return the symbol type as is
119 4129 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
120
2/2
✓ Branch 5 → 6 taken 1399 times.
✓ Branch 5 → 7 taken 2730 times.
4129 if (sourceSourceFile == sourceFile)
121 1399 return symbolType;
122
123 // Match the scope of the symbol type against all scopes in the name registry of this source file
124
1/2
✓ Branch 7 → 8 taken 2730 times.
✗ Branch 7 → 45 not taken.
2730 const QualType baseType = symbolType.getBase();
125
5/8
✓ Branch 8 → 9 taken 2730 times.
✗ Branch 8 → 44 not taken.
✓ Branch 9 → 10 taken 2730 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 2730 times.
✗ Branch 10 → 44 not taken.
✓ Branch 33 → 12 taken 119104 times.
✓ Branch 33 → 34 taken 64 times.
119168 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
126
7/10
✓ Branch 13 → 14 taken 119104 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 119104 times.
✗ Branch 14 → 44 not taken.
✓ Branch 15 → 16 taken 119104 times.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 8034 times.
✓ Branch 16 → 18 taken 111070 times.
✓ Branch 19 → 20 taken 8034 times.
✓ Branch 19 → 31 taken 111070 times.
119104 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
127
3/4
✓ Branch 20 → 21 taken 8034 times.
✗ Branch 20 → 43 not taken.
✓ Branch 29 → 23 taken 13711 times.
✓ Branch 29 → 30 taken 5368 times.
19079 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
128
3/4
✓ Branch 24 → 25 taken 13711 times.
✗ Branch 24 → 43 not taken.
✓ Branch 25 → 26 taken 2666 times.
✓ Branch 25 → 27 taken 11045 times.
13711 if (manifestation->scope == baseType.getBodyScope())
129 2666 return symbolType;
130
131 // This source file does not know about the struct at all
132 // -> show it how to find the struct
133
2/4
✓ Branch 34 → 35 taken 64 times.
✗ Branch 34 → 45 not taken.
✓ Branch 35 → 36 taken 64 times.
✗ Branch 35 → 45 not taken.
64 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
134
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 64 times.
64 assert(origRegistryEntry != nullptr);
135 64 const uint64_t typeId = origRegistryEntry->typeId;
136 64 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
137
2/4
✓ Branch 38 → 39 taken 64 times.
✗ Branch 38 → 45 not taken.
✓ Branch 39 → 40 taken 64 times.
✗ Branch 39 → 45 not taken.
64 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
138
139 64 return symbolType;
140 }
141
142 /**
143 * Returns the operator function list for the current manifestation and the given node
144 *
145 * @param node Node to retrieve the op fct pointer list from
146 * @return Op fct pointer list
147 */
148 924 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
149
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 924 times.
924 assert(node->getOpFctPointers()->size() > manIdx);
150 924 return node->getOpFctPointers()->at(manIdx);
151 }
152
153 /**
154 * Check if a function has been type-checked already. If not, request a revisit
155 *
156 * @param fct Function to check
157 */
158 28581 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
159
4/4
✓ Branch 2 → 3 taken 28477 times.
✓ Branch 2 → 5 taken 104 times.
✓ Branch 3 → 4 taken 16822 times.
✓ Branch 3 → 5 taken 11655 times.
28581 if (fct && !fct->alreadyTypeChecked)
160 16822 fct->entry->scope->sourceFile->reVisitRequested = true;
161 28581 }
162
163 /**
164 * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module.
165 *
166 * @param typeName Given type name
167 */
168 37265 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
169
2/2
✓ Branch 18 → 4 taken 109435 times.
✓ Branch 18 → 19 taken 34630 times.
144065 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
170
8/10
✓ Branch 7 → 8 taken 109435 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 8134 times.
✓ Branch 8 → 12 taken 101301 times.
✓ Branch 9 → 10 taken 8134 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2635 times.
✓ Branch 10 → 12 taken 5499 times.
✓ Branch 13 → 14 taken 2635 times.
✓ Branch 13 → 16 taken 106800 times.
109435 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
171
1/2
✓ Branch 14 → 15 taken 2635 times.
✗ Branch 14 → 20 not taken.
2635 sourceFile->requestRuntimeModule(runtimeModule);
172 2635 break;
173 }
174 }
175 37265 }
176
177 /**
178 * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module.
179 *
180 * @param functionName Given function name
181 */
182 11346 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
183
2/2
✓ Branch 18 → 4 taken 173971 times.
✓ Branch 18 → 19 taken 10733 times.
184704 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
184
8/10
✓ Branch 7 → 8 taken 173971 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 640 times.
✓ Branch 8 → 12 taken 173331 times.
✓ Branch 9 → 10 taken 640 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 613 times.
✓ Branch 10 → 12 taken 27 times.
✓ Branch 13 → 14 taken 613 times.
✓ Branch 13 → 16 taken 173358 times.
173971 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
185
1/2
✓ Branch 14 → 15 taken 613 times.
✗ Branch 14 → 20 not taken.
613 sourceFile->requestRuntimeModule(runtimeModule);
186 613 break;
187 }
188 }
189 11346 }
190
191 /**
192 * Add a soft error to the error list
193 */
194 25 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
195 25 resourceManager.errorManager.addSoftError(node, errorType, message);
196 25 }
197
198 } // namespace spice::compiler
199