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;
21 macro_rules! pathvec {
23 vec![ $( stringify!($x) ),+ ]
29 ::ext::deriving::generic::ty::Path::new( pathvec!( $($x)* ) )
33 macro_rules! pathvec_std {
34 ($cx:expr, $first:ident :: $($rest:ident)::+) => (
36 pathvec!(std :: $($rest)::+)
38 pathvec!($first :: $($rest)::+)
43 macro_rules! path_std {
45 ::ext::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) )
61 #[path="cmp/totaleq.rs"]
65 #[path="cmp/totalord.rs"]
71 pub fn expand_deprecated_deriving(cx: &mut ExtCtxt,
75 _: &mut FnMut(P<Item>)) {
76 cx.span_err(span, "`deriving` has been renamed to `derive`");
79 pub fn expand_meta_derive(cx: &mut ExtCtxt,
83 push: &mut FnMut(P<Item>)) {
85 MetaNameValue(_, ref l) => {
86 cx.span_err(l.span, "unexpected value in `derive`");
89 cx.span_warn(mitem.span, "empty trait list in `derive`");
91 MetaList(_, ref titems) if titems.len() == 0 => {
92 cx.span_warn(mitem.span, "empty trait list in `derive`");
94 MetaList(_, ref titems) => {
95 for titem in titems.iter().rev() {
97 MetaNameValue(ref tname, _) |
98 MetaList(ref tname, _) |
99 MetaWord(ref tname) => {
100 macro_rules! expand {
101 ($func:path) => ($func(cx, titem.span, &**titem, item,
106 "Clone" => expand!(clone::expand_deriving_clone),
108 "Hash" => expand!(hash::expand_deriving_hash),
110 "RustcEncodable" => {
111 expand!(encodable::expand_deriving_rustc_encodable)
113 "RustcDecodable" => {
114 expand!(decodable::expand_deriving_rustc_decodable)
117 cx.span_warn(titem.span,
118 "derive(Encodable) is deprecated \
119 in favor of derive(RustcEncodable)");
121 expand!(encodable::expand_deriving_encodable)
124 cx.span_warn(titem.span,
125 "derive(Decodable) is deprecated \
126 in favor of derive(RustcDecodable)");
128 expand!(decodable::expand_deriving_decodable)
131 "PartialEq" => expand!(eq::expand_deriving_eq),
132 "Eq" => expand!(totaleq::expand_deriving_totaleq),
133 "PartialOrd" => expand!(ord::expand_deriving_ord),
134 "Ord" => expand!(totalord::expand_deriving_totalord),
136 "Rand" => expand!(rand::expand_deriving_rand),
139 cx.span_warn(titem.span,
140 "derive(Show) is deprecated \
141 in favor of derive(Debug)");
143 expand!(show::expand_deriving_show)
146 "Debug" => expand!(show::expand_deriving_show),
148 "Default" => expand!(default::expand_deriving_default),
150 "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
152 "Send" => expand!(bounds::expand_deriving_bound),
153 "Sync" => expand!(bounds::expand_deriving_bound),
154 "Copy" => expand!(bounds::expand_deriving_bound),
157 cx.span_err(titem.span,
158 &format!("unknown `derive` \