blob: af8088da2917af470ee41c27c03b3fcb8d8360ed [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 Tolnay9bb232b2020-11-02 00:55:18 -08003 Atom, Derive, Enum, ExternFn, ExternType, Impl, Receiver, Ref, ResolvableName, Signature,
Adrian Taylorc8713432020-10-21 18:20:55 -07004 Slice, 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 }
Adrian Taylorf5dd5522020-04-13 16:50:14 -070023 Type::Ref(r) | Type::Str(r) | Type::SliceRefU8(r) => r.to_tokens(tokens),
24 Type::Slice(s) => s.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 Tolnay7db73692019-10-20 14:51:12 -040027 }
28 }
29}
30
31impl ToTokens for Var {
32 fn to_tokens(&self, tokens: &mut TokenStream) {
33 self.ident.to_tokens(tokens);
34 Token![:](self.ident.span()).to_tokens(tokens);
35 self.ty.to_tokens(tokens);
36 }
37}
38
39impl ToTokens for Ty1 {
40 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay51cc8ee2020-04-25 14:10:29 -070041 let span = self.name.span();
David Tolnayda71ef62020-11-02 00:36:00 -080042 let name = self.name.to_string();
David Tolnaya4170652020-11-15 17:00:59 -080043 if let Some((pin, langle, _rangle)) = self.pin_tokens {
44 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
45 langle.to_tokens(tokens);
46 }
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);
David Tolnaya4170652020-11-15 17:00:59 -080056 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
57 rangle.to_tokens(tokens);
58 }
David Tolnay7db73692019-10-20 14:51:12 -040059 }
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
Adrian Taylorf5dd5522020-04-13 16:50:14 -070078impl ToTokens for Slice {
79 fn to_tokens(&self, tokens: &mut TokenStream) {
80 self.bracket.surround(tokens, |tokens| {
81 self.inner.to_tokens(tokens);
82 });
83 }
84}
85
David Tolnay7db73692019-10-20 14:51:12 -040086impl ToTokens for Derive {
87 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye86b9cf2020-05-10 14:24:29 -070088 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040089 }
90}
91
David Tolnayc605e6f2020-05-10 23:37:12 -070092impl ToTokens for Atom {
93 fn to_tokens(&self, tokens: &mut TokenStream) {
94 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
95 }
96}
97
David Tolnay83496eb2020-05-04 00:36:53 -070098impl ToTokens for ExternType {
99 fn to_tokens(&self, tokens: &mut TokenStream) {
100 // Notional token range for error reporting purposes.
101 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800102 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700103 }
104}
105
David Tolnay99383812020-05-04 02:34:33 -0700106impl ToTokens for TypeAlias {
107 fn to_tokens(&self, tokens: &mut TokenStream) {
108 // Notional token range for error reporting purposes.
109 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800110 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700111 }
112}
113
David Tolnay83496eb2020-05-04 00:36:53 -0700114impl ToTokens for Struct {
115 fn to_tokens(&self, tokens: &mut TokenStream) {
116 // Notional token range for error reporting purposes.
117 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800118 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700119 }
120}
121
122impl ToTokens for Enum {
123 fn to_tokens(&self, tokens: &mut TokenStream) {
124 // Notional token range for error reporting purposes.
125 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800126 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700127 }
128}
129
David Tolnay7db73692019-10-20 14:51:12 -0400130impl ToTokens for ExternFn {
131 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700132 // Notional token range for error reporting purposes.
133 self.sig.fn_token.to_tokens(tokens);
134 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400135 }
136}
David Tolnayc071b892020-03-18 16:59:53 -0700137
David Tolnay7e69f892020-10-03 22:20:22 -0700138impl ToTokens for Impl {
139 fn to_tokens(&self, tokens: &mut TokenStream) {
140 self.impl_token.to_tokens(tokens);
141 self.ty.to_tokens(tokens);
142 self.brace_token.surround(tokens, |_tokens| {});
143 }
144}
145
David Tolnayc071b892020-03-18 16:59:53 -0700146impl ToTokens for Signature {
147 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700148 self.fn_token.to_tokens(tokens);
149 self.paren_token.surround(tokens, |tokens| {
150 self.args.to_tokens(tokens);
151 });
152 if let Some(ret) = &self.ret {
153 Token![->](self.paren_token.span).to_tokens(tokens);
154 if let Some((result, langle, rangle)) = self.throws_tokens {
155 result.to_tokens(tokens);
156 langle.to_tokens(tokens);
157 ret.to_tokens(tokens);
158 rangle.to_tokens(tokens);
159 } else {
160 ret.to_tokens(tokens);
161 }
David Tolnay57c166d2020-11-14 23:38:31 -0800162 } else if let Some((result, langle, rangle)) = self.throws_tokens {
163 Token![->](self.paren_token.span).to_tokens(tokens);
164 result.to_tokens(tokens);
165 langle.to_tokens(tokens);
166 token::Paren(langle.span).surround(tokens, |_| ());
167 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700168 }
David Tolnayc071b892020-03-18 16:59:53 -0700169 }
170}
David Tolnayfb6e3862020-04-20 01:33:23 -0700171
Adrian Taylorc8713432020-10-21 18:20:55 -0700172impl ToTokens for ResolvableName {
173 fn to_tokens(&self, tokens: &mut TokenStream) {
174 self.rust.to_tokens(tokens);
175 }
176}
177
David Tolnay18ba92c2020-04-22 16:17:30 -0700178pub struct ReceiverType<'a>(&'a Receiver);
179
180impl Receiver {
181 // &TheType
182 pub fn ty(&self) -> ReceiverType {
183 ReceiverType(self)
184 }
185}
186
187impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700188 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800189 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
190 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
191 langle.to_tokens(tokens);
192 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700193 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700194 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700195 self.0.mutability.to_tokens(tokens);
196 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800197 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
198 rangle.to_tokens(tokens);
199 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700200 }
201}