1 use crate::util::check_builtin_macro_attribute;
3 use rustc_expand::base::{Annotatable, ExtCtxt};
4 use rustc_span::symbol::{kw, sym, Symbol};
6 use syntax::ast::{self, Attribute, Expr, FnHeader, FnSig, Generics, Ident, Param};
7 use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
8 use syntax::expand::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
12 ecx: &mut ExtCtxt<'_>,
14 meta_item: &ast::MetaItem,
16 ) -> Vec<Annotatable> {
17 check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
19 let not_static = |item: Annotatable| {
20 ecx.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
23 let item = match item {
24 Annotatable::Item(item) => match item.kind {
25 ItemKind::Static(..) => item,
26 _ => return not_static(Annotatable::Item(item)),
28 _ => return not_static(item),
31 // Generate a bunch of new items using the AllocFnFactory
32 let span = ecx.with_def_site_ctxt(item.span);
33 let f = AllocFnFactory { span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
35 // Generate item statements for the allocator methods.
36 let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
38 // Generate anonymous constant serving as container for the allocator methods.
39 let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
40 let const_body = ecx.expr_block(ecx.block(span, stmts));
41 let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
43 // Return the original item and the new methods.
44 vec![Annotatable::Item(item), Annotatable::Item(const_item)]
47 struct AllocFnFactory<'a, 'b> {
54 impl AllocFnFactory<'_, '_> {
55 fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
56 let mut abi_args = Vec::new();
59 let name = self.cx.ident_of(&format!("arg{}", i), self.span);
63 let args = method.inputs.iter().map(|ty| self.arg_ty(ty, &mut abi_args, mk)).collect();
64 let result = self.call_allocator(method.name, args);
65 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
66 let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
67 let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() };
68 let sig = FnSig { decl, header };
69 let block = Some(self.cx.block_expr(output_expr));
70 let kind = ItemKind::Fn(ast::Defaultness::Final, sig, Generics::default(), block);
71 let item = self.cx.item(
73 self.cx.ident_of(&self.kind.fn_name(method.name), self.span),
77 self.cx.stmt_item(self.span, item)
80 fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
81 let method = self.cx.std_path(&[
82 Symbol::intern("alloc"),
83 Symbol::intern("GlobalAlloc"),
84 Symbol::intern(method),
86 let method = self.cx.expr_path(self.cx.path(self.span, method));
87 let allocator = self.cx.path_ident(self.span, self.global);
88 let allocator = self.cx.expr_path(allocator);
89 let allocator = self.cx.expr_addr_of(self.span, allocator);
90 args.insert(0, allocator);
92 self.cx.expr_call(self.span, method, args)
95 fn attrs(&self) -> Vec<Attribute> {
96 let special = sym::rustc_std_internal_symbol;
97 let special = self.cx.meta_word(self.span, special);
98 vec![self.cx.attribute(special)]
104 args: &mut Vec<Param>,
105 ident: &mut dyn FnMut() -> Ident,
108 AllocatorTy::Layout => {
109 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
110 let ty_usize = self.cx.ty_path(usize);
113 args.push(self.cx.param(self.span, size, ty_usize.clone()));
114 args.push(self.cx.param(self.span, align, ty_usize));
116 let layout_new = self.cx.std_path(&[
117 Symbol::intern("alloc"),
118 Symbol::intern("Layout"),
119 Symbol::intern("from_size_align_unchecked"),
121 let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
122 let size = self.cx.expr_ident(self.span, size);
123 let align = self.cx.expr_ident(self.span, align);
124 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
128 AllocatorTy::Ptr => {
130 args.push(self.cx.param(self.span, ident, self.ptr_u8()));
131 let arg = self.cx.expr_ident(self.span, ident);
132 self.cx.expr_cast(self.span, arg, self.ptr_u8())
135 AllocatorTy::Usize => {
137 args.push(self.cx.param(self.span, ident, self.usize()));
138 self.cx.expr_ident(self.span, ident)
141 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
142 panic!("can't convert AllocatorTy to an argument")
147 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
149 AllocatorTy::ResultPtr => {
154 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
155 (self.ptr_u8(), expr)
158 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
160 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
161 panic!("can't convert `AllocatorTy` to an output")
166 fn usize(&self) -> P<Ty> {
167 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
168 self.cx.ty_path(usize)
171 fn ptr_u8(&self) -> P<Ty> {
172 let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
173 let ty_u8 = self.cx.ty_path(u8);
174 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mut)