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