src/symboltablebuilder/ScopeHandle.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | #include <util/DeferredLogic.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | // Forward declarations | ||
| 13 | class CompilerPass; | ||
| 14 | class IRGenerator; | ||
| 15 | class Scope; | ||
| 16 | enum class ScopeType : uint8_t; | ||
| 17 | class ASTNode; | ||
| 18 | class ExprNode; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * This class is used to change the current scope to a child scope and change it back to the parent scope when the handle object | ||
| 22 | * is destroyed. This can be used in visit functions to automatically leave the scope when the function returns. | ||
| 23 | */ | ||
| 24 | class ScopeHandle : DeferredLogic { | ||
| 25 | public: | ||
| 26 | // Constructors | ||
| 27 | ScopeHandle(CompilerPass *pass, Scope *childScope, const ScopeType &scopeType); | ||
| 28 | ScopeHandle(CompilerPass *pass, const std::string &childScopeId, const ScopeType &scopeType); | ||
| 29 | ScopeHandle(IRGenerator *generator, Scope *childScope, const ScopeType &scopeType, const ASTNode *node); | ||
| 30 | ScopeHandle(IRGenerator *generator, const std::string &childScopeId, const ScopeType &scopeType, const ASTNode *node); | ||
| 31 | |||
| 32 | // Public methods | ||
| 33 | 72366 | void leaveScopeEarly() { execute(); } | |
| 34 | }; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * This class enters the expression scope of an expression, if it has one, and leaves it again when the handle object is | ||
| 38 | * destroyed. | ||
| 39 | * | ||
| 40 | * An expression scope holds the temporaries of an expression that is evaluated as a unit: a condition or an operand that is only | ||
| 41 | * evaluated conditionally. This allows destructing the temporaries right after the expression was evaluated, on every path. | ||
| 42 | * If the expression has no scope, the handle does nothing and the temporaries stay in the current scope. | ||
| 43 | */ | ||
| 44 | class ExprScopeHandle : DeferredLogic { | ||
| 45 | public: | ||
| 46 | // Constructors | ||
| 47 | ExprScopeHandle(CompilerPass *pass, const ExprNode *expr); | ||
| 48 | |||
| 49 | // Public methods | ||
| 50 | 120634 | [[nodiscard]] Scope *getExprScope() const { return exprScope; } | |
| 51 | |||
| 52 | private: | ||
| 53 | // Private constructors | ||
| 54 | ExprScopeHandle(CompilerPass *pass, Scope *exprScope); | ||
| 55 | |||
| 56 | // Private members | ||
| 57 | Scope *exprScope; | ||
| 58 | }; | ||
| 59 | |||
| 60 | } // namespace spice::compiler | ||
| 61 |