11
11
use rustc:: middle:: allocator:: AllocatorKind ;
12
12
use rustc_errors;
13
13
use syntax:: abi:: Abi ;
14
- use syntax:: ast:: { Crate , Attribute , LitKind , StrStyle } ;
15
- use syntax:: ast:: { Unsafety , Constness , Generics , Mutability , Ty , Mac , Arg } ;
16
- use syntax:: ast:: { self , Ident , Item , ItemKind , TyKind , VisibilityKind , Expr } ;
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
17
use syntax:: attr;
18
18
use syntax:: codemap:: { dummy_spanned, respan} ;
19
- use syntax:: codemap:: { ExpnInfo , NameAndSpan , MacroAttribute } ;
19
+ use syntax:: codemap:: { ExpnInfo , MacroAttribute , NameAndSpan } ;
20
20
use syntax:: ext:: base:: ExtCtxt ;
21
21
use syntax:: ext:: base:: Resolver ;
22
22
use syntax:: ext:: build:: AstBuilder ;
@@ -31,10 +31,12 @@ use syntax_pos::{Span, DUMMY_SP};
31
31
32
32
use { AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS } ;
33
33
34
- pub fn modify ( sess : & ParseSess ,
35
- resolver : & mut Resolver ,
36
- krate : Crate ,
37
- handler : & rustc_errors:: Handler ) -> ast:: Crate {
34
+ pub fn modify (
35
+ sess : & ParseSess ,
36
+ resolver : & mut Resolver ,
37
+ krate : Crate ,
38
+ handler : & rustc_errors:: Handler ,
39
+ ) -> ast:: Crate {
38
40
ExpandAllocatorDirectives {
39
41
handler,
40
42
sess,
@@ -55,20 +57,24 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
55
57
let name = if attr:: contains_name ( & item. attrs , "global_allocator" ) {
56
58
"global_allocator"
57
59
} else {
58
- return fold:: noop_fold_item ( item, self )
60
+ return fold:: noop_fold_item ( item, self ) ;
59
61
} ;
60
62
match item. node {
61
63
ItemKind :: Static ( ..) => { }
62
64
_ => {
63
- self . handler . span_err ( item. span , "allocators must be statics" ) ;
64
- return SmallVector :: one ( item)
65
+ self . handler
66
+ . span_err ( item. span , "allocators must be statics" ) ;
67
+ return SmallVector :: one ( item) ;
65
68
}
66
69
}
67
70
68
71
if self . found {
69
- self . handler . span_err ( item. span , "cannot define more than one \
70
- #[global_allocator]") ;
71
- return SmallVector :: one ( item)
72
+ self . handler . span_err (
73
+ item. span ,
74
+ "cannot define more than one \
75
+ #[global_allocator]",
76
+ ) ;
77
+ return SmallVector :: one ( item) ;
72
78
}
73
79
self . found = true ;
74
80
@@ -80,7 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
80
86
span : None ,
81
87
allow_internal_unstable : true ,
82
88
allow_internal_unsafe : false ,
83
- }
89
+ } ,
84
90
} ) ;
85
91
let span = item. span . with_ctxt ( SyntaxContext :: empty ( ) . apply_mark ( mark) ) ;
86
92
let ecfg = ExpansionConfig :: default ( name. to_string ( ) ) ;
@@ -91,10 +97,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
91
97
core : Ident :: from_str ( "core" ) ,
92
98
cx : ExtCtxt :: new ( self . sess , ecfg, self . resolver ) ,
93
99
} ;
94
- let super_path = f. cx . path ( f. span , vec ! [
95
- Ident :: from_str( "super" ) ,
96
- f. global,
97
- ] ) ;
100
+ let super_path = f. cx . path ( f. span , vec ! [ Ident :: from_str( "super" ) , f. global] ) ;
98
101
let mut items = vec ! [
99
102
f. cx. item_extern_crate( f. span, f. core) ,
100
103
f. cx. item_use_simple(
@@ -114,7 +117,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
114
117
let mut ret = SmallVector :: new ( ) ;
115
118
ret. push ( item) ;
116
119
ret. push ( module) ;
117
- return ret
120
+ return ret;
118
121
}
119
122
120
123
fn fold_mac ( & mut self , mac : Mac ) -> Mac {
@@ -139,30 +142,39 @@ impl<'a> AllocFnFactory<'a> {
139
142
i += 1 ;
140
143
name
141
144
} ;
142
- let args = method. inputs . iter ( ) . map ( |ty| {
143
- self . arg_ty ( ty, & mut abi_args, mk)
144
- } ) . collect ( ) ;
145
+ let args = method
146
+ . inputs
147
+ . iter ( )
148
+ . map ( |ty| self . arg_ty ( ty, & mut abi_args, mk) )
149
+ . collect ( ) ;
145
150
let result = self . call_allocator ( method. name , args) ;
146
151
let ( output_ty, output_expr) = self . ret_ty ( & method. output , result) ;
147
- let kind = ItemKind :: Fn ( self . cx . fn_decl ( abi_args, ast:: FunctionRetTy :: Ty ( output_ty) ) ,
148
- Unsafety :: Unsafe ,
149
- dummy_spanned ( Constness :: NotConst ) ,
150
- Abi :: Rust ,
151
- Generics :: default ( ) ,
152
- self . cx . block_expr ( output_expr) ) ;
153
- self . cx . item ( self . span ,
154
- Ident :: from_str ( & self . kind . fn_name ( method. name ) ) ,
155
- self . attrs ( ) ,
156
- kind)
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
+ )
157
166
}
158
167
159
168
fn call_allocator ( & self , method : & str , mut args : Vec < P < Expr > > ) -> P < Expr > {
160
- let method = self . cx . path ( self . span , vec ! [
161
- self . core,
162
- Ident :: from_str( "alloc" ) ,
163
- Ident :: from_str( "GlobalAlloc" ) ,
164
- Ident :: from_str( method) ,
165
- ] ) ;
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
+ ) ;
166
178
let method = self . cx . expr_path ( method) ;
167
179
let allocator = self . cx . path_ident ( self . span , self . global ) ;
168
180
let allocator = self . cx . expr_path ( allocator) ;
@@ -189,10 +201,12 @@ impl<'a> AllocFnFactory<'a> {
189
201
]
190
202
}
191
203
192
- fn arg_ty ( & self ,
193
- ty : & AllocatorTy ,
194
- args : & mut Vec < Arg > ,
195
- ident : & mut FnMut ( ) -> Ident ) -> P < Expr > {
204
+ fn arg_ty (
205
+ & self ,
206
+ ty : & AllocatorTy ,
207
+ args : & mut Vec < Arg > ,
208
+ ident : & mut FnMut ( ) -> Ident ,
209
+ ) -> P < Expr > {
196
210
match * ty {
197
211
AllocatorTy :: Layout => {
198
212
let usize = self . cx . path_ident ( self . span , Ident :: from_str ( "usize" ) ) ;
@@ -202,18 +216,19 @@ impl<'a> AllocFnFactory<'a> {
202
216
args. push ( self . cx . arg ( self . span , size, ty_usize. clone ( ) ) ) ;
203
217
args. push ( self . cx . arg ( self . span , align, ty_usize) ) ;
204
218
205
- let layout_new = self . cx . path ( self . span , vec ! [
206
- self . core,
207
- Ident :: from_str( "alloc" ) ,
208
- Ident :: from_str( "Layout" ) ,
209
- Ident :: from_str( "from_size_align_unchecked" ) ,
210
- ] ) ;
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
+ ) ;
211
228
let layout_new = self . cx . expr_path ( layout_new) ;
212
229
let size = self . cx . expr_ident ( self . span , size) ;
213
230
let align = self . cx . expr_ident ( self . span , align) ;
214
- let layout = self . cx . expr_call ( self . span ,
215
- layout_new,
216
- vec ! [ size, align] ) ;
231
+ let layout = self . cx . expr_call ( self . span , layout_new, vec ! [ size, align] ) ;
217
232
layout
218
233
}
219
234
@@ -230,9 +245,7 @@ impl<'a> AllocFnFactory<'a> {
230
245
self . cx . expr_ident ( self . span , ident)
231
246
}
232
247
233
- AllocatorTy :: ResultPtr |
234
- AllocatorTy :: Bang |
235
- AllocatorTy :: Unit => {
248
+ AllocatorTy :: ResultPtr | AllocatorTy :: Bang | AllocatorTy :: Unit => {
236
249
panic ! ( "can't convert AllocatorTy to an argument" )
237
250
}
238
251
}
@@ -249,17 +262,11 @@ impl<'a> AllocFnFactory<'a> {
249
262
( self . ptr_u8 ( ) , expr)
250
263
}
251
264
252
- AllocatorTy :: Bang => {
253
- ( self . cx . ty ( self . span , TyKind :: Never ) , expr)
254
- }
265
+ AllocatorTy :: Bang => ( self . cx . ty ( self . span , TyKind :: Never ) , expr) ,
255
266
256
- AllocatorTy :: Unit => {
257
- ( self . cx . ty ( self . span , TyKind :: Tup ( Vec :: new ( ) ) ) , expr)
258
- }
267
+ AllocatorTy :: Unit => ( self . cx . ty ( self . span , TyKind :: Tup ( Vec :: new ( ) ) ) , expr) ,
259
268
260
- AllocatorTy :: Layout |
261
- AllocatorTy :: Usize |
262
- AllocatorTy :: Ptr => {
269
+ AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
263
270
panic ! ( "can't convert AllocatorTy to an output" )
264
271
}
265
272
}
@@ -277,11 +284,14 @@ impl<'a> AllocFnFactory<'a> {
277
284
}
278
285
279
286
fn ptr_opaque ( & self ) -> P < Ty > {
280
- let opaque = self . cx . path ( self . span , vec ! [
281
- self . core,
282
- Ident :: from_str( "alloc" ) ,
283
- Ident :: from_str( "Opaque" ) ,
284
- ] ) ;
287
+ let opaque = self . cx . path (
288
+ self . span ,
289
+ vec ! [
290
+ self . core,
291
+ Ident :: from_str( "alloc" ) ,
292
+ Ident :: from_str( "Opaque" ) ,
293
+ ] ,
294
+ ) ;
285
295
let ty_opaque = self . cx . ty_path ( opaque) ;
286
296
self . cx . ty_ptr ( self . span , ty_opaque, Mutability :: Mutable )
287
297
}
0 commit comments