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
This commit is contained in:
Niko Matsakis 2015-10-21 17:24:05 -04:00
parent 3c07b46118
commit b5d3580843
9 changed files with 94 additions and 79 deletions

View File

@ -9,6 +9,7 @@
// except according to those terms.
use hair;
use hair::cx::{Cx, PatNode};
use rustc::middle::region::CodeExtent;
use rustc::middle::ty::{FnOutput, Ty};
use rustc_data_structures::fnv::FnvHashMap;
@ -16,7 +17,6 @@ use rustc_front::hir;
use repr::*;
use syntax::ast;
use syntax::codemap::Span;
use tcx::{Cx, PatNode};
struct Builder<'a, 'tcx: 'a> {
hir: Cx<'a, 'tcx>,

View File

@ -9,10 +9,9 @@
// except according to those terms.
use hair::*;
use tcx::Cx;
use tcx::pattern::PatNode;
use tcx::to_ref::ToRef;
use hair::cx::Cx;
use hair::cx::pattern::PatNode;
use hair::cx::to_ref::ToRef;
use rustc::middle::region::{BlockRemainder, CodeExtentData};
use rustc_front::hir;
use syntax::ast;
@ -34,22 +33,11 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Block {
}
}
impl<'tcx> Mirror<'tcx> for &'tcx hir::Stmt {
type Output = Stmt<'tcx>;
fn make_mirror<'a>(self, _cx: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
// In order to get the scoping correct, we eagerly mirror
// statements when we translate the enclosing block, so we
// should in fact never get to this point.
panic!("statements are eagerly mirrored");
}
}
fn mirror_stmts<'a, 'tcx: 'a, STMTS>(cx: &mut Cx<'a, 'tcx>,
block_id: ast::NodeId,
mut stmts: STMTS)
-> Vec<StmtRef<'tcx>>
where STMTS: Iterator<Item = (usize, &'tcx P<hir::Stmt>)>
fn mirror_stmts<'a,'tcx:'a,STMTS>(cx: &mut Cx<'a,'tcx>,
block_id: ast::NodeId,
mut stmts: STMTS)
-> Vec<StmtRef<'tcx>>
where STMTS: Iterator<Item=(usize, &'tcx P<hir::Stmt>)>
{
let mut result = vec![];
while let Some((index, stmt)) = stmts.next() {

View File

@ -12,16 +12,16 @@ use hair::*;
use repr::*;
use rustc_data_structures::fnv::FnvHashMap;
use std::rc::Rc;
use tcx::Cx;
use tcx::block;
use tcx::pattern::PatNode;
use tcx::to_ref::ToRef;
use hair::cx::Cx;
use hair::cx::block;
use hair::cx::pattern::PatNode;
use hair::cx::to_ref::ToRef;
use rustc::front::map;
use rustc::middle::const_eval;
use rustc::middle::def;
use rustc::middle::region::CodeExtent;
use rustc::middle::pat_util;
use rustc::middle::ty::{self, Ty};
use rustc::middle::ty::{self, VariantDef, Ty};
use rustc_front::hir;
use rustc_front::util as hir_util;
use syntax::parse::token;
@ -170,11 +170,12 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
hir::ExprStruct(_, ref fields, ref base) => {
match expr_ty.sty {
ty::TyStruct(adt, substs) => {
let field_refs = field_refs(&adt.variants[0], fields);
ExprKind::Adt {
adt_def: adt,
variant_index: 0,
substs: substs,
fields: fields.to_ref(),
fields: field_refs,
base: base.to_ref(),
}
}
@ -183,11 +184,12 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
def::DefVariant(enum_id, variant_id, true) => {
debug_assert!(adt.did == enum_id);
let index = adt.variant_index_with_id(variant_id);
let field_refs = field_refs(&adt.variants[index], fields);
ExprKind::Adt {
adt_def: adt,
variant_index: index,
substs: substs,
fields: fields.to_ref(),
fields: field_refs,
base: base.to_ref(),
}
}
@ -238,11 +240,10 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
}
};
let field_expr_ref = |s: &'tcx P<hir::Expr>, nm: &str| {
FieldExprRef {
name: Field::Named(token::intern(nm)),
expr: s.to_ref(),
}
let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
let name = token::intern(name);
let index = adt_def.variants[0].index_of_field_named(name).unwrap();
FieldExprRef { name: Field::new(index), expr: s.to_ref() }
};
let start_field = start.as_ref()
@ -293,12 +294,25 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
hir::ExprLoop(ref body, _) =>
ExprKind::Loop { condition: None,
body: block::to_expr_ref(cx, body) },
hir::ExprField(ref source, name) =>
ExprKind::Field { lhs: source.to_ref(),
name: Field::Named(name.node) },
hir::ExprField(ref source, name) => {
let index = match cx.tcx.expr_ty_adjusted(source).sty {
ty::TyStruct(adt_def, _) =>
adt_def.variants[0].index_of_field_named(name.node),
ref ty =>
cx.tcx.sess.span_bug(
self.span,
&format!("field of non-struct: {:?}", ty)),
};
let index = index.unwrap_or_else(|| {
cx.tcx.sess.span_bug(
self.span,
&format!("no index found for field `{}`", name.node));
});
ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
}
hir::ExprTupField(ref source, index) =>
ExprKind::Field { lhs: source.to_ref(),
name: Field::Indexed(index.node) },
name: Field::new(index.node as usize) },
hir::ExprCast(ref source, _) =>
ExprKind::Cast { source: source.to_ref() },
hir::ExprBox(ref value) =>
@ -616,7 +630,7 @@ fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
// at this point we have `self.n`, which loads up the upvar
let field_kind = ExprKind::Field {
lhs: self_expr.to_ref(),
name: Field::Indexed(index),
name: Field::new(index),
};
// ...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
}
}
}
fn field_refs<'tcx>(variant: VariantDef<'tcx>,
fields: &'tcx [hir::Field])
-> Vec<FieldExprRef<'tcx>>
{
fields.iter()
.map(|field| FieldExprRef {
name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
expr: field.expr.to_ref(),
})
.collect()
}

View File

@ -24,7 +24,7 @@ use rustc::middle::infer::InferCtxt;
use rustc::middle::subst::{Subst, Substs};
use rustc::middle::ty::{self, Ty};
use syntax::codemap::Span;
use syntax::parse::token::{self, special_idents};
use syntax::parse::token;
#[derive(Copy, Clone)]
pub struct Cx<'a, 'tcx: 'a> {
@ -87,18 +87,9 @@ impl<'a,'tcx:'a> Cx<'a, 'tcx> {
adt_def.variants.len()
}
pub fn fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field> {
adt_def.variants[variant_index]
.fields
.iter()
.enumerate()
.map(|(index, field)| {
if field.name == special_idents::unnamed_field.name {
Field::Indexed(index)
} else {
Field::Named(field.name)
}
})
pub fn all_fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field> {
(0..adt_def.variants[variant_index].fields.len())
.map(Field::new)
.collect()
}

View File

@ -9,12 +9,11 @@
// except according to those terms.
use hair::*;
use hair::cx::Cx;
use hair::cx::to_ref::ToRef;
use repr::*;
use rustc_data_structures::fnv::FnvHashMap;
use std::rc::Rc;
use tcx::Cx;
use tcx::to_ref::ToRef;
use rustc::middle::const_eval;
use rustc::middle::def;
use rustc::middle::pat_util::{pat_is_resolved_const, pat_is_binding};
@ -223,7 +222,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
subpatterns.iter()
.enumerate()
.map(|(i, subpattern)| FieldPatternRef {
field: Field::Indexed(i),
field: Field::new(i),
pattern: self.pat_ref(subpattern),
})
.collect();
@ -273,7 +272,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
.flat_map(|v| v.iter())
.enumerate()
.map(|(i, field)| FieldPatternRef {
field: Field::Indexed(i),
field: Field::new(i),
pattern: self.pat_ref(field),
})
.collect();
@ -281,13 +280,35 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
}
hir::PatStruct(_, ref fields, _) => {
let pat_ty = cx.tcx.node_id_to_type(self.pat.id);
let adt_def = match pat_ty.sty {
ty::TyStruct(adt_def, _) | ty::TyEnum(adt_def, _) => adt_def,
_ => {
cx.tcx.sess.span_bug(
self.pat.span,
"struct pattern not applied to struct or enum");
}
};
let def = cx.tcx.def_map.borrow().get(&self.pat.id).unwrap().full_def();
let variant_def = adt_def.variant_of_def(def);
let subpatterns =
fields.iter()
.map(|field| FieldPatternRef {
field: Field::Named(field.node.name),
pattern: self.pat_ref(&field.node.pat),
.map(|field| {
let index = variant_def.index_of_field_named(field.node.name);
let index = index.unwrap_or_else(|| {
cx.tcx.sess.span_bug(
self.pat.span,
&format!("no field with name {:?}", field.node.name));
});
FieldPatternRef {
field: Field::new(index),
pattern: self.pat_ref(&field.node.pat),
}
})
.collect();
self.variant_or_leaf(cx, subpatterns)
}

View File

@ -9,9 +9,8 @@
// except according to those terms.
use hair::*;
use repr::*;
use tcx::pattern::PatNode;
use hair::cx::pattern::PatNode;
use rustc_front::hir;
use syntax::ptr::P;
@ -79,14 +78,3 @@ impl<'a,'tcx:'a,T,U> ToRef for &'tcx Vec<T>
self.iter().map(|expr| expr.to_ref()).collect()
}
}
impl<'a,'tcx:'a> ToRef for &'tcx hir::Field {
type Output = FieldExprRef<'tcx>;
fn to_ref(self) -> FieldExprRef<'tcx> {
FieldExprRef {
name: Field::Named(self.name.node),
expr: self.expr.to_ref(),
}
}
}

View File

@ -22,7 +22,9 @@ use rustc::middle::ty::{AdtDef, ClosureSubsts, Region, Ty};
use rustc_front::hir;
use syntax::ast;
use syntax::codemap::Span;
use tcx::{Cx, PatNode};
use self::cx::{Cx, PatNode};
pub mod cx;
#[derive(Clone, Debug)]
pub struct ItemRef<'tcx> {
@ -41,7 +43,6 @@ pub struct Block<'tcx> {
#[derive(Clone, Debug)]
pub enum StmtRef<'tcx> {
Hair(&'tcx hir::Stmt),
Mirror(Box<Stmt<'tcx>>),
}
@ -392,9 +393,8 @@ impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
type Output = Stmt<'tcx>;
fn make_mirror<'a>(self, hir: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
fn make_mirror<'a>(self, _: &mut Cx<'a,'tcx>) -> Stmt<'tcx> {
match self {
StmtRef::Hair(h) => h.make_mirror(hir),
StmtRef::Mirror(m) => *m,
}
}

View File

@ -31,7 +31,8 @@ extern crate syntax;
pub mod build;
pub mod mir_map;
pub mod hair;
mod hair;
pub mod repr;
mod graphviz;
mod tcx;

View File

@ -23,8 +23,8 @@ extern crate rustc_front;
use build;
use dot;
use repr::Mir;
use hair::cx::{PatNode, Cx};
use std::fs::File;
use tcx::{PatNode, Cx};
use self::rustc::middle::infer;
use self::rustc::middle::region::CodeExtentData;