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