]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
debuginfo: Make debuginfo source location assignment more stable (Pt. 1)
[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_derive(cx: &mut ExtCtxt,
44                           _span: Span,
45                           mitem: &MetaItem,
46                           item: &Item,
47                           mut push: Box<FnMut(P<Item>)>) {
48     match mitem.node {
49         MetaNameValue(_, ref l) => {
50             cx.span_err(l.span, "unexpected value in `derive`");
51         }
52         MetaWord(_) => {
53             cx.span_warn(mitem.span, "empty trait list in `derive`");
54         }
55         MetaList(_, ref titems) if titems.len() == 0 => {
56             cx.span_warn(mitem.span, "empty trait list in `derive`");
57         }
58         MetaList(_, ref titems) => {
59             for titem in titems.iter().rev() {
60                 match titem.node {
61                     MetaNameValue(ref tname, _) |
62                     MetaList(ref tname, _) |
63                     MetaWord(ref tname) => {
64                         macro_rules! expand {
65                             ($func:path) => ($func(cx, titem.span, &**titem, item,
66                                                    |i| push(i)))
67                         }
68
69                         match tname.get() {
70                             "Clone" => expand!(clone::expand_deriving_clone),
71
72                             "Hash" => expand!(hash::expand_deriving_hash),
73
74                             "RustcEncodable" => {
75                                 expand!(encodable::expand_deriving_rustc_encodable)
76                             }
77                             "RustcDecodable" => {
78                                 expand!(decodable::expand_deriving_rustc_decodable)
79                             }
80                             "Encodable" => {
81                                 cx.span_warn(titem.span,
82                                              "derive(Encodable) is deprecated \
83                                               in favor of derive(RustcEncodable)");
84
85                                 expand!(encodable::expand_deriving_encodable)
86                             }
87                             "Decodable" => {
88                                 cx.span_warn(titem.span,
89                                              "derive(Decodable) is deprecated \
90                                               in favor of derive(RustcDecodable)");
91
92                                 expand!(decodable::expand_deriving_decodable)
93                             }
94
95                             "PartialEq" => expand!(eq::expand_deriving_eq),
96                             "Eq" => expand!(totaleq::expand_deriving_totaleq),
97                             "PartialOrd" => expand!(ord::expand_deriving_ord),
98                             "Ord" => expand!(totalord::expand_deriving_totalord),
99
100                             "Rand" => expand!(rand::expand_deriving_rand),
101
102                             "Show" => expand!(show::expand_deriving_show),
103
104                             "Default" => expand!(default::expand_deriving_default),
105
106                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
107
108                             "Send" => expand!(bounds::expand_deriving_bound),
109                             "Sync" => expand!(bounds::expand_deriving_bound),
110                             "Copy" => expand!(bounds::expand_deriving_bound),
111
112                             ref tname => {
113                                 cx.span_err(titem.span,
114                                             &format!("unknown `derive` \
115                                                      trait: `{}`",
116                                                     *tname)[]);
117                             }
118                         };
119                     }
120                 }
121             }
122         }
123     }
124 }