Skip to content

Commit b5d3580

Browse files
committed
Move the "HAIR" code that reads the tcx tables etc out of the tcx
module and into `hair/cx`, now that we don't have a trait defining the interface
1 parent 3c07b46 commit b5d3580

File tree

9 files changed

+94
-79
lines changed

9 files changed

+94
-79
lines changed

src/librustc_mir/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
// except according to those terms.
1010

1111
use hair;
12+
use hair::cx::{Cx, PatNode};
1213
use rustc::middle::region::CodeExtent;
1314
use rustc::middle::ty::{FnOutput, Ty};
1415
use rustc_data_structures::fnv::FnvHashMap;
1516
use rustc_front::hir;
1617
use repr::*;
1718
use syntax::ast;
1819
use syntax::codemap::Span;
19-
use tcx::{Cx, PatNode};
2020

2121
struct Builder<'a, 'tcx: 'a> {
2222
hir: Cx<'a, 'tcx>,

src/librustc_mir/tcx/block.rs renamed to src/librustc_mir/hair/cx/block.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
// except according to those terms.
1010

1111
use hair::*;
12-
13-
use tcx::Cx;
14-
use tcx::pattern::PatNode;
15-
use tcx::to_ref::ToRef;
12+
use hair::cx::Cx;
13+
use hair::cx::pattern::PatNode;
14+
use hair::cx::to_ref::ToRef;
1615
use rustc::middle::region::{BlockRemainder, CodeExtentData};
1716
use rustc_front::hir;
1817
use syntax::ast;
@@ -34,22 +33,11 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Block {
3433
}
3534
}
3635

37-
impl<'tcx> Mirror<'tcx> for &'tcx hir::Stmt {
38-
type Output = Stmt<'tcx>;
39-
40-
fn make_mirror<'a>(self, _cx: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
41-
// In order to get the scoping correct, we eagerly mirror
42-
// statements when we translate the enclosing block, so we
43-
// should in fact never get to this point.
44-
panic!("statements are eagerly mirrored");
45-
}
46-
}
47-
48-
fn mirror_stmts<'a, 'tcx: 'a, STMTS>(cx: &mut Cx<'a, 'tcx>,
49-
block_id: ast::NodeId,
50-
mut stmts: STMTS)
51-
-> Vec<StmtRef<'tcx>>
52-
where STMTS: Iterator<Item = (usize, &'tcx P<hir::Stmt>)>
36+
fn mirror_stmts<'a,'tcx:'a,STMTS>(cx: &mut Cx<'a,'tcx>,
37+
block_id: ast::NodeId,
38+
mut stmts: STMTS)
39+
-> Vec<StmtRef<'tcx>>
40+
where STMTS: Iterator<Item=(usize, &'tcx P<hir::Stmt>)>
5341
{
5442
let mut result = vec![];
5543
while let Some((index, stmt)) = stmts.next() {

src/librustc_mir/tcx/expr.rs renamed to src/librustc_mir/hair/cx/expr.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ use hair::*;
1212
use repr::*;
1313
use rustc_data_structures::fnv::FnvHashMap;
1414
use std::rc::Rc;
15-
use tcx::Cx;
16-
use tcx::block;
17-
use tcx::pattern::PatNode;
18-
use tcx::to_ref::ToRef;
15+
use hair::cx::Cx;
16+
use hair::cx::block;
17+
use hair::cx::pattern::PatNode;
18+
use hair::cx::to_ref::ToRef;
1919
use rustc::front::map;
2020
use rustc::middle::const_eval;
2121
use rustc::middle::def;
2222
use rustc::middle::region::CodeExtent;
2323
use rustc::middle::pat_util;
24-
use rustc::middle::ty::{self, Ty};
24+
use rustc::middle::ty::{self, VariantDef, Ty};
2525
use rustc_front::hir;
2626
use rustc_front::util as hir_util;
2727
use syntax::parse::token;
@@ -170,11 +170,12 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
170170
hir::ExprStruct(_, ref fields, ref base) => {
171171
match expr_ty.sty {
172172
ty::TyStruct(adt, substs) => {
173+
let field_refs = field_refs(&adt.variants[0], fields);
173174
ExprKind::Adt {
174175
adt_def: adt,
175176
variant_index: 0,
176177
substs: substs,
177-
fields: fields.to_ref(),
178+
fields: field_refs,
178179
base: base.to_ref(),
179180
}
180181
}
@@ -183,11 +184,12 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
183184
def::DefVariant(enum_id, variant_id, true) => {
184185
debug_assert!(adt.did == enum_id);
185186
let index = adt.variant_index_with_id(variant_id);
187+
let field_refs = field_refs(&adt.variants[index], fields);
186188
ExprKind::Adt {
187189
adt_def: adt,
188190
variant_index: index,
189191
substs: substs,
190-
fields: fields.to_ref(),
192+
fields: field_refs,
191193
base: base.to_ref(),
192194
}
193195
}
@@ -238,11 +240,10 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
238240
}
239241
};
240242

241-
let field_expr_ref = |s: &'tcx P<hir::Expr>, nm: &str| {
242-
FieldExprRef {
243-
name: Field::Named(token::intern(nm)),
244-
expr: s.to_ref(),
245-
}
243+
let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
244+
let name = token::intern(name);
245+
let index = adt_def.variants[0].index_of_field_named(name).unwrap();
246+
FieldExprRef { name: Field::new(index), expr: s.to_ref() }
246247
};
247248

248249
let start_field = start.as_ref()
@@ -293,12 +294,25 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
293294
hir::ExprLoop(ref body, _) =>
294295
ExprKind::Loop { condition: None,
295296
body: block::to_expr_ref(cx, body) },
296-
hir::ExprField(ref source, name) =>
297-
ExprKind::Field { lhs: source.to_ref(),
298-
name: Field::Named(name.node) },
297+
hir::ExprField(ref source, name) => {
298+
let index = match cx.tcx.expr_ty_adjusted(source).sty {
299+
ty::TyStruct(adt_def, _) =>
300+
adt_def.variants[0].index_of_field_named(name.node),
301+
ref ty =>
302+
cx.tcx.sess.span_bug(
303+
self.span,
304+
&format!("field of non-struct: {:?}", ty)),
305+
};
306+
let index = index.unwrap_or_else(|| {
307+
cx.tcx.sess.span_bug(
308+
self.span,
309+
&format!("no index found for field `{}`", name.node));
310+
});
311+
ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
312+
}
299313
hir::ExprTupField(ref source, index) =>
300314
ExprKind::Field { lhs: source.to_ref(),
301-
name: Field::Indexed(index.node) },
315+
name: Field::new(index.node as usize) },
302316
hir::ExprCast(ref source, _) =>
303317
ExprKind::Cast { source: source.to_ref() },
304318
hir::ExprBox(ref value) =>
@@ -616,7 +630,7 @@ fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
616630
// at this point we have `self.n`, which loads up the upvar
617631
let field_kind = ExprKind::Field {
618632
lhs: self_expr.to_ref(),
619-
name: Field::Indexed(index),
633+
name: Field::new(index),
620634
};
621635

622636
// ...but the upvar might be an `&T` or `&mut T` capture, at which
@@ -814,3 +828,15 @@ fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> Cod
814828
}
815829
}
816830
}
831+
832+
fn field_refs<'tcx>(variant: VariantDef<'tcx>,
833+
fields: &'tcx [hir::Field])
834+
-> Vec<FieldExprRef<'tcx>>
835+
{
836+
fields.iter()
837+
.map(|field| FieldExprRef {
838+
name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
839+
expr: field.expr.to_ref(),
840+
})
841+
.collect()
842+
}

src/librustc_mir/tcx/mod.rs renamed to src/librustc_mir/hair/cx/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc::middle::infer::InferCtxt;
2424
use rustc::middle::subst::{Subst, Substs};
2525
use rustc::middle::ty::{self, Ty};
2626
use syntax::codemap::Span;
27-
use syntax::parse::token::{self, special_idents};
27+
use syntax::parse::token;
2828

2929
#[derive(Copy, Clone)]
3030
pub struct Cx<'a, 'tcx: 'a> {
@@ -87,18 +87,9 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
8787
adt_def.variants.len()
8888
}
8989

90-
pub fn fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field> {
91-
adt_def.variants[variant_index]
92-
.fields
93-
.iter()
94-
.enumerate()
95-
.map(|(index, field)| {
96-
if field.name == special_idents::unnamed_field.name {
97-
Field::Indexed(index)
98-
} else {
99-
Field::Named(field.name)
100-
}
101-
})
90+
pub fn all_fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field> {
91+
(0..adt_def.variants[variant_index].fields.len())
92+
.map(Field::new)
10293
.collect()
10394
}
10495

src/librustc_mir/tcx/pattern.rs renamed to src/librustc_mir/hair/cx/pattern.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
// except according to those terms.
1010

1111
use hair::*;
12+
use hair::cx::Cx;
13+
use hair::cx::to_ref::ToRef;
1214
use repr::*;
13-
1415
use rustc_data_structures::fnv::FnvHashMap;
1516
use std::rc::Rc;
16-
use tcx::Cx;
17-
use tcx::to_ref::ToRef;
1817
use rustc::middle::const_eval;
1918
use rustc::middle::def;
2019
use rustc::middle::pat_util::{pat_is_resolved_const, pat_is_binding};
@@ -223,7 +222,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
223222
subpatterns.iter()
224223
.enumerate()
225224
.map(|(i, subpattern)| FieldPatternRef {
226-
field: Field::Indexed(i),
225+
field: Field::new(i),
227226
pattern: self.pat_ref(subpattern),
228227
})
229228
.collect();
@@ -273,21 +272,43 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
273272
.flat_map(|v| v.iter())
274273
.enumerate()
275274
.map(|(i, field)| FieldPatternRef {
276-
field: Field::Indexed(i),
275+
field: Field::new(i),
277276
pattern: self.pat_ref(field),
278277
})
279278
.collect();
280279
self.variant_or_leaf(cx, subpatterns)
281280
}
282281

283282
hir::PatStruct(_, ref fields, _) => {
283+
let pat_ty = cx.tcx.node_id_to_type(self.pat.id);
284+
let adt_def = match pat_ty.sty {
285+
ty::TyStruct(adt_def, _) | ty::TyEnum(adt_def, _) => adt_def,
286+
_ => {
287+
cx.tcx.sess.span_bug(
288+
self.pat.span,
289+
"struct pattern not applied to struct or enum");
290+
}
291+
};
292+
293+
let def = cx.tcx.def_map.borrow().get(&self.pat.id).unwrap().full_def();
294+
let variant_def = adt_def.variant_of_def(def);
295+
284296
let subpatterns =
285297
fields.iter()
286-
.map(|field| FieldPatternRef {
287-
field: Field::Named(field.node.name),
288-
pattern: self.pat_ref(&field.node.pat),
298+
.map(|field| {
299+
let index = variant_def.index_of_field_named(field.node.name);
300+
let index = index.unwrap_or_else(|| {
301+
cx.tcx.sess.span_bug(
302+
self.pat.span,
303+
&format!("no field with name {:?}", field.node.name));
304+
});
305+
FieldPatternRef {
306+
field: Field::new(index),
307+
pattern: self.pat_ref(&field.node.pat),
308+
}
289309
})
290310
.collect();
311+
291312
self.variant_or_leaf(cx, subpatterns)
292313
}
293314

src/librustc_mir/tcx/to_ref.rs renamed to src/librustc_mir/hair/cx/to_ref.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
// except according to those terms.
1010

1111
use hair::*;
12-
use repr::*;
1312

14-
use tcx::pattern::PatNode;
13+
use hair::cx::pattern::PatNode;
1514
use rustc_front::hir;
1615
use syntax::ptr::P;
1716

@@ -79,14 +78,3 @@ impl<'a,'tcx:'a,T,U> ToRef for &'tcx Vec<T>
7978
self.iter().map(|expr| expr.to_ref()).collect()
8079
}
8180
}
82-
83-
impl<'a,'tcx:'a> ToRef for &'tcx hir::Field {
84-
type Output = FieldExprRef<'tcx>;
85-
86-
fn to_ref(self) -> FieldExprRef<'tcx> {
87-
FieldExprRef {
88-
name: Field::Named(self.name.node),
89-
expr: self.expr.to_ref(),
90-
}
91-
}
92-
}

src/librustc_mir/hair.rs renamed to src/librustc_mir/hair/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use rustc::middle::ty::{AdtDef, ClosureSubsts, Region, Ty};
2222
use rustc_front::hir;
2323
use syntax::ast;
2424
use syntax::codemap::Span;
25-
use tcx::{Cx, PatNode};
25+
use self::cx::{Cx, PatNode};
26+
27+
pub mod cx;
2628

2729
#[derive(Clone, Debug)]
2830
pub struct ItemRef<'tcx> {
@@ -41,7 +43,6 @@ pub struct Block<'tcx> {
4143

4244
#[derive(Clone, Debug)]
4345
pub enum StmtRef<'tcx> {
44-
Hair(&'tcx hir::Stmt),
4546
Mirror(Box<Stmt<'tcx>>),
4647
}
4748

@@ -392,9 +393,8 @@ impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
392393
impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
393394
type Output = Stmt<'tcx>;
394395

395-
fn make_mirror<'a>(self, hir: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
396+
fn make_mirror<'a>(self, _: &mut Cx<'a,'tcx>) -> Stmt<'tcx> {
396397
match self {
397-
StmtRef::Hair(h) => h.make_mirror(hir),
398398
StmtRef::Mirror(m) => *m,
399399
}
400400
}

src/librustc_mir/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ extern crate syntax;
3131

3232
pub mod build;
3333
pub mod mir_map;
34-
pub mod hair;
34+
mod hair;
3535
pub mod repr;
3636
mod graphviz;
37-
mod tcx;
37+
38+

src/librustc_mir/mir_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ extern crate rustc_front;
2323
use build;
2424
use dot;
2525
use repr::Mir;
26+
use hair::cx::{PatNode, Cx};
2627
use std::fs::File;
27-
use tcx::{PatNode, Cx};
2828

2929
use self::rustc::middle::infer;
3030
use self::rustc::middle::region::CodeExtentData;

0 commit comments

Comments
 (0)