]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
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 | ||
d9579d0f | 11 | use ast::{MetaItem, Expr}; |
1a4d82fc | 12 | use codemap::Span; |
d9579d0f | 13 | use ext::base::{ExtCtxt, Annotatable}; |
970d7e83 LB |
14 | use ext::build::AstBuilder; |
15 | use ext::deriving::generic::*; | |
1a4d82fc JJ |
16 | use ext::deriving::generic::ty::*; |
17 | use parse::token::InternedString; | |
18 | use ptr::P; | |
970d7e83 | 19 | |
d9579d0f AL |
20 | pub fn expand_deriving_eq(cx: &mut ExtCtxt, |
21 | span: Span, | |
22 | mitem: &MetaItem, | |
62682a34 | 23 | item: &Annotatable, |
d9579d0f | 24 | push: &mut FnMut(Annotatable)) |
1a4d82fc | 25 | { |
d9579d0f AL |
26 | fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { |
27 | cs_same_method( | |
28 | |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) | |
1a4d82fc | 34 | }, |
d9579d0f AL |
35 | Box::new(|cx, sp, _, _| { |
36 | cx.span_bug(sp, "non matching enums in derive(Eq)?") }), | |
37 | cx, | |
38 | span, | |
39 | substr | |
40 | ) | |
970d7e83 LB |
41 | } |
42 | ||
d9579d0f AL |
43 | let inline = cx.meta_word(span, InternedString::new("inline")); |
44 | let hidden = cx.meta_word(span, InternedString::new("hidden")); | |
45 | let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden)); | |
46 | let attrs = vec!(cx.attribute(span, inline), | |
47 | cx.attribute(span, doc)); | |
48 | let trait_def = TraitDef { | |
49 | span: span, | |
50 | attributes: Vec::new(), | |
51 | path: path_std!(cx, core::cmp::Eq), | |
52 | additional_bounds: Vec::new(), | |
53 | generics: LifetimeBounds::empty(), | |
54 | methods: vec!( | |
970d7e83 | 55 | MethodDef { |
d9579d0f | 56 | name: "assert_receiver_is_total_eq", |
970d7e83 LB |
57 | generics: LifetimeBounds::empty(), |
58 | explicit_self: borrowed_explicit_self(), | |
d9579d0f AL |
59 | args: vec!(), |
60 | ret_ty: nil_ty(), | |
1a4d82fc | 61 | attributes: attrs, |
62682a34 | 62 | is_unsafe: false, |
c34b1796 | 63 | combine_substructure: combine_substructure(Box::new(|a, b, c| { |
d9579d0f | 64 | cs_total_eq_assert(a, b, c) |
c34b1796 | 65 | })) |
1a4d82fc | 66 | } |
85aaf69f SL |
67 | ), |
68 | associated_types: Vec::new(), | |
970d7e83 | 69 | }; |
62682a34 | 70 | trait_def.expand(cx, mitem, item, push) |
970d7e83 | 71 | } |