blob: ae2d1d9d20c9b756696a146cef7b5371153da796 [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 Tolnay75ea17c2020-12-06 21:08:34 -08003 Array, Atom, Derive, Enum, ExternFn, ExternType, Impl, Receiver, Ref, RustName, Signature,
4 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 }
Adrian Taylorc8713432020-10-21 18:20:55 -070021 ident.rust.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)
26 | Type::CxxVector(ty)
27 | Type::RustVec(ty) => ty.to_tokens(tokens),
David Tolnaye0dca7b2020-11-25 17:18:57 -080028 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Xiangpeng Hao78762352020-11-12 10:24:18 +080029 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070030 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070031 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080032 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040033 }
34 }
35}
36
37impl ToTokens for Var {
38 fn to_tokens(&self, tokens: &mut TokenStream) {
39 self.ident.to_tokens(tokens);
40 Token![:](self.ident.span()).to_tokens(tokens);
41 self.ty.to_tokens(tokens);
42 }
43}
44
45impl ToTokens for Ty1 {
46 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay51cc8ee2020-04-25 14:10:29 -070047 let span = self.name.span();
David Tolnayda71ef62020-11-02 00:36:00 -080048 let name = self.name.to_string();
David Tolnayb3b24a12020-12-01 15:27:43 -080049 if let "UniquePtr" | "SharedPtr" | "CxxVector" = name.as_str() {
David Tolnay7db73692019-10-20 14:51:12 -040050 tokens.extend(quote_spanned!(span=> ::cxx::));
David Tolnay51cc8ee2020-04-25 14:10:29 -070051 } else if name == "Vec" {
52 tokens.extend(quote_spanned!(span=> ::std::vec::));
David Tolnay7db73692019-10-20 14:51:12 -040053 }
54 self.name.to_tokens(tokens);
55 self.langle.to_tokens(tokens);
56 self.inner.to_tokens(tokens);
57 self.rangle.to_tokens(tokens);
58 }
59}
60
61impl ToTokens for Ref {
62 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -080063 if let Some((pin, langle, _rangle)) = self.pin_tokens {
64 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
65 langle.to_tokens(tokens);
66 }
David Tolnay7db73692019-10-20 14:51:12 -040067 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070068 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040069 self.mutability.to_tokens(tokens);
70 self.inner.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -080071 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
72 rangle.to_tokens(tokens);
73 }
David Tolnay7db73692019-10-20 14:51:12 -040074 }
75}
76
David Tolnaye0dca7b2020-11-25 17:18:57 -080077impl ToTokens for SliceRef {
78 fn to_tokens(&self, tokens: &mut TokenStream) {
79 self.ampersand.to_tokens(tokens);
80 self.lifetime.to_tokens(tokens);
81 self.mutability.to_tokens(tokens);
82 self.bracket.surround(tokens, |tokens| {
83 self.inner.to_tokens(tokens);
84 });
85 }
86}
87
Xiangpeng Hao78762352020-11-12 10:24:18 +080088impl ToTokens for Array {
89 fn to_tokens(&self, tokens: &mut TokenStream) {
90 self.bracket.surround(tokens, |tokens| {
91 self.inner.to_tokens(tokens);
92 self.semi_token.to_tokens(tokens);
David Tolnayc351dc42020-11-24 20:45:13 -080093 self.len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +080094 });
95 }
96}
97
David Tolnayc605e6f2020-05-10 23:37:12 -070098impl ToTokens for Atom {
99 fn to_tokens(&self, tokens: &mut TokenStream) {
100 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
101 }
102}
103
David Tolnayb600e4f2020-11-27 17:26:49 -0800104impl ToTokens for Derive {
105 fn to_tokens(&self, tokens: &mut TokenStream) {
106 Ident::new(self.what.as_ref(), self.span).to_tokens(tokens);
107 }
108}
109
David Tolnay83496eb2020-05-04 00:36:53 -0700110impl ToTokens for ExternType {
111 fn to_tokens(&self, tokens: &mut TokenStream) {
112 // Notional token range for error reporting purposes.
113 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800114 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700115 }
116}
117
David Tolnay99383812020-05-04 02:34:33 -0700118impl ToTokens for TypeAlias {
119 fn to_tokens(&self, tokens: &mut TokenStream) {
120 // Notional token range for error reporting purposes.
121 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800122 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700123 }
124}
125
David Tolnay83496eb2020-05-04 00:36:53 -0700126impl ToTokens for Struct {
127 fn to_tokens(&self, tokens: &mut TokenStream) {
128 // Notional token range for error reporting purposes.
129 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800130 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700131 }
132}
133
134impl ToTokens for Enum {
135 fn to_tokens(&self, tokens: &mut TokenStream) {
136 // Notional token range for error reporting purposes.
137 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800138 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700139 }
140}
141
David Tolnay7db73692019-10-20 14:51:12 -0400142impl ToTokens for ExternFn {
143 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700144 // Notional token range for error reporting purposes.
145 self.sig.fn_token.to_tokens(tokens);
146 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400147 }
148}
David Tolnayc071b892020-03-18 16:59:53 -0700149
David Tolnay7e69f892020-10-03 22:20:22 -0700150impl ToTokens for Impl {
151 fn to_tokens(&self, tokens: &mut TokenStream) {
152 self.impl_token.to_tokens(tokens);
David Tolnay028d3d22020-11-29 18:09:52 -0800153 self.negative_token.to_tokens(tokens);
David Tolnay7e69f892020-10-03 22:20:22 -0700154 self.ty.to_tokens(tokens);
155 self.brace_token.surround(tokens, |_tokens| {});
156 }
157}
158
David Tolnayc071b892020-03-18 16:59:53 -0700159impl ToTokens for Signature {
160 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700161 self.fn_token.to_tokens(tokens);
162 self.paren_token.surround(tokens, |tokens| {
163 self.args.to_tokens(tokens);
164 });
165 if let Some(ret) = &self.ret {
166 Token![->](self.paren_token.span).to_tokens(tokens);
167 if let Some((result, langle, rangle)) = self.throws_tokens {
168 result.to_tokens(tokens);
169 langle.to_tokens(tokens);
170 ret.to_tokens(tokens);
171 rangle.to_tokens(tokens);
172 } else {
173 ret.to_tokens(tokens);
174 }
David Tolnay57c166d2020-11-14 23:38:31 -0800175 } else if let Some((result, langle, rangle)) = self.throws_tokens {
176 Token![->](self.paren_token.span).to_tokens(tokens);
177 result.to_tokens(tokens);
178 langle.to_tokens(tokens);
179 token::Paren(langle.span).surround(tokens, |_| ());
180 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700181 }
David Tolnayc071b892020-03-18 16:59:53 -0700182 }
183}
David Tolnayfb6e3862020-04-20 01:33:23 -0700184
David Tolnay75ea17c2020-12-06 21:08:34 -0800185impl ToTokens for RustName {
Adrian Taylorc8713432020-10-21 18:20:55 -0700186 fn to_tokens(&self, tokens: &mut TokenStream) {
187 self.rust.to_tokens(tokens);
188 }
189}
190
David Tolnay18ba92c2020-04-22 16:17:30 -0700191pub struct ReceiverType<'a>(&'a Receiver);
192
193impl Receiver {
194 // &TheType
195 pub fn ty(&self) -> ReceiverType {
196 ReceiverType(self)
197 }
198}
199
200impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700201 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800202 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
203 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
204 langle.to_tokens(tokens);
205 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700206 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700207 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700208 self.0.mutability.to_tokens(tokens);
209 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800210 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
211 rangle.to_tokens(tokens);
212 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700213 }
214}