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