]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / test / run-pass-fulldeps / auxiliary / custom_derive_plugin.rs
CommitLineData
c34b1796
AL
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.
4//
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.
10
11// force-host
12
13#![feature(plugin_registrar)]
14#![feature(box_syntax)]
15#![feature(rustc_private)]
16
17extern crate syntax;
9cc50fc6 18extern crate syntax_ext;
3157f602 19extern crate syntax_pos;
c34b1796 20extern crate rustc;
92a42be0 21extern crate rustc_plugin;
c34b1796
AL
22
23use syntax::ast;
d9579d0f 24use syntax::ext::base::{MultiDecorator, ExtCtxt, Annotatable};
c34b1796 25use syntax::ext::build::AstBuilder;
c34b1796 26use syntax::parse::token;
9cc50fc6
SL
27use syntax_ext::deriving::generic::{cs_fold, TraitDef, MethodDef, combine_substructure};
28use syntax_ext::deriving::generic::ty::{Literal, LifetimeBounds, Path, borrowed_explicit_self};
3157f602 29use syntax_pos::Span;
92a42be0 30use rustc_plugin::Registry;
c34b1796
AL
31
32#[plugin_registrar]
33pub fn plugin_registrar(reg: &mut Registry) {
34 reg.register_syntax_extension(
35 token::intern("derive_TotalSum"),
d9579d0f 36 MultiDecorator(box expand));
c34b1796
AL
37}
38
39fn expand(cx: &mut ExtCtxt,
40 span: Span,
41 mitem: &ast::MetaItem,
62682a34 42 item: &Annotatable,
d9579d0f 43 push: &mut FnMut(Annotatable)) {
c34b1796
AL
44 let trait_def = TraitDef {
45 span: span,
46 attributes: vec![],
47 path: Path::new(vec!["TotalSum"]),
48 additional_bounds: vec![],
49 generics: LifetimeBounds::empty(),
50 associated_types: vec![],
e9174d1e 51 is_unsafe: false,
9e0c209e 52 supports_unions: false,
c34b1796
AL
53 methods: vec![
54 MethodDef {
55 name: "total_sum",
56 generics: LifetimeBounds::empty(),
57 explicit_self: borrowed_explicit_self(),
58 args: vec![],
59 ret_ty: Literal(Path::new_local("isize")),
60 attributes: vec![],
62682a34 61 is_unsafe: false,
a7813a04 62 unify_fieldless_variants: true,
c34b1796 63 combine_substructure: combine_substructure(box |cx, span, substr| {
d9579d0f 64 let zero = cx.expr_isize(span, 0);
c34b1796
AL
65 cs_fold(false,
66 |cx, span, subexpr, field, _| {
7453a54e 67 cx.expr_binary(span, ast::BinOpKind::Add, subexpr,
c34b1796
AL
68 cx.expr_method_call(span, field,
69 token::str_to_ident("total_sum"), vec![]))
70 },
71 zero,
72 box |cx, span, _, _| { cx.span_bug(span, "wtf??"); },
73 cx, span, substr)
74 }),
75 },
76 ],
77 };
78
62682a34 79 trait_def.expand(cx, mitem, item, push)
c34b1796 80}