]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/ext/deriving/cmp/totaleq.rs
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / libsyntax / ext / deriving / cmp / totaleq.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, Item, Expr};
12 use codemap::Span;
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;
18 use ptr::P;
19
20 pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
21 span: Span,
22 mitem: &MetaItem,
23 item: &Item,
24 push: F) where
25 F: FnOnce(P<Item>),
26 {
27 fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
28 cs_same_method(|cx, span, exprs| {
29 // create `a.<method>(); b.<method>(); c.<method>(); ...`
30 // (where method is `assert_receiver_is_total_eq`)
31 let stmts = exprs.into_iter().map(|e| cx.stmt_expr(e)).collect();
32 let block = cx.block(span, stmts, None);
33 cx.expr_block(block)
34 },
35 Box::new(|cx, sp, _, _| {
36 cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
37 cx,
38 span,
39 substr)
40 }
41
42 let inline = cx.meta_word(span, InternedString::new("inline"));
43 let hidden = cx.meta_word(span, InternedString::new("hidden"));
44 let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden));
45 let attrs = vec!(cx.attribute(span, inline),
46 cx.attribute(span, doc));
47 let trait_def = TraitDef {
48 span: span,
49 attributes: Vec::new(),
50 path: path_std!(cx, core::cmp::Eq),
51 additional_bounds: Vec::new(),
52 generics: LifetimeBounds::empty(),
53 methods: vec!(
54 MethodDef {
55 name: "assert_receiver_is_total_eq",
56 generics: LifetimeBounds::empty(),
57 explicit_self: borrowed_explicit_self(),
58 args: vec!(),
59 ret_ty: nil_ty(),
60 attributes: attrs,
61 combine_substructure: combine_substructure(Box::new(|a, b, c| {
62 cs_total_eq_assert(a, b, c)
63 }))
64 }
65 ),
66 associated_types: Vec::new(),
67 };
68 trait_def.expand(cx, mitem, item, push)
69 }