]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/hash.rs
Merge pull request #12308 from kballard/vim-nested-comments
[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
17 pub fn expand_deriving_hash(cx: &mut ExtCtxt,
18                             span: Span,
19                             mitem: @MetaItem,
20                             item: @Item,
21                             push: |@Item|) {
22
23     let hash_trait_def = TraitDef {
24         span: span,
25         attributes: ~[],
26         path: Path::new(~["std", "hash", "Hash"]),
27         additional_bounds: ~[],
28         generics: LifetimeBounds::empty(),
29         methods: ~[
30             MethodDef {
31                 name: "hash",
32                 generics: LifetimeBounds::empty(),
33                 explicit_self: borrowed_explicit_self(),
34                 args: ~[Ptr(~Literal(Path::new(~["std", "hash", "sip", "SipState"])),
35                             Borrowed(None, MutMutable))],
36                 ret_ty: nil_ty(),
37                 inline: true,
38                 const_nonmatching: false,
39                 combine_substructure: hash_substructure
40             }
41         ]
42     };
43
44     hash_trait_def.expand(cx, mitem, item, push);
45 }
46
47 fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
48     let state_expr = match substr.nonself_args {
49         [state_expr] => state_expr,
50         _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`")
51     };
52     let hash_ident = substr.method_ident;
53     let call_hash = |span, thing_expr| {
54         let expr = cx.expr_method_call(span, thing_expr, hash_ident, ~[state_expr]);
55         cx.stmt_expr(expr)
56     };
57     let mut stmts = ~[];
58
59     let fields = match *substr.fields {
60         Struct(ref fs) => fs,
61         EnumMatching(index, variant, ref fs) => {
62             // Determine the discriminant. We will feed this value to the byte
63             // iteration function.
64             let discriminant = match variant.node.disr_expr {
65                 Some(d) => d,
66                 None => cx.expr_uint(trait_span, index)
67             };
68
69             stmts.push(call_hash(trait_span, discriminant));
70
71             fs
72         }
73         _ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`")
74     };
75
76     for &FieldInfo { self_, span, .. } in fields.iter() {
77         stmts.push(call_hash(span, self_));
78     }
79
80     if stmts.len() == 0 {
81         cx.span_bug(trait_span, "#[deriving(Hash)] needs at least one field");
82     }
83
84     cx.expr_block(cx.block(trait_span, stmts, None))
85 }