(index<- )        ./libsyntax/ext/deriving/cmp/ord.rs

    git branch:    * master           5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
    modified:    Fri Apr 25 22:40:04 2014
   1  // Copyright 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;
  12  use ast::{MetaItem, Item, Expr};
  13  use codemap::Span;
  14  use ext::base::ExtCtxt;
  15  use ext::build::AstBuilder;
  16  use ext::deriving::generic::*;
  17  use parse::token::InternedString;
  18  
  19  pub fn expand_deriving_ord(cx: &mut ExtCtxt,
  20                             spanSpan,
  21                             mitem: @MetaItem,
  22                             item: @Item,
  23                             push: |@Item|) {
  24      macro_rules! md (
  25          ($name:expr, $op:expr, $equal:expr) => { {
  26              let inline = cx.meta_word(span, InternedString::new("inline"));
  27              let attrs = vec!(cx.attribute(span, inline));
  28              MethodDef {
  29                  name: $name,
  30                  generics: LifetimeBounds::empty(),
  31                  explicit_self: borrowed_explicit_self(),
  32                  args: vec!(borrowed_self()),
  33                  ret_ty: Literal(Path::new(vec!("bool"))),
  34                  attributes: attrs,
  35                  const_nonmatching: false,
  36                  combine_substructure: combine_substructure(|cx, span, substr| {
  37                      cs_op($op, $equal, cx, span, substr)
  38                  })
  39              }
  40          } }
  41      );
  42  
  43      let trait_def = TraitDef {
  44          span: span,
  45          attributes: Vec::new(),
  46          path: Path::new(vec!("std", "cmp", "Ord")),
  47          additional_bounds: Vec::new(),
  48          generics: LifetimeBounds::empty(),
  49          methods: vec!(
  50              md!("lt", true, false),
  51              md!("le", true, true),
  52              md!("gt", false, false),
  53              md!("ge", false, true)
  54          )
  55      };
  56      trait_def.expand(cx, mitem, item, push)
  57  }
  58  
  59  /// Strict inequality.
  60  fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt, spanSpan, substr: &Substructure) -> @Expr {
  61      let op = if less {ast::BiLt} else {ast::BiGt};
  62      cs_fold(
  63          false, // need foldr,
  64          |cx, span, subexpr, self_f, other_fs| {
  65              /*
  66              build up a series of chain ||'s and &&'s from the inside
  67              out (hence foldr) to get lexical ordering, i.e. for op ==
  68              `ast::lt`
  69  
  70              ```
  71              self.f1 < other.f1 || (!(other.f1 < self.f1) &&
  72                  (self.f2 < other.f2 || (!(other.f2 < self.f2) &&
  73                      (false)
  74                  ))
  75              )
  76              ```
  77  
  78              The optimiser should remove the redundancy. We explicitly
  79              get use the binops to avoid auto-deref derefencing too many
  80              layers of pointers, if the type includes pointers.
  81              */
  82              let other_f = match other_fs {
  83                  [o_f] => o_f,
  84                  _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
  85              };
  86  
  87              let cmp = cx.expr_binary(span, op, self_f, other_f);
  88  
  89              let not_cmp = cx.expr_unary(span, ast::UnNot,
  90                                          cx.expr_binary(span, op, other_f, self_f));
  91  
  92              let and = cx.expr_binary(span, ast::BiAnd, not_cmp, subexpr);
  93              cx.expr_binary(span, ast::BiOr, cmp, and)
  94          },
  95          cx.expr_bool(span, equal),
  96          |cx, span, args, _| {
  97              // nonmatching enums, order by the order the variants are
  98              // written
  99              match args {
 100                  [(self_var, _, _),
 101                   (other_var, _, _)] =>
 102                      cx.expr_bool(span,
 103                                   if less {
 104                                       self_var < other_var
 105                                   } else {
 106                                       self_var > other_var
 107                                   }),
 108                  _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(Ord)`")
 109              }
 110          },
 111          cx, span, substr)
 112  }