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