1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::middle::allocator::AllocatorKind;
13 use rustc_target::spec::abi::Abi;
14 use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
15 use syntax::ast::{Arg, Constness, Generics, Mac, Mutability, Ty, Unsafety};
16 use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
18 use syntax::codemap::{dummy_spanned, respan};
19 use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
20 use syntax::ext::base::ExtCtxt;
21 use syntax::ext::base::Resolver;
22 use syntax::ext::build::AstBuilder;
23 use syntax::ext::expand::ExpansionConfig;
24 use syntax::ext::hygiene::{self, Mark, SyntaxContext};
25 use syntax::fold::{self, Folder};
26 use syntax::parse::ParseSess;
28 use syntax::symbol::Symbol;
29 use syntax::util::small_vector::SmallVector;
30 use syntax_pos::{Span, DUMMY_SP};
32 use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
36 resolver: &mut Resolver,
38 handler: &rustc_errors::Handler,
40 ExpandAllocatorDirectives {
48 struct ExpandAllocatorDirectives<'a> {
50 handler: &'a rustc_errors::Handler,
52 resolver: &'a mut Resolver,
55 impl<'a> Folder for ExpandAllocatorDirectives<'a> {
56 fn fold_item(&mut self, item: P<Item>) -> SmallVector<P<Item>> {
57 let name = if attr::contains_name(&item.attrs, "global_allocator") {
60 return fold::noop_fold_item(item, self);
63 ItemKind::Static(..) => {}
66 .span_err(item.span, "allocators must be statics");
67 return SmallVector::one(item);
72 self.handler.span_err(
74 "cannot define more than one \
77 return SmallVector::one(item);
81 let mark = Mark::fresh(Mark::root());
82 mark.set_expn_info(ExpnInfo {
85 format: MacroAttribute(Symbol::intern(name)),
87 allow_internal_unstable: true,
88 allow_internal_unsafe: false,
89 edition: hygiene::default_edition(),
92 let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
93 let ecfg = ExpansionConfig::default(name.to_string());
94 let mut f = AllocFnFactory {
96 kind: AllocatorKind::Global,
98 core: Ident::from_str("core"),
99 cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
101 let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
102 let mut items = vec![
103 f.cx.item_extern_crate(f.span, f.core),
104 f.cx.item_use_simple(
106 respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
110 for method in ALLOCATOR_METHODS {
111 items.push(f.allocator_fn(method));
113 let name = f.kind.fn_name("allocator_abi");
114 let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
115 let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
116 let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
118 let mut ret = SmallVector::new();
124 fn fold_mac(&mut self, mac: Mac) -> Mac {
125 fold::noop_fold_mac(mac, self)
129 struct AllocFnFactory<'a> {
137 impl<'a> AllocFnFactory<'a> {
138 fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
139 let mut abi_args = Vec::new();
141 let ref mut mk = || {
142 let name = Ident::from_str(&format!("arg{}", i));
149 .map(|ty| self.arg_ty(ty, &mut abi_args, mk))
151 let result = self.call_allocator(method.name, args);
152 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
153 let kind = ItemKind::Fn(
154 self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
156 dummy_spanned(Constness::NotConst),
159 self.cx.block_expr(output_expr),
163 Ident::from_str(&self.kind.fn_name(method.name)),
169 fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
170 let method = self.cx.path(
174 Ident::from_str("alloc"),
175 Ident::from_str("GlobalAlloc"),
176 Ident::from_str(method),
179 let method = self.cx.expr_path(method);
180 let allocator = self.cx.path_ident(self.span, self.global);
181 let allocator = self.cx.expr_path(allocator);
182 let allocator = self.cx.expr_addr_of(self.span, allocator);
183 args.insert(0, allocator);
185 self.cx.expr_call(self.span, method, args)
188 fn attrs(&self) -> Vec<Attribute> {
189 let key = Symbol::intern("linkage");
190 let value = LitKind::Str(Symbol::intern("external"), StrStyle::Cooked);
191 let linkage = self.cx.meta_name_value(self.span, key, value);
193 let no_mangle = Symbol::intern("no_mangle");
194 let no_mangle = self.cx.meta_word(self.span, no_mangle);
196 let special = Symbol::intern("rustc_std_internal_symbol");
197 let special = self.cx.meta_word(self.span, special);
199 self.cx.attribute(self.span, linkage),
200 self.cx.attribute(self.span, no_mangle),
201 self.cx.attribute(self.span, special),
209 ident: &mut FnMut() -> Ident,
212 AllocatorTy::Layout => {
213 let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
214 let ty_usize = self.cx.ty_path(usize);
217 args.push(self.cx.arg(self.span, size, ty_usize.clone()));
218 args.push(self.cx.arg(self.span, align, ty_usize));
220 let layout_new = self.cx.path(
224 Ident::from_str("alloc"),
225 Ident::from_str("Layout"),
226 Ident::from_str("from_size_align_unchecked"),
229 let layout_new = self.cx.expr_path(layout_new);
230 let size = self.cx.expr_ident(self.span, size);
231 let align = self.cx.expr_ident(self.span, align);
232 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
236 AllocatorTy::Ptr => {
238 args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
239 let arg = self.cx.expr_ident(self.span, ident);
240 self.cx.expr_cast(self.span, arg, self.ptr_u8())
243 AllocatorTy::Usize => {
245 args.push(self.cx.arg(self.span, ident, self.usize()));
246 self.cx.expr_ident(self.span, ident)
249 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
250 panic!("can't convert AllocatorTy to an argument")
255 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
257 AllocatorTy::ResultPtr => {
262 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
263 (self.ptr_u8(), expr)
266 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
268 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
269 panic!("can't convert AllocatorTy to an output")
274 fn usize(&self) -> P<Ty> {
275 let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
276 self.cx.ty_path(usize)
279 fn ptr_u8(&self) -> P<Ty> {
280 let u8 = self.cx.path_ident(self.span, Ident::from_str("u8"));
281 let ty_u8 = self.cx.ty_path(u8);
282 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)