about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>2016-06-22 01:56:39 +0000
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>2016-07-14 01:30:19 +0000
commit195a27faab593c8cbfd6ac5389f578100074df13 (patch)
tree6f3c13511e156cd34b1bd5d1f6bef07a9db990d5
parent3265bd54b5b3f32d038273afec7554f007a5ce1d (diff)
downloadrust-195a27faab593c8cbfd6ac5389f578100074df13.tar.gz
rust-195a27faab593c8cbfd6ac5389f578100074df13.zip
Move node id assigning into `resolve`
-rw-r--r--src/librustc_driver/driver.rs70
-rw-r--r--src/librustc_resolve/assign_ids.rs59
-rw-r--r--src/librustc_resolve/lib.rs1
3 files changed, 73 insertions, 57 deletions
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<ast::Block>) -> P<ast::Block> {
-            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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<ast::Block>) -> P<ast::Block> {
+        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),