|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use Resolver; |
| 12 | +use rustc::util::nodemap::FnvHashMap; |
| 13 | +use std::cell::RefCell; |
| 14 | +use std::mem; |
| 15 | +use std::rc::Rc; |
| 16 | +use syntax::ast::{self, Name}; |
| 17 | +use syntax::errors::DiagnosticBuilder; |
| 18 | +use syntax::ext::base::{self, LoadedMacro, MultiModifier, MultiDecorator}; |
| 19 | +use syntax::ext::base::{NormalTT, SyntaxExtension}; |
| 20 | +use syntax::ext::expand::{Expansion, Invocation, InvocationKind}; |
| 21 | +use syntax::ext::hygiene::Mark; |
| 22 | +use syntax::parse::token::intern; |
| 23 | +use syntax::util::lev_distance::find_best_match_for_name; |
| 24 | +use syntax::visit::{self, Visitor}; |
| 25 | + |
| 26 | +#[derive(Clone, Default)] |
| 27 | +pub struct ExpansionData { |
| 28 | + module: Rc<ModuleData>, |
| 29 | +} |
| 30 | + |
| 31 | +// FIXME(jseyfried): merge with `::ModuleS`. |
| 32 | +#[derive(Default)] |
| 33 | +struct ModuleData { |
| 34 | + parent: Option<Rc<ModuleData>>, |
| 35 | + macros: RefCell<FnvHashMap<Name, Rc<SyntaxExtension>>>, |
| 36 | + macros_escape: bool, |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a> base::Resolver for Resolver<'a> { |
| 40 | + fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro> { |
| 41 | + self.macro_loader.load_crate(extern_crate, allows_macros) |
| 42 | + } |
| 43 | + |
| 44 | + fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) { |
| 45 | + expansion.visit_with(&mut ExpansionVisitor { |
| 46 | + current_module: self.expansion_data[mark.as_u32() as usize].module.clone(), |
| 47 | + resolver: self, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + fn add_macro(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>) { |
| 52 | + if let NormalTT(..) = *ext { |
| 53 | + self.macro_names.insert(ident.name); |
| 54 | + } |
| 55 | + |
| 56 | + let mut module = self.expansion_data[scope.as_u32() as usize].module.clone(); |
| 57 | + while module.macros_escape { |
| 58 | + module = module.parent.clone().unwrap(); |
| 59 | + } |
| 60 | + module.macros.borrow_mut().insert(ident.name, ext); |
| 61 | + } |
| 62 | + |
| 63 | + fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>) { |
| 64 | + self.macros_at_scope.insert(id, macros); |
| 65 | + } |
| 66 | + |
| 67 | + fn find_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> { |
| 68 | + for i in 0..attrs.len() { |
| 69 | + let name = intern(&attrs[i].name()); |
| 70 | + match self.expansion_data[0].module.macros.borrow().get(&name) { |
| 71 | + Some(ext) => match **ext { |
| 72 | + MultiModifier(..) | MultiDecorator(..) => return Some(attrs.remove(i)), |
| 73 | + _ => {} |
| 74 | + }, |
| 75 | + None => {} |
| 76 | + } |
| 77 | + } |
| 78 | + None |
| 79 | + } |
| 80 | + |
| 81 | + fn resolve_invoc(&mut self, invoc: &Invocation) -> Option<Rc<SyntaxExtension>> { |
| 82 | + let (name, span) = match invoc.kind { |
| 83 | + InvocationKind::Bang { ref mac, .. } => { |
| 84 | + let path = &mac.node.path; |
| 85 | + if path.segments.len() > 1 || path.global || |
| 86 | + !path.segments[0].parameters.is_empty() { |
| 87 | + self.session.span_err(path.span, |
| 88 | + "expected macro name without module separators"); |
| 89 | + return None; |
| 90 | + } |
| 91 | + (path.segments[0].identifier.name, path.span) |
| 92 | + } |
| 93 | + InvocationKind::Attr { ref attr, .. } => (intern(&*attr.name()), attr.span), |
| 94 | + }; |
| 95 | + |
| 96 | + let mut module = self.expansion_data[invoc.mark().as_u32() as usize].module.clone(); |
| 97 | + loop { |
| 98 | + if let Some(ext) = module.macros.borrow().get(&name) { |
| 99 | + return Some(ext.clone()); |
| 100 | + } |
| 101 | + match module.parent.clone() { |
| 102 | + Some(parent) => module = parent, |
| 103 | + None => break, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + let mut err = |
| 108 | + self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name)); |
| 109 | + self.suggest_macro_name(&name.as_str(), &mut err); |
| 110 | + err.emit(); |
| 111 | + None |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl<'a> Resolver<'a> { |
| 116 | + fn suggest_macro_name(&mut self, name: &str, err: &mut DiagnosticBuilder<'a>) { |
| 117 | + if let Some(suggestion) = find_best_match_for_name(self.macro_names.iter(), name, None) { |
| 118 | + if suggestion != name { |
| 119 | + err.help(&format!("did you mean `{}!`?", suggestion)); |
| 120 | + } else { |
| 121 | + err.help(&format!("have you added the `#[macro_use]` on the module/import?")); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +struct ExpansionVisitor<'b, 'a: 'b> { |
| 128 | + resolver: &'b mut Resolver<'a>, |
| 129 | + current_module: Rc<ModuleData>, |
| 130 | +} |
| 131 | + |
| 132 | +impl<'a, 'b> ExpansionVisitor<'a, 'b> { |
| 133 | + fn visit_invoc(&mut self, id: ast::NodeId) { |
| 134 | + assert_eq!(id, self.resolver.expansion_data.len() as u32); |
| 135 | + self.resolver.expansion_data.push(ExpansionData { |
| 136 | + module: self.current_module.clone(), |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + // does this attribute list contain "macro_use"? |
| 141 | + fn contains_macro_use(&mut self, attrs: &[ast::Attribute]) -> bool { |
| 142 | + for attr in attrs { |
| 143 | + if attr.check_name("macro_escape") { |
| 144 | + let msg = "macro_escape is a deprecated synonym for macro_use"; |
| 145 | + let mut err = self.resolver.session.struct_span_warn(attr.span, msg); |
| 146 | + if let ast::AttrStyle::Inner = attr.node.style { |
| 147 | + err.help("consider an outer attribute, #[macro_use] mod ...").emit(); |
| 148 | + } else { |
| 149 | + err.emit(); |
| 150 | + } |
| 151 | + } else if !attr.check_name("macro_use") { |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + if !attr.is_word() { |
| 156 | + self.resolver.session.span_err(attr.span, |
| 157 | + "arguments to macro_use are not allowed here"); |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + false |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +macro_rules! method { |
| 167 | + ($visit:ident: $ty:ty, $invoc:path, $walk:ident) => { |
| 168 | + fn $visit(&mut self, node: &$ty) { |
| 169 | + match node.node { |
| 170 | + $invoc(..) => self.visit_invoc(node.id), |
| 171 | + _ => visit::$walk(self, node), |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl<'a, 'b> Visitor for ExpansionVisitor<'a, 'b> { |
| 178 | + method!(visit_trait_item: ast::TraitItem, ast::TraitItemKind::Macro, walk_trait_item); |
| 179 | + method!(visit_impl_item: ast::ImplItem, ast::ImplItemKind::Macro, walk_impl_item); |
| 180 | + method!(visit_stmt: ast::Stmt, ast::StmtKind::Mac, walk_stmt); |
| 181 | + method!(visit_expr: ast::Expr, ast::ExprKind::Mac, walk_expr); |
| 182 | + method!(visit_pat: ast::Pat, ast::PatKind::Mac, walk_pat); |
| 183 | + method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty); |
| 184 | + |
| 185 | + fn visit_item(&mut self, item: &ast::Item) { |
| 186 | + match item.node { |
| 187 | + ast::ItemKind::Mac(..) if item.id == ast::DUMMY_NODE_ID => {} // Scope placeholder |
| 188 | + ast::ItemKind::Mac(..) => self.visit_invoc(item.id), |
| 189 | + ast::ItemKind::Mod(..) => { |
| 190 | + let module_data = ModuleData { |
| 191 | + parent: Some(self.current_module.clone()), |
| 192 | + macros: RefCell::new(FnvHashMap()), |
| 193 | + macros_escape: self.contains_macro_use(&item.attrs), |
| 194 | + }; |
| 195 | + let orig_module = mem::replace(&mut self.current_module, Rc::new(module_data)); |
| 196 | + visit::walk_item(self, item); |
| 197 | + self.current_module = orig_module; |
| 198 | + } |
| 199 | + _ => visit::walk_item(self, item), |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + fn visit_block(&mut self, block: &ast::Block) { |
| 204 | + let module_data = ModuleData { |
| 205 | + parent: Some(self.current_module.clone()), |
| 206 | + macros: RefCell::new(FnvHashMap()), |
| 207 | + macros_escape: false, |
| 208 | + }; |
| 209 | + let orig_module = mem::replace(&mut self.current_module, Rc::new(module_data)); |
| 210 | + visit::walk_block(self, block); |
| 211 | + self.current_module = orig_module; |
| 212 | + } |
| 213 | +} |
0 commit comments