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 use ast::{MetaItem, Item, Expr, mod};
13 use ext::base::ExtCtxt;
14 use ext::build::AstBuilder;
15 use ext::deriving::generic::*;
16 use ext::deriving::generic::ty::*;
17 use parse::token::InternedString;
20 pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
27 // structures are equal if all fields are equal, and non equal, if
28 // any fields are not equal or if the enum variants are different
29 fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
32 |cx, span, subexpr, self_f, other_fs| {
33 let other_f = match other_fs {
35 _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialEq)`")
38 let eq = cx.expr_binary(span, ast::BiEq, self_f, other_f.clone());
40 cx.expr_binary(span, ast::BiAnd, subexpr, eq)
42 cx.expr_bool(span, true),
43 |cx, span, _, _| cx.expr_bool(span, false),
46 fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
49 |cx, span, subexpr, self_f, other_fs| {
50 let other_f = match other_fs {
52 _ => cx.span_bug(span, "not exactly 2 arguments in `deriving(PartialEq)`")
55 let eq = cx.expr_binary(span, ast::BiNe, self_f, other_f.clone());
57 cx.expr_binary(span, ast::BiOr, subexpr, eq)
59 cx.expr_bool(span, false),
60 |cx, span, _, _| cx.expr_bool(span, true),
65 ($name:expr, $f:ident) => { {
66 let inline = cx.meta_word(span, InternedString::new("inline"));
67 let attrs = vec!(cx.attribute(span, inline));
70 generics: LifetimeBounds::empty(),
71 explicit_self: borrowed_explicit_self(),
72 args: vec!(borrowed_self()),
73 ret_ty: Literal(Path::new(vec!("bool"))),
75 combine_substructure: combine_substructure(|a, b, c| {
82 let trait_def = TraitDef {
84 attributes: Vec::new(),
85 path: Path::new(vec!("std", "cmp", "PartialEq")),
86 additional_bounds: Vec::new(),
87 generics: LifetimeBounds::empty(),
93 trait_def.expand(cx, mitem, item, push)