2013-03-19 08:52:10 -04:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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-16 08:55:57 +10:00
|
|
|
/*!
|
|
|
|
|
The compiler code necessary to implement the #[deriving] extensions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FIXME (#2810)--Hygiene. Search for "__" strings (in other files too).
|
2013-05-20 15:20:16 -07:00
|
|
|
We also assume "extra" is the standard library, and "std" is the core
|
2013-05-16 08:55:57 +10:00
|
|
|
library.
|
|
|
|
|
|
|
|
|
|
*/
|
2012-11-19 18:05:50 -08:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
use ast::{EnumDef, Ident, Item, Generics, StructDef};
|
2013-07-19 21:51:37 +10:00
|
|
|
use ast::{MetaItem, MetaList, MetaNameValue, MetaWord};
|
2013-05-17 21:27:17 +10:00
|
|
|
use ext::base::ExtCtxt;
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::Span;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2013-03-28 21:50:10 +11:00
|
|
|
pub mod clone;
|
2013-03-19 08:52:10 -04:00
|
|
|
pub mod iter_bytes;
|
2013-04-10 16:31:51 -07:00
|
|
|
pub mod encodable;
|
2013-04-08 18:53:39 -07:00
|
|
|
pub mod decodable;
|
2013-05-07 01:30:42 +10:00
|
|
|
pub mod rand;
|
|
|
|
|
pub mod to_str;
|
2013-06-14 18:27:35 -07:00
|
|
|
pub mod zero;
|
2013-09-11 21:51:13 -07:00
|
|
|
pub mod default;
|
2013-09-16 21:12:18 -07:00
|
|
|
pub mod primitive;
|
2012-11-19 18:05:50 -08:00
|
|
|
|
2013-03-31 01:58:05 +11: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 21:50:10 +11:00
|
|
|
pub mod generic;
|
|
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
pub type ExpandDerivingStructDefFn<'a> = 'a |&ExtCtxt,
|
2013-11-19 17:36:32 -08:00
|
|
|
Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
x: &StructDef,
|
2013-11-19 17:36:32 -08:00
|
|
|
Ident,
|
|
|
|
|
y: &Generics|
|
2014-01-09 15:05:33 +02:00
|
|
|
-> @Item;
|
2013-12-27 17:17:36 -07:00
|
|
|
pub type ExpandDerivingEnumDefFn<'a> = 'a |&ExtCtxt,
|
2013-11-19 17:36:32 -08:00
|
|
|
Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
x: &EnumDef,
|
2013-11-19 17:36:32 -08:00
|
|
|
Ident,
|
|
|
|
|
y: &Generics|
|
2014-01-09 15:05:33 +02:00
|
|
|
-> @Item;
|
2012-11-20 18:27:13 -08:00
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
pub fn expand_meta_deriving(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
_span: Span,
|
2013-07-19 21:51:37 +10:00
|
|
|
mitem: @MetaItem,
|
2014-01-09 15:05:33 +02:00
|
|
|
in_items: ~[@Item])
|
|
|
|
|
-> ~[@Item] {
|
2013-03-11 16:47:23 -04:00
|
|
|
match mitem.node {
|
2013-07-19 21:51:37 +10:00
|
|
|
MetaNameValue(_, ref l) => {
|
2013-05-19 01:07:44 -04:00
|
|
|
cx.span_err(l.span, "unexpected value in `deriving`");
|
2013-03-11 16:47:23 -04:00
|
|
|
in_items
|
|
|
|
|
}
|
2013-07-19 21:51:37 +10:00
|
|
|
MetaWord(_) | MetaList(_, []) => {
|
2013-05-19 01:07:44 -04:00
|
|
|
cx.span_warn(mitem.span, "empty trait list in `deriving`");
|
2013-03-11 16:47:23 -04:00
|
|
|
in_items
|
|
|
|
|
}
|
2013-07-19 21:51:37 +10:00
|
|
|
MetaList(_, ref titems) => {
|
2013-11-20 16:23:04 -08:00
|
|
|
titems.rev_iter().fold(in_items, |in_items, &titem| {
|
2013-03-11 16:47:23 -04:00
|
|
|
match titem.node {
|
2013-07-19 21:51:37 +10:00
|
|
|
MetaNameValue(tname, _) |
|
|
|
|
|
MetaList(tname, _) |
|
|
|
|
|
MetaWord(tname) => {
|
2013-05-07 01:23:51 +10:00
|
|
|
macro_rules! expand(($func:path) => ($func(cx, titem.span,
|
|
|
|
|
titem, in_items)));
|
2013-06-13 03:02:55 +10:00
|
|
|
match tname.as_slice() {
|
|
|
|
|
"Clone" => expand!(clone::expand_deriving_clone),
|
|
|
|
|
"DeepClone" => expand!(clone::expand_deriving_deep_clone),
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
"IterBytes" => expand!(iter_bytes::expand_deriving_iter_bytes),
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
"Encodable" => expand!(encodable::expand_deriving_encodable),
|
|
|
|
|
"Decodable" => expand!(decodable::expand_deriving_decodable),
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
"Eq" => expand!(eq::expand_deriving_eq),
|
|
|
|
|
"TotalEq" => expand!(totaleq::expand_deriving_totaleq),
|
|
|
|
|
"Ord" => expand!(ord::expand_deriving_ord),
|
|
|
|
|
"TotalOrd" => expand!(totalord::expand_deriving_totalord),
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
"Rand" => expand!(rand::expand_deriving_rand),
|
2013-05-07 01:30:42 +10:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
"ToStr" => expand!(to_str::expand_deriving_to_str),
|
2013-09-16 21:12:18 -07:00
|
|
|
|
2013-06-14 18:27:35 -07:00
|
|
|
"Zero" => expand!(zero::expand_deriving_zero),
|
2013-09-11 21:51:13 -07:00
|
|
|
"Default" => expand!(default::expand_deriving_default),
|
2013-05-07 01:30:42 +10:00
|
|
|
|
2013-09-16 21:12:18 -07:00
|
|
|
"FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
|
|
|
|
|
|
2013-05-12 00:25:31 -04:00
|
|
|
ref tname => {
|
2013-09-27 21:01:58 -07:00
|
|
|
cx.span_err(titem.span, format!("unknown \
|
|
|
|
|
`deriving` trait: `{}`", *tname));
|
2013-03-11 16:47:23 -04:00
|
|
|
in_items
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-03-11 16:47:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|