(index<- )        ./libsyntax/ext/deriving/zero.rs

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