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-03-07 13:05:12 -05:00
|
|
|
use syntax::ast::{self, MetaItem, Expr, Mutability};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use syntax::ext::base::{ExtCtxt, Annotatable};
|
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
use syntax::ptr::P;
|
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,
|
2015-04-28 17:34:39 +12:00
|
|
|
push: &mut FnMut(Annotatable))
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2014-02-21 21:33:23 -08:00
|
|
|
|
2014-09-07 14:57:26 -07:00
|
|
|
let path = Path::new_(pathvec_std!(cx, core::hash::Hash), None,
|
2015-02-17 20:48:07 -08:00
|
|
|
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,
|
2014-02-28 13:09:09 -08:00
|
|
|
methods: vec!(
|
2014-02-21 21:33:23 -08:00
|
|
|
MethodDef {
|
|
|
|
name: "hash",
|
2015-02-17 20:48:07 -08:00
|
|
|
generics: LifetimeBounds {
|
|
|
|
lifetimes: Vec::new(),
|
2016-03-08 13:24:28 -05:00
|
|
|
bounds: vec![(typaram,
|
2015-02-17 20:48:07 -08:00
|
|
|
vec![path_std!(cx, core::hash::Hasher)])],
|
|
|
|
},
|
2014-02-21 21:33:23 -08:00
|
|
|
explicit_self: borrowed_explicit_self(),
|
2016-02-09 17:44:47 +01:00
|
|
|
args: vec!(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mutable))),
|
2014-02-21 21:33:23 -08:00
|
|
|
ret_ty: nil_ty(),
|
2015-02-17 20:48:07 -08:00
|
|
|
attributes: vec![],
|
2015-05-17 11:28:19 +05:30
|
|
|
is_unsafe: false,
|
2015-02-15 09:52:21 +01:00
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
2014-04-21 23:25:18 -07:00
|
|
|
hash_substructure(a, b, c)
|
2015-02-15 09:52:21 +01:00
|
|
|
}))
|
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
|
|
|
};
|
|
|
|
|
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,
|
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 = {
|
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);
|
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 {
|
2016-02-22 21:24:32 +03:00
|
|
|
Struct(_, ref fs) => fs,
|
2016-03-07 13:05:12 -05:00
|
|
|
EnumMatching(_, _, ref fs) => {
|
|
|
|
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
|
|
|
|
let call = cx.expr_call_global(
|
|
|
|
trait_span, path, vec![cx.expr_self(trait_span)]);
|
|
|
|
let variant_value = cx.expr_block(P(ast::Block {
|
|
|
|
stmts: vec![],
|
|
|
|
expr: Some(call),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
|
|
|
span: 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
|
|
|
|
}
|
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))
|
|
|
|
}
|