]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/custom_derive_plugin.rs
Rollup merge of #28991 - goyox86:goyox86/rustfmting-liblog-II, r=alexcrichton
[rust.git] / src / test / auxiliary / custom_derive_plugin.rs
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
17 extern crate syntax;
18 extern crate rustc;
19
20 use syntax::ast;
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;
27 use syntax::ptr::P;
28 use rustc::plugin::Registry;
29
30 #[plugin_registrar]
31 pub fn plugin_registrar(reg: &mut Registry) {
32     reg.register_syntax_extension(
33         token::intern("derive_TotalSum"),
34         MultiDecorator(box expand));
35 }
36
37 fn expand(cx: &mut ExtCtxt,
38           span: Span,
39           mitem: &ast::MetaItem,
40           item: &Annotatable,
41           push: &mut FnMut(Annotatable)) {
42     let trait_def = TraitDef {
43         span: span,
44         attributes: vec![],
45         path: Path::new(vec!["TotalSum"]),
46         additional_bounds: vec![],
47         generics: LifetimeBounds::empty(),
48         associated_types: vec![],
49         is_unsafe: false,
50         methods: vec![
51             MethodDef {
52                 name: "total_sum",
53                 generics: LifetimeBounds::empty(),
54                 explicit_self: borrowed_explicit_self(),
55                 args: vec![],
56                 ret_ty: Literal(Path::new_local("isize")),
57                 attributes: vec![],
58                 is_unsafe: false,
59                 combine_substructure: combine_substructure(box |cx, span, substr| {
60                     let zero = cx.expr_isize(span, 0);
61                     cs_fold(false,
62                             |cx, span, subexpr, field, _| {
63                                 cx.expr_binary(span, ast::BiAdd, subexpr,
64                                     cx.expr_method_call(span, field,
65                                         token::str_to_ident("total_sum"), vec![]))
66                             },
67                             zero,
68                             box |cx, span, _, _| { cx.span_bug(span, "wtf??"); },
69                             cx, span, substr)
70                 }),
71             },
72         ],
73     };
74
75     trait_def.expand(cx, mitem, item, push)
76 }