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
::{Item, MetaItem, MetaWord}
;
17 use attr
::AttrMetaMethods
;
18 use ext
::base
::{ExtCtxt, SyntaxEnv, Decorator, ItemDecorator, Modifier}
;
19 use ext
::build
::AstBuilder
;
22 use parse
::token
::{intern, intern_and_get_ident}
;
25 macro_rules
! pathvec
{
27 vec
![ $
( stringify
!($x
) ),+ ]
33 ::ext
::deriving
::generic
::ty
::Path
::new( pathvec
!( $
($x
)* ) )
37 macro_rules
! path_local
{
39 ::ext
::deriving
::generic
::ty
::Path
::new_local(stringify
!($x
))
43 macro_rules
! pathvec_std
{
44 ($cx
:expr
, $first
:ident
:: $
($rest
:ident
)::+) => (
46 pathvec
!(std
:: $
($rest
)::+)
48 pathvec
!($first
:: $
($rest
)::+)
53 macro_rules
! path_std
{
55 ::ext
::deriving
::generic
::ty
::Path
::new( pathvec_std
!( $
($x
)* ) )
70 #[path="cmp/totaleq.rs"]
74 #[path="cmp/totalord.rs"]
80 fn expand_derive(cx
: &mut ExtCtxt
,
83 item
: P
<Item
>) -> P
<Item
> {
85 if mitem
.value_str().is_some() {
86 cx
.span_err(mitem
.span
, "unexpected value in `derive`");
89 let traits
= mitem
.meta_item_list().unwrap_or(&[]);
90 if traits
.is_empty() {
91 cx
.span_warn(mitem
.span
, "empty trait list in `derive`");
94 for titem
in traits
.iter().rev() {
95 let tname
= match titem
.node
{
96 MetaWord(ref tname
) => tname
,
98 cx
.span_err(titem
.span
, "malformed `derive` entry");
103 if !(is_builtin_trait(tname
) || cx
.ecfg
.enable_custom_derive()) {
104 feature_gate
::emit_feature_err(&cx
.parse_sess
.span_diagnostic
,
107 feature_gate
::EXPLAIN_CUSTOM_DERIVE
);
111 // #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
112 item
.attrs
.push(cx
.attribute(titem
.span
, cx
.meta_word(titem
.span
,
113 intern_and_get_ident(&format
!("derive_{}", tname
)))));
120 macro_rules
! derive_traits
{
121 ($
( $name
:expr
=> $func
:path
, )*) => {
122 pub fn register_all(env
: &mut SyntaxEnv
) {
123 // Define the #[derive_*] extensions.
125 struct DeriveExtension
;
127 impl ItemDecorator
for DeriveExtension
{
133 push
: &mut FnMut(P
<Item
>)) {
134 warn_if_deprecated(ecx
, sp
, $name
);
135 $
func(ecx
, sp
, mitem
, item
, |i
| push(i
));
139 env
.insert(intern(concat
!("derive_", $name
)),
140 Decorator(Box
::new(DeriveExtension
)));
143 env
.insert(intern("derive"),
144 Modifier(Box
::new(expand_derive
)));
147 fn is_builtin_trait(name
: &str) -> bool
{
149 $
( $name
)|* => true,
157 "Clone" => clone
::expand_deriving_clone
,
159 "Hash" => hash
::expand_deriving_hash
,
161 "RustcEncodable" => encodable
::expand_deriving_rustc_encodable
,
163 "RustcDecodable" => decodable
::expand_deriving_rustc_decodable
,
165 "PartialEq" => eq
::expand_deriving_eq
,
166 "Eq" => totaleq
::expand_deriving_totaleq
,
167 "PartialOrd" => ord
::expand_deriving_ord
,
168 "Ord" => totalord
::expand_deriving_totalord
,
170 "Debug" => show
::expand_deriving_show
,
172 "Default" => default::expand_deriving_default
,
174 "FromPrimitive" => primitive
::expand_deriving_from_primitive
,
176 "Send" => bounds
::expand_deriving_unsafe_bound
,
177 "Sync" => bounds
::expand_deriving_unsafe_bound
,
178 "Copy" => bounds
::expand_deriving_copy
,
181 "Show" => show
::expand_deriving_show
,
182 "Encodable" => encodable
::expand_deriving_encodable
,
183 "Decodable" => decodable
::expand_deriving_decodable
,
186 #[inline] // because `name` is a compile-time constant
187 fn warn_if_deprecated(ecx
: &mut ExtCtxt
, sp
: Span
, name
: &str) {
188 if let Some(replacement
) = match name
{
189 "Show" => Some("Debug"),
190 "Encodable" => Some("RustcEncodable"),
191 "Decodable" => Some("RustcDecodable"),
194 ecx
.span_warn(sp
, &format
!("derive({}) is deprecated in favor of derive({})",