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.
|
|
|
|
|
2016-03-08 13:24:28 -05:00
|
|
|
use deriving;
|
2015-12-10 23:23:14 +09:00
|
|
|
use deriving::generic::*;
|
|
|
|
use deriving::generic::ty::*;
|
|
|
|
|
2016-07-19 23:02:06 +05:30
|
|
|
use syntax::ast::{Expr, MetaItem, Mutability};
|
|
|
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
use syntax::ptr::P;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2015-03-26 18:07:49 -07:00
|
|
|
pub fn expand_deriving_hash(cx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2016-07-19 23:02:06 +05:30
|
|
|
push: &mut FnMut(Annotatable)) {
|
2014-02-21 21:33:23 -08:00
|
|
|
|
2016-07-19 23:02:06 +05:30
|
|
|
let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None, vec![], true);
|
2016-03-08 13:24:28 -05:00
|
|
|
|
|
|
|
let typaram = &*deriving::hygienic_type_parameter(item, "__H");
|
|
|
|
|
|
|
|
let arg = Path::new_local(typaram);
|
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(),
|
2015-02-17 20:48:07 -08:00
|
|
|
generics: LifetimeBounds::empty(),
|
2015-08-29 14:50:05 -04:00
|
|
|
is_unsafe: false,
|
2016-08-29 11:14:25 +00:00
|
|
|
supports_unions: false,
|
2016-07-19 23:02:06 +05:30
|
|
|
methods: vec![MethodDef {
|
|
|
|
name: "hash",
|
|
|
|
generics: LifetimeBounds {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
bounds: vec![(typaram, vec![path_std!(cx, core::hash::Hasher)])],
|
|
|
|
},
|
|
|
|
explicit_self: borrowed_explicit_self(),
|
|
|
|
args: vec![Ptr(Box::new(Literal(arg)),
|
|
|
|
Borrowed(None, Mutability::Mutable))],
|
|
|
|
ret_ty: nil_ty(),
|
|
|
|
attributes: vec![],
|
|
|
|
is_unsafe: false,
|
|
|
|
unify_fieldless_variants: true,
|
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
|
|
|
hash_substructure(a, b, c)
|
|
|
|
})),
|
|
|
|
}],
|
2015-01-25 00:29:24 -05:00
|
|
|
associated_types: Vec::new(),
|
2014-02-21 21:33:23 -08:00
|
|
|
};
|
|
|
|
|
2015-05-22 21:10:14 +05:30
|
|
|
hash_trait_def.expand(cx, mitem, item, push);
|
2014-02-21 21:33:23 -08:00
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
2015-04-15 22:12:12 -07:00
|
|
|
let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) {
|
|
|
|
(1, Some(o_f)) => o_f,
|
2016-07-19 23:02:06 +05:30
|
|
|
_ => {
|
|
|
|
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 = {
|
2015-07-29 17:01:14 -07:00
|
|
|
let strs = cx.std_path(&["hash", "Hash", "hash"]);
|
2015-01-14 20:03:17 -05:00
|
|
|
|
|
|
|
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);
|
2016-07-19 23:02:06 +05:30
|
|
|
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 {
|
2016-02-22 21:24:32 +03:00
|
|
|
Struct(_, ref fs) => fs,
|
2016-03-07 13:05:12 -05:00
|
|
|
EnumMatching(_, _, ref fs) => {
|
2016-03-10 00:31:19 -05:00
|
|
|
let variant_value = deriving::call_intrinsic(cx,
|
|
|
|
trait_span,
|
|
|
|
"discriminant_value",
|
|
|
|
vec![cx.expr_self(trait_span)]);
|
2014-02-21 21:33:23 -08:00
|
|
|
|
2016-03-07 13:05:12 -05:00
|
|
|
stmts.push(call_hash(trait_span, variant_value));
|
2014-02-21 21:33:23 -08:00
|
|
|
|
|
|
|
fs
|
|
|
|
}
|
2016-07-19 23:02:06 +05:30
|
|
|
_ => 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
|
|
|
}
|
|
|
|
|
2016-06-23 09:51:18 +00:00
|
|
|
cx.expr_block(cx.block(trait_span, stmts))
|
2014-02-21 21:33:23 -08:00
|
|
|
}
|