1 use crate::util::check_builtin_macro_attribute;
4 use rustc_ast::{self as ast, FnHeader, FnSig, Generics, StmtKind};
5 use rustc_ast::{Fn, ItemKind, Stmt, TyKind, Unsafe};
6 use rustc_expand::base::{Annotatable, ExtCtxt};
7 use rustc_span::symbol::{kw, sym, Ident};
9 use thin_vec::thin_vec;
12 ecx: &mut ExtCtxt<'_>,
14 meta_item: &ast::MetaItem,
16 ) -> Vec<Annotatable> {
17 check_builtin_macro_attribute(ecx, meta_item, sym::alloc_error_handler);
19 let orig_item = item.clone();
21 // Allow using `#[alloc_error_handler]` on an item statement
22 // FIXME - if we get deref patterns, use them to reduce duplication here
23 let (item, is_stmt, sig_span) =
24 if let Annotatable::Item(item) = &item
25 && let ItemKind::Fn(fn_kind) = &item.kind
27 (item, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
28 } else if let Annotatable::Stmt(stmt) = &item
29 && let StmtKind::Item(item) = &stmt.kind
30 && let ItemKind::Fn(fn_kind) = &item.kind
32 (item, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
34 ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "alloc_error_handler must be a function");
35 return vec![orig_item];
38 // Generate a bunch of new items using the AllocFnFactory
39 let span = ecx.with_def_site_ctxt(item.span);
41 // Generate item statements for the allocator methods.
42 let stmts = vec![generate_handler(ecx, item.ident, span, sig_span)];
44 // Generate anonymous constant serving as container for the allocator methods.
45 let const_ty = ecx.ty(sig_span, TyKind::Tup(Vec::new()));
46 let const_body = ecx.expr_block(ecx.block(span, stmts));
47 let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
48 let const_item = if is_stmt {
49 Annotatable::Stmt(P(ecx.stmt_item(span, const_item)))
51 Annotatable::Item(const_item)
54 // Return the original item and the new methods.
55 vec![orig_item, const_item]
58 // #[rustc_std_internal_symbol]
59 // unsafe fn __rg_oom(size: usize, align: usize) -> ! {
60 // handler(core::alloc::Layout::from_size_align_unchecked(size, align))
62 fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
63 let usize = cx.path_ident(span, Ident::new(sym::usize, span));
64 let ty_usize = cx.ty_path(usize);
65 let size = Ident::from_str_and_span("size", span);
66 let align = Ident::from_str_and_span("align", span);
68 let layout_new = cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
69 let layout_new = cx.expr_path(cx.path(span, layout_new));
71 cx.expr_call(span, layout_new, vec![cx.expr_ident(span, size), cx.expr_ident(span, align)]);
73 let call = cx.expr_call_ident(sig_span, handler, vec![layout]);
75 let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
76 let params = vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
77 let decl = cx.fn_decl(params, never);
78 let header = FnHeader { unsafety: Unsafe::Yes(span), ..FnHeader::default() };
79 let sig = FnSig { decl, header, span: span };
81 let body = Some(cx.block_expr(call));
82 let kind = ItemKind::Fn(Box::new(Fn {
83 defaultness: ast::Defaultness::Final,
85 generics: Generics::default(),
89 let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
91 let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
92 cx.stmt_item(sig_span, item)