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