]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
Auto merge of #104160 - Ayush1325:windows-args, r=m-ou-se
[rust.git] / compiler / rustc_mir_build / src / build / custom / parse / instruction.rs
1 use rustc_middle::{mir::*, thir::*, ty};
2
3 use super::{parse_by_kind, PResult, ParseCtxt};
4
5 impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
6     pub fn parse_statement(&self, expr_id: ExprId) -> PResult<StatementKind<'tcx>> {
7         parse_by_kind!(self, expr_id, "statement",
8             @call("mir_retag", args) => {
9                 Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?)))
10             },
11             @call("mir_retag_raw", args) => {
12                 Ok(StatementKind::Retag(RetagKind::Raw, Box::new(self.parse_place(args[0])?)))
13             },
14             ExprKind::Assign { lhs, rhs } => {
15                 let lhs = self.parse_place(*lhs)?;
16                 let rhs = self.parse_rvalue(*rhs)?;
17                 Ok(StatementKind::Assign(Box::new((lhs, rhs))))
18             },
19         )
20     }
21
22     pub fn parse_terminator(&self, expr_id: ExprId) -> PResult<TerminatorKind<'tcx>> {
23         parse_by_kind!(self, expr_id, "terminator",
24             @call("mir_return", _args) => {
25                 Ok(TerminatorKind::Return)
26             },
27             @call("mir_goto", args) => {
28                 Ok(TerminatorKind::Goto { target: self.parse_block(args[0])? } )
29             },
30         )
31     }
32
33     fn parse_rvalue(&self, expr_id: ExprId) -> PResult<Rvalue<'tcx>> {
34         parse_by_kind!(self, expr_id, "rvalue",
35             ExprKind::Borrow { borrow_kind, arg } => Ok(
36                 Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
37             ),
38             ExprKind::AddressOf { mutability, arg } => Ok(
39                 Rvalue::AddressOf(*mutability, self.parse_place(*arg)?)
40             ),
41             _ => self.parse_operand(expr_id).map(Rvalue::Use),
42         )
43     }
44
45     fn parse_operand(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
46         parse_by_kind!(self, expr_id, "operand",
47             @call("mir_move", args) => self.parse_place(args[0]).map(Operand::Move),
48             _ => self.parse_place(expr_id).map(Operand::Copy),
49         )
50     }
51
52     fn parse_place(&self, expr_id: ExprId) -> PResult<Place<'tcx>> {
53         parse_by_kind!(self, expr_id, "place",
54             ExprKind::Deref { arg } => Ok(
55                 self.parse_place(*arg)?.project_deeper(&[PlaceElem::Deref], self.tcx)
56             ),
57             _ => self.parse_local(expr_id).map(Place::from),
58         )
59     }
60
61     fn parse_local(&self, expr_id: ExprId) -> PResult<Local> {
62         parse_by_kind!(self, expr_id, "local",
63             ExprKind::VarRef { id } => Ok(self.local_map[id]),
64         )
65     }
66
67     fn parse_block(&self, expr_id: ExprId) -> PResult<BasicBlock> {
68         parse_by_kind!(self, expr_id, "basic block",
69             ExprKind::VarRef { id } => Ok(self.block_map[id]),
70         )
71     }
72 }