]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/mod.rs
/*! -> //!
[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 `#[deriving]` 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 zero;
29 pub mod default;
30 pub mod primitive;
31
32 #[path="cmp/eq.rs"]
33 pub mod eq;
34 #[path="cmp/totaleq.rs"]
35 pub mod totaleq;
36 #[path="cmp/ord.rs"]
37 pub mod ord;
38 #[path="cmp/totalord.rs"]
39 pub mod totalord;
40
41
42 pub mod generic;
43
44 pub fn expand_meta_deriving(cx: &mut ExtCtxt,
45                             _span: Span,
46                             mitem: &MetaItem,
47                             item: &Item,
48                             push: |P<Item>|) {
49     match mitem.node {
50         MetaNameValue(_, ref l) => {
51             cx.span_err(l.span, "unexpected value in `deriving`");
52         }
53         MetaWord(_) => {
54             cx.span_warn(mitem.span, "empty trait list in `deriving`");
55         }
56         MetaList(_, ref titems) if titems.len() == 0 => {
57             cx.span_warn(mitem.span, "empty trait list in `deriving`");
58         }
59         MetaList(_, ref titems) => {
60             for titem in titems.iter().rev() {
61                 match titem.node {
62                     MetaNameValue(ref tname, _) |
63                     MetaList(ref tname, _) |
64                     MetaWord(ref tname) => {
65                         macro_rules! expand(($func:path) => ($func(cx, titem.span,
66                                                                    &**titem, item,
67                                                                    |i| push(i))));
68                         match tname.get() {
69                             "Clone" => expand!(clone::expand_deriving_clone),
70
71                             "Hash" => expand!(hash::expand_deriving_hash),
72
73                             "Encodable" => expand!(encodable::expand_deriving_encodable),
74                             "Decodable" => expand!(decodable::expand_deriving_decodable),
75
76                             "PartialEq" => expand!(eq::expand_deriving_eq),
77                             "Eq" => expand!(totaleq::expand_deriving_totaleq),
78                             "PartialOrd" => expand!(ord::expand_deriving_ord),
79                             "Ord" => expand!(totalord::expand_deriving_totalord),
80
81                             "Rand" => expand!(rand::expand_deriving_rand),
82
83                             "Show" => expand!(show::expand_deriving_show),
84
85                             "Zero" => expand!(zero::expand_deriving_zero),
86                             "Default" => expand!(default::expand_deriving_default),
87
88                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
89
90                             "Send" => expand!(bounds::expand_deriving_bound),
91                             "Sync" => expand!(bounds::expand_deriving_bound),
92                             "Copy" => expand!(bounds::expand_deriving_bound),
93
94                             ref tname => {
95                                 cx.span_err(titem.span,
96                                             format!("unknown `deriving` \
97                                                      trait: `{}`",
98                                                     *tname).as_slice());
99                             }
100                         };
101                     }
102                 }
103             }
104         }
105     }
106 }