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
|
|
|
/*!
|
|
|
|
The compiler code necessary to implement the #[deriving] extensions.
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-06-07 07:30:38 +00:00
|
|
|
use ast::{enum_def, ident, item, Generics, meta_item, struct_def};
|
2013-05-17 11:27:17 +00:00
|
|
|
use ext::base::ExtCtxt;
|
2013-05-17 14:19:28 +00:00
|
|
|
use ext::build::AstBuilder;
|
2013-05-19 05:53:42 +00:00
|
|
|
use codemap::span;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod clone;
|
2013-03-19 12:52:10 +00:00
|
|
|
pub mod iter_bytes;
|
2013-04-10 23:31:51 +00:00
|
|
|
pub mod encodable;
|
2013-04-09 01:53:39 +00:00
|
|
|
pub mod decodable;
|
2013-05-06 15:30:42 +00:00
|
|
|
pub mod rand;
|
|
|
|
pub mod to_str;
|
2013-06-15 01:27:35 +00:00
|
|
|
pub mod zero;
|
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;
|
|
|
|
|
2013-05-17 11:27:17 +00:00
|
|
|
pub type ExpandDerivingStructDefFn<'self> = &'self fn(@ExtCtxt,
|
2013-03-28 10:50:10 +00:00
|
|
|
span,
|
|
|
|
x: &struct_def,
|
|
|
|
ident,
|
|
|
|
y: &Generics)
|
|
|
|
-> @item;
|
2013-05-17 11:27:17 +00:00
|
|
|
pub type ExpandDerivingEnumDefFn<'self> = &'self fn(@ExtCtxt,
|
2013-03-28 10:50:10 +00:00
|
|
|
span,
|
|
|
|
x: &enum_def,
|
|
|
|
ident,
|
|
|
|
y: &Generics)
|
|
|
|
-> @item;
|
2012-11-21 02:27:13 +00:00
|
|
|
|
2013-05-17 11:27:17 +00:00
|
|
|
pub fn expand_meta_deriving(cx: @ExtCtxt,
|
2013-03-11 20:47:23 +00:00
|
|
|
_span: span,
|
|
|
|
mitem: @meta_item,
|
|
|
|
in_items: ~[@item])
|
|
|
|
-> ~[@item] {
|
|
|
|
use ast::{meta_list, meta_name_value, meta_word};
|
|
|
|
|
|
|
|
match mitem.node {
|
2013-05-12 04:25:31 +00:00
|
|
|
meta_name_value(_, 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
|
|
|
in_items
|
|
|
|
}
|
|
|
|
meta_word(_) | meta_list(_, []) => {
|
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
|
|
|
in_items
|
|
|
|
}
|
2013-05-12 04:25:31 +00:00
|
|
|
meta_list(_, ref titems) => {
|
2013-06-08 08:28:08 +00:00
|
|
|
do titems.rev_iter().fold(in_items) |in_items, &titem| {
|
2013-03-11 20:47:23 +00:00
|
|
|
match titem.node {
|
|
|
|
meta_name_value(tname, _) |
|
|
|
|
meta_list(tname, _) |
|
|
|
|
meta_word(tname) => {
|
2013-05-06 15:23:51 +00:00
|
|
|
macro_rules! expand(($func:path) => ($func(cx, titem.span,
|
|
|
|
titem, in_items)));
|
2013-06-12 17:02:55 +00:00
|
|
|
match tname.as_slice() {
|
|
|
|
"Clone" => expand!(clone::expand_deriving_clone),
|
|
|
|
"DeepClone" => expand!(clone::expand_deriving_deep_clone),
|
2013-05-06 15:23:51 +00:00
|
|
|
|
2013-06-12 17:02:55 +00:00
|
|
|
"IterBytes" => expand!(iter_bytes::expand_deriving_iter_bytes),
|
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
|
|
|
|
2013-06-12 17:02:55 +00: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-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
|
|
|
|
2013-06-12 17:02:55 +00:00
|
|
|
"ToStr" => expand!(to_str::expand_deriving_to_str),
|
2013-06-15 01:27:35 +00:00
|
|
|
"Zero" => expand!(zero::expand_deriving_zero),
|
2013-05-06 15:30:42 +00:00
|
|
|
|
2013-05-12 04:25:31 +00:00
|
|
|
ref tname => {
|
2013-03-11 20:47:23 +00:00
|
|
|
cx.span_err(titem.span, fmt!("unknown \
|
2013-05-12 04:25:31 +00:00
|
|
|
`deriving` trait: `%s`", *tname));
|
2013-03-11 20:47:23 +00:00
|
|
|
in_items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|