Template Class ScopeStack#

Class Documentation#

template<typename T>
class ScopeStack#

A scope stack for maintaining hierarchical state during IR visiting.

During IR tree traversal, visitors often need to track scope-local state (e.g., active constraints, variable bindings) that should be automatically cleaned up when leaving a scope. ScopeStack provides this via WithNewScope, which pushes a new element on entry and pops it on exit.

ScopeStack<std::vector<Var>> definitions;

definitions.WithNewScope([&]() {
  definitions.Current().push_back(var);
  Visit(body);
});
Template Parameters:

T – The element type stored on the stack. Must be default-constructible.

Public Functions

inline ScopeStack()#

Construct with one initial scope level.

inline auto rbegin() const#

Iterate active scopes from innermost to outermost.

inline auto rend() const#
inline size_t size() const#

Return the number of active scopes.

inline bool empty() const#

Return true if no scopes are active.

inline T &Current()#

Access the current (innermost) scope element.

The returned reference is stable across push_back/pop_back because std::deque guarantees pointer stability for these operations.

Returns:

Mutable reference to the top element.

inline const T &Current() const#

Const access to the current (innermost) scope element.

template<typename F>
inline decltype(body()) WithNewScope(F &&body)#

Execute body within a new scope.

Pushes a new T onto the stack, executes the body, then pops it.

Parameters:

body – A callable to execute within the scope.

Returns:

The return value of body(), if non-void.