]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/hash.rs
Rollup merge of #34732 - durka:patch-27, r=steveklabnik
[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 deriving;
12 use deriving::generic::*;
13 use deriving::generic::ty::*;
14
15 use syntax::ast::{Expr, MetaItem, Mutability};
16 use syntax::ext::base::{Annotatable, ExtCtxt};
17 use syntax::ext::build::AstBuilder;
18 use syntax::ptr::P;
19 use syntax_pos::Span;
20
21 pub fn expand_deriving_hash(cx: &mut ExtCtxt,
22                             span: Span,
23                             mitem: &MetaItem,
24                             item: &Annotatable,
25                             push: &mut FnMut(Annotatable)) {
26
27     let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None, vec![], true);
28
29     let typaram = &*deriving::hygienic_type_parameter(item, "__H");
30
31     let arg = Path::new_local(typaram);
32     let hash_trait_def = TraitDef {
33         span: span,
34         attributes: Vec::new(),
35         path: path,
36         additional_bounds: Vec::new(),
37         generics: LifetimeBounds::empty(),
38         is_unsafe: false,
39         methods: vec![MethodDef {
40                           name: "hash",
41                           generics: LifetimeBounds {
42                               lifetimes: Vec::new(),
43                               bounds: vec![(typaram, vec![path_std!(cx, core::hash::Hasher)])],
44                           },
45                           explicit_self: borrowed_explicit_self(),
46                           args: vec![Ptr(Box::new(Literal(arg)),
47                                          Borrowed(None, Mutability::Mutable))],
48                           ret_ty: nil_ty(),
49                           attributes: vec![],
50                           is_unsafe: false,
51                           unify_fieldless_variants: true,
52                           combine_substructure: combine_substructure(Box::new(|a, b, c| {
53                               hash_substructure(a, b, c)
54                           })),
55                       }],
56         associated_types: Vec::new(),
57     };
58
59     hash_trait_def.expand(cx, mitem, item, push);
60 }
61
62 fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
63     let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) {
64         (1, Some(o_f)) => o_f,
65         _ => {
66             cx.span_bug(trait_span,
67                         "incorrect number of arguments in `derive(Hash)`")
68         }
69     };
70     let call_hash = |span, thing_expr| {
71         let hash_path = {
72             let strs = cx.std_path(&["hash", "Hash", "hash"]);
73
74             cx.expr_path(cx.path_global(span, strs))
75         };
76         let ref_thing = cx.expr_addr_of(span, thing_expr);
77         let expr = cx.expr_call(span, hash_path, vec![ref_thing, state_expr.clone()]);
78         cx.stmt_expr(expr)
79     };
80     let mut stmts = Vec::new();
81
82     let fields = match *substr.fields {
83         Struct(_, ref fs) => fs,
84         EnumMatching(_, _, ref fs) => {
85             let variant_value = deriving::call_intrinsic(cx,
86                                                          trait_span,
87                                                          "discriminant_value",
88                                                          vec![cx.expr_self(trait_span)]);
89
90             stmts.push(call_hash(trait_span, variant_value));
91
92             fs
93         }
94         _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"),
95     };
96
97     for &FieldInfo { ref self_, span, .. } in fields {
98         stmts.push(call_hash(span, self_.clone()));
99     }
100
101     cx.expr_block(cx.block(trait_span, stmts))
102 }