Skip to content

Commit

Permalink
Refactor ExtCtxt to use a Resolver instead of a MacroLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Sep 13, 2016
1 parent 60440b2 commit a9821e1
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use std::rc::Rc;
use std::path::PathBuf;
use syntax::ast;
use syntax::attr;
use syntax::ext::base::LoadedMacro;
use syntax::ptr::P;
use syntax::parse::token::InternedString;
use syntax_pos::Span;
Expand Down Expand Up @@ -488,6 +489,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
}

pub trait MacroLoader {
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
}

/// Metadata encoding and decoding can make use of thread-local encoding and
/// decoding contexts. These allow implementers of serialize::Encodable and
Expand Down
15 changes: 7 additions & 8 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,12 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
}
sess.track_errors(|| sess.lint_store.borrow_mut().process_command_line(sess))?;

let mut macro_loader =
macro_import::MacroLoader::new(sess, &cstore, crate_name, krate.config.clone());

let resolver_arenas = Resolver::arenas();
let mut resolver = Resolver::new(sess, make_glob_map, &mut macro_loader, &resolver_arenas);

krate = time(time_passes, "expansion", || {
// Windows dlls do not have rpaths, so they don't know how to find their
// dependencies. It's up to us to tell the system where to find all the
Expand Down Expand Up @@ -672,14 +678,10 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
trace_mac: sess.opts.debugging_opts.trace_macros,
should_test: sess.opts.test,
};
let mut loader = macro_import::MacroLoader::new(sess,
&cstore,
crate_name,
krate.config.clone());
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
krate.config.clone(),
cfg,
&mut loader);
&mut resolver);
syntax_ext::register_builtins(&mut ecx.syntax_env);
let ret = syntax::ext::expand::expand_crate(&mut ecx, syntax_exts, krate);
if cfg!(windows) {
Expand Down Expand Up @@ -708,9 +710,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
&sess.features.borrow())
});

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 {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/macro_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use creader::{CrateReader, Macros};
use cstore::CStore;

use rustc::hir::def_id::DefIndex;
use rustc::middle;
use rustc::session::Session;
use rustc::util::nodemap::FnvHashMap;
use rustc_back::dynamic_lib::DynamicLibrary;
Expand All @@ -26,7 +27,6 @@ use rustc_macro::__internal::Registry;
use syntax::ast;
use syntax::attr;
use syntax::ext::base::LoadedMacro;
use syntax::ext;
use syntax::parse::token;
use syntax_ext::deriving::custom::CustomDerive;
use syntax_pos::Span;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn call_bad_macro_reexport(a: &Session, b: Span) {

pub type MacroSelection = FnvHashMap<token::InternedString, Span>;

impl<'a> ext::base::MacroLoader for MacroLoader<'a> {
impl<'a> middle::cstore::MacroLoader for MacroLoader<'a> {
fn load_crate(&mut self,
extern_crate: &ast::Item,
allows_macros: bool) -> Vec<LoadedMacro> {
Expand Down
18 changes: 17 additions & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use self::ParentLink::*;

use rustc::hir::map::Definitions;
use rustc::hir::{self, PrimTy, TyBool, TyChar, TyFloat, TyInt, TyUint, TyStr};
use rustc::middle::cstore::MacroLoader;
use rustc::session::Session;
use rustc::lint;
use rustc::hir::def::*;
Expand All @@ -53,6 +54,8 @@ use rustc::ty;
use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
use rustc::util::nodemap::{NodeMap, NodeSet, FnvHashMap, FnvHashSet};

use syntax::ext;
use syntax::ext::base::LoadedMacro;
use syntax::ext::hygiene::Mark;
use syntax::ast::{self, FloatTy};
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy};
Expand Down Expand Up @@ -1068,6 +1071,8 @@ pub struct Resolver<'a> {
arenas: &'a ResolverArenas<'a>,
dummy_binding: &'a NameBinding<'a>,
new_import_semantics: bool, // true if `#![feature(item_like_imports)]`

macro_loader: &'a mut MacroLoader,
}

pub struct ResolverArenas<'a> {
Expand Down Expand Up @@ -1149,6 +1154,12 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
}
}

impl<'a> ext::base::Resolver for Resolver<'a> {
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro> {
self.macro_loader.load_crate(extern_crate, allows_macros)
}
}

trait Named {
fn name(&self) -> Name;
}
Expand All @@ -1166,7 +1177,10 @@ impl Named for hir::PathSegment {
}

impl<'a> Resolver<'a> {
pub fn new(session: &'a Session, make_glob_map: MakeGlobMap, arenas: &'a ResolverArenas<'a>)
pub fn new(session: &'a Session,
make_glob_map: MakeGlobMap,
macro_loader: &'a mut MacroLoader,
arenas: &'a ResolverArenas<'a>)
-> Resolver<'a> {
let root_def_id = DefId::local(CRATE_DEF_INDEX);
let graph_root =
Expand Down Expand Up @@ -1227,6 +1241,8 @@ impl<'a> Resolver<'a> {
vis: ty::Visibility::Public,
}),
new_import_semantics: session.features.borrow().item_like_imports,

macro_loader: macro_loader,
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
syntax_expanders
}

pub trait MacroLoader {
pub trait Resolver {
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool)
-> Vec<LoadedMacro>;
}
Expand All @@ -556,8 +556,8 @@ pub enum LoadedMacro {
CustomDerive(String, Box<MultiItemModifier>),
}

pub struct DummyMacroLoader;
impl MacroLoader for DummyMacroLoader {
pub struct DummyResolver;
impl Resolver for DummyResolver {
fn load_crate(&mut self, _: &ast::Item, _: bool) -> Vec<LoadedMacro> {
Vec::new()
}
Expand All @@ -572,7 +572,7 @@ pub struct ExtCtxt<'a> {
pub backtrace: ExpnId,
pub ecfg: expand::ExpansionConfig<'a>,
pub crate_root: Option<&'static str>,
pub loader: &'a mut MacroLoader,
pub resolver: &'a mut Resolver,

pub exported_macros: Vec<ast::MacroDef>,

Expand All @@ -584,7 +584,7 @@ pub struct ExtCtxt<'a> {
impl<'a> ExtCtxt<'a> {
pub fn new(parse_sess: &'a parse::ParseSess, cfg: ast::CrateConfig,
ecfg: expand::ExpansionConfig<'a>,
loader: &'a mut MacroLoader)
resolver: &'a mut Resolver)
-> ExtCtxt<'a> {
ExtCtxt {
syntax_env: initial_syntax_expander_table(&ecfg),
Expand All @@ -594,7 +594,7 @@ impl<'a> ExtCtxt<'a> {
ecfg: ecfg,
crate_root: None,
exported_macros: Vec::new(),
loader: loader,
resolver: resolver,
derive_modes: HashMap::new(),
recursion_count: 0,
}
Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
// We need to error on `#[macro_use] extern crate` when it isn't at the
// crate root, because `$crate` won't work properly.
let is_crate_root = self.cx.syntax_env.is_crate_root();
for def in self.cx.loader.load_crate(&*item, is_crate_root) {
for def in self.cx.resolver.load_crate(&*item, is_crate_root) {
match def {
LoadedMacro::Def(def) => self.cx.insert_macro(def),
LoadedMacro::CustomDerive(name, ext) => {
Expand Down Expand Up @@ -809,7 +809,7 @@ fn mark_tts(tts: &[TokenTree], m: Mark) -> Vec<TokenTree> {
mod tests {
use super::{expand_crate, ExpansionConfig};
use ast;
use ext::base::{ExtCtxt, DummyMacroLoader};
use ext::base::{ExtCtxt, DummyResolver};
use parse;
use util::parser_testing::{string_to_parser};
use visit;
Expand Down Expand Up @@ -850,7 +850,7 @@ mod tests {
src,
Vec::new(), &sess).unwrap();
// should fail:
let mut loader = DummyMacroLoader;
let mut loader = DummyResolver;
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
expand_crate(&mut ecx, vec![], crate_ast);
}
Expand All @@ -865,7 +865,7 @@ mod tests {
"<test>".to_string(),
src,
Vec::new(), &sess).unwrap();
let mut loader = DummyMacroLoader;
let mut loader = DummyResolver;
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
expand_crate(&mut ecx, vec![], crate_ast);
}
Expand All @@ -879,7 +879,7 @@ mod tests {
"<test>".to_string(),
src,
Vec::new(), &sess).unwrap();
let mut loader = DummyMacroLoader;
let mut loader = DummyResolver;
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
expand_crate(&mut ecx, vec![], crate_ast);
}
Expand All @@ -888,7 +888,7 @@ mod tests {
let ps = parse::ParseSess::new();
let crate_ast = panictry!(string_to_parser(&ps, crate_str).parse_crate_mod());
// the cfg argument actually does matter, here...
let mut loader = DummyMacroLoader;
let mut loader = DummyResolver;
let mut ecx = ExtCtxt::new(&ps, vec![], test_ecfg(), &mut loader);
expand_crate(&mut ecx, vec![], crate_ast)
}
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use errors;
use errors::snippet::{SnippetData};
use config;
use entry::{self, EntryPointType};
use ext::base::{ExtCtxt, DummyMacroLoader};
use ext::base::{ExtCtxt, DummyResolver};
use ext::build::AstBuilder;
use ext::expand::ExpansionConfig;
use fold::Folder;
Expand Down Expand Up @@ -276,13 +276,13 @@ fn generate_test_harness(sess: &ParseSess,
let mut cleaner = EntryPointCleaner { depth: 0 };
let krate = cleaner.fold_crate(krate);

let mut loader = DummyMacroLoader;
let mut resolver = DummyResolver;
let mut cx: TestCtxt = TestCtxt {
sess: sess,
span_diagnostic: sd,
ext_cx: ExtCtxt::new(sess, vec![],
ExpansionConfig::default("test".to_string()),
&mut loader),
&mut resolver),
path: Vec::new(),
testfns: Vec::new(),
reexport_test_harness_main: reexport_test_harness_main,
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/rustc_macro_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::mem;
use errors;
use syntax::ast::{self, Ident, NodeId};
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute};
use syntax::ext::base::{ExtCtxt, DummyMacroLoader};
use syntax::ext::base::{ExtCtxt, DummyResolver};
use syntax::ext::build::AstBuilder;
use syntax::ext::expand::ExpansionConfig;
use syntax::parse::ParseSess;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn modify(sess: &ParseSess,
num_crate_types: usize,
handler: &errors::Handler,
features: &Features) -> ast::Crate {
let mut loader = DummyMacroLoader;
let mut loader = DummyResolver;
let mut cx = ExtCtxt::new(sess,
Vec::new(),
ExpansionConfig::default("rustc_macro".to_string()),
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail-fulldeps/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use syntax_pos::DUMMY_SP;

fn main() {
let ps = syntax::parse::ParseSess::new();
let mut loader = syntax::ext::base::DummyMacroLoader;
let mut resolver = syntax::ext::base::DummyResolver;
let mut cx = syntax::ext::base::ExtCtxt::new(
&ps, vec![],
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()),
&mut loader);
&mut resolver);
cx.bt_push(syntax::codemap::ExpnInfo {
call_site: DUMMY_SP,
callee: syntax::codemap::NameAndSpan {
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-fail-fulldeps/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use syntax_pos::DUMMY_SP;

fn main() {
let ps = syntax::parse::ParseSess::new();
let mut loader = syntax::ext::base::DummyMacroLoader;
let mut resolver = syntax::ext::base::DummyResolver;
let mut cx = syntax::ext::base::ExtCtxt::new(
&ps, vec![],
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()),
&mut loader);
&mut resolver);
cx.bt_push(syntax::codemap::ExpnInfo {
call_site: DUMMY_SP,
callee: syntax::codemap::NameAndSpan {
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass-fulldeps/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use syntax_pos::DUMMY_SP;

fn main() {
let ps = syntax::parse::ParseSess::new();
let mut loader = syntax::ext::base::DummyMacroLoader;
let mut resolver = syntax::ext::base::DummyResolver;
let mut cx = syntax::ext::base::ExtCtxt::new(
&ps, vec![],
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()),
&mut loader);
&mut resolver);
cx.bt_push(syntax::codemap::ExpnInfo {
call_site: DUMMY_SP,
callee: syntax::codemap::NameAndSpan {
Expand Down

0 comments on commit a9821e1

Please sign in to comment.