]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/cmp/totalord.rs
Use path helper macros in deriving
[rust.git] / src / libsyntax / ext / deriving / cmp / totalord.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 use ext::deriving::generic::ty::*;
18 use parse::token::InternedString;
19 use ptr::P;
20
21 pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
22                                    span: Span,
23                                    mitem: &MetaItem,
24                                    item: &Item,
25                                    push: F) where
26     F: FnOnce(P<Item>),
27 {
28     let inline = cx.meta_word(span, InternedString::new("inline"));
29     let attrs = vec!(cx.attribute(span, inline));
30     let trait_def = TraitDef {
31         span: span,
32         attributes: Vec::new(),
33         path: path!(std::cmp::Ord),
34         additional_bounds: Vec::new(),
35         generics: LifetimeBounds::empty(),
36         methods: vec!(
37             MethodDef {
38                 name: "cmp",
39                 generics: LifetimeBounds::empty(),
40                 explicit_self: borrowed_explicit_self(),
41                 args: vec!(borrowed_self()),
42                 ret_ty: Literal(path!(std::cmp::Ordering)),
43                 attributes: attrs,
44                 combine_substructure: combine_substructure(box |a, b, c| {
45                     cs_cmp(a, b, c)
46                 }),
47             }
48         ),
49         associated_types: Vec::new(),
50     };
51
52     trait_def.expand(cx, mitem, item, push)
53 }
54
55
56 pub fn ordering_collapsed(cx: &mut ExtCtxt,
57                           span: Span,
58                           self_arg_tags: &[ast::Ident]) -> 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,
65               substr: &Substructure) -> P<Expr> {
66     let test_id = cx.ident_of("__test");
67     let equals_path = cx.path_global(span,
68                                      vec!(cx.ident_of("std"),
69                                           cx.ident_of("cmp"),
70                                           cx.ident_of("Ordering"),
71                                           cx.ident_of("Equal")));
72
73     let cmp_path = vec![
74         cx.ident_of("std"),
75         cx.ident_of("cmp"),
76         cx.ident_of("Ord"),
77         cx.ident_of("cmp"),
78     ];
79
80     /*
81     Builds:
82
83     let __test = ::std::cmp::Ord::cmp(&self_field1, &other_field1);
84     if other == ::std::cmp::Ordering::Equal {
85         let __test = ::std::cmp::Ord::cmp(&self_field2, &other_field2);
86         if __test == ::std::cmp::Ordering::Equal {
87             ...
88         } else {
89             __test
90         }
91     } else {
92         __test
93     }
94
95     FIXME #6449: These `if`s could/should be `match`es.
96     */
97     cs_fold(
98         // foldr nests the if-elses correctly, leaving the first field
99         // as the outermost one, and the last as the innermost.
100         false,
101         |cx, span, old, self_f, other_fs| {
102             // let __test = new;
103             // if __test == ::std::cmp::Ordering::Equal {
104             //    old
105             // } else {
106             //    __test
107             // }
108
109             let new = {
110                 let other_f = match other_fs {
111                     [ref o_f] => o_f,
112                     _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
113                 };
114
115                 let args = vec![
116                     cx.expr_addr_of(span, self_f),
117                     cx.expr_addr_of(span, other_f.clone()),
118                 ];
119
120                 cx.expr_call_global(span, cmp_path.clone(), args)
121             };
122
123             let assign = cx.stmt_let(span, false, test_id, new);
124
125             let cond = cx.expr_binary(span, ast::BiEq,
126                                       cx.expr_ident(span, test_id),
127                                       cx.expr_path(equals_path.clone()));
128             let if_ = cx.expr_if(span,
129                                  cond,
130                                  old, Some(cx.expr_ident(span, test_id)));
131             cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
132         },
133         cx.expr_path(equals_path.clone()),
134         box |cx, span, (self_args, tag_tuple), _non_self_args| {
135             if self_args.len() != 2 {
136                 cx.span_bug(span, "not exactly 2 arguments in `derives(Ord)`")
137             } else {
138                 ordering_collapsed(cx, span, tag_tuple)
139             }
140         },
141         cx, span, substr)
142 }