]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/ext/deriving/cmp/ord.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / libsyntax / ext / deriving / cmp / ord.rs
CommitLineData
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
11use ast;
d9579d0f 12use ast::{MetaItem, Expr};
1a4d82fc 13use codemap::Span;
d9579d0f 14use ext::base::{ExtCtxt, Annotatable};
970d7e83
LB
15use ext::build::AstBuilder;
16use ext::deriving::generic::*;
1a4d82fc
JJ
17use ext::deriving::generic::ty::*;
18use parse::token::InternedString;
19use ptr::P;
970d7e83 20
d9579d0f
AL
21pub 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(),
35 methods: vec!(
970d7e83 36 MethodDef {
d9579d0f 37 name: "cmp",
970d7e83
LB
38 generics: LifetimeBounds::empty(),
39 explicit_self: borrowed_explicit_self(),
1a4d82fc 40 args: vec!(borrowed_self()),
d9579d0f 41 ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
1a4d82fc 42 attributes: attrs,
62682a34 43 is_unsafe: false,
d9579d0f
AL
44 combine_substructure: combine_substructure(Box::new(|a, b, c| {
45 cs_cmp(a, b, c)
46 })),
970d7e83 47 }
d9579d0f 48 ),
85aaf69f 49 associated_types: Vec::new(),
970d7e83 50 };
1a4d82fc 51
62682a34 52 trait_def.expand(cx, mitem, item, push)
1a4d82fc
JJ
53}
54
d9579d0f
AL
55
56pub fn ordering_collapsed(cx: &mut ExtCtxt,
57 span: Span,
58 self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
1a4d82fc
JJ
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]));
d9579d0f 61 cx.expr_method_call(span, lft, cx.ident_of("cmp"), vec![rgt])
1a4d82fc
JJ
62}
63
d9579d0f 64pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
1a4d82fc
JJ
65 substr: &Substructure) -> P<Expr> {
66 let test_id = cx.ident_of("__test");
d9579d0f
AL
67 let equals_path = cx.path_global(span,
68 vec!(cx.ident_of_std("core"),
69 cx.ident_of("cmp"),
70 cx.ident_of("Ordering"),
71 cx.ident_of("Equal")));
1a4d82fc 72
d9579d0f 73 let cmp_path = vec![
85aaf69f 74 cx.ident_of_std("core"),
1a4d82fc 75 cx.ident_of("cmp"),
d9579d0f
AL
76 cx.ident_of("Ord"),
77 cx.ident_of("cmp"),
1a4d82fc
JJ
78 ];
79
80 /*
81 Builds:
82
d9579d0f
AL
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 {
1a4d82fc
JJ
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;
d9579d0f 103 // if __test == ::std::cmp::Ordering::Equal {
1a4d82fc
JJ
104 // old
105 // } else {
106 // __test
107 // }
108
109 let new = {
d9579d0f
AL
110 let other_f = match (other_fs.len(), other_fs.get(0)) {
111 (1, Some(o_f)) => o_f,
85aaf69f 112 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
1a4d82fc
JJ
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
d9579d0f 120 cx.expr_call_global(span, cmp_path.clone(), args)
1a4d82fc
JJ
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),
d9579d0f 127 cx.expr_path(equals_path.clone()));
1a4d82fc
JJ
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 },
d9579d0f 133 cx.expr_path(equals_path.clone()),
c34b1796 134 Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
1a4d82fc 135 if self_args.len() != 2 {
d9579d0f 136 cx.span_bug(span, "not exactly 2 arguments in `derives(Ord)`")
1a4d82fc 137 } else {
d9579d0f 138 ordering_collapsed(cx, span, tag_tuple)
970d7e83 139 }
c34b1796 140 }),
970d7e83
LB
141 cx, span, substr)
142}