2014-02-11 22:43:23 -08:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2016-09-29 00:22:46 +00:00
|
|
|
use ast::{Block, Ident, Mac_, PatKind};
|
2016-09-23 23:09:23 +00:00
|
|
|
use ast::{Name, MacStmtStyle, StmtKind, ItemKind};
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2016-07-16 19:11:28 +00:00
|
|
|
use ext::hygiene::Mark;
|
2016-09-13 06:25:02 +00:00
|
|
|
use ext::placeholders::{placeholder, PlaceholderExpander};
|
2016-06-22 08:03:42 +00:00
|
|
|
use attr::{self, HasAttrs};
|
2016-08-29 05:32:41 +00:00
|
|
|
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{self, Span, ExpnId};
|
2016-09-23 07:23:01 +00:00
|
|
|
use config::{is_test_or_bench, StripUnconfigured};
|
2012-09-04 11:37:29 -07:00
|
|
|
use ext::base::*;
|
2015-12-10 23:23:14 +09:00
|
|
|
use feature_gate::{self, Features};
|
2014-07-02 23:16:01 -07:00
|
|
|
use fold;
|
2012-12-23 17:41:37 -05:00
|
|
|
use fold::*;
|
2016-11-05 04:16:26 +00:00
|
|
|
use parse::{ParseSess, DirectoryOwnership, PResult, lexer};
|
2016-09-06 17:57:58 +12:00
|
|
|
use parse::parser::Parser;
|
2016-11-16 08:21:52 +00:00
|
|
|
use parse::token;
|
2016-09-06 17:57:58 +12:00
|
|
|
use print::pprust;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2016-09-29 00:22:46 +00:00
|
|
|
use std_inject;
|
2016-11-16 08:21:52 +00:00
|
|
|
use symbol::keywords;
|
2016-08-29 16:16:43 +12:00
|
|
|
use tokenstream::{TokenTree, TokenStream};
|
2014-09-13 19:06:01 +03:00
|
|
|
use util::small_vector::SmallVector;
|
2016-09-07 23:21:59 +00:00
|
|
|
use visit::Visitor;
|
2011-08-05 13:06:11 -07:00
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
use std::mem;
|
2016-08-31 09:02:45 +00:00
|
|
|
use std::path::PathBuf;
|
2016-09-01 06:44:54 +00:00
|
|
|
use std::rc::Rc;
|
2016-08-31 09:02:45 +00:00
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
macro_rules! expansions {
|
2016-09-02 09:12:47 +00:00
|
|
|
($($kind:ident: $ty:ty [$($vec:ident, $ty_elt:ty)*], $kind_name:expr, .$make:ident,
|
2016-09-07 23:21:59 +00:00
|
|
|
$(.$fold:ident)* $(lift .$fold_elt:ident)*,
|
|
|
|
$(.$visit:ident)* $(lift .$visit_elt:ident)*;)*) => {
|
2016-09-23 09:32:58 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2016-08-29 05:32:41 +00:00
|
|
|
pub enum ExpansionKind { OptExpr, $( $kind, )* }
|
|
|
|
pub enum Expansion { OptExpr(Option<P<ast::Expr>>), $( $kind($ty), )* }
|
2016-08-27 05:27:59 +00:00
|
|
|
|
|
|
|
impl ExpansionKind {
|
2016-09-23 09:32:58 +00:00
|
|
|
pub fn name(self) -> &'static str {
|
2016-08-27 05:27:59 +00:00
|
|
|
match self {
|
|
|
|
ExpansionKind::OptExpr => "expression",
|
|
|
|
$( ExpansionKind::$kind => $kind_name, )*
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 06:12:54 +00:00
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
fn make_from<'a>(self, result: Box<MacResult + 'a>) -> Option<Expansion> {
|
|
|
|
match self {
|
|
|
|
ExpansionKind::OptExpr => result.make_expr().map(Some).map(Expansion::OptExpr),
|
|
|
|
$( ExpansionKind::$kind => result.$make().map(Expansion::$kind), )*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 11:02:42 +00:00
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
impl Expansion {
|
2016-08-29 05:32:41 +00:00
|
|
|
pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
|
2016-08-27 05:27:59 +00:00
|
|
|
match self {
|
|
|
|
Expansion::OptExpr(expr) => expr,
|
|
|
|
_ => panic!("Expansion::make_* called on the wrong kind of expansion"),
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 05:32:41 +00:00
|
|
|
$( pub fn $make(self) -> $ty {
|
2016-08-27 05:27:59 +00:00
|
|
|
match self {
|
|
|
|
Expansion::$kind(ast) => ast,
|
|
|
|
_ => panic!("Expansion::make_* called on the wrong kind of expansion"),
|
|
|
|
}
|
|
|
|
} )*
|
2016-05-19 09:45:37 +00:00
|
|
|
|
2016-08-29 05:32:41 +00:00
|
|
|
pub fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
|
2016-08-27 05:27:59 +00:00
|
|
|
use self::Expansion::*;
|
|
|
|
match self {
|
|
|
|
OptExpr(expr) => OptExpr(expr.and_then(|expr| folder.fold_opt_expr(expr))),
|
|
|
|
$($( $kind(ast) => $kind(folder.$fold(ast)), )*)*
|
|
|
|
$($( $kind(ast) => {
|
|
|
|
$kind(ast.into_iter().flat_map(|ast| folder.$fold_elt(ast)).collect())
|
|
|
|
}, )*)*
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
|
|
|
pub fn visit_with<V: Visitor>(&self, visitor: &mut V) {
|
|
|
|
match *self {
|
|
|
|
Expansion::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
|
|
|
|
Expansion::OptExpr(None) => {}
|
|
|
|
$($( Expansion::$kind(ref ast) => visitor.$visit(ast), )*)*
|
2016-11-02 22:33:35 -06:00
|
|
|
$($( Expansion::$kind(ref ast) => for ast in &ast[..] {
|
2016-09-07 23:21:59 +00:00
|
|
|
visitor.$visit_elt(ast);
|
|
|
|
}, )*)*
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
|
|
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
|
|
|
|
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
|
|
|
self.expand(Expansion::OptExpr(Some(expr))).make_opt_expr()
|
|
|
|
}
|
|
|
|
$($(fn $fold(&mut self, node: $ty) -> $ty {
|
|
|
|
self.expand(Expansion::$kind(node)).$make()
|
|
|
|
})*)*
|
|
|
|
$($(fn $fold_elt(&mut self, node: $ty_elt) -> $ty {
|
|
|
|
self.expand(Expansion::$kind(SmallVector::one(node))).$make()
|
|
|
|
})*)*
|
|
|
|
}
|
2016-09-23 09:32:58 +00:00
|
|
|
|
|
|
|
impl<'a> MacResult for ::ext::tt::macro_rules::ParserAnyMacro<'a> {
|
|
|
|
$(fn $make(self: Box<::ext::tt::macro_rules::ParserAnyMacro<'a>>) -> Option<$ty> {
|
|
|
|
Some(self.make(ExpansionKind::$kind).$make())
|
|
|
|
})*
|
|
|
|
}
|
2016-08-27 05:27:59 +00:00
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
expansions! {
|
2016-09-07 23:21:59 +00:00
|
|
|
Expr: P<ast::Expr> [], "expression", .make_expr, .fold_expr, .visit_expr;
|
|
|
|
Pat: P<ast::Pat> [], "pattern", .make_pat, .fold_pat, .visit_pat;
|
|
|
|
Ty: P<ast::Ty> [], "type", .make_ty, .fold_ty, .visit_ty;
|
2016-09-02 09:12:47 +00:00
|
|
|
Stmts: SmallVector<ast::Stmt> [SmallVector, ast::Stmt],
|
2016-09-07 23:21:59 +00:00
|
|
|
"statement", .make_stmts, lift .fold_stmt, lift .visit_stmt;
|
2016-09-02 09:12:47 +00:00
|
|
|
Items: SmallVector<P<ast::Item>> [SmallVector, P<ast::Item>],
|
2016-09-07 23:21:59 +00:00
|
|
|
"item", .make_items, lift .fold_item, lift .visit_item;
|
2016-09-02 09:12:47 +00:00
|
|
|
TraitItems: SmallVector<ast::TraitItem> [SmallVector, ast::TraitItem],
|
2016-09-07 23:21:59 +00:00
|
|
|
"trait item", .make_trait_items, lift .fold_trait_item, lift .visit_trait_item;
|
2016-09-02 09:12:47 +00:00
|
|
|
ImplItems: SmallVector<ast::ImplItem> [SmallVector, ast::ImplItem],
|
2016-09-07 23:21:59 +00:00
|
|
|
"impl item", .make_impl_items, lift .fold_impl_item, lift .visit_impl_item;
|
2016-05-19 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
impl ExpansionKind {
|
|
|
|
fn dummy(self, span: Span) -> Expansion {
|
|
|
|
self.make_from(DummyResult::any(span)).unwrap()
|
2016-06-11 22:59:33 +00:00
|
|
|
}
|
2016-09-02 06:14:38 +00:00
|
|
|
|
|
|
|
fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I) -> Expansion {
|
|
|
|
let items = items.into_iter();
|
|
|
|
match self {
|
|
|
|
ExpansionKind::Items =>
|
|
|
|
Expansion::Items(items.map(Annotatable::expect_item).collect()),
|
|
|
|
ExpansionKind::ImplItems =>
|
|
|
|
Expansion::ImplItems(items.map(Annotatable::expect_impl_item).collect()),
|
|
|
|
ExpansionKind::TraitItems =>
|
|
|
|
Expansion::TraitItems(items.map(Annotatable::expect_trait_item).collect()),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
}
|
2015-01-27 01:22:12 +01:00
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
pub struct Invocation {
|
2016-09-07 23:21:59 +00:00
|
|
|
pub kind: InvocationKind,
|
2016-09-02 06:14:38 +00:00
|
|
|
expansion_kind: ExpansionKind,
|
2016-09-07 23:21:59 +00:00
|
|
|
expansion_data: ExpansionData,
|
2016-09-02 06:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
pub enum InvocationKind {
|
2016-09-02 06:14:38 +00:00
|
|
|
Bang {
|
|
|
|
attrs: Vec<ast::Attribute>,
|
|
|
|
mac: ast::Mac,
|
|
|
|
ident: Option<Ident>,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Attr {
|
|
|
|
attr: ast::Attribute,
|
|
|
|
item: Annotatable,
|
|
|
|
},
|
2011-07-06 15:22:23 -07:00
|
|
|
}
|
2013-07-29 17:25:00 -07:00
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
impl Invocation {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
match self.kind {
|
|
|
|
InvocationKind::Bang { span, .. } => span,
|
|
|
|
InvocationKind::Attr { ref attr, .. } => attr.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
pub struct MacroExpander<'a, 'b:'a> {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub cx: &'a mut ExtCtxt<'b>,
|
2016-09-06 05:42:45 +00:00
|
|
|
monotonic: bool, // c.f. `cx.monotonic_expander()`
|
2014-12-14 15:42:41 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> MacroExpander<'a, 'b> {
|
2016-09-06 05:42:45 +00:00
|
|
|
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
|
2016-09-12 09:47:54 +00:00
|
|
|
MacroExpander { cx: cx, monotonic: monotonic }
|
2014-12-14 15:42:41 +13:00
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
|
2016-09-29 00:22:46 +00:00
|
|
|
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 {
|
2016-11-16 08:21:52 +00:00
|
|
|
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
|
2016-09-29 00:22:46 +00:00
|
|
|
directory: PathBuf::from(self.cx.codemap().span_to_filename(krate.span)),
|
|
|
|
};
|
|
|
|
module.directory.pop();
|
|
|
|
self.cx.current_expansion.module = Rc::new(module);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
2016-09-21 09:20:42 +00:00
|
|
|
let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
|
|
|
|
attrs: krate.attrs,
|
|
|
|
span: krate.span,
|
|
|
|
node: ast::ItemKind::Mod(krate.module),
|
|
|
|
ident: keywords::Invalid.ident(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
vis: ast::Visibility::Public,
|
|
|
|
})));
|
|
|
|
|
|
|
|
match self.expand(krate_item).make_items().pop().unwrap().unwrap() {
|
|
|
|
ast::Item { attrs, node: ast::ItemKind::Mod(module), .. } => {
|
|
|
|
krate.attrs = attrs;
|
|
|
|
krate.module = module;
|
|
|
|
},
|
2016-09-06 01:45:23 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2016-09-05 00:10:27 +00:00
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
krate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fully expand all the invocations in `expansion`.
|
|
|
|
fn expand(&mut self, expansion: Expansion) -> Expansion {
|
2016-09-07 23:21:59 +00:00
|
|
|
let orig_expansion_data = self.cx.current_expansion.clone();
|
|
|
|
self.cx.current_expansion.depth = 0;
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
let (expansion, mut invocations) = self.collect_invocations(expansion);
|
2016-11-10 10:11:25 +00:00
|
|
|
self.resolve_imports();
|
2016-09-02 09:12:47 +00:00
|
|
|
invocations.reverse();
|
|
|
|
|
2016-09-26 22:54:36 +00:00
|
|
|
let mut expansions = Vec::new();
|
2016-10-11 03:41:48 +00:00
|
|
|
let mut undetermined_invocations = Vec::new();
|
|
|
|
let (mut progress, mut force) = (false, !self.monotonic);
|
|
|
|
loop {
|
|
|
|
let invoc = if let Some(invoc) = invocations.pop() {
|
|
|
|
invoc
|
|
|
|
} else {
|
2016-11-10 10:11:25 +00:00
|
|
|
self.resolve_imports();
|
|
|
|
if undetermined_invocations.is_empty() { break }
|
2016-10-11 03:41:48 +00:00
|
|
|
invocations = mem::replace(&mut undetermined_invocations, Vec::new());
|
|
|
|
force = !mem::replace(&mut progress, false);
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
|
|
|
|
let scope =
|
|
|
|
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
2016-10-14 23:08:53 +00:00
|
|
|
let resolution = match invoc.kind {
|
|
|
|
InvocationKind::Bang { ref mac, .. } => {
|
|
|
|
self.cx.resolver.resolve_macro(scope, &mac.node.path, force)
|
|
|
|
}
|
|
|
|
InvocationKind::Attr { ref attr, .. } => {
|
2016-11-16 08:21:52 +00:00
|
|
|
let ident = Ident::with_empty_ctxt(attr.name());
|
2016-10-14 23:08:53 +00:00
|
|
|
let path = ast::Path::from_ident(attr.span, ident);
|
|
|
|
self.cx.resolver.resolve_macro(scope, &path, force)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let ext = match resolution {
|
2016-10-11 03:41:48 +00:00
|
|
|
Ok(ext) => Some(ext),
|
|
|
|
Err(Determinacy::Determined) => None,
|
|
|
|
Err(Determinacy::Undetermined) => {
|
|
|
|
undetermined_invocations.push(invoc);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
progress = true;
|
2016-09-07 23:21:59 +00:00
|
|
|
let ExpansionData { depth, mark, .. } = invoc.expansion_data;
|
|
|
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
2016-09-19 07:27:20 +00:00
|
|
|
self.cx.current_expansion.mark = scope;
|
2016-10-11 03:41:48 +00:00
|
|
|
let expansion = match ext {
|
2016-09-07 23:21:59 +00:00
|
|
|
Some(ext) => self.expand_invoc(invoc, ext),
|
|
|
|
None => invoc.expansion_kind.dummy(invoc.span()),
|
|
|
|
};
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
|
|
let (expansion, new_invocations) = self.collect_invocations(expansion);
|
|
|
|
|
2016-09-26 22:54:36 +00:00
|
|
|
if expansions.len() < depth {
|
2016-09-02 03:35:59 +00:00
|
|
|
expansions.push(Vec::new());
|
|
|
|
}
|
2016-09-26 22:54:36 +00:00
|
|
|
expansions[depth - 1].push((mark.as_u32(), expansion));
|
2016-09-12 09:47:54 +00:00
|
|
|
if !self.cx.ecfg.single_step {
|
2016-09-02 09:12:47 +00:00
|
|
|
invocations.extend(new_invocations.into_iter().rev());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
self.cx.current_expansion = orig_expansion_data;
|
|
|
|
|
2016-09-06 05:42:45 +00:00
|
|
|
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
2016-09-02 03:35:59 +00:00
|
|
|
while let Some(expansions) = expansions.pop() {
|
|
|
|
for (mark, expansion) in expansions.into_iter().rev() {
|
2016-08-31 14:00:29 +03:00
|
|
|
placeholder_expander.add(ast::NodeId::from_u32(mark), expansion);
|
2016-09-02 03:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:54:36 +00:00
|
|
|
expansion.fold_with(&mut placeholder_expander)
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
if self.monotonic {
|
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
|
|
|
self.cx.resolver.resolve_imports();
|
|
|
|
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
fn collect_invocations(&mut self, expansion: Expansion) -> (Expansion, Vec<Invocation>) {
|
2016-09-07 22:24:01 +00:00
|
|
|
let result = {
|
|
|
|
let mut collector = InvocationCollector {
|
|
|
|
cfg: StripUnconfigured {
|
|
|
|
should_test: self.cx.ecfg.should_test,
|
|
|
|
sess: self.cx.parse_sess,
|
|
|
|
features: self.cx.ecfg.features,
|
|
|
|
},
|
|
|
|
cx: self.cx,
|
|
|
|
invocations: Vec::new(),
|
2016-09-06 05:42:45 +00:00
|
|
|
monotonic: self.monotonic,
|
2016-09-07 22:24:01 +00:00
|
|
|
};
|
|
|
|
(expansion.fold_with(&mut collector), collector.invocations)
|
|
|
|
};
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2016-09-19 07:27:20 +00:00
|
|
|
if self.monotonic {
|
2016-09-20 06:33:42 +00:00
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
2016-09-19 07:27:20 +00:00
|
|
|
let mark = self.cx.current_expansion.mark;
|
|
|
|
self.cx.resolver.visit_expansion(mark, &result.0);
|
2016-09-20 06:33:42 +00:00
|
|
|
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
2016-09-19 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
result
|
2016-05-16 10:09:23 +00:00
|
|
|
}
|
2016-06-12 01:50:52 +00:00
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
fn expand_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
2016-09-01 07:01:45 +00:00
|
|
|
match invoc.kind {
|
2016-09-07 23:21:59 +00:00
|
|
|
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext),
|
|
|
|
InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext),
|
2016-09-01 07:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
fn expand_attr_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
2016-09-01 07:01:45 +00:00
|
|
|
let Invocation { expansion_kind: kind, .. } = invoc;
|
|
|
|
let (attr, item) = match invoc.kind {
|
|
|
|
InvocationKind::Attr { attr, item } => (attr, item),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
attr::mark_used(&attr);
|
2016-11-15 04:34:52 +00:00
|
|
|
let name = attr.name();
|
2016-09-01 07:01:45 +00:00
|
|
|
self.cx.bt_push(ExpnInfo {
|
|
|
|
call_site: attr.span,
|
|
|
|
callee: NameAndSpan {
|
2016-09-23 23:09:23 +00:00
|
|
|
format: MacroAttribute(name),
|
2016-09-01 07:01:45 +00:00
|
|
|
span: Some(attr.span),
|
|
|
|
allow_internal_unstable: false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
match *ext {
|
2016-09-01 07:01:45 +00:00
|
|
|
MultiModifier(ref mac) => {
|
2016-11-14 12:00:25 +00:00
|
|
|
let item = mac.expand(self.cx, attr.span, &attr.value, item);
|
2016-09-01 07:01:45 +00:00
|
|
|
kind.expect_from_annotatables(item)
|
|
|
|
}
|
|
|
|
MultiDecorator(ref mac) => {
|
|
|
|
let mut items = Vec::new();
|
2016-11-14 12:00:25 +00:00
|
|
|
mac.expand(self.cx, attr.span, &attr.value, &item,
|
2016-09-01 07:01:45 +00:00
|
|
|
&mut |item| items.push(item));
|
|
|
|
items.push(item);
|
|
|
|
kind.expect_from_annotatables(items)
|
|
|
|
}
|
2016-08-29 16:16:43 +12:00
|
|
|
SyntaxExtension::AttrProcMacro(ref mac) => {
|
2016-09-06 17:57:58 +12:00
|
|
|
let attr_toks = TokenStream::from_tts(tts_for_attr(&attr, &self.cx.parse_sess));
|
|
|
|
let item_toks = TokenStream::from_tts(tts_for_item(&item, &self.cx.parse_sess));
|
|
|
|
|
|
|
|
let tok_result = mac.expand(self.cx, attr.span, attr_toks, item_toks);
|
2016-09-23 23:09:23 +00:00
|
|
|
self.parse_expansion(tok_result, kind, name, attr.span)
|
2016-08-29 16:16:43 +12:00
|
|
|
}
|
2016-10-15 20:50:30 +00:00
|
|
|
SyntaxExtension::CustomDerive(_) => {
|
|
|
|
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", name));
|
|
|
|
kind.dummy(attr.span)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let msg = &format!("macro `{}` may not be used in attributes", name);
|
|
|
|
self.cx.span_err(attr.span, &msg);
|
|
|
|
kind.dummy(attr.span)
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Expand a macro invocation. Returns the result of expansion.
|
2016-09-07 23:21:59 +00:00
|
|
|
fn expand_bang_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
2016-09-19 07:27:20 +00:00
|
|
|
let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind);
|
2016-09-01 07:01:45 +00:00
|
|
|
let (attrs, mac, ident, span) = match invoc.kind {
|
|
|
|
InvocationKind::Bang { attrs, mac, ident, span } => (attrs, mac, ident, span),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let Mac_ { path, tts, .. } = mac.node;
|
|
|
|
|
|
|
|
// Detect use of feature-gated or invalid attributes on macro invoations
|
|
|
|
// since they will not be detected after macro expansion.
|
|
|
|
for attr in attrs.iter() {
|
2016-09-24 18:42:54 +02:00
|
|
|
feature_gate::check_attribute(&attr, &self.cx.parse_sess,
|
2016-09-01 07:01:45 +00:00
|
|
|
&self.cx.parse_sess.codemap(),
|
|
|
|
&self.cx.ecfg.features.unwrap());
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:58:46 +00:00
|
|
|
let extname = path.segments.last().unwrap().identifier.name;
|
2016-09-01 07:01:45 +00:00
|
|
|
let ident = ident.unwrap_or(keywords::Invalid.ident());
|
|
|
|
let marked_tts = mark_tts(&tts, mark);
|
2016-09-07 23:21:59 +00:00
|
|
|
let opt_expanded = match *ext {
|
2016-09-01 07:01:45 +00:00
|
|
|
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
|
|
|
|
if ident.name != keywords::Invalid.name() {
|
|
|
|
let msg =
|
|
|
|
format!("macro {}! expects no ident argument, given '{}'", extname, ident);
|
|
|
|
self.cx.span_err(path.span, &msg);
|
|
|
|
return kind.dummy(span);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cx.bt_push(ExpnInfo {
|
|
|
|
call_site: span,
|
|
|
|
callee: NameAndSpan {
|
|
|
|
format: MacroBang(extname),
|
|
|
|
span: exp_span,
|
|
|
|
allow_internal_unstable: allow_internal_unstable,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
kind.make_from(expandfun.expand(self.cx, span, &marked_tts))
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentTT(ref expander, tt_span, allow_internal_unstable) => {
|
|
|
|
if ident.name == keywords::Invalid.name() {
|
|
|
|
self.cx.span_err(path.span,
|
|
|
|
&format!("macro {}! expects an ident argument", extname));
|
|
|
|
return kind.dummy(span);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.cx.bt_push(ExpnInfo {
|
|
|
|
call_site: span,
|
|
|
|
callee: NameAndSpan {
|
|
|
|
format: MacroBang(extname),
|
|
|
|
span: tt_span,
|
|
|
|
allow_internal_unstable: allow_internal_unstable,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-13 05:21:54 +00:00
|
|
|
kind.make_from(expander.expand(self.cx, span, ident, marked_tts, attrs))
|
2016-09-01 07:01:45 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 16:16:43 +12:00
|
|
|
MultiDecorator(..) | MultiModifier(..) | SyntaxExtension::AttrProcMacro(..) => {
|
2016-09-01 07:01:45 +00:00
|
|
|
self.cx.span_err(path.span,
|
|
|
|
&format!("`{}` can only be used in attributes", extname));
|
|
|
|
return kind.dummy(span);
|
|
|
|
}
|
2016-08-29 16:16:43 +12:00
|
|
|
|
2016-10-15 20:50:30 +00:00
|
|
|
SyntaxExtension::CustomDerive(..) => {
|
|
|
|
self.cx.span_err(path.span, &format!("`{}` is a derive mode", extname));
|
|
|
|
return kind.dummy(span);
|
|
|
|
}
|
|
|
|
|
2016-08-29 16:16:43 +12:00
|
|
|
SyntaxExtension::ProcMacro(ref expandfun) => {
|
|
|
|
if ident.name != keywords::Invalid.name() {
|
|
|
|
let msg =
|
|
|
|
format!("macro {}! expects no ident argument, given '{}'", extname, ident);
|
2016-09-06 17:57:58 +12:00
|
|
|
self.cx.span_err(path.span, &msg);
|
|
|
|
return kind.dummy(span);
|
2016-08-29 16:16:43 +12:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:57:58 +12:00
|
|
|
self.cx.bt_push(ExpnInfo {
|
|
|
|
call_site: span,
|
2016-08-29 16:16:43 +12:00
|
|
|
callee: NameAndSpan {
|
|
|
|
format: MacroBang(extname),
|
|
|
|
// FIXME procedural macros do not have proper span info
|
|
|
|
// yet, when they do, we should use it here.
|
|
|
|
span: None,
|
|
|
|
// FIXME probably want to follow macro_rules macros here.
|
|
|
|
allow_internal_unstable: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-09-26 04:16:55 +00:00
|
|
|
let toks = TokenStream::from_tts(marked_tts);
|
|
|
|
let tok_result = expandfun.expand(self.cx, span, toks);
|
2016-09-23 23:09:23 +00:00
|
|
|
Some(self.parse_expansion(tok_result, kind, extname, span))
|
2016-08-29 16:16:43 +12:00
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let expanded = if let Some(expanded) = opt_expanded {
|
|
|
|
expanded
|
|
|
|
} else {
|
|
|
|
let msg = format!("non-{kind} macro in {kind} position: {name}",
|
|
|
|
name = path.segments[0].identifier.name, kind = kind.name());
|
|
|
|
self.cx.span_err(path.span, &msg);
|
|
|
|
return kind.dummy(span);
|
|
|
|
};
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
expanded.fold_with(&mut Marker {
|
|
|
|
mark: mark,
|
|
|
|
expn_id: Some(self.cx.backtrace()),
|
|
|
|
})
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
2016-09-23 23:09:23 +00:00
|
|
|
fn parse_expansion(&mut self, toks: TokenStream, kind: ExpansionKind, name: Name, span: Span)
|
|
|
|
-> Expansion {
|
2016-09-26 04:16:55 +00:00
|
|
|
let mut parser = self.cx.new_parser_from_tts(&toks.to_tts());
|
|
|
|
let expansion = match parser.parse_expansion(kind, false) {
|
|
|
|
Ok(expansion) => expansion,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return kind.dummy(span);
|
|
|
|
}
|
|
|
|
};
|
2016-09-23 23:09:23 +00:00
|
|
|
parser.ensure_complete_parse(name, kind.name(), span);
|
2016-09-26 04:16:55 +00:00
|
|
|
// FIXME better span info
|
|
|
|
expansion.fold_with(&mut ChangeSpan { span: span })
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 09:32:58 +00:00
|
|
|
impl<'a> Parser<'a> {
|
2016-09-26 04:16:55 +00:00
|
|
|
pub fn parse_expansion(&mut self, kind: ExpansionKind, macro_legacy_warnings: bool)
|
|
|
|
-> PResult<'a, Expansion> {
|
2016-09-23 09:32:58 +00:00
|
|
|
Ok(match kind {
|
|
|
|
ExpansionKind::Items => {
|
2016-11-02 22:33:35 -06:00
|
|
|
let mut items = SmallVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
while let Some(item) = self.parse_item()? {
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
Expansion::Items(items)
|
|
|
|
}
|
|
|
|
ExpansionKind::TraitItems => {
|
2016-11-02 22:33:35 -06:00
|
|
|
let mut items = SmallVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
while self.token != token::Eof {
|
|
|
|
items.push(self.parse_trait_item()?);
|
|
|
|
}
|
|
|
|
Expansion::TraitItems(items)
|
|
|
|
}
|
|
|
|
ExpansionKind::ImplItems => {
|
2016-11-02 22:33:35 -06:00
|
|
|
let mut items = SmallVector::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
while self.token != token::Eof {
|
|
|
|
items.push(self.parse_impl_item()?);
|
|
|
|
}
|
|
|
|
Expansion::ImplItems(items)
|
|
|
|
}
|
|
|
|
ExpansionKind::Stmts => {
|
2016-11-02 22:33:35 -06:00
|
|
|
let mut stmts = SmallVector::new();
|
2016-10-17 21:01:36 -07:00
|
|
|
while self.token != token::Eof &&
|
|
|
|
// won't make progress on a `}`
|
|
|
|
self.token != token::CloseDelim(token::Brace) {
|
2016-09-26 04:16:55 +00:00
|
|
|
if let Some(stmt) = self.parse_full_stmt(macro_legacy_warnings)? {
|
2016-09-23 09:32:58 +00:00
|
|
|
stmts.push(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expansion::Stmts(stmts)
|
|
|
|
}
|
|
|
|
ExpansionKind::Expr => Expansion::Expr(self.parse_expr()?),
|
|
|
|
ExpansionKind::OptExpr => Expansion::OptExpr(Some(self.parse_expr()?)),
|
|
|
|
ExpansionKind::Ty => Expansion::Ty(self.parse_ty()?),
|
|
|
|
ExpansionKind::Pat => Expansion::Pat(self.parse_pat()?),
|
|
|
|
})
|
|
|
|
}
|
2016-09-26 11:24:10 +00:00
|
|
|
|
|
|
|
pub fn ensure_complete_parse(&mut self, macro_name: ast::Name, kind_name: &str, span: Span) {
|
|
|
|
if self.token != token::Eof {
|
|
|
|
let msg = format!("macro expansion ignores token `{}` and any following",
|
|
|
|
self.this_token_to_string());
|
|
|
|
let mut err = self.diagnostic().struct_span_err(self.span, &msg);
|
|
|
|
let msg = format!("caused by the macro expansion here; the usage \
|
|
|
|
of `{}!` is likely invalid in {} context",
|
|
|
|
macro_name, kind_name);
|
|
|
|
err.span_note(span, &msg).emit();
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct InvocationCollector<'a, 'b: 'a> {
|
|
|
|
cx: &'a mut ExtCtxt<'b>,
|
2016-09-07 22:24:01 +00:00
|
|
|
cfg: StripUnconfigured<'a>,
|
2016-09-02 09:12:47 +00:00
|
|
|
invocations: Vec<Invocation>,
|
2016-09-06 05:42:45 +00:00
|
|
|
monotonic: bool,
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
macro_rules! fully_configure {
|
|
|
|
($this:ident, $node:ident, $noop_fold:ident) => {
|
|
|
|
match $noop_fold($node, &mut $this.cfg).pop() {
|
|
|
|
Some(node) => node,
|
2016-11-02 22:33:35 -06:00
|
|
|
None => return SmallVector::new(),
|
2016-09-07 22:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
impl<'a, 'b> InvocationCollector<'a, 'b> {
|
|
|
|
fn collect(&mut self, expansion_kind: ExpansionKind, kind: InvocationKind) -> Expansion {
|
|
|
|
let mark = Mark::fresh();
|
|
|
|
self.invocations.push(Invocation {
|
|
|
|
kind: kind,
|
|
|
|
expansion_kind: expansion_kind,
|
2016-09-26 22:54:36 +00:00
|
|
|
expansion_data: ExpansionData {
|
|
|
|
mark: mark,
|
|
|
|
depth: self.cx.current_expansion.depth + 1,
|
|
|
|
..self.cx.current_expansion.clone()
|
|
|
|
},
|
2016-09-01 07:01:45 +00:00
|
|
|
});
|
2016-08-31 14:00:29 +03:00
|
|
|
placeholder(expansion_kind, ast::NodeId::from_u32(mark.as_u32()))
|
2016-09-02 09:12:47 +00:00
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
fn collect_bang(
|
|
|
|
&mut self, mac: ast::Mac, attrs: Vec<ast::Attribute>, span: Span, kind: ExpansionKind,
|
|
|
|
) -> Expansion {
|
|
|
|
self.collect(kind, InvocationKind::Bang { attrs: attrs, mac: mac, ident: None, span: span })
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
fn collect_attr(&mut self, attr: ast::Attribute, item: Annotatable, kind: ExpansionKind)
|
|
|
|
-> Expansion {
|
|
|
|
self.collect(kind, InvocationKind::Attr { attr: attr, item: item })
|
|
|
|
}
|
|
|
|
|
|
|
|
// If `item` is an attr invocation, remove and return the macro attribute.
|
2016-09-07 23:21:59 +00:00
|
|
|
fn classify_item<T: HasAttrs>(&mut self, mut item: T) -> (T, Option<ast::Attribute>) {
|
2016-09-02 09:12:47 +00:00
|
|
|
let mut attr = None;
|
|
|
|
item = item.map_attrs(|mut attrs| {
|
2016-09-07 23:21:59 +00:00
|
|
|
attr = self.cx.resolver.find_attr_invoc(&mut attrs);
|
2016-09-02 09:12:47 +00:00
|
|
|
attrs
|
|
|
|
});
|
|
|
|
(item, attr)
|
|
|
|
}
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
|
|
|
self.cfg.configure(node)
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:57:58 +12:00
|
|
|
// These are pretty nasty. Ideally, we would keep the tokens around, linked from
|
|
|
|
// the AST. However, we don't so we need to create new ones. Since the item might
|
|
|
|
// have come from a macro expansion (possibly only in part), we can't use the
|
|
|
|
// existing codemap.
|
|
|
|
//
|
|
|
|
// Therefore, we must use the pretty printer (yuck) to turn the AST node into a
|
|
|
|
// string, which we then re-tokenise (double yuck), but first we have to patch
|
|
|
|
// the pretty-printed string on to the end of the existing codemap (infinity-yuck).
|
|
|
|
fn tts_for_item(item: &Annotatable, parse_sess: &ParseSess) -> Vec<TokenTree> {
|
|
|
|
let text = match *item {
|
|
|
|
Annotatable::Item(ref i) => pprust::item_to_string(i),
|
|
|
|
Annotatable::TraitItem(ref ti) => pprust::trait_item_to_string(ti),
|
|
|
|
Annotatable::ImplItem(ref ii) => pprust::impl_item_to_string(ii),
|
|
|
|
};
|
|
|
|
string_to_tts(text, parse_sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tts_for_attr(attr: &ast::Attribute, parse_sess: &ParseSess) -> Vec<TokenTree> {
|
|
|
|
string_to_tts(pprust::attr_to_string(attr), parse_sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn string_to_tts(text: String, parse_sess: &ParseSess) -> Vec<TokenTree> {
|
|
|
|
let filemap = parse_sess.codemap()
|
|
|
|
.new_filemap(String::from("<macro expansion>"), None, text);
|
|
|
|
|
|
|
|
let lexer = lexer::StringReader::new(&parse_sess.span_diagnostic, filemap);
|
2016-10-27 06:36:56 +00:00
|
|
|
let mut parser = Parser::new(parse_sess, Box::new(lexer));
|
2016-09-06 17:57:58 +12:00
|
|
|
panictry!(parser.parse_all_token_trees())
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
|
2014-09-13 19:06:01 +03:00
|
|
|
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let mut expr = self.cfg.configure_expr(expr).unwrap();
|
|
|
|
expr.node = self.cfg.configure_expr_kind(expr.node);
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
2016-09-02 09:12:47 +00:00
|
|
|
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::Expr).make_expr()
|
2016-08-30 23:03:52 +00:00
|
|
|
} else {
|
|
|
|
P(noop_fold_expr(expr, self))
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2016-05-16 10:09:23 +00:00
|
|
|
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let mut expr = configure!(self, expr).unwrap();
|
|
|
|
expr.node = self.cfg.configure_expr_kind(expr.node);
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
2016-09-02 09:12:47 +00:00
|
|
|
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::OptExpr)
|
|
|
|
.make_opt_expr()
|
2016-08-30 23:03:52 +00:00
|
|
|
} else {
|
|
|
|
Some(P(noop_fold_expr(expr, self)))
|
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2016-08-30 23:03:52 +00:00
|
|
|
match pat.node {
|
|
|
|
PatKind::Mac(_) => {}
|
2016-09-01 07:01:45 +00:00
|
|
|
_ => return noop_fold_pat(pat, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
2014-05-19 13:59:35 -07:00
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
pat.and_then(|pat| match pat.node {
|
2016-09-02 09:12:47 +00:00
|
|
|
PatKind::Mac(mac) =>
|
|
|
|
self.collect_bang(mac, Vec::new(), pat.span, ExpansionKind::Pat).make_pat(),
|
2016-08-30 23:03:52 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2016-02-11 23:33:09 +03:00
|
|
|
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let stmt = match self.cfg.configure_stmt(stmt) {
|
|
|
|
Some(stmt) => stmt,
|
2016-11-02 22:33:35 -06:00
|
|
|
None => return SmallVector::new(),
|
2016-09-07 22:24:01 +00:00
|
|
|
};
|
|
|
|
|
2016-09-05 00:10:27 +00:00
|
|
|
let (mac, style, attrs) = if let StmtKind::Mac(mac) = stmt.node {
|
|
|
|
mac.unwrap()
|
|
|
|
} else {
|
|
|
|
// The placeholder expander gives ids to statements, so we avoid folding the id here.
|
|
|
|
let ast::Stmt { id, node, span } = stmt;
|
|
|
|
return noop_fold_stmt_kind(node, self).into_iter().map(|node| {
|
|
|
|
ast::Stmt { id: id, node: node, span: span }
|
|
|
|
}).collect()
|
2016-08-30 23:03:52 +00:00
|
|
|
};
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
let mut placeholder =
|
|
|
|
self.collect_bang(mac, attrs.into(), stmt.span, ExpansionKind::Stmts).make_stmts();
|
2016-08-30 23:03:52 +00:00
|
|
|
|
|
|
|
// If this is a macro invocation with a semicolon, then apply that
|
|
|
|
// semicolon to the final statement produced by expansion.
|
|
|
|
if style == MacStmtStyle::Semicolon {
|
2016-09-02 09:12:47 +00:00
|
|
|
if let Some(stmt) = placeholder.pop() {
|
|
|
|
placeholder.push(stmt.add_trailing_semicolon());
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
placeholder
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:34:51 -07:00
|
|
|
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
|
2016-11-05 04:16:26 +00:00
|
|
|
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
|
|
|
|
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
|
2016-09-01 06:44:54 +00:00
|
|
|
let result = noop_fold_block(block, self);
|
2016-11-05 04:16:26 +00:00
|
|
|
self.cx.current_expansion.directory_ownership = old_directory_ownership;
|
2016-03-01 09:28:42 +00:00
|
|
|
result
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item = configure!(self, item);
|
|
|
|
|
2016-09-23 07:23:01 +00:00
|
|
|
let (mut item, attr) = self.classify_item(item);
|
2016-08-30 23:03:52 +00:00
|
|
|
if let Some(attr) = attr {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item = Annotatable::Item(fully_configure!(self, item, noop_fold_item));
|
2016-09-02 09:12:47 +00:00
|
|
|
return self.collect_attr(attr, item, ExpansionKind::Items).make_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ast::ItemKind::Mac(..) => {
|
|
|
|
if match item.node {
|
|
|
|
ItemKind::Mac(ref mac) => mac.node.path.segments.is_empty(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
} {
|
|
|
|
return SmallVector::one(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
item.and_then(|item| match item.node {
|
|
|
|
ItemKind::Mac(mac) => {
|
2016-09-02 09:12:47 +00:00
|
|
|
self.collect(ExpansionKind::Items, InvocationKind::Bang {
|
2016-08-30 23:03:52 +00:00
|
|
|
mac: mac,
|
|
|
|
attrs: item.attrs,
|
|
|
|
ident: Some(item.ident),
|
|
|
|
span: item.span,
|
2016-09-02 09:12:47 +00:00
|
|
|
}).make_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
|
2016-09-06 01:45:23 +00:00
|
|
|
if item.ident == keywords::Invalid.ident() {
|
|
|
|
return noop_fold_item(item, self);
|
|
|
|
}
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
|
2016-09-07 23:21:59 +00:00
|
|
|
let mut module = (*self.cx.current_expansion.module).clone();
|
|
|
|
module.mod_path.push(item.ident);
|
2016-09-05 04:08:38 +00:00
|
|
|
|
|
|
|
// Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
|
|
|
|
// In the non-inline case, `inner` is never the dummy span (c.f. `parse_item_mod`).
|
|
|
|
// Thus, if `inner` is the dummy span, we know the module is inline.
|
|
|
|
let inline_module = item.span.contains(inner) || inner == syntax_pos::DUMMY_SP;
|
|
|
|
|
|
|
|
if inline_module {
|
2016-09-27 21:14:45 +00:00
|
|
|
if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, "path") {
|
2016-11-05 04:16:26 +00:00
|
|
|
self.cx.current_expansion.directory_ownership = DirectoryOwnership::Owned;
|
2016-11-16 10:52:37 +00:00
|
|
|
module.directory.push(&*path.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
} else {
|
|
|
|
module.directory.push(&*item.ident.name.as_str());
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
} else {
|
2016-11-05 04:16:26 +00:00
|
|
|
let mut path =
|
2016-09-05 04:08:38 +00:00
|
|
|
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
|
2016-11-05 04:16:26 +00:00
|
|
|
let directory_ownership = match path.file_name().unwrap().to_str() {
|
|
|
|
Some("mod.rs") => DirectoryOwnership::Owned,
|
2016-11-14 09:31:03 +00:00
|
|
|
_ => DirectoryOwnership::UnownedViaMod(false),
|
2016-11-05 04:16:26 +00:00
|
|
|
};
|
|
|
|
path.pop();
|
|
|
|
module.directory = path;
|
|
|
|
self.cx.current_expansion.directory_ownership = directory_ownership;
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
let orig_module =
|
|
|
|
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
2016-09-01 06:44:54 +00:00
|
|
|
let result = noop_fold_item(item, self);
|
2016-09-07 23:21:59 +00:00
|
|
|
self.cx.current_expansion.module = orig_module;
|
2016-11-05 04:16:26 +00:00
|
|
|
self.cx.current_expansion.directory_ownership = orig_directory_ownership;
|
2016-09-07 23:21:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-09-23 07:23:01 +00:00
|
|
|
// Ensure that test functions are accessible from the test harness.
|
|
|
|
ast::ItemKind::Fn(..) if self.cx.ecfg.should_test => {
|
|
|
|
if item.attrs.iter().any(|attr| is_test_or_bench(attr)) {
|
|
|
|
item = item.map(|mut item| { item.vis = ast::Visibility::Public; item });
|
|
|
|
}
|
|
|
|
noop_fold_item(item, self)
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
_ => noop_fold_item(item, self),
|
|
|
|
}
|
2014-12-02 10:07:41 -08:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item = configure!(self, item);
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
let (item, attr) = self.classify_item(item);
|
|
|
|
if let Some(attr) = attr {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item =
|
|
|
|
Annotatable::TraitItem(P(fully_configure!(self, item, noop_fold_trait_item)));
|
2016-09-02 09:12:47 +00:00
|
|
|
return self.collect_attr(attr, item, ExpansionKind::TraitItems).make_trait_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ast::TraitItemKind::Macro(mac) => {
|
|
|
|
let ast::TraitItem { attrs, span, .. } = item;
|
2016-09-02 09:12:47 +00:00
|
|
|
self.collect_bang(mac, attrs, span, ExpansionKind::TraitItems).make_trait_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
_ => fold::noop_fold_trait_item(item, self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item = configure!(self, item);
|
|
|
|
|
2016-08-30 23:03:52 +00:00
|
|
|
let (item, attr) = self.classify_item(item);
|
|
|
|
if let Some(attr) = attr {
|
2016-09-07 22:24:01 +00:00
|
|
|
let item = Annotatable::ImplItem(P(fully_configure!(self, item, noop_fold_impl_item)));
|
2016-09-02 09:12:47 +00:00
|
|
|
return self.collect_attr(attr, item, ExpansionKind::ImplItems).make_impl_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ast::ImplItemKind::Macro(mac) => {
|
|
|
|
let ast::ImplItem { attrs, span, .. } = item;
|
2016-09-02 09:12:47 +00:00
|
|
|
self.collect_bang(mac, attrs, span, ExpansionKind::ImplItems).make_impl_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
_ => fold::noop_fold_impl_item(item, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
}
|
2014-07-04 11:24:28 -07:00
|
|
|
}
|
|
|
|
|
2015-07-25 21:54:19 -07:00
|
|
|
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
|
2016-08-30 23:03:52 +00:00
|
|
|
let ty = match ty.node {
|
|
|
|
ast::TyKind::Mac(_) => ty.unwrap(),
|
|
|
|
_ => return fold::noop_fold_ty(ty, self),
|
|
|
|
};
|
|
|
|
|
|
|
|
match ty.node {
|
2016-09-02 09:12:47 +00:00
|
|
|
ast::TyKind::Mac(mac) =>
|
|
|
|
self.collect_bang(mac, Vec::new(), ty.span, ExpansionKind::Ty).make_ty(),
|
2016-08-30 23:03:52 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2015-07-25 21:54:19 -07:00
|
|
|
}
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
|
|
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
|
|
|
|
noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
|
|
|
|
noop_fold_item_kind(self.cfg.configure_item_kind(item), self)
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
|
|
|
|
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
|
2016-09-06 05:42:45 +00:00
|
|
|
if self.monotonic {
|
|
|
|
assert_eq!(id, ast::DUMMY_NODE_ID);
|
|
|
|
self.cx.resolver.next_node_id()
|
|
|
|
} else {
|
|
|
|
id
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
}
|
2013-07-16 15:05:50 +10:00
|
|
|
}
|
|
|
|
|
2015-02-15 21:30:45 +01:00
|
|
|
pub struct ExpansionConfig<'feat> {
|
2014-06-06 13:21:18 -07:00
|
|
|
pub crate_name: String,
|
2015-02-15 21:30:45 +01:00
|
|
|
pub features: Option<&'feat Features>,
|
2015-01-17 23:33:05 +00:00
|
|
|
pub recursion_limit: usize,
|
2015-04-14 15:36:38 +02:00
|
|
|
pub trace_mac: bool,
|
2016-06-01 01:27:12 +00:00
|
|
|
pub should_test: bool, // If false, strip `#[test]` nodes
|
2016-09-12 09:47:54 +00:00
|
|
|
pub single_step: bool,
|
|
|
|
pub keep_macs: bool,
|
2014-09-26 17:14:23 -07:00
|
|
|
}
|
|
|
|
|
2015-03-06 12:56:28 -08:00
|
|
|
macro_rules! feature_tests {
|
|
|
|
($( fn $getter:ident = $field:ident, )*) => {
|
|
|
|
$(
|
|
|
|
pub fn $getter(&self) -> bool {
|
|
|
|
match self.features {
|
|
|
|
Some(&Features { $field: true, .. }) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:30:45 +01:00
|
|
|
impl<'feat> ExpansionConfig<'feat> {
|
|
|
|
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
|
2014-09-26 17:14:23 -07:00
|
|
|
ExpansionConfig {
|
|
|
|
crate_name: crate_name,
|
2015-02-15 21:30:45 +01:00
|
|
|
features: None,
|
2014-10-01 11:38:54 +02:00
|
|
|
recursion_limit: 64,
|
2015-04-14 15:36:38 +02:00
|
|
|
trace_mac: false,
|
2016-06-01 01:27:12 +00:00
|
|
|
should_test: false,
|
2016-09-12 09:47:54 +00:00
|
|
|
single_step: false,
|
|
|
|
keep_macs: false,
|
2014-09-26 17:14:23 -07:00
|
|
|
}
|
|
|
|
}
|
2015-02-15 21:30:45 +01:00
|
|
|
|
2015-03-06 12:56:28 -08:00
|
|
|
feature_tests! {
|
2016-04-04 17:08:41 +02:00
|
|
|
fn enable_quotes = quote,
|
|
|
|
fn enable_asm = asm,
|
|
|
|
fn enable_log_syntax = log_syntax,
|
|
|
|
fn enable_concat_idents = concat_idents,
|
|
|
|
fn enable_trace_macros = trace_macros,
|
2015-03-06 12:56:28 -08:00
|
|
|
fn enable_allow_internal_unstable = allow_internal_unstable,
|
2016-04-04 17:08:41 +02:00
|
|
|
fn enable_custom_derive = custom_derive,
|
|
|
|
fn enable_pushpop_unsafe = pushpop_unsafe,
|
2016-10-03 09:49:39 -07:00
|
|
|
fn enable_proc_macro = proc_macro,
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 14:09:28 +11:00
|
|
|
}
|
2014-02-28 23:17:38 -08:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:20:09 +00:00
|
|
|
// A Marker adds the given mark to the syntax context and
|
|
|
|
// sets spans' `expn_id` to the given expn_id (unless it is `None`).
|
2016-06-22 08:03:42 +00:00
|
|
|
struct Marker { mark: Mark, expn_id: Option<ExpnId> }
|
2013-07-09 15:56:21 -07:00
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
impl Folder for Marker {
|
2016-06-22 08:03:42 +00:00
|
|
|
fn fold_ident(&mut self, mut ident: Ident) -> Ident {
|
|
|
|
ident.ctxt = ident.ctxt.apply_mark(self.mark);
|
|
|
|
ident
|
|
|
|
}
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
noop_fold_mac(mac, self)
|
2016-05-17 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_span(&mut self, mut span: Span) -> Span {
|
|
|
|
if let Some(expn_id) = self.expn_id {
|
|
|
|
span.expn_id = expn_id;
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2016-05-17 21:20:09 +00:00
|
|
|
span
|
2013-07-09 15:56:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 05:59:08 -07:00
|
|
|
// apply a given mark to the given token trees. Used prior to expansion of a macro.
|
2016-10-16 03:39:52 +00:00
|
|
|
pub fn mark_tts(tts: &[TokenTree], m: Mark) -> Vec<TokenTree> {
|
2016-05-17 21:20:09 +00:00
|
|
|
noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
|
2013-06-29 05:59:08 -07:00
|
|
|
}
|