]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/zero.rs
auto merge of #12101 : csherratt/rust/master, r=alexcrichton
[rust.git] / src / libsyntax / ext / deriving / zero.rs
1 // Copyright 2012-2013 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 use ast::{MetaItem, Item, Expr};
12 use codemap::Span;
13 use ext::base::ExtCtxt;
14 use ext::build::AstBuilder;
15 use ext::deriving::generic::*;
16
17 pub fn expand_deriving_zero(cx: &mut ExtCtxt,
18                             span: Span,
19                             mitem: @MetaItem,
20                             in_items: ~[@Item])
21     -> ~[@Item] {
22     let trait_def = TraitDef {
23         cx: cx, span: span,
24
25         path: Path::new(~["std", "num", "Zero"]),
26         additional_bounds: ~[],
27         generics: LifetimeBounds::empty(),
28         methods: ~[
29             MethodDef {
30                 name: "zero",
31                 generics: LifetimeBounds::empty(),
32                 explicit_self: None,
33                 args: ~[],
34                 ret_ty: Self,
35                 inline: true,
36                 const_nonmatching: false,
37                 combine_substructure: zero_substructure
38             },
39             MethodDef {
40                 name: "is_zero",
41                 generics: LifetimeBounds::empty(),
42                 explicit_self: borrowed_explicit_self(),
43                 args: ~[],
44                 ret_ty: Literal(Path::new(~["bool"])),
45                 inline: true,
46                 const_nonmatching: false,
47                 combine_substructure: |cx, span, substr| {
48                     cs_and(|cx, span, _, _| cx.span_bug(span,
49                                                         "Non-matching enum \
50                                                          variant in \
51                                                          deriving(Zero)"),
52                            cx, span, substr)
53                 }
54             }
55         ]
56     };
57     trait_def.expand(mitem, in_items)
58 }
59
60 fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
61     let zero_ident = ~[
62         cx.ident_of("std"),
63         cx.ident_of("num"),
64         cx.ident_of("Zero"),
65         cx.ident_of("zero")
66     ];
67     let zero_call = |span| cx.expr_call_global(span, zero_ident.clone(), ~[]);
68
69     return match *substr.fields {
70         StaticStruct(_, ref summary) => {
71             match *summary {
72                 Unnamed(ref fields) => {
73                     if fields.is_empty() {
74                         cx.expr_ident(trait_span, substr.type_ident)
75                     } else {
76                         let exprs = fields.map(|sp| zero_call(*sp));
77                         cx.expr_call_ident(trait_span, substr.type_ident, exprs)
78                     }
79                 }
80                 Named(ref fields) => {
81                     let zero_fields = fields.map(|&(ident, span)| {
82                         cx.field_imm(span, ident, zero_call(span))
83                     });
84                     cx.expr_struct_ident(trait_span, substr.type_ident, zero_fields)
85                 }
86             }
87         }
88         StaticEnum(..) => {
89             cx.span_err(trait_span, "`Zero` cannot be derived for enums, only structs");
90             // let compilation continue
91             cx.expr_uint(trait_span, 0)
92         }
93         _ => cx.bug("Non-static method in `deriving(Zero)`")
94     };
95 }