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