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