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: &ExtCtxt,
22 let trait_def = TraitDef {
25 path: Path::new(~["std", "num", "Zero"]),
26 additional_bounds: ~[],
27 generics: LifetimeBounds::empty(),
31 generics: LifetimeBounds::empty(),
36 const_nonmatching: false,
37 combine_substructure: zero_substructure
41 generics: LifetimeBounds::empty(),
42 explicit_self: borrowed_explicit_self(),
44 ret_ty: Literal(Path::new(~["bool"])),
46 const_nonmatching: false,
47 combine_substructure: |cx, span, substr| {
48 cs_and(|cx, span, _, _| cx.span_bug(span,
57 trait_def.expand(mitem, in_items)
60 fn zero_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
67 let zero_call = |span| cx.expr_call_global(span, zero_ident.clone(), ~[]);
69 return match *substr.fields {
70 StaticStruct(_, ref summary) => {
72 Unnamed(ref fields) => {
73 if fields.is_empty() {
74 cx.expr_ident(span, substr.type_ident)
76 let exprs = fields.map(|sp| zero_call(*sp));
77 cx.expr_call_ident(span, substr.type_ident, exprs)
80 Named(ref fields) => {
81 let zero_fields = fields.map(|&(ident, span)| {
82 cx.field_imm(span, ident, zero_call(span))
84 cx.expr_struct_ident(span, substr.type_ident, zero_fields)
89 cx.span_fatal(span, "`Zero` cannot be derived for enums, \
92 _ => cx.bug("Non-static method in `deriving(Zero)`")