Skip to content

Add decorator syntax extensions on trait and impl items #25024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/librustc/plugin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use lint::{LintPassObject, LintId, Lint};
use session::Session;

use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
use syntax::ext::base::{IdentTT, Decorator, Modifier, MultiModifier, MacroRulesTT};
use syntax::ext::base::MacroExpanderFn;
use syntax::ext::base::{IdentTT, Decorator, Modifier, MultiModifier, MultiDecorator};
use syntax::ext::base::{MacroExpanderFn, MacroRulesTT};
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::ptr::P;
Expand Down Expand Up @@ -84,6 +84,7 @@ impl<'a> Registry<'a> {
/// Register a syntax extension of any kind.
///
/// This is the most general hook into `libsyntax`'s expansion behavior.
#[allow(deprecated)]
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
self.syntax_exts.push((name, match extension {
NormalTT(ext, _, allow_internal_unstable) => {
Expand All @@ -93,6 +94,7 @@ impl<'a> Registry<'a> {
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
}
Decorator(ext) => Decorator(ext),
MultiDecorator(ext) => MultiDecorator(ext),
Modifier(ext) => Modifier(ext),
MultiModifier(ext) => MultiModifier(ext),
MacroRulesTT => {
Expand Down
58 changes: 56 additions & 2 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::collections::HashMap;
use std::rc::Rc;
use std::default::Default;

#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
pub trait ItemDecorator {
fn expand(&self,
ecx: &mut ExtCtxt,
Expand All @@ -39,6 +41,9 @@ pub trait ItemDecorator {
push: &mut FnMut(P<ast::Item>));
}

#[allow(deprecated)]
#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
impl<F> ItemDecorator for F
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, &mut FnMut(P<ast::Item>))
{
Expand All @@ -52,6 +57,8 @@ impl<F> ItemDecorator for F
}
}

#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
pub trait ItemModifier {
fn expand(&self,
ecx: &mut ExtCtxt,
Expand All @@ -61,9 +68,13 @@ pub trait ItemModifier {
-> P<ast::Item>;
}

#[allow(deprecated)]
#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
impl<F> ItemModifier for F
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
{

fn expand(&self,
ecx: &mut ExtCtxt,
span: Span,
Expand Down Expand Up @@ -112,6 +123,16 @@ impl Annotatable {
}
}

pub fn map_item_or<F, G>(self, mut f: F, mut or: G) -> Annotatable
where F: FnMut(P<ast::Item>) -> P<ast::Item>,
G: FnMut(Annotatable) -> Annotatable
{
match self {
Annotatable::Item(i) => Annotatable::Item(f(i)),
_ => or(self)
}
}

pub fn expect_trait_item(self) -> P<ast::TraitItem> {
match self {
Annotatable::TraitItem(i) => i,
Expand All @@ -127,6 +148,29 @@ impl Annotatable {
}
}

// A more flexible ItemDecorator.
pub trait MultiItemDecorator {
fn expand(&self,
ecx: &mut ExtCtxt,
sp: Span,
meta_item: &ast::MetaItem,
item: Annotatable,
push: &mut FnMut(Annotatable));
}

impl<F> MultiItemDecorator for F
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, Annotatable, &mut FnMut(Annotatable))
{
fn expand(&self,
ecx: &mut ExtCtxt,
sp: Span,
meta_item: &ast::MetaItem,
item: Annotatable,
push: &mut FnMut(Annotatable)) {
(*self)(ecx, sp, meta_item, item, push)
}
}

// A more flexible ItemModifier (ItemModifier should go away, eventually, FIXME).
// meta_item is the annotation, item is the item being modified, parent_item
// is the impl or trait item is declared in if item is part of such a thing.
Expand Down Expand Up @@ -397,12 +441,22 @@ impl MacResult for DummyResult {
pub enum SyntaxExtension {
/// A syntax extension that is attached to an item and creates new items
/// based upon it.
///
/// `#[derive(...)]` is an `ItemDecorator`.
#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiDecorator")]
#[allow(deprecated)]
Decorator(Box<ItemDecorator + 'static>),

/// A syntax extension that is attached to an item and creates new items
/// based upon it.
///
/// `#[derive(...)]` is a `MultiItemDecorator`.
MultiDecorator(Box<MultiItemDecorator + 'static>),

/// A syntax extension that is attached to an item and modifies it
/// in-place.
#[unstable(feature = "rustc_private")]
#[deprecated(since = "1.0.0", reason = "replaced by MultiModifier")]
#[allow(deprecated)]
Modifier(Box<ItemModifier + 'static>),

/// A syntax extension that is attached to an item and modifies it
Expand Down
15 changes: 7 additions & 8 deletions src/libsyntax/ext/deriving/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{MetaItem, Item};
use ast::MetaItem;
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
use ptr::P;

pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
span: Span,
_: &MetaItem,
_: &Item,
_: &mut FnMut(P<Item>))
_: Annotatable,
_: &mut FnMut(Annotatable))
{
cx.span_err(span, "this unsafe trait should be implemented explicitly");
}

pub fn expand_deriving_copy(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
let path = Path::new(vec![
if cx.use_std { "std" } else { "core" },
Expand All @@ -46,5 +45,5 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push);
trait_def.expand(cx, mitem, &item, push);
}
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{MetaItem, Item, Expr};
use ast::{MetaItem, Expr};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -20,8 +20,8 @@ use ptr::P;
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
let inline = cx.meta_word(span, InternedString::new("inline"));
let attrs = vec!(cx.attribute(span, inline));
Expand All @@ -47,7 +47,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}

fn cs_clone(
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{MetaItem, Item, Expr};
use ast::{MetaItem, Expr};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -20,8 +20,8 @@ use ptr::P;
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
cs_same_method(
Expand Down Expand Up @@ -66,5 +66,5 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

use ast;
use ast::{MetaItem, Item, Expr};
use ast::{MetaItem, Expr};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -21,8 +21,8 @@ use ptr::P;
pub fn expand_deriving_ord(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
let inline = cx.meta_word(span, InternedString::new("inline"));
let attrs = vec!(cx.attribute(span, inline));
Expand All @@ -48,7 +48,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}


Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{MetaItem, Item, Expr, self};
use ast::{MetaItem, Expr, self};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -20,8 +20,8 @@ use ptr::P;
pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
// structures are equal if all fields are equal, and non equal, if
// any fields are not equal or if the enum variants are different
Expand Down Expand Up @@ -90,5 +90,5 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
pub use self::OrderingOp::*;

use ast;
use ast::{MetaItem, Item, Expr};
use ast::{MetaItem, Expr};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -23,8 +23,8 @@ use ptr::P;
pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
macro_rules! md {
($name:expr, $op:expr, $equal:expr) => { {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}

#[derive(Copy, Clone)]
Expand Down
18 changes: 9 additions & 9 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! The compiler code necessary for `#[derive(Decodable)]`. See encodable.rs for more.

use ast;
use ast::{MetaItem, Item, Expr, MutMutable};
use ast::{MetaItem, Expr, MutMutable};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
use ext::deriving::generic::ty::*;
Expand All @@ -24,26 +24,26 @@ use ptr::P;
pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize")
}

pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>))
item: Annotatable,
push: &mut FnMut(Annotatable))
{
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
}

fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
push: &mut FnMut(P<Item>),
item: Annotatable,
push: &mut FnMut(Annotatable),
krate: &'static str)
{
if !cx.use_std {
Expand Down Expand Up @@ -87,7 +87,7 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, &item, push)
}

fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
Expand Down
Loading