]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_builtin_macros / src / deriving / cmp / ord.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4
5 use rustc_ast::ptr::P;
6 use rustc_ast::{self as ast, Expr, MetaItem};
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::{sym, Ident};
9 use rustc_span::Span;
10
11 pub fn expand_deriving_ord(
12 cx: &mut ExtCtxt<'_>,
13 span: Span,
14 mitem: &MetaItem,
15 item: &Annotatable,
16 push: &mut dyn FnMut(Annotatable),
17 ) {
18 let inline = cx.meta_word(span, sym::inline);
19 let attrs = vec![cx.attribute(inline)];
20 let trait_def = TraitDef {
21 span,
22 attributes: Vec::new(),
23 path: path_std!(cmp::Ord),
24 additional_bounds: Vec::new(),
25 generics: Bounds::empty(),
26 is_unsafe: false,
27 supports_unions: false,
28 methods: vec![MethodDef {
29 name: sym::cmp,
30 generics: Bounds::empty(),
31 explicit_self: borrowed_explicit_self(),
32 args: vec![(borrowed_self(), sym::other)],
33 ret_ty: Literal(path_std!(cmp::Ordering)),
34 attributes: attrs,
35 is_unsafe: false,
36 unify_fieldless_variants: true,
37 combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
38 }],
39 associated_types: Vec::new(),
40 };
41
42 trait_def.expand(cx, mitem, item, push)
43 }
44
45 pub fn ordering_collapsed(
46 cx: &mut ExtCtxt<'_>,
47 span: Span,
48 self_arg_tags: &[Ident],
49 ) -> P<ast::Expr> {
50 let lft = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[0]));
51 let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
52 let fn_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
53 cx.expr_call_global(span, fn_cmp_path, vec![lft, rgt])
54 }
55
56 pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
57 let test_id = Ident::new(sym::cmp, span);
58 let equals_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
59
60 let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
61
62 // Builds:
63 //
64 // match ::std::cmp::Ord::cmp(&self_field1, &other_field1) {
65 // ::std::cmp::Ordering::Equal =>
66 // match ::std::cmp::Ord::cmp(&self_field2, &other_field2) {
67 // ::std::cmp::Ordering::Equal => {
68 // ...
69 // }
70 // cmp => cmp
71 // },
72 // cmp => cmp
73 // }
74 //
75 cs_fold(
76 // foldr nests the if-elses correctly, leaving the first field
77 // as the outermost one, and the last as the innermost.
78 false,
79 |cx, span, old, self_f, other_fs| {
80 // match new {
81 // ::std::cmp::Ordering::Equal => old,
82 // cmp => cmp
83 // }
84
85 let new = {
86 let other_f = match other_fs {
87 [o_f] => o_f,
88 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"),
89 };
90
91 let args =
92 vec![cx.expr_addr_of(span, self_f), cx.expr_addr_of(span, other_f.clone())];
93
94 cx.expr_call_global(span, cmp_path.clone(), args)
95 };
96
97 let eq_arm = cx.arm(span, cx.pat_path(span, equals_path.clone()), old);
98 let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
99
100 cx.expr_match(span, new, vec![eq_arm, neq_arm])
101 },
102 cx.expr_path(equals_path.clone()),
103 Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
104 if self_args.len() != 2 {
105 cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
106 } else {
107 ordering_collapsed(cx, span, tag_tuple)
108 }
109 }),
110 cx,
111 span,
112 substr,
113 )
114 }