]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/mir/statement.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / librustc_trans / mir / statement.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::mir::repr as mir;
12
13 use base;
14 use common::{self, BlockAndBuilder};
15
16 use super::MirContext;
17 use super::LocalRef;
18 use super::super::adt;
19 use super::super::disr::Disr;
20
21 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
22 pub fn trans_statement(&mut self,
23 bcx: BlockAndBuilder<'bcx, 'tcx>,
24 statement: &mir::Statement<'tcx>)
25 -> BlockAndBuilder<'bcx, 'tcx> {
26 debug!("trans_statement(statement={:?})", statement);
27
28 let debug_loc = self.debug_loc(statement.source_info);
29 debug_loc.apply_to_bcx(&bcx);
30 debug_loc.apply(bcx.fcx());
31 match statement.kind {
32 mir::StatementKind::Assign(ref lvalue, ref rvalue) => {
33 if let Some(index) = self.mir.local_index(lvalue) {
34 match self.locals[index] {
35 LocalRef::Lvalue(tr_dest) => {
36 self.trans_rvalue(bcx, tr_dest, rvalue, debug_loc)
37 }
38 LocalRef::Operand(None) => {
39 let (bcx, operand) = self.trans_rvalue_operand(bcx, rvalue,
40 debug_loc);
41 self.locals[index] = LocalRef::Operand(Some(operand));
42 bcx
43 }
44 LocalRef::Operand(Some(_)) => {
45 let ty = self.monomorphized_lvalue_ty(lvalue);
46
47 if !common::type_is_zero_size(bcx.ccx(), ty) {
48 span_bug!(statement.source_info.span,
49 "operand {:?} already assigned",
50 rvalue);
51 } else {
52 // If the type is zero-sized, it's already been set here,
53 // but we still need to make sure we translate the operand
54 self.trans_rvalue_operand(bcx, rvalue, debug_loc).0
55 }
56 }
57 }
58 } else {
59 let tr_dest = self.trans_lvalue(&bcx, lvalue);
60 self.trans_rvalue(bcx, tr_dest, rvalue, debug_loc)
61 }
62 }
63 mir::StatementKind::SetDiscriminant{ref lvalue, variant_index} => {
64 let ty = self.monomorphized_lvalue_ty(lvalue);
65 let repr = adt::represent_type(bcx.ccx(), ty);
66 let lvalue_transed = self.trans_lvalue(&bcx, lvalue);
67 bcx.with_block(|bcx|
68 adt::trans_set_discr(bcx,
69 &repr,
70 lvalue_transed.llval,
71 Disr::from(variant_index))
72 );
73 bcx
74 }
75 mir::StatementKind::StorageLive(ref lvalue) => {
76 self.trans_storage_liveness(bcx, lvalue, base::Lifetime::Start)
77 }
78 mir::StatementKind::StorageDead(ref lvalue) => {
79 self.trans_storage_liveness(bcx, lvalue, base::Lifetime::End)
80 }
81 }
82 }
83
84 fn trans_storage_liveness(&self,
85 bcx: BlockAndBuilder<'bcx, 'tcx>,
86 lvalue: &mir::Lvalue<'tcx>,
87 intrinsic: base::Lifetime)
88 -> BlockAndBuilder<'bcx, 'tcx> {
89 if let Some(index) = self.mir.local_index(lvalue) {
90 if let LocalRef::Lvalue(tr_lval) = self.locals[index] {
91 intrinsic.call(&bcx, tr_lval.llval);
92 }
93 }
94 bcx
95 }
96 }