]> git.proxmox.com Git - rustc.git/blob - src/librustc_allocator/expand.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / librustc_allocator / expand.rs
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.
4 //
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.
10
11 use rustc::middle::allocator::AllocatorKind;
12 use rustc_errors;
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};
17 use syntax::attr;
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::{Mark, SyntaxContext};
25 use syntax::fold::{self, Folder};
26 use syntax::parse::ParseSess;
27 use syntax::ptr::P;
28 use syntax::symbol::Symbol;
29 use syntax::util::small_vector::SmallVector;
30 use syntax_pos::{Span, DUMMY_SP};
31
32 use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
33
34 pub fn modify(
35 sess: &ParseSess,
36 resolver: &mut Resolver,
37 krate: Crate,
38 handler: &rustc_errors::Handler,
39 ) -> ast::Crate {
40 ExpandAllocatorDirectives {
41 handler,
42 sess,
43 resolver,
44 found: false,
45 }.fold_crate(krate)
46 }
47
48 struct ExpandAllocatorDirectives<'a> {
49 found: bool,
50 handler: &'a rustc_errors::Handler,
51 sess: &'a ParseSess,
52 resolver: &'a mut Resolver,
53 }
54
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") {
58 "global_allocator"
59 } else {
60 return fold::noop_fold_item(item, self);
61 };
62 match item.node {
63 ItemKind::Static(..) => {}
64 _ => {
65 self.handler
66 .span_err(item.span, "allocators must be statics");
67 return SmallVector::one(item);
68 }
69 }
70
71 if self.found {
72 self.handler.span_err(
73 item.span,
74 "cannot define more than one \
75 #[global_allocator]",
76 );
77 return SmallVector::one(item);
78 }
79 self.found = true;
80
81 let mark = Mark::fresh(Mark::root());
82 mark.set_expn_info(ExpnInfo {
83 call_site: DUMMY_SP,
84 callee: NameAndSpan {
85 format: MacroAttribute(Symbol::intern(name)),
86 span: None,
87 allow_internal_unstable: true,
88 allow_internal_unsafe: false,
89 },
90 });
91 let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
92 let ecfg = ExpansionConfig::default(name.to_string());
93 let mut f = AllocFnFactory {
94 span,
95 kind: AllocatorKind::Global,
96 global: item.ident,
97 core: Ident::from_str("core"),
98 cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
99 };
100 let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
101 let mut items = vec![
102 f.cx.item_extern_crate(f.span, f.core),
103 f.cx.item_use_simple(
104 f.span,
105 respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
106 super_path,
107 ),
108 ];
109 for method in ALLOCATOR_METHODS {
110 items.push(f.allocator_fn(method));
111 }
112 let name = f.kind.fn_name("allocator_abi");
113 let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
114 let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
115 let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
116
117 let mut ret = SmallVector::new();
118 ret.push(item);
119 ret.push(module);
120 return ret;
121 }
122
123 fn fold_mac(&mut self, mac: Mac) -> Mac {
124 fold::noop_fold_mac(mac, self)
125 }
126 }
127
128 struct AllocFnFactory<'a> {
129 span: Span,
130 kind: AllocatorKind,
131 global: Ident,
132 core: Ident,
133 cx: ExtCtxt<'a>,
134 }
135
136 impl<'a> AllocFnFactory<'a> {
137 fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
138 let mut abi_args = Vec::new();
139 let mut i = 0;
140 let ref mut mk = || {
141 let name = Ident::from_str(&format!("arg{}", i));
142 i += 1;
143 name
144 };
145 let args = method
146 .inputs
147 .iter()
148 .map(|ty| self.arg_ty(ty, &mut abi_args, mk))
149 .collect();
150 let result = self.call_allocator(method.name, args);
151 let (output_ty, output_expr) = self.ret_ty(&method.output, result);
152 let kind = ItemKind::Fn(
153 self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
154 Unsafety::Unsafe,
155 dummy_spanned(Constness::NotConst),
156 Abi::Rust,
157 Generics::default(),
158 self.cx.block_expr(output_expr),
159 );
160 self.cx.item(
161 self.span,
162 Ident::from_str(&self.kind.fn_name(method.name)),
163 self.attrs(),
164 kind,
165 )
166 }
167
168 fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
169 let method = self.cx.path(
170 self.span,
171 vec![
172 self.core,
173 Ident::from_str("alloc"),
174 Ident::from_str("GlobalAlloc"),
175 Ident::from_str(method),
176 ],
177 );
178 let method = self.cx.expr_path(method);
179 let allocator = self.cx.path_ident(self.span, self.global);
180 let allocator = self.cx.expr_path(allocator);
181 let allocator = self.cx.expr_addr_of(self.span, allocator);
182 args.insert(0, allocator);
183
184 self.cx.expr_call(self.span, method, args)
185 }
186
187 fn attrs(&self) -> Vec<Attribute> {
188 let key = Symbol::intern("linkage");
189 let value = LitKind::Str(Symbol::intern("external"), StrStyle::Cooked);
190 let linkage = self.cx.meta_name_value(self.span, key, value);
191
192 let no_mangle = Symbol::intern("no_mangle");
193 let no_mangle = self.cx.meta_word(self.span, no_mangle);
194
195 let special = Symbol::intern("rustc_std_internal_symbol");
196 let special = self.cx.meta_word(self.span, special);
197 vec![
198 self.cx.attribute(self.span, linkage),
199 self.cx.attribute(self.span, no_mangle),
200 self.cx.attribute(self.span, special),
201 ]
202 }
203
204 fn arg_ty(
205 &self,
206 ty: &AllocatorTy,
207 args: &mut Vec<Arg>,
208 ident: &mut FnMut() -> Ident,
209 ) -> P<Expr> {
210 match *ty {
211 AllocatorTy::Layout => {
212 let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
213 let ty_usize = self.cx.ty_path(usize);
214 let size = ident();
215 let align = ident();
216 args.push(self.cx.arg(self.span, size, ty_usize.clone()));
217 args.push(self.cx.arg(self.span, align, ty_usize));
218
219 let layout_new = self.cx.path(
220 self.span,
221 vec![
222 self.core,
223 Ident::from_str("alloc"),
224 Ident::from_str("Layout"),
225 Ident::from_str("from_size_align_unchecked"),
226 ],
227 );
228 let layout_new = self.cx.expr_path(layout_new);
229 let size = self.cx.expr_ident(self.span, size);
230 let align = self.cx.expr_ident(self.span, align);
231 let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
232 layout
233 }
234
235 AllocatorTy::Ptr => {
236 let ident = ident();
237 args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
238 let arg = self.cx.expr_ident(self.span, ident);
239 self.cx.expr_cast(self.span, arg, self.ptr_opaque())
240 }
241
242 AllocatorTy::Usize => {
243 let ident = ident();
244 args.push(self.cx.arg(self.span, ident, self.usize()));
245 self.cx.expr_ident(self.span, ident)
246 }
247
248 AllocatorTy::ResultPtr | AllocatorTy::Unit => {
249 panic!("can't convert AllocatorTy to an argument")
250 }
251 }
252 }
253
254 fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
255 match *ty {
256 AllocatorTy::ResultPtr => {
257 // We're creating:
258 //
259 // #expr as *mut u8
260
261 let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
262 (self.ptr_u8(), expr)
263 }
264
265 AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
266
267 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
268 panic!("can't convert AllocatorTy to an output")
269 }
270 }
271 }
272
273 fn usize(&self) -> P<Ty> {
274 let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
275 self.cx.ty_path(usize)
276 }
277
278 fn ptr_u8(&self) -> P<Ty> {
279 let u8 = self.cx.path_ident(self.span, Ident::from_str("u8"));
280 let ty_u8 = self.cx.ty_path(u8);
281 self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
282 }
283
284 fn ptr_opaque(&self) -> P<Ty> {
285 let opaque = self.cx.path(
286 self.span,
287 vec![
288 self.core,
289 Ident::from_str("alloc"),
290 Ident::from_str("Opaque"),
291 ],
292 );
293 let ty_opaque = self.cx.ty_path(opaque);
294 self.cx.ty_ptr(self.span, ty_opaque, Mutability::Mutable)
295 }
296 }