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