Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store items out-of-line in the HIR #29903

Merged
merged 19 commits into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6913ed0
Remove seemingly pointless case -- this customized variant avoided
nikomatsakis Oct 31, 2015
6ccd390
rename `_lctx` to `lctx` where appropriate
nikomatsakis Nov 12, 2015
66326bc
refactorings of `lowering` that make it more amenable to using `&mut`
nikomatsakis Nov 12, 2015
767ee79
Refactor the HIR so that items are stored in a map in the `Crate`,
nikomatsakis Nov 17, 2015
25727d7
Port the `map` construction code to use the new visitor.
nikomatsakis Nov 17, 2015
e14562d
Rework the `IdVisitor` so that it only visits item contents (and doesn't
nikomatsakis Nov 17, 2015
e4ff9f7
Port a bunch of code new-visitor; all of these ports were
nikomatsakis Nov 17, 2015
ac38021
Port entry code to `visit_all_items` -- since this was tracking whether
nikomatsakis Nov 17, 2015
98b046e
Various straight-forward ports that override `visit_nested_items`
nikomatsakis Nov 17, 2015
1e941f8
Port trans to use visit_all_items: this was mostly straight-forward, but
nikomatsakis Nov 17, 2015
db97c93
Add comment explaining why it is called `intravisit`
nikomatsakis Nov 18, 2015
e303c25
Change to a BTreeMap rather than sorting the keys of a FnvHashMap.
nikomatsakis Nov 18, 2015
0bc6140
Remove rustc_data_structures from the deps of librustc_front now
nikomatsakis Nov 18, 2015
06f2d9d
Modify trans to use an outer walk and ensure that we rotate as we
nikomatsakis Nov 18, 2015
bca026e
Fix two long lines.
nikomatsakis Nov 18, 2015
7c2ee5e
Patch graphviz tests to account for the fact that nested items are not
nikomatsakis Nov 18, 2015
cfe4c35
ratchet down the recursion limit because, at least in my testing,
nikomatsakis Nov 18, 2015
f8f2e2b
minor fixes to #[cfg(test)] code
nikomatsakis Nov 18, 2015
7926fa1
Update unit tests in driver.
nikomatsakis Nov 18, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/front/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_front::hir::{Block, FnDecl};
use syntax::ast::{Name, NodeId};
use rustc_front::hir as ast;
use syntax::codemap::Span;
use rustc_front::visit::FnKind;
use rustc_front::intravisit::FnKind;

/// An FnLikeNode is a Node that is like a fn, in that it has a decl
/// and a body (as well as a NodeId, a span, etc).
Expand Down
39 changes: 25 additions & 14 deletions src/librustc/front/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::MapEntry::*;

use rustc_front::hir::*;
use rustc_front::util;
use rustc_front::visit::{self, Visitor};
use rustc_front::intravisit::{self, Visitor};
use middle::def_id::{CRATE_DEF_INDEX, DefIndex};
use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID, DUMMY_NODE_ID};
Expand All @@ -22,14 +22,16 @@ use syntax::codemap::Span;
/// A Visitor that walks over an AST and collects Node's into an AST
/// Map.
pub struct NodeCollector<'ast> {
pub krate: &'ast Crate,
pub map: Vec<MapEntry<'ast>>,
pub definitions: Definitions,
pub parent_node: NodeId,
}

impl<'ast> NodeCollector<'ast> {
pub fn root() -> NodeCollector<'ast> {
pub fn root(krate: &'ast Crate) -> NodeCollector<'ast> {
let mut collector = NodeCollector {
krate: krate,
map: vec![],
definitions: Definitions::new(),
parent_node: CRATE_NODE_ID,
Expand All @@ -44,13 +46,15 @@ impl<'ast> NodeCollector<'ast> {
collector
}

pub fn extend(parent: &'ast InlinedParent,
pub fn extend(krate: &'ast Crate,
parent: &'ast InlinedParent,
parent_node: NodeId,
parent_def_path: DefPath,
map: Vec<MapEntry<'ast>>,
definitions: Definitions)
-> NodeCollector<'ast> {
let mut collector = NodeCollector {
krate: krate,
map: map,
parent_node: parent_node,
definitions: definitions,
Expand Down Expand Up @@ -107,6 +111,13 @@ impl<'ast> NodeCollector<'ast> {
}

impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
/// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of
/// their outer items.
fn visit_nested_item(&mut self, item: ItemId) {
self.visit_item(self.krate.item(item.id))
}

fn visit_item(&mut self, i: &'ast Item) {
// Pick the def data. This need not be unique, but the more
// information we encapsulate into
Expand Down Expand Up @@ -173,7 +184,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
}
_ => {}
}
visit::walk_item(self, i);
intravisit::walk_item(self, i);
self.parent_node = parent_node;
}

Expand All @@ -184,7 +195,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {

let parent_node = self.parent_node;
self.parent_node = foreign_item.id;
visit::walk_foreign_item(self, foreign_item);
intravisit::walk_foreign_item(self, foreign_item);
self.parent_node = parent_node;
}

Expand All @@ -195,7 +206,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
DefPathData::TypeParam(ty_param.name));
}

visit::walk_generics(self, generics);
intravisit::walk_generics(self, generics);
}

fn visit_trait_item(&mut self, ti: &'ast TraitItem) {
Expand All @@ -217,7 +228,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
_ => { }
}

visit::walk_trait_item(self, ti);
intravisit::walk_trait_item(self, ti);

self.parent_node = parent_node;
}
Expand All @@ -240,7 +251,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
_ => { }
}

visit::walk_impl_item(self, ii);
intravisit::walk_impl_item(self, ii);

self.parent_node = parent_node;
}
Expand All @@ -259,7 +270,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {

let parent_node = self.parent_node;
self.parent_node = pat.id;
visit::walk_pat(self, pat);
intravisit::walk_pat(self, pat);
self.parent_node = parent_node;
}

Expand All @@ -273,7 +284,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {

let parent_node = self.parent_node;
self.parent_node = expr.id;
visit::walk_expr(self, expr);
intravisit::walk_expr(self, expr);
self.parent_node = parent_node;
}

Expand All @@ -282,21 +293,21 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
self.insert(id, NodeStmt(stmt));
let parent_node = self.parent_node;
self.parent_node = id;
visit::walk_stmt(self, stmt);
intravisit::walk_stmt(self, stmt);
self.parent_node = parent_node;
}

fn visit_fn(&mut self, fk: visit::FnKind<'ast>, fd: &'ast FnDecl,
fn visit_fn(&mut self, fk: intravisit::FnKind<'ast>, fd: &'ast FnDecl,
b: &'ast Block, s: Span, id: NodeId) {
assert_eq!(self.parent_node, id);
visit::walk_fn(self, fk, fd, b, s);
intravisit::walk_fn(self, fk, fd, b, s);
}

fn visit_block(&mut self, block: &'ast Block) {
self.insert(block.id, NodeBlock(block));
let parent_node = self.parent_node;
self.parent_node = block.id;
visit::walk_block(self, block);
intravisit::walk_block(self, block);
self.parent_node = parent_node;
}

Expand Down
13 changes: 8 additions & 5 deletions src/librustc/front/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use syntax::parse::token;

use rustc_front::hir::*;
use rustc_front::fold::Folder;
use rustc_front::visit;
use rustc_front::intravisit;
use rustc_front::print::pprust;

use arena::TypedArena;
Expand Down Expand Up @@ -809,9 +809,11 @@ impl<F: FoldOps> Folder for IdAndSpanUpdater<F> {
}

pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
let mut collector = NodeCollector::root();
visit::walk_crate(&mut collector, &forest.krate);
let NodeCollector { map, definitions, .. } = collector;
let (map, definitions) = {
let mut collector = NodeCollector::root(&forest.krate);
intravisit::walk_crate(&mut collector, &forest.krate);
(collector.map, collector.definitions)
};

if log_enabled!(::log::DEBUG) {
// This only makes sense for ordered stores; note the
Expand Down Expand Up @@ -847,7 +849,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
-> &'ast InlinedItem {
let mut fld = IdAndSpanUpdater { fold_ops: fold_ops };
let ii = match ii {
II::Item(i) => II::Item(fld.fold_item(i)),
II::Item(i) => II::Item(i.map(|i| fld.fold_item(i))),
II::TraitItem(d, ti) => {
II::TraitItem(fld.fold_ops.new_def_id(d),
fld.fold_trait_item(ti))
Expand All @@ -867,6 +869,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
let ii_parent_id = fld.new_id(DUMMY_NODE_ID);
let mut collector =
NodeCollector::extend(
map.krate(),
ii_parent,
ii_parent_id,
def_path,
Expand Down
25 changes: 15 additions & 10 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use syntax::parse::token::InternedString;
use syntax::ast;
use rustc_front::hir;
use rustc_front::util;
use rustc_front::visit as hir_visit;
use rustc_front::intravisit as hir_visit;
use syntax::visit as ast_visit;
use syntax::diagnostic;

Expand Down Expand Up @@ -555,7 +555,6 @@ impl<'a> EarlyContext<'a> {
{
let mut v = ast_util::IdVisitor {
operation: self,
pass_through_items: false,
visited_outermost: false,
};
f(&mut v);
Expand Down Expand Up @@ -583,11 +582,7 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
fn visit_ids<F>(&mut self, f: F)
where F: FnOnce(&mut util::IdVisitor<LateContext>)
{
let mut v = util::IdVisitor {
operation: self,
pass_through_items: false,
visited_outermost: false,
};
let mut v = util::IdVisitor::new(self);
f(&mut v);
}
}
Expand All @@ -611,10 +606,12 @@ impl<'a, 'tcx> LintContext for LateContext<'a, 'tcx> {
}

fn enter_attrs(&mut self, attrs: &[ast::Attribute]) {
debug!("late context: enter_attrs({:?})", attrs);
run_lints!(self, enter_lint_attrs, late_passes, attrs);
}

fn exit_attrs(&mut self, attrs: &[ast::Attribute]) {
debug!("late context: exit_attrs({:?})", attrs);
run_lints!(self, exit_lint_attrs, late_passes, attrs);
}
}
Expand All @@ -638,15 +635,24 @@ impl<'a> LintContext for EarlyContext<'a> {
}

fn enter_attrs(&mut self, attrs: &[ast::Attribute]) {
debug!("early context: exit_attrs({:?})", attrs);
run_lints!(self, enter_lint_attrs, early_passes, attrs);
}

fn exit_attrs(&mut self, attrs: &[ast::Attribute]) {
debug!("early context: exit_attrs({:?})", attrs);
run_lints!(self, exit_lint_attrs, early_passes, attrs);
}
}

impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
/// Because lints are scoped lexically, we want to walk nested
/// items in the context of the outer item, so enable
/// deep-walking.
fn visit_nested_item(&mut self, item: hir::ItemId) {
self.visit_item(self.tcx.map.expect_item(item.id))
}

fn visit_item(&mut self, it: &hir::Item) {
self.with_lint_attrs(&it.attrs, |cx| {
run_lints!(cx, check_item, late_passes, it);
Expand Down Expand Up @@ -952,6 +958,7 @@ impl<'a, 'tcx> IdVisitingOperation for LateContext<'a, 'tcx> {
match self.sess().lints.borrow_mut().remove(&id) {
None => {}
Some(lints) => {
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
for (lint_id, span, msg) in lints {
self.span_lint(lint_id.lint, span, &msg[..])
}
Expand Down Expand Up @@ -1008,16 +1015,14 @@ impl LateLintPass for GatherNodeLevels {
///
/// Consumes the `lint_store` field of the `Session`.
pub fn check_crate(tcx: &ty::ctxt,
krate: &hir::Crate,
exported_items: &ExportedItems) {

let krate = tcx.map.krate();
let mut cx = LateContext::new(tcx, krate, exported_items);

// Visit the whole crate.
cx.with_lint_attrs(&krate.attrs, |cx| {
cx.visit_id(ast::CRATE_NODE_ID);
cx.visit_ids(|v| {
v.visited_outermost = true;
hir_visit::walk_crate(v, krate);
});

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use self::LintSource::*;
use std::hash;
use std::ascii::AsciiExt;
use syntax::codemap::Span;
use rustc_front::visit::FnKind;
use rustc_front::intravisit::FnKind;
use syntax::visit as ast_visit;
use syntax::ast;
use rustc_front::hir;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub type EarlyLintPassObject = Box<EarlyLintPass + 'static>;
pub type LateLintPassObject = Box<LateLintPass + 'static>;

/// Identifies a lint known to the compiler.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct LintId {
// Identity is based on pointer equality of this field.
lint: &'static Lint,
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::parse::token::InternedString;
use syntax::util::small_vector::SmallVector;
use rustc_front::visit;
use rustc_front::intravisit::Visitor;
use rustc_front::hir;
use log;

Expand All @@ -53,10 +53,9 @@ pub struct CrateReader<'a> {
foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
}

impl<'a, 'b, 'v> visit::Visitor<'v> for LocalCrateReader<'a, 'b> {
fn visit_item(&mut self, a: &hir::Item) {
impl<'a, 'b, 'hir> Visitor<'hir> for LocalCrateReader<'a, 'b> {
fn visit_item(&mut self, a: &'hir hir::Item) {
self.process_item(a);
visit::walk_item(self, a);
}
}

Expand Down Expand Up @@ -716,7 +715,7 @@ impl<'a, 'b> LocalCrateReader<'a, 'b> {
// etc.
pub fn read_crates(&mut self, krate: &hir::Crate) {
self.process_crate(krate);
visit::walk_crate(self, krate);
krate.visit_all_items(self);
self.creader.inject_allocator_crate();

if log_enabled!(log::INFO) {
Expand Down
Loading