slang::ast::ASTContext class

Contains required context for binding syntax nodes with symbols to form an AST.

Expressions, statements, timing controls, constraints, and assertion items all use this for creation.

Public types

struct RandomizeDetails
A collection of information needed to bind names inside inline constraint blocks for class and scope randomize function calls.

Constructors, destructors, conversion operators

ASTContext(const Scope& scope, LookupLocation lookupLocation, bitmask<ASTFlags> flags = ASTFlags::None)
Constructs a new ASTContext instance.

Public functions

auto getCompilation() const -> Compilation&
Gets the parent compilation associated with the AST.
auto getLocation() const -> LookupLocation
Gets the lookup location passed to the ASTContext constructor.
auto inUnevaluatedBranch() const -> bool
Indicates whether the AST creation is happening inside an unevaluated branch.
auto getDriverKind() const -> DriverKind
Indicates the kind of driver that each assignment expression created using this context should use.
auto getInstance() const -> const InstanceSymbolBase*
Gets the parent instance if this context is being used to bind expressions for an instantiation.
auto getProceduralBlock() const -> const ProceduralBlockSymbol*
If this context is within a procedural block, returns a pointer to that symbol.
auto getContainingSubroutine() const -> const SubroutineSymbol*
If this context is within a subroutine, returns a pointer to that subroutine.
auto inAlwaysCombLatch() const -> bool
Indicates whether AST creation is happening within an always_comb or always_latch procedure.
void setInstance(const InstanceSymbolBase& inst)
Sets the parent instance associated with the context.
void setProceduralBlock(const ProceduralBlockSymbol& block)
Sets the procedural block associated with the context.
void clearInstanceAndProc()
Clears the parent instance and parent procedural block symbol associated with the context.
auto tryFillAssertionDetails() -> const Symbol*
Tries to fill the assertionInstance member by searching upward through parent scopes to find an assertion instantiation.
void setAttributes(const Statement& stmt, std::span<const syntax::AttributeInstanceSyntax*const> syntax) const
Registers attributes for the given statement.
void setAttributes(const Expression& expr, std::span<const syntax::AttributeInstanceSyntax*const> syntax) const
Registers attributes for the given expression.
void addDriver(const ValueSymbol& symbol, const Expression& longestStaticPrefix, bitmask<AssignFlags> assignFlags) const
Registers a driver for the given symbol.
auto getContainingSymbol() const -> const Symbol&
Gets the symbol that contains the AST context.
auto addDiag(DiagCode code, SourceLocation location) const -> Diagnostic&
Issues a new diagnostic.
auto addDiag(DiagCode code, SourceRange sourceRange) const -> Diagnostic&
Issues a new diagnostic.
auto requireIntegral(const Expression& expr) const -> bool
Reports an error if the given expression is not integral.
auto requireIntegral(const ConstantValue& cv, SourceRange range) const -> bool
Reports an error if the given constant value is not integral.
auto requireNoUnknowns(const SVInt& value, SourceRange range) const -> bool
Reports an error if the given integer has unknowns.
auto requirePositive(const SVInt& value, SourceRange range) const -> bool
Reports an error if the given integer is not positive or zero.
auto requirePositive(std::optional<int32_t> value, SourceRange range) const -> bool
Reports an error if the given integer is not positive or zero.
auto requireGtZero(std::optional<int32_t> value, SourceRange range) const -> bool
Reports an error if the given integer is not greater than zero.
auto requireBooleanConvertible(const Expression& expr) const -> bool
Reports an error if the given expression's type is not boolean convertible.
auto requireValidBitWidth(bitwidth_t width, SourceRange range) const -> bool
Reports an error if the given width is a valid bit width.
auto requireTimingAllowed(SourceRange range) const -> bool
Reports an error if time-controlling statements are not allowed in this context.
auto requireValidBitWidth(const SVInt& value, SourceRange range) const -> std::optional<bitwidth_t>
Reports an error if the given width is a valid bit width.
auto eval(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const -> ConstantValue
Evaluates the provided expression as a constant expression.
auto tryEval(const Expression& expr) const -> ConstantValue
Evaluates the provided expression as a constant expression.
auto evalInteger(const syntax::ExpressionSyntax& syntax, bitmask<ASTFlags> extraFlags = {}) const -> std::optional<int32_t>
Evaluates the provided expression as an integral constant expression.
auto evalInteger(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const -> std::optional<int32_t>
Evaluates the provided expression as an integral constant expression.
auto evalDimension(const syntax::VariableDimensionSyntax& syntax, bool requireRange, bool isPacked) const -> EvaluatedDimension
Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
auto evalPackedDimension(const syntax::VariableDimensionSyntax& syntax) const -> EvaluatedDimension
Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
auto evalPackedDimension(const syntax::ElementSelectSyntax& syntax) const -> EvaluatedDimension
Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
auto evalUnpackedDimension(const syntax::VariableDimensionSyntax& syntax) const -> EvaluatedDimension
Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
auto requireSimpleExpr(const syntax::PropertyExprSyntax& expr) const -> const syntax::ExpressionSyntax*
Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.
auto requireSimpleExpr(const syntax::PropertyExprSyntax& expr, DiagCode code) const -> const syntax::ExpressionSyntax*
Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.
auto getRandMode(const Symbol& symbol) const -> RandMode
Gets the rand mode for the given symbol, taking into account any randomize scope that may be active in this context.
void addAssertionBacktrace(Diagnostic& diag) const
If this context is within an assertion instance, report a backtrace of how that instance was expanded to the given diagnostic; otherwise, do nothing.
auto resetFlags(bitmask<ASTFlags> addedFlags) const -> ASTContext
Resets the flags for this context; the addedFlags will be added to the flag set, and any non-sticky flags will be removed.

Public variables

not_null<const Scope*> scope
The scope where the AST creation is occurring.
SymbolIndex lookupIndex
The location to use when looking up names.
bitmask<ASTFlags> flags
Various flags that control how AST creation is performed.
const TempVarSymbol* firstTempVar
If any temporary variables have been materialized in this context, contains a pointer to the first one along with a linked list of any others that may be active.
const RandomizeDetails* randomizeDetails
If this context is for creating an inline constraint block for a randomize function call, this points to information about the scope.
const AssertionInstanceDetails* assertionInstance
If this context is for creating an instantiation of a sequence or property this points to information about that instantiation.

Function documentation

slang::ast::ASTContext::ASTContext(const Scope& scope, LookupLocation lookupLocation, bitmask<ASTFlags> flags = ASTFlags::None)

Constructs a new ASTContext instance.

Parameters
scope The scope in which the AST is being created
lookupLocation The lookup location within the parent scope where lookups are being performed.
flags Flags that control AST creation

const Symbol* slang::ast::ASTContext::tryFillAssertionDetails()

Tries to fill the assertionInstance member by searching upward through parent scopes to find an assertion instantiation.

Returns The nearest parent instantiation, instance body or checker instance, or nullptr if neither is found.

void slang::ast::ASTContext::addDriver(const ValueSymbol& symbol, const Expression& longestStaticPrefix, bitmask<AssignFlags> assignFlags) const

Registers a driver for the given symbol.

Parameters
symbol The symbol that is being driven
longestStaticPrefix The portion of the symbol that is being driven
assignFlags Flags that specify how the driver functions

const Symbol& slang::ast::ASTContext::getContainingSymbol() const

Gets the symbol that contains the AST context.

Returns Either a parent procedural block or subroutine if one is registered, and if not the scope passed to the ASTContext constructor.

bool slang::ast::ASTContext::requireIntegral(const Expression& expr) const

Reports an error if the given expression is not integral.

Returns true if the expression is integral and false otherwise

bool slang::ast::ASTContext::requireIntegral(const ConstantValue& cv, SourceRange range) const

Reports an error if the given constant value is not integral.

Returns true if the value is integral and false otherwise

bool slang::ast::ASTContext::requireNoUnknowns(const SVInt& value, SourceRange range) const

Reports an error if the given integer has unknowns.

Returns false if the value has unknowns and true otherwise

bool slang::ast::ASTContext::requirePositive(const SVInt& value, SourceRange range) const

Reports an error if the given integer is not positive or zero.

Returns true if the value is positive or zero and false otherwise

bool slang::ast::ASTContext::requirePositive(std::optional<int32_t> value, SourceRange range) const

Reports an error if the given integer is not positive or zero.

Returns true if the value is set and positive or zero and false otherwise

If value is unset, this method does not report an error.

bool slang::ast::ASTContext::requireGtZero(std::optional<int32_t> value, SourceRange range) const

Reports an error if the given integer is not greater than zero.

Returns true if the value is set and greater than zero and false otherwise

If value is unset, this method does not report an error.

bool slang::ast::ASTContext::requireBooleanConvertible(const Expression& expr) const

Reports an error if the given expression's type is not boolean convertible.

Returns true if the expression is boolean convertible and false otherwise

bool slang::ast::ASTContext::requireValidBitWidth(bitwidth_t width, SourceRange range) const

Reports an error if the given width is a valid bit width.

Returns true if the width is valid as a bit width and false otherwise

bool slang::ast::ASTContext::requireTimingAllowed(SourceRange range) const

Reports an error if time-controlling statements are not allowed in this context.

Returns true if time-controlling statements are allowed and false otherwise

std::optional<bitwidth_t> slang::ast::ASTContext::requireValidBitWidth(const SVInt& value, SourceRange range) const

Reports an error if the given width is a valid bit width.

Returns A bitwidth if the provided integer is a valid bit width, and std::nullopt otherwise.

ConstantValue slang::ast::ASTContext::eval(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const

Evaluates the provided expression as a constant expression.

Returns The result of constant evaluation or an empty value if it failed

If evaluation fails appropriate diagnostics will be issued.

ConstantValue slang::ast::ASTContext::tryEval(const Expression& expr) const

Evaluates the provided expression as a constant expression.

Returns The result of constant evaluation or an empty value if it failed

If evaluation fails no diagnostics will be issued.

std::optional<int32_t> slang::ast::ASTContext::evalInteger(const syntax::ExpressionSyntax& syntax, bitmask<ASTFlags> extraFlags = {}) const

Evaluates the provided expression as an integral constant expression.

Returns The result of constant evaluation or std::nullopt if it failed

If evaluation fails (or if the expression is not integral to begin with) appropriate diagnostics will be issued.

std::optional<int32_t> slang::ast::ASTContext::evalInteger(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const

Evaluates the provided expression as an integral constant expression.

Returns The result of constant evaluation or std::nullopt if it failed

If evaluation fails (or if the expression is not integral to begin with) appropriate diagnostics will be issued.

EvaluatedDimension slang::ast::ASTContext::evalDimension(const syntax::VariableDimensionSyntax& syntax, bool requireRange, bool isPacked) const

Evaluates the given dimension syntax to determine its compile-time bounds and other properties.

Parameters
syntax The dimension syntax node to evaluate
requireRange If true, the dimension syntax must represent a range, as opposed to a single index or other kind of dimension
isPacked If true, this dimension should be treated as a packed dimension
Returns Details about the evaluated dimension

EvaluatedDimension slang::ast::ASTContext::evalPackedDimension(const syntax::VariableDimensionSyntax& syntax) const

Evaluates the given dimension syntax to determine its compile-time bounds and other properties.

Parameters
syntax The dimension syntax node to evaluate
Returns Details about the evaluated dimension

EvaluatedDimension slang::ast::ASTContext::evalPackedDimension(const syntax::ElementSelectSyntax& syntax) const

Evaluates the given dimension syntax to determine its compile-time bounds and other properties.

Parameters
syntax The dimension syntax node to evaluate
Returns Details about the evaluated dimension

EvaluatedDimension slang::ast::ASTContext::evalUnpackedDimension(const syntax::VariableDimensionSyntax& syntax) const

Evaluates the given dimension syntax to determine its compile-time bounds and other properties.

Parameters
syntax The dimension syntax node to evaluate
Returns Details about the evaluated dimension

const syntax::ExpressionSyntax* slang::ast::ASTContext::requireSimpleExpr(const syntax::PropertyExprSyntax& expr) const

Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.

Returns The unwrapped expression if the given syntax node represents a simple expression, and nullptr otherwise.

Subroutine argument expressions are parsed as property expressions, since we don't know up front whether they will be used to instantiate a property or a sequence or are actually for a subroutine. This method unwraps for the case where we are calling a subroutine.

If the given property expression is actually a real sequence or property expression and not just a plain old expression an appropriate diagnostic will be issued.

const syntax::ExpressionSyntax* slang::ast::ASTContext::requireSimpleExpr(const syntax::PropertyExprSyntax& expr, DiagCode code) const

Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.

Returns The unwrapped expression if the given syntax node represents a simple expression, and nullptr otherwise.

Subroutine argument expressions are parsed as property expressions, since we don't know up front whether they will be used to instantiate a property or a sequence or are actually for a subroutine. This method unwraps for the case where we are calling a subroutine.

If the given property expression is actually a real sequence or property expression and not just a plain old expression a diagnostic will be issued with the specified code

Variable documentation

const TempVarSymbol* slang::ast::ASTContext::firstTempVar

If any temporary variables have been materialized in this context, contains a pointer to the first one along with a linked list of any others that may be active.

Otherwise nullptr.

const RandomizeDetails* slang::ast::ASTContext::randomizeDetails

If this context is for creating an inline constraint block for a randomize function call, this points to information about the scope.

Name lookups happen inside the class scope before going through the normal local lookup, for example.