1 // Copyright 2012-2015 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.
15 use syntax::ext::base::{Annotatable, ExtCtxt, SyntaxExtension, Resolver};
16 use syntax::ext::build::AstBuilder;
17 use syntax::ext::hygiene::{Mark, SyntaxContext};
19 use syntax::symbol::Symbol;
22 macro_rules! pathvec {
24 vec![ $( stringify!($x) ),+ ]
30 ::ext::deriving::generic::ty::Path::new( pathvec![ $($x)* ] )
34 macro_rules! path_local {
36 ::deriving::generic::ty::Path::new_local(stringify!($x))
40 macro_rules! pathvec_std {
41 ($cx:expr, $first:ident :: $($rest:ident)::+) => ({
42 let mut v = pathvec![$($rest)::+];
43 if let Some(s) = $cx.crate_root {
50 macro_rules! path_std {
52 ::deriving::generic::ty::Path::new( pathvec_std!( $($x)* ) )
65 #[path="cmp/partial_eq.rs"]
69 #[path="cmp/partial_ord.rs"]
77 macro_rules! derive_traits {
78 ($( $name:expr => $func:path, )+) => {
79 pub fn is_builtin_trait(name: ast::Name) -> bool {
80 match &*name.as_str() {
86 pub fn register_builtin_derives(resolver: &mut Resolver) {
89 ast::Ident::with_empty_ctxt(Symbol::intern($name)),
90 Rc::new(SyntaxExtension::BuiltinDerive($func))
98 "Clone" => clone::expand_deriving_clone,
100 "Hash" => hash::expand_deriving_hash,
102 "RustcEncodable" => encodable::expand_deriving_rustc_encodable,
104 "RustcDecodable" => decodable::expand_deriving_rustc_decodable,
106 "PartialEq" => partial_eq::expand_deriving_partial_eq,
107 "Eq" => eq::expand_deriving_eq,
108 "PartialOrd" => partial_ord::expand_deriving_partial_ord,
109 "Ord" => ord::expand_deriving_ord,
111 "Debug" => debug::expand_deriving_debug,
113 "Default" => default::expand_deriving_default,
115 "Send" => bounds::expand_deriving_unsafe_bound,
116 "Sync" => bounds::expand_deriving_unsafe_bound,
117 "Copy" => bounds::expand_deriving_copy,
120 "Encodable" => encodable::expand_deriving_encodable,
121 "Decodable" => decodable::expand_deriving_decodable,
124 #[inline] // because `name` is a compile-time constant
125 fn warn_if_deprecated(ecx: &mut ExtCtxt, sp: Span, name: &str) {
126 if let Some(replacement) = match name {
127 "Encodable" => Some("RustcEncodable"),
128 "Decodable" => Some("RustcDecodable"),
132 &format!("derive({}) is deprecated in favor of derive({})",
138 /// Construct a name for the inner type parameter that can't collide with any type parameters of
139 /// the item. This is achieved by starting with a base and then concatenating the names of all
140 /// other type parameters.
141 // FIXME(aburka): use real hygiene when that becomes possible
142 fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
143 let mut typaram = String::from(base);
144 if let Annotatable::Item(ref item) = *item {
146 ast::ItemKind::Struct(_, ast::Generics { ref ty_params, .. }) |
147 ast::ItemKind::Enum(_, ast::Generics { ref ty_params, .. }) => {
148 for ty in ty_params.iter() {
149 typaram.push_str(&ty.ident.name.as_str());
160 /// Constructs an expression that calls an intrinsic
161 fn call_intrinsic(cx: &ExtCtxt,
164 args: Vec<P<ast::Expr>>)
166 if cx.current_expansion.mark.expn_info().unwrap().callee.allow_internal_unstable {
167 span.ctxt = cx.backtrace();
168 } else { // Avoid instability errors with user defined curstom derives, cc #36316
169 let mut info = cx.current_expansion.mark.expn_info().unwrap();
170 info.callee.allow_internal_unstable = true;
171 let mark = Mark::fresh();
172 mark.set_expn_info(info);
173 span.ctxt = SyntaxContext::empty().apply_mark(mark);
175 let path = cx.std_path(&["intrinsics", intrinsic]);
176 let call = cx.expr_call_global(span, path, args);
178 cx.expr_block(P(ast::Block {
179 stmts: vec![cx.stmt_expr(call)],
180 id: ast::DUMMY_NODE_ID,
181 rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),