2014-02-21 21:33:23 -08:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use ast::{MetaItem, Item, Expr, MutMutable};
|
|
|
|
use codemap::Span;
|
|
|
|
use ext::base::ExtCtxt;
|
|
|
|
use ext::build::AstBuilder;
|
|
|
|
use ext::deriving::generic::*;
|
2014-05-29 12:19:05 +09:00
|
|
|
use ext::deriving::generic::ty::*;
|
2014-04-23 22:43:45 +08:00
|
|
|
use parse::token::InternedString;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2014-12-08 13:28:32 -05:00
|
|
|
pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
|
|
|
item: &Item,
|
|
|
|
push: F) where
|
|
|
|
F: FnOnce(P<Item>),
|
|
|
|
{
|
2014-02-21 21:33:23 -08:00
|
|
|
|
2014-09-07 13:58:41 -07:00
|
|
|
let path = Path::new_(pathvec!(std::hash::Hash), None,
|
2015-01-05 17:07:16 +11:00
|
|
|
vec!(box Literal(Path::new_local("__S"))), true);
|
|
|
|
let generics = LifetimeBounds {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
bounds: vec!(("__S",
|
2014-09-07 13:58:41 -07:00
|
|
|
vec!(path!(std::hash::Writer),
|
|
|
|
path!(std::hash::Hasher)))),
|
2014-02-28 23:17:38 -08:00
|
|
|
};
|
2015-01-05 17:07:16 +11:00
|
|
|
let args = Path::new_local("__S");
|
2014-04-23 22:43:45 +08:00
|
|
|
let inline = cx.meta_word(span, InternedString::new("inline"));
|
|
|
|
let attrs = vec!(cx.attribute(span, inline));
|
2014-02-21 21:33:23 -08:00
|
|
|
let hash_trait_def = TraitDef {
|
|
|
|
span: span,
|
2014-02-28 13:09:09 -08:00
|
|
|
attributes: Vec::new(),
|
2014-02-28 23:17:38 -08:00
|
|
|
path: path,
|
2014-02-28 13:09:09 -08:00
|
|
|
additional_bounds: Vec::new(),
|
2014-02-28 23:17:38 -08:00
|
|
|
generics: generics,
|
2014-02-28 13:09:09 -08:00
|
|
|
methods: vec!(
|
2014-02-21 21:33:23 -08:00
|
|
|
MethodDef {
|
|
|
|
name: "hash",
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: borrowed_explicit_self(),
|
2014-04-25 01:08:02 -07:00
|
|
|
args: vec!(Ptr(box Literal(args), Borrowed(None, MutMutable))),
|
2014-02-21 21:33:23 -08:00
|
|
|
ret_ty: nil_ty(),
|
2014-04-23 22:43:45 +08:00
|
|
|
attributes: attrs,
|
2015-01-01 18:32:49 -05:00
|
|
|
combine_substructure: combine_substructure(box |a, b, c| {
|
2014-04-21 23:25:18 -07:00
|
|
|
hash_substructure(a, b, c)
|
|
|
|
})
|
2014-02-21 21:33:23 -08:00
|
|
|
}
|
2015-01-25 00:29:24 -05:00
|
|
|
),
|
|
|
|
associated_types: Vec::new(),
|
2014-02-21 21:33:23 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
hash_trait_def.expand(cx, mitem, item, push);
|
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
2014-02-21 21:33:23 -08:00
|
|
|
let state_expr = match substr.nonself_args {
|
2014-09-13 19:06:01 +03:00
|
|
|
[ref state_expr] => state_expr,
|
2015-01-14 18:56:17 -05:00
|
|
|
_ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`")
|
2014-02-21 21:33:23 -08:00
|
|
|
};
|
2015-02-01 12:44:15 -05:00
|
|
|
let call_hash = |span, thing_expr| {
|
2015-01-14 20:03:17 -05:00
|
|
|
let hash_path = {
|
|
|
|
let strs = vec![
|
|
|
|
cx.ident_of("std"),
|
|
|
|
cx.ident_of("hash"),
|
|
|
|
cx.ident_of("Hash"),
|
|
|
|
cx.ident_of("hash"),
|
|
|
|
];
|
|
|
|
|
|
|
|
cx.expr_path(cx.path_global(span, strs))
|
|
|
|
};
|
2015-01-14 18:22:16 -05:00
|
|
|
let ref_thing = cx.expr_addr_of(span, thing_expr);
|
2015-01-14 20:03:17 -05:00
|
|
|
let expr = cx.expr_call(span, hash_path, vec!(ref_thing, state_expr.clone()));
|
2014-02-21 21:33:23 -08:00
|
|
|
cx.stmt_expr(expr)
|
|
|
|
};
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut stmts = Vec::new();
|
2014-02-21 21:33:23 -08:00
|
|
|
|
|
|
|
let fields = match *substr.fields {
|
|
|
|
Struct(ref fs) => fs,
|
|
|
|
EnumMatching(index, variant, ref fs) => {
|
|
|
|
// Determine the discriminant. We will feed this value to the byte
|
|
|
|
// iteration function.
|
|
|
|
let discriminant = match variant.node.disr_expr {
|
2014-09-13 19:06:01 +03:00
|
|
|
Some(ref d) => d.clone(),
|
2015-01-17 23:49:08 +00:00
|
|
|
None => cx.expr_usize(trait_span, index)
|
2014-02-21 21:33:23 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
stmts.push(call_hash(trait_span, discriminant));
|
|
|
|
|
|
|
|
fs
|
|
|
|
}
|
2015-01-14 18:56:17 -05:00
|
|
|
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`")
|
2014-02-21 21:33:23 -08:00
|
|
|
};
|
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for &FieldInfo { ref self_, span, .. } in fields {
|
2014-09-13 19:06:01 +03:00
|
|
|
stmts.push(call_hash(span, self_.clone()));
|
2014-02-21 21:33:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cx.expr_block(cx.block(trait_span, stmts, None))
|
|
|
|
}
|