1 use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafety};
2 use syntax::ast::{self, Arg, Attribute, Expr, FnHeader, Generics, Ident};
3 use syntax::attr::check_builtin_macro_attribute;
4 use syntax::ext::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
5 use syntax::ext::base::{Annotatable, ExtCtxt};
6 use syntax::ext::hygiene::SyntaxContext;
8 use syntax::symbol::{kw, sym, Symbol};
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.node {
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 = item.span.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.id));
33 let f = AllocFnFactory {
35 kind: AllocatorKind::Global,
40 // Generate item statements for the allocator methods.
41 let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
43 // Generate anonymous constant serving as container for the allocator methods.
44 let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
45 let const_body = ecx.expr_block(ecx.block(span, stmts));
47 ecx.item_const(span, Ident::with_empty_ctxt(kw::Underscore), const_ty, const_body);
49 // Return the original item and the new methods.
50 vec![Annotatable::Item(item), Annotatable::Item(const_item)]
53 struct AllocFnFactory<'a, 'b> {
60 impl AllocFnFactory<'_, '_> {
61 fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
62 let mut abi_args = Vec::new();
65 let name = Ident::from_str(&format!("arg{}", i));
72 .map(|ty| self.arg_ty(ty, &mut abi_args, mk))
74 let result = self.call_allocator(method.name, args);
75 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
76 let kind = ItemKind::Fn(
77 self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
79 unsafety: Unsafety::Unsafe,
83 self.cx.block_expr(output_expr),
85 let item = self.cx.item(
87 Ident::from_str(&self.kind.fn_name(method.name)),
91 self.cx.stmt_item(self.span, item)
94 fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
95 let method = self.cx.std_path(&[
96 Symbol::intern("alloc"),
97 Symbol::intern("GlobalAlloc"),
98 Symbol::intern(method),
100 let method = self.cx.expr_path(self.cx.path(self.span, method));
101 let allocator = self.cx.path_ident(self.span, self.global);
102 let allocator = self.cx.expr_path(allocator);
103 let allocator = self.cx.expr_addr_of(self.span, allocator);
104 args.insert(0, allocator);
106 self.cx.expr_call(self.span, method, args)
109 fn attrs(&self) -> Vec<Attribute> {
110 let special = sym::rustc_std_internal_symbol;
111 let special = self.cx.meta_word(self.span, special);
112 vec![self.cx.attribute(special)]
119 ident: &mut dyn FnMut() -> Ident,
122 AllocatorTy::Layout => {
123 let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
124 let ty_usize = self.cx.ty_path(usize);
127 args.push(self.cx.arg(self.span, size, ty_usize.clone()));
128 args.push(self.cx.arg(self.span, align, ty_usize));
130 let layout_new = self.cx.std_path(&[
131 Symbol::intern("alloc"),
132 Symbol::intern("Layout"),
133 Symbol::intern("from_size_align_unchecked"),
135 let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
136 let size = self.cx.expr_ident(self.span, size);
137 let align = self.cx.expr_ident(self.span, align);
138 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
142 AllocatorTy::Ptr => {
144 args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
145 let arg = self.cx.expr_ident(self.span, ident);
146 self.cx.expr_cast(self.span, arg, self.ptr_u8())
149 AllocatorTy::Usize => {
151 args.push(self.cx.arg(self.span, ident, self.usize()));
152 self.cx.expr_ident(self.span, ident)
155 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
156 panic!("can't convert AllocatorTy to an argument")
161 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
163 AllocatorTy::ResultPtr => {
168 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
169 (self.ptr_u8(), expr)
172 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
174 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
175 panic!("can't convert `AllocatorTy` to an output")
180 fn usize(&self) -> P<Ty> {
181 let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
182 self.cx.ty_path(usize)
185 fn ptr_u8(&self) -> P<Ty> {
186 let u8 = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::u8));
187 let ty_u8 = self.cx.ty_path(u8);
188 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)