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

Simplify the macro hygiene algorithm #34570

Merged
merged 7 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
70 changes: 13 additions & 57 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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));

Expand Down Expand Up @@ -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.
Expand Down
92 changes: 92 additions & 0 deletions src/librustc_resolve/assign_ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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::ext::mtwt;
use syntax::fold::{self, Folder};
use syntax::ptr::P;
use syntax::util::move_map::MoveMap;
use syntax::util::small_vector::SmallVector;

use std::collections::HashMap;
use std::mem;

impl<'a> Resolver<'a> {
pub fn assign_node_ids(&mut self, krate: ast::Crate) -> ast::Crate {
NodeIdAssigner {
sess: self.session,
macros_at_scope: &mut self.macros_at_scope,
}.fold_crate(krate)
}
}

struct NodeIdAssigner<'a> {
sess: &'a Session,
macros_at_scope: &'a mut HashMap<ast::NodeId, Vec<ast::Mrk>>,
}

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();
let mut macros = Vec::new();
block.stmts = block.stmts.move_flat_map(|stmt| {
if let ast::StmtKind::Item(ref item) = stmt.node {
if let ast::ItemKind::Mac(..) = item.node {
macros.push(mtwt::outer_mark(item.ident.ctxt));
return None;
}
}

let stmt = self.fold_stmt(stmt).pop().unwrap();
if !macros.is_empty() {
self.macros_at_scope.insert(stmt.id, mem::replace(&mut macros, Vec::new()));
}
Some(stmt)
});

stmt.and_then(|mut stmt| {
// Avoid wasting a node id on a trailing expression statement,
// which shares a HIR node with the expression itself.
if let ast::StmtKind::Expr(expr) = stmt.node {
let expr = self.fold_expr(expr);
stmt.id = expr.id;
stmt.node = ast::StmtKind::Expr(expr);
Some(stmt)
} else {
self.fold_stmt(stmt).pop()
}
}).map(|stmt| {
if !macros.is_empty() {
self.macros_at_scope.insert(stmt.id, mem::replace(&mut macros, Vec::new()));
}
block.stmts.push(stmt);
});

block
})
}

fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
match item.node {
ast::ItemKind::Mac(..) => SmallVector::zero(),
_ => fold::noop_fold_item(item, self),
}
}
}
Loading