blob: 2e14f7aa99e805eb8b49d1e33c5054df21d7c8fd [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 Tolnay8c80de72020-12-28 22:13:34 -08003 Array, Atom, Derive, Enum, ExternFn, ExternType, Impl, Lifetimes, Receiver, Ref, RustName,
4 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 }
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)
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),
Xiangpeng Hao78762352020-11-12 10:24:18 +080030 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070031 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070032 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080033 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040034 }
35 }
36}
37
38impl ToTokens for Var {
39 fn to_tokens(&self, tokens: &mut TokenStream) {
40 self.ident.to_tokens(tokens);
41 Token![:](self.ident.span()).to_tokens(tokens);
42 self.ty.to_tokens(tokens);
43 }
44}
45
46impl ToTokens for Ty1 {
47 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay51cc8ee2020-04-25 14:10:29 -070048 let span = self.name.span();
David Tolnayda71ef62020-11-02 00:36:00 -080049 let name = self.name.to_string();
David Tolnay215e77f2020-12-28 17:09:48 -080050 if let "UniquePtr" | "SharedPtr" | "WeakPtr" | "CxxVector" = name.as_str() {
David Tolnay7db73692019-10-20 14:51:12 -040051 tokens.extend(quote_spanned!(span=> ::cxx::));
David Tolnay51cc8ee2020-04-25 14:10:29 -070052 } else if name == "Vec" {
53 tokens.extend(quote_spanned!(span=> ::std::vec::));
David Tolnay7db73692019-10-20 14:51:12 -040054 }
55 self.name.to_tokens(tokens);
56 self.langle.to_tokens(tokens);
57 self.inner.to_tokens(tokens);
58 self.rangle.to_tokens(tokens);
59 }
60}
61
62impl ToTokens for Ref {
63 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -080064 if let Some((pin, langle, _rangle)) = self.pin_tokens {
65 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
66 langle.to_tokens(tokens);
67 }
David Tolnay7db73692019-10-20 14:51:12 -040068 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070069 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040070 self.mutability.to_tokens(tokens);
71 self.inner.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -080072 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
73 rangle.to_tokens(tokens);
74 }
David Tolnay7db73692019-10-20 14:51:12 -040075 }
76}
77
David Tolnaye0dca7b2020-11-25 17:18:57 -080078impl ToTokens for SliceRef {
79 fn to_tokens(&self, tokens: &mut TokenStream) {
80 self.ampersand.to_tokens(tokens);
81 self.lifetime.to_tokens(tokens);
82 self.mutability.to_tokens(tokens);
83 self.bracket.surround(tokens, |tokens| {
84 self.inner.to_tokens(tokens);
85 });
86 }
87}
88
Xiangpeng Hao78762352020-11-12 10:24:18 +080089impl ToTokens for Array {
90 fn to_tokens(&self, tokens: &mut TokenStream) {
91 self.bracket.surround(tokens, |tokens| {
92 self.inner.to_tokens(tokens);
93 self.semi_token.to_tokens(tokens);
David Tolnayc351dc42020-11-24 20:45:13 -080094 self.len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +080095 });
96 }
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 Tolnayb600e4f2020-11-27 17:26:49 -0800105impl ToTokens for Derive {
106 fn to_tokens(&self, tokens: &mut TokenStream) {
107 Ident::new(self.what.as_ref(), self.span).to_tokens(tokens);
108 }
109}
110
David Tolnay83496eb2020-05-04 00:36:53 -0700111impl ToTokens for ExternType {
112 fn to_tokens(&self, tokens: &mut TokenStream) {
113 // Notional token range for error reporting purposes.
114 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800115 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700116 }
117}
118
David Tolnay99383812020-05-04 02:34:33 -0700119impl ToTokens for TypeAlias {
120 fn to_tokens(&self, tokens: &mut TokenStream) {
121 // Notional token range for error reporting purposes.
122 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800123 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700124 }
125}
126
David Tolnay83496eb2020-05-04 00:36:53 -0700127impl ToTokens for Struct {
128 fn to_tokens(&self, tokens: &mut TokenStream) {
129 // Notional token range for error reporting purposes.
130 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800131 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700132 }
133}
134
135impl ToTokens for Enum {
136 fn to_tokens(&self, tokens: &mut TokenStream) {
137 // Notional token range for error reporting purposes.
138 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800139 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700140 }
141}
142
David Tolnay7db73692019-10-20 14:51:12 -0400143impl ToTokens for ExternFn {
144 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700145 // Notional token range for error reporting purposes.
146 self.sig.fn_token.to_tokens(tokens);
147 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400148 }
149}
David Tolnayc071b892020-03-18 16:59:53 -0700150
David Tolnay7e69f892020-10-03 22:20:22 -0700151impl ToTokens for Impl {
152 fn to_tokens(&self, tokens: &mut TokenStream) {
153 self.impl_token.to_tokens(tokens);
David Tolnay028d3d22020-11-29 18:09:52 -0800154 self.negative_token.to_tokens(tokens);
David Tolnay7e69f892020-10-03 22:20:22 -0700155 self.ty.to_tokens(tokens);
156 self.brace_token.surround(tokens, |_tokens| {});
157 }
158}
159
David Tolnay8c80de72020-12-28 22:13:34 -0800160impl ToTokens for Lifetimes {
161 fn to_tokens(&self, tokens: &mut TokenStream) {
162 self.lt_token.to_tokens(tokens);
163 self.lifetimes.to_tokens(tokens);
164 self.gt_token.to_tokens(tokens);
165 }
166}
167
David Tolnayc071b892020-03-18 16:59:53 -0700168impl ToTokens for Signature {
169 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700170 self.fn_token.to_tokens(tokens);
171 self.paren_token.surround(tokens, |tokens| {
172 self.args.to_tokens(tokens);
173 });
174 if let Some(ret) = &self.ret {
175 Token![->](self.paren_token.span).to_tokens(tokens);
176 if let Some((result, langle, rangle)) = self.throws_tokens {
177 result.to_tokens(tokens);
178 langle.to_tokens(tokens);
179 ret.to_tokens(tokens);
180 rangle.to_tokens(tokens);
181 } else {
182 ret.to_tokens(tokens);
183 }
David Tolnay57c166d2020-11-14 23:38:31 -0800184 } else if let Some((result, langle, rangle)) = self.throws_tokens {
185 Token![->](self.paren_token.span).to_tokens(tokens);
186 result.to_tokens(tokens);
187 langle.to_tokens(tokens);
188 token::Paren(langle.span).surround(tokens, |_| ());
189 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700190 }
David Tolnayc071b892020-03-18 16:59:53 -0700191 }
192}
David Tolnayfb6e3862020-04-20 01:33:23 -0700193
David Tolnay75ea17c2020-12-06 21:08:34 -0800194impl ToTokens for RustName {
Adrian Taylorc8713432020-10-21 18:20:55 -0700195 fn to_tokens(&self, tokens: &mut TokenStream) {
196 self.rust.to_tokens(tokens);
197 }
198}
199
David Tolnay18ba92c2020-04-22 16:17:30 -0700200pub struct ReceiverType<'a>(&'a Receiver);
201
202impl Receiver {
203 // &TheType
204 pub fn ty(&self) -> ReceiverType {
205 ReceiverType(self)
206 }
207}
208
209impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700210 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800211 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
212 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
213 langle.to_tokens(tokens);
214 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700215 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700216 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700217 self.0.mutability.to_tokens(tokens);
218 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800219 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
220 rangle.to_tokens(tokens);
221 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700222 }
223}