2
2
3
3
use ir:: context:: BindgenContext ;
4
4
use ir:: layout:: Layout ;
5
- use quote ;
6
- use proc_macro2 :: { Term , Span } ;
5
+ use proc_macro2 :: { self , Ident , Span } ;
6
+ use quote :: TokenStreamExt ;
7
7
8
8
pub mod attributes {
9
- use quote;
10
- use proc_macro2:: { Term , Span } ;
9
+ use proc_macro2:: { self , Ident , Span } ;
11
10
12
- pub fn repr ( which : & str ) -> quote :: Tokens {
13
- let which = Term :: new ( which, Span :: call_site ( ) ) ;
11
+ pub fn repr ( which : & str ) -> proc_macro2 :: TokenStream {
12
+ let which = Ident :: new ( which, Span :: call_site ( ) ) ;
14
13
quote ! {
15
14
#[ repr( #which ) ]
16
15
}
17
16
}
18
17
19
- pub fn repr_list ( which_ones : & [ & str ] ) -> quote :: Tokens {
20
- let which_ones = which_ones. iter ( ) . cloned ( ) . map ( |one| Term :: new ( one, Span :: call_site ( ) ) ) ;
18
+ pub fn repr_list ( which_ones : & [ & str ] ) -> proc_macro2 :: TokenStream {
19
+ let which_ones = which_ones. iter ( ) . cloned ( ) . map ( |one| Ident :: new ( one, Span :: call_site ( ) ) ) ;
21
20
quote ! {
22
21
#[ repr( #( #which_ones ) , * ) ]
23
22
}
24
23
}
25
24
26
- pub fn derives ( which_ones : & [ & str ] ) -> quote :: Tokens {
27
- let which_ones = which_ones. iter ( ) . cloned ( ) . map ( |one| Term :: new ( one, Span :: call_site ( ) ) ) ;
25
+ pub fn derives ( which_ones : & [ & str ] ) -> proc_macro2 :: TokenStream {
26
+ let which_ones = which_ones. iter ( ) . cloned ( ) . map ( |one| Ident :: new ( one, Span :: call_site ( ) ) ) ;
28
27
quote ! {
29
28
#[ derive( #( #which_ones ) , * ) ]
30
29
}
31
30
}
32
31
33
- pub fn inline ( ) -> quote :: Tokens {
32
+ pub fn inline ( ) -> proc_macro2 :: TokenStream {
34
33
quote ! {
35
34
#[ inline]
36
35
}
37
36
}
38
37
39
- pub fn doc ( comment : String ) -> quote:: Tokens {
40
- // Doc comments are already preprocessed into nice `///` formats by the
41
- // time they get here. Just make sure that we have newlines around it so
42
- // that nothing else gets wrapped into the comment.
43
- let mut tokens = quote ! { } ;
44
- tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
45
- tokens. append ( Term :: new ( & comment, Span :: call_site ( ) ) ) ;
46
- tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
47
- tokens
38
+ pub fn doc ( comment : String ) -> proc_macro2:: TokenStream {
39
+ use std:: str:: FromStr ;
40
+
41
+ // NOTE(emilio): By this point comments are already preprocessed and in
42
+ // `///` form. Quote turns them into `#[doc]` comments, but oh well.
43
+ proc_macro2:: TokenStream :: from_str ( & comment) . unwrap ( )
48
44
}
49
45
50
- pub fn link_name ( name : & str ) -> quote :: Tokens {
46
+ pub fn link_name ( name : & str ) -> proc_macro2 :: TokenStream {
51
47
// LLVM mangles the name by default but it's already mangled.
52
48
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
53
49
let name = format ! ( "\u{1} {}" , name) ;
@@ -59,7 +55,7 @@ pub mod attributes {
59
55
60
56
/// Generates a proper type for a field or type with a given `Layout`, that is,
61
57
/// a type with the correct size and alignment restrictions.
62
- pub fn blob ( ctx : & BindgenContext , layout : Layout ) -> quote :: Tokens {
58
+ pub fn blob ( ctx : & BindgenContext , layout : Layout ) -> proc_macro2 :: TokenStream {
63
59
let opaque = layout. opaque ( ) ;
64
60
65
61
// FIXME(emilio, #412): We fall back to byte alignment, but there are
@@ -74,7 +70,7 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
74
70
}
75
71
} ;
76
72
77
- let ty_name = Term :: new ( ty_name, Span :: call_site ( ) ) ;
73
+ let ty_name = Ident :: new ( ty_name, Span :: call_site ( ) ) ;
78
74
79
75
let data_len = opaque. array_size ( ctx) . unwrap_or ( layout. size ) ;
80
76
@@ -90,14 +86,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
90
86
}
91
87
92
88
/// Integer type of the same size as the given `Layout`.
93
- pub fn integer_type ( ctx : & BindgenContext , layout : Layout ) -> Option < quote :: Tokens > {
89
+ pub fn integer_type ( ctx : & BindgenContext , layout : Layout ) -> Option < proc_macro2 :: TokenStream > {
94
90
let name = Layout :: known_type_for_size ( ctx, layout. size ) ?;
95
- let name = Term :: new ( name, Span :: call_site ( ) ) ;
91
+ let name = Ident :: new ( name, Span :: call_site ( ) ) ;
96
92
Some ( quote ! { #name } )
97
93
}
98
94
99
95
/// Generates a bitfield allocation unit type for a type with the given `Layout`.
100
- pub fn bitfield_unit ( ctx : & BindgenContext , layout : Layout ) -> quote :: Tokens {
96
+ pub fn bitfield_unit ( ctx : & BindgenContext , layout : Layout ) -> proc_macro2 :: TokenStream {
101
97
let mut tokens = quote ! { } ;
102
98
103
99
if ctx. options ( ) . enable_cxx_namespaces {
@@ -124,10 +120,9 @@ pub mod ast_ty {
124
120
use ir:: function:: FunctionSig ;
125
121
use ir:: layout:: Layout ;
126
122
use ir:: ty:: FloatKind ;
127
- use quote;
128
123
use proc_macro2;
129
124
130
- pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> quote :: Tokens {
125
+ pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> proc_macro2 :: TokenStream {
131
126
let ident = ctx. rust_ident_raw ( name) ;
132
127
match ctx. options ( ) . ctypes_prefix {
133
128
Some ( ref prefix) => {
@@ -146,7 +141,7 @@ pub mod ast_ty {
146
141
ctx : & BindgenContext ,
147
142
fk : FloatKind ,
148
143
layout : Option < Layout > ,
149
- ) -> quote :: Tokens {
144
+ ) -> proc_macro2 :: TokenStream {
150
145
// TODO: we probably should take the type layout into account more
151
146
// often?
152
147
//
@@ -186,25 +181,25 @@ pub mod ast_ty {
186
181
}
187
182
}
188
183
189
- pub fn int_expr ( val : i64 ) -> quote :: Tokens {
184
+ pub fn int_expr ( val : i64 ) -> proc_macro2 :: TokenStream {
190
185
// Don't use quote! { #val } because that adds the type suffix.
191
186
let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
192
187
quote ! ( #val)
193
188
}
194
189
195
- pub fn uint_expr ( val : u64 ) -> quote :: Tokens {
190
+ pub fn uint_expr ( val : u64 ) -> proc_macro2 :: TokenStream {
196
191
// Don't use quote! { #val } because that adds the type suffix.
197
192
let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
198
193
quote ! ( #val)
199
194
}
200
195
201
- pub fn byte_array_expr ( bytes : & [ u8 ] ) -> quote :: Tokens {
196
+ pub fn byte_array_expr ( bytes : & [ u8 ] ) -> proc_macro2 :: TokenStream {
202
197
let mut bytes: Vec < _ > = bytes. iter ( ) . cloned ( ) . collect ( ) ;
203
198
bytes. push ( 0 ) ;
204
199
quote ! { [ #( #bytes) , * ] }
205
200
}
206
201
207
- pub fn cstr_expr ( mut string : String ) -> quote :: Tokens {
202
+ pub fn cstr_expr ( mut string : String ) -> proc_macro2 :: TokenStream {
208
203
string. push ( '\0' ) ;
209
204
let b = proc_macro2:: Literal :: byte_string ( & string. as_bytes ( ) ) ;
210
205
quote ! {
@@ -215,7 +210,7 @@ pub mod ast_ty {
215
210
pub fn float_expr (
216
211
ctx : & BindgenContext ,
217
212
f : f64 ,
218
- ) -> Result < quote :: Tokens , ( ) > {
213
+ ) -> Result < proc_macro2 :: TokenStream , ( ) > {
219
214
if f. is_finite ( ) {
220
215
let val = proc_macro2:: Literal :: f64_unsuffixed ( f) ;
221
216
@@ -249,7 +244,7 @@ pub mod ast_ty {
249
244
pub fn arguments_from_signature (
250
245
signature : & FunctionSig ,
251
246
ctx : & BindgenContext ,
252
- ) -> Vec < quote :: Tokens > {
247
+ ) -> Vec < proc_macro2 :: TokenStream > {
253
248
let mut unnamed_arguments = 0 ;
254
249
signature
255
250
. argument_types ( )
0 commit comments