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