|
| 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 core::prelude::*; |
| 12 | + |
| 13 | +use ast::{meta_item, item, expr}; |
| 14 | +use codemap::span; |
| 15 | +use ext::base::ExtCtxt; |
| 16 | +use ext::build::AstBuilder; |
| 17 | +use ext::deriving::generic::*; |
| 18 | + |
| 19 | +use core::vec; |
| 20 | + |
| 21 | +pub fn expand_deriving_zero(cx: @ExtCtxt, |
| 22 | + span: span, |
| 23 | + mitem: @meta_item, |
| 24 | + in_items: ~[@item]) |
| 25 | + -> ~[@item] { |
| 26 | + let trait_def = TraitDef { |
| 27 | + path: Path::new(~["std", "num", "Zero"]), |
| 28 | + additional_bounds: ~[], |
| 29 | + generics: LifetimeBounds::empty(), |
| 30 | + methods: ~[ |
| 31 | + MethodDef { |
| 32 | + name: "zero", |
| 33 | + generics: LifetimeBounds::empty(), |
| 34 | + explicit_self: None, |
| 35 | + args: ~[], |
| 36 | + ret_ty: Self, |
| 37 | + const_nonmatching: false, |
| 38 | + combine_substructure: zero_substructure |
| 39 | + }, |
| 40 | + MethodDef { |
| 41 | + name: "is_zero", |
| 42 | + generics: LifetimeBounds::empty(), |
| 43 | + explicit_self: borrowed_explicit_self(), |
| 44 | + args: ~[], |
| 45 | + 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, |
| 49 | + "Non-matching enum \ |
| 50 | + variant in \ |
| 51 | + deriving(Zero)"), |
| 52 | + cx, span, substr) |
| 53 | + } |
| 54 | + } |
| 55 | + ] |
| 56 | + }; |
| 57 | + trait_def.expand(cx, span, mitem, in_items) |
| 58 | +} |
| 59 | + |
| 60 | +fn zero_substructure(cx: @ExtCtxt, 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 = || { |
| 68 | + cx.expr_call_global(span, copy zero_ident, ~[]) |
| 69 | + }; |
| 70 | + |
| 71 | + return match *substr.fields { |
| 72 | + StaticStruct(_, ref summary) => { |
| 73 | + match *summary { |
| 74 | + Left(count) => { |
| 75 | + if count == 0 { |
| 76 | + cx.expr_ident(span, substr.type_ident) |
| 77 | + } else { |
| 78 | + let exprs = vec::from_fn(count, |_| zero_call()); |
| 79 | + cx.expr_call_ident(span, substr.type_ident, exprs) |
| 80 | + } |
| 81 | + } |
| 82 | + Right(ref fields) => { |
| 83 | + let zero_fields = do fields.map |ident| { |
| 84 | + cx.field_imm(span, *ident, zero_call()) |
| 85 | + }; |
| 86 | + cx.expr_struct_ident(span, substr.type_ident, zero_fields) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + StaticEnum(*) => { |
| 91 | + cx.span_fatal(span, "`Zero` cannot be derived for enums, \ |
| 92 | + only structs") |
| 93 | + } |
| 94 | + _ => cx.bug("Non-static method in `deriving(Zero)`") |
| 95 | + }; |
| 96 | +} |
0 commit comments