2019-02-04 21:49:54 +09:00
|
|
|
use crate::deriving::generic::ty::*;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::deriving::generic::*;
|
|
|
|
use crate::deriving::{self, path_std, pathvec_std};
|
2015-12-10 23:23:14 +09:00
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ast::{Expr, MetaItem, Mutability};
|
|
|
|
use rustc_ast::ptr::P;
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn expand_deriving_hash(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
|
|
|
item: &Annotatable,
|
|
|
|
push: &mut dyn FnMut(Annotatable),
|
|
|
|
) {
|
2017-12-06 10:50:55 -08:00
|
|
|
let path = Path::new_(pathvec_std!(cx, hash::Hash), None, vec![], PathKind::Std);
|
2016-03-08 13:24:28 -05:00
|
|
|
|
2019-08-11 14:16:12 +01:00
|
|
|
let typaram = "__H";
|
2016-03-08 13:24:28 -05:00
|
|
|
|
|
|
|
let arg = Path::new_local(typaram);
|
2014-02-21 21:33:23 -08:00
|
|
|
let hash_trait_def = TraitDef {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2014-02-28 13:09:09 -08:00
|
|
|
attributes: Vec::new(),
|
2017-08-06 22:54:09 -07:00
|
|
|
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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
name: "hash",
|
|
|
|
generics: LifetimeBounds {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
bounds: vec![(typaram, vec![path_std!(cx, hash::Hasher)])],
|
|
|
|
},
|
|
|
|
explicit_self: borrowed_explicit_self(),
|
|
|
|
args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), "state")],
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> P<Expr> {
|
2019-06-08 10:49:46 +02:00
|
|
|
let state_expr = match &substr.nonself_args {
|
|
|
|
&[o_f] => o_f,
|
2019-12-22 17:42:04 -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 = {
|
2019-05-22 14:41:15 +10:00
|
|
|
let strs = cx.std_path(&[sym::hash, sym::Hash, sym::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 {
|
2017-06-16 22:59:20 +03:00
|
|
|
Struct(_, ref fs) | EnumMatching(_, 1, .., ref fs) => fs,
|
2016-08-26 19:23:42 +03:00
|
|
|
EnumMatching(.., ref fs) => {
|
2019-12-22 17:42:04 -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
|
|
|
};
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
stmts.extend(
|
|
|
|
fields.iter().map(|FieldInfo { ref self_, span, .. }| 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
|
|
|
}
|