GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeChecker.cpp
Date: 2025-10-09 06:28:01
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 3324 TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sourceFile, TypeCheckerMode typeCheckerMode)
13
1/2
✓ Branch 4 → 5 taken 3324 times.
✗ Branch 4 → 7 not taken.
3324 : CompilerPass(resourceManager, sourceFile), typeCheckerMode(typeCheckerMode), warnings(sourceFile->compilerOutput.warnings) {
14 3324 }
15
16 2598 std::any TypeChecker::visitEntry(EntryNode *node) {
17 // Initialize
18 2598 currentScope = rootScope;
19
20 // Initialize AST nodes with size of 1
21 2598 const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
22
2/2
✓ Branch 2 → 3 taken 1093 times.
✓ Branch 2 → 4 taken 1505 times.
2598 if (isPrepare)
23 1093 node->resizeToNumberOfManifestations(1);
24
25 // Visit children
26
2/2
✓ Branch 4 → 5 taken 2554 times.
✓ Branch 4 → 22 taken 44 times.
2598 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 1079 times.
✓ Branch 6 → 19 taken 1475 times.
2554 if (isPrepare) {
30
1/2
✓ Branch 7 → 8 taken 1079 times.
✗ Branch 7 → 26 not taken.
1079 const std::vector<const Struct *> manifestations = rootScope->getAllStructManifestationsInDeclarationOrder();
31
2/2
✓ Branch 16 → 10 taken 663 times.
✓ Branch 16 → 17 taken 1079 times.
1742 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 663 times.
✗ Branch 11 → 23 not taken.
663 createDefaultCtorIfRequired(*manifestation, manifestation->scope);
34
1/2
✓ Branch 12 → 13 taken 663 times.
✗ Branch 12 → 23 not taken.
663 createDefaultCopyCtorIfRequired(*manifestation, manifestation->scope);
35
1/2
✓ Branch 13 → 14 taken 663 times.
✗ Branch 13 → 23 not taken.
663 createDefaultDtorIfRequired(*manifestation, manifestation->scope);
36 }
37 1079 }
38
39
1/2
✓ Branch 19 → 20 taken 2554 times.
✗ Branch 19 → 27 not taken.
2554 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(this, matchScope, CTOR_FUNCTION_NAME, thisType, args, {}, true, node);
80 24 }
81
82 128807 QualType TypeChecker::mapLocalTypeToImportedScopeType(const Scope *targetScope, const QualType &symbolType) const {
83 // Skip all types, except structs
84
3/4
✓ Branch 2 → 3 taken 128807 times.
✗ Branch 2 → 50 not taken.
✓ Branch 3 → 4 taken 116558 times.
✓ Branch 3 → 5 taken 12249 times.
128807 if (!symbolType.isBase(TY_STRUCT))
85 116558 return symbolType;
86
87 // If the target scope is in the current source file, we can return the symbol type as is
88 12249 SourceFile *targetSourceFile = targetScope->sourceFile;
89
2/2
✓ Branch 5 → 6 taken 5482 times.
✓ Branch 5 → 7 taken 6767 times.
12249 if (targetSourceFile == sourceFile)
90 5482 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 6767 times.
✗ Branch 7 → 46 not taken.
✓ Branch 8 → 9 taken 6767 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 6767 times.
✗ Branch 9 → 46 not taken.
✓ Branch 33 → 11 taken 319831 times.
✓ Branch 33 → 34 taken 289 times.
320120 for (const auto &entry : targetSourceFile->exportedNameRegistry | std::views::values)
94
7/10
✓ Branch 12 → 13 taken 319831 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 319831 times.
✗ Branch 13 → 46 not taken.
✓ Branch 14 → 15 taken 319831 times.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 22319 times.
✓ Branch 15 → 17 taken 297512 times.
✓ Branch 18 → 19 taken 22319 times.
✓ Branch 18 → 31 taken 297512 times.
319831 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
95
3/4
✓ Branch 19 → 20 taken 22319 times.
✗ Branch 19 → 45 not taken.
✓ Branch 29 → 22 taken 46680 times.
✓ Branch 29 → 30 taken 15841 times.
62521 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
96
4/6
✓ Branch 23 → 24 taken 46680 times.
✗ Branch 23 → 44 not taken.
✓ Branch 24 → 25 taken 46680 times.
✗ Branch 24 → 44 not taken.
✓ Branch 25 → 26 taken 6478 times.
✓ Branch 25 → 27 taken 40202 times.
46680 if (manifestation->scope == symbolType.getBase().getBodyScope())
97 6478 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 289 times.
✗ Branch 34 → 47 not taken.
✓ Branch 35 → 36 taken 289 times.
✗ Branch 35 → 47 not taken.
✓ Branch 36 → 37 taken 289 times.
✗ Branch 36 → 47 not taken.
289 const std::string structName = symbolType.getBase().getSubType();
102
1/2
✓ Branch 37 → 38 taken 289 times.
✗ Branch 37 → 48 not taken.
289 const NameRegistryEntry *origRegistryEntry = sourceFile->getNameRegistryEntry(structName);
103
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 289 times.
289 assert(origRegistryEntry != nullptr);
104 289 const uint64_t targetTypeId = origRegistryEntry->typeId;
105 289 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
106
1/2
✓ Branch 40 → 41 taken 289 times.
✗ Branch 40 → 48 not taken.
289 targetSourceFile->addNameRegistryEntry(structName, targetTypeId, targetEntry, origRegistryEntry->targetScope, false);
107
108 289 return symbolType;
109 289 }
110
111 4289 QualType TypeChecker::mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const {
112 // Skip all types, except structs
113
3/4
✓ Branch 2 → 3 taken 4289 times.
✗ Branch 2 → 45 not taken.
✓ Branch 3 → 4 taken 539 times.
✓ Branch 3 → 5 taken 3750 times.
4289 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 3750 const SourceFile *sourceSourceFile = sourceScope->sourceFile;
118
2/2
✓ Branch 5 → 6 taken 1248 times.
✓ Branch 5 → 7 taken 2502 times.
3750 if (sourceSourceFile == sourceFile)
119 1248 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 2502 times.
✗ Branch 7 → 45 not taken.
2502 const QualType baseType = symbolType.getBase();
123
5/8
✓ Branch 8 → 9 taken 2502 times.
✗ Branch 8 → 44 not taken.
✓ Branch 9 → 10 taken 2502 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 2502 times.
✗ Branch 10 → 44 not taken.
✓ Branch 33 → 12 taken 145058 times.
✓ Branch 33 → 34 taken 54 times.
145112 for (const auto &entry : sourceFile->exportedNameRegistry | std::views::values)
124
7/10
✓ Branch 13 → 14 taken 145058 times.
✗ Branch 13 → 18 not taken.
✓ Branch 14 → 15 taken 145058 times.
✗ Branch 14 → 44 not taken.
✓ Branch 15 → 16 taken 145058 times.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 8187 times.
✓ Branch 16 → 18 taken 136871 times.
✓ Branch 19 → 20 taken 8187 times.
✓ Branch 19 → 31 taken 136871 times.
145058 if (entry.targetEntry != nullptr && entry.targetEntry->getQualType().isBase(TY_STRUCT))
125
3/4
✓ Branch 20 → 21 taken 8187 times.
✗ Branch 20 → 43 not taken.
✓ Branch 29 → 23 taken 16678 times.
✓ Branch 29 → 30 taken 5739 times.
22417 for (const Struct *manifestation : *entry.targetEntry->declNode->getStructManifestations())
126
3/4
✓ Branch 24 → 25 taken 16678 times.
✗ Branch 24 → 43 not taken.
✓ Branch 25 → 26 taken 2448 times.
✓ Branch 25 → 27 taken 14230 times.
16678 if (manifestation->scope == baseType.getBodyScope())
127 2448 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 54 times.
✗ Branch 34 → 45 not taken.
✓ Branch 35 → 36 taken 54 times.
✗ Branch 35 → 45 not taken.
54 const NameRegistryEntry *origRegistryEntry = sourceSourceFile->getNameRegistryEntry(baseType.getSubType());
132
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 54 times.
54 assert(origRegistryEntry != nullptr);
133 54 const uint64_t typeId = origRegistryEntry->typeId;
134 54 SymbolTableEntry *targetEntry = origRegistryEntry->targetEntry;
135
2/4
✓ Branch 38 → 39 taken 54 times.
✗ Branch 38 → 45 not taken.
✓ Branch 39 → 40 taken 54 times.
✗ Branch 39 → 45 not taken.
54 sourceFile->addNameRegistryEntry(baseType.getSubType(), typeId, targetEntry, origRegistryEntry->targetScope, false);
136
137 54 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 861 std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) const {
147
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 861 times.
861 assert(node->getOpFctPointers()->size() > manIdx);
148 861 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 23664 void TypeChecker::requestRevisitIfRequired(const Function *fct) {
157
4/4
✓ Branch 2 → 3 taken 23576 times.
✓ Branch 2 → 5 taken 88 times.
✓ Branch 3 → 4 taken 13227 times.
✓ Branch 3 → 5 taken 10349 times.
23664 if (fct && !fct->alreadyTypeChecked)
158 13227 fct->entry->scope->sourceFile->reVisitRequested = true;
159 23664 }
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 29707 void TypeChecker::ensureLoadedRuntimeForTypeName(const std::string &typeName) const {
167
2/2
✓ Branch 18 → 4 taken 87068 times.
✓ Branch 18 → 19 taken 27406 times.
114474 for (const auto &[wellKnownTypeName, runtimeModule] : TYPE_NAME_TO_RT_MODULE_MAPPING) {
168
8/10
✓ Branch 7 → 8 taken 87068 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 7160 times.
✓ Branch 8 → 12 taken 79908 times.
✓ Branch 9 → 10 taken 7160 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 2301 times.
✓ Branch 10 → 12 taken 4859 times.
✓ Branch 13 → 14 taken 2301 times.
✓ Branch 13 → 16 taken 84767 times.
87068 if (typeName == wellKnownTypeName && !sourceFile->isRT(runtimeModule)) {
169
1/2
✓ Branch 14 → 15 taken 2301 times.
✗ Branch 14 → 20 not taken.
2301 sourceFile->requestRuntimeModule(runtimeModule);
170 2301 break;
171 }
172 }
173 29707 }
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 8864 void TypeChecker::ensureLoadedRuntimeForFunctionName(const std::string &functionName) const {
181
2/2
✓ Branch 18 → 4 taken 126460 times.
✓ Branch 18 → 19 taken 8317 times.
134777 for (const auto &[wellKnownFunctionName, runtimeModule] : FCT_NAME_TO_RT_MODULE_MAPPING) {
182
8/10
✓ Branch 7 → 8 taken 126460 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 573 times.
✓ Branch 8 → 12 taken 125887 times.
✓ Branch 9 → 10 taken 573 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 547 times.
✓ Branch 10 → 12 taken 26 times.
✓ Branch 13 → 14 taken 547 times.
✓ Branch 13 → 16 taken 125913 times.
126460 if (functionName == wellKnownFunctionName && !sourceFile->isRT(runtimeModule)) {
183
1/2
✓ Branch 14 → 15 taken 547 times.
✗ Branch 14 → 20 not taken.
547 sourceFile->requestRuntimeModule(runtimeModule);
184 547 break;
185 }
186 }
187 8864 }
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