Refactor ext::base::Resolver::add_ext to only define macros in the crate root.
This commit is contained in:
@@ -74,8 +74,16 @@ impl<'a> base::Resolver for Resolver<'a> {
|
||||
self.session.span_err(def.span, "user-defined macros may not be named `macro_rules`");
|
||||
}
|
||||
if def.use_locally {
|
||||
self.macro_names.insert(def.ident.name);
|
||||
let ext = macro_rules::compile(&self.session.parse_sess, &def);
|
||||
self.add_ext(scope, def.ident, Rc::new(ext));
|
||||
|
||||
let mut module = self.expansion_data[&scope].module;
|
||||
while module.macros_escape {
|
||||
module = module.parent.unwrap();
|
||||
}
|
||||
module.macros.borrow_mut().insert(def.ident.name, NameBinding {
|
||||
ext: Rc::new(ext),
|
||||
});
|
||||
}
|
||||
if def.export {
|
||||
def.id = self.next_node_id();
|
||||
@@ -83,16 +91,11 @@ impl<'a> base::Resolver for Resolver<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn add_ext(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
|
||||
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
|
||||
if let NormalTT(..) = *ext {
|
||||
self.macro_names.insert(ident.name);
|
||||
}
|
||||
|
||||
let mut module = self.expansion_data[&scope].module;
|
||||
while module.macros_escape {
|
||||
module = module.parent.unwrap();
|
||||
}
|
||||
module.macros.borrow_mut().insert(ident.name, NameBinding {
|
||||
self.graph_root.macros.borrow_mut().insert(ident.name, NameBinding {
|
||||
ext: ext,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -519,7 +519,7 @@ pub trait Resolver {
|
||||
|
||||
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
|
||||
fn add_macro(&mut self, scope: Mark, def: ast::MacroDef);
|
||||
fn add_ext(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>);
|
||||
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
|
||||
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
|
||||
|
||||
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
|
||||
@@ -535,7 +535,7 @@ impl Resolver for DummyResolver {
|
||||
|
||||
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
|
||||
fn add_macro(&mut self, _scope: Mark, _def: ast::MacroDef) {}
|
||||
fn add_ext(&mut self, _scope: Mark, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
|
||||
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
|
||||
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
|
||||
|
||||
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
|
||||
@@ -749,7 +749,7 @@ impl<'a> ExtCtxt<'a> {
|
||||
|
||||
for (name, extension) in user_exts {
|
||||
let ident = ast::Ident::with_empty_ctxt(name);
|
||||
self.resolver.add_ext(Mark::root(), ident, Rc::new(extension));
|
||||
self.resolver.add_ext(ident, Rc::new(extension));
|
||||
}
|
||||
|
||||
let mut module = ModuleData {
|
||||
|
||||
@@ -51,13 +51,12 @@ pub mod deriving;
|
||||
use std::rc::Rc;
|
||||
use syntax::ast;
|
||||
use syntax::ext::base::{MacroExpanderFn, NormalTT, IdentTT, MultiModifier};
|
||||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::ext::tt::macro_rules::MacroRulesExpander;
|
||||
use syntax::parse::token::intern;
|
||||
|
||||
pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, enable_quotes: bool) {
|
||||
let mut register = |name, ext| {
|
||||
resolver.add_ext(Mark::root(), ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
|
||||
resolver.add_ext(ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
|
||||
};
|
||||
|
||||
register("macro_rules", IdentTT(Box::new(MacroRulesExpander), None, false));
|
||||
|
||||
Reference in New Issue
Block a user