2015-03-06 21:15:54 +00:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-12-31 04:25:18 +00:00
|
|
|
//! The compiler code necessary to implement the `#[derive]` extensions.
|
2014-11-26 02:17:11 +00:00
|
|
|
//!
|
|
|
|
//! FIXME (#2810): hygiene. Search for "__" strings (in other files too). We also assume "extra" is
|
|
|
|
//! the standard library, and "std" is the core library.
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
use ast::{MetaItem, MetaWord};
|
2015-03-06 21:15:54 +00:00
|
|
|
use attr::AttrMetaMethods;
|
2015-04-28 05:34:39 +00:00
|
|
|
use ext::base::{ExtCtxt, SyntaxEnv, MultiDecorator, MultiItemDecorator, MultiModifier, Annotatable};
|
2015-03-06 21:15:54 +00:00
|
|
|
use ext::build::AstBuilder;
|
|
|
|
use feature_gate;
|
2013-08-31 16:13:04 +00:00
|
|
|
use codemap::Span;
|
2015-03-06 21:15:54 +00:00
|
|
|
use parse::token::{intern, intern_and_get_ident};
|
2014-05-16 07:16:13 +00:00
|
|
|
|
2014-09-07 20:58:41 +00:00
|
|
|
macro_rules! pathvec {
|
|
|
|
($($x:ident)::+) => (
|
|
|
|
vec![ $( stringify!($x) ),+ ]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! path {
|
|
|
|
($($x:tt)*) => (
|
|
|
|
::ext::deriving::generic::ty::Path::new( pathvec!( $($x)* ) )
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-02-05 07:19:07 +00:00
|
|
|
macro_rules! path_local {
|
|
|
|
($x:ident) => (
|
|
|
|
::ext::deriving::generic::ty::Path::new_local(stringify!($x))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-09-07 21:57:26 +00:00
|
|
|
macro_rules! pathvec_std {
|
|
|
|
($cx:expr, $first:ident :: $($rest:ident)::+) => (
|
|
|
|
if $cx.use_std {
|
|
|
|
pathvec!(std :: $($rest)::+)
|
|
|
|
} else {
|
|
|
|
pathvec!($first :: $($rest)::+)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! path_std {
|
|
|
|
($($x:tt)*) => (
|
|
|
|
::ext::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) )
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-04-30 09:26:50 +00:00
|
|
|
pub mod bounds;
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod clone;
|
2013-04-10 23:31:51 +00:00
|
|
|
pub mod encodable;
|
2013-04-09 01:53:39 +00:00
|
|
|
pub mod decodable;
|
2014-02-22 05:33:23 +00:00
|
|
|
pub mod hash;
|
2014-02-06 12:02:28 +00:00
|
|
|
pub mod show;
|
2013-09-12 04:51:13 +00:00
|
|
|
pub mod default;
|
2013-09-17 04:12:18 +00:00
|
|
|
pub mod primitive;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2015-04-02 16:14:53 +00:00
|
|
|
#[path="cmp/partial_eq.rs"]
|
|
|
|
pub mod partial_eq;
|
2013-03-30 14:58:05 +00:00
|
|
|
#[path="cmp/eq.rs"]
|
|
|
|
pub mod eq;
|
2015-04-02 16:14:53 +00:00
|
|
|
#[path="cmp/partial_ord.rs"]
|
|
|
|
pub mod partial_ord;
|
2013-03-30 14:58:05 +00:00
|
|
|
#[path="cmp/ord.rs"]
|
|
|
|
pub mod ord;
|
|
|
|
|
|
|
|
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod generic;
|
|
|
|
|
2015-03-06 21:15:54 +00:00
|
|
|
fn expand_derive(cx: &mut ExtCtxt,
|
2015-04-25 02:04:46 +00:00
|
|
|
span: Span,
|
2015-03-06 21:15:54 +00:00
|
|
|
mitem: &MetaItem,
|
2015-04-28 05:34:39 +00:00
|
|
|
annotatable: Annotatable)
|
|
|
|
-> Annotatable {
|
|
|
|
annotatable.map_item_or(|item| {
|
|
|
|
item.map(|mut item| {
|
|
|
|
if mitem.value_str().is_some() {
|
|
|
|
cx.span_err(mitem.span, "unexpected value in `derive`");
|
|
|
|
}
|
2015-03-06 21:15:54 +00:00
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
let traits = mitem.meta_item_list().unwrap_or(&[]);
|
|
|
|
if traits.is_empty() {
|
|
|
|
cx.span_warn(mitem.span, "empty trait list in `derive`");
|
|
|
|
}
|
2015-03-06 21:15:54 +00:00
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
for titem in traits.iter().rev() {
|
|
|
|
let tname = match titem.node {
|
|
|
|
MetaWord(ref tname) => tname,
|
|
|
|
_ => {
|
|
|
|
cx.span_err(titem.span, "malformed `derive` entry");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if !(is_builtin_trait(tname) || cx.ecfg.enable_custom_derive()) {
|
|
|
|
feature_gate::emit_feature_err(&cx.parse_sess.span_diagnostic,
|
|
|
|
"custom_derive",
|
|
|
|
titem.span,
|
|
|
|
feature_gate::EXPLAIN_CUSTOM_DERIVE);
|
2015-03-06 21:15:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
// #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
|
|
|
|
item.attrs.push(cx.attribute(titem.span, cx.meta_word(titem.span,
|
|
|
|
intern_and_get_ident(&format!("derive_{}", tname)))));
|
|
|
|
}
|
2015-03-06 21:15:54 +00:00
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
item
|
|
|
|
})
|
|
|
|
}, |a| {
|
|
|
|
cx.span_err(span, "`derive` can only be applied to items");
|
|
|
|
a
|
2015-03-06 21:15:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! derive_traits {
|
2015-03-27 01:07:49 +00:00
|
|
|
($( $name:expr => $func:path, )+) => {
|
2015-03-06 21:15:54 +00:00
|
|
|
pub fn register_all(env: &mut SyntaxEnv) {
|
|
|
|
// Define the #[derive_*] extensions.
|
|
|
|
$({
|
|
|
|
struct DeriveExtension;
|
|
|
|
|
2015-04-28 05:34:39 +00:00
|
|
|
impl MultiItemDecorator for DeriveExtension {
|
2015-03-06 21:15:54 +00:00
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
mitem: &MetaItem,
|
2015-04-28 05:34:39 +00:00
|
|
|
annotatable: Annotatable,
|
|
|
|
push: &mut FnMut(Annotatable)) {
|
2015-03-06 21:15:54 +00:00
|
|
|
warn_if_deprecated(ecx, sp, $name);
|
2015-04-28 05:34:39 +00:00
|
|
|
$func(ecx, sp, mitem, annotatable, push);
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 21:15:54 +00:00
|
|
|
|
|
|
|
env.insert(intern(concat!("derive_", $name)),
|
2015-04-28 05:34:39 +00:00
|
|
|
MultiDecorator(Box::new(DeriveExtension)));
|
2015-03-27 01:07:49 +00:00
|
|
|
})+
|
2015-03-06 21:15:54 +00:00
|
|
|
|
|
|
|
env.insert(intern("derive"),
|
2015-04-28 05:34:39 +00:00
|
|
|
MultiModifier(Box::new(expand_derive)));
|
2015-03-06 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_builtin_trait(name: &str) -> bool {
|
|
|
|
match name {
|
2015-03-27 01:07:49 +00:00
|
|
|
$( $name )|+ => true,
|
2015-03-06 21:15:54 +00:00
|
|
|
_ => false,
|
2014-02-13 07:53:52 +00:00
|
|
|
}
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 21:15:54 +00:00
|
|
|
|
|
|
|
derive_traits! {
|
|
|
|
"Clone" => clone::expand_deriving_clone,
|
|
|
|
|
|
|
|
"Hash" => hash::expand_deriving_hash,
|
|
|
|
|
|
|
|
"RustcEncodable" => encodable::expand_deriving_rustc_encodable,
|
|
|
|
|
|
|
|
"RustcDecodable" => decodable::expand_deriving_rustc_decodable,
|
|
|
|
|
2015-04-02 16:14:53 +00:00
|
|
|
"PartialEq" => partial_eq::expand_deriving_partial_eq,
|
|
|
|
"Eq" => eq::expand_deriving_eq,
|
|
|
|
"PartialOrd" => partial_ord::expand_deriving_partial_ord,
|
|
|
|
"Ord" => ord::expand_deriving_ord,
|
2015-03-06 21:15:54 +00:00
|
|
|
|
|
|
|
"Debug" => show::expand_deriving_show,
|
|
|
|
|
|
|
|
"Default" => default::expand_deriving_default,
|
|
|
|
|
|
|
|
"FromPrimitive" => primitive::expand_deriving_from_primitive,
|
|
|
|
|
|
|
|
"Send" => bounds::expand_deriving_unsafe_bound,
|
|
|
|
"Sync" => bounds::expand_deriving_unsafe_bound,
|
|
|
|
"Copy" => bounds::expand_deriving_copy,
|
|
|
|
|
|
|
|
// deprecated
|
|
|
|
"Show" => show::expand_deriving_show,
|
|
|
|
"Encodable" => encodable::expand_deriving_encodable,
|
|
|
|
"Decodable" => decodable::expand_deriving_decodable,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline] // because `name` is a compile-time constant
|
|
|
|
fn warn_if_deprecated(ecx: &mut ExtCtxt, sp: Span, name: &str) {
|
|
|
|
if let Some(replacement) = match name {
|
|
|
|
"Show" => Some("Debug"),
|
|
|
|
"Encodable" => Some("RustcEncodable"),
|
|
|
|
"Decodable" => Some("RustcDecodable"),
|
|
|
|
_ => None,
|
|
|
|
} {
|
|
|
|
ecx.span_warn(sp, &format!("derive({}) is deprecated in favor of derive({})",
|
|
|
|
name, replacement));
|
|
|
|
}
|
|
|
|
}
|