]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use crate::deriving::generic::ty::*; |
2 | use crate::deriving::generic::*; | |
3 | use crate::deriving::{self, path_std, pathvec_std}; | |
4 | ||
74b04a01 | 5 | use rustc_ast::ptr::P; |
3dfed10e | 6 | use rustc_ast::{Expr, MetaItem, Mutability}; |
dfeec247 XL |
7 | use rustc_expand::base::{Annotatable, ExtCtxt}; |
8 | use rustc_span::symbol::sym; | |
9 | use rustc_span::Span; | |
dfeec247 XL |
10 | |
11 | pub fn expand_deriving_hash( | |
12 | cx: &mut ExtCtxt<'_>, | |
13 | span: Span, | |
14 | mitem: &MetaItem, | |
15 | item: &Annotatable, | |
16 | push: &mut dyn FnMut(Annotatable), | |
17 | ) { | |
3dfed10e | 18 | let path = Path::new_(pathvec_std!(hash::Hash), None, vec![], PathKind::Std); |
dfeec247 | 19 | |
3dfed10e | 20 | let typaram = sym::__H; |
dfeec247 XL |
21 | |
22 | let arg = Path::new_local(typaram); | |
23 | let hash_trait_def = TraitDef { | |
24 | span, | |
25 | attributes: Vec::new(), | |
26 | path, | |
27 | additional_bounds: Vec::new(), | |
3dfed10e | 28 | generics: Bounds::empty(), |
dfeec247 XL |
29 | is_unsafe: false, |
30 | supports_unions: false, | |
31 | methods: vec![MethodDef { | |
3dfed10e XL |
32 | name: sym::hash, |
33 | generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] }, | |
dfeec247 | 34 | explicit_self: borrowed_explicit_self(), |
3dfed10e | 35 | args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), sym::state)], |
dfeec247 XL |
36 | ret_ty: nil_ty(), |
37 | attributes: vec![], | |
38 | is_unsafe: false, | |
39 | unify_fieldless_variants: true, | |
40 | combine_substructure: combine_substructure(Box::new(|a, b, c| { | |
41 | hash_substructure(a, b, c) | |
42 | })), | |
43 | }], | |
44 | associated_types: Vec::new(), | |
45 | }; | |
46 | ||
47 | hash_trait_def.expand(cx, mitem, item, push); | |
48 | } | |
49 | ||
50 | fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> P<Expr> { | |
5869c6ff XL |
51 | let state_expr = match substr.nonself_args { |
52 | [o_f] => o_f, | |
dfeec247 XL |
53 | _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`"), |
54 | }; | |
55 | let call_hash = |span, thing_expr| { | |
56 | let hash_path = { | |
57 | let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]); | |
58 | ||
59 | cx.expr_path(cx.path_global(span, strs)) | |
60 | }; | |
61 | let ref_thing = cx.expr_addr_of(span, thing_expr); | |
62 | let expr = cx.expr_call(span, hash_path, vec![ref_thing, state_expr.clone()]); | |
63 | cx.stmt_expr(expr) | |
64 | }; | |
65 | let mut stmts = Vec::new(); | |
66 | ||
5869c6ff XL |
67 | let fields = match substr.fields { |
68 | Struct(_, fs) | EnumMatching(_, 1, .., fs) => fs, | |
69 | EnumMatching(.., fs) => { | |
dfeec247 XL |
70 | let variant_value = deriving::call_intrinsic( |
71 | cx, | |
72 | trait_span, | |
3dfed10e | 73 | sym::discriminant_value, |
dfeec247 XL |
74 | vec![cx.expr_self(trait_span)], |
75 | ); | |
76 | ||
77 | stmts.push(call_hash(trait_span, variant_value)); | |
78 | ||
79 | fs | |
80 | } | |
81 | _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"), | |
82 | }; | |
83 | ||
84 | stmts.extend( | |
85 | fields.iter().map(|FieldInfo { ref self_, span, .. }| call_hash(*span, self_.clone())), | |
86 | ); | |
87 | ||
88 | cx.expr_block(cx.block(trait_span, stmts)) | |
89 | } |