Skip to content

Improve binding of a const value #253

Description

Currently

const int[32] n = 3;

Produces a Cast expression from a 128 bit wide 3 to int[32]. This loosely follow a document on how Rust treats literals in the first levels of compilation. We recently introduced storing the value of x in a table of const values. We store this Cast expression. This then has to be parsed to extract the value 3 when it is used later.

This Cast should either be avoided, or immediately reduced to a Literal of type int[32]. In particular, the Cast should never appear in the ASG (or any other persistent structure). This does not change the semantics of the program, and simplifies using the table of const values.

This would allow us, for example, to avoid this match arm:

impl TryFrom<&TExpr> for u32 {
type Error = TryFromU32Error;
fn try_from(value: &TExpr) -> Result<Self, TryFromU32Error> {
match &value.expression {
Expr::Cast(thecast) => {
match **thecast {
Cast {
operand:
TExpr {
expression:
Expr::Literal(Literal::Int(IntLiteral {
value: int_value,
sign: true,
})),
ty: _, // Type::Int(_, _),
},
typ: _, // Type::Int(_, _),
} => u32::try_from(int_value).map_err(|_| TryFromU32Error),
_ => Err(TryFromU32Error),
}
}
_ => Err(TryFromU32Error),
}
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions