1 // Copyright 2012-2015 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.
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.
11 //! The compiler code necessary to implement the `#[derive]` extensions.
13 //! FIXME (#2810): hygiene. Search for "__" strings (in other files too). We also assume "extra" is
14 //! the standard library, and "std" is the core library.
16 use ast
::{MetaItem, MetaWord}
;
17 use attr
::AttrMetaMethods
;
18 use ext
::base
::{ExtCtxt, SyntaxEnv, MultiDecorator, MultiItemDecorator, MultiModifier, Annotatable}
;
19 use ext
::build
::AstBuilder
;
22 use parse
::token
::{intern, intern_and_get_ident}
;
24 macro_rules
! pathvec
{
26 vec
![ $
( stringify
!($x
) ),+ ]
32 ::ext
::deriving
::generic
::ty
::Path
::new( pathvec
!( $
($x
)* ) )
36 macro_rules
! path_local
{
38 ::ext
::deriving
::generic
::ty
::Path
::new_local(stringify
!($x
))
42 macro_rules
! pathvec_std
{
43 ($cx
:expr
, $first
:ident
:: $
($rest
:ident
)::+) => (
45 pathvec
!(std
:: $
($rest
)::+)
47 pathvec
!($first
:: $
($rest
)::+)
52 macro_rules
! path_std
{
54 ::ext
::deriving
::generic
::ty
::Path
::new( pathvec_std
!( $
($x
)* ) )
67 #[path="cmp/partial_eq.rs"]
71 #[path="cmp/partial_ord.rs"]
79 fn expand_derive(cx
: &mut ExtCtxt
,
82 annotatable
: Annotatable
)
84 annotatable
.map_item_or(|item
| {
86 if mitem
.value_str().is_some() {
87 cx
.span_err(mitem
.span
, "unexpected value in `derive`");
90 let traits
= mitem
.meta_item_list().unwrap_or(&[]);
91 if traits
.is_empty() {
92 cx
.span_warn(mitem
.span
, "empty trait list in `derive`");
95 for titem
in traits
.iter().rev() {
96 let tname
= match titem
.node
{
97 MetaWord(ref tname
) => tname
,
99 cx
.span_err(titem
.span
, "malformed `derive` entry");
104 if !(is_builtin_trait(tname
) || cx
.ecfg
.enable_custom_derive()) {
105 feature_gate
::emit_feature_err(&cx
.parse_sess
.span_diagnostic
,
108 feature_gate
::EXPLAIN_CUSTOM_DERIVE
);
112 // #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
113 item
.attrs
.push(cx
.attribute(titem
.span
, cx
.meta_word(titem
.span
,
114 intern_and_get_ident(&format
!("derive_{}", tname
)))));
120 cx
.span_err(span
, "`derive` can only be applied to items");
125 macro_rules
! derive_traits
{
126 ($
( $name
:expr
=> $func
:path
, )+) => {
127 pub fn register_all(env
: &mut SyntaxEnv
) {
128 // Define the #[derive_*] extensions.
130 struct DeriveExtension
;
132 impl MultiItemDecorator
for DeriveExtension
{
137 annotatable
: &Annotatable
,
138 push
: &mut FnMut(Annotatable
)) {
139 warn_if_deprecated(ecx
, sp
, $name
);
140 $
func(ecx
, sp
, mitem
, annotatable
, push
);
144 env
.insert(intern(concat
!("derive_", $name
)),
145 MultiDecorator(Box
::new(DeriveExtension
)));
148 env
.insert(intern("derive"),
149 MultiModifier(Box
::new(expand_derive
)));
152 fn is_builtin_trait(name
: &str) -> bool
{
154 $
( $name
)|+ => true,
162 "Clone" => clone
::expand_deriving_clone
,
164 "Hash" => hash
::expand_deriving_hash
,
166 "RustcEncodable" => encodable
::expand_deriving_rustc_encodable
,
168 "RustcDecodable" => decodable
::expand_deriving_rustc_decodable
,
170 "PartialEq" => partial_eq
::expand_deriving_partial_eq
,
171 "Eq" => eq
::expand_deriving_eq
,
172 "PartialOrd" => partial_ord
::expand_deriving_partial_ord
,
173 "Ord" => ord
::expand_deriving_ord
,
175 "Debug" => show
::expand_deriving_show
,
177 "Default" => default::expand_deriving_default
,
179 "FromPrimitive" => primitive
::expand_deriving_from_primitive
,
181 "Send" => bounds
::expand_deriving_unsafe_bound
,
182 "Sync" => bounds
::expand_deriving_unsafe_bound
,
183 "Copy" => bounds
::expand_deriving_copy
,
186 "Show" => show
::expand_deriving_show
,
187 "Encodable" => encodable
::expand_deriving_encodable
,
188 "Decodable" => decodable
::expand_deriving_decodable
,
191 #[inline] // because `name` is a compile-time constant
192 fn warn_if_deprecated(ecx
: &mut ExtCtxt
, sp
: Span
, name
: &str) {
193 if let Some(replacement
) = match name
{
194 "Show" => Some("Debug"),
195 "Encodable" => Some("RustcEncodable"),
196 "Decodable" => Some("RustcDecodable"),
199 ecx
.span_warn(sp
, &format
!("derive({}) is deprecated in favor of derive({})",