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.
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.
11 pub use self::OrderingOp::*;
14 use ast::{MetaItem, Item, Expr};
16 use ext::base::ExtCtxt;
17 use ext::build::AstBuilder;
18 use ext::deriving::generic::*;
19 use ext::deriving::generic::ty::*;
20 use parse::token::InternedString;
23 pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
31 ($name:expr, $op:expr, $equal:expr) => { {
32 let inline = cx.meta_word(span, InternedString::new("inline"));
33 let attrs = vec!(cx.attribute(span, inline));
36 generics: LifetimeBounds::empty(),
37 explicit_self: borrowed_explicit_self(),
38 args: vec!(borrowed_self()),
39 ret_ty: Literal(path!(bool)),
41 combine_substructure: combine_substructure(box |cx, span, substr| {
42 cs_op($op, $equal, cx, span, substr)
48 let ordering_ty = Literal(path!(std::cmp::Ordering));
49 let ret_ty = Literal(Path::new_(pathvec!(std::option::Option),
51 vec![box ordering_ty],
54 let inline = cx.meta_word(span, InternedString::new("inline"));
55 let attrs = vec!(cx.attribute(span, inline));
57 let partial_cmp_def = MethodDef {
59 generics: LifetimeBounds::empty(),
60 explicit_self: borrowed_explicit_self(),
61 args: vec![borrowed_self()],
64 combine_substructure: combine_substructure(box |cx, span, substr| {
65 cs_partial_cmp(cx, span, substr)
69 let trait_def = TraitDef {
72 path: path!(std::cmp::PartialOrd),
73 additional_bounds: vec![],
74 generics: LifetimeBounds::empty(),
77 md!("lt", true, false),
78 md!("le", true, true),
79 md!("gt", false, false),
80 md!("ge", false, true)
82 associated_types: Vec::new(),
84 trait_def.expand(cx, mitem, item, push)
89 PartialCmpOp, LtOp, LeOp, GtOp, GeOp,
92 pub fn some_ordering_collapsed(cx: &mut ExtCtxt,
95 self_arg_tags: &[ast::Ident]) -> P<ast::Expr> {
96 let lft = cx.expr_ident(span, self_arg_tags[0]);
97 let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
98 let op_str = match op {
99 PartialCmpOp => "partial_cmp",
100 LtOp => "lt", LeOp => "le",
101 GtOp => "gt", GeOp => "ge",
103 cx.expr_method_call(span, lft, cx.ident_of(op_str), vec![rgt])
106 pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
107 substr: &Substructure) -> P<Expr> {
108 let test_id = cx.ident_of("__test");
109 let ordering = cx.path_global(span,
110 vec!(cx.ident_of("std"),
112 cx.ident_of("Ordering"),
113 cx.ident_of("Equal")));
114 let ordering = cx.expr_path(ordering);
115 let equals_expr = cx.expr_some(span, ordering);
117 let partial_cmp_path = vec![
120 cx.ident_of("PartialOrd"),
121 cx.ident_of("partial_cmp"),
127 let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1);
128 if __test == ::std::option::Option::Some(::std::cmp::Ordering::Equal) {
129 let __test = ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2);
130 if __test == ::std::option::Option::Some(::std::cmp::Ordering::Equal) {
139 FIXME #6449: These `if`s could/should be `match`es.
142 // foldr nests the if-elses correctly, leaving the first field
143 // as the outermost one, and the last as the innermost.
145 |cx, span, old, self_f, other_fs| {
147 // if __test == Some(::std::cmp::Ordering::Equal) {
154 let other_f = match other_fs {
156 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
160 cx.expr_addr_of(span, self_f),
161 cx.expr_addr_of(span, other_f.clone()),
164 cx.expr_call_global(span, partial_cmp_path.clone(), args)
167 let assign = cx.stmt_let(span, false, test_id, new);
169 let cond = cx.expr_binary(span, ast::BiEq,
170 cx.expr_ident(span, test_id),
171 equals_expr.clone());
172 let if_ = cx.expr_if(span,
174 old, Some(cx.expr_ident(span, test_id)));
175 cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
178 box |cx, span, (self_args, tag_tuple), _non_self_args| {
179 if self_args.len() != 2 {
180 cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
182 some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
188 /// Strict inequality.
189 fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
190 span: Span, substr: &Substructure) -> P<Expr> {
191 let op = if less {ast::BiLt} else {ast::BiGt};
193 false, // need foldr,
194 |cx, span, subexpr, self_f, other_fs| {
196 build up a series of chain ||'s and &&'s from the inside
197 out (hence foldr) to get lexical ordering, i.e. for op ==
201 self.f1 < other.f1 || (!(other.f1 < self.f1) &&
202 (self.f2 < other.f2 || (!(other.f2 < self.f2) &&
208 The optimiser should remove the redundancy. We explicitly
209 get use the binops to avoid auto-deref dereferencing too many
210 layers of pointers, if the type includes pointers.
212 let other_f = match other_fs {
214 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
217 let cmp = cx.expr_binary(span, op, self_f.clone(), other_f.clone());
219 let not_cmp = cx.expr_unary(span, ast::UnNot,
220 cx.expr_binary(span, op, other_f.clone(), self_f));
222 let and = cx.expr_binary(span, ast::BiAnd, not_cmp, subexpr);
223 cx.expr_binary(span, ast::BiOr, cmp, and)
225 cx.expr_bool(span, equal),
226 box |cx, span, (self_args, tag_tuple), _non_self_args| {
227 if self_args.len() != 2 {
228 cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
230 let op = match (less, equal) {
231 (true, true) => LeOp, (true, false) => LtOp,
232 (false, true) => GeOp, (false, false) => GtOp,
234 some_ordering_collapsed(cx, span, op, tag_tuple)