1 // Copyright 2015 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.
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.
13 use hair
::cx
::to_ref
::ToRef
;
14 use rustc
::middle
::region
::{BlockRemainder, CodeExtentData}
;
18 impl<'tcx
> Mirror
<'tcx
> for &'tcx hir
::Block
{
19 type Output
= Block
<'tcx
>;
21 fn make_mirror
<'a
, 'gcx
>(self, cx
: &mut Cx
<'a
, 'gcx
, 'tcx
>) -> Block
<'tcx
> {
22 // We have to eagerly translate the "spine" of the statements
23 // in order to get the lexical scoping correctly.
24 let stmts
= mirror_stmts(cx
, self.id
, &*self.stmts
);
26 extent
: cx
.tcx
.region_maps
.node_extent(self.id
),
29 expr
: self.expr
.to_ref(),
34 fn mirror_stmts
<'a
, 'gcx
, 'tcx
>(cx
: &mut Cx
<'a
, 'gcx
, 'tcx
>,
35 block_id
: ast
::NodeId
,
36 stmts
: &'tcx
[hir
::Stmt
])
37 -> Vec
<StmtRef
<'tcx
>> {
38 let mut result
= vec
![];
39 for (index
, stmt
) in stmts
.iter().enumerate() {
41 hir
::StmtExpr(ref expr
, id
) |
42 hir
::StmtSemi(ref expr
, id
) => {
43 result
.push(StmtRef
::Mirror(Box
::new(Stmt
{
45 kind
: StmtKind
::Expr
{
46 scope
: cx
.tcx
.region_maps
.node_extent(id
),
51 hir
::StmtDecl(ref decl
, id
) => {
53 hir
::DeclItem(..) => {
54 // ignore for purposes of the MIR
56 hir
::DeclLocal(ref local
) => {
57 let remainder_extent
= CodeExtentData
::Remainder(BlockRemainder
{
59 first_statement_index
: index
as u32,
61 let remainder_extent
=
62 cx
.tcx
.region_maps
.lookup_code_extent(remainder_extent
);
64 let pattern
= Pattern
::from_hir(cx
.tcx
, &local
.pat
);
65 result
.push(StmtRef
::Mirror(Box
::new(Stmt
{
68 remainder_scope
: remainder_extent
,
69 init_scope
: cx
.tcx
.region_maps
.node_extent(id
),
71 initializer
: local
.init
.to_ref(),
82 pub fn to_expr_ref
<'a
, 'gcx
, 'tcx
>(cx
: &mut Cx
<'a
, 'gcx
, 'tcx
>,
83 block
: &'tcx hir
::Block
)
85 let block_ty
= cx
.tcx
.tables().node_id_to_type(block
.id
);
86 let temp_lifetime
= cx
.tcx
.region_maps
.temporary_scope(block
.id
);
89 temp_lifetime
: temp_lifetime
,
91 kind
: ExprKind
::Block { body: block }
,