1 //! The compiler code necessary to implement the `#[derive]` extensions.
3 use syntax::ast::{self, MetaItem};
4 use syntax::ext::base::{Annotatable, ExtCtxt, MultiItemModifier};
5 use syntax::ext::build::AstBuilder;
7 use syntax::symbol::{Symbol, sym};
10 macro path_local($x:ident) {
11 generic::ty::Path::new_local(stringify!($x))
14 macro pathvec_std($cx:expr, $($rest:ident)::+) {{
15 vec![ $( stringify!($rest) ),+ ]
18 macro path_std($($x:tt)*) {
19 generic::ty::Path::new( pathvec_std!( $($x)* ) )
31 #[path="cmp/partial_eq.rs"]
35 #[path="cmp/partial_ord.rs"]
42 crate struct BuiltinDerive(
43 crate fn(&mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable))
46 impl MultiItemModifier for BuiltinDerive {
48 ecx: &mut ExtCtxt<'_>,
53 let mut items = Vec::new();
54 (self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
59 /// Construct a name for the inner type parameter that can't collide with any type parameters of
60 /// the item. This is achieved by starting with a base and then concatenating the names of all
61 /// other type parameters.
62 // FIXME(aburka): use real hygiene when that becomes possible
63 fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
64 let mut typaram = String::from(base);
65 if let Annotatable::Item(ref item) = *item {
67 ast::ItemKind::Struct(_, ast::Generics { ref params, .. }) |
68 ast::ItemKind::Enum(_, ast::Generics { ref params, .. }) => {
71 ast::GenericParamKind::Type { .. } => {
72 typaram.push_str(¶m.ident.as_str());
86 /// Constructs an expression that calls an intrinsic
87 fn call_intrinsic(cx: &ExtCtxt<'_>,
90 args: Vec<P<ast::Expr>>)
92 let span = span.with_ctxt(cx.backtrace());
93 let path = cx.std_path(&[sym::intrinsics, Symbol::intern(intrinsic)]);
94 let call = cx.expr_call_global(span, path, args);
96 cx.expr_block(P(ast::Block {
97 stmts: vec![cx.stmt_expr(call)],
98 id: ast::DUMMY_NODE_ID,
99 rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),