]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/hash.rs
auto merge of #13686 : alexcrichton/rust/issue-12224, r=nikomatsakis
[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;
12 use ast::{MetaItem, Item, Expr, MutMutable};
13 use codemap::Span;
14 use ext::base::ExtCtxt;
15 use ext::build::AstBuilder;
16 use ext::deriving::generic::*;
17
18 pub fn expand_deriving_hash(cx: &mut ExtCtxt,
19                             span: Span,
20                             mitem: @MetaItem,
21                             item: @Item,
22                             push: |@Item|) {
23
24     let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter {
25         (Path::new_(vec!("std", "hash", "Hash"), None,
26                     vec!(~Literal(Path::new_local("__S"))), true),
27          LifetimeBounds {
28              lifetimes: Vec::new(),
29              bounds: vec!(("__S", ast::StaticSize, vec!(Path::new(vec!("std", "io", "Writer"))))),
30          },
31          Path::new_local("__S"))
32     } else {
33         (Path::new(vec!("std", "hash", "Hash")),
34          LifetimeBounds::empty(),
35          Path::new(vec!("std", "hash", "sip", "SipState")))
36     };
37     let hash_trait_def = TraitDef {
38         span: span,
39         attributes: Vec::new(),
40         path: path,
41         additional_bounds: Vec::new(),
42         generics: generics,
43         methods: vec!(
44             MethodDef {
45                 name: "hash",
46                 generics: LifetimeBounds::empty(),
47                 explicit_self: borrowed_explicit_self(),
48                 args: vec!(Ptr(~Literal(args), Borrowed(None, MutMutable))),
49                 ret_ty: nil_ty(),
50                 inline: true,
51                 const_nonmatching: false,
52                 combine_substructure: combine_substructure(|a, b, c| {
53                     hash_substructure(a, b, c)
54                 })
55             }
56         )
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) -> @Expr {
63     let state_expr = match substr.nonself_args {
64         [state_expr] => state_expr,
65         _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`")
66     };
67     let hash_ident = substr.method_ident;
68     let call_hash = |span, thing_expr| {
69         let expr = cx.expr_method_call(span, thing_expr, hash_ident, vec!(state_expr));
70         cx.stmt_expr(expr)
71     };
72     let mut stmts = Vec::new();
73
74     let fields = match *substr.fields {
75         Struct(ref fs) => fs,
76         EnumMatching(index, variant, ref fs) => {
77             // Determine the discriminant. We will feed this value to the byte
78             // iteration function.
79             let discriminant = match variant.node.disr_expr {
80                 Some(d) => d,
81                 None => cx.expr_uint(trait_span, index)
82             };
83
84             stmts.push(call_hash(trait_span, discriminant));
85
86             fs
87         }
88         _ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`")
89     };
90
91     for &FieldInfo { self_, span, .. } in fields.iter() {
92         stmts.push(call_hash(span, self_));
93     }
94
95     if stmts.len() == 0 {
96         cx.span_bug(trait_span, "#[deriving(Hash)] needs at least one field");
97     }
98
99     cx.expr_block(cx.block(trait_span, stmts, None))
100 }