]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/ext/deriving/cmp/partial_eq.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / libsyntax / ext / deriving / cmp / partial_eq.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::{MetaItem, Expr, self};
12 use codemap::Span;
13 use ext::base::{ExtCtxt, Annotatable};
14 use ext::build::AstBuilder;
15 use ext::deriving::generic::*;
16 use ext::deriving::generic::ty::*;
17 use parse::token::InternedString;
18 use ptr::P;
19
20 pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
21 span: Span,
22 mitem: &MetaItem,
23 item: &Annotatable,
24 push: &mut FnMut(Annotatable))
25 {
26 // structures are equal if all fields are equal, and non equal, if
27 // any fields are not equal or if the enum variants are different
28 fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
29 cs_fold(
30 true, // use foldl
31 |cx, span, subexpr, self_f, other_fs| {
32 let other_f = match (other_fs.len(), other_fs.get(0)) {
33 (1, Some(o_f)) => o_f,
34 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
35 };
36
37 let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
38
39 cx.expr_binary(span, ast::BiAnd, subexpr, eq)
40 },
41 cx.expr_bool(span, true),
42 Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
43 cx, span, substr)
44 }
45 fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
46 cs_fold(
47 true, // use foldl
48 |cx, span, subexpr, self_f, other_fs| {
49 let other_f = match (other_fs.len(), other_fs.get(0)) {
50 (1, Some(o_f)) => o_f,
51 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`")
52 };
53
54 let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
55
56 cx.expr_binary(span, ast::BiOr, subexpr, eq)
57 },
58 cx.expr_bool(span, false),
59 Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
60 cx, span, substr)
61 }
62
63 macro_rules! md {
64 ($name:expr, $f:ident) => { {
65 let inline = cx.meta_word(span, InternedString::new("inline"));
66 let attrs = vec!(cx.attribute(span, inline));
67 MethodDef {
68 name: $name,
69 generics: LifetimeBounds::empty(),
70 explicit_self: borrowed_explicit_self(),
71 args: vec!(borrowed_self()),
72 ret_ty: Literal(path_local!(bool)),
73 attributes: attrs,
74 is_unsafe: false,
75 combine_substructure: combine_substructure(Box::new(|a, b, c| {
76 $f(a, b, c)
77 }))
78 }
79 } }
80 }
81
82 let trait_def = TraitDef {
83 span: span,
84 attributes: Vec::new(),
85 path: path_std!(cx, core::cmp::PartialEq),
86 additional_bounds: Vec::new(),
87 generics: LifetimeBounds::empty(),
88 methods: vec!(
89 md!("eq", cs_eq),
90 md!("ne", cs_ne)
91 ),
92 associated_types: Vec::new(),
93 };
94 trait_def.expand(cx, mitem, item, push)
95 }