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.
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.
11 use ast::{MetaItem, Item, Expr};
13 use ext::base::ExtCtxt;
14 use ext::build::AstBuilder;
15 use ext::deriving::generic::*;
17 pub fn expand_deriving_zero(cx: &mut ExtCtxt,
22 let trait_def = TraitDef {
24 path: Path::new(~["std", "num", "Zero"]),
25 additional_bounds: ~[],
26 generics: LifetimeBounds::empty(),
30 generics: LifetimeBounds::empty(),
35 const_nonmatching: false,
36 combine_substructure: zero_substructure
40 generics: LifetimeBounds::empty(),
41 explicit_self: borrowed_explicit_self(),
43 ret_ty: Literal(Path::new(~["bool"])),
45 const_nonmatching: false,
46 combine_substructure: |cx, span, substr| {
47 cs_and(|cx, span, _, _| cx.span_bug(span,
56 trait_def.expand(cx, mitem, in_items)
59 fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
66 let zero_call = |span| cx.expr_call_global(span, zero_ident.clone(), ~[]);
68 return match *substr.fields {
69 StaticStruct(_, ref summary) => {
71 Unnamed(ref fields) => {
72 if fields.is_empty() {
73 cx.expr_ident(trait_span, substr.type_ident)
75 let exprs = fields.map(|sp| zero_call(*sp));
76 cx.expr_call_ident(trait_span, substr.type_ident, exprs)
79 Named(ref fields) => {
80 let zero_fields = fields.map(|&(ident, span)| {
81 cx.field_imm(span, ident, zero_call(span))
83 cx.expr_struct_ident(trait_span, substr.type_ident, zero_fields)
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)
92 _ => cx.bug("Non-static method in `deriving(Zero)`")