]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/hash.rs
doc/guide-ffi: A few minor typo/language fixes
[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;
12 use ast::{MetaItem, Item, Expr, MutMutable};
13 use codemap::Span;
14 use ext::base::ExtCtxt;
15 use ext::build::AstBuilder;
16 use ext::deriving::generic::*;
17 use ext::deriving::generic::ty::*;
18 use parse::token::InternedString;
19
20 use std::gc::Gc;
21
22 pub fn expand_deriving_hash(cx: &mut ExtCtxt,
23                             span: Span,
24                             mitem: Gc<MetaItem>,
25                             item: Gc<Item>,
26                             push: |Gc<Item>|) {
27
28     let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter {
29         (Path::new_(vec!("std", "hash", "Hash"), None,
30                     vec!(box Literal(Path::new_local("__S"))), true),
31          LifetimeBounds {
32              lifetimes: Vec::new(),
33              bounds: vec!(("__S", ast::StaticSize,
34                            vec!(Path::new(vec!("std", "hash", "Writer"))))),
35          },
36          Path::new_local("__S"))
37     } else {
38         (Path::new(vec!("std", "hash", "Hash")),
39          LifetimeBounds::empty(),
40          Path::new(vec!("std", "hash", "sip", "SipState")))
41     };
42     let inline = cx.meta_word(span, InternedString::new("inline"));
43     let attrs = vec!(cx.attribute(span, inline));
44     let hash_trait_def = TraitDef {
45         span: span,
46         attributes: Vec::new(),
47         path: path,
48         additional_bounds: Vec::new(),
49         generics: generics,
50         methods: vec!(
51             MethodDef {
52                 name: "hash",
53                 generics: LifetimeBounds::empty(),
54                 explicit_self: borrowed_explicit_self(),
55                 args: vec!(Ptr(box Literal(args), Borrowed(None, MutMutable))),
56                 ret_ty: nil_ty(),
57                 attributes: attrs,
58                 const_nonmatching: false,
59                 combine_substructure: combine_substructure(|a, b, c| {
60                     hash_substructure(a, b, c)
61                 })
62             }
63         )
64     };
65
66     hash_trait_def.expand(cx, mitem, item, push);
67 }
68
69 fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span,
70                      substr: &Substructure) -> Gc<Expr> {
71     let state_expr = match substr.nonself_args {
72         [state_expr] => state_expr,
73         _ => cx.span_bug(trait_span, "incorrect number of arguments in `deriving(Hash)`")
74     };
75     let hash_ident = substr.method_ident;
76     let call_hash = |span, thing_expr| {
77         let expr = cx.expr_method_call(span, thing_expr, hash_ident, vec!(state_expr));
78         cx.stmt_expr(expr)
79     };
80     let mut stmts = Vec::new();
81
82     let fields = match *substr.fields {
83         Struct(ref fs) => fs,
84         EnumMatching(index, variant, ref fs) => {
85             // Determine the discriminant. We will feed this value to the byte
86             // iteration function.
87             let discriminant = match variant.node.disr_expr {
88                 Some(d) => d,
89                 None => cx.expr_uint(trait_span, index)
90             };
91
92             stmts.push(call_hash(trait_span, discriminant));
93
94             fs
95         }
96         _ => cx.span_bug(trait_span, "impossible substructure in `deriving(Hash)`")
97     };
98
99     for &FieldInfo { self_, span, .. } in fields.iter() {
100         stmts.push(call_hash(span, self_));
101     }
102
103     if stmts.len() == 0 {
104         cx.span_bug(trait_span, "#[deriving(Hash)] needs at least one field");
105     }
106
107     cx.expr_block(cx.block(trait_span, stmts, None))
108 }