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