]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/custom_derive_plugin_attr.rs
Rollup merge of #28991 - goyox86:goyox86/rustfmting-liblog-II, r=alexcrichton
[rust.git] / src / test / auxiliary / custom_derive_plugin_attr.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::attr::AttrMetaMethods;
22 use syntax::codemap::Span;
23 use syntax::ext::base::{MultiDecorator, ExtCtxt, Annotatable};
24 use syntax::ext::build::AstBuilder;
25 use syntax::ext::deriving::generic::{cs_fold, TraitDef, MethodDef, combine_substructure};
26 use syntax::ext::deriving::generic::{Substructure, Struct, EnumMatching};
27 use syntax::ext::deriving::generic::ty::{Literal, LifetimeBounds, Path, borrowed_explicit_self};
28 use syntax::parse::token;
29 use syntax::ptr::P;
30 use rustc::plugin::Registry;
31
32 #[plugin_registrar]
33 pub fn plugin_registrar(reg: &mut Registry) {
34     reg.register_syntax_extension(
35         token::intern("derive_TotalSum"),
36         MultiDecorator(box expand));
37 }
38
39 fn expand(cx: &mut ExtCtxt,
40           span: Span,
41           mitem: &ast::MetaItem,
42           item: &Annotatable,
43           push: &mut FnMut(Annotatable)) {
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![],
51         is_unsafe: false,
52         methods: vec![
53             MethodDef {
54                 name: "total_sum",
55                 generics: LifetimeBounds::empty(),
56                 explicit_self: borrowed_explicit_self(),
57                 args: vec![],
58                 ret_ty: Literal(Path::new_local("isize")),
59                 attributes: vec![],
60                 is_unsafe: false,
61                 combine_substructure: combine_substructure(Box::new(totalsum_substructure)),
62             },
63         ],
64     };
65
66     trait_def.expand(cx, mitem, item, push)
67 }
68
69 // Mostly copied from syntax::ext::deriving::hash
70 /// Defines how the implementation for `trace()` is to be generated
71 fn totalsum_substructure(cx: &mut ExtCtxt, trait_span: Span,
72                          substr: &Substructure) -> P<ast::Expr> {
73     let fields = match *substr.fields {
74         Struct(ref fs) | EnumMatching(_, _, ref fs) => fs,
75         _ => cx.span_bug(trait_span, "impossible substructure")
76     };
77
78     fields.iter().fold(cx.expr_isize(trait_span, 0), |acc, ref item| {
79         if item.attrs.iter().find(|a| a.check_name("ignore")).is_some() {
80             acc
81         } else {
82             cx.expr_binary(item.span, ast::BiAdd, acc,
83                            cx.expr_method_call(item.span,
84                                                item.self_.clone(),
85                                                substr.method_ident,
86                                                Vec::new()))
87         }
88     })
89 }