blob: c1a06a213ebacbbc6317d6127046fff77a22fcd6 [file] [log] [blame]
David Tolnaya52602b2020-03-06 10:24:34 -08001use crate::syntax::atom::Atom::*;
David Tolnay83496eb2020-05-04 00:36:53 -07002use crate::syntax::{
David Tolnaybeba43d2021-03-22 15:13:20 -07003 Array, Atom, Derive, Enum, ExternFn, ExternType, Impl, Lifetimes, NamedType, Ptr, Receiver,
4 Ref, Signature, SliceRef, Struct, Ty1, Type, TypeAlias, Var,
David Tolnay83496eb2020-05-04 00:36:53 -07005};
David Tolnay7db73692019-10-20 14:51:12 -04006use proc_macro2::{Ident, Span, TokenStream};
David Tolnayc071b892020-03-18 16:59:53 -07007use quote::{quote_spanned, ToTokens};
David Tolnay57c166d2020-11-14 23:38:31 -08008use syn::{token, Token};
David Tolnay7db73692019-10-20 14:51:12 -04009
10impl ToTokens for Type {
11 fn to_tokens(&self, tokens: &mut TokenStream) {
12 match self {
13 Type::Ident(ident) => {
David Tolnayb3873dc2020-11-25 19:47:49 -080014 if ident.rust == Char {
15 let span = ident.rust.span();
16 tokens.extend(quote_spanned!(span=> ::std::os::raw::));
17 } else if ident.rust == CxxString {
Adrian Taylorc8713432020-10-21 18:20:55 -070018 let span = ident.rust.span();
David Tolnay7db73692019-10-20 14:51:12 -040019 tokens.extend(quote_spanned!(span=> ::cxx::));
20 }
David Tolnay89fd09b2020-12-31 02:01:31 -080021 ident.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040022 }
David Tolnayb3b24a12020-12-01 15:27:43 -080023 Type::RustBox(ty)
24 | Type::UniquePtr(ty)
25 | Type::SharedPtr(ty)
David Tolnay215e77f2020-12-28 17:09:48 -080026 | Type::WeakPtr(ty)
David Tolnayb3b24a12020-12-01 15:27:43 -080027 | Type::CxxVector(ty)
28 | Type::RustVec(ty) => ty.to_tokens(tokens),
David Tolnaye0dca7b2020-11-25 17:18:57 -080029 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Adrian Taylor38ae2282021-01-23 10:20:32 -080030 Type::Ptr(p) => p.to_tokens(tokens),
Xiangpeng Hao78762352020-11-12 10:24:18 +080031 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070032 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070033 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080034 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040035 }
36 }
37}
38
39impl ToTokens for Var {
40 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080041 let Var {
42 doc: _,
43 attrs: _,
44 visibility: _,
David Tolnay84ed6ad2021-01-01 15:30:14 -080045 name,
David Tolnay444f2dc2020-12-30 19:52:56 -080046 ty,
47 } = self;
David Tolnay84ed6ad2021-01-01 15:30:14 -080048 name.rust.to_tokens(tokens);
49 Token![:](name.rust.span()).to_tokens(tokens);
David Tolnay444f2dc2020-12-30 19:52:56 -080050 ty.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52}
53
54impl ToTokens for Ty1 {
55 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080056 let Ty1 {
57 name,
58 langle,
59 inner,
60 rangle,
61 } = self;
62 let span = name.span();
63 match name.to_string().as_str() {
64 "UniquePtr" | "SharedPtr" | "WeakPtr" | "CxxVector" => {
65 tokens.extend(quote_spanned!(span=> ::cxx::));
66 }
67 "Vec" => {
68 tokens.extend(quote_spanned!(span=> ::std::vec::));
69 }
70 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040071 }
David Tolnay444f2dc2020-12-30 19:52:56 -080072 name.to_tokens(tokens);
73 langle.to_tokens(tokens);
74 inner.to_tokens(tokens);
75 rangle.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77}
78
79impl ToTokens for Ref {
80 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080081 let Ref {
82 pinned: _,
83 ampersand,
84 lifetime,
85 mutable: _,
86 inner,
87 pin_tokens,
88 mutability,
89 } = self;
90 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -080091 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
92 langle.to_tokens(tokens);
93 }
David Tolnay444f2dc2020-12-30 19:52:56 -080094 ampersand.to_tokens(tokens);
95 lifetime.to_tokens(tokens);
96 mutability.to_tokens(tokens);
97 inner.to_tokens(tokens);
98 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -080099 rangle.to_tokens(tokens);
100 }
David Tolnay7db73692019-10-20 14:51:12 -0400101 }
102}
103
Adrian Taylor38ae2282021-01-23 10:20:32 -0800104impl ToTokens for Ptr {
105 fn to_tokens(&self, tokens: &mut TokenStream) {
106 let Ptr {
107 star,
108 mutable: _,
109 inner,
110 mutability,
111 constness,
112 } = self;
113 star.to_tokens(tokens);
114 mutability.to_tokens(tokens);
115 constness.to_tokens(tokens);
116 inner.to_tokens(tokens);
117 }
118}
119
David Tolnaye0dca7b2020-11-25 17:18:57 -0800120impl ToTokens for SliceRef {
121 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800122 let SliceRef {
123 ampersand,
124 lifetime,
125 mutable: _,
126 bracket,
127 inner,
128 mutability,
129 } = self;
130 ampersand.to_tokens(tokens);
131 lifetime.to_tokens(tokens);
132 mutability.to_tokens(tokens);
133 bracket.surround(tokens, |tokens| {
134 inner.to_tokens(tokens);
David Tolnaye0dca7b2020-11-25 17:18:57 -0800135 });
136 }
137}
138
Xiangpeng Hao78762352020-11-12 10:24:18 +0800139impl ToTokens for Array {
140 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800141 let Array {
142 bracket,
143 inner,
144 semi_token,
145 len: _,
146 len_token,
147 } = self;
148 bracket.surround(tokens, |tokens| {
149 inner.to_tokens(tokens);
150 semi_token.to_tokens(tokens);
151 len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +0800152 });
153 }
154}
155
David Tolnayc605e6f2020-05-10 23:37:12 -0700156impl ToTokens for Atom {
157 fn to_tokens(&self, tokens: &mut TokenStream) {
158 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
159 }
160}
161
David Tolnayb600e4f2020-11-27 17:26:49 -0800162impl ToTokens for Derive {
163 fn to_tokens(&self, tokens: &mut TokenStream) {
164 Ident::new(self.what.as_ref(), self.span).to_tokens(tokens);
165 }
166}
167
David Tolnay83496eb2020-05-04 00:36:53 -0700168impl ToTokens for ExternType {
169 fn to_tokens(&self, tokens: &mut TokenStream) {
170 // Notional token range for error reporting purposes.
171 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800172 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800173 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700174 }
175}
176
David Tolnay99383812020-05-04 02:34:33 -0700177impl ToTokens for TypeAlias {
178 fn to_tokens(&self, tokens: &mut TokenStream) {
179 // Notional token range for error reporting purposes.
180 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800181 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800182 self.generics.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700183 }
184}
185
David Tolnay83496eb2020-05-04 00:36:53 -0700186impl ToTokens for Struct {
187 fn to_tokens(&self, tokens: &mut TokenStream) {
188 // Notional token range for error reporting purposes.
189 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800190 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800191 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700192 }
193}
194
195impl ToTokens for Enum {
196 fn to_tokens(&self, tokens: &mut TokenStream) {
197 // Notional token range for error reporting purposes.
198 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800199 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800200 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700201 }
202}
203
David Tolnay7db73692019-10-20 14:51:12 -0400204impl ToTokens for ExternFn {
205 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700206 // Notional token range for error reporting purposes.
David Tolnay2dec4212021-03-26 17:07:45 -0400207 self.unsafety.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700208 self.sig.fn_token.to_tokens(tokens);
209 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400210 }
211}
David Tolnayc071b892020-03-18 16:59:53 -0700212
David Tolnay7e69f892020-10-03 22:20:22 -0700213impl ToTokens for Impl {
214 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800215 let Impl {
216 impl_token,
David Tolnayc4179772021-01-01 19:39:35 -0800217 impl_generics,
David Tolnay444f2dc2020-12-30 19:52:56 -0800218 negative: _,
219 ty,
David Tolnayc4179772021-01-01 19:39:35 -0800220 ty_generics: _,
David Tolnay444f2dc2020-12-30 19:52:56 -0800221 brace_token,
222 negative_token,
223 } = self;
224 impl_token.to_tokens(tokens);
David Tolnayc4179772021-01-01 19:39:35 -0800225 impl_generics.to_tokens(tokens);
David Tolnay444f2dc2020-12-30 19:52:56 -0800226 negative_token.to_tokens(tokens);
227 ty.to_tokens(tokens);
228 brace_token.surround(tokens, |_tokens| {});
David Tolnay7e69f892020-10-03 22:20:22 -0700229 }
230}
231
David Tolnay8c80de72020-12-28 22:13:34 -0800232impl ToTokens for Lifetimes {
233 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800234 let Lifetimes {
235 lt_token,
236 lifetimes,
237 gt_token,
238 } = self;
239 lt_token.to_tokens(tokens);
240 lifetimes.to_tokens(tokens);
241 gt_token.to_tokens(tokens);
David Tolnay8c80de72020-12-28 22:13:34 -0800242 }
243}
244
David Tolnayc071b892020-03-18 16:59:53 -0700245impl ToTokens for Signature {
246 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800247 let Signature {
248 unsafety: _,
249 fn_token,
250 generics: _,
251 receiver: _,
252 args,
253 ret,
254 throws: _,
255 paren_token,
256 throws_tokens,
257 } = self;
258 fn_token.to_tokens(tokens);
259 paren_token.surround(tokens, |tokens| {
260 args.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700261 });
David Tolnay444f2dc2020-12-30 19:52:56 -0800262 if let Some(ret) = ret {
263 Token![->](paren_token.span).to_tokens(tokens);
264 if let Some((result, langle, rangle)) = throws_tokens {
David Tolnaye3a48152020-04-08 19:38:05 -0700265 result.to_tokens(tokens);
266 langle.to_tokens(tokens);
267 ret.to_tokens(tokens);
268 rangle.to_tokens(tokens);
269 } else {
270 ret.to_tokens(tokens);
271 }
David Tolnay444f2dc2020-12-30 19:52:56 -0800272 } else if let Some((result, langle, rangle)) = throws_tokens {
273 Token![->](paren_token.span).to_tokens(tokens);
David Tolnay57c166d2020-11-14 23:38:31 -0800274 result.to_tokens(tokens);
275 langle.to_tokens(tokens);
276 token::Paren(langle.span).surround(tokens, |_| ());
277 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700278 }
David Tolnayc071b892020-03-18 16:59:53 -0700279 }
280}
David Tolnayfb6e3862020-04-20 01:33:23 -0700281
David Tolnay77a5e752021-01-01 14:15:18 -0800282impl ToTokens for NamedType {
Adrian Taylorc8713432020-10-21 18:20:55 -0700283 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay77a5e752021-01-01 14:15:18 -0800284 let NamedType { rust, generics } = self;
David Tolnay444f2dc2020-12-30 19:52:56 -0800285 rust.to_tokens(tokens);
David Tolnay679b15d2020-12-30 20:39:06 -0800286 generics.to_tokens(tokens);
Adrian Taylorc8713432020-10-21 18:20:55 -0700287 }
288}
289
David Tolnay18ba92c2020-04-22 16:17:30 -0700290pub struct ReceiverType<'a>(&'a Receiver);
David Tolnay9bd65aa2021-01-03 21:04:17 -0800291pub struct ReceiverTypeSelf<'a>(&'a Receiver);
David Tolnay18ba92c2020-04-22 16:17:30 -0700292
293impl Receiver {
294 // &TheType
295 pub fn ty(&self) -> ReceiverType {
296 ReceiverType(self)
297 }
David Tolnay9bd65aa2021-01-03 21:04:17 -0800298
299 // &Self
300 pub fn ty_self(&self) -> ReceiverTypeSelf {
301 ReceiverTypeSelf(self)
302 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700303}
304
305impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700306 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800307 let Receiver {
308 pinned: _,
309 ampersand,
310 lifetime,
311 mutable: _,
312 var: _,
313 ty,
314 shorthand: _,
315 pin_tokens,
316 mutability,
317 } = &self.0;
318 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800319 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
320 langle.to_tokens(tokens);
321 }
David Tolnay444f2dc2020-12-30 19:52:56 -0800322 ampersand.to_tokens(tokens);
323 lifetime.to_tokens(tokens);
324 mutability.to_tokens(tokens);
325 ty.to_tokens(tokens);
326 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800327 rangle.to_tokens(tokens);
328 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700329 }
330}
David Tolnay9bd65aa2021-01-03 21:04:17 -0800331
332impl ToTokens for ReceiverTypeSelf<'_> {
333 fn to_tokens(&self, tokens: &mut TokenStream) {
334 let Receiver {
335 pinned: _,
336 ampersand,
337 lifetime,
338 mutable: _,
339 var: _,
340 ty,
341 shorthand: _,
342 pin_tokens,
343 mutability,
344 } = &self.0;
345 if let Some((pin, langle, _rangle)) = pin_tokens {
346 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
347 langle.to_tokens(tokens);
348 }
349 ampersand.to_tokens(tokens);
350 lifetime.to_tokens(tokens);
351 mutability.to_tokens(tokens);
352 Token![Self](ty.rust.span()).to_tokens(tokens);
353 if let Some((_pin, _langle, rangle)) = pin_tokens {
354 rangle.to_tokens(tokens);
355 }
356 }
357}