]> git.proxmox.com Git - rustc.git/blob - src/libsyntax_ext/deriving/cmp/ord.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.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::{MetaItem, Expr, BinOpKind, self};
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;
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 {
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(),
35 is_unsafe: false,
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!(cx, core::cmp::Ordering)),
43 attributes: attrs,
44 is_unsafe: false,
45 combine_substructure: combine_substructure(Box::new(|a, b, c| {
46 cs_cmp(a, b, c)
47 })),
48 }
49 ),
50 associated_types: Vec::new(),
51 };
52
53 trait_def.expand(cx, mitem, item, push)
54 }
55
56
57 pub fn ordering_collapsed(cx: &mut ExtCtxt,
58 span: Span,
59 self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
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]));
62 cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt])
63 }
64
65 pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
66 substr: &Substructure) -> P<Expr> {
67 let test_id = cx.ident_of("__test");
68 let equals_path = cx.path_global(span,
69 cx.std_path(&["cmp", "Ordering", "Equal"]));
70
71 let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]);
72
73 /*
74 Builds:
75
76 let __test = ::std::cmp::Ord::cmp(&self_field1, &other_field1);
77 if other == ::std::cmp::Ordering::Equal {
78 let __test = ::std::cmp::Ord::cmp(&self_field2, &other_field2);
79 if __test == ::std::cmp::Ordering::Equal {
80 ...
81 } else {
82 __test
83 }
84 } else {
85 __test
86 }
87
88 FIXME #6449: These `if`s could/should be `match`es.
89 */
90 cs_fold(
91 // foldr nests the if-elses correctly, leaving the first field
92 // as the outermost one, and the last as the innermost.
93 false,
94 |cx, span, old, self_f, other_fs| {
95 // let __test = new;
96 // if __test == ::std::cmp::Ordering::Equal {
97 // old
98 // } else {
99 // __test
100 // }
101
102 let new = {
103 let other_f = match (other_fs.len(), other_fs.get(0)) {
104 (1, Some(o_f)) => o_f,
105 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
106 };
107
108 let args = vec![
109 cx.expr_addr_of(span, self_f),
110 cx.expr_addr_of(span, other_f.clone()),
111 ];
112
113 cx.expr_call_global(span, cmp_path.clone(), args)
114 };
115
116 let assign = cx.stmt_let(span, false, test_id, new);
117
118 let cond = cx.expr_binary(span, BinOpKind::Eq,
119 cx.expr_ident(span, test_id),
120 cx.expr_path(equals_path.clone()));
121 let if_ = cx.expr_if(span,
122 cond,
123 old, Some(cx.expr_ident(span, test_id)));
124 cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
125 },
126 cx.expr_path(equals_path.clone()),
127 Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
128 if self_args.len() != 2 {
129 cx.span_bug(span, "not exactly 2 arguments in `derives(Ord)`")
130 } else {
131 ordering_collapsed(cx, span, tag_tuple)
132 }
133 }),
134 cx, span, substr)
135 }