blob: d450f73065c3130dd7c91a5eb259cc28736ef2fd [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) => {
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 Tolnay4377a9e2020-04-24 15:20:26 -070023 Type::RustBox(ty) | Type::UniquePtr(ty) | Type::CxxVector(ty) | Type::RustVec(ty) => {
Myron Ahneba35cf2020-02-05 19:41:51 +070024 ty.to_tokens(tokens)
25 }
David Tolnaye0dca7b2020-11-25 17:18:57 -080026 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Xiangpeng Hao78762352020-11-12 10:24:18 +080027 Type::Array(a) => a.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070028 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070029 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay73b72642020-11-25 17:44:05 -080030 Type::SliceRef(r) => r.to_tokens(tokens),
David Tolnay7db73692019-10-20 14:51:12 -040031 }
32 }
33}
34
35impl ToTokens for Var {
36 fn to_tokens(&self, tokens: &mut TokenStream) {
37 self.ident.to_tokens(tokens);
38 Token![:](self.ident.span()).to_tokens(tokens);
39 self.ty.to_tokens(tokens);
40 }
41}
42
43impl ToTokens for Ty1 {
44 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay51cc8ee2020-04-25 14:10:29 -070045 let span = self.name.span();
David Tolnayda71ef62020-11-02 00:36:00 -080046 let name = self.name.to_string();
David Tolnay51cc8ee2020-04-25 14:10:29 -070047 if let "UniquePtr" | "CxxVector" = name.as_str() {
David Tolnay7db73692019-10-20 14:51:12 -040048 tokens.extend(quote_spanned!(span=> ::cxx::));
David Tolnay51cc8ee2020-04-25 14:10:29 -070049 } else if name == "Vec" {
50 tokens.extend(quote_spanned!(span=> ::std::vec::));
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 self.name.to_tokens(tokens);
53 self.langle.to_tokens(tokens);
54 self.inner.to_tokens(tokens);
55 self.rangle.to_tokens(tokens);
56 }
57}
58
59impl ToTokens for Ref {
60 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -080061 if let Some((pin, langle, _rangle)) = self.pin_tokens {
62 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
63 langle.to_tokens(tokens);
64 }
David Tolnay7db73692019-10-20 14:51:12 -040065 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070066 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040067 self.mutability.to_tokens(tokens);
68 self.inner.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -080069 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
70 rangle.to_tokens(tokens);
71 }
David Tolnay7db73692019-10-20 14:51:12 -040072 }
73}
74
David Tolnaye0dca7b2020-11-25 17:18:57 -080075impl ToTokens for SliceRef {
76 fn to_tokens(&self, tokens: &mut TokenStream) {
77 self.ampersand.to_tokens(tokens);
78 self.lifetime.to_tokens(tokens);
79 self.mutability.to_tokens(tokens);
80 self.bracket.surround(tokens, |tokens| {
81 self.inner.to_tokens(tokens);
82 });
83 }
84}
85
Xiangpeng Hao78762352020-11-12 10:24:18 +080086impl ToTokens for Array {
87 fn to_tokens(&self, tokens: &mut TokenStream) {
88 self.bracket.surround(tokens, |tokens| {
89 self.inner.to_tokens(tokens);
90 self.semi_token.to_tokens(tokens);
David Tolnayc351dc42020-11-24 20:45:13 -080091 self.len_token.to_tokens(tokens);
Xiangpeng Hao78762352020-11-12 10:24:18 +080092 });
93 }
94}
95
David Tolnay7db73692019-10-20 14:51:12 -040096impl ToTokens for Derive {
97 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye86b9cf2020-05-10 14:24:29 -070098 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040099 }
100}
101
David Tolnayc605e6f2020-05-10 23:37:12 -0700102impl ToTokens for Atom {
103 fn to_tokens(&self, tokens: &mut TokenStream) {
104 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
105 }
106}
107
David Tolnay83496eb2020-05-04 00:36:53 -0700108impl ToTokens for ExternType {
109 fn to_tokens(&self, tokens: &mut TokenStream) {
110 // Notional token range for error reporting purposes.
111 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800112 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700113 }
114}
115
David Tolnay99383812020-05-04 02:34:33 -0700116impl ToTokens for TypeAlias {
117 fn to_tokens(&self, tokens: &mut TokenStream) {
118 // Notional token range for error reporting purposes.
119 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800120 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700121 }
122}
123
David Tolnay83496eb2020-05-04 00:36:53 -0700124impl ToTokens for Struct {
125 fn to_tokens(&self, tokens: &mut TokenStream) {
126 // Notional token range for error reporting purposes.
127 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800128 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700129 }
130}
131
132impl ToTokens for Enum {
133 fn to_tokens(&self, tokens: &mut TokenStream) {
134 // Notional token range for error reporting purposes.
135 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800136 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700137 }
138}
139
David Tolnay7db73692019-10-20 14:51:12 -0400140impl ToTokens for ExternFn {
141 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700142 // Notional token range for error reporting purposes.
143 self.sig.fn_token.to_tokens(tokens);
144 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
146}
David Tolnayc071b892020-03-18 16:59:53 -0700147
David Tolnay7e69f892020-10-03 22:20:22 -0700148impl ToTokens for Impl {
149 fn to_tokens(&self, tokens: &mut TokenStream) {
150 self.impl_token.to_tokens(tokens);
151 self.ty.to_tokens(tokens);
152 self.brace_token.surround(tokens, |_tokens| {});
153 }
154}
155
David Tolnayc071b892020-03-18 16:59:53 -0700156impl ToTokens for Signature {
157 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700158 self.fn_token.to_tokens(tokens);
159 self.paren_token.surround(tokens, |tokens| {
160 self.args.to_tokens(tokens);
161 });
162 if let Some(ret) = &self.ret {
163 Token![->](self.paren_token.span).to_tokens(tokens);
164 if let Some((result, langle, rangle)) = self.throws_tokens {
165 result.to_tokens(tokens);
166 langle.to_tokens(tokens);
167 ret.to_tokens(tokens);
168 rangle.to_tokens(tokens);
169 } else {
170 ret.to_tokens(tokens);
171 }
David Tolnay57c166d2020-11-14 23:38:31 -0800172 } else if let Some((result, langle, rangle)) = self.throws_tokens {
173 Token![->](self.paren_token.span).to_tokens(tokens);
174 result.to_tokens(tokens);
175 langle.to_tokens(tokens);
176 token::Paren(langle.span).surround(tokens, |_| ());
177 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700178 }
David Tolnayc071b892020-03-18 16:59:53 -0700179 }
180}
David Tolnayfb6e3862020-04-20 01:33:23 -0700181
Adrian Taylorc8713432020-10-21 18:20:55 -0700182impl ToTokens for ResolvableName {
183 fn to_tokens(&self, tokens: &mut TokenStream) {
184 self.rust.to_tokens(tokens);
185 }
186}
187
David Tolnay18ba92c2020-04-22 16:17:30 -0700188pub struct ReceiverType<'a>(&'a Receiver);
189
190impl Receiver {
191 // &TheType
192 pub fn ty(&self) -> ReceiverType {
193 ReceiverType(self)
194 }
195}
196
197impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700198 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800199 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
200 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
201 langle.to_tokens(tokens);
202 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700203 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700204 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700205 self.0.mutability.to_tokens(tokens);
206 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800207 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
208 rangle.to_tokens(tokens);
209 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700210 }
211}