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