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_deprecated_deriving(cx: &mut ExtCtxt,
47 _: Box<FnMut(P<Item>)>) {
48 cx.span_err(span, "`deriving` has been renamed to `derive`");
51 pub fn expand_meta_derive(cx: &mut ExtCtxt,
55 mut push: Box<FnMut(P<Item>)>) {
57 MetaNameValue(_, ref l) => {
58 cx.span_err(l.span, "unexpected value in `derive`");
61 cx.span_warn(mitem.span, "empty trait list in `derive`");
63 MetaList(_, ref titems) if titems.len() == 0 => {
64 cx.span_warn(mitem.span, "empty trait list in `derive`");
66 MetaList(_, ref titems) => {
67 for titem in titems.iter().rev() {
69 MetaNameValue(ref tname, _) |
70 MetaList(ref tname, _) |
71 MetaWord(ref tname) => {
73 ($func:path) => ($func(cx, titem.span, &**titem, item,
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),
111 cx.span_warn(titem.span,
112 "derive(Show) is deprecated \
113 in favor of derive(Debug)");
115 expand!(show::expand_deriving_show)
118 "Debug" => expand!(show::expand_deriving_show),
120 "Default" => expand!(default::expand_deriving_default),
122 "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
124 "Send" => expand!(bounds::expand_deriving_bound),
125 "Sync" => expand!(bounds::expand_deriving_bound),
126 "Copy" => expand!(bounds::expand_deriving_bound),
129 cx.span_err(titem.span,
130 &format!("unknown `derive` \