]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/ext/deriving/mod.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / libsyntax / ext / deriving / mod.rs
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.
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 //! The compiler code necessary to implement the `#[derive]` extensions.
12 //!
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.
15
16 use ast::{MetaItem, MetaWord};
17 use attr::AttrMetaMethods;
18 use ext::base::{ExtCtxt, SyntaxEnv, MultiDecorator, MultiItemDecorator, MultiModifier, Annotatable};
19 use ext::build::AstBuilder;
20 use feature_gate;
21 use codemap::Span;
22 use parse::token::{intern, intern_and_get_ident};
23
24 macro_rules! pathvec {
25 ($($x:ident)::+) => (
26 vec![ $( stringify!($x) ),+ ]
27 )
28 }
29
30 macro_rules! path {
31 ($($x:tt)*) => (
32 ::ext::deriving::generic::ty::Path::new( pathvec!( $($x)* ) )
33 )
34 }
35
36 macro_rules! path_local {
37 ($x:ident) => (
38 ::ext::deriving::generic::ty::Path::new_local(stringify!($x))
39 )
40 }
41
42 macro_rules! pathvec_std {
43 ($cx:expr, $first:ident :: $($rest:ident)::+) => (
44 if $cx.use_std {
45 pathvec!(std :: $($rest)::+)
46 } else {
47 pathvec!($first :: $($rest)::+)
48 }
49 )
50 }
51
52 macro_rules! path_std {
53 ($($x:tt)*) => (
54 ::ext::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) )
55 )
56 }
57
58 pub mod bounds;
59 pub mod clone;
60 pub mod encodable;
61 pub mod decodable;
62 pub mod hash;
63 pub mod show;
64 pub mod default;
65 pub mod primitive;
66
67 #[path="cmp/partial_eq.rs"]
68 pub mod partial_eq;
69 #[path="cmp/eq.rs"]
70 pub mod eq;
71 #[path="cmp/partial_ord.rs"]
72 pub mod partial_ord;
73 #[path="cmp/ord.rs"]
74 pub mod ord;
75
76
77 pub mod generic;
78
79 fn expand_derive(cx: &mut ExtCtxt,
80 span: Span,
81 mitem: &MetaItem,
82 annotatable: Annotatable)
83 -> Annotatable {
84 annotatable.map_item_or(|item| {
85 item.map(|mut item| {
86 if mitem.value_str().is_some() {
87 cx.span_err(mitem.span, "unexpected value in `derive`");
88 }
89
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`");
93 }
94
95 for titem in traits.iter().rev() {
96 let tname = match titem.node {
97 MetaWord(ref tname) => tname,
98 _ => {
99 cx.span_err(titem.span, "malformed `derive` entry");
100 continue;
101 }
102 };
103
104 if !(is_builtin_trait(tname) || cx.ecfg.enable_custom_derive()) {
105 feature_gate::emit_feature_err(&cx.parse_sess.span_diagnostic,
106 "custom_derive",
107 titem.span,
108 feature_gate::EXPLAIN_CUSTOM_DERIVE);
109 continue;
110 }
111
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)))));
115 }
116
117 item
118 })
119 }, |a| {
120 cx.span_err(span, "`derive` can only be applied to items");
121 a
122 })
123 }
124
125 macro_rules! derive_traits {
126 ($( $name:expr => $func:path, )+) => {
127 pub fn register_all(env: &mut SyntaxEnv) {
128 // Define the #[derive_*] extensions.
129 $({
130 struct DeriveExtension;
131
132 impl MultiItemDecorator for DeriveExtension {
133 fn expand(&self,
134 ecx: &mut ExtCtxt,
135 sp: Span,
136 mitem: &MetaItem,
137 annotatable: &Annotatable,
138 push: &mut FnMut(Annotatable)) {
139 warn_if_deprecated(ecx, sp, $name);
140 $func(ecx, sp, mitem, annotatable, push);
141 }
142 }
143
144 env.insert(intern(concat!("derive_", $name)),
145 MultiDecorator(Box::new(DeriveExtension)));
146 })+
147
148 env.insert(intern("derive"),
149 MultiModifier(Box::new(expand_derive)));
150 }
151
152 fn is_builtin_trait(name: &str) -> bool {
153 match name {
154 $( $name )|+ => true,
155 _ => false,
156 }
157 }
158 }
159 }
160
161 derive_traits! {
162 "Clone" => clone::expand_deriving_clone,
163
164 "Hash" => hash::expand_deriving_hash,
165
166 "RustcEncodable" => encodable::expand_deriving_rustc_encodable,
167
168 "RustcDecodable" => decodable::expand_deriving_rustc_decodable,
169
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,
174
175 "Debug" => show::expand_deriving_show,
176
177 "Default" => default::expand_deriving_default,
178
179 "FromPrimitive" => primitive::expand_deriving_from_primitive,
180
181 "Send" => bounds::expand_deriving_unsafe_bound,
182 "Sync" => bounds::expand_deriving_unsafe_bound,
183 "Copy" => bounds::expand_deriving_copy,
184
185 // deprecated
186 "Show" => show::expand_deriving_show,
187 "Encodable" => encodable::expand_deriving_encodable,
188 "Decodable" => decodable::expand_deriving_decodable,
189 }
190
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"),
197 _ => None,
198 } {
199 ecx.span_warn(sp, &format!("derive({}) is deprecated in favor of derive({})",
200 name, replacement));
201 }
202 }