blob: 33f20fa3c928eae16dcffe9ca86c162cb7af1b12 [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 Tolnay45849582021-04-22 19:18:23 -07003 Array, Atom, Derive, Enum, EnumRepr, ExternFn, ExternType, Impl, Lifetimes, NamedType, Ptr,
4 Receiver, 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 Tolnayf6cad332021-04-10 13:40:03 -070046 colon_token: _,
David Tolnay444f2dc2020-12-30 19:52:56 -080047 ty,
48 } = self;
David Tolnay84ed6ad2021-01-01 15:30:14 -080049 name.rust.to_tokens(tokens);
50 Token![:](name.rust.span()).to_tokens(tokens);
David Tolnay444f2dc2020-12-30 19:52:56 -080051 ty.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040052 }
53}
54
55impl ToTokens for Ty1 {
56 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080057 let Ty1 {
58 name,
59 langle,
60 inner,
61 rangle,
62 } = self;
63 let span = name.span();
64 match name.to_string().as_str() {
65 "UniquePtr" | "SharedPtr" | "WeakPtr" | "CxxVector" => {
66 tokens.extend(quote_spanned!(span=> ::cxx::));
67 }
68 "Vec" => {
69 tokens.extend(quote_spanned!(span=> ::std::vec::));
70 }
71 _ => {}
David Tolnay7db73692019-10-20 14:51:12 -040072 }
David Tolnay444f2dc2020-12-30 19:52:56 -080073 name.to_tokens(tokens);
74 langle.to_tokens(tokens);
75 inner.to_tokens(tokens);
76 rangle.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040077 }
78}
79
80impl ToTokens for Ref {
81 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -080082 let Ref {
83 pinned: _,
84 ampersand,
85 lifetime,
86 mutable: _,
87 inner,
88 pin_tokens,
89 mutability,
90 } = self;
91 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -080092 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
93 langle.to_tokens(tokens);
94 }
David Tolnay444f2dc2020-12-30 19:52:56 -080095 ampersand.to_tokens(tokens);
96 lifetime.to_tokens(tokens);
97 mutability.to_tokens(tokens);
98 inner.to_tokens(tokens);
99 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800100 rangle.to_tokens(tokens);
101 }
David Tolnay7db73692019-10-20 14:51:12 -0400102 }
103}
104
Adrian Taylor38ae2282021-01-23 10:20:32 -0800105impl ToTokens for Ptr {
106 fn to_tokens(&self, tokens: &mut TokenStream) {
107 let Ptr {
108 star,
109 mutable: _,
110 inner,
111 mutability,
112 constness,
113 } = self;
114 star.to_tokens(tokens);
115 mutability.to_tokens(tokens);
116 constness.to_tokens(tokens);
117 inner.to_tokens(tokens);
118 }
119}
120
David Tolnaye0dca7b2020-11-25 17:18:57 -0800121impl ToTokens for SliceRef {
122 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800123 let SliceRef {
124 ampersand,
125 lifetime,
126 mutable: _,
127 bracket,
128 inner,
129 mutability,
130 } = self;
131 ampersand.to_tokens(tokens);
132 lifetime.to_tokens(tokens);
133 mutability.to_tokens(tokens);
134 bracket.surround(tokens, |tokens| {
135 inner.to_tokens(tokens);
David Tolnaye0dca7b2020-11-25 17:18:57 -0800136 });
137 }
138}
139
Xiangpeng Hao78762352020-11-12 10:24:18 +0800140impl ToTokens for Array {
141 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800142 let Array {
143 bracket,
144 inner,
145 semi_token,
146 len: _,
147 len_token,
148 } = self;
149 bracket.surround(tokens, |tokens| {
150 inner.to_tokens(tokens);
151 semi_token.to_tokens(tokens);
152 len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +0800153 });
154 }
155}
156
David Tolnayc605e6f2020-05-10 23:37:12 -0700157impl ToTokens for Atom {
158 fn to_tokens(&self, tokens: &mut TokenStream) {
159 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
160 }
161}
162
David Tolnayb600e4f2020-11-27 17:26:49 -0800163impl ToTokens for Derive {
164 fn to_tokens(&self, tokens: &mut TokenStream) {
165 Ident::new(self.what.as_ref(), self.span).to_tokens(tokens);
166 }
167}
168
David Tolnay83496eb2020-05-04 00:36:53 -0700169impl ToTokens for ExternType {
170 fn to_tokens(&self, tokens: &mut TokenStream) {
171 // Notional token range for error reporting purposes.
172 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800173 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800174 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700175 }
176}
177
David Tolnay99383812020-05-04 02:34:33 -0700178impl ToTokens for TypeAlias {
179 fn to_tokens(&self, tokens: &mut TokenStream) {
180 // Notional token range for error reporting purposes.
181 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800182 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800183 self.generics.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700184 }
185}
186
David Tolnay83496eb2020-05-04 00:36:53 -0700187impl ToTokens for Struct {
188 fn to_tokens(&self, tokens: &mut TokenStream) {
189 // Notional token range for error reporting purposes.
190 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800191 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800192 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700193 }
194}
195
196impl ToTokens for Enum {
197 fn to_tokens(&self, tokens: &mut TokenStream) {
198 // Notional token range for error reporting purposes.
199 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800200 self.name.rust.to_tokens(tokens);
David Tolnay3a480212021-01-02 23:25:53 -0800201 self.generics.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700202 }
203}
204
David Tolnay7db73692019-10-20 14:51:12 -0400205impl ToTokens for ExternFn {
206 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700207 // Notional token range for error reporting purposes.
David Tolnay2dec4212021-03-26 17:07:45 -0400208 self.unsafety.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700209 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 Tolnay45849582021-04-22 19:18:23 -0700283impl ToTokens for EnumRepr {
284 fn to_tokens(&self, tokens: &mut TokenStream) {
285 match self {
286 EnumRepr::Native { atom, repr_type: _ } => atom.to_tokens(tokens),
287 EnumRepr::Foreign { rust_type } => rust_type.to_tokens(tokens),
288 }
289 }
290}
291
David Tolnay77a5e752021-01-01 14:15:18 -0800292impl ToTokens for NamedType {
Adrian Taylorc8713432020-10-21 18:20:55 -0700293 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay77a5e752021-01-01 14:15:18 -0800294 let NamedType { rust, generics } = self;
David Tolnay444f2dc2020-12-30 19:52:56 -0800295 rust.to_tokens(tokens);
David Tolnay679b15d2020-12-30 20:39:06 -0800296 generics.to_tokens(tokens);
Adrian Taylorc8713432020-10-21 18:20:55 -0700297 }
298}
299
David Tolnay18ba92c2020-04-22 16:17:30 -0700300pub struct ReceiverType<'a>(&'a Receiver);
David Tolnay9bd65aa2021-01-03 21:04:17 -0800301pub struct ReceiverTypeSelf<'a>(&'a Receiver);
David Tolnay18ba92c2020-04-22 16:17:30 -0700302
303impl Receiver {
304 // &TheType
305 pub fn ty(&self) -> ReceiverType {
306 ReceiverType(self)
307 }
David Tolnay9bd65aa2021-01-03 21:04:17 -0800308
309 // &Self
310 pub fn ty_self(&self) -> ReceiverTypeSelf {
311 ReceiverTypeSelf(self)
312 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700313}
314
315impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700316 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay444f2dc2020-12-30 19:52:56 -0800317 let Receiver {
318 pinned: _,
319 ampersand,
320 lifetime,
321 mutable: _,
322 var: _,
David Tolnay281d49a2021-04-10 13:44:31 -0700323 colon_token: _,
David Tolnay444f2dc2020-12-30 19:52:56 -0800324 ty,
325 shorthand: _,
326 pin_tokens,
327 mutability,
328 } = &self.0;
329 if let Some((pin, langle, _rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800330 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
331 langle.to_tokens(tokens);
332 }
David Tolnay444f2dc2020-12-30 19:52:56 -0800333 ampersand.to_tokens(tokens);
334 lifetime.to_tokens(tokens);
335 mutability.to_tokens(tokens);
336 ty.to_tokens(tokens);
337 if let Some((_pin, _langle, rangle)) = pin_tokens {
David Tolnayc9673842020-11-15 16:26:10 -0800338 rangle.to_tokens(tokens);
339 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700340 }
341}
David Tolnay9bd65aa2021-01-03 21:04:17 -0800342
343impl ToTokens for ReceiverTypeSelf<'_> {
344 fn to_tokens(&self, tokens: &mut TokenStream) {
345 let Receiver {
346 pinned: _,
347 ampersand,
348 lifetime,
349 mutable: _,
350 var: _,
David Tolnay281d49a2021-04-10 13:44:31 -0700351 colon_token: _,
David Tolnay9bd65aa2021-01-03 21:04:17 -0800352 ty,
353 shorthand: _,
354 pin_tokens,
355 mutability,
356 } = &self.0;
357 if let Some((pin, langle, _rangle)) = pin_tokens {
358 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
359 langle.to_tokens(tokens);
360 }
361 ampersand.to_tokens(tokens);
362 lifetime.to_tokens(tokens);
363 mutability.to_tokens(tokens);
364 Token![Self](ty.rust.span()).to_tokens(tokens);
365 if let Some((_pin, _langle, rangle)) = pin_tokens {
366 rangle.to_tokens(tokens);
367 }
368 }
369}