1 use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafety};
2 use syntax::ast::{self, Param, 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};
7 use syntax::symbol::{kw, sym, Symbol};
11 ecx: &mut ExtCtxt<'_>,
13 meta_item: &ast::MetaItem,
15 ) -> Vec<Annotatable> {
16 check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
18 let not_static = |item: Annotatable| {
19 ecx.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
22 let item = match item {
23 Annotatable::Item(item) => match item.node {
24 ItemKind::Static(..) => item,
25 _ => return not_static(Annotatable::Item(item)),
27 _ => return not_static(item),
30 // Generate a bunch of new items using the AllocFnFactory
31 let span = ecx.with_def_site_ctxt(item.span);
32 let f = AllocFnFactory {
34 kind: AllocatorKind::Global,
39 // Generate item statements for the allocator methods.
40 let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
42 // Generate anonymous constant serving as container for the allocator methods.
43 let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
44 let const_body = ecx.expr_block(ecx.block(span, stmts));
46 ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
48 // Return the original item and the new methods.
49 vec![Annotatable::Item(item), Annotatable::Item(const_item)]
52 struct AllocFnFactory<'a, 'b> {
59 impl AllocFnFactory<'_, '_> {
60 fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
61 let mut abi_args = Vec::new();
64 let name = self.cx.ident_of(&format!("arg{}", i), self.span);
71 .map(|ty| self.arg_ty(ty, &mut abi_args, mk))
73 let result = self.call_allocator(method.name, args);
74 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
75 let kind = ItemKind::Fn(
76 self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
78 unsafety: Unsafety::Unsafe,
82 self.cx.block_expr(output_expr),
84 let item = self.cx.item(
86 self.cx.ident_of(&self.kind.fn_name(method.name), self.span),
90 self.cx.stmt_item(self.span, item)
93 fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
94 let method = self.cx.std_path(&[
95 Symbol::intern("alloc"),
96 Symbol::intern("GlobalAlloc"),
97 Symbol::intern(method),
99 let method = self.cx.expr_path(self.cx.path(self.span, method));
100 let allocator = self.cx.path_ident(self.span, self.global);
101 let allocator = self.cx.expr_path(allocator);
102 let allocator = self.cx.expr_addr_of(self.span, allocator);
103 args.insert(0, allocator);
105 self.cx.expr_call(self.span, method, args)
108 fn attrs(&self) -> Vec<Attribute> {
109 let special = sym::rustc_std_internal_symbol;
110 let special = self.cx.meta_word(self.span, special);
111 vec![self.cx.attribute(special)]
117 args: &mut Vec<Param>,
118 ident: &mut dyn FnMut() -> Ident,
121 AllocatorTy::Layout => {
122 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
123 let ty_usize = self.cx.ty_path(usize);
126 args.push(self.cx.param(self.span, size, ty_usize.clone()));
127 args.push(self.cx.param(self.span, align, ty_usize));
129 let layout_new = self.cx.std_path(&[
130 Symbol::intern("alloc"),
131 Symbol::intern("Layout"),
132 Symbol::intern("from_size_align_unchecked"),
134 let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
135 let size = self.cx.expr_ident(self.span, size);
136 let align = self.cx.expr_ident(self.span, align);
137 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
141 AllocatorTy::Ptr => {
143 args.push(self.cx.param(self.span, ident, self.ptr_u8()));
144 let arg = self.cx.expr_ident(self.span, ident);
145 self.cx.expr_cast(self.span, arg, self.ptr_u8())
148 AllocatorTy::Usize => {
150 args.push(self.cx.param(self.span, ident, self.usize()));
151 self.cx.expr_ident(self.span, ident)
154 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
155 panic!("can't convert AllocatorTy to an argument")
160 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
162 AllocatorTy::ResultPtr => {
167 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
168 (self.ptr_u8(), expr)
171 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
173 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
174 panic!("can't convert `AllocatorTy` to an output")
179 fn usize(&self) -> P<Ty> {
180 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
181 self.cx.ty_path(usize)
184 fn ptr_u8(&self) -> P<Ty> {
185 let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
186 let ty_u8 = self.cx.ty_path(u8);
187 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)