GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.2% 351 / 0 / 365
Functions: 100.0% 31 / 0 / 31
Branches: 59.0% 418 / 0 / 708

src/symboltablebuilder/SymbolTableBuilder.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "SymbolTableBuilder.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTBuilder.h>
7 #include <ast/Attributes.h>
8 #include <driver/Driver.h>
9 #include <exception/SemanticError.h>
10 #include <global/GlobalResourceManager.h>
11 #include <symboltablebuilder/Scope.h>
12
13 namespace spice::compiler {
14
15 1226 SymbolTableBuilder::SymbolTableBuilder(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
16 1226 : CompilerPass(resourceManager, sourceFile), rootScope(sourceFile->globalScope.get()) {}
17
18 1226 std::any SymbolTableBuilder::visitEntry(EntryNode *node) {
19 // Initialize
20 1226 currentScope = rootScope;
21
22 // Visit children
23
2/2
✓ Branch 2 → 3 taken 1209 times.
✓ Branch 2 → 22 taken 17 times.
1226 visitChildren(node);
24
25 // Check if the main function exists
26
4/4
✓ Branch 4 → 5 taken 1206 times.
✓ Branch 4 → 7 taken 3 times.
✓ Branch 5 → 6 taken 1201 times.
✓ Branch 5 → 7 taken 5 times.
1209 const bool mainFctRequired = cliOptions.outputContainer == OutputContainer::EXECUTABLE && !cliOptions.noEntryFct;
27
6/6
✓ Branch 8 → 9 taken 419 times.
✓ Branch 8 → 19 taken 790 times.
✓ Branch 9 → 10 taken 412 times.
✓ Branch 9 → 19 taken 7 times.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 19 taken 410 times.
1209 if (sourceFile->isMainFile && mainFctRequired && !hasMainFunction)
28
2/4
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 26 not taken.
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 23 not taken.
6 throw SemanticError(node, MISSING_MAIN_FUNCTION, "No main function found", false);
29
30
1/2
✓ Branch 19 → 20 taken 1207 times.
✗ Branch 19 → 32 not taken.
1207 return nullptr;
31 }
32
33 415 std::any SymbolTableBuilder::visitMainFctDef(MainFctDefNode *node) {
34 // Visit attributes
35
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 6 taken 414 times.
415 if (node->attrs)
36
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 51 taken 1 time.
1 visit(node->attrs);
37
38 // Check if the function is already defined
39
3/4
✓ Branch 8 → 9 taken 414 times.
✗ Branch 8 → 54 not taken.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 23 taken 413 times.
1656 if (rootScope->lookupStrict(MAIN_FUNCTION_NAME))
40
2/4
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 61 not taken.
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 58 not taken.
3 throw SemanticError(node, FUNCTION_DECLARED_TWICE, "Main function is declared twice");
41
42 // Insert symbol for main function
43
1/2
✓ Branch 25 → 26 taken 413 times.
✗ Branch 25 → 69 not taken.
1239 SymbolTableEntry *mainFctEntry = currentScope->insert(MAIN_FUNCTION_NAME, node);
44 413 mainFctEntry->used = true;
45
46 // Create scope for main function body
47
1/2
✓ Branch 31 → 32 taken 413 times.
✗ Branch 31 → 84 not taken.
413 const std::string &scopeId = MainFctDefNode::getScopeId();
48
1/2
✓ Branch 32 → 33 taken 413 times.
✗ Branch 32 → 82 not taken.
413 node->bodyScope = currentScope = rootScope->createChildScope(scopeId, ScopeType::FUNC_PROC_BODY, &node->codeLoc);
49 413 currentScope->isGenericScope = false;
50
51 // Declare variable for the return value in the function scope
52
1/2
✓ Branch 35 → 36 taken 413 times.
✗ Branch 35 → 75 not taken.
1239 SymbolTableEntry *resultVarEntry = node->bodyScope->insert(RETURN_VARIABLE_NAME, node);
53 413 resultVarEntry->used = true;
54
55 // Visit arguments in new scope
56
2/2
✓ Branch 41 → 42 taken 4 times.
✓ Branch 41 → 45 taken 409 times.
413 if (node->takesArgs)
57
1/2
✓ Branch 42 → 43 taken 4 times.
✗ Branch 42 → 79 not taken.
4 visit(node->paramLst);
58
59 // Visit function body in new scope
60
2/2
✓ Branch 45 → 46 taken 412 times.
✓ Branch 45 → 80 taken 1 time.
413 visit(node->body);
61
62 // Return to root scope
63 412 currentScope = rootScope;
64
65 412 hasMainFunction = true;
66
1/2
✓ Branch 47 → 48 taken 412 times.
✗ Branch 47 → 81 not taken.
824 return nullptr;
67 413 }
68
69 8241 std::any SymbolTableBuilder::visitFctDef(FctDefNode *node) {
70 // Visit attributes
71
2/2
✓ Branch 2 → 3 taken 339 times.
✓ Branch 2 → 6 taken 7902 times.
8241 if (node->attrs)
72
1/2
✓ Branch 3 → 4 taken 339 times.
✗ Branch 3 → 89 not taken.
339 visit(node->attrs);
73
74 // Build function qualifiers
75
2/2
✓ Branch 6 → 7 taken 8043 times.
✓ Branch 6 → 28 taken 198 times.
8241 if (const QualifierLstNode *qualifierLst = node->qualifierLst; qualifierLst) {
76
2/2
✓ Branch 26 → 9 taken 10879 times.
✓ Branch 26 → 27 taken 8043 times.
18922 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
77
2/2
✓ Branch 10 → 11 taken 2859 times.
✓ Branch 10 → 12 taken 8020 times.
10879 if (qualifier->type == QualifierNode::QualifierType::TY_INLINE)
78 2859 node->qualifiers.isInline = true;
79
2/2
✓ Branch 12 → 13 taken 8005 times.
✓ Branch 12 → 14 taken 15 times.
8020 else if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
80 8005 node->qualifiers.isPublic = true;
81
1/2
✓ Branch 14 → 15 taken 15 times.
✗ Branch 14 → 16 not taken.
15 else if (qualifier->type == QualifierNode::QualifierType::TY_CONST)
82 15 node->qualifiers.isConst = true;
83 else
84 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on a function definition");
85 }
86 }
87
88 // Change to struct scope if this function is a method
89
2/2
✓ Branch 28 → 29 taken 3321 times.
✓ Branch 28 → 43 taken 4920 times.
8241 if (node->isMethod) {
90
1/2
✓ Branch 30 → 31 taken 3321 times.
✗ Branch 30 → 100 not taken.
3321 const std::string scopeName = Struct::getScopeName(node->name->structName);
91
1/2
✓ Branch 32 → 33 taken 3321 times.
✗ Branch 32 → 112 not taken.
3321 node->structScope = currentScope = currentScope->getChildScope(scopeName);
92
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 41 taken 3321 times.
3321 if (!currentScope)
93 throw SemanticError(node, REFERENCED_UNDEFINED_STRUCT, "Struct '" + node->name->structName + "' could not be found");
94 3321 }
95
96 // Create scope for the function
97
2/4
✓ Branch 43 → 44 taken 8241 times.
✗ Branch 43 → 117 not taken.
✓ Branch 44 → 45 taken 8241 times.
✗ Branch 44 → 115 not taken.
8241 node->scope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::FUNC_PROC_BODY, &node->codeLoc);
98
6/6
✓ Branch 46 → 47 taken 7130 times.
✓ Branch 46 → 49 taken 1111 times.
✓ Branch 47 → 48 taken 3191 times.
✓ Branch 47 → 50 taken 3939 times.
✓ Branch 48 → 49 taken 1024 times.
✓ Branch 48 → 50 taken 2167 times.
8241 currentScope->isGenericScope = node->hasTemplateTypes || (node->structScope && node->structScope->isGenericScope);
99
100 // Create symbol for 'this' variable
101
2/2
✓ Branch 51 → 52 taken 3321 times.
✓ Branch 51 → 61 taken 4920 times.
8241 if (node->isMethod)
102
1/2
✓ Branch 54 → 55 taken 3321 times.
✗ Branch 54 → 120 not taken.
13284 currentScope->insert(THIS_VARIABLE_NAME, node);
103
104 // Create symbol for 'result' variable
105
1/2
✓ Branch 63 → 64 taken 8241 times.
✗ Branch 63 → 126 not taken.
24723 currentScope->insert(RETURN_VARIABLE_NAME, node);
106
107 // Create symbols for the parameters
108
2/2
✓ Branch 69 → 70 taken 6348 times.
✓ Branch 69 → 73 taken 1893 times.
8241 if (node->hasParams)
109
1/2
✓ Branch 70 → 71 taken 6348 times.
✗ Branch 70 → 130 not taken.
6348 visit(node->paramLst);
110
111 // Visit the function body
112
1/2
✓ Branch 73 → 74 taken 8241 times.
✗ Branch 73 → 131 not taken.
8241 visit(node->body);
113
114 // Leave function body scope
115 8241 currentScope = node->scope->parent;
116
117 // Insert symbol for function into the symbol table
118
1/2
✓ Branch 75 → 76 taken 8241 times.
✗ Branch 75 → 134 not taken.
16482 node->entry = currentScope->insert(node->getSymbolTableEntryName(), node);
119
120 // Add to external name registry
121 // if a function has overloads, they both refer to the same entry in the registry. So we only register the name once
122 8241 const NameRegistryEntry *existingRegistryEntry = sourceFile->getNameRegistryEntry(node->name->fqName);
123
3/4
✓ Branch 81 → 82 taken 2100 times.
✓ Branch 81 → 83 taken 6141 times.
✓ Branch 82 → 83 taken 2100 times.
✗ Branch 82 → 84 not taken.
8241 if (!existingRegistryEntry || existingRegistryEntry->targetEntry != node->entry)
124 8241 sourceFile->addNameRegistryEntry(node->name->fqName, TY_FUNCTION, node->entry, currentScope, true);
125
126 // Leave the struct scope
127
2/2
✓ Branch 84 → 85 taken 3321 times.
✓ Branch 84 → 86 taken 4920 times.
8241 if (node->isMethod)
128 3321 currentScope = node->structScope->parent;
129
130
1/2
✓ Branch 86 → 87 taken 8241 times.
✗ Branch 86 → 135 not taken.
8241 return nullptr;
131 }
132
133 4268 std::any SymbolTableBuilder::visitProcDef(ProcDefNode *node) {
134 // Visit attributes
135
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 6 taken 4267 times.
4268 if (node->attrs)
136
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 88 taken 1 time.
1 visit(node->attrs);
137
138 // Build procedure qualifiers
139
2/2
✓ Branch 6 → 7 taken 3698 times.
✓ Branch 6 → 28 taken 569 times.
4267 if (const QualifierLstNode *qualifierLst = node->qualifierLst) {
140
2/2
✓ Branch 26 → 9 taken 4223 times.
✓ Branch 26 → 27 taken 3698 times.
7921 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
141
2/2
✓ Branch 10 → 11 taken 526 times.
✓ Branch 10 → 12 taken 3697 times.
4223 if (qualifier->type == QualifierNode::QualifierType::TY_INLINE)
142 526 node->qualifiers.isInline = true;
143
1/2
✓ Branch 12 → 13 taken 3697 times.
✗ Branch 12 → 14 not taken.
3697 else if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
144 3697 node->qualifiers.isPublic = true;
145 else if (qualifier->type == QualifierNode::QualifierType::TY_CONST)
146 node->qualifiers.isConst = true;
147 else
148 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on a procedure definition");
149 }
150 }
151
152 // Change to struct scope if this procedure is a method
153
2/2
✓ Branch 28 → 29 taken 3502 times.
✓ Branch 28 → 43 taken 765 times.
4267 if (node->isMethod) {
154
1/2
✓ Branch 30 → 31 taken 3502 times.
✗ Branch 30 → 99 not taken.
3502 const std::string &scopeName = Struct::getScopeName(node->name->structName);
155
1/2
✓ Branch 32 → 33 taken 3502 times.
✗ Branch 32 → 111 not taken.
3502 node->structScope = currentScope = currentScope->getChildScope(scopeName);
156
2/2
✓ Branch 33 → 34 taken 1 time.
✓ Branch 33 → 41 taken 3501 times.
3502 if (!currentScope)
157
3/6
✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 107 not taken.
✓ Branch 36 → 37 taken 1 time.
✗ Branch 36 → 105 not taken.
✓ Branch 37 → 38 taken 1 time.
✗ Branch 37 → 102 not taken.
1 throw SemanticError(node, REFERENCED_UNDEFINED_STRUCT, "Struct '" + node->name->structName + "' could not be found");
158 3502 }
159
160 // Create scope for the procedure
161
2/4
✓ Branch 43 → 44 taken 4266 times.
✗ Branch 43 → 116 not taken.
✓ Branch 44 → 45 taken 4266 times.
✗ Branch 44 → 114 not taken.
4266 node->scope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::FUNC_PROC_BODY, &node->codeLoc);
162
6/6
✓ Branch 46 → 47 taken 3072 times.
✓ Branch 46 → 49 taken 1194 times.
✓ Branch 47 → 48 taken 2845 times.
✓ Branch 47 → 50 taken 227 times.
✓ Branch 48 → 49 taken 865 times.
✓ Branch 48 → 50 taken 1980 times.
4266 currentScope->isGenericScope = node->hasTemplateTypes || (node->structScope && node->structScope->isGenericScope);
163
4/4
✓ Branch 51 → 52 taken 3501 times.
✓ Branch 51 → 55 taken 765 times.
✓ Branch 53 → 54 taken 121 times.
✓ Branch 53 → 55 taken 3380 times.
4266 currentScope->isDtorScope = node->isMethod && node->name->name == DTOR_FUNCTION_NAME;
164
165 // Create symbol for 'this' variable
166
2/2
✓ Branch 56 → 57 taken 3501 times.
✓ Branch 56 → 66 taken 765 times.
4266 if (node->isMethod)
167
1/2
✓ Branch 59 → 60 taken 3501 times.
✗ Branch 59 → 119 not taken.
14004 currentScope->insert(THIS_VARIABLE_NAME, node);
168
169 // Create symbols for the parameters
170
2/2
✓ Branch 66 → 67 taken 3158 times.
✓ Branch 66 → 70 taken 1108 times.
4266 if (node->hasParams)
171
1/2
✓ Branch 67 → 68 taken 3158 times.
✗ Branch 67 → 123 not taken.
3158 visit(node->paramLst);
172
173 // Visit the procedure body
174
1/2
✓ Branch 70 → 71 taken 4266 times.
✗ Branch 70 → 124 not taken.
4266 visit(node->body);
175
176 // Leave procedure body scope
177 4266 currentScope = node->scope->parent;
178
179 // Insert symbol for procedure into the symbol table
180
1/2
✓ Branch 72 → 73 taken 4266 times.
✗ Branch 72 → 127 not taken.
8532 node->entry = currentScope->insert(node->getSymbolTableEntryName(), node);
181
182 // Add to external name registry
183 // if a procedure has overloads, they both refer to the same entry in the registry. So we only register the name once
184 4266 const NameRegistryEntry *existingRegistryEntry = sourceFile->getNameRegistryEntry(node->name->fqName);
185
3/4
✓ Branch 78 → 79 taken 1182 times.
✓ Branch 78 → 80 taken 3084 times.
✓ Branch 79 → 80 taken 1182 times.
✗ Branch 79 → 81 not taken.
4266 if (!existingRegistryEntry || existingRegistryEntry->targetEntry != node->entry)
186 4266 sourceFile->addNameRegistryEntry(node->name->fqName, TY_PROCEDURE, node->entry, currentScope, true);
187
188 // Leave the struct scope
189
2/2
✓ Branch 81 → 82 taken 3501 times.
✓ Branch 81 → 83 taken 765 times.
4266 if (node->isMethod)
190 3501 currentScope = node->structScope->parent;
191
192 // Check if this is a constructor
193 4266 node->isCtor = node->name->nameFragments.back() == CTOR_FUNCTION_NAME;
194
195
1/2
✓ Branch 85 → 86 taken 4266 times.
✗ Branch 85 → 128 not taken.
4266 return nullptr;
196 }
197
198 738 std::any SymbolTableBuilder::visitStructDef(StructDefNode *node) {
199 // Visit attributes
200
2/2
✓ Branch 2 → 3 taken 65 times.
✓ Branch 2 → 6 taken 673 times.
738 if (node->attrs)
201
1/2
✓ Branch 3 → 4 taken 65 times.
✗ Branch 3 → 63 not taken.
65 visit(node->attrs);
202
203 // Check if this name already exists
204
3/4
✓ Branch 6 → 7 taken 738 times.
✗ Branch 6 → 94 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 17 taken 737 times.
1476 if (rootScope->lookupStrict(node->structName))
205
3/6
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 69 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 67 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 64 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->structName + "'");
206
207 // Create scope for the struct
208
1/2
✓ Branch 18 → 19 taken 737 times.
✗ Branch 18 → 73 not taken.
737 const std::string &scopeName = Struct::getScopeName(node->structName);
209
1/2
✓ Branch 20 → 21 taken 737 times.
✗ Branch 20 → 92 not taken.
737 node->structScope = currentScope = rootScope->createChildScope(scopeName, ScopeType::STRUCT, &node->codeLoc);
210 737 currentScope->isGenericScope = node->hasTemplateTypes;
211
212 // Insert implicit field for each interface type
213
2/2
✓ Branch 21 → 22 taken 143 times.
✓ Branch 21 → 35 taken 594 times.
737 if (node->hasInterfaces) {
214
2/2
✓ Branch 33 → 24 taken 143 times.
✓ Branch 33 → 34 taken 143 times.
286 for (DataTypeNode *interfaceNode : node->interfaceTypeLst->dataTypes) {
215 143 const std::string &interfaceName = interfaceNode->baseDataType->customDataType->typeNameFragments.back();
216
1/2
✓ Branch 26 → 27 taken 143 times.
✗ Branch 26 → 78 not taken.
286 SymbolTableEntry *interfaceFieldEntry = currentScope->insert("this." + interfaceName, interfaceNode);
217 143 interfaceFieldEntry->used = true;
218 143 interfaceFieldEntry->isImplicitField = true;
219 }
220 }
221
222 // Visit children
223
2/2
✓ Branch 35 → 36 taken 736 times.
✓ Branch 35 → 80 taken 1 time.
737 visitChildren(node);
224
225 // Leave the struct scope
226 736 currentScope = node->structScope->parent;
227
228 // Build struct qualifiers
229
2/2
✓ Branch 37 → 38 taken 574 times.
✓ Branch 37 → 55 taken 162 times.
736 if (const QualifierLstNode *qualifierLst = node->qualifierLst) {
230
2/2
✓ Branch 53 → 40 taken 574 times.
✓ Branch 53 → 54 taken 574 times.
1148 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
231
1/2
✓ Branch 41 → 42 taken 574 times.
✗ Branch 41 → 43 not taken.
574 if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
232 574 node->qualifiers.isPublic = true;
233 else
234 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on a struct definition");
235 }
236 }
237
238 // Add the struct to the symbol table
239
1/2
✓ Branch 55 → 56 taken 736 times.
✗ Branch 55 → 92 not taken.
736 node->entry = rootScope->insert(node->structName, node);
240 // Register the name in the exported name registry
241
1/2
✓ Branch 58 → 59 taken 736 times.
✗ Branch 58 → 92 not taken.
736 sourceFile->addNameRegistryEntry(node->structName, node->typeId, node->entry, node->structScope, true);
242
243
1/2
✓ Branch 59 → 60 taken 736 times.
✗ Branch 59 → 91 not taken.
1472 return nullptr;
244 737 }
245
246 108 std::any SymbolTableBuilder::visitInterfaceDef(InterfaceDefNode *node) {
247 // Visit attributes
248
2/2
✓ Branch 2 → 3 taken 79 times.
✓ Branch 2 → 6 taken 29 times.
108 if (node->attrs)
249
1/2
✓ Branch 3 → 4 taken 79 times.
✗ Branch 3 → 55 not taken.
79 visit(node->attrs);
250
251 // Check if this name already exists
252
3/4
✓ Branch 6 → 7 taken 108 times.
✗ Branch 6 → 83 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 17 taken 107 times.
216 if (rootScope->lookupStrict(node->interfaceName))
253
3/6
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 61 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 59 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 56 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->interfaceName + "'");
254
255 // Create scope for the interface
256
1/2
✓ Branch 18 → 19 taken 107 times.
✗ Branch 18 → 65 not taken.
107 const std::string &scopeName = Interface::getScopeName(node->interfaceName);
257
1/2
✓ Branch 20 → 21 taken 107 times.
✗ Branch 20 → 81 not taken.
107 node->interfaceScope = currentScope = rootScope->createChildScope(scopeName, ScopeType::INTERFACE, &node->codeLoc);
258
259 // Visit signatures
260
2/2
✓ Branch 28 → 23 taken 246 times.
✓ Branch 28 → 29 taken 107 times.
353 for (SignatureNode *signature : node->signatures)
261
1/2
✓ Branch 24 → 25 taken 246 times.
✗ Branch 24 → 68 not taken.
246 visit(signature);
262
263 // Leave the interface scope
264 107 currentScope = node->interfaceScope->parent;
265
266 // Build interface qualifiers
267
2/2
✓ Branch 29 → 30 taken 91 times.
✓ Branch 29 → 47 taken 16 times.
107 if (const QualifierLstNode *qualifierLst = node->qualifierLst) {
268
2/2
✓ Branch 45 → 32 taken 91 times.
✓ Branch 45 → 46 taken 91 times.
182 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
269
1/2
✓ Branch 33 → 34 taken 91 times.
✗ Branch 33 → 35 not taken.
91 if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
270 91 node->qualifiers.isPublic = true;
271 else
272 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on an interface definition");
273 }
274 }
275
276 // Add the interface to the symbol table
277
1/2
✓ Branch 47 → 48 taken 107 times.
✗ Branch 47 → 81 not taken.
107 node->entry = rootScope->insert(node->interfaceName, node);
278 // Register the name in the exported name registry
279
1/2
✓ Branch 50 → 51 taken 107 times.
✗ Branch 50 → 81 not taken.
107 sourceFile->addNameRegistryEntry(node->interfaceName, node->typeId, node->entry, node->interfaceScope, true);
280
281
1/2
✓ Branch 51 → 52 taken 107 times.
✗ Branch 51 → 80 not taken.
214 return nullptr;
282 107 }
283
284 68 std::any SymbolTableBuilder::visitEnumDef(EnumDefNode *node) {
285 // Check if this name already exists
286
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 67 times.
136 if (rootScope->lookupStrict(node->enumName))
287
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 43 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->enumName + "'");
288
289 // Create scope for the enum
290 67 node->enumScope = currentScope =
291
2/4
✓ Branch 13 → 14 taken 67 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 67 times.
✗ Branch 14 → 52 not taken.
67 rootScope->createChildScope(ENUM_SCOPE_PREFIX + node->enumName, ScopeType::ENUM, &node->codeLoc);
292
293 // Visit items
294
2/2
✓ Branch 16 → 17 taken 66 times.
✓ Branch 16 → 55 taken 1 time.
67 visit(node->itemLst);
295
296 // Leave the enum scope
297 66 currentScope = node->enumScope->parent;
298
299 // Build enum qualifiers
300
2/2
✓ Branch 18 → 19 taken 52 times.
✓ Branch 18 → 36 taken 14 times.
66 if (node->qualifierLst) {
301
2/2
✓ Branch 34 → 21 taken 52 times.
✓ Branch 34 → 35 taken 52 times.
104 for (const QualifierNode *qualifier : node->qualifierLst->qualifiers) {
302
1/2
✓ Branch 22 → 23 taken 52 times.
✗ Branch 22 → 24 not taken.
52 if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
303 52 node->qualifiers.isPublic = true;
304 else
305 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on an enum definition");
306 }
307 }
308
309 // Add the enum to the symbol table
310 66 node->entry = rootScope->insert(node->enumName, node);
311 // Register the name in the exported name registry
312 66 sourceFile->addNameRegistryEntry(node->enumName, node->typeId, node->entry, node->enumScope, true);
313
314
1/2
✓ Branch 40 → 41 taken 66 times.
✗ Branch 40 → 66 not taken.
66 return nullptr;
315 }
316
317 997 std::any SymbolTableBuilder::visitGenericTypeDef(GenericTypeDefNode *node) {
318 // Check if this name already exists
319
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 996 times.
1994 if (rootScope->lookupStrict(node->typeName))
320
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 24 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 19 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->typeName + "'");
321
322 // Create the generic type to the symbol table
323 996 node->entry = rootScope->insert(node->typeName, node);
324 996 node->entry->used = true; // Generic types are always used
325
326
1/2
✓ Branch 16 → 17 taken 996 times.
✗ Branch 16 → 28 not taken.
996 return nullptr;
327 }
328
329 71 std::any SymbolTableBuilder::visitAliasDef(AliasDefNode *node) {
330 // Check if this name already exists
331
3/4
✓ Branch 2 → 3 taken 71 times.
✗ Branch 2 → 65 not taken.
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 70 times.
142 if (rootScope->lookupStrict(node->aliasName))
332
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 43 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->aliasName + "'");
333
334 // Build alias qualifiers
335
2/2
✓ Branch 13 → 14 taken 21 times.
✓ Branch 13 → 31 taken 49 times.
70 if (const QualifierLstNode *qualifierLst = node->qualifierLst) {
336
2/2
✓ Branch 29 → 16 taken 21 times.
✓ Branch 29 → 30 taken 21 times.
42 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
337
1/2
✓ Branch 17 → 18 taken 21 times.
✗ Branch 17 → 19 not taken.
21 if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
338 21 node->qualifiers.isPublic = true;
339 else
340 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on an alias definition");
341 }
342 }
343
344 // Add the alias to the symbol table
345
1/2
✓ Branch 31 → 32 taken 70 times.
✗ Branch 31 → 65 not taken.
70 node->entry = rootScope->insert(node->aliasName, node);
346 // Register the name in the exported name registry
347
1/2
✓ Branch 34 → 35 taken 70 times.
✗ Branch 34 → 65 not taken.
70 sourceFile->addNameRegistryEntry(node->aliasName, node->typeId, node->entry, rootScope, true);
348
349 // Add another symbol for the aliased type container
350
1/2
✓ Branch 35 → 36 taken 70 times.
✗ Branch 35 → 65 not taken.
70 const std::string aliasedTypeContainerName = node->aliasName + ALIAS_CONTAINER_SUFFIX;
351
1/2
✓ Branch 36 → 37 taken 70 times.
✗ Branch 36 → 63 not taken.
70 node->aliasedTypeContainerEntry = rootScope->insert(aliasedTypeContainerName, node);
352
353
1/2
✓ Branch 39 → 40 taken 70 times.
✗ Branch 39 → 62 not taken.
140 return nullptr;
354 70 }
355
356 1201 std::any SymbolTableBuilder::visitGlobalVarDef(GlobalVarDefNode *node) {
357 // Check if this name already exists
358
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 1200 times.
2402 if (rootScope->lookupStrict(node->varName))
359
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 42 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 40 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 37 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->varName + "'");
360
361 // Check if global already exists in an imported source file
362
5/8
✓ Branch 13 → 14 taken 1200 times.
✗ Branch 13 → 55 not taken.
✓ Branch 14 → 15 taken 1200 times.
✗ Branch 14 → 55 not taken.
✓ Branch 15 → 16 taken 1200 times.
✗ Branch 15 → 55 not taken.
✓ Branch 29 → 17 taken 420 times.
✓ Branch 29 → 30 taken 1199 times.
1619 for (const auto &dependency : sourceFile->dependencies | std::views::values)
363
3/4
✓ Branch 18 → 19 taken 420 times.
✗ Branch 18 → 55 not taken.
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 27 taken 419 times.
420 if (dependency->exportedNameRegistry.contains(node->varName))
364
3/6
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 49 not taken.
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 46 not taken.
1 throw SemanticError(node, GLOBAL_DECLARED_TWICE, "Duplicate global variable '" + node->varName + "' in other module");
365
366 // Add the global to the symbol table
367 1199 node->entry = rootScope->insert(node->varName, node);
368 // Register the name in the exported name registry
369 1199 sourceFile->addNameRegistryEntry(node->varName, TY_INVALID, node->entry, currentScope, true);
370
371
1/2
✓ Branch 34 → 35 taken 1199 times.
✗ Branch 34 → 56 not taken.
1199 return nullptr;
372 }
373
374 1040 std::any SymbolTableBuilder::visitExtDecl(ExtDeclNode *node) {
375 // Visit attributes
376
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 6 taken 1039 times.
1040 if (node->attrs)
377
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 30 not taken.
1 visit(node->attrs);
378
379 // Check if this name already exists
380
2/2
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 17 taken 1039 times.
2080 if (rootScope->lookupStrict(node->extFunctionName))
381
3/6
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 36 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 34 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 31 not taken.
1 throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->extFunctionName + "'");
382
383 // Create scope for the external function (this is required in case of forceSubstantiation in FunctionManager::matchFunction)
384
2/4
✓ Branch 17 → 18 taken 1039 times.
✗ Branch 17 → 42 not taken.
✓ Branch 18 → 19 taken 1039 times.
✗ Branch 18 → 40 not taken.
1039 rootScope->createChildScope(node->getScopeId(), ScopeType::FUNC_PROC_BODY, &node->codeLoc);
385
386 // Add the external declaration to the symbol table
387 1039 node->entry = rootScope->insert(node->extFunctionName, node);
388 // Register the name in the exported name registry
389
2/2
✓ Branch 23 → 24 taken 679 times.
✓ Branch 23 → 25 taken 360 times.
1039 const uint64_t typeId = node->returnType ? TY_FUNCTION : TY_PROCEDURE;
390 1039 sourceFile->addNameRegistryEntry(node->extFunctionName, typeId, node->entry, rootScope, /*keepNewOnCollision=*/true);
391
392
1/2
✓ Branch 27 → 28 taken 1039 times.
✗ Branch 27 → 43 not taken.
1039 return nullptr;
393 }
394
395 2711 std::any SymbolTableBuilder::visitUnsafeBlock(UnsafeBlockNode *node) {
396 // Create scope for the unsafe block body
397 2711 node->bodyScope = currentScope =
398
2/4
✓ Branch 2 → 3 taken 2711 times.
✗ Branch 2 → 12 not taken.
✓ Branch 3 → 4 taken 2711 times.
✗ Branch 3 → 10 not taken.
2711 currentScope->createChildScope(node->getScopeId(), ScopeType::UNSAFE_BODY, &node->body->codeLoc);
399
400 // Visit body
401
1/2
✓ Branch 5 → 6 taken 2711 times.
✗ Branch 5 → 13 not taken.
2711 visit(node->body);
402
403 // Leave thread body scope
404 2711 currentScope = node->bodyScope->parent;
405
406
1/2
✓ Branch 7 → 8 taken 2711 times.
✗ Branch 7 → 14 not taken.
2711 return nullptr;
407 }
408
409 1450 std::any SymbolTableBuilder::visitForLoop(ForLoopNode *node) {
410 // Create scope for the loop body
411
2/4
✓ Branch 2 → 3 taken 1450 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 1450 times.
✗ Branch 3 → 12 not taken.
1450 node->bodyScope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::FOR_BODY, &node->body->codeLoc);
412
413 // Visit loop variable declaration
414
1/2
✓ Branch 5 → 6 taken 1450 times.
✗ Branch 5 → 15 not taken.
1450 visit(node->initDecl);
415
416 // Visit body
417
1/2
✓ Branch 7 → 8 taken 1450 times.
✗ Branch 7 → 16 not taken.
1450 visit(node->body);
418
419 // Leave for body scope
420 1450 currentScope = node->bodyScope->parent;
421
422
1/2
✓ Branch 9 → 10 taken 1450 times.
✗ Branch 9 → 17 not taken.
1450 return nullptr;
423 }
424
425 125 std::any SymbolTableBuilder::visitForeachLoop(ForeachLoopNode *node) {
426 // Create scope for the loop body
427 125 node->bodyScope = currentScope =
428
2/4
✓ Branch 2 → 3 taken 125 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 125 times.
✗ Branch 3 → 16 not taken.
125 currentScope->createChildScope(node->getScopeId(), ScopeType::FOREACH_BODY, &node->body->codeLoc);
429
430 // Visit index variable declaration
431
2/2
✓ Branch 5 → 6 taken 6 times.
✓ Branch 5 → 9 taken 119 times.
125 if (node->idxVarDecl)
432
1/2
✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 19 not taken.
6 visit(node->idxVarDecl);
433
434 // Visit item variable declaration
435
1/2
✓ Branch 9 → 10 taken 125 times.
✗ Branch 9 → 20 not taken.
125 visit(node->itemVarDecl);
436
437 // Visit body
438
1/2
✓ Branch 11 → 12 taken 125 times.
✗ Branch 11 → 21 not taken.
125 visit(node->body);
439
440 // Leave foreach body scope
441 125 currentScope = node->bodyScope->parent;
442
443
1/2
✓ Branch 13 → 14 taken 125 times.
✗ Branch 13 → 22 not taken.
125 return nullptr;
444 }
445
446 822 std::any SymbolTableBuilder::visitWhileLoop(WhileLoopNode *node) {
447 // Create scope for the loop body
448 822 node->bodyScope = currentScope =
449
2/4
✓ Branch 2 → 3 taken 822 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 822 times.
✗ Branch 3 → 12 not taken.
822 currentScope->createChildScope(node->getScopeId(), ScopeType::WHILE_BODY, &node->body->codeLoc);
450
451 // Visit condition
452
1/2
✓ Branch 5 → 6 taken 822 times.
✗ Branch 5 → 15 not taken.
822 visit(node->condition);
453
454 // Visit body
455
1/2
✓ Branch 7 → 8 taken 822 times.
✗ Branch 7 → 16 not taken.
822 visit(node->body);
456
457 // Leave while body scope
458 822 currentScope = node->bodyScope->parent;
459
460
1/2
✓ Branch 9 → 10 taken 822 times.
✗ Branch 9 → 17 not taken.
822 return nullptr;
461 }
462
463 9 std::any SymbolTableBuilder::visitDoWhileLoop(DoWhileLoopNode *node) {
464 // Create scope for the loop body
465 9 node->bodyScope = currentScope =
466
2/4
✓ Branch 2 → 3 taken 9 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 9 times.
✗ Branch 3 → 12 not taken.
9 currentScope->createChildScope(node->getScopeId(), ScopeType::WHILE_BODY, &node->body->codeLoc);
467
468 // Visit condition
469
1/2
✓ Branch 5 → 6 taken 9 times.
✗ Branch 5 → 15 not taken.
9 visit(node->condition);
470
471 // Visit body
472
1/2
✓ Branch 7 → 8 taken 9 times.
✗ Branch 7 → 16 not taken.
9 visit(node->body);
473
474 // Leave do-while body scope
475 9 currentScope = node->bodyScope->parent;
476
477
1/2
✓ Branch 9 → 10 taken 9 times.
✗ Branch 9 → 17 not taken.
9 return nullptr;
478 }
479
480 4408 std::any SymbolTableBuilder::visitIfStmt(IfStmtNode *node) {
481 // Create scope for the then body
482 4408 node->thenBodyScope = currentScope =
483
2/4
✓ Branch 2 → 3 taken 4408 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 4408 times.
✗ Branch 3 → 19 not taken.
4408 currentScope->createChildScope(node->getScopeId(), ScopeType::IF_ELSE_BODY, &node->thenBody->codeLoc);
484
485 // Visit condition
486
1/2
✓ Branch 5 → 6 taken 4408 times.
✗ Branch 5 → 22 not taken.
4408 visit(node->condition);
487
488 // Visit then body
489
2/2
✓ Branch 7 → 8 taken 4403 times.
✓ Branch 7 → 11 taken 5 times.
4408 if (node->compileThenBranch)
490
1/2
✓ Branch 8 → 9 taken 4403 times.
✗ Branch 8 → 23 not taken.
4403 visit(node->thenBody);
491
492 // Leave then body scope
493 4408 currentScope = node->thenBodyScope->parent;
494
495 // Visit else stmt
496
4/4
✓ Branch 11 → 12 taken 4405 times.
✓ Branch 11 → 16 taken 3 times.
✓ Branch 12 → 13 taken 249 times.
✓ Branch 12 → 16 taken 4156 times.
4408 if (node->compileElseBranch && node->elseStmt)
497
1/2
✓ Branch 13 → 14 taken 249 times.
✗ Branch 13 → 24 not taken.
249 visit(node->elseStmt);
498
499
1/2
✓ Branch 16 → 17 taken 4408 times.
✗ Branch 16 → 25 not taken.
4408 return nullptr;
500 }
501
502 249 std::any SymbolTableBuilder::visitElseStmt(ElseStmtNode *node) {
503 // Visit if statement in the case of an else if branch
504
2/2
✓ Branch 2 → 3 taken 73 times.
✓ Branch 2 → 7 taken 176 times.
249 if (node->isElseIf) {
505
1/2
✓ Branch 3 → 4 taken 73 times.
✗ Branch 3 → 15 not taken.
73 visit(node->ifStmt);
506
1/2
✓ Branch 5 → 6 taken 73 times.
✗ Branch 5 → 16 not taken.
73 return nullptr;
507 }
508
509 // Create scope for the else body
510 176 node->elseBodyScope = currentScope =
511
2/4
✓ Branch 7 → 8 taken 176 times.
✗ Branch 7 → 19 not taken.
✓ Branch 8 → 9 taken 176 times.
✗ Branch 8 → 17 not taken.
176 currentScope->createChildScope(node->getScopeId(), ScopeType::IF_ELSE_BODY, &node->body->codeLoc);
512
513 // Visit else body
514
1/2
✓ Branch 10 → 11 taken 176 times.
✗ Branch 10 → 20 not taken.
176 visit(node->body);
515
516 // Leave else body scope
517 176 currentScope = node->elseBodyScope->parent;
518
519
1/2
✓ Branch 12 → 13 taken 176 times.
✗ Branch 12 → 21 not taken.
176 return nullptr;
520 }
521
522 53 std::any SymbolTableBuilder::visitCaseBranch(CaseBranchNode *node) {
523 // Create scope for the case branch
524
2/4
✓ Branch 2 → 3 taken 53 times.
✗ Branch 2 → 12 not taken.
✓ Branch 3 → 4 taken 53 times.
✗ Branch 3 → 10 not taken.
53 node->bodyScope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::CASE_BODY, &node->body->codeLoc);
525
526 // Visit case body
527
1/2
✓ Branch 5 → 6 taken 53 times.
✗ Branch 5 → 13 not taken.
53 visit(node->body);
528
529 // Leave case body scope
530 53 currentScope = node->bodyScope->parent;
531
532
1/2
✓ Branch 7 → 8 taken 53 times.
✗ Branch 7 → 14 not taken.
53 return nullptr;
533 }
534
535 6 std::any SymbolTableBuilder::visitDefaultBranch(DefaultBranchNode *node) {
536 // Create scope for the default branch
537 6 node->bodyScope = currentScope =
538
2/4
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 12 not taken.
✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 10 not taken.
6 currentScope->createChildScope(node->getScopeId(), ScopeType::DEFAULT_BODY, &node->body->codeLoc);
539
540 // Visit default body
541
1/2
✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 13 not taken.
6 visit(node->body);
542
543 // Leave default body scope
544 6 currentScope = node->bodyScope->parent;
545
546
1/2
✓ Branch 7 → 8 taken 6 times.
✗ Branch 7 → 14 not taken.
6 return nullptr;
547 }
548
549 32 std::any SymbolTableBuilder::visitAnonymousBlockStmt(AnonymousBlockStmtNode *node) {
550 // Create scope for the anonymous block body
551 32 node->bodyScope = currentScope =
552
2/4
✓ Branch 2 → 3 taken 32 times.
✗ Branch 2 → 12 not taken.
✓ Branch 3 → 4 taken 32 times.
✗ Branch 3 → 10 not taken.
32 currentScope->createChildScope(node->getScopeId(), ScopeType::ANONYMOUS_BLOCK_BODY, &node->body->codeLoc);
553
554 // Visit body
555
1/2
✓ Branch 5 → 6 taken 32 times.
✗ Branch 5 → 13 not taken.
32 visit(node->body);
556
557 // Leave anonymous block body scope
558 32 currentScope = node->bodyScope->parent;
559
560
1/2
✓ Branch 7 → 8 taken 32 times.
✗ Branch 7 → 14 not taken.
32 return nullptr;
561 }
562
563 736 std::any SymbolTableBuilder::visitEnumItem(EnumItemNode *node) {
564 // Check if enum item already exists in the same scope.
565
3/4
✓ Branch 2 → 3 taken 736 times.
✗ Branch 2 → 41 not taken.
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 735 times.
1472 if (currentScope->lookupStrict(node->itemName))
566
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 31 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 29 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 26 not taken.
1 throw SemanticError(node, VARIABLE_DECLARED_TWICE, "The enum item '" + node->itemName + "' was declared more than once");
567
568 // Add enum item entry to symbol table
569
1/2
✓ Branch 13 → 14 taken 735 times.
✗ Branch 13 → 41 not taken.
735 SymbolTableEntry *enumItemEntry = currentScope->insert(node->itemName, node);
570
571 // Add external registry entry
572
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 735 times.
735 assert(node->enumDef != nullptr);
573
2/4
✓ Branch 18 → 19 taken 735 times.
✗ Branch 18 → 37 not taken.
✓ Branch 19 → 20 taken 735 times.
✗ Branch 19 → 35 not taken.
735 const std::string name = node->enumDef->enumName + SCOPE_ACCESS_TOKEN + node->itemName;
574
1/2
✓ Branch 21 → 22 taken 735 times.
✗ Branch 21 → 39 not taken.
735 sourceFile->addNameRegistryEntry(name, TY_INT, enumItemEntry, currentScope, true);
575
576
1/2
✓ Branch 22 → 23 taken 735 times.
✗ Branch 22 → 38 not taken.
1470 return nullptr;
577 735 }
578
579 1570 std::any SymbolTableBuilder::visitField(FieldNode *node) {
580 // Check if field already exists in the same scope.
581
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 1569 times.
3140 if (currentScope->lookupStrict(node->fieldName))
582
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 24 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 22 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 19 not taken.
1 throw SemanticError(node, VARIABLE_DECLARED_TWICE, "The field '" + node->fieldName + "' was declared more than once");
583
584 // Add field entry to symbol table
585 1569 currentScope->insert(node->fieldName, node);
586
587
1/2
✓ Branch 16 → 17 taken 1569 times.
✗ Branch 16 → 28 not taken.
1569 return nullptr;
588 }
589
590 246 std::any SymbolTableBuilder::visitSignature(SignatureNode *node) {
591 // Build signature qualifiers
592
2/2
✓ Branch 2 → 3 taken 14 times.
✓ Branch 2 → 24 taken 232 times.
246 if (const QualifierLstNode *qualifierLst = node->qualifierLst) {
593
2/2
✓ Branch 22 → 5 taken 14 times.
✓ Branch 22 → 23 taken 14 times.
28 for (const QualifierNode *qualifier : qualifierLst->qualifiers) {
594
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 14 times.
14 if (qualifier->type == QualifierNode::QualifierType::TY_INLINE)
595 node->signatureQualifiers.isInline = true;
596
1/2
✓ Branch 8 → 9 taken 14 times.
✗ Branch 8 → 10 not taken.
14 else if (qualifier->type == QualifierNode::QualifierType::TY_PUBLIC)
597 14 node->signatureQualifiers.isPublic = true;
598 else if (qualifier->type == QualifierNode::QualifierType::TY_CONST)
599 node->signatureQualifiers.isConst = true;
600 else
601 throw SemanticError(qualifier, QUALIFIER_AT_ILLEGAL_CONTEXT, "Cannot use this qualifier on a signature definition");
602 }
603 }
604
605 // Add signature entry to symbol table
606 246 node->entry = currentScope->insert(node->methodName, node);
607
608
1/2
✓ Branch 27 → 28 taken 246 times.
✗ Branch 27 → 40 not taken.
246 return nullptr;
609 }
610
611 21712 std::any SymbolTableBuilder::visitDeclStmt(DeclStmtNode *node) {
612 // Check if variable already exists in the same scope.
613
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 13 taken 21711 times.
43424 if (currentScope->lookupStrict(node->varName))
614
3/6
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 28 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 26 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 23 not taken.
1 throw SemanticError(node, VARIABLE_DECLARED_TWICE, "The variable '" + node->varName + "' was declared more than once");
615
616 // Visit the right side
617
2/2
✓ Branch 13 → 14 taken 7756 times.
✓ Branch 13 → 17 taken 13955 times.
21711 if (node->hasAssignment)
618
1/2
✓ Branch 14 → 15 taken 7756 times.
✗ Branch 14 → 32 not taken.
7756 visit(node->assignExpr);
619
620 // Add variable entry to symbol table
621 21711 SymbolTableEntry *varEntry = currentScope->insert(node->varName, node);
622 21711 varEntry->isParam = node->isFctParam;
623
624
1/2
✓ Branch 20 → 21 taken 21711 times.
✗ Branch 20 → 33 not taken.
21711 return nullptr;
625 }
626
627 369 std::any SymbolTableBuilder::visitModAttr(ModAttrNode *node) {
628 // Visit attributes
629
2/2
✓ Branch 2 → 3 taken 368 times.
✓ Branch 2 → 99 taken 1 time.
369 visitChildren(node);
630
631 // Retrieve attributes
632 368 const AttrLstNode *attrs = node->attrLst;
633
634 // Collect linker flags
635 368 std::vector<const CompileTimeValue *> linkerFlagValues;
636 // core.linker.flag
637
2/4
✓ Branch 6 → 7 taken 368 times.
✗ Branch 6 → 102 not taken.
✓ Branch 7 → 8 taken 368 times.
✗ Branch 7 → 100 not taken.
736 std::vector<const CompileTimeValue *> values = attrs->getAttrValuesByName(ATTR_CORE_LINKER_FLAG);
638
1/2
✓ Branch 14 → 15 taken 368 times.
✗ Branch 14 → 106 not taken.
368 linkerFlagValues.insert(linkerFlagValues.end(), values.begin(), values.end());
639 // core.linux.linker.flag
640 368 const llvm::Triple &targetTriple = sourceFile->targetMachine->getTargetTriple();
641
2/2
✓ Branch 18 → 19 taken 358 times.
✓ Branch 18 → 33 taken 10 times.
368 if (targetTriple.isOSLinux()) {
642
2/4
✓ Branch 21 → 22 taken 358 times.
✗ Branch 21 → 110 not taken.
✓ Branch 22 → 23 taken 358 times.
✗ Branch 22 → 108 not taken.
716 values = attrs->getAttrValuesByName(ATTR_CORE_LINUX_LINKER_FLAG);
643
1/2
✓ Branch 31 → 32 taken 358 times.
✗ Branch 31 → 115 not taken.
358 linkerFlagValues.insert(linkerFlagValues.end(), values.begin(), values.end());
644 }
645 // core.darwin.linker.flag
646
3/4
✓ Branch 33 → 34 taken 368 times.
✗ Branch 33 → 155 not taken.
✓ Branch 34 → 35 taken 5 times.
✓ Branch 34 → 49 taken 363 times.
368 if (targetTriple.isOSDarwin()) {
647
2/4
✓ Branch 37 → 38 taken 5 times.
✗ Branch 37 → 119 not taken.
✓ Branch 38 → 39 taken 5 times.
✗ Branch 38 → 117 not taken.
10 values = attrs->getAttrValuesByName(ATTR_CORE_DARWIN_LINKER_FLAG);
648
1/2
✓ Branch 47 → 48 taken 5 times.
✗ Branch 47 → 124 not taken.
5 linkerFlagValues.insert(linkerFlagValues.end(), values.begin(), values.end());
649 }
650 // core.windows.linker.flag
651
2/2
✓ Branch 50 → 51 taken 5 times.
✓ Branch 50 → 65 taken 363 times.
368 if (targetTriple.isOSWindows()) {
652
2/4
✓ Branch 53 → 54 taken 5 times.
✗ Branch 53 → 128 not taken.
✓ Branch 54 → 55 taken 5 times.
✗ Branch 54 → 126 not taken.
10 values = attrs->getAttrValuesByName(ATTR_CORE_WINDOWS_LINKER_FLAG);
653
1/2
✓ Branch 63 → 64 taken 5 times.
✗ Branch 63 → 133 not taken.
5 linkerFlagValues.insert(linkerFlagValues.end(), values.begin(), values.end());
654 }
655
2/2
✓ Branch 72 → 67 taken 257 times.
✓ Branch 72 → 73 taken 368 times.
625 for (const CompileTimeValue *value : linkerFlagValues)
656
2/4
✓ Branch 68 → 69 taken 257 times.
✗ Branch 68 → 135 not taken.
✓ Branch 69 → 70 taken 257 times.
✗ Branch 69 → 135 not taken.
257 resourceManager.linker.addLinkerFlag(resourceManager.compileTimeStringValues.at(value->stringValueOffset));
657
658 // core.linker.additionalSource
659
4/6
✓ Branch 75 → 76 taken 368 times.
✗ Branch 75 → 138 not taken.
✓ Branch 76 → 77 taken 368 times.
✗ Branch 76 → 136 not taken.
✓ Branch 92 → 81 taken 3 times.
✓ Branch 92 → 93 taken 367 times.
1106 for (const CompileTimeValue *value : attrs->getAttrValuesByName(ATTR_CORE_LINKER_ADDITIONAL_SOURCE)) {
660
1/2
✓ Branch 82 → 83 taken 3 times.
✗ Branch 82 → 151 not taken.
3 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
661
5/8
✓ Branch 83 → 84 taken 3 times.
✗ Branch 83 → 149 not taken.
✓ Branch 84 → 85 taken 3 times.
✗ Branch 84 → 146 not taken.
✓ Branch 85 → 86 taken 3 times.
✗ Branch 85 → 144 not taken.
✓ Branch 86 → 87 taken 2 times.
✓ Branch 86 → 142 taken 1 time.
6 resourceManager.linker.addAdditionalSourcePath(sourceFile->filePath.parent_path() / stringValue);
662 368 }
663
664
1/2
✓ Branch 94 → 95 taken 367 times.
✗ Branch 94 → 154 not taken.
734 return nullptr;
665 369 }
666
667 1207 std::any SymbolTableBuilder::visitAttr(AttrNode *node) {
668 // Check if this attribute exists
669
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 11 taken 1206 times.
1207 if (!ATTR_CONFIGS.contains(node->key))
670
3/6
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 37 not taken.
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 35 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 32 not taken.
1 throw SemanticError(node, UNKNOWN_ATTR, "Unknown attribute '" + node->key + "'");
671
672 // Check if the target is correct
673 1206 const auto &[target, type] = ATTR_CONFIGS.at(node->key);
674
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 20 taken 1205 times.
1206 if ((node->target & target) == 0)
675
3/6
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 46 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 44 not taken.
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 41 not taken.
1 throw SemanticError(node, INVALID_ATTR_TARGET, "Attribute '" + node->key + "' cannot be used on this target");
676
677 // Check if a value is present
678
4/4
✓ Branch 20 → 21 taken 340 times.
✓ Branch 20 → 29 taken 865 times.
✓ Branch 21 → 22 taken 1 time.
✓ Branch 21 → 29 taken 339 times.
1205 if (!node->value && type != AttrNode::AttrType::TYPE_BOOL)
679
3/6
✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 55 not taken.
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 53 not taken.
✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 50 not taken.
1 throw SemanticError(node, MISSING_ATTR_VALUE, "Attribute '" + node->key + "' requires a value");
680
681
1/2
✓ Branch 29 → 30 taken 1204 times.
✗ Branch 29 → 59 not taken.
1204 return nullptr;
682 }
683
684 16 std::any SymbolTableBuilder::visitLambdaFunc(LambdaFuncNode *node) {
685 // Create scope for the lambda body
686 16 const CodeLoc &codeLoc = node->body->codeLoc;
687
2/4
✓ Branch 2 → 3 taken 16 times.
✗ Branch 2 → 46 not taken.
✓ Branch 3 → 4 taken 16 times.
✗ Branch 3 → 44 not taken.
16 node->bodyScope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::LAMBDA_BODY, &codeLoc);
688 // Requires capturing because the LLVM IR will end up in a separate function
689 16 currentScope->symbolTable.setCapturingRequired();
690 // Set to async scope if this is an async lambda
691
4/18
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 13 taken 16 times.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 47 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 47 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 16 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 19 taken 16 times.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 27 taken 16 times.
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 54 not taken.
16 if (node->lambdaAttr && node->lambdaAttr->attrLst->hasAttr(ATTR_ASYNC))
692 node->bodyScope->isAsyncScope = node->lambdaAttr->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue;
693
694 // Create symbol for 'result' variable
695
1/2
✓ Branch 29 → 30 taken 16 times.
✗ Branch 29 → 64 not taken.
48 currentScope->insert(RETURN_VARIABLE_NAME, node);
696
697 // Create symbols for the parameters
698
2/2
✓ Branch 35 → 36 taken 12 times.
✓ Branch 35 → 39 taken 4 times.
16 if (node->hasParams)
699
1/2
✓ Branch 36 → 37 taken 12 times.
✗ Branch 36 → 68 not taken.
12 visit(node->paramLst);
700
701 // Visit body
702
1/2
✓ Branch 39 → 40 taken 16 times.
✗ Branch 39 → 69 not taken.
16 visit(node->body);
703
704 // Leave anonymous block body scope
705 16 currentScope = node->bodyScope->parent;
706
707
1/2
✓ Branch 41 → 42 taken 16 times.
✗ Branch 41 → 70 not taken.
16 return nullptr;
708 }
709
710 31 std::any SymbolTableBuilder::visitLambdaProc(LambdaProcNode *node) {
711 // Create scope for the lambda body
712 31 const CodeLoc &codeLoc = node->body->codeLoc;
713
2/4
✓ Branch 2 → 3 taken 31 times.
✗ Branch 2 → 38 not taken.
✓ Branch 3 → 4 taken 31 times.
✗ Branch 3 → 36 not taken.
31 node->bodyScope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::LAMBDA_BODY, &codeLoc);
714 // Requires capturing because the LLVM IR will end up in a separate function
715 31 currentScope->symbolTable.setCapturingRequired();
716 // Set to async scope if this is an async lambda
717
11/18
✓ Branch 6 → 7 taken 16 times.
✓ Branch 6 → 13 taken 15 times.
✓ Branch 9 → 10 taken 16 times.
✗ Branch 9 → 39 not taken.
✓ Branch 10 → 11 taken 16 times.
✗ Branch 10 → 39 not taken.
✓ Branch 11 → 12 taken 16 times.
✗ Branch 11 → 13 not taken.
✓ Branch 14 → 15 taken 16 times.
✓ Branch 14 → 16 taken 15 times.
✓ Branch 16 → 17 taken 16 times.
✓ Branch 16 → 19 taken 15 times.
✓ Branch 19 → 20 taken 16 times.
✓ Branch 19 → 27 taken 15 times.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
63 if (node->lambdaAttr && node->lambdaAttr->attrLst->hasAttr(ATTR_ASYNC))
718
2/4
✓ Branch 22 → 23 taken 16 times.
✗ Branch 22 → 50 not taken.
✓ Branch 23 → 24 taken 16 times.
✗ Branch 23 → 48 not taken.
48 node->bodyScope->isAsyncScope = node->lambdaAttr->attrLst->getAttrValueByName(ATTR_ASYNC)->boolValue;
719
720 // Create symbols for the parameters
721
2/2
✓ Branch 27 → 28 taken 11 times.
✓ Branch 27 → 31 taken 20 times.
31 if (node->hasParams)
722
1/2
✓ Branch 28 → 29 taken 11 times.
✗ Branch 28 → 54 not taken.
11 visit(node->paramLst);
723
724 // Visit body
725
1/2
✓ Branch 31 → 32 taken 31 times.
✗ Branch 31 → 55 not taken.
31 visit(node->body);
726
727 // Leave anonymous block body scope
728 31 currentScope = node->bodyScope->parent;
729
730
1/2
✓ Branch 33 → 34 taken 31 times.
✗ Branch 33 → 56 not taken.
31 return nullptr;
731 }
732
733 1 std::any SymbolTableBuilder::visitLambdaExpr(LambdaExprNode *node) {
734 // Create scope for the anonymous block body
735 1 const CodeLoc &codeLoc = node->lambdaExpr->codeLoc;
736
2/4
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 15 not taken.
1 node->bodyScope = currentScope = currentScope->createChildScope(node->getScopeId(), ScopeType::LAMBDA_BODY, &codeLoc);
737 // Requires capturing because the LLVM IR will end up in a separate function
738 1 currentScope->symbolTable.setCapturingRequired();
739
740 // Create symbols for the parameters
741
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 10 not taken.
1 if (node->hasParams)
742
1/2
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 18 not taken.
1 visit(node->paramLst);
743
744 // Visit lambda expression
745
1/2
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 19 not taken.
1 visit(node->lambdaExpr);
746
747 // Leave anonymous block body scope
748 1 currentScope = node->bodyScope->parent;
749
750
1/2
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 20 not taken.
1 return nullptr;
751 }
752
753 } // namespace spice::compiler
754