ASTContext class
#include <slang/ast/ASTContext.h>
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
- Compilation& getCompilation() const
- Gets the parent compilation associated with the AST.
- LookupLocation getLocation() const
- Gets the lookup location passed to the ASTContext constructor.
- bool inUnevaluatedBranch() const
- Indicates whether the AST creation is happening inside an unevaluated branch.
-
void addTemporaryEnums(const syntax::
SyntaxNode& syntax) - Declares the enums in the given syntax in a new scope just for this context, which then becomes the context's scope.
- const InstanceSymbolBase* getInstance() const
- Gets the parent instance if this context is being used to bind expressions for an instantiation.
- const ProceduralBlockSymbol* getProceduralBlock() const
- If this context is within a procedural block, returns a pointer to that symbol.
- bool inAlwaysCombLatch() const
- 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 setPort(const Symbol& port)
- Sets the port associated with the context.
- void clearSymbolCtx()
- Clears the symbol assocated with the context (either a parent instance, a port, or a procedural block).
- const Symbol* tryFillAssertionDetails()
- 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.
- Diagnostic& addDiag(DiagCode code, SourceLocation location) const
- Issues a new diagnostic.
- Diagnostic& addDiag(DiagCode code, SourceRange sourceRange) const
- Issues a new diagnostic.
- bool requireIntegral(const Expression& expr) const
- Reports an error if the given expression is not integral.
- bool requireIntegral(const ConstantValue& cv, SourceRange range) const
- Reports an error if the given constant value is not integral.
- bool requireNoUnknowns(const SVInt& value, SourceRange range) const
- Reports an error if the given integer has unknowns.
- bool requirePositive(const SVInt& value, SourceRange range) const
- Reports an error if the given integer is not positive or zero.
-
bool requirePositive(std::
optional<int32_t> value, SourceRange range) const - Reports an error if the given integer is not positive or zero.
-
bool requireGtZero(std::
optional<int32_t> value, SourceRange range) const - Reports an error if the given integer is not greater than zero.
- bool requireBooleanConvertible(const Expression& expr) const
- Reports an error if the given expression's type is not boolean convertible.
-
bool requireValidBitWidth(bitwidth_
t width, SourceRange range) const - Reports an error if the given width is a valid bit width.
- bool requireTimingAllowed(SourceRange range) const
- Reports an error if time-controlling statements are not allowed in this context.
-
std::
optional<bitwidth_ t> requireValidBitWidth(const SVInt& value, SourceRange range) const - Reports an error if the given width is a valid bit width.
- ConstantValue eval(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const
- Evaluates the provided expression as a constant expression.
- ConstantValue tryEval(const Expression& expr) const
- Evaluates the provided expression as a constant expression.
-
std::
optional<int32_t> evalInteger(const syntax:: ExpressionSyntax& syntax, bitmask<ASTFlags> extraFlags = {}) const - Evaluates the provided expression as an integral constant expression.
-
std::
optional<int32_t> evalInteger(const Expression& expr, bitmask<EvalFlags> extraFlags = {}) const - Evaluates the provided expression as an integral constant expression.
-
EvaluatedDimension evalDimension(const syntax::
VariableDimensionSyntax& syntax, bool requireRange, bool isPacked) const - Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
-
EvaluatedDimension evalPackedDimension(const syntax::
VariableDimensionSyntax& syntax) const - Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
-
EvaluatedDimension evalPackedDimension(const syntax::
ElementSelectSyntax& syntax) const - Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
-
EvaluatedDimension evalUnpackedDimension(const syntax::
VariableDimensionSyntax& syntax) const - Evaluates the given dimension syntax to determine its compile-time bounds and other properties.
-
const syntax::
ExpressionSyntax* requireSimpleExpr(const syntax:: PropertyExprSyntax& expr) const - Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.
-
const syntax::
ExpressionSyntax* requireSimpleExpr(const syntax:: PropertyExprSyntax& expr, DiagCode code) const - Checks and unwraps a given property expression syntax node into a simple expression syntax pointer.
- void noteReference(const ValueSymbol& symbol, bool isDottedAccess) const
- Notes that the given value symbol has been referenced, taking into account active AST flags to determine whether it's used as an lvalue or rvalue.
- RandMode getRandMode(const Symbol& symbol) const
- 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.
- const Scope* getAssertionInstantiationScope() const
- Gets the scope in which the current assertion expression is considered to appear, after walking up through any named sequence and property instantiations.
- ASTContext resetFlags(bitmask<ASTFlags> addedFlags) const
- 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 |
void slang:: ast:: ASTContext:: addTemporaryEnums(const syntax:: SyntaxNode& syntax)
Declares the enums in the given syntax in a new scope just for this context, which then becomes the context's scope.
This is for syntax that can refer to variables that exist only while binding, like iterators, pattern variables, and assertion formal arguments, so the enums are resolved right away using this context.
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. |
|---|
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
const Scope* slang:: ast:: ASTContext:: getAssertionInstantiationScope() const
Gets the scope in which the current assertion expression is considered to appear, after walking up through any named sequence and property instantiations.
If not in an assertion instance this is just the current scope. Returns nullptr if the chain ends at a default instance of a declaration, or if a checker's actual argument is being bound at the instantiation site (ASTFlags::CheckerConnection), meaning that the real point of appearance isn't known yet.
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.