]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/hash.rs
rollup merge of #20642: michaelwoerister/sane-source-locations-pt1
[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 use ext::deriving::generic::ty::*;
17 use parse::token::InternedString;
18 use ptr::P;
19
20 pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
21                                span: Span,
22                                mitem: &MetaItem,
23                                item: &Item,
24                                push: F) where
25     F: FnOnce(P<Item>),
26 {
27
28     let path = Path::new_(vec!("std", "hash", "Hash"), None,
29                           vec!(box Literal(Path::new_local("__S"))), true);
30     let generics = LifetimeBounds {
31         lifetimes: Vec::new(),
32         bounds: vec!(("__S",
33                       vec!(Path::new(vec!("std", "hash", "Writer")),
34                            Path::new(vec!("std", "hash", "Hasher"))))),
35     };
36     let args = Path::new_local("__S");
37     let inline = cx.meta_word(span, InternedString::new("inline"));
38     let attrs = vec!(cx.attribute(span, inline));
39     let hash_trait_def = TraitDef {
40         span: span,
41         attributes: Vec::new(),
42         path: path,
43         additional_bounds: Vec::new(),
44         generics: generics,
45         methods: vec!(
46             MethodDef {
47                 name: "hash",
48                 generics: LifetimeBounds::empty(),
49                 explicit_self: borrowed_explicit_self(),
50                 args: vec!(Ptr(box Literal(args), Borrowed(None, MutMutable))),
51                 ret_ty: nil_ty(),
52                 attributes: attrs,
53                 combine_substructure: combine_substructure(box |a, b, c| {
54                     hash_substructure(a, b, c)
55                 })
56             }
57         )
58     };
59
60     hash_trait_def.expand(cx, mitem, item, push);
61 }
62
63 fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
64     let state_expr = match substr.nonself_args {
65         [ref state_expr] => state_expr,
66         _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`")
67     };
68     let call_hash = |&: span, thing_expr| {
69         let hash_path = {
70             let strs = vec![
71                 cx.ident_of("std"),
72                 cx.ident_of("hash"),
73                 cx.ident_of("Hash"),
74                 cx.ident_of("hash"),
75             ];
76
77             cx.expr_path(cx.path_global(span, strs))
78         };
79         let ref_thing = cx.expr_addr_of(span, thing_expr);
80         let expr = cx.expr_call(span, hash_path, vec!(ref_thing, state_expr.clone()));
81         cx.stmt_expr(expr)
82     };
83     let mut stmts = Vec::new();
84
85     let fields = match *substr.fields {
86         Struct(ref fs) => fs,
87         EnumMatching(index, variant, ref fs) => {
88             // Determine the discriminant. We will feed this value to the byte
89             // iteration function.
90             let discriminant = match variant.node.disr_expr {
91                 Some(ref d) => d.clone(),
92                 None => cx.expr_usize(trait_span, index)
93             };
94
95             stmts.push(call_hash(trait_span, discriminant));
96
97             fs
98         }
99         _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
100     };
101
102     for &FieldInfo { ref self_, span, .. } in fields.iter() {
103         stmts.push(call_hash(span, self_.clone()));
104     }
105
106     cx.expr_block(cx.block(trait_span, stmts, None))
107 }