blob: d728ac0f6d583642f163e101c668ca18f50ced3a [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 Tolnay77a5e752021-01-01 14:15:18 -08003 Array, Atom, Derive, Enum, ExternFn, ExternType, Impl, Lifetimes, NamedType, Receiver, Ref,
David Tolnay8c80de72020-12-28 22:13:34 -08004 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
Adrian Taylor38ae2282021-01-23 10:20:32 -080010use super::Ptr;
11
David Tolnay7db73692019-10-20 14:51:12 -040012impl ToTokens for Type {
13 fn to_tokens(&self, tokens: &mut TokenStream) {
14 match self {
15 Type::Ident(ident) => {
David Tolnayb3873dc2020-11-25 19:47:49 -080016 if ident.rust == Char {
17 let span = ident.rust.span();
18 tokens.extend(quote_spanned!(span=> ::std::os::raw::));
19 } else if ident.rust == CxxString {
Adrian Taylorc8713432020-10-21 18:20:55 -070020 let span = ident.rust.span();
David Tolnay7db73692019-10-20 14:51:12 -040021 tokens.extend(quote_spanned!(span=> ::cxx::));
22 }
David Tolnay89fd09b2020-12-31 02:01:31 -080023 ident.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040024 }
David Tolnayb3b24a12020-12-01 15:27:43 -080025 Type::RustBox(ty)
26 | Type::UniquePtr(ty)
27 | Type::SharedPtr(ty)
David Tolnay215e77f2020-12-28 17:09:48 -080028 | Type::WeakPtr(ty)
David Tolnayb3b24a12020-12-01 15:27:43 -080029 | Type::CxxVector(ty)
30 | Type::RustVec(ty) => ty.to_tokens(tokens),
David Tolnaye0dca7b2020-11-25 17:18:57 -080031 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Adrian Taylor38ae2282021-01-23 10:20:32 -080032 Type::Ptr(p) => p.to_tokens(tokens),
Xiangpeng Hao78762352020-11-12 10:24:18 +080033 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070034 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070035 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080036 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040037 }
38 }
39}
40
41impl ToTokens for Var {
42 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080043 let Var {
44 doc: _,
45 attrs: _,
46 visibility: _,
David Tolnay84ed6ad2021-01-01 15:30:14 -080047 name,
David Tolnay444f2dc2020-12-30 19:52:56 -080048 ty,
49 } = self;
David Tolnay84ed6ad2021-01-01 15:30:14 -080050 name.rust.to_tokens(tokens);
51 Token![:](name.rust.span()).to_tokens(tokens);
David Tolnay444f2dc2020-12-30 19:52:56 -080052 ty.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040053 }
54}
55
56impl ToTokens for Ty1 {
57 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080058 let Ty1 {
59 name,
60 langle,
61 inner,
62 rangle,
63 } = self;
64 let span = name.span();
65 match name.to_string().as_str() {
66 "UniquePtr" | "SharedPtr" | "WeakPtr" | "CxxVector" => {
67 tokens.extend(quote_spanned!(span=> ::cxx::));
68 }
69 "Vec" => {
70 tokens.extend(quote_spanned!(span=> ::std::vec::));
71 }
72 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040073 }
David Tolnay444f2dc2020-12-30 19:52:56 -080074 name.to_tokens(tokens);
75 langle.to_tokens(tokens);
76 inner.to_tokens(tokens);
77 rangle.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79}
80
81impl ToTokens for Ref {
82 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080083 let Ref {
84 pinned: _,
85 ampersand,
86 lifetime,
87 mutable: _,
88 inner,
89 pin_tokens,
90 mutability,
91 } = self;
92 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -080093 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
94 langle.to_tokens(tokens);
95 }
David Tolnay444f2dc2020-12-30 19:52:56 -080096 ampersand.to_tokens(tokens);
97 lifetime.to_tokens(tokens);
98 mutability.to_tokens(tokens);
99 inner.to_tokens(tokens);
100 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800101 rangle.to_tokens(tokens);
102 }
David Tolnay7db73692019-10-20 14:51:12 -0400103 }
104}
105
Adrian Taylor38ae2282021-01-23 10:20:32 -0800106impl ToTokens for Ptr {
107 fn to_tokens(&self, tokens: &mut TokenStream) {
108 let Ptr {
109 star,
110 mutable: _,
111 inner,
112 mutability,
113 constness,
114 } = self;
115 star.to_tokens(tokens);
116 mutability.to_tokens(tokens);
117 constness.to_tokens(tokens);
118 inner.to_tokens(tokens);
119 }
120}
121
David Tolnaye0dca7b2020-11-25 17:18:57 -0800122impl ToTokens for SliceRef {
123 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800124 let SliceRef {
125 ampersand,
126 lifetime,
127 mutable: _,
128 bracket,
129 inner,
130 mutability,
131 } = self;
132 ampersand.to_tokens(tokens);
133 lifetime.to_tokens(tokens);
134 mutability.to_tokens(tokens);
135 bracket.surround(tokens, |tokens| {
136 inner.to_tokens(tokens);
David Tolnaye0dca7b2020-11-25 17:18:57 -0800137 });
138 }
139}
140
Xiangpeng Hao78762352020-11-12 10:24:18 +0800141impl ToTokens for Array {
142 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800143 let Array {
144 bracket,
145 inner,
146 semi_token,
147 len: _,
148 len_token,
149 } = self;
150 bracket.surround(tokens, |tokens| {
151 inner.to_tokens(tokens);
152 semi_token.to_tokens(tokens);
153 len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +0800154 });
155 }
156}
157
David Tolnayc605e6f2020-05-10 23:37:12 -0700158impl ToTokens for Atom {
159 fn to_tokens(&self, tokens: &mut TokenStream) {
160 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
161 }
162}
163
David Tolnayb600e4f2020-11-27 17:26:49 -0800164impl ToTokens for Derive {
165 fn to_tokens(&self, tokens: &mut TokenStream) {
166 Ident::new(self.what.as_ref(), self.span).to_tokens(tokens);
167 }
168}
169
David Tolnay83496eb2020-05-04 00:36:53 -0700170impl ToTokens for ExternType {
171 fn to_tokens(&self, tokens: &mut TokenStream) {
172 // Notional token range for error reporting purposes.
173 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800174 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800175 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700176 }
177}
178
David Tolnay99383812020-05-04 02:34:33 -0700179impl ToTokens for TypeAlias {
180 fn to_tokens(&self, tokens: &mut TokenStream) {
181 // Notional token range for error reporting purposes.
182 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800183 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800184 self.generics.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700185 }
186}
187
David Tolnay83496eb2020-05-04 00:36:53 -0700188impl ToTokens for Struct {
189 fn to_tokens(&self, tokens: &mut TokenStream) {
190 // Notional token range for error reporting purposes.
191 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800192 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800193 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700194 }
195}
196
197impl ToTokens for Enum {
198 fn to_tokens(&self, tokens: &mut TokenStream) {
199 // Notional token range for error reporting purposes.
200 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800201 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800202 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700203 }
204}
205
David Tolnay7db73692019-10-20 14:51:12 -0400206impl ToTokens for ExternFn {
207 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700208 // Notional token range for error reporting purposes.
209 self.sig.fn_token.to_tokens(tokens);
210 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400211 }
212}
David Tolnayc071b892020-03-18 16:59:53 -0700213
David Tolnay7e69f892020-10-03 22:20:22 -0700214impl ToTokens for Impl {
215 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800216 let Impl {
217 impl_token,
David Tolnayc4179772021-01-01 19:39:35 -0800218 impl_generics,
David Tolnay444f2dc2020-12-30 19:52:56 -0800219 negative: _,
220 ty,
David Tolnayc4179772021-01-01 19:39:35 -0800221 ty_generics: _,
David Tolnay444f2dc2020-12-30 19:52:56 -0800222 brace_token,
223 negative_token,
224 } = self;
225 impl_token.to_tokens(tokens);
David Tolnayc4179772021-01-01 19:39:35 -0800226 impl_generics.to_tokens(tokens);
David Tolnay444f2dc2020-12-30 19:52:56 -0800227 negative_token.to_tokens(tokens);
228 ty.to_tokens(tokens);
229 brace_token.surround(tokens, |_tokens| {});
David Tolnay7e69f892020-10-03 22:20:22 -0700230 }
231}
232
David Tolnay8c80de72020-12-28 22:13:34 -0800233impl ToTokens for Lifetimes {
234 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800235 let Lifetimes {
236 lt_token,
237 lifetimes,
238 gt_token,
239 } = self;
240 lt_token.to_tokens(tokens);
241 lifetimes.to_tokens(tokens);
242 gt_token.to_tokens(tokens);
David Tolnay8c80de72020-12-28 22:13:34 -0800243 }
244}
245
David Tolnayc071b892020-03-18 16:59:53 -0700246impl ToTokens for Signature {
247 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800248 let Signature {
249 unsafety: _,
250 fn_token,
251 generics: _,
252 receiver: _,
253 args,
254 ret,
255 throws: _,
256 paren_token,
257 throws_tokens,
258 } = self;
259 fn_token.to_tokens(tokens);
260 paren_token.surround(tokens, |tokens| {
261 args.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700262 });
David Tolnay444f2dc2020-12-30 19:52:56 -0800263 if let Some(ret) = ret {
264 Token![->](paren_token.span).to_tokens(tokens);
265 if let Some((result, langle, rangle)) = throws_tokens {
David Tolnaye3a48152020-04-08 19:38:05 -0700266 result.to_tokens(tokens);
267 langle.to_tokens(tokens);
268 ret.to_tokens(tokens);
269 rangle.to_tokens(tokens);
270 } else {
271 ret.to_tokens(tokens);
272 }
David Tolnay444f2dc2020-12-30 19:52:56 -0800273 } else if let Some((result, langle, rangle)) = throws_tokens {
274 Token![->](paren_token.span).to_tokens(tokens);
David Tolnay57c166d2020-11-14 23:38:31 -0800275 result.to_tokens(tokens);
276 langle.to_tokens(tokens);
277 token::Paren(langle.span).surround(tokens, |_| ());
278 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700279 }
David Tolnayc071b892020-03-18 16:59:53 -0700280 }
281}
David Tolnayfb6e3862020-04-20 01:33:23 -0700282
David Tolnay77a5e752021-01-01 14:15:18 -0800283impl ToTokens for NamedType {
Adrian Taylorc8713432020-10-21 18:20:55 -0700284 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay77a5e752021-01-01 14:15:18 -0800285 let NamedType { rust, generics } = self;
David Tolnay444f2dc2020-12-30 19:52:56 -0800286 rust.to_tokens(tokens);
David Tolnay679b15d2020-12-30 20:39:06 -0800287 generics.to_tokens(tokens);
Adrian Taylorc8713432020-10-21 18:20:55 -0700288 }
289}
290
David Tolnay18ba92c2020-04-22 16:17:30 -0700291pub struct ReceiverType<'a>(&'a Receiver);
David Tolnay9bd65aa2021-01-03 21:04:17 -0800292pub struct ReceiverTypeSelf<'a>(&'a Receiver);
David Tolnay18ba92c2020-04-22 16:17:30 -0700293
294impl Receiver {
295 // &TheType
296 pub fn ty(&self) -> ReceiverType {
297 ReceiverType(self)
298 }
David Tolnay9bd65aa2021-01-03 21:04:17 -0800299
300 // &Self
301 pub fn ty_self(&self) -> ReceiverTypeSelf {
302 ReceiverTypeSelf(self)
303 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700304}
305
306impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700307 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800308 let Receiver {
309 pinned: _,
310 ampersand,
311 lifetime,
312 mutable: _,
313 var: _,
314 ty,
315 shorthand: _,
316 pin_tokens,
317 mutability,
318 } = &self.0;
319 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800320 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
321 langle.to_tokens(tokens);
322 }
David Tolnay444f2dc2020-12-30 19:52:56 -0800323 ampersand.to_tokens(tokens);
324 lifetime.to_tokens(tokens);
325 mutability.to_tokens(tokens);
326 ty.to_tokens(tokens);
327 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800328 rangle.to_tokens(tokens);
329 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700330 }
331}
David Tolnay9bd65aa2021-01-03 21:04:17 -0800332
333impl ToTokens for ReceiverTypeSelf<'_> {
334 fn to_tokens(&self, tokens: &mut TokenStream) {
335 let Receiver {
336 pinned: _,
337 ampersand,
338 lifetime,
339 mutable: _,
340 var: _,
341 ty,
342 shorthand: _,
343 pin_tokens,
344 mutability,
345 } = &self.0;
346 if let Some((pin, langle, _rangle)) = pin_tokens {
347 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
348 langle.to_tokens(tokens);
349 }
350 ampersand.to_tokens(tokens);
351 lifetime.to_tokens(tokens);
352 mutability.to_tokens(tokens);
353 Token![Self](ty.rust.span()).to_tokens(tokens);
354 if let Some((_pin, _langle, rangle)) = pin_tokens {
355 rangle.to_tokens(tokens);
356 }
357 }
358}