2019-10-08 14:15:26 +02:00
|
|
|
use crate::util::check_builtin_macro_attribute;
|
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::expand::allocator::{
|
|
|
|
AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
|
|
|
|
};
|
|
|
|
use rustc_ast::ptr::P;
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param};
|
|
|
|
use rustc_ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-07-19 00:24:58 +03:00
|
|
|
pub fn expand(
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
2019-07-19 02:10:36 +03:00
|
|
|
_span: Span,
|
2019-07-19 00:24:58 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable,
|
|
|
|
) -> Vec<Annotatable> {
|
2019-07-19 02:10:36 +03:00
|
|
|
check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
|
2018-05-20 17:04:00 -05:00
|
|
|
|
2019-07-19 00:24:58 +03:00
|
|
|
let not_static = |item: Annotatable| {
|
2020-07-30 11:27:50 +10:00
|
|
|
ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
|
2019-07-19 00:24:58 +03:00
|
|
|
vec![item]
|
|
|
|
};
|
|
|
|
let item = match item {
|
2019-09-26 17:51:36 +01:00
|
|
|
Annotatable::Item(item) => match item.kind {
|
2019-07-19 00:24:58 +03:00
|
|
|
ItemKind::Static(..) => item,
|
|
|
|
_ => return not_static(Annotatable::Item(item)),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2019-07-19 00:24:58 +03:00
|
|
|
_ => return not_static(item),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate a bunch of new items using the AllocFnFactory
|
2019-09-14 21:17:11 +01:00
|
|
|
let span = ecx.with_def_site_ctxt(item.span);
|
2019-12-22 17:42:04 -05:00
|
|
|
let f = AllocFnFactory { span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
|
2019-07-19 00:24:58 +03:00
|
|
|
|
2019-07-19 02:51:07 +03:00
|
|
|
// Generate item statements for the allocator methods.
|
|
|
|
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-07-19 02:51:07 +03:00
|
|
|
// Generate anonymous constant serving as container for the allocator methods.
|
|
|
|
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
|
|
|
|
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
2019-12-22 17:42:04 -05:00
|
|
|
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
|
2018-06-05 19:24:10 -05:00
|
|
|
|
2019-07-19 02:51:07 +03:00
|
|
|
// Return the original item and the new methods.
|
|
|
|
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
|
2019-07-19 00:24:58 +03:00
|
|
|
struct AllocFnFactory<'a, 'b> {
|
2017-06-03 14:54:08 -07:00
|
|
|
span: Span,
|
|
|
|
kind: AllocatorKind,
|
|
|
|
global: Ident,
|
2019-07-19 00:24:58 +03:00
|
|
|
cx: &'b ExtCtxt<'a>,
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
|
2019-07-19 00:24:58 +03:00
|
|
|
impl AllocFnFactory<'_, '_> {
|
2019-07-19 02:51:07 +03:00
|
|
|
fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
|
2017-06-03 14:54:08 -07:00
|
|
|
let mut abi_args = Vec::new();
|
|
|
|
let mut i = 0;
|
2020-03-05 13:56:01 +01:00
|
|
|
let mut mk = || {
|
2020-07-14 15:05:26 +10:00
|
|
|
let name = Ident::from_str_and_span(&format!("arg{}", i), self.span);
|
2017-06-03 14:54:08 -07:00
|
|
|
i += 1;
|
|
|
|
name
|
|
|
|
};
|
2020-03-05 13:56:01 +01:00
|
|
|
let args = method.inputs.iter().map(|ty| self.arg_ty(ty, &mut abi_args, &mut mk)).collect();
|
2017-06-03 14:54:08 -07:00
|
|
|
let result = self.call_allocator(method.name, args);
|
2018-04-03 17:12:57 +02:00
|
|
|
let (output_ty, output_expr) = self.ret_ty(&method.output, result);
|
2020-02-15 12:10:59 +09:00
|
|
|
let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
|
2020-01-30 02:42:33 +01:00
|
|
|
let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() };
|
2020-08-12 17:02:14 -04:00
|
|
|
let sig = FnSig { decl, header, span: self.span };
|
2020-02-23 10:24:30 +01:00
|
|
|
let block = Some(self.cx.block_expr(output_expr));
|
|
|
|
let kind = ItemKind::Fn(ast::Defaultness::Final, sig, Generics::default(), block);
|
2019-07-19 02:51:07 +03:00
|
|
|
let item = self.cx.item(
|
2018-04-13 15:58:16 -05:00
|
|
|
self.span,
|
2020-07-14 15:05:26 +10:00
|
|
|
Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span),
|
2018-04-13 15:58:16 -05:00
|
|
|
self.attrs(),
|
|
|
|
kind,
|
2019-07-19 02:51:07 +03:00
|
|
|
);
|
|
|
|
self.cx.stmt_item(self.span, item)
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
fn call_allocator(&self, method: Symbol, mut args: Vec<P<Expr>>) -> P<Expr> {
|
|
|
|
let method = self.cx.std_path(&[sym::alloc, sym::GlobalAlloc, method]);
|
2019-07-19 02:51:07 +03:00
|
|
|
let method = self.cx.expr_path(self.cx.path(self.span, method));
|
2017-06-03 14:54:08 -07:00
|
|
|
let allocator = self.cx.path_ident(self.span, self.global);
|
|
|
|
let allocator = self.cx.expr_path(allocator);
|
|
|
|
let allocator = self.cx.expr_addr_of(self.span, allocator);
|
|
|
|
args.insert(0, allocator);
|
|
|
|
|
|
|
|
self.cx.expr_call(self.span, method, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn attrs(&self) -> Vec<Attribute> {
|
2019-05-22 12:42:23 +10:00
|
|
|
let special = sym::rustc_std_internal_symbol;
|
2017-11-01 13:16:36 -07:00
|
|
|
let special = self.cx.meta_word(self.span, special);
|
2019-07-30 14:12:52 -04:00
|
|
|
vec![self.cx.attribute(special)]
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
|
2018-04-13 15:58:16 -05:00
|
|
|
fn arg_ty(
|
|
|
|
&self,
|
|
|
|
ty: &AllocatorTy,
|
2019-08-27 13:24:32 +02:00
|
|
|
args: &mut Vec<Param>,
|
2018-07-11 12:08:49 +02:00
|
|
|
ident: &mut dyn FnMut() -> Ident,
|
2018-04-13 15:58:16 -05:00
|
|
|
) -> P<Expr> {
|
2017-06-03 14:54:08 -07:00
|
|
|
match *ty {
|
|
|
|
AllocatorTy::Layout => {
|
2019-09-14 21:16:51 +01:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
2017-06-03 14:54:08 -07:00
|
|
|
let ty_usize = self.cx.ty_path(usize);
|
|
|
|
let size = ident();
|
|
|
|
let align = ident();
|
2019-08-27 13:24:32 +02:00
|
|
|
args.push(self.cx.param(self.span, size, ty_usize.clone()));
|
|
|
|
args.push(self.cx.param(self.span, align, ty_usize));
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
let layout_new =
|
|
|
|
self.cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
|
2019-07-19 02:51:07 +03:00
|
|
|
let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
|
2017-06-03 14:54:08 -07:00
|
|
|
let size = self.cx.expr_ident(self.span, size);
|
|
|
|
let align = self.cx.expr_ident(self.span, align);
|
2018-04-13 15:58:16 -05:00
|
|
|
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
|
2017-06-03 14:54:08 -07:00
|
|
|
layout
|
|
|
|
}
|
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
AllocatorTy::Ptr => {
|
2017-06-03 14:54:08 -07:00
|
|
|
let ident = ident();
|
2019-08-27 13:24:32 +02:00
|
|
|
args.push(self.cx.param(self.span, ident, self.ptr_u8()));
|
2018-04-03 17:12:57 +02:00
|
|
|
let arg = self.cx.expr_ident(self.span, ident);
|
2018-05-31 15:57:43 +09:00
|
|
|
self.cx.expr_cast(self.span, arg, self.ptr_u8())
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
AllocatorTy::Usize => {
|
2017-06-03 14:54:08 -07:00
|
|
|
let ident = ident();
|
2019-08-27 13:24:32 +02:00
|
|
|
args.push(self.cx.param(self.span, ident, self.usize()));
|
2017-06-03 14:54:08 -07:00
|
|
|
self.cx.expr_ident(self.span, ident)
|
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:59 -07:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => {
|
2017-06-03 14:54:08 -07:00
|
|
|
panic!("can't convert AllocatorTy to an argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
|
2017-06-03 14:54:08 -07:00
|
|
|
match *ty {
|
|
|
|
AllocatorTy::ResultPtr => {
|
|
|
|
// We're creating:
|
|
|
|
//
|
2018-04-03 17:12:57 +02:00
|
|
|
// #expr as *mut u8
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
|
2017-06-03 14:54:08 -07:00
|
|
|
(self.ptr_u8(), expr)
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:58:16 -05:00
|
|
|
AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2018-04-13 15:58:16 -05:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
2019-07-22 22:52:13 +02:00
|
|
|
panic!("can't convert `AllocatorTy` to an output")
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
fn usize(&self) -> P<Ty> {
|
2019-09-14 21:16:51 +01:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
2018-04-03 17:12:57 +02:00
|
|
|
self.cx.ty_path(usize)
|
|
|
|
}
|
|
|
|
|
2017-06-03 14:54:08 -07:00
|
|
|
fn ptr_u8(&self) -> P<Ty> {
|
2019-09-14 21:16:51 +01:00
|
|
|
let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
|
2017-06-03 14:54:08 -07:00
|
|
|
let ty_u8 = self.cx.ty_path(u8);
|
2019-12-16 17:28:40 +01:00
|
|
|
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mut)
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
}
|