|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// force-host |
| 12 | + |
| 13 | +#![feature(plugin_registrar, rustc_private, item_like_imports)] |
| 14 | + |
| 15 | +extern crate syntax; |
| 16 | +extern crate syntax_ext; |
| 17 | +extern crate rustc_plugin; |
| 18 | + |
| 19 | +use syntax_ext::deriving; |
| 20 | +use deriving::generic::*; |
| 21 | +use deriving::generic::ty::*; |
| 22 | + |
| 23 | +use rustc_plugin::Registry; |
| 24 | +use syntax::ast::*; |
| 25 | +use syntax::codemap::Span; |
| 26 | +use syntax::ext::base::*; |
| 27 | +use syntax::ext::build::AstBuilder; |
| 28 | +use syntax::parse::token::{intern, InternedString}; |
| 29 | +use syntax::ptr::P; |
| 30 | + |
| 31 | +#[plugin_registrar] |
| 32 | +pub fn plugin_registrar(reg: &mut Registry) { |
| 33 | + reg.register_syntax_extension(intern("derive_CustomPartialEq"), |
| 34 | + MultiDecorator(Box::new(expand_deriving_partial_eq))); |
| 35 | +} |
| 36 | + |
| 37 | +fn expand_deriving_partial_eq(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Annotatable, |
| 38 | + push: &mut FnMut(Annotatable)) { |
| 39 | + // structures are equal if all fields are equal, and non equal, if |
| 40 | + // any fields are not equal or if the enum variants are different |
| 41 | + fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { |
| 42 | + cs_fold(true, |
| 43 | + |cx, span, subexpr, self_f, other_fs| { |
| 44 | + let other_f = (other_fs.len(), other_fs.get(0)).1.unwrap(); |
| 45 | + let eq = cx.expr_binary(span, BinOpKind::Eq, self_f, other_f.clone()); |
| 46 | + cx.expr_binary(span, BinOpKind::And, subexpr, eq) |
| 47 | + }, |
| 48 | + cx.expr_bool(span, true), |
| 49 | + Box::new(|cx, span, _, _| cx.expr_bool(span, false)), |
| 50 | + cx, |
| 51 | + span, |
| 52 | + substr) |
| 53 | + } |
| 54 | + |
| 55 | + let inline = cx.meta_word(span, InternedString::new("inline")); |
| 56 | + let attrs = vec!(cx.attribute(span, inline)); |
| 57 | + let methods = vec![MethodDef { |
| 58 | + name: "eq", |
| 59 | + generics: LifetimeBounds::empty(), |
| 60 | + explicit_self: borrowed_explicit_self(), |
| 61 | + args: vec!(borrowed_self()), |
| 62 | + ret_ty: Literal(deriving::generic::ty::Path::new_local("bool")), |
| 63 | + attributes: attrs, |
| 64 | + is_unsafe: false, |
| 65 | + unify_fieldless_variants: true, |
| 66 | + combine_substructure: combine_substructure(Box::new(cs_eq)), |
| 67 | + }]; |
| 68 | + |
| 69 | + let trait_def = TraitDef { |
| 70 | + span: span, |
| 71 | + attributes: Vec::new(), |
| 72 | + path: deriving::generic::ty::Path::new(vec!["std", "cmp", "PartialEq"]), |
| 73 | + additional_bounds: Vec::new(), |
| 74 | + generics: LifetimeBounds::empty(), |
| 75 | + is_unsafe: false, |
| 76 | + supports_unions: false, |
| 77 | + methods: methods, |
| 78 | + associated_types: Vec::new(), |
| 79 | + }; |
| 80 | + trait_def.expand(cx, mitem, item, push) |
| 81 | +} |
0 commit comments