]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
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 | ||
9cc50fc6 SL |
11 | use deriving::generic::*; |
12 | use deriving::generic::ty::*; | |
13 | ||
54a0048b | 14 | use syntax::ast::{MetaItem, Expr, self}; |
9cc50fc6 SL |
15 | use syntax::codemap::Span; |
16 | use syntax::ext::base::{ExtCtxt, Annotatable}; | |
17 | use syntax::ext::build::AstBuilder; | |
18 | use syntax::parse::token::InternedString; | |
19 | use syntax::ptr::P; | |
970d7e83 | 20 | |
d9579d0f AL |
21 | pub fn expand_deriving_ord(cx: &mut ExtCtxt, |
22 | span: Span, | |
23 | mitem: &MetaItem, | |
62682a34 | 24 | item: &Annotatable, |
d9579d0f | 25 | push: &mut FnMut(Annotatable)) |
1a4d82fc | 26 | { |
d9579d0f AL |
27 | let inline = cx.meta_word(span, InternedString::new("inline")); |
28 | let attrs = vec!(cx.attribute(span, inline)); | |
29 | let trait_def = TraitDef { | |
30 | span: span, | |
31 | attributes: Vec::new(), | |
32 | path: path_std!(cx, core::cmp::Ord), | |
33 | additional_bounds: Vec::new(), | |
34 | generics: LifetimeBounds::empty(), | |
e9174d1e | 35 | is_unsafe: false, |
d9579d0f | 36 | methods: vec!( |
970d7e83 | 37 | MethodDef { |
d9579d0f | 38 | name: "cmp", |
970d7e83 LB |
39 | generics: LifetimeBounds::empty(), |
40 | explicit_self: borrowed_explicit_self(), | |
1a4d82fc | 41 | args: vec!(borrowed_self()), |
d9579d0f | 42 | ret_ty: Literal(path_std!(cx, core::cmp::Ordering)), |
1a4d82fc | 43 | attributes: attrs, |
62682a34 | 44 | is_unsafe: false, |
d9579d0f AL |
45 | combine_substructure: combine_substructure(Box::new(|a, b, c| { |
46 | cs_cmp(a, b, c) | |
47 | })), | |
970d7e83 | 48 | } |
d9579d0f | 49 | ), |
85aaf69f | 50 | associated_types: Vec::new(), |
970d7e83 | 51 | }; |
1a4d82fc | 52 | |
62682a34 | 53 | trait_def.expand(cx, mitem, item, push) |
1a4d82fc JJ |
54 | } |
55 | ||
d9579d0f AL |
56 | |
57 | pub fn ordering_collapsed(cx: &mut ExtCtxt, | |
58 | span: Span, | |
59 | self_arg_tags: &[ast::Ident]) -> P<ast::Expr> { | |
1a4d82fc JJ |
60 | let lft = cx.expr_ident(span, self_arg_tags[0]); |
61 | let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1])); | |
d9579d0f | 62 | cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt]) |
1a4d82fc JJ |
63 | } |
64 | ||
d9579d0f | 65 | pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, |
1a4d82fc | 66 | substr: &Substructure) -> P<Expr> { |
54a0048b | 67 | let test_id = cx.ident_of("__cmp"); |
d9579d0f | 68 | let equals_path = cx.path_global(span, |
e9174d1e | 69 | cx.std_path(&["cmp", "Ordering", "Equal"])); |
1a4d82fc | 70 | |
e9174d1e | 71 | let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]); |
1a4d82fc JJ |
72 | |
73 | /* | |
74 | Builds: | |
75 | ||
54a0048b SL |
76 | match ::std::cmp::Ord::cmp(&self_field1, &other_field1) { |
77 | ::std::cmp::Ordering::Equal => | |
78 | match ::std::cmp::Ord::cmp(&self_field2, &other_field2) { | |
79 | ::std::cmp::Ordering::Equal => { | |
80 | ... | |
81 | } | |
82 | __cmp => __cmp | |
83 | }, | |
84 | __cmp => __cmp | |
1a4d82fc | 85 | } |
1a4d82fc JJ |
86 | */ |
87 | cs_fold( | |
88 | // foldr nests the if-elses correctly, leaving the first field | |
89 | // as the outermost one, and the last as the innermost. | |
90 | false, | |
91 | |cx, span, old, self_f, other_fs| { | |
54a0048b SL |
92 | // match new { |
93 | // ::std::cmp::Ordering::Equal => old, | |
94 | // __cmp => __cmp | |
1a4d82fc JJ |
95 | // } |
96 | ||
97 | let new = { | |
d9579d0f AL |
98 | let other_f = match (other_fs.len(), other_fs.get(0)) { |
99 | (1, Some(o_f)) => o_f, | |
54a0048b | 100 | _ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"), |
1a4d82fc JJ |
101 | }; |
102 | ||
103 | let args = vec![ | |
104 | cx.expr_addr_of(span, self_f), | |
105 | cx.expr_addr_of(span, other_f.clone()), | |
106 | ]; | |
107 | ||
d9579d0f | 108 | cx.expr_call_global(span, cmp_path.clone(), args) |
1a4d82fc JJ |
109 | }; |
110 | ||
54a0048b SL |
111 | let eq_arm = cx.arm(span, |
112 | vec![cx.pat_enum(span, | |
113 | equals_path.clone(), | |
114 | vec![])], | |
115 | old); | |
116 | let neq_arm = cx.arm(span, | |
117 | vec![cx.pat_ident(span, test_id)], | |
118 | cx.expr_ident(span, test_id)); | |
1a4d82fc | 119 | |
54a0048b | 120 | cx.expr_match(span, new, vec![eq_arm, neq_arm]) |
1a4d82fc | 121 | }, |
d9579d0f | 122 | cx.expr_path(equals_path.clone()), |
c34b1796 | 123 | Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| { |
1a4d82fc | 124 | if self_args.len() != 2 { |
54a0048b | 125 | cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`") |
1a4d82fc | 126 | } else { |
d9579d0f | 127 | ordering_collapsed(cx, span, tag_tuple) |
970d7e83 | 128 | } |
c34b1796 | 129 | }), |
970d7e83 LB |
130 | cx, span, substr) |
131 | } |