]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/statement.rs
Rollup merge of #68342 - lcnr:type_name_docs, r=Dylan-DPC
[rust.git] / src / librustc_codegen_ssa / mir / statement.rs
1 use rustc::mir;
2 use rustc_errors::struct_span_err;
3
4 use super::FunctionCx;
5 use super::LocalRef;
6 use super::OperandValue;
7 use crate::traits::BuilderMethods;
8 use crate::traits::*;
9
10 use rustc_error_codes::*;
11
12 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13     pub fn codegen_statement(&mut self, mut bx: Bx, statement: &mir::Statement<'tcx>) -> Bx {
14         debug!("codegen_statement(statement={:?})", statement);
15
16         self.set_debug_loc(&mut bx, statement.source_info);
17         match statement.kind {
18             mir::StatementKind::Assign(box (ref place, ref rvalue)) => {
19                 if let Some(index) = place.as_local() {
20                     match self.locals[index] {
21                         LocalRef::Place(cg_dest) => self.codegen_rvalue(bx, cg_dest, rvalue),
22                         LocalRef::UnsizedPlace(cg_indirect_dest) => {
23                             self.codegen_rvalue_unsized(bx, cg_indirect_dest, rvalue)
24                         }
25                         LocalRef::Operand(None) => {
26                             let (mut bx, operand) = self.codegen_rvalue_operand(bx, rvalue);
27                             self.locals[index] = LocalRef::Operand(Some(operand));
28                             self.debug_introduce_local(&mut bx, index);
29                             bx
30                         }
31                         LocalRef::Operand(Some(op)) => {
32                             if !op.layout.is_zst() {
33                                 span_bug!(
34                                     statement.source_info.span,
35                                     "operand {:?} already assigned",
36                                     rvalue
37                                 );
38                             }
39
40                             // If the type is zero-sized, it's already been set here,
41                             // but we still need to make sure we codegen the operand
42                             self.codegen_rvalue_operand(bx, rvalue).0
43                         }
44                     }
45                 } else {
46                     let cg_dest = self.codegen_place(&mut bx, &place.as_ref());
47                     self.codegen_rvalue(bx, cg_dest, rvalue)
48                 }
49             }
50             mir::StatementKind::SetDiscriminant { box ref place, variant_index } => {
51                 self.codegen_place(&mut bx, &place.as_ref())
52                     .codegen_set_discr(&mut bx, variant_index);
53                 bx
54             }
55             mir::StatementKind::StorageLive(local) => {
56                 if let LocalRef::Place(cg_place) = self.locals[local] {
57                     cg_place.storage_live(&mut bx);
58                 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
59                     cg_indirect_place.storage_live(&mut bx);
60                 }
61                 bx
62             }
63             mir::StatementKind::StorageDead(local) => {
64                 if let LocalRef::Place(cg_place) = self.locals[local] {
65                     cg_place.storage_dead(&mut bx);
66                 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
67                     cg_indirect_place.storage_dead(&mut bx);
68                 }
69                 bx
70             }
71             mir::StatementKind::InlineAsm(ref asm) => {
72                 let outputs = asm
73                     .outputs
74                     .iter()
75                     .map(|output| self.codegen_place(&mut bx, &output.as_ref()))
76                     .collect();
77
78                 let input_vals = asm.inputs.iter().fold(
79                     Vec::with_capacity(asm.inputs.len()),
80                     |mut acc, (span, input)| {
81                         let op = self.codegen_operand(&mut bx, input);
82                         if let OperandValue::Immediate(_) = op.val {
83                             acc.push(op.immediate());
84                         } else {
85                             struct_span_err!(
86                                 bx.sess(),
87                                 span.to_owned(),
88                                 E0669,
89                                 "invalid value for constraint in inline assembly"
90                             )
91                             .emit();
92                         }
93                         acc
94                     },
95                 );
96
97                 if input_vals.len() == asm.inputs.len() {
98                     let res = bx.codegen_inline_asm(
99                         &asm.asm,
100                         outputs,
101                         input_vals,
102                         statement.source_info.span,
103                     );
104                     if !res {
105                         struct_span_err!(
106                             bx.sess(),
107                             statement.source_info.span,
108                             E0668,
109                             "malformed inline assembly"
110                         )
111                         .emit();
112                     }
113                 }
114                 bx
115             }
116             mir::StatementKind::FakeRead(..)
117             | mir::StatementKind::Retag { .. }
118             | mir::StatementKind::AscribeUserType(..)
119             | mir::StatementKind::Nop => bx,
120         }
121     }
122 }