1 use crate::util::check_builtin_macro_attribute;
3 use rustc_ast::expand::allocator::{
4 AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
7 use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
8 use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
9 use rustc_expand::base::{Annotatable, ExtCtxt};
10 use rustc_span::symbol::{kw, sym, Ident, Symbol};
14 ecx: &mut ExtCtxt<'_>,
16 meta_item: &ast::MetaItem,
18 ) -> Vec<Annotatable> {
19 check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
21 let orig_item = item.clone();
23 ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
24 vec![orig_item.clone()]
27 // Allow using `#[global_allocator]` on an item statement
28 // FIXME - if we get deref patterns, use them to reduce duplication here
29 let (item, is_stmt) = match &item {
30 Annotatable::Item(item) => match item.kind {
31 ItemKind::Static(..) => (item, false),
32 _ => return not_static(),
34 Annotatable::Stmt(stmt) => match &stmt.kind {
35 StmtKind::Item(item_) => match item_.kind {
36 ItemKind::Static(..) => (item_, true),
37 _ => return not_static(),
39 _ => return not_static(),
41 _ => return not_static(),
44 // Generate a bunch of new items using the AllocFnFactory
45 let span = ecx.with_def_site_ctxt(item.span);
46 let f = AllocFnFactory { span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
48 // Generate item statements for the allocator methods.
49 let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
51 // Generate anonymous constant serving as container for the allocator methods.
52 let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
53 let const_body = ecx.expr_block(ecx.block(span, stmts));
54 let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
55 let const_item = if is_stmt {
56 Annotatable::Stmt(P(ecx.stmt_item(span, const_item)))
58 Annotatable::Item(const_item)
61 // Return the original item and the new methods.
62 vec![orig_item, const_item]
65 struct AllocFnFactory<'a, 'b> {
72 impl AllocFnFactory<'_, '_> {
73 fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
74 let mut abi_args = Vec::new();
77 let name = Ident::from_str_and_span(&format!("arg{}", i), self.span);
81 let args = method.inputs.iter().map(|ty| self.arg_ty(ty, &mut abi_args, &mut mk)).collect();
82 let result = self.call_allocator(method.name, args);
83 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
84 let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
85 let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() };
86 let sig = FnSig { decl, header, span: self.span };
87 let body = Some(self.cx.block_expr(output_expr));
88 let kind = ItemKind::Fn(Box::new(Fn {
89 defaultness: ast::Defaultness::Final,
91 generics: Generics::default(),
94 let item = self.cx.item(
96 Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span),
100 self.cx.stmt_item(self.span, item)
103 fn call_allocator(&self, method: Symbol, mut args: Vec<P<Expr>>) -> P<Expr> {
104 let method = self.cx.std_path(&[sym::alloc, sym::GlobalAlloc, method]);
105 let method = self.cx.expr_path(self.cx.path(self.span, method));
106 let allocator = self.cx.path_ident(self.span, self.global);
107 let allocator = self.cx.expr_path(allocator);
108 let allocator = self.cx.expr_addr_of(self.span, allocator);
109 args.insert(0, allocator);
111 self.cx.expr_call(self.span, method, args)
114 fn attrs(&self) -> Vec<Attribute> {
115 let special = sym::rustc_std_internal_symbol;
116 let special = self.cx.meta_word(self.span, special);
117 vec![self.cx.attribute(special)]
123 args: &mut Vec<Param>,
124 ident: &mut dyn FnMut() -> Ident,
127 AllocatorTy::Layout => {
128 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
129 let ty_usize = self.cx.ty_path(usize);
132 args.push(self.cx.param(self.span, size, ty_usize.clone()));
133 args.push(self.cx.param(self.span, align, ty_usize));
136 self.cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
137 let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
138 let size = self.cx.expr_ident(self.span, size);
139 let align = self.cx.expr_ident(self.span, align);
140 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
144 AllocatorTy::Ptr => {
146 args.push(self.cx.param(self.span, ident, self.ptr_u8()));
147 let arg = self.cx.expr_ident(self.span, ident);
148 self.cx.expr_cast(self.span, arg, self.ptr_u8())
151 AllocatorTy::Usize => {
153 args.push(self.cx.param(self.span, ident, self.usize()));
154 self.cx.expr_ident(self.span, ident)
157 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
158 panic!("can't convert AllocatorTy to an argument")
163 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
165 AllocatorTy::ResultPtr => {
170 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
171 (self.ptr_u8(), expr)
174 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
176 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
177 panic!("can't convert `AllocatorTy` to an output")
182 fn usize(&self) -> P<Ty> {
183 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
184 self.cx.ty_path(usize)
187 fn ptr_u8(&self) -> P<Ty> {
188 let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
189 let ty_u8 = self.cx.ty_path(u8);
190 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mut)