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 `#[deriving]` 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;
34 #[path="cmp/totaleq.rs"]
38 #[path="cmp/totalord.rs"]
44 pub fn expand_meta_deriving(cx: &mut ExtCtxt,
50 MetaNameValue(_, ref l) => {
51 cx.span_err(l.span, "unexpected value in `deriving`");
54 cx.span_warn(mitem.span, "empty trait list in `deriving`");
56 MetaList(_, ref titems) if titems.len() == 0 => {
57 cx.span_warn(mitem.span, "empty trait list in `deriving`");
59 MetaList(_, ref titems) => {
60 for titem in titems.iter().rev() {
62 MetaNameValue(ref tname, _) |
63 MetaList(ref tname, _) |
64 MetaWord(ref tname) => {
65 macro_rules! expand(($func:path) => ($func(cx, titem.span,
69 "Clone" => expand!(clone::expand_deriving_clone),
71 "Hash" => expand!(hash::expand_deriving_hash),
73 "Encodable" => expand!(encodable::expand_deriving_encodable),
74 "Decodable" => expand!(decodable::expand_deriving_decodable),
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),
81 "Rand" => expand!(rand::expand_deriving_rand),
83 "Show" => expand!(show::expand_deriving_show),
85 "Zero" => expand!(zero::expand_deriving_zero),
86 "Default" => expand!(default::expand_deriving_default),
88 "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
90 "Send" => expand!(bounds::expand_deriving_bound),
91 "Sync" => expand!(bounds::expand_deriving_bound),
92 "Copy" => expand!(bounds::expand_deriving_bound),
95 cx.span_err(titem.span,
96 format!("unknown `deriving` \