Refactor away ext::expand::{expand_crate, expand_crate_with_expander}.

This commit is contained in:
Jeffrey Seyfried
2016-09-29 00:22:46 +00:00
parent 09e41b6784
commit 21b4369322
3 changed files with 18 additions and 33 deletions

View File

@@ -686,11 +686,17 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
..syntax::ext::expand::ExpansionConfig::default(crate_name.to_string())
};
let mut ecx = ExtCtxt::new(&sess.parse_sess, krate.config.clone(), cfg, &mut resolver);
let ret = syntax::ext::expand::expand_crate(&mut ecx, krate);
let err_count = ecx.parse_sess.span_diagnostic.err_count();
let krate = ecx.monotonic_expander().expand_crate(krate);
if ecx.parse_sess.span_diagnostic.err_count() - ecx.resolve_err_count > err_count {
ecx.parse_sess.span_diagnostic.abort_if_errors();
}
if cfg!(windows) {
env::set_var("PATH", &old_path);
}
ret
krate
});
krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());

View File

@@ -22,7 +22,6 @@ use parse::{self, parser};
use parse::token;
use parse::token::{InternedString, str_to_ident};
use ptr::P;
use std_inject;
use util::small_vector::SmallVector;
use std::path::PathBuf;
@@ -737,17 +736,6 @@ impl<'a> ExtCtxt<'a> {
pub fn name_of(&self, st: &str) -> ast::Name {
token::intern(st)
}
pub fn initialize(&mut self, krate: &ast::Crate) {
self.crate_root = std_inject::injected_crate_name(krate);
let mut module = ModuleData {
mod_path: vec![token::str_to_ident(&self.ecfg.crate_name)],
directory: PathBuf::from(self.parse_sess.codemap().span_to_filename(krate.span)),
};
module.directory.pop();
self.current_expansion.module = Rc::new(module);
}
}
/// Extract a string literal from the macro expanded version of `expr`,

View File

@@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ast::{Block, Crate, Ident, Mac_, PatKind};
use ast::{Block, Ident, Mac_, PatKind};
use ast::{Name, MacStmtStyle, StmtKind, ItemKind};
use ast;
use ext::hygiene::Mark;
@@ -26,6 +26,7 @@ use parse::parser::Parser;
use parse::token::{self, intern, keywords};
use print::pprust;
use ptr::P;
use std_inject;
use tokenstream::{TokenTree, TokenStream};
use util::small_vector::SmallVector;
use visit::Visitor;
@@ -186,8 +187,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
MacroExpander { cx: cx, monotonic: monotonic }
}
fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
self.cx.crate_root = std_inject::injected_crate_name(&krate);
let mut module = ModuleData {
mod_path: vec![token::str_to_ident(&self.cx.ecfg.crate_name)],
directory: PathBuf::from(self.cx.codemap().span_to_filename(krate.span)),
};
module.directory.pop();
self.cx.current_expansion.module = Rc::new(module);
let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
attrs: krate.attrs,
@@ -206,10 +213,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
_ => unreachable!(),
};
if self.cx.parse_sess.span_diagnostic.err_count() - self.cx.resolve_err_count > err_count {
self.cx.parse_sess.span_diagnostic.abort_if_errors();
}
krate
}
@@ -866,18 +869,6 @@ impl<'feat> ExpansionConfig<'feat> {
}
}
pub fn expand_crate(cx: &mut ExtCtxt, c: Crate) -> Crate {
cx.initialize(&c);
cx.monotonic_expander().expand_crate(c)
}
// Expands crate using supplied MacroExpander - allows for
// non-standard expansion behaviour (e.g. step-wise).
pub fn expand_crate_with_expander(expander: &mut MacroExpander, c: Crate) -> Crate {
expander.cx.initialize(&c);
expander.expand_crate(c)
}
// A Marker adds the given mark to the syntax context and
// sets spans' `expn_id` to the given expn_id (unless it is `None`).
struct Marker { mark: Mark, expn_id: Option<ExpnId> }