1
- use syntax:: ast:: { ItemKind , Mutability , Ty , TyKind , Unsafety , VisibilityKind } ;
2
- use syntax:: ast:: { self , Arg , Attribute , Expr , FnHeader , Generics , Ident , Item } ;
1
+ use syntax:: ast:: { ItemKind , Mutability , Stmt , Ty , TyKind , Unsafety } ;
2
+ use syntax:: ast:: { self , Arg , Attribute , Expr , FnHeader , Generics , Ident } ;
3
3
use syntax:: attr:: check_builtin_macro_attribute;
4
4
use syntax:: ext:: allocator:: { AllocatorKind , AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS } ;
5
5
use syntax:: ext:: base:: { Annotatable , ExtCtxt } ;
6
6
use syntax:: ext:: build:: AstBuilder ;
7
7
use syntax:: ext:: hygiene:: SyntaxContext ;
8
8
use syntax:: ptr:: P ;
9
- use syntax:: source_map:: respan;
10
- use syntax:: symbol:: { kw, sym} ;
9
+ use syntax:: symbol:: { kw, sym, Symbol } ;
11
10
use syntax_pos:: Span ;
12
11
13
12
pub fn expand (
@@ -36,52 +35,31 @@ pub fn expand(
36
35
span,
37
36
kind : AllocatorKind :: Global ,
38
37
global : item. ident ,
39
- core : Ident :: with_empty_ctxt ( sym:: core) ,
40
38
cx : ecx,
41
39
} ;
42
40
43
- // We will generate a new submodule. To `use` the static from that module, we need to get
44
- // the `super::...` path.
45
- let super_path = f. cx . path ( f. span , vec ! [ Ident :: with_empty_ctxt( kw:: Super ) , f. global] ) ;
46
-
47
- // Generate the items in the submodule
48
- let mut items = vec ! [
49
- // import `core` to use allocators
50
- f. cx. item_extern_crate( f. span, f. core) ,
51
- // `use` the `global_allocator` in `super`
52
- f. cx. item_use_simple(
53
- f. span,
54
- respan( f. span. shrink_to_lo( ) , VisibilityKind :: Inherited ) ,
55
- super_path,
56
- ) ,
57
- ] ;
58
-
59
- // Add the allocator methods to the submodule
60
- items. extend (
61
- ALLOCATOR_METHODS
62
- . iter ( )
63
- . map ( |method| f. allocator_fn ( method) ) ,
64
- ) ;
41
+ // Generate item statements for the allocator methods.
42
+ let stmts = ALLOCATOR_METHODS . iter ( ) . map ( |method| f. allocator_fn ( method) ) . collect ( ) ;
65
43
66
- // Generate the submodule itself
67
- let name = f. kind . fn_name ( "allocator_abi" ) ;
68
- let allocator_abi = Ident :: from_str ( & name) . gensym ( ) ;
69
- let module = f. cx . item_mod ( span, span, allocator_abi, Vec :: new ( ) , items) ;
44
+ // Generate anonymous constant serving as container for the allocator methods.
45
+ let const_ty = ecx. ty ( span, TyKind :: Tup ( Vec :: new ( ) ) ) ;
46
+ let const_body = ecx. expr_block ( ecx. block ( span, stmts) ) ;
47
+ let const_item =
48
+ ecx. item_const ( span, Ident :: with_empty_ctxt ( kw:: Underscore ) , const_ty, const_body) ;
70
49
71
- // Return the item and new submodule
72
- vec ! [ Annotatable :: Item ( item) , Annotatable :: Item ( module ) ]
50
+ // Return the original item and the new methods.
51
+ vec ! [ Annotatable :: Item ( item) , Annotatable :: Item ( const_item ) ]
73
52
}
74
53
75
54
struct AllocFnFactory < ' a , ' b > {
76
55
span : Span ,
77
56
kind : AllocatorKind ,
78
57
global : Ident ,
79
- core : Ident ,
80
58
cx : & ' b ExtCtxt < ' a > ,
81
59
}
82
60
83
61
impl AllocFnFactory < ' _ , ' _ > {
84
- fn allocator_fn ( & self , method : & AllocatorMethod ) -> P < Item > {
62
+ fn allocator_fn ( & self , method : & AllocatorMethod ) -> Stmt {
85
63
let mut abi_args = Vec :: new ( ) ;
86
64
let mut i = 0 ;
87
65
let ref mut mk = || {
@@ -105,25 +83,22 @@ impl AllocFnFactory<'_, '_> {
105
83
Generics :: default ( ) ,
106
84
self . cx . block_expr ( output_expr) ,
107
85
) ;
108
- self . cx . item (
86
+ let item = self . cx . item (
109
87
self . span ,
110
88
Ident :: from_str ( & self . kind . fn_name ( method. name ) ) ,
111
89
self . attrs ( ) ,
112
90
kind,
113
- )
91
+ ) ;
92
+ self . cx . stmt_item ( self . span , item)
114
93
}
115
94
116
95
fn call_allocator ( & self , method : & str , mut args : Vec < P < Expr > > ) -> P < Expr > {
117
- let method = self . cx . path (
118
- self . span ,
119
- vec ! [
120
- self . core,
121
- Ident :: from_str( "alloc" ) ,
122
- Ident :: from_str( "GlobalAlloc" ) ,
123
- Ident :: from_str( method) ,
124
- ] ,
125
- ) ;
126
- let method = self . cx . expr_path ( method) ;
96
+ let method = self . cx . std_path ( & [
97
+ Symbol :: intern ( "alloc" ) ,
98
+ Symbol :: intern ( "GlobalAlloc" ) ,
99
+ Symbol :: intern ( method) ,
100
+ ] ) ;
101
+ let method = self . cx . expr_path ( self . cx . path ( self . span , method) ) ;
127
102
let allocator = self . cx . path_ident ( self . span , self . global ) ;
128
103
let allocator = self . cx . expr_path ( allocator) ;
129
104
let allocator = self . cx . expr_addr_of ( self . span , allocator) ;
@@ -153,16 +128,12 @@ impl AllocFnFactory<'_, '_> {
153
128
args. push ( self . cx . arg ( self . span , size, ty_usize. clone ( ) ) ) ;
154
129
args. push ( self . cx . arg ( self . span , align, ty_usize) ) ;
155
130
156
- let layout_new = self . cx . path (
157
- self . span ,
158
- vec ! [
159
- self . core,
160
- Ident :: from_str( "alloc" ) ,
161
- Ident :: from_str( "Layout" ) ,
162
- Ident :: from_str( "from_size_align_unchecked" ) ,
163
- ] ,
164
- ) ;
165
- let layout_new = self . cx . expr_path ( layout_new) ;
131
+ let layout_new = self . cx . std_path ( & [
132
+ Symbol :: intern ( "alloc" ) ,
133
+ Symbol :: intern ( "Layout" ) ,
134
+ Symbol :: intern ( "from_size_align_unchecked" ) ,
135
+ ] ) ;
136
+ let layout_new = self . cx . expr_path ( self . cx . path ( self . span , layout_new) ) ;
166
137
let size = self . cx . expr_ident ( self . span , size) ;
167
138
let align = self . cx . expr_ident ( self . span , align) ;
168
139
let layout = self . cx . expr_call ( self . span , layout_new, vec ! [ size, align] ) ;
0 commit comments