GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeChecker.cpp
Date: 2025-11-06 00:20:37
Coverage Exec Excl Total
Lines: 100.0% 95 0 95
Functions: 100.0% 11 0 11
Branches: 67.2% 137 0 204

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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/SymbolTableBuilder.h>
9
10 namespace spice::compiler {
11
12 3348 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
13
1/2
✓ Branch 4 → 5 taken 3348 times.
✗ Branch 4 → 7 not taken.
3348 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
14 3348 }
15
16 2631 std::any TypeChecker::visitEntry(EntryNode *node) {
17 // Initialize
18 2631 currentScope = rootScope;
19
20 // Initialize AST nodes with size of 1
21 2631 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
22
2/2
✓ Branch 2 → 3 taken 1109 times.
✓ Branch 2 → 4 taken 1522 times.
2631 if (isPrepare)
23 1109 node->resizeToNumberOfManifestations(1);
24
25 // Visit children
26
2/2
✓ Branch 4 → 5 taken 2587 times.
✓ Branch 4 → 22 taken 44 times.
2631 visitChildren(node);
27
28 // Check which implicit structures we need for each struct, defined in this source file
29
2/2
✓ Branch 6 → 7 taken 1095 times.
✓ Branch 6 → 19 taken 1492 times.
2587 if (isPrepare) {
30
1/2
✓ Branch 7 → 8 taken 1095 times.
✗ Branch 7 → 26 not taken.
1095 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
31
2/2
✓ Branch 16 → 10 taken 674 times.
✓ Branch 16 → 17 taken 1095 times.
1769 for (const Struct *manifestation : manifestations) {
32 // Check if we need to create a default ctor, copy ctor or dtor
33
1/2
✓ Branch 11 → 12 taken 674 times.
✗ Branch 11 → 23 not taken.
674 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
34
1/2
✓ Branch 12 → 13 taken 674 times.
✗ Branch 12 → 23 not taken.
674 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
35
1/2
✓ Branch 13 → 14 taken 674 times.
✗ Branch 13 → 23 not taken.
674 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
36 }
37 1095 }
38
39
1/2
✓ Branch 19 → 20 taken 2587 times.
✗ Branch 19 → 27 not taken.
2587 return nullptr;
40 }
41
42 /**
43 * Check if the capture rules for async lambdas are enforced if the async attribute is set
44 *
45 * Only one capture with pointer type, pass-by-val is allowed, since only then we can store it in the second field of the
46 * fat pointer and can ensure, that no stack variable is referenced inside the lambda.
47 *
48 * @param node Lambda base node
49 * @param attrs Lambda attributes
50 * @return False if the rules are violated, true otherwise
51 */
52 37 bool TypeChecker::checkAsyncLambdaCaptureRules(const LambdaBaseNode *node, const LambdaAttrNode *attrs) const {
53 // If the async attribute is not set, we can return early
54
18/32
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 13 taken 21 times.
✓ Branch 5 → 6 taken 16 times.
✗ Branch 5 → 53 not taken.
✓ Branch 6 → 7 taken 16 times.
✗ Branch 6 → 53 not taken.
✓ Branch 7 → 8 taken 16 times.
✗ Branch 7 → 13 not taken.
✓ Branch 10 → 11 taken 16 times.
✗ Branch 10 → 53 not taken.
✓ Branch 11 → 12 taken 16 times.
✗ Branch 11 → 53 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 16 times.
✓ Branch 15 → 16 taken 16 times.
✓ Branch 15 → 17 taken 21 times.
✓ Branch 17 → 18 taken 16 times.
✓ Branch 17 → 20 taken 21 times.
✓ Branch 20 → 21 taken 16 times.
✓ Branch 20 → 22 taken 21 times.
✓ Branch 22 → 23 taken 16 times.
✓ Branch 22 → 25 taken 21 times.
✓ Branch 25 → 26 taken 21 times.
✓ Branch 25 → 27 taken 16 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.
101 if (!attrs || !attrs->attrLst->hasAttr(ATTR_ASYNC) || !attrs->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue)
55 21 return true; // Not violated
56
57 // If we don't have any captures, we can return early
58 16 const CaptureMap &captures = node->bodyScope->symbolTable.captures;
59
2/2
✓ Branch 28 → 29 taken 10 times.
✓ Branch 28 → 30 taken 6 times.
16 if (captures.empty())
60 10 return true; // Not violated
61
62 // Check for the capture rules
63 6 if (const Capture &capture = captures.begin()->second;
64
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) {
65 5 const auto warningMessage =
66 "Async lambdas can only capture one pointer by value without storing captures in the caller stack frame, which can lead "
67 "to bugs due to references, outliving the validity scope of the referenced variable.";
68
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);
69
1/2
✓ Branch 48 → 49 taken 5 times.
✗ Branch 48 → 77 not taken.
5 currentScope->sourceFile->compilerOutput.warnings.push_back(warning);
70 5 }
71
72 6 return false; // Violated
73 }
74
75 24 Function *TypeChecker::matchCopyCtor(const QualType &thisType, const ASTNode *node) const {
76
1/2
✓ Branch 2 → 3 taken 24 times.
✗ Branch 2 → 40 not taken.
24 Scope *matchScope = thisType.getBodyScope();
77
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24 times.
24 assert(matchScope != nullptr);
78
2/4
✓ Branch 5 → 6 taken 24 times.
✗ Branch 5 → 27 not taken.
✓ Branch 9 → 10 taken 24 times.
✗ Branch 9 → 23 not taken.
48 const ArgList args = {{thisType.toConstRef(node), false}};
79
2/4
✓ Branch 14 → 15 taken 24 times.
✗ Branch 14 → 31 not taken.
✓ Branch 15 → 16 taken 24 times.
✗ Branch 15 → 29 not taken.
96 return FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
80 24 }
81
82 129516 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
83 // Skip all types, except structs
84
3/4
✓ Branch 2 → 3 taken 129516 times.
✗ Branch 2 → 50 not taken.
✓ Branch 3 → 4 taken 117080 times.
✓ Branch 3 → 5 taken 12436 times.
129516 if (!symbolType.isBase(TY_STRUCT))
85 117080 return symbolType;
86
87 // If the target scope is in the current source file, we can return the symbol type as is
88 12436 SourceFile *targetSourceFile = targetScope->sourceFile;
89
2/2
✓ Branch 5 → 6 taken 5526 times.
✓ Branch 5 → 7 taken 6910 times.
12436 if (targetSourceFile == sourceFile)
90 5526 return symbolType;
91
92 // Match the scope of the symbol type against all scopes in the name registry of the target file
93
5/8
✓ Branch 7 → 8 taken 6910 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 6910 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 6910 times.
✗ Branch 9 → 46 not taken.
✓ Branch 33 → 11 taken 157645 times.
✓ Branch 33 → 34 taken 293 times.
157938 for (const auto &entry : targetSourceFile->exportedNameRegistry | std::views::values)
94
7/10
✓ Branch 12 → 13 taken 157645 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 157645 times.
✗ Branch 13 → 46 not taken.
✓ Branch 14 → 15 taken 157645 times.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 17871 times.
✓ Branch 15 → 17 taken 139774 times.
✓ Branch 18 → 19 taken 17871 times.
✓ Branch 18 → 31 taken 139774 times.
157645 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
95
3/4
✓ Branch 19 → 20 taken 17871 times.
✗ Branch 19 → 45 not taken.
✓ Branch 29 → 22 taken 34252 times.
✓ Branch 29 → 30 taken 11254 times.
45506 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
96
4/6
✓ Branch 23 → 24 taken 34252 times.
✗ Branch 23 → 44 not taken.
✓ Branch 24 → 25 taken 34252 times.
✗ Branch 24 → 44 not taken.
✓ Branch 25 → 26 taken 6617 times.
✓ Branch 25 → 27 taken 27635 times.
34252 if (manifestation->scope == symbolType.getBase().getBodyScope())
97 6617 return symbolType;
98
99 // The target file does not know about the struct at all
100 // -> show it how to find the struct
101
3/6
✓ Branch 34 → 35 taken 293 times.
✗ Branch 34 → 47 not taken.
✓ Branch 35 → 36 taken 293 times.
✗ Branch 35 → 47 not taken.
✓ Branch 36 → 37 taken 293 times.
✗ Branch 36 → 47 not taken.
293 const std::string structName = symbolType.getBase().getSubType();
102
1/2
✓ Branch 37 → 38 taken 293 times.
✗ Branch 37 → 48 not taken.
293 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
103
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 293 times.
293 assert(origRegistryEntry != nullptr);
104 293 const uint64_t targetTypeId = origRegistryEntry->typeId;
105 293 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
106
1/2
✓ Branch 40 → 41 taken 293 times.
✗ Branch 40 → 48 not taken.
293 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
107
108 293 return symbolType;
109 293 }
110
111 4384 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
112 // Skip all types, except structs
113
3/4
✓ Branch 2 → 3 taken 4384 times.
✗ Branch 2 → 45 not taken.
✓ Branch 3 → 4 taken 539 times.
✓ Branch 3 → 5 taken 3845 times.
4384 if (!symbolType.isBase(TY_STRUCT))
114 539 return symbolType;
115
116 // If the given source file is in the current one, we can return the symbol type as is
117 3845 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
118
2/2
✓ Branch 5 → 6 taken 1265 times.
✓ Branch 5 → 7 taken 2580 times.
3845 if (sourceSourceFile == sourceFile)
119 1265 return symbolType;
120
121 // Match the scope of the symbol type against all scopes in the name registry of this source file
122
1/2
✓ Branch 7 → 8 taken 2580 times.
✗ Branch 7 → 45 not taken.
2580 const QualType baseType = symbolType.getBase();
123
5/8
✓ Branch 8 → 9 taken 2580 times.
✗ Branch 8 → 44 not taken.
✓ Branch 9 → 10 taken 2580 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 2580 times.
✗ Branch 10 → 44 not taken.
✓ Branch 33 → 12 taken 106427 times.
✓ Branch 33 → 34 taken 58 times.
106485 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
124
7/10
✓ Branch 13 → 14 taken 106427 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 106427 times.
✗ Branch 14 → 44 not taken.
✓ Branch 15 → 16 taken 106427 times.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 7671 times.
✓ Branch 16 → 18 taken 98756 times.
✓ Branch 19 → 20 taken 7671 times.
✓ Branch 19 → 31 taken 98756 times.
106427 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
125
3/4
✓ Branch 20 → 21 taken 7671 times.
✗ Branch 20 → 43 not taken.
✓ Branch 29 → 23 taken 13197 times.
✓ Branch 29 → 30 taken 5149 times.
18346 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
126
3/4
✓ Branch 24 → 25 taken 13197 times.
✗ Branch 24 → 43 not taken.
✓ Branch 25 → 26 taken 2522 times.
✓ Branch 25 → 27 taken 10675 times.
13197 if (manifestation->scope == baseType.getBodyScope())
127 2522 return symbolType;
128
129 // This source file does not know about the struct at all
130 // -> show it how to find the struct
131
2/4
✓ Branch 34 → 35 taken 58 times.
✗ Branch 34 → 45 not taken.
✓ Branch 35 → 36 taken 58 times.
✗ Branch 35 → 45 not taken.
58 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
132
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 58 times.
58 assert(origRegistryEntry != nullptr);
133 58 const uint64_t typeId = origRegistryEntry->typeId;
134 58 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
135
2/4
✓ Branch 38 → 39 taken 58 times.
✗ Branch 38 → 45 not taken.
✓ Branch 39 → 40 taken 58 times.
✗ Branch 39 → 45 not taken.
58 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
136
137 58 return symbolType;
138 }
139
140 /**
141 * Returns the operator function list for the current manifestation and the given node
142 *
143 * @param node Node to retrieve the op fct pointer list from
144 * @return Op fct pointer list
145 */
146 869 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
147
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 869 times.
869 assert(node->getOpFctPointers()->size() > manIdx);
148 869 return node->getOpFctPointers()->at(manIdx);
149 }
150
151 /**
152 * Check if a function has been type-checked already. If not, request a revisit
153 *
154 * @param fct Function to check
155 */
156 23890 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
157
4/4
✓ Branch 2 → 3 taken 23800 times.
✓ Branch 2 → 5 taken 90 times.
✓ Branch 3 → 4 taken 13441 times.
✓ Branch 3 → 5 taken 10359 times.
23890 if (fct && !fct->alreadyTypeChecked)
158 13441 fct->entry->scope->sourceFile->reVisitRequested = true;
159 23890 }
160
161 /**
162 * Check type name against well-known type names that require a runtime import. If found one, auto-import the runtime module.
163 *
164 * @param typeName Given type name
165 */
166 30175 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
167
2/2
✓ Branch 18 → 4 taken 88388 times.
✓ Branch 18 → 19 taken 27787 times.
116175 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
168
8/10
✓ Branch 7 → 8 taken 88388 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 7277 times.
✓ Branch 8 → 12 taken 81111 times.
✓ Branch 9 → 10 taken 7277 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2388 times.
✓ Branch 10 → 12 taken 4889 times.
✓ Branch 13 → 14 taken 2388 times.
✓ Branch 13 → 16 taken 86000 times.
88388 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
169
1/2
✓ Branch 14 → 15 taken 2388 times.
✗ Branch 14 → 20 not taken.
2388 sourceFile->requestRuntimeModule(runtimeModule);
170 2388 break;
171 }
172 }
173 30175 }
174
175 /**
176 * Check type name against well-known function names that require a runtime import. If found one, auto-import the runtime module.
177 *
178 * @param functionName Given function name
179 */
180 8957 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
181
2/2
✓ Branch 18 → 4 taken 135986 times.
✓ Branch 18 → 19 taken 8392 times.
144378 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
182
8/10
✓ Branch 7 → 8 taken 135986 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 592 times.
✓ Branch 8 → 12 taken 135394 times.
✓ Branch 9 → 10 taken 592 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 565 times.
✓ Branch 10 → 12 taken 27 times.
✓ Branch 13 → 14 taken 565 times.
✓ Branch 13 → 16 taken 135421 times.
135986 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
183
1/2
✓ Branch 14 → 15 taken 565 times.
✗ Branch 14 → 20 not taken.
565 sourceFile->requestRuntimeModule(runtimeModule);
184 565 break;
185 }
186 }
187 8957 }
188
189 /**
190 * Add a soft error to the error list
191 */
192 25 void TypeChecker::softError(const ASTNode *node, const SemanticErrorType errorType, const std::string &message) const {
193 25 resourceManager.errorManager.addSoftError(node, errorType, message);
194 25 }
195
196 } // namespace spice::compiler
197