charon_lib/ast/
llbc_ast.rs

1//! LLBC
2//!
3//! MIR code where we have rebuilt the control-flow (`if ... then ... else ...`,
4//! `while ...`, ...).
5//!
6//! Also note that we completely break the definitions Statement and Terminator
7//! from MIR to use Statement only.
8
9pub use super::llbc_ast_utils::*;
10pub use crate::ast::*;
11use derive_generic_visitor::{Drive, DriveMut};
12use macros::{EnumAsGetters, EnumIsA, EnumToGetters, VariantIndexArity, VariantName};
13use serde::{Deserialize, Serialize};
14
15generate_index_type!(StatementId);
16
17/// A raw statement: a statement without meta data.
18#[derive(
19    Debug, Clone, EnumIsA, EnumToGetters, EnumAsGetters, Serialize, Deserialize, Drive, DriveMut,
20)]
21pub enum StatementKind {
22    /// Assigns an `Rvalue` to a `Place`. e.g. `let y = x;` could become
23    /// `y := move x` which is represented as `Assign(y, Rvalue::Use(Operand::Move(x)))`.
24    Assign(Place, Rvalue),
25    /// Not used today because we take MIR built.
26    SetDiscriminant(Place, VariantId),
27    /// Equivalent to std::intrinsics::copy_nonoverlapping; this is not modelled as a function
28    /// call as it cannot diverge
29    CopyNonOverlapping(Box<CopyNonOverlapping>),
30    /// Indicates that this local should be allocated; if it is already allocated, this frees
31    /// the local and re-allocates it. The return value and arguments do not receive a
32    /// `StorageLive`. We ensure in the micro-pass `insert_storage_lives` that all other locals
33    /// have a `StorageLive` associated with them.
34    StorageLive(LocalId),
35    /// Indicates that this local should be deallocated; if it is already deallocated, this is
36    /// a no-op. A local may not have a `StorageDead` in the function's body, in which case it
37    /// is implicitly deallocated at the end of the function.
38    StorageDead(LocalId),
39    Deinit(Place),
40    /// Drop the value at the given place.
41    ///
42    /// Depending on `DropKind`, this may be a real call to `drop_in_place`, or a conditional call
43    /// that should only happen if the place has not been moved out of. See the docs of `DropKind`
44    /// for more details; to get precise drops use `--precise-drops`.
45    Drop(Place, TraitRef, #[drive(skip)] DropKind),
46    Assert(Assert),
47    Call(Call),
48    /// Panic also handles "unreachable". We keep the name of the panicking function that was
49    /// called.
50    Abort(AbortKind),
51    Return,
52    /// Break to outer loops.
53    /// The `usize` gives the index of the outer loop to break to:
54    /// * 0: break to first outer loop (the current loop)
55    /// * 1: break to second outer loop
56    /// * ...
57    #[drive(skip)]
58    Break(usize),
59    /// Continue to outer loops.
60    /// The `usize` gives the index of the outer loop to continue to:
61    /// * 0: continue to first outer loop (the current loop)
62    /// * 1: continue to second outer loop
63    /// * ...
64    #[drive(skip)]
65    Continue(usize),
66    /// No-op.
67    Nop,
68    Switch(Switch),
69    Loop(Block),
70    #[drive(skip)]
71    Error(String),
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize, Drive, DriveMut)]
75pub struct Statement {
76    pub span: Span,
77    /// Integer uniquely identifying this statement among the statmeents in the current body. To
78    /// simplify things we generate globally-fresh ids when creating a new `Statement`.
79    #[charon::rename("statement_id")]
80    pub id: StatementId,
81    pub kind: StatementKind,
82    /// Comments that precede this statement.
83    // This is filled in a late pass after all the control-flow manipulation.
84    #[drive(skip)]
85    pub comments_before: Vec<String>,
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize, Drive, DriveMut)]
89pub struct Block {
90    pub span: Span,
91    pub statements: Vec<Statement>,
92}
93
94#[derive(
95    Debug,
96    Clone,
97    EnumIsA,
98    EnumToGetters,
99    EnumAsGetters,
100    Serialize,
101    Deserialize,
102    Drive,
103    DriveMut,
104    VariantName,
105    VariantIndexArity,
106)]
107pub enum Switch {
108    /// Gives the `if` block and the `else` block. The `Operand` is the condition of the `if`, e.g. `if (y == 0)` could become
109    /// ```text
110    /// v@3 := copy y; // Represented as `Assign(v@3, Use(Copy(y))`
111    /// v@2 := move v@3 == 0; // Represented as `Assign(v@2, BinOp(BinOp::Eq, Move(y), Const(0)))`
112    /// if (move v@2) { // Represented as `If(Move(v@2), <then branch>, <else branch>)`
113    /// ```
114    If(Operand, Block, Block),
115    /// Gives the integer type, a map linking values to switch branches, and the
116    /// otherwise block. Note that matches over enumerations are performed by
117    /// switching over the discriminant, which is an integer.
118    /// Also, we use a `Vec` to make sure the order of the switch
119    /// branches is preserved.
120    ///
121    /// Rk.: we use a vector of values, because some of the branches may
122    /// be grouped together, like for the following code:
123    /// ```text
124    /// match e {
125    ///   E::V1 | E::V2 => ..., // Grouped
126    ///   E::V3 => ...
127    /// }
128    /// ```
129    SwitchInt(Operand, LiteralTy, Vec<(Vec<Literal>, Block)>, Block),
130    /// A match over an ADT.
131    ///
132    /// The match statement is introduced in [crate::transform::resugar::reconstruct_matches]
133    /// (whenever we find a discriminant read, we merge it with the subsequent
134    /// switch into a match).
135    Match(Place, Vec<(Vec<VariantId>, Block)>, Option<Block>),
136}
137
138pub type ExprBody = GExprBody<Block>;