expansion: Give names to some fields of SyntaxExtension

This commit is contained in:
Vadim Petrochenkov 2018-06-24 19:24:51 +03:00
parent 1328bdeef8
commit 09856c85b7
7 changed files with 44 additions and 34 deletions

View File

@ -569,9 +569,11 @@ impl<'a> CrateLoader<'a> {
fn register_bang_proc_macro(&mut self,
name: &str,
expand: fn(TokenStream) -> TokenStream) {
let expand = SyntaxExtension::ProcMacro(
Box::new(BangProcMacro { inner: expand }), false, self.edition
);
let expand = SyntaxExtension::ProcMacro {
expander: Box::new(BangProcMacro { inner: expand }),
allow_internal_unstable: false,
edition: self.edition,
};
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
}
}

View File

@ -518,8 +518,11 @@ impl CrateStore for cstore::CStore {
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
} else if data.name == "proc_macro" &&
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
true, data.root.edition);
let ext = SyntaxExtension::ProcMacro {
expander: Box::new(::proc_macro::__internal::Quoter),
allow_internal_unstable: true,
edition: data.root.edition,
};
return LoadedMacro::ProcMacro(Lrc::new(ext));
}

View File

@ -350,8 +350,8 @@ impl<'a> base::Resolver for Resolver<'a> {
fn check_unused_macros(&self) {
for did in self.unused_macros.iter() {
let id_span = match *self.macro_map[did] {
SyntaxExtension::NormalTT { def_info, .. } => def_info,
SyntaxExtension::DeclMacro(.., osp, _) => osp,
SyntaxExtension::NormalTT { def_info, .. } |
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
_ => None,
};
if let Some((id, span)) = id_span {
@ -848,8 +848,6 @@ impl<'a> Resolver<'a> {
/// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
binding: &NameBinding<'a>) {
use self::SyntaxExtension::*;
let krate = binding.def().def_id().krate;
// Plugin-based syntax extensions are exempt from this check
@ -859,15 +857,16 @@ impl<'a> Resolver<'a> {
match *ext {
// If `ext` is a procedural macro, check if we've already warned about it
AttrProcMacro(..) | ProcMacro(..) =>
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
if !self.warned_proc_macros.insert(name) { return; },
_ => return,
}
let warn_msg = match *ext {
AttrProcMacro(..) => "attribute procedural macros cannot be \
imported with `#[macro_use]`",
ProcMacro(..) => "procedural macros cannot be imported with `#[macro_use]`",
SyntaxExtension::AttrProcMacro(..) =>
"attribute procedural macros cannot be imported with `#[macro_use]`",
SyntaxExtension::ProcMacro { .. } =>
"procedural macros cannot be imported with `#[macro_use]`",
_ => return,
};

View File

@ -1219,7 +1219,7 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
let res = resolver
.resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
if let Ok(def) = res {
if let SyntaxExtension::DeclMacro(..) = *resolver.get_macro(def) {
if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(def) {
Some(def)
} else {
None

View File

@ -597,11 +597,11 @@ pub enum SyntaxExtension {
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),
/// A function-like procedural macro. TokenStream -> TokenStream.
ProcMacro(
/* expander: */ Box<ProcMacro + sync::Sync + sync::Send>,
/* allow_internal_unstable: */ bool,
/* edition: */ Edition,
),
ProcMacro {
expander: Box<ProcMacro + sync::Sync + sync::Send>,
allow_internal_unstable: bool,
edition: Edition,
},
/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream.
/// The first TokenSteam is the attribute, the second is the annotated item.
@ -646,19 +646,21 @@ pub enum SyntaxExtension {
BuiltinDerive(BuiltinDeriveFn),
/// A declarative macro, e.g. `macro m() {}`.
///
/// The second element is the definition site span.
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>, Edition),
DeclMacro {
expander: Box<TTMacroExpander + sync::Sync + sync::Send>,
def_info: Option<(ast::NodeId, Span)>,
edition: Edition,
}
}
impl SyntaxExtension {
/// Return which kind of macro calls this syntax extension.
pub fn kind(&self) -> MacroKind {
match *self {
SyntaxExtension::DeclMacro(..) |
SyntaxExtension::DeclMacro { .. } |
SyntaxExtension::NormalTT { .. } |
SyntaxExtension::IdentTT(..) |
SyntaxExtension::ProcMacro(..) =>
SyntaxExtension::ProcMacro { .. } =>
MacroKind::Bang,
SyntaxExtension::MultiDecorator(..) |
SyntaxExtension::MultiModifier(..) |
@ -672,8 +674,8 @@ impl SyntaxExtension {
pub fn is_modern(&self) -> bool {
match *self {
SyntaxExtension::DeclMacro(..) |
SyntaxExtension::ProcMacro(..) |
SyntaxExtension::DeclMacro { .. } |
SyntaxExtension::ProcMacro { .. } |
SyntaxExtension::AttrProcMacro(..) |
SyntaxExtension::ProcMacroDerive(..) => true,
_ => false,
@ -683,8 +685,8 @@ impl SyntaxExtension {
pub fn edition(&self) -> Edition {
match *self {
SyntaxExtension::NormalTT { edition, .. } |
SyntaxExtension::DeclMacro(.., edition) |
SyntaxExtension::ProcMacro(.., edition) |
SyntaxExtension::DeclMacro { edition, .. } |
SyntaxExtension::ProcMacro { edition, .. } |
SyntaxExtension::AttrProcMacro(.., edition) |
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
// Unstable legacy stuff

View File

@ -738,13 +738,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
};
let opt_expanded = match *ext {
DeclMacro(ref expand, def_span, edition) => {
if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s),
DeclMacro { ref expander, def_info, edition } => {
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
false, false, false, None,
edition) {
dummy_span
} else {
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
}
}
@ -804,7 +804,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
kind.dummy(span)
}
ProcMacro(ref expandfun, allow_internal_unstable, edition) => {
SyntaxExtension::ProcMacro { ref expander, allow_internal_unstable, edition } => {
if ident.name != keywords::Invalid.name() {
let msg =
format!("macro {}! expects no ident argument, given '{}'", path, ident);
@ -826,7 +826,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
edition,
});
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
let tok_result = expander.expand(self.cx, span, mac.node.stream());
let result = self.parse_ast_fragment(tok_result, kind, path, span);
self.gate_proc_macro_expansion(span, &result);
result

View File

@ -312,7 +312,11 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
edition,
}
} else {
SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)), edition)
SyntaxExtension::DeclMacro {
expander,
def_info: Some((def.id, def.span)),
edition,
}
}
}