rust/src/librustc_resolve/macros.rs

237 lines
9.0 KiB
Rust
Raw Normal View History

// 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 {Module, Resolver};
use build_reduced_graph::BuildReducedGraphVisitor;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
use rustc::hir::map::{self, DefCollector};
2016-09-16 03:50:34 -05:00
use std::cell::Cell;
use std::rc::Rc;
2016-09-16 01:45:03 -05:00
use syntax::ast;
use syntax::errors::DiagnosticBuilder;
use syntax::ext::base::{self, MultiModifier, MultiDecorator, MultiItemModifier};
2016-09-16 01:45:03 -05:00
use syntax::ext::base::{NormalTT, SyntaxExtension};
use syntax::ext::expand::{Expansion, Invocation, InvocationKind};
use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::ext::tt::macro_rules;
2016-09-16 01:45:03 -05:00
use syntax::parse::token::intern;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{Span, DUMMY_SP};
2016-09-30 22:49:12 -05:00
// FIXME(jseyfried) Merge with `::NameBinding`.
pub struct NameBinding {
pub ext: Rc<SyntaxExtension>,
pub expansion: Mark,
pub shadowing: bool,
pub span: Span,
2016-09-30 22:49:12 -05:00
}
#[derive(Clone)]
pub struct InvocationData<'a> {
backtrace: SyntaxContext,
2016-09-16 03:50:34 -05:00
pub module: Cell<Module<'a>>,
def_index: DefIndex,
// True if this expansion is in a `const_integer` position, for example `[u32; m!()]`.
// c.f. `DefCollector::visit_ast_const_integer`.
const_integer: bool,
}
impl<'a> InvocationData<'a> {
pub fn root(graph_root: Module<'a>) -> Self {
InvocationData {
backtrace: SyntaxContext::empty(),
2016-09-16 03:50:34 -05:00
module: Cell::new(graph_root),
def_index: CRATE_DEF_INDEX,
const_integer: false,
}
}
}
impl<'a> base::Resolver for Resolver<'a> {
fn next_node_id(&mut self) -> ast::NodeId {
self.session.next_node_id()
}
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
let mark = Mark::fresh();
let module = self.module_map[&id];
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
backtrace: SyntaxContext::empty(),
2016-09-16 03:50:34 -05:00
module: Cell::new(module),
def_index: module.def_id().unwrap().index,
const_integer: false,
2016-09-16 03:50:34 -05:00
}));
mark
}
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
2016-09-16 01:45:03 -05:00
self.collect_def_ids(mark, expansion);
self.current_module = self.invocations[&mark].module.get();
expansion.visit_with(&mut BuildReducedGraphVisitor { resolver: self, expansion: mark });
}
fn add_macro(&mut self, scope: Mark, mut def: ast::MacroDef) {
if &def.ident.name.as_str() == "macro_rules" {
self.session.span_err(def.span, "user-defined macros may not be named `macro_rules`");
}
if def.use_locally {
let invocation = self.invocations[&scope];
let mut module = invocation.module.get();
while module.macros_escape {
module = module.parent.unwrap();
}
let binding = NameBinding {
ext: Rc::new(macro_rules::compile(&self.session.parse_sess, &def)),
expansion: invocation.backtrace.data().prev_ctxt.data().outer_mark,
shadowing: self.resolve_macro_name(scope, def.ident.name, false).is_some(),
span: def.span,
};
module.macros.borrow_mut().insert(def.ident.name, binding);
self.macro_names.insert(def.ident.name);
}
if def.export {
def.id = self.next_node_id();
self.exported_macros.push(def);
}
}
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
if let NormalTT(..) = *ext {
self.macro_names.insert(ident.name);
}
self.graph_root.macros.borrow_mut().insert(ident.name, NameBinding {
2016-09-30 22:49:12 -05:00
ext: ext,
expansion: Mark::root(),
shadowing: false,
span: DUMMY_SP,
2016-09-30 22:49:12 -05:00
});
}
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>) {
self.macros_at_scope.insert(id, macros);
}
fn find_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
for i in 0..attrs.len() {
let name = intern(&attrs[i].name());
match self.invocations[&Mark::root()].module.get().macros.borrow().get(&name) {
2016-09-30 22:49:12 -05:00
Some(binding) => match *binding.ext {
2016-09-06 00:57:58 -05:00
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
return Some(attrs.remove(i))
}
_ => {}
},
None => {}
}
}
None
}
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation) -> Option<Rc<SyntaxExtension>> {
let (name, span) = match invoc.kind {
InvocationKind::Bang { ref mac, .. } => {
let path = &mac.node.path;
if path.segments.len() > 1 || path.global ||
!path.segments[0].parameters.is_empty() {
self.session.span_err(path.span,
"expected macro name without module separators");
return None;
}
(path.segments[0].identifier.name, path.span)
}
InvocationKind::Attr { ref attr, .. } => (intern(&*attr.name()), attr.span),
};
self.resolve_macro_name(scope, name, true).or_else(|| {
2016-09-25 22:17:05 -05:00
let mut err =
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
self.suggest_macro_name(&name.as_str(), &mut err);
err.emit();
None
})
}
fn resolve_derive_mode(&mut self, ident: ast::Ident) -> Option<Rc<MultiItemModifier>> {
self.derive_modes.get(&ident.name).cloned()
}
}
impl<'a> Resolver<'a> {
pub fn resolve_macro_name(&mut self, scope: Mark, name: ast::Name, record_used: bool)
-> Option<Rc<SyntaxExtension>> {
let invocation = self.invocations[&scope];
let mut module = invocation.module.get();
loop {
2016-09-30 22:49:12 -05:00
if let Some(binding) = module.macros.borrow().get(&name) {
let mut backtrace = invocation.backtrace.data();
while binding.expansion != backtrace.outer_mark {
if backtrace.outer_mark != Mark::root() {
backtrace = backtrace.prev_ctxt.data();
continue
}
if record_used && binding.shadowing &&
self.macro_shadowing_errors.insert(binding.span) {
let msg = format!("`{}` is already in scope", name);
self.session.struct_span_err(binding.span, &msg)
.note("macro-expanded `macro_rules!`s and `#[macro_use]`s \
may not shadow existing macros (see RFC 1560)")
.emit();
}
break
}
2016-09-30 22:49:12 -05:00
return Some(binding.ext.clone());
}
2016-09-16 01:45:03 -05:00
match module.parent {
Some(parent) => module = parent,
None => break,
}
}
None
}
fn suggest_macro_name(&mut self, name: &str, err: &mut DiagnosticBuilder<'a>) {
if let Some(suggestion) = find_best_match_for_name(self.macro_names.iter(), name, None) {
if suggestion != name {
err.help(&format!("did you mean `{}!`?", suggestion));
} else {
err.help(&format!("have you added the `#[macro_use]` on the module/import?"));
}
}
}
fn collect_def_ids(&mut self, mark: Mark, expansion: &Expansion) {
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
let InvocationData { def_index, const_integer, backtrace, .. } = invocations[&mark].clone();
2016-09-16 03:50:34 -05:00
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
invocations.entry(invoc.mark).or_insert_with(|| {
arenas.alloc_invocation_data(InvocationData {
2016-09-16 03:50:34 -05:00
backtrace: backtrace.apply_mark(invoc.mark),
def_index: invoc.def_index,
const_integer: invoc.const_integer,
module: Cell::new(graph_root),
})
});
};
2016-09-16 01:45:03 -05:00
let mut def_collector = DefCollector::new(&mut self.definitions);
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
def_collector.with_parent(def_index, |def_collector| {
if const_integer {
if let Expansion::Expr(ref expr) = *expansion {
def_collector.visit_ast_const_integer(expr);
}
}
expansion.visit_with(def_collector)
});
}
}