//
/// The source of expansion.
-#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
+#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum ExpnFormat {
/// e.g. #[derive(...)] <item>
- MacroAttribute,
+ MacroAttribute(String),
/// e.g. `format!()`
- MacroBang,
+ MacroBang(String),
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
- CompilerExpansion,
+ CompilerExpansion(CompilerExpansionFormat),
}
+#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
+pub enum CompilerExpansionFormat {
+ IfLet,
+ PlacementIn,
+ WhileLet,
+ ForLoop,
+ Closure,
+}
+
+impl CompilerExpansionFormat {
+ pub fn name(self) -> &'static str {
+ match self {
+ CompilerExpansionFormat::IfLet => "if let expansion",
+ CompilerExpansionFormat::PlacementIn => "placement-in expansion",
+ CompilerExpansionFormat::WhileLet => "while let expansion",
+ CompilerExpansionFormat::ForLoop => "for loop expansion",
+ CompilerExpansionFormat::Closure => "closure expansion",
+ }
+ }
+}
#[derive(Clone, Hash, Debug)]
pub struct NameAndSpan {
- /// The name of the macro that was invoked to create the thing
- /// with this Span.
- pub name: String,
/// The format with which the macro was invoked.
pub format: ExpnFormat,
/// Whether the macro is allowed to use #[unstable]/feature-gated
pub span: Option<Span>
}
+impl NameAndSpan {
+ pub fn name(&self) -> &str{
+ match self.format {
+ ExpnFormat::MacroAttribute(ref s) => &s,
+ ExpnFormat::MacroBang(ref s) => &s,
+ ExpnFormat::CompilerExpansion(ce) => ce.name(),
+ }
+ }
+}
+
/// Extra information for tracking spans of macro and syntax sugar expansion
#[derive(Hash, Debug)]
pub struct ExpnInfo {
use attr;
use attr::AttrMetaMethods;
use codemap;
-use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, CompilerExpansion};
+use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
+use codemap::{CompilerExpansion, CompilerExpansionFormat};
use ext::base::*;
use feature_gate::{self, Features, GatedCfg};
use fold;
}
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
- fn push_compiler_expansion(fld: &mut MacroExpander, span: Span, expansion_desc: &str) {
+ fn push_compiler_expansion(fld: &mut MacroExpander, span: Span,
+ expansion_type: CompilerExpansionFormat) {
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
- name: expansion_desc.to_string(),
- format: CompilerExpansion,
+ format: CompilerExpansion(expansion_type),
// This does *not* mean code generated after
// `push_compiler_expansion` is automatically exempt
&fld.cx.parse_sess.span_diagnostic,
expr_span);
- push_compiler_expansion(fld, expr_span, "placement-in expansion");
+ push_compiler_expansion(fld, expr_span, CompilerExpansionFormat::PlacementIn);
let value_span = value_expr.span;
let placer_span = placer.span;
// }
// }
- push_compiler_expansion(fld, span, "while let expansion");
+ push_compiler_expansion(fld, span, CompilerExpansionFormat::WhileLet);
// `<pat> => <body>`
let pat_arm = {
// _ => [<elseopt> | ()]
// }
- push_compiler_expansion(fld, span, "if let expansion");
+ push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
// `<pat> => <body>`
let pat_arm = {
ast::ExprIf(cond, blk, elseopt) => {
let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
ast::ExprIfLet(..) => {
- push_compiler_expansion(fld, span, "if let expansion");
+ push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
// wrap the if-let expr in a block
let span = els.span;
let blk = P(ast::Block {
// result
// }
- push_compiler_expansion(fld, span, "for loop expansion");
+ push_compiler_expansion(fld, span, CompilerExpansionFormat::ForLoop);
let span = fld.new_span(span);
}
ast::ExprClosure(capture_clause, fn_decl, block) => {
- push_compiler_expansion(fld, span, "closure expansion");
+ push_compiler_expansion(fld, span, CompilerExpansionFormat::Closure);
let (rewritten_fn_decl, rewritten_block)
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
let new_node = ast::ExprClosure(capture_clause,
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
- name: extname.to_string(),
- format: MacroBang,
+ format: MacroBang(extname.to_string()),
span: exp_span,
allow_internal_unstable: allow_internal_unstable,
},
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
- name: extname.to_string(),
- format: MacroBang,
+ format: MacroBang(extname.to_string()),
span: span,
allow_internal_unstable: allow_internal_unstable,
}
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
- name: extname.to_string(),
- format: MacroBang,
+ format: MacroBang(extname.to_string()),
span: span,
allow_internal_unstable: allow_internal_unstable,
}
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
- name: extname.to_string(),
- format: MacroBang,
+ format: MacroBang(extname.to_string()),
span: None,
// `macro_rules!` doesn't directly allow
// unstable (this is orthogonal to whether
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
- name: extname.to_string(),
- format: MacroBang,
+ format: MacroBang(extname.to_string()),
span: tt_span,
allow_internal_unstable: allow_internal_unstable,
}
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
- name: mname.to_string(),
- format: MacroAttribute,
+ format: MacroAttribute(mname.to_string()),
span: Some(attr.span),
// attributes can do whatever they like,
// for now.
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
- name: mname.to_string(),
- format: MacroAttribute,
+ format: MacroAttribute(mname.to_string()),
span: Some(attr.span),
// attributes can do whatever they like,
// for now.
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
- name: mname.to_string(),
- format: MacroAttribute,
+ format: MacroAttribute(mname.to_string()),
span: Some(attr.span),
// attributes can do whatever they like,
// for now
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
- name: mname.to_string(),
- format: MacroAttribute,
+ format: MacroAttribute(mname.to_string()),
span: Some(attr.span),
// attributes can do whatever they like,
// for now