Skip to content

Commit

Permalink
rustc: separate bodies for static/(associated)const and embedded cons…
Browse files Browse the repository at this point in the history
…tants.
  • Loading branch information
eddyb committed Dec 28, 2016
1 parent 8649282 commit e64f64a
Show file tree
Hide file tree
Showing 72 changed files with 637 additions and 655 deletions.
7 changes: 2 additions & 5 deletions src/librustc/cfg/construct.rs
Expand Up @@ -327,10 +327,6 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
self.opt_expr(base, field_cfg)
}

hir::ExprRepeat(ref elem, ref count) => {
self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
}

hir::ExprAssign(ref l, ref r) |
hir::ExprAssignOp(_, ref l, ref r) => {
self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
Expand All @@ -347,7 +343,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
hir::ExprType(ref e, _) |
hir::ExprUnary(_, ref e) |
hir::ExprField(ref e, _) |
hir::ExprTupField(ref e, _) => {
hir::ExprTupField(ref e, _) |
hir::ExprRepeat(ref e, _) => {
self.straightline(expr, pred, Some(&**e).into_iter())
}

Expand Down
1 change: 0 additions & 1 deletion src/librustc/dep_graph/visit.rs
Expand Up @@ -50,7 +50,6 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
let task_id = (self.dep_node_fn)(trait_item_def_id);
let _task = self.tcx.dep_graph.in_task(task_id.clone());
debug!("Started task {:?}", task_id);
assert!(!self.tcx.map.is_inlined_def_id(trait_item_def_id));
self.tcx.dep_graph.read(DepNode::Hir(trait_item_def_id));
self.visitor.visit_trait_item(i);
debug!("Ended task {:?}", task_id);
Expand Down
70 changes: 31 additions & 39 deletions src/librustc/hir/intravisit.rs
Expand Up @@ -203,10 +203,10 @@ pub trait Visitor<'v> : Sized {
/// visit_nested_item, does nothing by default unless you override
/// `nested_visit_map` to return `Some(_)`, in which case it will walk the
/// body.
fn visit_body(&mut self, id: ExprId) {
let opt_expr = self.nested_visit_map().intra().map(|map| map.expr(id));
if let Some(expr) = opt_expr {
self.visit_expr(expr);
fn visit_nested_body(&mut self, id: BodyId) {
let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
if let Some(body) = opt_body {
self.visit_body(body);
}
}

Expand All @@ -216,6 +216,10 @@ pub trait Visitor<'v> : Sized {
walk_item(self, i)
}

fn visit_body(&mut self, b: &'v Body) {
walk_body(self, b);
}

/// When invoking `visit_all_item_likes()`, you need to supply an
/// item-like visitor. This method converts a "intra-visit"
/// visitor into an item-like visitor that walks the entire tree.
Expand Down Expand Up @@ -264,8 +268,6 @@ pub trait Visitor<'v> : Sized {
fn visit_expr(&mut self, ex: &'v Expr) {
walk_expr(self, ex)
}
fn visit_expr_post(&mut self, _ex: &'v Expr) {
}
fn visit_ty(&mut self, t: &'v Ty) {
walk_ty(self, t)
}
Expand All @@ -278,7 +280,7 @@ pub trait Visitor<'v> : Sized {
fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
walk_fn_decl(self, fd)
}
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: ExprId, s: Span, id: NodeId) {
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
walk_fn(self, fk, fd, b, s, id)
}
fn visit_trait_item(&mut self, ti: &'v TraitItem) {
Expand Down Expand Up @@ -392,6 +394,10 @@ pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_i
}
}

pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
visitor.visit_expr(&body.value);
}

pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
visitor.visit_id(local.id);
visitor.visit_pat(&local.pat);
Expand Down Expand Up @@ -437,11 +443,11 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_id(item.id);
visitor.visit_path(path, item.id);
}
ItemStatic(ref typ, _, ref expr) |
ItemConst(ref typ, ref expr) => {
ItemStatic(ref typ, _, body) |
ItemConst(ref typ, body) => {
visitor.visit_id(item.id);
visitor.visit_ty(typ);
visitor.visit_expr(expr);
visitor.visit_nested_body(body);
}
ItemFn(ref declaration, unsafety, constness, abi, ref generics, body_id) => {
visitor.visit_fn(FnKind::ItemFn(item.name,
Expand Down Expand Up @@ -523,7 +529,7 @@ pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
generics,
parent_item_id,
variant.span);
walk_list!(visitor, visit_expr, &variant.node.disr_expr);
walk_list!(visitor, visit_nested_body, variant.node.disr_expr);
walk_list!(visitor, visit_attribute, &variant.node.attrs);
}

Expand Down Expand Up @@ -556,18 +562,18 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
visitor.visit_ty(ty);
walk_list!(visitor, visit_ty_param_bound, bounds);
}
TyArray(ref ty, ref expression) => {
TyArray(ref ty, length) => {
visitor.visit_ty(ty);
visitor.visit_expr(expression)
visitor.visit_nested_body(length)
}
TyPolyTraitRef(ref bounds) => {
walk_list!(visitor, visit_ty_param_bound, bounds);
}
TyImplTrait(ref bounds) => {
walk_list!(visitor, visit_ty_param_bound, bounds);
}
TyTypeof(ref expression) => {
visitor.visit_expr(expression)
TyTypeof(expression) => {
visitor.visit_nested_body(expression)
}
TyInfer => {}
}
Expand Down Expand Up @@ -775,35 +781,23 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
function_kind: FnKind<'v>,
function_declaration: &'v FnDecl,
body_id: ExprId,
body_id: BodyId,
_span: Span,
id: NodeId) {
visitor.visit_id(id);
visitor.visit_fn_decl(function_declaration);
walk_fn_kind(visitor, function_kind);
visitor.visit_body(body_id)
}

pub fn walk_fn_with_body<'v, V: Visitor<'v>>(visitor: &mut V,
function_kind: FnKind<'v>,
function_declaration: &'v FnDecl,
body: &'v Expr,
_span: Span,
id: NodeId) {
visitor.visit_id(id);
visitor.visit_fn_decl(function_declaration);
walk_fn_kind(visitor, function_kind);
visitor.visit_expr(body)
visitor.visit_nested_body(body_id)
}

pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
visitor.visit_name(trait_item.span, trait_item.name);
walk_list!(visitor, visit_attribute, &trait_item.attrs);
match trait_item.node {
TraitItemKind::Const(ref ty, ref default) => {
TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.id);
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, default);
walk_list!(visitor, visit_nested_body, default);
}
TraitItemKind::Method(ref sig, None) => {
visitor.visit_id(trait_item.id);
Expand Down Expand Up @@ -846,10 +840,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
visitor.visit_defaultness(defaultness);
walk_list!(visitor, visit_attribute, attrs);
match *node {
ImplItemKind::Const(ref ty, ref expr) => {
ImplItemKind::Const(ref ty, body) => {
visitor.visit_id(impl_item.id);
visitor.visit_ty(ty);
visitor.visit_expr(expr);
visitor.visit_nested_body(body);
}
ImplItemKind::Method(ref sig, body_id) => {
visitor.visit_fn(FnKind::Method(impl_item.name,
Expand Down Expand Up @@ -928,9 +922,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
ExprArray(ref subexpressions) => {
walk_list!(visitor, visit_expr, subexpressions);
}
ExprRepeat(ref element, ref count) => {
ExprRepeat(ref element, count) => {
visitor.visit_expr(element);
visitor.visit_expr(count)
visitor.visit_nested_body(count)
}
ExprStruct(ref qpath, ref fields, ref optional_base) => {
visitor.visit_qpath(qpath, expression.id, expression.span);
Expand Down Expand Up @@ -1037,8 +1031,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
}
}
}

visitor.visit_expr_post(expression)
}

pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
Expand Down Expand Up @@ -1125,12 +1117,12 @@ impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
/// Computes the id range for a single fn body, ignoring nested items.
pub fn compute_id_range_for_fn_body<'v>(fk: FnKind<'v>,
decl: &'v FnDecl,
body: &'v Expr,
body: BodyId,
sp: Span,
id: NodeId,
map: &map::Map<'v>)
-> IdRange {
let mut visitor = IdRangeComputingVisitor::new(map);
walk_fn_with_body(&mut visitor, fk, decl, body, sp, id);
visitor.visit_fn(fk, decl, body, sp, id);
visitor.result()
}
62 changes: 39 additions & 23 deletions src/librustc/hir/lowering.rs
Expand Up @@ -46,8 +46,7 @@ use hir::map::definitions::DefPathData;
use hir::def_id::{DefIndex, DefId};
use hir::def::{Def, PathResolution};
use session::Session;
use util::nodemap::NodeMap;
use rustc_data_structures::fnv::FnvHashMap;
use util::nodemap::{NodeMap, FxHashMap};

use std::collections::BTreeMap;
use std::iter;
Expand All @@ -70,14 +69,14 @@ pub struct LoweringContext<'a> {
// the form of a DefIndex) so that if we create a new node which introduces
// a definition, then we can properly create the def id.
parent_def: Option<DefIndex>,
exprs: FnvHashMap<hir::ExprId, hir::Expr>,
resolver: &'a mut Resolver,

/// The items being lowered are collected here.
items: BTreeMap<NodeId, hir::Item>,

trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
bodies: FxHashMap<hir::BodyId, hir::Body>,
}

pub trait Resolver {
Expand Down Expand Up @@ -105,11 +104,11 @@ pub fn lower_crate(sess: &Session,
crate_root: std_inject::injected_crate_name(krate),
sess: sess,
parent_def: None,
exprs: FnvHashMap(),
resolver: resolver,
items: BTreeMap::new(),
trait_items: BTreeMap::new(),
impl_items: BTreeMap::new(),
bodies: FxHashMap(),
}.lower_crate(krate)
}

Expand All @@ -136,7 +135,7 @@ impl<'a> LoweringContext<'a> {
items: self.items,
trait_items: self.trait_items,
impl_items: self.impl_items,
exprs: self.exprs,
bodies: self.bodies,
}
}

Expand Down Expand Up @@ -171,9 +170,12 @@ impl<'a> LoweringContext<'a> {
visit::walk_crate(&mut item_lowerer, c);
}

fn record_expr(&mut self, expr: hir::Expr) -> hir::ExprId {
let id = hir::ExprId(expr.id);
self.exprs.insert(id, expr);
fn record_body(&mut self, value: hir::Expr) -> hir::BodyId {
let body = hir::Body {
value: value
};
let id = body.id();
self.bodies.insert(id, body);
id
}

Expand Down Expand Up @@ -305,11 +307,14 @@ impl<'a> LoweringContext<'a> {
TyKind::ObjectSum(ref ty, ref bounds) => {
hir::TyObjectSum(self.lower_ty(ty), self.lower_bounds(bounds))
}
TyKind::Array(ref ty, ref e) => {
hir::TyArray(self.lower_ty(ty), P(self.lower_expr(e)))
TyKind::Array(ref ty, ref length) => {
let length = self.lower_expr(length);
hir::TyArray(self.lower_ty(ty),
self.record_body(length))
}
TyKind::Typeof(ref expr) => {
hir::TyTypeof(P(self.lower_expr(expr)))
let expr = self.lower_expr(expr);
hir::TyTypeof(self.record_body(expr))
}
TyKind::PolyTraitRef(ref bounds) => {
hir::TyPolyTraitRef(self.lower_bounds(bounds))
Expand All @@ -336,7 +341,10 @@ impl<'a> LoweringContext<'a> {
name: v.node.name.name,
attrs: self.lower_attrs(&v.node.attrs),
data: self.lower_variant_data(&v.node.data),
disr_expr: v.node.disr_expr.as_ref().map(|e| P(self.lower_expr(e))),
disr_expr: v.node.disr_expr.as_ref().map(|e| {
let e = self.lower_expr(e);
self.record_body(e)
}),
},
span: v.span,
}
Expand Down Expand Up @@ -858,17 +866,20 @@ impl<'a> LoweringContext<'a> {
hir::ItemUse(path, kind)
}
ItemKind::Static(ref t, m, ref e) => {
let value = self.lower_expr(e);
hir::ItemStatic(self.lower_ty(t),
self.lower_mutability(m),
P(self.lower_expr(e)))
self.record_body(value))
}
ItemKind::Const(ref t, ref e) => {
hir::ItemConst(self.lower_ty(t), P(self.lower_expr(e)))
let value = self.lower_expr(e);
hir::ItemConst(self.lower_ty(t),
self.record_body(value))
}
ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
let body = self.lower_block(body);
let body = self.expr_block(body, ThinVec::new());
let body_id = self.record_expr(body);
let body_id = self.record_body(body);
hir::ItemFn(self.lower_fn_decl(decl),
self.lower_unsafety(unsafety),
self.lower_constness(constness),
Expand Down Expand Up @@ -935,14 +946,17 @@ impl<'a> LoweringContext<'a> {
node: match i.node {
TraitItemKind::Const(ref ty, ref default) => {
hir::TraitItemKind::Const(this.lower_ty(ty),
default.as_ref().map(|x| P(this.lower_expr(x))))
default.as_ref().map(|x| {
let value = this.lower_expr(x);
this.record_body(value)
}))
}
TraitItemKind::Method(ref sig, ref body) => {
hir::TraitItemKind::Method(this.lower_method_sig(sig),
body.as_ref().map(|x| {
let body = this.lower_block(x);
let expr = this.expr_block(body, ThinVec::new());
this.record_expr(expr)
this.record_body(expr)
}))
}
TraitItemKind::Type(ref bounds, ref default) => {
Expand Down Expand Up @@ -990,13 +1004,15 @@ impl<'a> LoweringContext<'a> {
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
node: match i.node {
ImplItemKind::Const(ref ty, ref expr) => {
hir::ImplItemKind::Const(this.lower_ty(ty), P(this.lower_expr(expr)))
let value = this.lower_expr(expr);
let body_id = this.record_body(value);
hir::ImplItemKind::Const(this.lower_ty(ty), body_id)
}
ImplItemKind::Method(ref sig, ref body) => {
let body = this.lower_block(body);
let expr = this.expr_block(body, ThinVec::new());
let expr_id = this.record_expr(expr);
hir::ImplItemKind::Method(this.lower_method_sig(sig), expr_id)
let body_id = this.record_body(expr);
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
}
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
Expand Down Expand Up @@ -1350,8 +1366,8 @@ impl<'a> LoweringContext<'a> {
}
ExprKind::Repeat(ref expr, ref count) => {
let expr = P(self.lower_expr(expr));
let count = P(self.lower_expr(count));
hir::ExprRepeat(expr, count)
let count = self.lower_expr(count);
hir::ExprRepeat(expr, self.record_body(count))
}
ExprKind::Tup(ref elts) => {
hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect())
Expand Down Expand Up @@ -1434,7 +1450,7 @@ impl<'a> LoweringContext<'a> {
let expr = this.lower_expr(body);
hir::ExprClosure(this.lower_capture_clause(capture_clause),
this.lower_fn_decl(decl),
this.record_expr(expr),
this.record_body(expr),
fn_decl_span)
})
}
Expand Down

0 comments on commit e64f64a

Please sign in to comment.