blob: 8ce7ed9e5d4d578c31a7aaaae0b5605415b883ff [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::{
Xiangpeng Hao78762352020-11-12 10:24:18 +08003 Array, Atom, Derive, Enum, ExternFn, ExternType, Impl, Receiver, Ref, ResolvableName,
David Tolnaye0dca7b2020-11-25 17:18:57 -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
10impl ToTokens for Type {
11 fn to_tokens(&self, tokens: &mut TokenStream) {
12 match self {
13 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -070014 if ident.rust == CxxString {
15 let span = ident.rust.span();
David Tolnay7db73692019-10-20 14:51:12 -040016 tokens.extend(quote_spanned!(span=> ::cxx::));
17 }
Adrian Taylorc8713432020-10-21 18:20:55 -070018 ident.rust.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040019 }
David Tolnay4377a9e2020-04-24 15:20:26 -070020 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) | Type::RustVec(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +070021 ty.to_tokens(tokens)
22 }
David Tolnaye0dca7b2020-11-25 17:18:57 -080023 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Xiangpeng Hao78762352020-11-12 10:24:18 +080024 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070025 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070026 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080027 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040028 }
29 }
30}
31
32impl ToTokens for Var {
33 fn to_tokens(&self, tokens: &mut TokenStream) {
34 self.ident.to_tokens(tokens);
35 Token![:](self.ident.span()).to_tokens(tokens);
36 self.ty.to_tokens(tokens);
37 }
38}
39
40impl ToTokens for Ty1 {
41 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay51cc8ee2020-04-25 14:10:29 -070042 let span = self.name.span();
David Tolnayda71ef62020-11-02 00:36:00 -080043 let name = self.name.to_string();
David Tolnay51cc8ee2020-04-25 14:10:29 -070044 if let "UniquePtr" | "CxxVector" = name.as_str() {
David Tolnay7db73692019-10-20 14:51:12 -040045 tokens.extend(quote_spanned!(span=> ::cxx::));
David Tolnay51cc8ee2020-04-25 14:10:29 -070046 } else if name == "Vec" {
47 tokens.extend(quote_spanned!(span=> ::std::vec::));
David Tolnay7db73692019-10-20 14:51:12 -040048 }
49 self.name.to_tokens(tokens);
50 self.langle.to_tokens(tokens);
51 self.inner.to_tokens(tokens);
52 self.rangle.to_tokens(tokens);
53 }
54}
55
56impl ToTokens for Ref {
57 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -080058 if let Some((pin, langle, _rangle)) = self.pin_tokens {
59 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
60 langle.to_tokens(tokens);
61 }
David Tolnay7db73692019-10-20 14:51:12 -040062 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070063 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040064 self.mutability.to_tokens(tokens);
65 self.inner.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -080066 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
67 rangle.to_tokens(tokens);
68 }
David Tolnay7db73692019-10-20 14:51:12 -040069 }
70}
71
David Tolnaye0dca7b2020-11-25 17:18:57 -080072impl ToTokens for SliceRef {
73 fn to_tokens(&self, tokens: &mut TokenStream) {
74 self.ampersand.to_tokens(tokens);
75 self.lifetime.to_tokens(tokens);
76 self.mutability.to_tokens(tokens);
77 self.bracket.surround(tokens, |tokens| {
78 self.inner.to_tokens(tokens);
79 });
80 }
81}
82
Xiangpeng Hao78762352020-11-12 10:24:18 +080083impl ToTokens for Array {
84 fn to_tokens(&self, tokens: &mut TokenStream) {
85 self.bracket.surround(tokens, |tokens| {
86 self.inner.to_tokens(tokens);
87 self.semi_token.to_tokens(tokens);
David Tolnayc351dc42020-11-24 20:45:13 -080088 self.len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +080089 });
90 }
91}
92
David Tolnay7db73692019-10-20 14:51:12 -040093impl ToTokens for Derive {
94 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye86b9cf2020-05-10 14:24:29 -070095 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040096 }
97}
98
David Tolnayc605e6f2020-05-10 23:37:12 -070099impl ToTokens for Atom {
100 fn to_tokens(&self, tokens: &mut TokenStream) {
101 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
102 }
103}
104
David Tolnay83496eb2020-05-04 00:36:53 -0700105impl ToTokens for ExternType {
106 fn to_tokens(&self, tokens: &mut TokenStream) {
107 // Notional token range for error reporting purposes.
108 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800109 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700110 }
111}
112
David Tolnay99383812020-05-04 02:34:33 -0700113impl ToTokens for TypeAlias {
114 fn to_tokens(&self, tokens: &mut TokenStream) {
115 // Notional token range for error reporting purposes.
116 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800117 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700118 }
119}
120
David Tolnay83496eb2020-05-04 00:36:53 -0700121impl ToTokens for Struct {
122 fn to_tokens(&self, tokens: &mut TokenStream) {
123 // Notional token range for error reporting purposes.
124 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800125 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700126 }
127}
128
129impl ToTokens for Enum {
130 fn to_tokens(&self, tokens: &mut TokenStream) {
131 // Notional token range for error reporting purposes.
132 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800133 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700134 }
135}
136
David Tolnay7db73692019-10-20 14:51:12 -0400137impl ToTokens for ExternFn {
138 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700139 // Notional token range for error reporting purposes.
140 self.sig.fn_token.to_tokens(tokens);
141 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400142 }
143}
David Tolnayc071b892020-03-18 16:59:53 -0700144
David Tolnay7e69f892020-10-03 22:20:22 -0700145impl ToTokens for Impl {
146 fn to_tokens(&self, tokens: &mut TokenStream) {
147 self.impl_token.to_tokens(tokens);
148 self.ty.to_tokens(tokens);
149 self.brace_token.surround(tokens, |_tokens| {});
150 }
151}
152
David Tolnayc071b892020-03-18 16:59:53 -0700153impl ToTokens for Signature {
154 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700155 self.fn_token.to_tokens(tokens);
156 self.paren_token.surround(tokens, |tokens| {
157 self.args.to_tokens(tokens);
158 });
159 if let Some(ret) = &self.ret {
160 Token![->](self.paren_token.span).to_tokens(tokens);
161 if let Some((result, langle, rangle)) = self.throws_tokens {
162 result.to_tokens(tokens);
163 langle.to_tokens(tokens);
164 ret.to_tokens(tokens);
165 rangle.to_tokens(tokens);
166 } else {
167 ret.to_tokens(tokens);
168 }
David Tolnay57c166d2020-11-14 23:38:31 -0800169 } else if let Some((result, langle, rangle)) = self.throws_tokens {
170 Token![->](self.paren_token.span).to_tokens(tokens);
171 result.to_tokens(tokens);
172 langle.to_tokens(tokens);
173 token::Paren(langle.span).surround(tokens, |_| ());
174 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700175 }
David Tolnayc071b892020-03-18 16:59:53 -0700176 }
177}
David Tolnayfb6e3862020-04-20 01:33:23 -0700178
Adrian Taylorc8713432020-10-21 18:20:55 -0700179impl ToTokens for ResolvableName {
180 fn to_tokens(&self, tokens: &mut TokenStream) {
181 self.rust.to_tokens(tokens);
182 }
183}
184
David Tolnay18ba92c2020-04-22 16:17:30 -0700185pub struct ReceiverType<'a>(&'a Receiver);
186
187impl Receiver {
188 // &TheType
189 pub fn ty(&self) -> ReceiverType {
190 ReceiverType(self)
191 }
192}
193
194impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700195 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800196 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
197 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
198 langle.to_tokens(tokens);
199 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700200 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700201 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700202 self.0.mutability.to_tokens(tokens);
203 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800204 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
205 rangle.to_tokens(tokens);
206 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700207 }
208}