slang::ast::Expression class

The base class for all expressions in SystemVerilog.

Derived classes

class ArbitrarySymbolExpression
Adapts an arbitrary symbol reference for use in an expression tree.
class AssertionInstanceExpression
Represents an instance of an assertion item, either a sequence, a property, or a formal argument that is being referenced and expanded.
class AssignmentExpression
Represents an assignment expression.
class AssignmentPatternExpressionBase
Base class for assignment pattern expressions.
class BinaryExpression
Represents a binary operator expression.
class CallExpression
Represents a subroutine call.
class ClockingEventExpression
Represents a clocking event expression.
class ConcatenationExpression
Represents a concatenation expression.
class ConditionalExpression
Represents a conditional operator expression.
class ConversionExpression
Represents a type conversion expression (implicit or explicit).
class CopyClassExpression
Represents a new expression that copies a class instance.
class DataTypeExpression
Adapts a data type for use in an expression tree.
class DistExpression
Denotes an expression along with a distribution of probabilities for that expression.
class ElementSelectExpression
Represents a single element selection expression.
class EmptyArgumentExpression
Represents an empty argument.
class InsideExpression
Represents a set membership operator expression.
class IntegerLiteral
Represents an integer literal.
class InvalidExpression
Represents an invalid expression.
class LValueReferenceExpression
A placeholder expression that is generated to take the place of one side of a compound assignment expression's binary operator.
class MemberAccessExpression
Represents an access of a structure variable's members.
class MinTypMaxExpression
Represents a min:typ:max expression.
class NewArrayExpression
Represents a new[] expression that creates a dynamic array.
class NewClassExpression
Represents a new expression that creates a class instance.
class NewCovergroupExpression
Represents a new expression that creates a covergroup instance.
class NullLiteral
Represents a null literal.
class RangeSelectExpression
Represents a range selection expression.
class RealLiteral
Represents a real number literal.
class ReplicationExpression
Represents a replication expression.
class StreamingConcatenationExpression
Represents a streaming concatenation.
class StringLiteral
Represents a string literal.
class TaggedUnionExpression
Represents a tagged union member setter expression.
class TimeLiteral
Represents a time literal.
class TypeReferenceExpression
An expression that gets the type of a nested expression using the type() operator.
class UnaryExpression
Represents a unary operator expression.
class UnbasedUnsizedIntegerLiteral
Represents an unbased unsized integer literal, which fills all bits in an expression.
class UnboundedLiteral
Represents the unbounded queue or range literal.
class ValueExpressionBase
Common base class for both NamedValueExpression and HierarchicalValueExpression.
class ValueRangeExpression
Denotes a range of values by providing expressions for the lower and upper bounds of the range.

Public static functions

static auto bind(const ExpressionSyntax& syntax, const ASTContext& context, bitmask<ASTFlags> extraFlags = ASTFlags::None) -> const Expression&
Binds an expression tree from the given syntax node.
static auto bindLValue(const ExpressionSyntax& lhs, const Type& rhs, SourceLocation location, const ASTContext& context, bool isInout) -> const Expression&
Binds the left hand side of an assignment-like expression from the given syntax nodes.
static auto bindLValue(const ExpressionSyntax& syntax, const ASTContext& context) -> const Expression&
Binds an lvalue that is not a typical assignment-like context.
static auto bindRValue(const Type& lhs, const ExpressionSyntax& rhs, SourceLocation location, const ASTContext& context, bitmask<ASTFlags> extraFlags = ASTFlags::None) -> const Expression&
Binds the right hand side of an assignment-like expression from the given syntax nodes.
static auto bindRefArg(const Type& lhs, bitmask<VariableFlags> argFlags, const ExpressionSyntax& rhs, SourceLocation location, const ASTContext& context) -> const Expression&
Binds a connection to a ref argument from the given syntax nodes.
static auto bindArgument(const Type& argType, ArgumentDirection direction, bitmask<VariableFlags> argFlags, const ExpressionSyntax& syntax, const ASTContext& context) -> const Expression&
Binds an argument or port connection with the given direction.
static auto checkConnectionDirection(const Expression& expr, ArgumentDirection direction, const ASTContext& context, SourceLocation loc, bitmask<AssignFlags> flags = {}) -> bool
Checks that the given expression is valid for the specified connection direction.
static auto bindImplicitParam(const syntax::DataTypeSyntax& implicitType, const ExpressionSyntax& rhs, SourceLocation location, const ASTContext& exprContext, const ASTContext& typeContext, bitmask<ASTFlags> extraFlags = ASTFlags::None) -> std::tuple<const Expression*, const Type*>
Binds an initializer expression for an implicitly typed parameter.
static auto bindSelector(Expression& value, const syntax::ElementSelectSyntax& syntax, const ASTContext& context) -> const Expression&
Bind a selector expression given an already existing value expression to select from.
static auto tryBindInterfaceRef(const ASTContext& context, const ExpressionSyntax& syntax, bool isInterfacePort) -> Expression*
Tries to bind an interface reference into an expression for an interface port or a virtual interface assignment.
static auto bindMembershipExpressions(const ASTContext& context, parsing::TokenKind keyword, bool requireIntegral, bool unwrapUnpacked, bool allowTypeReferences, bool allowValueRange, const ExpressionSyntax& valueExpr, std::span<const ExpressionSyntax*const> expressions, SmallVectorBase<const Expression*>& results) -> bool
Specialized method for creating all of the expressions in a set membership check.
static void findPotentiallyImplicitNets(const syntax::SyntaxNode& expr, const ASTContext& context, SmallVectorBase<const syntax::IdentifierNameSyntax*>& results)
This method finds all unqualified name references in the given expression and attempts to look them up in the given context.
static auto convertAssignment(const ASTContext& context, const Type& type, Expression& expr, SourceLocation location, Expression** lhsExpr = nullptr, bitmask<AssignFlags>* assignFlags = nullptr) -> Expression&
Converts an expression to be of the given type, following the rules for implicit conversions, array port slicing, etc.

Public functions

auto bad() const -> bool
Indicates whether the expression is invalid.
auto isImplicitString() const -> bool
Indicates whether the expression is of type string, or if it is implicitly convertible to a string.
auto isUnsizedInteger() const -> bool
Indicates whether the expression is represented by an unsized integer value.
auto eval(EvalContext& context) const -> ConstantValue
Evaluates the expression under the given evaluation context.
auto evalLValue(EvalContext& context) const -> LValue
Evaluates the expression as an lvalue.
auto evalSelector(EvalContext& context) const -> std::optional<ConstantRange>
Evaluates the expression as a selector and returns the selection range that results.
auto requireLValue(const ASTContext& context, SourceLocation location = {}, bitmask<AssignFlags> flags = {}, const Expression* longestStaticPrefix = nullptr) const -> bool
Verifies that this expression is a valid lvalue and that each element of that lvalue can be assigned to.
void getLongestStaticPrefixes(SmallVector<std::pair<const ValueSymbol*, const Expression*>>& results, EvalContext& evalContext, const Expression* longestStaticPrefix = nullptr) const
If this expression is a valid lvalue, returns the part(s) of it that constitutes the "longest static prefix" for purposes of determining duplicate assignments / drivers to a portion of a value, for each such lvalue (usually one unless there is an lvalue concatenation).
auto isImplicitlyAssignableTo(Compilation& compilation, const Type& type) const -> bool
Returns true if this expression can be implicitly assigned to a value of the given type.
auto getEffectiveWidth() const -> std::optional<bitwidth_t>
Traverses the expression tree and computes what its width would be (in bits) if the types of all known constants were declared with only the bits necessary to represent them.
auto getEffectiveSign() const -> bool
Traverses the expression tree and determines whether all operands are known to be signed, even if the types involved end up being computed as unsigned.
auto getSymbolReference(bool allowPacked = true) const -> const Symbol*
If this expression is a reference to a symbol, returns a pointer to that symbol.
auto hasHierarchicalReference() const -> bool
Returns true if any subexpression of this expression is a hierarchical reference.
template<typename T>
auto as() -> T&
Casts this expression to the given concrete derived type.
template<typename T>
auto as() const -> const T&
Casts this expression to the given concrete derived type.
template<typename T>
auto as_if() -> T*
Tries to cast this expression to the given concrete derived type.
template<typename T>
auto as_if() const -> const T*
Tries to cast this expression to the given concrete derived type.
template<typename TVisitor, typename... Args>
auto visit(TVisitor&& visitor, Args && ... args) -> decltype(auto)
Visits this expression's concrete derived type via the provided visitor object.
template<typename TVisitor, typename... Args>
auto visit(TVisitor&& visitor, Args && ... args) const -> decltype(auto)
Visits this expression's concrete derived type via the provided visitor object.

Public variables

ExpressionKind kind
The kind of expression; indicates the type of derived class.
not_null<const Type*> type
The type of the expression.
const ConstantValue* constant
A pointer to a constant value if the expression has been evaluated.
const ExpressionSyntax* syntax
The syntax used to create the expression, if any.
SourceRange sourceRange
The source range of this expression, if it originated from source code.

Function documentation

static const Expression& slang::ast::Expression::bindLValue(const ExpressionSyntax& lhs, const Type& rhs, SourceLocation location, const ASTContext& context, bool isInout)

Binds the left hand side of an assignment-like expression from the given syntax nodes.

Parameters
lhs The syntax node representing the expression to bind
rhs The type of the right hand side, for type checking
location The location of the assignment, for reporting diagnostics
context The AST context under which binding is performed
isInout true if the assignment is for an inout port

static const Expression& slang::ast::Expression::bindLValue(const ExpressionSyntax& syntax, const ASTContext& context)

Binds an lvalue that is not a typical assignment-like context.

For example, the output argument of certain system tasks that accept almost any type.

static const Expression& slang::ast::Expression::bindRValue(const Type& lhs, const ExpressionSyntax& rhs, SourceLocation location, const ASTContext& context, bitmask<ASTFlags> extraFlags = ASTFlags::None)

Binds the right hand side of an assignment-like expression from the given syntax nodes.

Parameters
lhs The type of the left hand side, for type checking
rhs The syntax node representing the expression to bind
location The location of the assignment, for reporting diagnostics
context The AST context under which binding is performed
extraFlags Extra flags to apply when binding

static bool slang::ast::Expression::checkConnectionDirection(const Expression& expr, ArgumentDirection direction, const ASTContext& context, SourceLocation loc, bitmask<AssignFlags> flags = {})

Checks that the given expression is valid for the specified connection direction.

Returns true if the connection is valid and false otherwise.

static std::tuple<const Expression*, const Type*> slang::ast::Expression::bindImplicitParam(const syntax::DataTypeSyntax& implicitType, const ExpressionSyntax& rhs, SourceLocation location, const ASTContext& exprContext, const ASTContext& typeContext, bitmask<ASTFlags> extraFlags = ASTFlags::None)

Binds an initializer expression for an implicitly typed parameter.

Parameters
implicitType The implicit type syntax for the parameter
rhs The initializer expression to bind
location The location of the initializer, for reporting diagnostics
exprContext The AST context to use for binding the initializer
typeContext The AST context to use for binding the type
extraFlags Extra flags to apply to AST creation
Returns A tuple, the first element of which is the bound initializer and the second of which is the original type of that expression prior to any conversions being performed.

static Expression* slang::ast::Expression::tryBindInterfaceRef(const ASTContext& context, const ExpressionSyntax& syntax, bool isInterfacePort)

Tries to bind an interface reference into an expression for an interface port or a virtual interface assignment.

Returns nullptr if unable to do so.

static bool slang::ast::Expression::bindMembershipExpressions(const ASTContext& context, parsing::TokenKind keyword, bool requireIntegral, bool unwrapUnpacked, bool allowTypeReferences, bool allowValueRange, const ExpressionSyntax& valueExpr, std::span<const ExpressionSyntax*const> expressions, SmallVectorBase<const Expression*>& results)

Specialized method for creating all of the expressions in a set membership check.

Parameters
context The context used for creating expressions.
keyword The kind of membership being created, which is used to customize any diagnostics produced.
requireIntegral If set to true, expression types will be restricted to be only integral types.
unwrapUnpacked If set to true, unpacked arrays will be unwrapped to their element types to find the type to check against. Otherwise, all aggregates are illegal.
allowTypeReferences If set to true the created expressions are allowed to be type reference expressions. Otherwise an error will be issued.
allowValueRange If set to true, value range expressions will be allowed. Otherwise an error will be issued for them.
valueExpr The value being checked for membership.
expressions Denotes the set to check within. All of the expressions influence each other for purposes of finding a common comparison type.
results A vector that will be filled with the created membership expressions.
Returns true if all expressions are legal, otherwise false and appropriate diagnostics are issued.

This is used for case statements and the 'inside' operator.

static void slang::ast::Expression::findPotentiallyImplicitNets(const syntax::SyntaxNode& expr, const ASTContext& context, SmallVectorBase<const syntax::IdentifierNameSyntax*>& results)

This method finds all unqualified name references in the given expression and attempts to look them up in the given context.

If they can't be found, their name tokens are returned in the given results vector.

static Expression& slang::ast::Expression::convertAssignment(const ASTContext& context, const Type& type, Expression& expr, SourceLocation location, Expression** lhsExpr = nullptr, bitmask<AssignFlags>* assignFlags = nullptr)

Converts an expression to be of the given type, following the rules for implicit conversions, array port slicing, etc.

Parameters
context The AST context
type The type to convert to
expr The expression being converted
location The location where conversion is happening, for reporting diagnostics
lhsExpr If the conversion is for an output port, this is a pointer to the left-hand side expression. The pointer will be reassigned if array port slicing occurs.
assignFlags If lhsExpr is provided, this parameter must also be provided. It will the AssignFlags::SlicedPort flag added to it if array port slicing occurs.

bool slang::ast::Expression::isUnsizedInteger() const

Indicates whether the expression is represented by an unsized integer value.

For example, the integer literal "4" or the unbased unsized literal "'1";

ConstantValue slang::ast::Expression::eval(EvalContext& context) const

Evaluates the expression under the given evaluation context.

Any errors that occur will be stored in the evaluation context instead of issued to the compilation.

LValue slang::ast::Expression::evalLValue(EvalContext& context) const

Evaluates the expression as an lvalue.

Note that this will throw an exception if the expression does not represent an lvalue.

std::optional<ConstantRange> slang::ast::Expression::evalSelector(EvalContext& context) const

Evaluates the expression as a selector and returns the selection range that results.

If the evaluates fails or the expression does not represent a selection std::nullopt will be returned.

bool slang::ast::Expression::requireLValue(const ASTContext& context, SourceLocation location = {}, bitmask<AssignFlags> flags = {}, const Expression* longestStaticPrefix = nullptr) const

Verifies that this expression is a valid lvalue and that each element of that lvalue can be assigned to.

If it's not, appropriate diagnostics will be issued. Information about the source expression driving the lvalue will be registered with the various symbols involved.

void slang::ast::Expression::getLongestStaticPrefixes(SmallVector<std::pair<const ValueSymbol*, const Expression*>>& results, EvalContext& evalContext, const Expression* longestStaticPrefix = nullptr) const

If this expression is a valid lvalue, returns the part(s) of it that constitutes the "longest static prefix" for purposes of determining duplicate assignments / drivers to a portion of a value, for each such lvalue (usually one unless there is an lvalue concatenation).

If there are no lvalues the vector will not have any entries added to it.

std::optional<bitwidth_t> slang::ast::Expression::getEffectiveWidth() const

Traverses the expression tree and computes what its width would be (in bits) if the types of all known constants were declared with only the bits necessary to represent them.

If any encountered expressions have errors, returns nullopt.

const Symbol* slang::ast::Expression::getSymbolReference(bool allowPacked = true) const

If this expression is a reference to a symbol, returns a pointer to that symbol.

Parameters
allowPacked Determines whether selects of a packed type are considered a symbol reference or whether to consider only unpacked structs and arrays.

If the expression is a member access of a struct or class, returns the member being accessed. If it's a select of an array, returns the root array variable.

template<typename T>
T& slang::ast::Expression::as()

Casts this expression to the given concrete derived type.

Asserts that the type is appropriate given this expression's kind.

template<typename T>
const T& slang::ast::Expression::as() const

Casts this expression to the given concrete derived type.

Asserts that the type is appropriate given this expression's kind.

template<typename T>
T* slang::ast::Expression::as_if()

Tries to cast this expression to the given concrete derived type.

If the type is not appropriate given this expression's kind, returns nullptr.

template<typename T>
const T* slang::ast::Expression::as_if() const

Tries to cast this expression to the given concrete derived type.

If the type is not appropriate given this expression's kind, returns nullptr.

Variable documentation

const ConstantValue* slang::ast::Expression::constant

A pointer to a constant value if the expression has been evaluated.

The value may be empty to indicate that the expression is known to not be constant. If the pointer is null, the expression hasn't been evaluated yet.

const ExpressionSyntax* slang::ast::Expression::syntax

The syntax used to create the expression, if any.

An expression tree can be created manually in which case it may not have a syntax representation.