]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libsyntax / ext / deriving / mod.rs
1 // Copyright 2012-2013 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, MetaList, MetaNameValue, MetaWord};
17 use ext::base::ExtCtxt;
18 use codemap::Span;
19 use ptr::P;
20
21 macro_rules! pathvec {
22     ($($x:ident)::+) => (
23         vec![ $( stringify!($x) ),+ ]
24     )
25 }
26
27 macro_rules! path {
28     ($($x:tt)*) => (
29         ::ext::deriving::generic::ty::Path::new( pathvec!( $($x)* ) )
30     )
31 }
32
33 macro_rules! pathvec_std {
34     ($cx:expr, $first:ident :: $($rest:ident)::+) => (
35         if $cx.use_std {
36             pathvec!(std :: $($rest)::+)
37         } else {
38             pathvec!($first :: $($rest)::+)
39         }
40     )
41 }
42
43 macro_rules! path_std {
44     ($($x:tt)*) => (
45         ::ext::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) )
46     )
47 }
48
49 pub mod bounds;
50 pub mod clone;
51 pub mod encodable;
52 pub mod decodable;
53 pub mod hash;
54 pub mod rand;
55 pub mod show;
56 pub mod default;
57 pub mod primitive;
58
59 #[path="cmp/eq.rs"]
60 pub mod eq;
61 #[path="cmp/totaleq.rs"]
62 pub mod totaleq;
63 #[path="cmp/ord.rs"]
64 pub mod ord;
65 #[path="cmp/totalord.rs"]
66 pub mod totalord;
67
68
69 pub mod generic;
70
71 pub fn expand_deprecated_deriving(cx: &mut ExtCtxt,
72                                   span: Span,
73                                   _: &MetaItem,
74                                   _: &Item,
75                                   _: &mut FnMut(P<Item>)) {
76     cx.span_err(span, "`deriving` has been renamed to `derive`");
77 }
78
79 pub fn expand_meta_derive(cx: &mut ExtCtxt,
80                           _span: Span,
81                           mitem: &MetaItem,
82                           item: &Item,
83                           push: &mut FnMut(P<Item>)) {
84     match mitem.node {
85         MetaNameValue(_, ref l) => {
86             cx.span_err(l.span, "unexpected value in `derive`");
87         }
88         MetaWord(_) => {
89             cx.span_warn(mitem.span, "empty trait list in `derive`");
90         }
91         MetaList(_, ref titems) if titems.len() == 0 => {
92             cx.span_warn(mitem.span, "empty trait list in `derive`");
93         }
94         MetaList(_, ref titems) => {
95             for titem in titems.iter().rev() {
96                 match titem.node {
97                     MetaNameValue(ref tname, _) |
98                     MetaList(ref tname, _) |
99                     MetaWord(ref tname) => {
100                         macro_rules! expand {
101                             ($func:path) => ($func(cx, titem.span, &**titem, item,
102                                                    |i| push(i)))
103                         }
104
105                         match &tname[..] {
106                             "Clone" => expand!(clone::expand_deriving_clone),
107
108                             "Hash" => expand!(hash::expand_deriving_hash),
109
110                             "RustcEncodable" => {
111                                 expand!(encodable::expand_deriving_rustc_encodable)
112                             }
113                             "RustcDecodable" => {
114                                 expand!(decodable::expand_deriving_rustc_decodable)
115                             }
116                             "Encodable" => {
117                                 cx.span_warn(titem.span,
118                                              "derive(Encodable) is deprecated \
119                                               in favor of derive(RustcEncodable)");
120
121                                 expand!(encodable::expand_deriving_encodable)
122                             }
123                             "Decodable" => {
124                                 cx.span_warn(titem.span,
125                                              "derive(Decodable) is deprecated \
126                                               in favor of derive(RustcDecodable)");
127
128                                 expand!(decodable::expand_deriving_decodable)
129                             }
130
131                             "PartialEq" => expand!(eq::expand_deriving_eq),
132                             "Eq" => expand!(totaleq::expand_deriving_totaleq),
133                             "PartialOrd" => expand!(ord::expand_deriving_ord),
134                             "Ord" => expand!(totalord::expand_deriving_totalord),
135
136                             "Rand" => expand!(rand::expand_deriving_rand),
137
138                             "Show" => {
139                                 cx.span_warn(titem.span,
140                                              "derive(Show) is deprecated \
141                                               in favor of derive(Debug)");
142
143                                 expand!(show::expand_deriving_show)
144                             },
145
146                             "Debug" => expand!(show::expand_deriving_show),
147
148                             "Default" => expand!(default::expand_deriving_default),
149
150                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
151
152                             "Send" => expand!(bounds::expand_deriving_bound),
153                             "Sync" => expand!(bounds::expand_deriving_bound),
154                             "Copy" => expand!(bounds::expand_deriving_bound),
155
156                             ref tname => {
157                                 cx.span_err(titem.span,
158                                             &format!("unknown `derive` \
159                                                      trait: `{}`",
160                                                     *tname)[]);
161                             }
162                         };
163                     }
164                 }
165             }
166         }
167     }
168 }