slang::ast::EvalContext class

A container for all context required to evaluate a statement or expression.

Mostly this involves tracking the callstack and maintaining storage for local variables.

Public types

struct Frame
Represents a single frame in the call stack.

Constructors, destructors, conversion operators

EvalContext(const ASTContext& astCtx, bitmask<EvalFlags> flags = {}) explicit
Constructs a new EvalContext instance.

Public functions

auto getCompilation() const -> Compilation&
Gets the compilation associated with the context.
void reset()
Resets the evaluation context back to an initial constructed state.
auto createLocal(const ValueSymbol* symbol, ConstantValue value = nullptr) -> ConstantValue*
Creates storage for a local variable in the current frame.
auto findLocal(const ValueSymbol* symbol) -> ConstantValue*
Gets the current value for the given local variable symbol.
void deleteLocal(const ValueSymbol* symbol)
Removes a previously created local.
auto pushFrame(const SubroutineSymbol& subroutine, SourceLocation callLocation, LookupLocation lookupLocation) -> bool
Push a new frame onto the call stack.
void pushEmptyFrame()
Pushes an empty frame onto the call stack.
void popFrame()
Pop the active frame from the call stack.
void pushLValue(LValue& lval)
Pushes an lvalue onto the stack for later reference during evaluation.
void popLValue()
Pops the top of the lvalue stack (requires that the stack be non-empty).
auto getTopLValue() const -> LValue*
Gets the top of the lvalue stack, or nullptr if the stack is empty.
auto step(SourceLocation loc) -> bool
Records the fact that we are executing another statement.
auto inFunction() const -> bool
Returns true if the context is currently within a function call, and false if this is a top-level expression.
auto cacheResults() const -> bool
Indicates whether the results of evaluating expressions using this context can be cached in each expression's constant pointer.
auto disableCaching() -> auto
If result caching is enabled, this method disables it and returns an object that when destructed will restore the previous caching mode.
auto topFrame() const -> const Frame&
Gets the top of the call stack.
auto getDisableTarget() const -> const Symbol*
If a disable statement has been evaluated, this returns a pointer to the block that should be disabled (presumed to be higher up in the stack).
auto getDisableRange() const -> SourceRange
If a disable statement has been evaluated, this returns the source range denoting where that statement occurred.
void setDisableTarget(const Symbol* symbol, SourceRange range)
Sets the target block that should be disabled, based on evaluating a disable statement.
void setQueueTarget(const ConstantValue* cv)
Sets the target queue value for use with unbounded '$' expressions.
auto getQueueTarget() const -> const ConstantValue*
Gets the target queue value for use with unbounded '$' expressions.
auto dumpStack() const -> std::string
Dumps the contents of the call stack to a string for debugging.
auto getAllDiagnostics() const -> Diagnostics
Gets the set of diagnostics that have been produced during constant evaluation.
auto addDiag(DiagCode code, SourceLocation location) -> Diagnostic&
Records a diagnostic under the current evaluation context.
auto addDiag(DiagCode code, SourceRange range) -> Diagnostic&
Records a diagnostic under the current evaluation context.
void reportAllDiags()
Issues all recorded diagnostics to the associated AST context.
void reportWarnings()
Issues only warnings to the associated AST context.
void reportStack(Diagnostic& diag) const
Reports the current function call stack as notes to the given diagnostic.

Public variables

ASTContext astCtx
The AST context in which the evaluation is occurring.
bitmask<EvalFlags> flags
Flags that control evaluation.

Function documentation

ConstantValue* slang::ast::EvalContext::findLocal(const ValueSymbol* symbol)

Gets the current value for the given local variable symbol.

Returns nullptr if the symbol cannot be found.

void slang::ast::EvalContext::deleteLocal(const ValueSymbol* symbol)

Removes a previously created local.

Pointers to the local's storage will be invalidated.

void slang::ast::EvalContext::pushEmptyFrame()

Pushes an empty frame onto the call stack.

Intended for use with on-the-fly evaluation in a scripting context.

void slang::ast::EvalContext::pushLValue(LValue& lval)

Pushes an lvalue onto the stack for later reference during evaluation.

NOTE: the lvalue storage must remain alive for as long as it remains on the eval context's lvalue stack.

bool slang::ast::EvalContext::step(SourceLocation loc)

Records the fact that we are executing another statement.

This is used to count the number of statements executed so far to detect if we've been evaluating a single constant function for too long.

auto slang::ast::EvalContext::disableCaching()

If result caching is enabled, this method disables it and returns an object that when destructed will restore the previous caching mode.

Otherwise does nothing.

const Symbol* slang::ast::EvalContext::getDisableTarget() const

If a disable statement has been evaluated, this returns a pointer to the block that should be disabled (presumed to be higher up in the stack).

Otherwise returns nullptr.

SourceRange slang::ast::EvalContext::getDisableRange() const

If a disable statement has been evaluated, this returns the source range denoting where that statement occurred.

Otherwise returns an empty range.

void slang::ast::EvalContext::setDisableTarget(const Symbol* symbol, SourceRange range)

Sets the target block that should be disabled, based on evaluating a disable statement.

This can be set to nullptr to clear out the target once it has been found the disable is completed.

void slang::ast::EvalContext::setQueueTarget(const ConstantValue* cv)

Sets the target queue value for use with unbounded '$' expressions.

If set to nullptr, the target is cleared.

const ConstantValue* slang::ast::EvalContext::getQueueTarget() const

Gets the target queue value for use with unbounded '$' expressions.

Returns nullptr if there is no queue target active.