From 195a27faab593c8cbfd6ac5389f578100074df13 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Wed, 22 Jun 2016 01:56:39 +0000 Subject: [PATCH] Move node id assigning into `resolve` --- src/librustc_driver/driver.rs | 70 ++++++------------------------ src/librustc_resolve/assign_ids.rs | 59 +++++++++++++++++++++++++ src/librustc_resolve/lib.rs | 1 + 3 files changed, 73 insertions(+), 57 deletions(-) create mode 100644 src/librustc_resolve/assign_ids.rs diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index eef2b6e6f37..81fb825604e 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -50,7 +50,6 @@ use std::io::{self, Write}; use std::path::{Path, PathBuf}; use syntax::{ast, diagnostics, visit}; use syntax::attr::{self, AttrMetaMethods}; -use syntax::fold::Folder; use syntax::parse::{self, PResult, token}; use syntax::util::node_count::NodeCounter; use syntax; @@ -695,6 +694,19 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session, sess.diagnostic()) }); + let resolver_arenas = Resolver::arenas(); + let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas); + + let krate = time(sess.time_passes(), "assigning node ids", || resolver.assign_node_ids(krate)); + + if sess.opts.debugging_opts.input_stats { + println!("Post-expansion node count: {}", count_nodes(&krate)); + } + + if sess.opts.debugging_opts.ast_json { + println!("{}", json::as_json(&krate)); + } + time(time_passes, "checking for inline asm in case the target doesn't support it", || no_asm::check_crate(sess, &krate)); @@ -710,15 +722,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session, }) })?; - if sess.opts.debugging_opts.input_stats { - println!("Post-expansion node count: {}", count_nodes(&krate)); - } - - krate = assign_node_ids(sess, krate); - - let resolver_arenas = Resolver::arenas(); - let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas); - // Collect defintions for def ids. time(sess.time_passes(), "collecting defs", || resolver.definitions.collect(&krate)); @@ -783,53 +786,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session, }) } -pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate { - use syntax::ptr::P; - use syntax::util::move_map::MoveMap; - - struct NodeIdAssigner<'a> { - sess: &'a Session, - } - - impl<'a> Folder for NodeIdAssigner<'a> { - fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId { - assert_eq!(old_id, ast::DUMMY_NODE_ID); - self.sess.next_node_id() - } - - fn fold_block(&mut self, block: P) -> P { - block.map(|mut block| { - block.id = self.new_id(block.id); - - let stmt = block.stmts.pop(); - block.stmts = block.stmts.move_flat_map(|s| self.fold_stmt(s).into_iter()); - if let Some(ast::Stmt { node: ast::StmtKind::Expr(expr), span, .. }) = stmt { - let expr = self.fold_expr(expr); - block.stmts.push(ast::Stmt { - id: expr.id, - node: ast::StmtKind::Expr(expr), - span: span, - }); - } else if let Some(stmt) = stmt { - block.stmts.extend(self.fold_stmt(stmt)); - } - - block - }) - } - } - - let krate = time(sess.time_passes(), - "assigning node ids", - || NodeIdAssigner { sess: sess }.fold_crate(krate)); - - if sess.opts.debugging_opts.ast_json { - println!("{}", json::as_json(&krate)); - } - - krate -} - /// Run the resolution, typechecking, region checking and other /// miscellaneous analysis passes on the crate. Return various /// structures carrying the results of the analysis. diff --git a/src/librustc_resolve/assign_ids.rs b/src/librustc_resolve/assign_ids.rs new file mode 100644 index 00000000000..f3aa78bd4e2 --- /dev/null +++ b/src/librustc_resolve/assign_ids.rs @@ -0,0 +1,59 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use Resolver; +use rustc::session::Session; +use syntax::ast; +use syntax::fold::Folder; +use syntax::ptr::P; +use syntax::util::move_map::MoveMap; + +impl<'a> Resolver<'a> { + pub fn assign_node_ids(&mut self, krate: ast::Crate) -> ast::Crate { + NodeIdAssigner { + sess: self.session, + }.fold_crate(krate) + } +} + +struct NodeIdAssigner<'a> { + sess: &'a Session, +} + +impl<'a> Folder for NodeIdAssigner<'a> { + fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId { + assert_eq!(old_id, ast::DUMMY_NODE_ID); + self.sess.next_node_id() + } + + fn fold_block(&mut self, block: P) -> P { + block.map(|mut block| { + block.id = self.new_id(block.id); + + let stmt = block.stmts.pop(); + block.stmts = block.stmts.move_flat_map(|s| self.fold_stmt(s).into_iter()); + if let Some(ast::Stmt { node: ast::StmtKind::Expr(expr), span, .. }) = stmt { + // Avoid wasting a node id on a trailing expression statement, + // which shares a HIR node with the expression itself. + let expr = self.fold_expr(expr); + block.stmts.push(ast::Stmt { + id: expr.id, + node: ast::StmtKind::Expr(expr), + span: span, + }); + } else if let Some(stmt) = stmt { + block.stmts.extend(self.fold_stmt(stmt)); + } + + block + }) + } +} + diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 9079cc8ccb1..15e9d85381c 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -83,6 +83,7 @@ mod diagnostics; mod check_unused; mod build_reduced_graph; mod resolve_imports; +mod assign_ids; enum SuggestionType { Macro(String),