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 deriving::generic::*;
12 use deriving::generic::ty::*;
14 use syntax::ast::{MetaItem, Expr};
15 use syntax::ext::base::{ExtCtxt, Annotatable};
16 use syntax::ext::build::AstBuilder;
17 use syntax::parse::token::InternedString;
21 pub fn expand_deriving_eq(cx: &mut ExtCtxt,
25 push: &mut FnMut(Annotatable))
27 fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
30 // create `a.<method>(); b.<method>(); c.<method>(); ...`
31 // (where method is `assert_receiver_is_total_eq`)
32 let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
33 let block = cx.block(span, stmts);
36 Box::new(|cx, sp, _, _| {
37 cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
44 let inline = cx.meta_word(span, InternedString::new("inline"));
45 let hidden = cx.meta_word(span, InternedString::new("hidden"));
46 let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
47 let attrs = vec!(cx.attribute(span, inline),
48 cx.attribute(span, doc));
49 let trait_def = TraitDef {
51 attributes: Vec::new(),
52 path: path_std!(cx, core::cmp::Eq),
53 additional_bounds: Vec::new(),
54 generics: LifetimeBounds::empty(),
58 name: "assert_receiver_is_total_eq",
59 generics: LifetimeBounds::empty(),
60 explicit_self: borrowed_explicit_self(),
65 unify_fieldless_variants: true,
66 combine_substructure: combine_substructure(Box::new(|a, b, c| {
67 cs_total_eq_assert(a, b, c)
71 associated_types: Vec::new(),
73 trait_def.expand(cx, mitem, item, push)