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.
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.
11 //! The compiler code necessary to implement the `#[derive]` extensions.
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.
16 use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
17 use ext::base::ExtCtxt;
33 #[path="cmp/totaleq.rs"]
37 #[path="cmp/totalord.rs"]
43 pub fn expand_meta_deriving(cx: &mut ExtCtxt,
47 push: Box<FnMut(P<Item>)>) {
48 cx.span_warn(mitem.span, "`deriving` is deprecated; use `derive`");
50 expand_meta_derive(cx, _span, mitem, item, push)
53 pub fn expand_meta_derive(cx: &mut ExtCtxt,
57 mut push: Box<FnMut(P<Item>)>) {
59 MetaNameValue(_, ref l) => {
60 cx.span_err(l.span, "unexpected value in `derive`");
63 cx.span_warn(mitem.span, "empty trait list in `derive`");
65 MetaList(_, ref titems) if titems.len() == 0 => {
66 cx.span_warn(mitem.span, "empty trait list in `derive`");
68 MetaList(_, ref titems) => {
69 for titem in titems.iter().rev() {
71 MetaNameValue(ref tname, _) |
72 MetaList(ref tname, _) |
73 MetaWord(ref tname) => {
74 macro_rules! expand(($func:path) => ($func(cx, titem.span,
76 |i| push.call_mut((i,)))));
78 "Clone" => expand!(clone::expand_deriving_clone),
80 "Hash" => expand!(hash::expand_deriving_hash),
83 expand!(encodable::expand_deriving_rustc_encodable)
86 expand!(decodable::expand_deriving_rustc_decodable)
89 cx.span_warn(titem.span,
90 "derive(Encodable) is deprecated \
91 in favor of derive(RustcEncodable)");
93 expand!(encodable::expand_deriving_encodable)
96 cx.span_warn(titem.span,
97 "derive(Decodable) is deprecated \
98 in favor of derive(RustcDecodable)");
100 expand!(decodable::expand_deriving_decodable)
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),
108 "Rand" => expand!(rand::expand_deriving_rand),
110 "Show" => expand!(show::expand_deriving_show),
112 "Default" => expand!(default::expand_deriving_default),
114 "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
116 "Send" => expand!(bounds::expand_deriving_bound),
117 "Sync" => expand!(bounds::expand_deriving_bound),
118 "Copy" => expand!(bounds::expand_deriving_bound),
121 cx.span_err(titem.span,
122 format!("unknown `derive` \