1 // Copyright 2015 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.
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.
13 #![feature(plugin_registrar)]
14 #![feature(box_syntax)]
15 #![feature(rustc_private)]
21 use syntax
::codemap
::Span
;
22 use syntax
::ext
::base
::{MultiDecorator, ExtCtxt, Annotatable}
;
23 use syntax
::ext
::build
::AstBuilder
;
24 use syntax
::ext
::deriving
::generic
::{cs_fold, TraitDef, MethodDef, combine_substructure}
;
25 use syntax
::ext
::deriving
::generic
::ty
::{Literal, LifetimeBounds, Path, borrowed_explicit_self}
;
26 use syntax
::parse
::token
;
28 use rustc
::plugin
::Registry
;
31 pub fn plugin_registrar(reg
: &mut Registry
) {
32 reg
.register_syntax_extension(
33 token
::intern("derive_TotalSum"),
34 MultiDecorator(box expand
));
37 fn expand(cx
: &mut ExtCtxt
,
39 mitem
: &ast
::MetaItem
,
41 push
: &mut FnMut(Annotatable
)) {
42 let trait_def
= TraitDef
{
45 path
: Path
::new(vec
!["TotalSum"]),
46 additional_bounds
: vec
![],
47 generics
: LifetimeBounds
::empty(),
48 associated_types
: vec
![],
52 generics
: LifetimeBounds
::empty(),
53 explicit_self
: borrowed_explicit_self(),
55 ret_ty
: Literal(Path
::new_local("isize")),
57 combine_substructure
: combine_substructure(box |cx
, span
, substr
| {
58 let zero
= cx
.expr_isize(span
, 0);
60 |cx
, span
, subexpr
, field
, _
| {
61 cx
.expr_binary(span
, ast
::BiAdd
, subexpr
,
62 cx
.expr_method_call(span
, field
,
63 token
::str_to_ident("total_sum"), vec
![]))
66 box |cx
, span
, _
, _
| { cx.span_bug(span, "wtf??"); }
,
73 trait_def
.expand(cx
, mitem
, &item
, push
)