]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
Merge pull request #20674 from jbcrail/fix-misspelled-comments
[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 {
75                             ($func:path) => ($func(cx, titem.span, &**titem, item,
76                                                    |i| push(i)))
77                         }
78
79                         match tname.get() {
80                             "Clone" => expand!(clone::expand_deriving_clone),
81
82                             "Hash" => expand!(hash::expand_deriving_hash),
83
84                             "RustcEncodable" => {
85                                 expand!(encodable::expand_deriving_rustc_encodable)
86                             }
87                             "RustcDecodable" => {
88                                 expand!(decodable::expand_deriving_rustc_decodable)
89                             }
90                             "Encodable" => {
91                                 cx.span_warn(titem.span,
92                                              "derive(Encodable) is deprecated \
93                                               in favor of derive(RustcEncodable)");
94
95                                 expand!(encodable::expand_deriving_encodable)
96                             }
97                             "Decodable" => {
98                                 cx.span_warn(titem.span,
99                                              "derive(Decodable) is deprecated \
100                                               in favor of derive(RustcDecodable)");
101
102                                 expand!(decodable::expand_deriving_decodable)
103                             }
104
105                             "PartialEq" => expand!(eq::expand_deriving_eq),
106                             "Eq" => expand!(totaleq::expand_deriving_totaleq),
107                             "PartialOrd" => expand!(ord::expand_deriving_ord),
108                             "Ord" => expand!(totalord::expand_deriving_totalord),
109
110                             "Rand" => expand!(rand::expand_deriving_rand),
111
112                             "Show" => expand!(show::expand_deriving_show),
113
114                             "Default" => expand!(default::expand_deriving_default),
115
116                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
117
118                             "Send" => expand!(bounds::expand_deriving_bound),
119                             "Sync" => expand!(bounds::expand_deriving_bound),
120                             "Copy" => expand!(bounds::expand_deriving_bound),
121
122                             ref tname => {
123                                 cx.span_err(titem.span,
124                                             format!("unknown `derive` \
125                                                      trait: `{}`",
126                                                     *tname).index(&FullRange));
127                             }
128                         };
129                     }
130                 }
131             }
132         }
133     }
134 }