Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "CompilerPass.h" | ||
4 | |||
5 | #include <SourceFile.h> | ||
6 | #include <global/GlobalResourceManager.h> | ||
7 | #include <symboltablebuilder/Scope.h> | ||
8 | |||
9 | namespace spice::compiler { | ||
10 | |||
11 | 9402 | CompilerPass::CompilerPass(GlobalResourceManager &resourceManager, SourceFile *sourceFile) | |
12 | 9402 | : resourceManager(resourceManager), cliOptions(resourceManager.cliOptions), sourceFile(sourceFile), | |
13 |
2/2✓ Branch 0 taken 9401 times.
✓ Branch 1 taken 1 times.
|
9402 | rootScope(sourceFile != nullptr ? sourceFile->globalScope.get() : nullptr), currentScope(rootScope) {} |
14 | |||
15 | /** | ||
16 | * Change to the passed scope. | ||
17 | * For nested scopes in generic functions/procedures it is important to have the right parent for symbol lookups | ||
18 | * Therefore, changeToScope sets the children's parent to the old scope to always have the right parent | ||
19 | * | ||
20 | * @param scope Scope to change to | ||
21 | * @param scopeType Expected type of the given scope | ||
22 | */ | ||
23 | 28372 | void CompilerPass::changeToScope(Scope *scope, [[maybe_unused]] ScopeType scopeType) { | |
24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28372 times.
|
28372 | assert(scope != nullptr); |
25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28372 times.
|
28372 | assert(scope->type == scopeType); |
26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28372 times.
|
28372 | assert(!scope->isGenericScope); |
27 | // Adjust members of the new scope | ||
28 | 28372 | scope->parent = currentScope; | |
29 | 28372 | scope->symbolTable.parent = ¤tScope->symbolTable; | |
30 | // Set the scope | ||
31 | 28372 | currentScope = scope; | |
32 | 28372 | } | |
33 | |||
34 | /** | ||
35 | * Change to the scope with the given name. | ||
36 | * | ||
37 | * @param scopeName Name of the scope to change to | ||
38 | * @param scopeType Expected type of the given scope | ||
39 | */ | ||
40 | 4914 | void CompilerPass::changeToScope(const std::string &scopeName, ScopeType scopeType) { | |
41 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4914 times.
|
4914 | assert(!scopeName.empty()); |
42 | 4914 | changeToScope(currentScope->getChildScope(scopeName), scopeType); | |
43 | 4914 | } | |
44 | |||
45 | /** | ||
46 | * Change to the parent scope of the current. | ||
47 | * | ||
48 | * @param oldScopeType Expected type of the scope to leave | ||
49 | */ | ||
50 | 12536 | void CompilerPass::changeToParentScope([[maybe_unused]] ScopeType oldScopeType) { | |
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12536 times.
|
12536 | assert(currentScope->type == oldScopeType); |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12536 times.
|
12536 | assert(currentScope->parent != nullptr); |
53 | 12536 | currentScope = currentScope->parent; | |
54 | 12536 | } | |
55 | |||
56 | } // namespace spice::compiler | ||
57 |