]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/hash.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libsyntax / ext / deriving / hash.rs
1 // Copyright 2014 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, MutMutable};
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 ptr::P;
18
19 pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
20                                span: Span,
21                                mitem: &MetaItem,
22                                item: &Item,
23                                push: F) where
24     F: FnOnce(P<Item>),
25 {
26
27     let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None,
28                           vec!(), true);
29     let arg = Path::new_local("__H");
30     let hash_trait_def = TraitDef {
31         span: span,
32         attributes: Vec::new(),
33         path: path,
34         additional_bounds: Vec::new(),
35         generics: LifetimeBounds::empty(),
36         methods: vec!(
37             MethodDef {
38                 name: "hash",
39                 generics: LifetimeBounds {
40                     lifetimes: Vec::new(),
41                     bounds: vec![("__H",
42                                   vec![path_std!(cx, core::hash::Hasher)])],
43                 },
44                 explicit_self: borrowed_explicit_self(),
45                 args: vec!(Ptr(box Literal(arg), Borrowed(None, MutMutable))),
46                 ret_ty: nil_ty(),
47                 attributes: vec![],
48                 combine_substructure: combine_substructure(box |a, b, c| {
49                     hash_substructure(a, b, c)
50                 })
51             }
52         ),
53         associated_types: Vec::new(),
54     };
55
56     hash_trait_def.expand(cx, mitem, item, push);
57 }
58
59 fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
60     let state_expr = match substr.nonself_args {
61         [ref state_expr] => state_expr,
62         _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`")
63     };
64     let call_hash = |span, thing_expr| {
65         let hash_path = {
66             let strs = vec![
67                 cx.ident_of_std("core"),
68                 cx.ident_of("hash"),
69                 cx.ident_of("Hash"),
70                 cx.ident_of("hash"),
71             ];
72
73             cx.expr_path(cx.path_global(span, strs))
74         };
75         let ref_thing = cx.expr_addr_of(span, thing_expr);
76         let expr = cx.expr_call(span, hash_path, vec!(ref_thing, state_expr.clone()));
77         cx.stmt_expr(expr)
78     };
79     let mut stmts = Vec::new();
80
81     let fields = match *substr.fields {
82         Struct(ref fs) => fs,
83         EnumMatching(index, variant, ref fs) => {
84             // Determine the discriminant. We will feed this value to the byte
85             // iteration function.
86             let discriminant = match variant.node.disr_expr {
87                 Some(ref d) => d.clone(),
88                 None => cx.expr_usize(trait_span, index)
89             };
90
91             stmts.push(call_hash(trait_span, discriminant));
92
93             fs
94         }
95         _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
96     };
97
98     for &FieldInfo { ref self_, span, .. } in fields {
99         stmts.push(call_hash(span, self_.clone()));
100     }
101
102     cx.expr_block(cx.block(trait_span, stmts, None))
103 }