]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
Merge pull request #20510 from tshepang/patch-6
[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_meta_deriving(cx: &mut ExtCtxt,
44                             _span: Span,
45                             mitem: &MetaItem,
46                             item: &Item,
47                             push: Box<FnMut(P<Item>)>) {
48     cx.span_warn(mitem.span, "`deriving` is deprecated; use `derive`");
49
50     expand_meta_derive(cx, _span, mitem, item, push)
51 }
52
53 pub fn expand_meta_derive(cx: &mut ExtCtxt,
54                           _span: Span,
55                           mitem: &MetaItem,
56                           item: &Item,
57                           mut push: Box<FnMut(P<Item>)>) {
58     match mitem.node {
59         MetaNameValue(_, ref l) => {
60             cx.span_err(l.span, "unexpected value in `derive`");
61         }
62         MetaWord(_) => {
63             cx.span_warn(mitem.span, "empty trait list in `derive`");
64         }
65         MetaList(_, ref titems) if titems.len() == 0 => {
66             cx.span_warn(mitem.span, "empty trait list in `derive`");
67         }
68         MetaList(_, ref titems) => {
69             for titem in titems.iter().rev() {
70                 match titem.node {
71                     MetaNameValue(ref tname, _) |
72                     MetaList(ref tname, _) |
73                     MetaWord(ref tname) => {
74                         macro_rules! expand(($func:path) => ($func(cx, titem.span,
75                                                                    &**titem, item,
76                                                                    |i| push.call_mut((i,)))));
77                         match tname.get() {
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" => expand!(show::expand_deriving_show),
111
112                             "Default" => expand!(default::expand_deriving_default),
113
114                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
115
116                             "Send" => expand!(bounds::expand_deriving_bound),
117                             "Sync" => expand!(bounds::expand_deriving_bound),
118                             "Copy" => expand!(bounds::expand_deriving_bound),
119
120                             ref tname => {
121                                 cx.span_err(titem.span,
122                                             format!("unknown `derive` \
123                                                      trait: `{}`",
124                                                     *tname)[]);
125                             }
126                         };
127                     }
128                 }
129             }
130         }
131     }
132 }