syntax_ext: Improve and simplify code generated by #[global_allocator]
Instead of ``` mod allocator_abi { /* methods */ } ``` we now generate ``` const _: () = { /* methods */ } ``` and use `std_path` for paths referring to standard library entities. This way we no longer need to generate `use` and `extern crate` imports, and `#[global_allocator]` starts working inside unnamed blocks.
This commit is contained in:
parent
76b1ffaf6c
commit
bf8fc8adfc
3 changed files with 34 additions and 61 deletions
|
@ -1,13 +1,12 @@
|
||||||
use syntax::ast::{ItemKind, Mutability, Ty, TyKind, Unsafety, VisibilityKind};
|
use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafety};
|
||||||
use syntax::ast::{self, Arg, Attribute, Expr, FnHeader, Generics, Ident, Item};
|
use syntax::ast::{self, Arg, Attribute, Expr, FnHeader, Generics, Ident};
|
||||||
use syntax::attr::check_builtin_macro_attribute;
|
use syntax::attr::check_builtin_macro_attribute;
|
||||||
use syntax::ext::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
|
use syntax::ext::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
use syntax::ext::build::AstBuilder;
|
use syntax::ext::build::AstBuilder;
|
||||||
use syntax::ext::hygiene::SyntaxContext;
|
use syntax::ext::hygiene::SyntaxContext;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
use syntax::source_map::respan;
|
use syntax::symbol::{kw, sym, Symbol};
|
||||||
use syntax::symbol::{kw, sym};
|
|
||||||
use syntax_pos::Span;
|
use syntax_pos::Span;
|
||||||
|
|
||||||
pub fn expand(
|
pub fn expand(
|
||||||
|
@ -36,52 +35,31 @@ pub fn expand(
|
||||||
span,
|
span,
|
||||||
kind: AllocatorKind::Global,
|
kind: AllocatorKind::Global,
|
||||||
global: item.ident,
|
global: item.ident,
|
||||||
core: Ident::with_empty_ctxt(sym::core),
|
|
||||||
cx: ecx,
|
cx: ecx,
|
||||||
};
|
};
|
||||||
|
|
||||||
// We will generate a new submodule. To `use` the static from that module, we need to get
|
// Generate item statements for the allocator methods.
|
||||||
// the `super::...` path.
|
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
|
||||||
let super_path = f.cx.path(f.span, vec![Ident::with_empty_ctxt(kw::Super), f.global]);
|
|
||||||
|
|
||||||
// Generate the items in the submodule
|
// Generate anonymous constant serving as container for the allocator methods.
|
||||||
let mut items = vec![
|
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
|
||||||
// import `core` to use allocators
|
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
||||||
f.cx.item_extern_crate(f.span, f.core),
|
let const_item =
|
||||||
// `use` the `global_allocator` in `super`
|
ecx.item_const(span, Ident::with_empty_ctxt(kw::Underscore), const_ty, const_body);
|
||||||
f.cx.item_use_simple(
|
|
||||||
f.span,
|
|
||||||
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
|
|
||||||
super_path,
|
|
||||||
),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Add the allocator methods to the submodule
|
// Return the original item and the new methods.
|
||||||
items.extend(
|
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
|
||||||
ALLOCATOR_METHODS
|
|
||||||
.iter()
|
|
||||||
.map(|method| f.allocator_fn(method)),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Generate the submodule itself
|
|
||||||
let name = f.kind.fn_name("allocator_abi");
|
|
||||||
let allocator_abi = Ident::from_str(&name).gensym();
|
|
||||||
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
|
|
||||||
|
|
||||||
// Return the item and new submodule
|
|
||||||
vec![Annotatable::Item(item), Annotatable::Item(module)]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AllocFnFactory<'a, 'b> {
|
struct AllocFnFactory<'a, 'b> {
|
||||||
span: Span,
|
span: Span,
|
||||||
kind: AllocatorKind,
|
kind: AllocatorKind,
|
||||||
global: Ident,
|
global: Ident,
|
||||||
core: Ident,
|
|
||||||
cx: &'b ExtCtxt<'a>,
|
cx: &'b ExtCtxt<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AllocFnFactory<'_, '_> {
|
impl AllocFnFactory<'_, '_> {
|
||||||
fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
|
fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
|
||||||
let mut abi_args = Vec::new();
|
let mut abi_args = Vec::new();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let ref mut mk = || {
|
let ref mut mk = || {
|
||||||
|
@ -105,25 +83,22 @@ impl AllocFnFactory<'_, '_> {
|
||||||
Generics::default(),
|
Generics::default(),
|
||||||
self.cx.block_expr(output_expr),
|
self.cx.block_expr(output_expr),
|
||||||
);
|
);
|
||||||
self.cx.item(
|
let item = self.cx.item(
|
||||||
self.span,
|
self.span,
|
||||||
Ident::from_str(&self.kind.fn_name(method.name)),
|
Ident::from_str(&self.kind.fn_name(method.name)),
|
||||||
self.attrs(),
|
self.attrs(),
|
||||||
kind,
|
kind,
|
||||||
)
|
);
|
||||||
|
self.cx.stmt_item(self.span, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
|
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
|
||||||
let method = self.cx.path(
|
let method = self.cx.std_path(&[
|
||||||
self.span,
|
Symbol::intern("alloc"),
|
||||||
vec![
|
Symbol::intern("GlobalAlloc"),
|
||||||
self.core,
|
Symbol::intern(method),
|
||||||
Ident::from_str("alloc"),
|
]);
|
||||||
Ident::from_str("GlobalAlloc"),
|
let method = self.cx.expr_path(self.cx.path(self.span, method));
|
||||||
Ident::from_str(method),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let method = self.cx.expr_path(method);
|
|
||||||
let allocator = self.cx.path_ident(self.span, self.global);
|
let allocator = self.cx.path_ident(self.span, self.global);
|
||||||
let allocator = self.cx.expr_path(allocator);
|
let allocator = self.cx.expr_path(allocator);
|
||||||
let allocator = self.cx.expr_addr_of(self.span, allocator);
|
let allocator = self.cx.expr_addr_of(self.span, allocator);
|
||||||
|
@ -153,16 +128,12 @@ impl AllocFnFactory<'_, '_> {
|
||||||
args.push(self.cx.arg(self.span, size, ty_usize.clone()));
|
args.push(self.cx.arg(self.span, size, ty_usize.clone()));
|
||||||
args.push(self.cx.arg(self.span, align, ty_usize));
|
args.push(self.cx.arg(self.span, align, ty_usize));
|
||||||
|
|
||||||
let layout_new = self.cx.path(
|
let layout_new = self.cx.std_path(&[
|
||||||
self.span,
|
Symbol::intern("alloc"),
|
||||||
vec![
|
Symbol::intern("Layout"),
|
||||||
self.core,
|
Symbol::intern("from_size_align_unchecked"),
|
||||||
Ident::from_str("alloc"),
|
]);
|
||||||
Ident::from_str("Layout"),
|
let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
|
||||||
Ident::from_str("from_size_align_unchecked"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let layout_new = self.cx.expr_path(layout_new);
|
|
||||||
let size = self.cx.expr_ident(self.span, size);
|
let size = self.cx.expr_ident(self.span, size);
|
||||||
let align = self.cx.expr_ident(self.span, align);
|
let align = self.cx.expr_ident(self.span, align);
|
||||||
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
|
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
|
||||||
|
|
|
@ -13,9 +13,10 @@ fn main() {
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
pub static GLOBAL: A = A(AtomicUsize::new(0));
|
pub static GLOBAL: A = A(AtomicUsize::new(0));
|
||||||
|
|
||||||
|
let n = GLOBAL.0.load(Ordering::SeqCst);
|
||||||
let s = Box::new(0);
|
let s = Box::new(0);
|
||||||
helper::work_with(&s);
|
helper::work_with(&s);
|
||||||
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 1);
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
|
||||||
drop(s);
|
drop(s);
|
||||||
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 2);
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,10 @@ mod submodule {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let n = submodule::GLOBAL.0.load(Ordering::SeqCst);
|
||||||
let s = Box::new(0);
|
let s = Box::new(0);
|
||||||
helper::work_with(&s);
|
helper::work_with(&s);
|
||||||
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), 1);
|
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 1);
|
||||||
drop(s);
|
drop(s);
|
||||||
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), 2);
|
assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue