Skip to content

Commit

Permalink
auto merge of #11161 : eddyb/rust/de-at-visitor, r=huonw
Browse files Browse the repository at this point in the history
The primary user of `@T`/`P<T>` references from `Visitor` was `ast_map`, which in turn had two users (phase 3 in rustc and a step in loading items from metadata).
Both of them have been rewritten to use `ast_map` as a folder (this might speed up the compilation time of stage2 rustc by 100-200ms just because a fold + a visit are merged together).
  • Loading branch information
bors committed Jan 6, 2014
2 parents e5eab2c + 3119d18 commit 03a38b9
Show file tree
Hide file tree
Showing 57 changed files with 1,038 additions and 1,141 deletions.
32 changes: 15 additions & 17 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &input)
/// standard library and prelude.
pub fn phase_2_configure_and_expand(sess: Session,
cfg: ast::CrateConfig,
mut crate: ast::Crate) -> ast::Crate {
mut crate: ast::Crate)
-> (ast::Crate, syntax::ast_map::map) {
let time_passes = sess.time_passes();

sess.building_library.set(session::building_library(sess.opts, &crate));
Expand Down Expand Up @@ -201,10 +202,8 @@ pub fn phase_2_configure_and_expand(sess: Session,
crate = time(time_passes, "std injection", crate, |crate|
front::std_inject::maybe_inject_libstd_ref(sess, crate));

crate = time(time_passes, "assigning node ids", crate, |crate|
front::assign_node_ids::assign_node_ids(sess, crate));

return crate;
time(time_passes, "assinging node ids and indexing ast", crate, |crate|
front::assign_node_ids_and_map::assign_node_ids_and_map(sess, crate))
}

pub struct CrateAnalysis {
Expand All @@ -219,13 +218,11 @@ pub struct CrateAnalysis {
/// miscellaneous analysis passes on the crate. Return various
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes(sess: Session,
crate: &ast::Crate) -> CrateAnalysis {
crate: &ast::Crate,
ast_map: syntax::ast_map::map) -> CrateAnalysis {

let time_passes = sess.time_passes();

let ast_map = time(time_passes, "ast indexing", (), |_|
syntax::ast_map::map_crate(sess.diagnostic(), crate));

time(time_passes, "external crate/lib resolution", (), |_|
creader::read_crates(sess, crate,
session::sess_os_to_meta_os(sess.targ_cfg.os),
Expand Down Expand Up @@ -260,8 +257,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
region_map, lang_items);

// passes are timed inside typeck
let (method_map, vtable_map) = typeck::check_crate(
ty_cx, trait_map, crate);
let (method_map, vtable_map) = typeck::check_crate(ty_cx, trait_map, crate);

// These next two const passes can probably be merged
time(time_passes, "const marking", (), |_|
Expand Down Expand Up @@ -489,7 +485,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
// large chunks of memory alive and we want to free them as soon as
// possible to keep the peak memory usage low
let (outputs, trans) = {
let expanded_crate = {
let (expanded_crate, ast_map) = {
let crate = phase_1_parse_input(sess, cfg.clone(), input);
if stop_after_phase_1(sess) { return; }
phase_2_configure_and_expand(sess, cfg, crate)
Expand All @@ -501,7 +497,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,

if stop_after_phase_2(sess) { return; }

let analysis = phase_3_run_analysis_passes(sess, &expanded_crate);
let analysis = phase_3_run_analysis_passes(sess, &expanded_crate, ast_map);
if stop_after_phase_3(sess) { return; }
let trans = phase_4_translate_to_llvm(sess, expanded_crate,
&analysis, outputs);
Expand Down Expand Up @@ -578,11 +574,12 @@ pub fn pretty_print_input(sess: Session,
ppm: PpMode) {
let crate = phase_1_parse_input(sess, cfg.clone(), input);

let (crate, is_expanded) = match ppm {
let (crate, ast_map, is_expanded) = match ppm {
PpmExpanded | PpmExpandedIdentified | PpmTyped => {
(phase_2_configure_and_expand(sess, cfg, crate), true)
let (crate, ast_map) = phase_2_configure_and_expand(sess, cfg, crate);
(crate, Some(ast_map), true)
}
_ => (crate, false)
_ => (crate, None, false)
};

let annotation = match ppm {
Expand All @@ -592,7 +589,8 @@ pub fn pretty_print_input(sess: Session,
} as @pprust::pp_ann
}
PpmTyped => {
let analysis = phase_3_run_analysis_passes(sess, &crate);
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
let analysis = phase_3_run_analysis_passes(sess, &crate, ast_map);
@TypedAnnotation {
analysis: analysis
} as @pprust::pp_ann
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -11,22 +11,19 @@
use driver::session::Session;

use syntax::ast;
use syntax::fold::ast_fold;
use syntax::ast_map;

struct NodeIdAssigner {
sess: Session,
sess: Session
}

impl ast_fold for NodeIdAssigner {
fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
impl ast_map::FoldOps for NodeIdAssigner {
fn new_id(&self, old_id: ast::NodeId) -> ast::NodeId {
assert_eq!(old_id, ast::DUMMY_NODE_ID);
self.sess.next_node_id()
}
}

pub fn assign_node_ids(sess: Session, crate: ast::Crate) -> ast::Crate {
let mut fold = NodeIdAssigner {
sess: sess,
};
fold.fold_crate(crate)
pub fn assign_node_ids_and_map(sess: Session, crate: ast::Crate) -> (ast::Crate, ast_map::map) {
ast_map::map_crate(sess.diagnostic(), crate, NodeIdAssigner { sess: sess })
}
4 changes: 2 additions & 2 deletions src/librustc/front/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Visitor<()> for Context {
visit::walk_view_item(self, i, ())
}

fn visit_item(&mut self, i: @ast::item, _:()) {
fn visit_item(&mut self, i: &ast::item, _:()) {
for attr in i.attrs.iter() {
if "thread_local" == attr.name() {
self.gate_feature("thread_local", i.span,
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Visitor<()> for Context {
visit::walk_ty(self, t, ());
}

fn visit_expr(&mut self, e: @ast::Expr, _: ()) {
fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
match e.node {
ast::ExprUnary(_, ast::UnBox, _) |
ast::ExprVstore(_, ast::ExprVstoreBox) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub mod front {
pub mod config;
pub mod test;
pub mod std_inject;
pub mod assign_node_ids;
pub mod assign_node_ids_and_map;
pub mod feature_gate;
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ struct ReadCrateVisitor<'a> {
}

impl<'a> visit::Visitor<()> for ReadCrateVisitor<'a> {
fn visit_view_item(&mut self, a:&ast::view_item, _:()) {
fn visit_view_item(&mut self, a: &ast::view_item, _: ()) {
visit_view_item(self.e, a);
visit::walk_view_item(self, a, ());
}
fn visit_item(&mut self, a:@ast::item, _:()) {
fn visit_item(&mut self, a: &ast::item, _: ()) {
visit_item(self.e, a);
visit::walk_item(self, a, ());
}
Expand Down Expand Up @@ -164,7 +164,7 @@ fn visit_view_item(e: &mut Env, i: &ast::view_item) {
}
}

fn visit_item(e: &Env, i: @ast::item) {
fn visit_item(e: &Env, i: &ast::item) {
match i.node {
ast::item_foreign_mod(ref fm) => {
if fm.abis.is_rust() || fm.abis.is_intrinsic() {
Expand Down
49 changes: 28 additions & 21 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ use writer = extra::ebml::writer;
// used by astencode:
type abbrev_map = @RefCell<HashMap<ty::t, tyencode::ty_abbrev>>;

/// A borrowed version of ast::inlined_item.
pub enum InlinedItemRef<'a> {
ii_item_ref(&'a ast::item),
ii_method_ref(ast::DefId, bool, &'a ast::method),
ii_foreign_ref(&'a ast::foreign_item)
}

pub type encode_inlined_item<'a> = 'a |ecx: &EncodeContext,
ebml_w: &mut writer::Encoder,
path: &[ast_map::path_elt],
ii: ast::inlined_item|;
ebml_w: &mut writer::Encoder,
path: &[ast_map::path_elt],
ii: InlinedItemRef|;

pub struct EncodeParams<'a> {
diag: @SpanHandler,
Expand Down Expand Up @@ -837,13 +844,13 @@ fn encode_info_for_method(ecx: &EncodeContext,
None => ()
}

for ast_method in ast_method_opt.iter() {
for &ast_method in ast_method_opt.iter() {
let num_params = tpt.generics.type_param_defs.len();
if num_params > 0u || is_default_impl
|| should_inline(ast_method.attrs) {
(ecx.encode_inlined_item)(
ecx, ebml_w, impl_path,
ii_method(local_def(parent_id), false, *ast_method));
ii_method_ref(local_def(parent_id), false, ast_method));
} else {
encode_symbol(ecx, ebml_w, m.def_id.node);
}
Expand Down Expand Up @@ -915,13 +922,13 @@ fn encode_extension_implementations(ecx: &EncodeContext,

fn encode_info_for_item(ecx: &EncodeContext,
ebml_w: &mut writer::Encoder,
item: @item,
item: &item,
index: @RefCell<~[entry<i64>]>,
path: &[ast_map::path_elt],
vis: ast::visibility) {
let tcx = ecx.tcx;

fn add_to_index(item: @item, ebml_w: &writer::Encoder,
fn add_to_index(item: &item, ebml_w: &writer::Encoder,
index: @RefCell<~[entry<i64>]>) {
let mut index = index.borrow_mut();
index.get().push(entry {
Expand Down Expand Up @@ -958,7 +965,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
}

if !non_inlineable {
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item_ref(item));
}
encode_visibility(ebml_w, vis);
ebml_w.end_tag();
Expand All @@ -974,7 +981,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
encode_attributes(ebml_w, item.attrs);
if tps_len > 0u || should_inline(item.attrs) {
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item_ref(item));
} else {
encode_symbol(ecx, ebml_w, item.id);
}
Expand Down Expand Up @@ -1032,7 +1039,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
for v in (*enum_definition).variants.iter() {
encode_variant_id(ebml_w, local_def(v.node.id));
}
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item_ref(item));
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));

// Encode inherent implementations for this enumeration.
Expand Down Expand Up @@ -1077,7 +1084,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
needs to know*/
encode_struct_fields(ecx, ebml_w, struct_def);

(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item_ref(item));

// Encode inherent implementations for this structure.
encode_inherent_implementations(ecx, ebml_w, def_id);
Expand Down Expand Up @@ -1272,7 +1279,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_method_sort(ebml_w, 'p');
(ecx.encode_inlined_item)(
ecx, ebml_w, path,
ii_method(def_id, true, m));
ii_method_ref(def_id, true, m));
}
}

Expand All @@ -1288,7 +1295,7 @@ fn encode_info_for_item(ecx: &EncodeContext,

fn encode_info_for_foreign_item(ecx: &EncodeContext,
ebml_w: &mut writer::Encoder,
nitem: @foreign_item,
nitem: &foreign_item,
index: @RefCell<~[entry<i64>]>,
path: &ast_map::path,
abi: AbiSet) {
Expand All @@ -1309,7 +1316,7 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
&lookup_item_type(ecx.tcx,local_def(nitem.id)));
encode_name(ecx, ebml_w, nitem.ident);
if abi.is_intrinsic() {
(ecx.encode_inlined_item)(ecx, ebml_w, *path, ii_foreign(nitem));
(ecx.encode_inlined_item)(ecx, ebml_w, *path, ii_foreign_ref(nitem));
} else {
encode_symbol(ecx, ebml_w, nitem.id);
}
Expand All @@ -1331,9 +1338,9 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
ebml_w.end_tag();
}

fn my_visit_expr(_e:@Expr) { }
fn my_visit_expr(_e: &Expr) { }

fn my_visit_item(i: @item,
fn my_visit_item(i: &item,
items: ast_map::map,
ebml_w: &mut writer::Encoder,
ecx_ptr: *int,
Expand All @@ -1352,7 +1359,7 @@ fn my_visit_item(i: @item,
}
}

fn my_visit_foreign_item(ni: @foreign_item,
fn my_visit_foreign_item(ni: &foreign_item,
items: ast_map::map,
ebml_w: &mut writer::Encoder,
ecx_ptr:*int,
Expand Down Expand Up @@ -1391,19 +1398,19 @@ struct EncodeVisitor<'a,'b> {
}

impl<'a,'b> visit::Visitor<()> for EncodeVisitor<'a,'b> {
fn visit_expr(&mut self, ex:@Expr, _:()) {
fn visit_expr(&mut self, ex: &Expr, _: ()) {
visit::walk_expr(self, ex, ());
my_visit_expr(ex);
}
fn visit_item(&mut self, i:@item, _:()) {
fn visit_item(&mut self, i: &item, _: ()) {
visit::walk_item(self, i, ());
my_visit_item(i,
self.items,
self.ebml_w_for_visit_item,
self.ecx_ptr,
self.index);
}
fn visit_foreign_item(&mut self, ni:@foreign_item, _:()) {
fn visit_foreign_item(&mut self, ni: &foreign_item, _: ()) {
visit::walk_foreign_item(self, ni, ());
my_visit_foreign_item(ni,
self.items,
Expand Down Expand Up @@ -1692,7 +1699,7 @@ struct ImplVisitor<'a,'b> {
}

impl<'a,'b> Visitor<()> for ImplVisitor<'a,'b> {
fn visit_item(&mut self, item: @item, _: ()) {
fn visit_item(&mut self, item: &item, _: ()) {
match item.node {
item_impl(_, Some(ref trait_ref), _, _) => {
let def_map = self.ecx.tcx.def_map;
Expand Down
Loading

0 comments on commit 03a38b9

Please sign in to comment.