2013-03-19 12:52:10 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-15 22:55:57 +00:00
|
|
|
/*!
|
2014-02-27 05:30:28 +00:00
|
|
|
The compiler code necessary to implement the `#[deriving]` extensions.
|
2013-05-15 22:55:57 +00:00
|
|
|
|
|
|
|
|
2014-02-27 05:30:28 +00:00
|
|
|
FIXME (#2810): hygiene. Search for "__" strings (in other files too).
|
2013-05-20 22:20:16 +00:00
|
|
|
We also assume "extra" is the standard library, and "std" is the core
|
2013-05-15 22:55:57 +00:00
|
|
|
library.
|
|
|
|
|
|
|
|
*/
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2014-02-05 14:08:17 +00:00
|
|
|
use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
|
2013-05-17 11:27:17 +00:00
|
|
|
use ext::base::ExtCtxt;
|
2013-08-31 16:13:04 +00:00
|
|
|
use codemap::Span;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2014-04-30 09:26:50 +00:00
|
|
|
pub mod bounds;
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod clone;
|
2013-04-10 23:31:51 +00:00
|
|
|
pub mod encodable;
|
2013-04-09 01:53:39 +00:00
|
|
|
pub mod decodable;
|
2014-02-22 05:33:23 +00:00
|
|
|
pub mod hash;
|
2013-05-06 15:30:42 +00:00
|
|
|
pub mod rand;
|
2014-02-06 12:02:28 +00:00
|
|
|
pub mod show;
|
2013-06-15 01:27:35 +00:00
|
|
|
pub mod zero;
|
2013-09-12 04:51:13 +00:00
|
|
|
pub mod default;
|
2013-09-17 04:12:18 +00:00
|
|
|
pub mod primitive;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2013-03-30 14:58:05 +00:00
|
|
|
#[path="cmp/eq.rs"]
|
|
|
|
pub mod eq;
|
|
|
|
#[path="cmp/totaleq.rs"]
|
|
|
|
pub mod totaleq;
|
|
|
|
#[path="cmp/ord.rs"]
|
|
|
|
pub mod ord;
|
|
|
|
#[path="cmp/totalord.rs"]
|
|
|
|
pub mod totalord;
|
|
|
|
|
|
|
|
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod generic;
|
|
|
|
|
2014-02-05 21:50:21 +00:00
|
|
|
pub fn expand_meta_deriving(cx: &mut ExtCtxt,
|
2013-08-31 16:13:04 +00:00
|
|
|
_span: Span,
|
2013-07-19 11:51:37 +00:00
|
|
|
mitem: @MetaItem,
|
2014-02-13 07:53:52 +00:00
|
|
|
item: @Item,
|
|
|
|
push: |@Item|) {
|
2013-03-11 20:47:23 +00:00
|
|
|
match mitem.node {
|
2013-07-19 11:51:37 +00:00
|
|
|
MetaNameValue(_, ref l) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
cx.span_err(l.span, "unexpected value in `deriving`");
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
2014-02-13 17:46:46 +00:00
|
|
|
MetaWord(_) => {
|
|
|
|
cx.span_warn(mitem.span, "empty trait list in `deriving`");
|
|
|
|
}
|
|
|
|
MetaList(_, ref titems) if titems.len() == 0 => {
|
2013-05-19 05:07:44 +00:00
|
|
|
cx.span_warn(mitem.span, "empty trait list in `deriving`");
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
2013-07-19 11:51:37 +00:00
|
|
|
MetaList(_, ref titems) => {
|
2014-03-16 23:04:29 +00:00
|
|
|
for &titem in titems.iter().rev() {
|
2013-03-11 20:47:23 +00:00
|
|
|
match titem.node {
|
2014-01-08 18:35:15 +00:00
|
|
|
MetaNameValue(ref tname, _) |
|
|
|
|
MetaList(ref tname, _) |
|
|
|
|
MetaWord(ref tname) => {
|
2013-05-06 15:23:51 +00:00
|
|
|
macro_rules! expand(($func:path) => ($func(cx, titem.span,
|
2014-02-13 07:53:52 +00:00
|
|
|
titem, item,
|
|
|
|
|i| push(i))));
|
2014-01-08 18:35:15 +00:00
|
|
|
match tname.get() {
|
2013-06-12 17:02:55 +00:00
|
|
|
"Clone" => expand!(clone::expand_deriving_clone),
|
2013-05-06 15:23:51 +00:00
|
|
|
|
2014-02-22 05:33:23 +00:00
|
|
|
"Hash" => expand!(hash::expand_deriving_hash),
|
2013-05-06 15:23:51 +00:00
|
|
|
|
2013-06-12 17:02:55 +00:00
|
|
|
"Encodable" => expand!(encodable::expand_deriving_encodable),
|
|
|
|
"Decodable" => expand!(decodable::expand_deriving_decodable),
|
2013-05-06 15:23:51 +00:00
|
|
|
|
2014-05-30 03:57:18 +00:00
|
|
|
// NOTE: after a stage0 snap this needs treatment
|
2014-05-30 00:45:07 +00:00
|
|
|
"PartialEq" => expand!(eq::expand_deriving_eq),
|
2014-05-30 03:57:18 +00:00
|
|
|
"Eq" | "TotalEq" => expand!(totaleq::expand_deriving_totaleq),
|
2014-05-30 00:45:07 +00:00
|
|
|
"PartialOrd" => expand!(ord::expand_deriving_ord),
|
2014-05-30 03:57:18 +00:00
|
|
|
"Ord" | "TotalOrd" => expand!(totalord::expand_deriving_totalord),
|
2013-05-06 15:23:51 +00:00
|
|
|
|
2013-06-12 17:02:55 +00:00
|
|
|
"Rand" => expand!(rand::expand_deriving_rand),
|
2013-05-06 15:30:42 +00:00
|
|
|
|
2014-02-06 12:02:28 +00:00
|
|
|
"Show" => expand!(show::expand_deriving_show),
|
2013-09-17 04:12:18 +00:00
|
|
|
|
2013-06-15 01:27:35 +00:00
|
|
|
"Zero" => expand!(zero::expand_deriving_zero),
|
2013-09-12 04:51:13 +00:00
|
|
|
"Default" => expand!(default::expand_deriving_default),
|
2013-05-06 15:30:42 +00:00
|
|
|
|
2013-09-17 04:12:18 +00:00
|
|
|
"FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
|
|
|
|
|
2014-04-30 09:26:50 +00:00
|
|
|
"Send" => expand!(bounds::expand_deriving_bound),
|
|
|
|
"Share" => expand!(bounds::expand_deriving_bound),
|
|
|
|
"Copy" => expand!(bounds::expand_deriving_bound),
|
|
|
|
|
2013-05-12 04:25:31 +00:00
|
|
|
ref tname => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(titem.span,
|
|
|
|
format!("unknown `deriving` \
|
|
|
|
trait: `{}`",
|
|
|
|
*tname).as_slice());
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
2014-02-13 07:53:52 +00:00
|
|
|
};
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 07:53:52 +00:00
|
|
|
}
|
2013-03-11 20:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|