]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
318b748ad7ff22354064a61319fd198d165828fb
[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 pub mod bounds;
22 pub mod clone;
23 pub mod encodable;
24 pub mod decodable;
25 pub mod hash;
26 pub mod rand;
27 pub mod show;
28 pub mod default;
29 pub mod primitive;
30
31 #[path="cmp/eq.rs"]
32 pub mod eq;
33 #[path="cmp/totaleq.rs"]
34 pub mod totaleq;
35 #[path="cmp/ord.rs"]
36 pub mod ord;
37 #[path="cmp/totalord.rs"]
38 pub mod totalord;
39
40
41 pub mod generic;
42
43 pub fn expand_deprecated_deriving(cx: &mut ExtCtxt,
44                                   span: Span,
45                                   _: &MetaItem,
46                                   _: &Item,
47                                   _: Box<FnMut(P<Item>)>) {
48     cx.span_err(span, "`deriving` has been renamed to `derive`");
49 }
50
51 pub fn expand_meta_derive(cx: &mut ExtCtxt,
52                           _span: Span,
53                           mitem: &MetaItem,
54                           item: &Item,
55                           mut push: Box<FnMut(P<Item>)>) {
56     match mitem.node {
57         MetaNameValue(_, ref l) => {
58             cx.span_err(l.span, "unexpected value in `derive`");
59         }
60         MetaWord(_) => {
61             cx.span_warn(mitem.span, "empty trait list in `derive`");
62         }
63         MetaList(_, ref titems) if titems.len() == 0 => {
64             cx.span_warn(mitem.span, "empty trait list in `derive`");
65         }
66         MetaList(_, ref titems) => {
67             for titem in titems.iter().rev() {
68                 match titem.node {
69                     MetaNameValue(ref tname, _) |
70                     MetaList(ref tname, _) |
71                     MetaWord(ref tname) => {
72                         macro_rules! expand {
73                             ($func:path) => ($func(cx, titem.span, &**titem, item,
74                                                    |i| push(i)))
75                         }
76
77                         match &tname[] {
78                             "Clone" => expand!(clone::expand_deriving_clone),
79
80                             "Hash" => expand!(hash::expand_deriving_hash),
81
82                             "RustcEncodable" => {
83                                 expand!(encodable::expand_deriving_rustc_encodable)
84                             }
85                             "RustcDecodable" => {
86                                 expand!(decodable::expand_deriving_rustc_decodable)
87                             }
88                             "Encodable" => {
89                                 cx.span_warn(titem.span,
90                                              "derive(Encodable) is deprecated \
91                                               in favor of derive(RustcEncodable)");
92
93                                 expand!(encodable::expand_deriving_encodable)
94                             }
95                             "Decodable" => {
96                                 cx.span_warn(titem.span,
97                                              "derive(Decodable) is deprecated \
98                                               in favor of derive(RustcDecodable)");
99
100                                 expand!(decodable::expand_deriving_decodable)
101                             }
102
103                             "PartialEq" => expand!(eq::expand_deriving_eq),
104                             "Eq" => expand!(totaleq::expand_deriving_totaleq),
105                             "PartialOrd" => expand!(ord::expand_deriving_ord),
106                             "Ord" => expand!(totalord::expand_deriving_totalord),
107
108                             "Rand" => expand!(rand::expand_deriving_rand),
109
110                             "Show" => {
111                                 cx.span_warn(titem.span,
112                                              "derive(Show) is deprecated \
113                                               in favor of derive(Debug)");
114
115                                 expand!(show::expand_deriving_show)
116                             },
117
118                             "Debug" => expand!(show::expand_deriving_show),
119
120                             "Default" => expand!(default::expand_deriving_default),
121
122                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
123
124                             "Send" => expand!(bounds::expand_deriving_bound),
125                             "Sync" => expand!(bounds::expand_deriving_bound),
126                             "Copy" => expand!(bounds::expand_deriving_bound),
127
128                             ref tname => {
129                                 cx.span_err(titem.span,
130                                             &format!("unknown `derive` \
131                                                      trait: `{}`",
132                                                     *tname)[]);
133                             }
134                         };
135                     }
136                 }
137             }
138         }
139     }
140 }