1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::ty::cast::mir_cast_kind;
use rustc_middle::{mir::*, thir::*, ty};
use rustc_span::source_map::Spanned;
use rustc_span::Span;
use rustc_target::abi::{FieldIdx, VariantIdx};

use crate::build::custom::ParseError;
use crate::build::expr::as_constant::as_constant_inner;

use super::{parse_by_kind, PResult, ParseCtxt};

impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
    pub fn parse_statement(&self, expr_id: ExprId) -> PResult<StatementKind<'tcx>> {
        parse_by_kind!(self, expr_id, _, "statement",
            @call(mir_storage_live, args) => {
                Ok(StatementKind::StorageLive(self.parse_local(args[0])?))
            },
            @call(mir_storage_dead, args) => {
                Ok(StatementKind::StorageDead(self.parse_local(args[0])?))
            },
            @call(mir_assume, args) => {
                let op = self.parse_operand(args[0])?;
                Ok(StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume(op))))
            },
            @call(mir_deinit, args) => {
                Ok(StatementKind::Deinit(Box::new(self.parse_place(args[0])?)))
            },
            @call(mir_retag, args) => {
                Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?)))
            },
            @call(mir_set_discriminant, args) => {
                let place = self.parse_place(args[0])?;
                let var = self.parse_integer_literal(args[1])? as u32;
                Ok(StatementKind::SetDiscriminant {
                    place: Box::new(place),
                    variant_index: VariantIdx::from_u32(var),
                })
            },
            ExprKind::Assign { lhs, rhs } => {
                let lhs = self.parse_place(*lhs)?;
                let rhs = self.parse_rvalue(*rhs)?;
                Ok(StatementKind::Assign(Box::new((lhs, rhs))))
            },
        )
    }

    pub fn parse_terminator(&self, expr_id: ExprId) -> PResult<TerminatorKind<'tcx>> {
        parse_by_kind!(self, expr_id, expr, "terminator",
            @call(mir_return, _args) => {
                Ok(TerminatorKind::Return)
            },
            @call(mir_goto, args) => {
                Ok(TerminatorKind::Goto { target: self.parse_block(args[0])? } )
            },
            @call(mir_unreachable, _args) => {
                Ok(TerminatorKind::Unreachable)
            },
            @call(mir_unwind_resume, _args) => {
                Ok(TerminatorKind::UnwindResume)
            },
            @call(mir_unwind_terminate, args) => {
                Ok(TerminatorKind::UnwindTerminate(self.parse_unwind_terminate_reason(args[0])?))
            },
            @call(mir_drop, args) => {
                Ok(TerminatorKind::Drop {
                    place: self.parse_place(args[0])?,
                    target: self.parse_return_to(args[1])?,
                    unwind: self.parse_unwind_action(args[2])?,
                    replace: false,
                })
            },
            @call(mir_call, args) => {
                self.parse_call(args)
            },
            ExprKind::Match { scrutinee, arms, .. } => {
                let discr = self.parse_operand(*scrutinee)?;
                self.parse_match(arms, expr.span).map(|t| TerminatorKind::SwitchInt { discr, targets: t })
            },
        )
    }

    fn parse_unwind_terminate_reason(&self, expr_id: ExprId) -> PResult<UnwindTerminateReason> {
        parse_by_kind!(self, expr_id, _, "unwind terminate reason",
            @variant(mir_unwind_terminate_reason, Abi) => {
                Ok(UnwindTerminateReason::Abi)
            },
            @variant(mir_unwind_terminate_reason, InCleanup) => {
                Ok(UnwindTerminateReason::InCleanup)
            },
        )
    }

    fn parse_unwind_action(&self, expr_id: ExprId) -> PResult<UnwindAction> {
        parse_by_kind!(self, expr_id, _, "unwind action",
            @call(mir_unwind_continue, _args) => {
                Ok(UnwindAction::Continue)
            },
            @call(mir_unwind_unreachable, _args) => {
                Ok(UnwindAction::Unreachable)
            },
            @call(mir_unwind_terminate, args) => {
                Ok(UnwindAction::Terminate(self.parse_unwind_terminate_reason(args[0])?))
            },
            @call(mir_unwind_cleanup, args) => {
                Ok(UnwindAction::Cleanup(self.parse_block(args[0])?))
            },
        )
    }

    fn parse_return_to(&self, expr_id: ExprId) -> PResult<BasicBlock> {
        parse_by_kind!(self, expr_id, _, "return block",
            @call(mir_return_to, args) => {
                self.parse_block(args[0])
            },
        )
    }

    fn parse_match(&self, arms: &[ArmId], span: Span) -> PResult<SwitchTargets> {
        let Some((otherwise, rest)) = arms.split_last() else {
            return Err(ParseError {
                span,
                item_description: "no arms".to_string(),
                expected: "at least one arm".to_string(),
            });
        };

        let otherwise = &self.thir[*otherwise];
        let PatKind::Wild = otherwise.pattern.kind else {
            return Err(ParseError {
                span: otherwise.span,
                item_description: format!("{:?}", otherwise.pattern.kind),
                expected: "wildcard pattern".to_string(),
            });
        };
        let otherwise = self.parse_block(otherwise.body)?;

        let mut values = Vec::new();
        let mut targets = Vec::new();
        for arm in rest {
            let arm = &self.thir[*arm];
            let PatKind::Constant { value } = arm.pattern.kind else {
                return Err(ParseError {
                    span: arm.pattern.span,
                    item_description: format!("{:?}", arm.pattern.kind),
                    expected: "constant pattern".to_string(),
                });
            };
            values.push(value.eval_bits(self.tcx, self.param_env));
            targets.push(self.parse_block(arm.body)?);
        }

        Ok(SwitchTargets::new(values.into_iter().zip(targets), otherwise))
    }

    fn parse_call(&self, args: &[ExprId]) -> PResult<TerminatorKind<'tcx>> {
        let (destination, call) = parse_by_kind!(self, args[0], _, "function call",
            ExprKind::Assign { lhs, rhs } => (*lhs, *rhs),
        );
        let destination = self.parse_place(destination)?;
        let target = self.parse_return_to(args[1])?;
        let unwind = self.parse_unwind_action(args[2])?;

        parse_by_kind!(self, call, _, "function call",
            ExprKind::Call { fun, args, from_hir_call, fn_span, .. } => {
                let fun = self.parse_operand(*fun)?;
                let args = args
                    .iter()
                    .map(|arg|
                        Ok(Spanned { node: self.parse_operand(*arg)?, span: self.thir.exprs[*arg].span  } )
                    )
                    .collect::<PResult<Vec<_>>>()?;
                Ok(TerminatorKind::Call {
                    func: fun,
                    args,
                    destination,
                    target: Some(target),
                    unwind,
                    call_source: if *from_hir_call { CallSource::Normal } else {
                        CallSource::OverloadedOperator
                    },
                    fn_span: *fn_span,
                })
            },
        )
    }

    fn parse_rvalue(&self, expr_id: ExprId) -> PResult<Rvalue<'tcx>> {
        parse_by_kind!(self, expr_id, expr, "rvalue",
            @call(mir_discriminant, args) => self.parse_place(args[0]).map(Rvalue::Discriminant),
            @call(mir_cast_transmute, args) => {
                let source = self.parse_operand(args[0])?;
                Ok(Rvalue::Cast(CastKind::Transmute, source, expr.ty))
            },
            @call(mir_checked, args) => {
                parse_by_kind!(self, args[0], _, "binary op",
                    ExprKind::Binary { op, lhs, rhs } => Ok(Rvalue::CheckedBinaryOp(
                        *op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?))
                    )),
                )
            },
            @call(mir_offset, args) => {
                let ptr = self.parse_operand(args[0])?;
                let offset = self.parse_operand(args[1])?;
                Ok(Rvalue::BinaryOp(BinOp::Offset, Box::new((ptr, offset))))
            },
            @call(mir_len, args) => Ok(Rvalue::Len(self.parse_place(args[0])?)),
            @call(mir_copy_for_deref, args) => Ok(Rvalue::CopyForDeref(self.parse_place(args[0])?)),
            ExprKind::Borrow { borrow_kind, arg } => Ok(
                Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
            ),
            ExprKind::AddressOf { mutability, arg } => Ok(
                Rvalue::AddressOf(*mutability, self.parse_place(*arg)?)
            ),
            ExprKind::Binary { op, lhs, rhs } =>  Ok(
                Rvalue::BinaryOp(*op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)))
            ),
            ExprKind::Unary { op, arg } => Ok(
                Rvalue::UnaryOp(*op, self.parse_operand(*arg)?)
            ),
            ExprKind::Repeat { value, count } => Ok(
                Rvalue::Repeat(self.parse_operand(*value)?, *count)
            ),
            ExprKind::Cast { source } => {
                let source = self.parse_operand(*source)?;
                let source_ty = source.ty(self.body.local_decls(), self.tcx);
                let cast_kind = mir_cast_kind(source_ty, expr.ty);
                Ok(Rvalue::Cast(cast_kind, source, expr.ty))
            },
            ExprKind::Tuple { fields } => Ok(
                Rvalue::Aggregate(
                    Box::new(AggregateKind::Tuple),
                    fields.iter().map(|e| self.parse_operand(*e)).collect::<Result<_, _>>()?
                )
            ),
            ExprKind::Array { fields } => {
                let elem_ty = expr.ty.builtin_index().expect("ty must be an array");
                Ok(Rvalue::Aggregate(
                    Box::new(AggregateKind::Array(elem_ty)),
                    fields.iter().map(|e| self.parse_operand(*e)).collect::<Result<_, _>>()?
                ))
            },
            ExprKind::Adt(box AdtExpr{ adt_def, variant_index, args, fields, .. }) => {
                let is_union = adt_def.is_union();
                let active_field_index = is_union.then(|| fields[0].name);

                Ok(Rvalue::Aggregate(
                    Box::new(AggregateKind::Adt(adt_def.did(), *variant_index, args, None, active_field_index)),
                    fields.iter().map(|f| self.parse_operand(f.expr)).collect::<Result<_, _>>()?
                ))
            },
            _ => self.parse_operand(expr_id).map(Rvalue::Use),
        )
    }

    pub fn parse_operand(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
        parse_by_kind!(self, expr_id, expr, "operand",
            @call(mir_move, args) => self.parse_place(args[0]).map(Operand::Move),
            @call(mir_static, args) => self.parse_static(args[0]),
            @call(mir_static_mut, args) => self.parse_static(args[0]),
            ExprKind::Literal { .. }
            | ExprKind::NamedConst { .. }
            | ExprKind::NonHirLiteral { .. }
            | ExprKind::ZstLiteral { .. }
            | ExprKind::ConstParam { .. }
            | ExprKind::ConstBlock { .. } => {
                Ok(Operand::Constant(Box::new(
                    as_constant_inner(expr, |_| None, self.tcx)
                )))
            },
            _ => self.parse_place(expr_id).map(Operand::Copy),
        )
    }

    fn parse_place(&self, expr_id: ExprId) -> PResult<Place<'tcx>> {
        self.parse_place_inner(expr_id).map(|(x, _)| x)
    }

    fn parse_place_inner(&self, expr_id: ExprId) -> PResult<(Place<'tcx>, PlaceTy<'tcx>)> {
        let (parent, proj) = parse_by_kind!(self, expr_id, expr, "place",
            @call(mir_field, args) => {
                let (parent, ty) = self.parse_place_inner(args[0])?;
                let field = FieldIdx::from_u32(self.parse_integer_literal(args[1])? as u32);
                let field_ty = ty.field_ty(self.tcx, field);
                let proj = PlaceElem::Field(field, field_ty);
                let place = parent.project_deeper(&[proj], self.tcx);
                return Ok((place, PlaceTy::from_ty(field_ty)));
            },
            @call(mir_variant, args) => {
                (args[0], PlaceElem::Downcast(
                    None,
                    VariantIdx::from_u32(self.parse_integer_literal(args[1])? as u32)
                ))
            },
            ExprKind::Deref { arg } => {
                parse_by_kind!(self, *arg, _, "does not matter",
                    @call(mir_make_place, args) => return self.parse_place_inner(args[0]),
                    _ => (*arg, PlaceElem::Deref),
                )
            },
            ExprKind::Index { lhs, index } => (*lhs, PlaceElem::Index(self.parse_local(*index)?)),
            ExprKind::Field { lhs, name: field, .. } => (*lhs, PlaceElem::Field(*field, expr.ty)),
            _ => {
                let place = self.parse_local(expr_id).map(Place::from)?;
                return Ok((place, PlaceTy::from_ty(expr.ty)))
            },
        );
        let (parent, ty) = self.parse_place_inner(parent)?;
        let place = parent.project_deeper(&[proj], self.tcx);
        let ty = ty.projection_ty(self.tcx, proj);
        Ok((place, ty))
    }

    fn parse_local(&self, expr_id: ExprId) -> PResult<Local> {
        parse_by_kind!(self, expr_id, _, "local",
            ExprKind::VarRef { id } => Ok(self.local_map[id]),
        )
    }

    fn parse_block(&self, expr_id: ExprId) -> PResult<BasicBlock> {
        parse_by_kind!(self, expr_id, _, "basic block",
            ExprKind::VarRef { id } => Ok(self.block_map[id]),
        )
    }

    fn parse_static(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
        let expr_id = parse_by_kind!(self, expr_id, _, "static",
            ExprKind::Deref { arg } => *arg,
        );

        parse_by_kind!(self, expr_id, expr, "static",
            ExprKind::StaticRef { alloc_id, ty, .. } => {
                let const_val =
                    ConstValue::Scalar(Scalar::from_pointer((*alloc_id).into(), &self.tcx));
                let const_ = Const::Val(const_val, *ty);

                Ok(Operand::Constant(Box::new(ConstOperand {
                    span: expr.span,
                    user_ty: None,
                    const_
                })))
            },
        )
    }

    fn parse_integer_literal(&self, expr_id: ExprId) -> PResult<u128> {
        parse_by_kind!(self, expr_id, expr, "constant",
            ExprKind::Literal { .. }
            | ExprKind::NamedConst { .. }
            | ExprKind::NonHirLiteral { .. }
            | ExprKind::ConstBlock { .. } => Ok({
                let value = as_constant_inner(expr, |_| None, self.tcx);
                value.const_.eval_bits(self.tcx, self.param_env)
            }),
        )
    }
}