|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use formality_core::{id, Downcast, DowncastFrom, Upcast}; |
| 4 | +use formality_macros::term; |
| 5 | +use formality_types::grammar::{FieldId, Ty}; |
| 6 | + |
| 7 | +// This definition is based on [MiniRust](https://github.com/minirust/minirust/blob/master/spec/lang/syntax.md). |
| 8 | + |
| 9 | +id!(BbId); |
| 10 | +id!(LocalId); |
| 11 | + |
| 12 | +/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators). |
| 13 | +#[term(minirust($,args) -> $ret { |
| 14 | + $*locals |
| 15 | + $*blocks |
| 16 | +})] |
| 17 | +pub struct Body { |
| 18 | + pub args: Vec<LocalId>, |
| 19 | + pub ret: LocalId, |
| 20 | + pub locals: Vec<LocalDecl>, |
| 21 | + pub blocks: Vec<BasicBlock>, |
| 22 | +} |
| 23 | + |
| 24 | +/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators). |
| 25 | +#[term(let $id: $ty;)] |
| 26 | +pub struct LocalDecl { |
| 27 | + pub id: LocalId, |
| 28 | + pub ty: Ty, |
| 29 | +} |
| 30 | + |
| 31 | +/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators). |
| 32 | +#[term($id: ${statements} $terminator;)] |
| 33 | +pub struct BasicBlock { |
| 34 | + pub id: BbId, |
| 35 | + pub statements: Vec<Statement>, |
| 36 | + pub terminator: Terminator, |
| 37 | +} |
| 38 | + |
| 39 | +/// Based on [MiniRust statements](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators). |
| 40 | +#[term] |
| 41 | +pub enum Statement { |
| 42 | + #[grammar($v0 = $v1;)] |
| 43 | + Assign(PlaceExpression, ValueExpression), |
| 44 | + |
| 45 | + #[grammar($v0;)] |
| 46 | + PlaceMention(PlaceExpression), |
| 47 | + |
| 48 | + // SetDiscriminant |
| 49 | + // Validate |
| 50 | + // Deinit |
| 51 | + // StorageLive |
| 52 | + // StorageDead |
| 53 | +} |
| 54 | + |
| 55 | +/// Based on [MiniRust terminators](https://github.com/minirust/minirust/blob/9ae11cc202d040f08bc13ec5254d3d41d5f3cc25/spec/lang/syntax.md#statements-terminators). |
| 56 | +#[term] |
| 57 | +pub enum Terminator { |
| 58 | + #[grammar(goto $v0)] |
| 59 | + Goto(BbId), |
| 60 | + |
| 61 | + // Switch |
| 62 | + // Unreachable |
| 63 | + // Intrinsic |
| 64 | + |
| 65 | + #[grammar(call $?cc $callee$(arguments) -> $ret $:goto $next_block)] |
| 66 | + Call { |
| 67 | + callee: ValueExpression, |
| 68 | + cc: CallingConvention, |
| 69 | + arguments: Vec<ArgumentExpression>, |
| 70 | + ret: PlaceExpression, |
| 71 | + next_block: Option<BbId>, |
| 72 | + }, |
| 73 | + |
| 74 | + #[grammar(return)] |
| 75 | + Return, |
| 76 | +} |
| 77 | + |
| 78 | +#[term] |
| 79 | +#[derive(Default)] |
| 80 | +pub enum CallingConvention { |
| 81 | + #[default] |
| 82 | + #[grammar((Rust))] |
| 83 | + Rust, |
| 84 | + |
| 85 | + #[grammar((C))] |
| 86 | + C, |
| 87 | +} |
| 88 | + |
| 89 | +#[term] |
| 90 | +pub enum ArgumentExpression { |
| 91 | + ByValue(ValueExpression), |
| 92 | + InPlace(PlaceExpression), |
| 93 | +} |
| 94 | + |
| 95 | +#[term] |
| 96 | +pub enum ValueExpression { |
| 97 | + // Const |
| 98 | + #[grammar($(v0) as $v1)] |
| 99 | + Tuple(Vec<ValueExpression>, Ty), |
| 100 | + // Union |
| 101 | + // Variant |
| 102 | + // GetDiscriminant |
| 103 | + #[grammar(load($v0))] |
| 104 | + Load(PlaceExpression), |
| 105 | + // AddrOf |
| 106 | + // UnOp |
| 107 | + // BinOp |
| 108 | +} |
| 109 | + |
| 110 | +#[term] |
| 111 | +pub enum PlaceExpression { |
| 112 | + Local(LocalId), |
| 113 | + Deref(Arc<ValueExpression>), |
| 114 | + Field(Arc<PlaceExpression>, FieldId), |
| 115 | + // Index |
| 116 | + // Downcast |
| 117 | +} |
| 118 | + |
| 119 | + |
0 commit comments