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 must_use ( ) -> quote :: Tokens {
38
+ pub fn must_use ( ) -> proc_macro2 :: TokenStream {
40
39
quote ! {
41
40
#[ must_use]
42
41
}
43
42
}
44
43
45
- pub fn doc ( comment : String ) -> quote:: Tokens {
46
- // Doc comments are already preprocessed into nice `///` formats by the
47
- // time they get here. Just make sure that we have newlines around it so
48
- // that nothing else gets wrapped into the comment.
49
- let mut tokens = quote ! { } ;
50
- tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
51
- tokens. append ( Term :: new ( & comment, Span :: call_site ( ) ) ) ;
52
- tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
53
- tokens
44
+ pub fn doc ( comment : String ) -> proc_macro2:: TokenStream {
45
+ use std:: str:: FromStr ;
46
+
47
+ // NOTE(emilio): By this point comments are already preprocessed and in
48
+ // `///` form. Quote turns them into `#[doc]` comments, but oh well.
49
+ proc_macro2:: TokenStream :: from_str ( & comment) . unwrap ( )
54
50
}
55
51
56
- pub fn link_name ( name : & str ) -> quote :: Tokens {
52
+ pub fn link_name ( name : & str ) -> proc_macro2 :: TokenStream {
57
53
// LLVM mangles the name by default but it's already mangled.
58
54
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
59
55
let name = format ! ( "\u{1} {}" , name) ;
@@ -65,7 +61,7 @@ pub mod attributes {
65
61
66
62
/// Generates a proper type for a field or type with a given `Layout`, that is,
67
63
/// a type with the correct size and alignment restrictions.
68
- pub fn blob ( ctx : & BindgenContext , layout : Layout ) -> quote :: Tokens {
64
+ pub fn blob ( ctx : & BindgenContext , layout : Layout ) -> proc_macro2 :: TokenStream {
69
65
let opaque = layout. opaque ( ) ;
70
66
71
67
// FIXME(emilio, #412): We fall back to byte alignment, but there are
@@ -80,7 +76,7 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
80
76
}
81
77
} ;
82
78
83
- let ty_name = Term :: new ( ty_name, Span :: call_site ( ) ) ;
79
+ let ty_name = Ident :: new ( ty_name, Span :: call_site ( ) ) ;
84
80
85
81
let data_len = opaque. array_size ( ctx) . unwrap_or ( layout. size ) ;
86
82
@@ -96,14 +92,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
96
92
}
97
93
98
94
/// Integer type of the same size as the given `Layout`.
99
- pub fn integer_type ( ctx : & BindgenContext , layout : Layout ) -> Option < quote :: Tokens > {
95
+ pub fn integer_type ( ctx : & BindgenContext , layout : Layout ) -> Option < proc_macro2 :: TokenStream > {
100
96
let name = Layout :: known_type_for_size ( ctx, layout. size ) ?;
101
- let name = Term :: new ( name, Span :: call_site ( ) ) ;
97
+ let name = Ident :: new ( name, Span :: call_site ( ) ) ;
102
98
Some ( quote ! { #name } )
103
99
}
104
100
105
101
/// Generates a bitfield allocation unit type for a type with the given `Layout`.
106
- pub fn bitfield_unit ( ctx : & BindgenContext , layout : Layout ) -> quote :: Tokens {
102
+ pub fn bitfield_unit ( ctx : & BindgenContext , layout : Layout ) -> proc_macro2 :: TokenStream {
107
103
let mut tokens = quote ! { } ;
108
104
109
105
if ctx. options ( ) . enable_cxx_namespaces {
@@ -130,10 +126,9 @@ pub mod ast_ty {
130
126
use ir:: function:: FunctionSig ;
131
127
use ir:: layout:: Layout ;
132
128
use ir:: ty:: FloatKind ;
133
- use quote;
134
129
use proc_macro2;
135
130
136
- pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> quote :: Tokens {
131
+ pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> proc_macro2 :: TokenStream {
137
132
let ident = ctx. rust_ident_raw ( name) ;
138
133
match ctx. options ( ) . ctypes_prefix {
139
134
Some ( ref prefix) => {
@@ -152,7 +147,7 @@ pub mod ast_ty {
152
147
ctx : & BindgenContext ,
153
148
fk : FloatKind ,
154
149
layout : Option < Layout > ,
155
- ) -> quote :: Tokens {
150
+ ) -> proc_macro2 :: TokenStream {
156
151
// TODO: we probably should take the type layout into account more
157
152
// often?
158
153
//
@@ -192,25 +187,25 @@ pub mod ast_ty {
192
187
}
193
188
}
194
189
195
- pub fn int_expr ( val : i64 ) -> quote :: Tokens {
190
+ pub fn int_expr ( val : i64 ) -> proc_macro2 :: TokenStream {
196
191
// Don't use quote! { #val } because that adds the type suffix.
197
192
let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
198
193
quote ! ( #val)
199
194
}
200
195
201
- pub fn uint_expr ( val : u64 ) -> quote :: Tokens {
196
+ pub fn uint_expr ( val : u64 ) -> proc_macro2 :: TokenStream {
202
197
// Don't use quote! { #val } because that adds the type suffix.
203
198
let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
204
199
quote ! ( #val)
205
200
}
206
201
207
- pub fn byte_array_expr ( bytes : & [ u8 ] ) -> quote :: Tokens {
202
+ pub fn byte_array_expr ( bytes : & [ u8 ] ) -> proc_macro2 :: TokenStream {
208
203
let mut bytes: Vec < _ > = bytes. iter ( ) . cloned ( ) . collect ( ) ;
209
204
bytes. push ( 0 ) ;
210
205
quote ! { [ #( #bytes) , * ] }
211
206
}
212
207
213
- pub fn cstr_expr ( mut string : String ) -> quote :: Tokens {
208
+ pub fn cstr_expr ( mut string : String ) -> proc_macro2 :: TokenStream {
214
209
string. push ( '\0' ) ;
215
210
let b = proc_macro2:: Literal :: byte_string ( & string. as_bytes ( ) ) ;
216
211
quote ! {
@@ -221,7 +216,7 @@ pub mod ast_ty {
221
216
pub fn float_expr (
222
217
ctx : & BindgenContext ,
223
218
f : f64 ,
224
- ) -> Result < quote :: Tokens , ( ) > {
219
+ ) -> Result < proc_macro2 :: TokenStream , ( ) > {
225
220
if f. is_finite ( ) {
226
221
let val = proc_macro2:: Literal :: f64_unsuffixed ( f) ;
227
222
@@ -255,7 +250,7 @@ pub mod ast_ty {
255
250
pub fn arguments_from_signature (
256
251
signature : & FunctionSig ,
257
252
ctx : & BindgenContext ,
258
- ) -> Vec < quote :: Tokens > {
253
+ ) -> Vec < proc_macro2 :: TokenStream > {
259
254
let mut unnamed_arguments = 0 ;
260
255
signature
261
256
. argument_types ( )
0 commit comments