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
use crate::build::scope::BreakableTarget;
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::thir::*;
impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn stmt_expr(
&mut self,
mut block: BasicBlock,
expr: &Expr<'tcx>,
statement_scope: Option<region::Scope>,
) -> BlockAnd<()> {
let this = self;
let expr_span = expr.span;
let source_info = this.source_info(expr.span);
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
this.stmt_expr(block, &this.thir[value], statement_scope)
})
}
ExprKind::Assign { lhs, rhs } => {
let lhs = &this.thir[lhs];
let rhs = &this.thir[rhs];
let lhs_span = lhs.span;
debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
if lhs.ty.needs_drop(this.tcx, this.param_env) {
let rhs = unpack!(block = this.as_local_operand(block, rhs));
let lhs = unpack!(block = this.as_place(block, lhs));
unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
} else {
let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
let lhs = unpack!(block = this.as_place(block, lhs));
this.cfg.push_assign(block, source_info, lhs, rhs);
}
this.block_context.pop();
block.unit()
}
ExprKind::AssignOp { op, lhs, rhs } => {
let lhs = &this.thir[lhs];
let rhs = &this.thir[rhs];
let lhs_ty = lhs.ty;
debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
let rhs = unpack!(block = this.as_local_operand(block, rhs));
let lhs = unpack!(block = this.as_place(block, lhs));
let result = unpack!(
block =
this.build_binary_op(block, op, expr_span, lhs_ty, Operand::Copy(lhs), rhs)
);
this.cfg.push_assign(block, source_info, lhs, result);
this.block_context.pop();
block.unit()
}
ExprKind::Continue { label } => {
this.break_scope(block, None, BreakableTarget::Continue(label), source_info)
}
ExprKind::Break { label, value } => this.break_scope(
block,
value.map(|value| &this.thir[value]),
BreakableTarget::Break(label),
source_info,
),
ExprKind::Return { value } => this.break_scope(
block,
value.map(|value| &this.thir[value]),
BreakableTarget::Return,
source_info,
),
ExprKind::LlvmInlineAsm { asm, ref outputs, ref inputs } => {
debug!("stmt_expr LlvmInlineAsm block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
let outputs = outputs
.into_iter()
.copied()
.map(|output| unpack!(block = this.as_place(block, &this.thir[output])))
.collect::<Vec<_>>()
.into_boxed_slice();
let inputs = inputs
.into_iter()
.copied()
.map(|input| {
let input = &this.thir[input];
(input.span, unpack!(block = this.as_local_operand(block, &input)))
})
.collect::<Vec<_>>()
.into_boxed_slice();
this.cfg.push(
block,
Statement {
source_info,
kind: StatementKind::LlvmInlineAsm(Box::new(LlvmInlineAsm {
asm: asm.clone(),
outputs,
inputs,
})),
},
);
this.block_context.pop();
block.unit()
}
_ => {
assert!(
statement_scope.is_some(),
"Should not be calling `stmt_expr` on a general expression \
without a statement scope",
);
let adjusted_span = (|| {
if let ExprKind::Block { body } = &expr.kind {
if let Some(tail_expr) = body.expr {
let mut expr = &this.thir[tail_expr];
while let ExprKind::Block {
body: Block { expr: Some(nested_expr), .. },
}
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
{
expr = &this.thir[nested_expr];
}
this.block_context.push(BlockFrame::TailExpr {
tail_result_is_ignored: true,
span: expr.span,
});
return Some(expr.span);
}
}
None
})();
let temp =
unpack!(block = this.as_temp(block, statement_scope, expr, Mutability::Not));
if let Some(span) = adjusted_span {
this.local_decls[temp].source_info.span = span;
this.block_context.pop();
}
block.unit()
}
}
}
}