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