blob: 40ab7239828637481975dd37dfe166d9785c7d65 [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 Tolnay51cc8ee2020-04-25 14:10:29 -070043 if let "UniquePtr" | "CxxVector" = name.as_str() {
David Tolnay7db73692019-10-20 14:51:12 -040044 tokens.extend(quote_spanned!(span=> ::cxx::));
David Tolnay51cc8ee2020-04-25 14:10:29 -070045 } else if name == "Vec" {
46 tokens.extend(quote_spanned!(span=> ::std::vec::));
David Tolnay7db73692019-10-20 14:51:12 -040047 }
48 self.name.to_tokens(tokens);
49 self.langle.to_tokens(tokens);
50 self.inner.to_tokens(tokens);
51 self.rangle.to_tokens(tokens);
52 }
53}
54
55impl ToTokens for Ref {
56 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -080057 if let Some((pin, langle, _rangle)) = self.pin_tokens {
58 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
59 langle.to_tokens(tokens);
60 }
David Tolnay7db73692019-10-20 14:51:12 -040061 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070062 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040063 self.mutability.to_tokens(tokens);
64 self.inner.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -080065 if let Some((_pin, _langle, rangle)) = self.pin_tokens {
66 rangle.to_tokens(tokens);
67 }
David Tolnay7db73692019-10-20 14:51:12 -040068 }
69}
70
Adrian Taylorf5dd5522020-04-13 16:50:14 -070071impl ToTokens for Slice {
72 fn to_tokens(&self, tokens: &mut TokenStream) {
73 self.bracket.surround(tokens, |tokens| {
74 self.inner.to_tokens(tokens);
75 });
76 }
77}
78
David Tolnay7db73692019-10-20 14:51:12 -040079impl ToTokens for Derive {
80 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye86b9cf2020-05-10 14:24:29 -070081 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040082 }
83}
84
David Tolnayc605e6f2020-05-10 23:37:12 -070085impl ToTokens for Atom {
86 fn to_tokens(&self, tokens: &mut TokenStream) {
87 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
88 }
89}
90
David Tolnay83496eb2020-05-04 00:36:53 -070091impl ToTokens for ExternType {
92 fn to_tokens(&self, tokens: &mut TokenStream) {
93 // Notional token range for error reporting purposes.
94 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -080095 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -070096 }
97}
98
David Tolnay99383812020-05-04 02:34:33 -070099impl ToTokens for TypeAlias {
100 fn to_tokens(&self, tokens: &mut TokenStream) {
101 // Notional token range for error reporting purposes.
102 self.type_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800103 self.name.rust.to_tokens(tokens);
David Tolnay99383812020-05-04 02:34:33 -0700104 }
105}
106
David Tolnay83496eb2020-05-04 00:36:53 -0700107impl ToTokens for Struct {
108 fn to_tokens(&self, tokens: &mut TokenStream) {
109 // Notional token range for error reporting purposes.
110 self.struct_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800111 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700112 }
113}
114
115impl ToTokens for Enum {
116 fn to_tokens(&self, tokens: &mut TokenStream) {
117 // Notional token range for error reporting purposes.
118 self.enum_token.to_tokens(tokens);
David Tolnay9bb232b2020-11-02 00:55:18 -0800119 self.name.rust.to_tokens(tokens);
David Tolnay83496eb2020-05-04 00:36:53 -0700120 }
121}
122
David Tolnay7db73692019-10-20 14:51:12 -0400123impl ToTokens for ExternFn {
124 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700125 // Notional token range for error reporting purposes.
126 self.sig.fn_token.to_tokens(tokens);
127 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400128 }
129}
David Tolnayc071b892020-03-18 16:59:53 -0700130
David Tolnay7e69f892020-10-03 22:20:22 -0700131impl ToTokens for Impl {
132 fn to_tokens(&self, tokens: &mut TokenStream) {
133 self.impl_token.to_tokens(tokens);
134 self.ty.to_tokens(tokens);
135 self.brace_token.surround(tokens, |_tokens| {});
136 }
137}
138
David Tolnayc071b892020-03-18 16:59:53 -0700139impl ToTokens for Signature {
140 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700141 self.fn_token.to_tokens(tokens);
142 self.paren_token.surround(tokens, |tokens| {
143 self.args.to_tokens(tokens);
144 });
145 if let Some(ret) = &self.ret {
146 Token![->](self.paren_token.span).to_tokens(tokens);
147 if let Some((result, langle, rangle)) = self.throws_tokens {
148 result.to_tokens(tokens);
149 langle.to_tokens(tokens);
150 ret.to_tokens(tokens);
151 rangle.to_tokens(tokens);
152 } else {
153 ret.to_tokens(tokens);
154 }
David Tolnay57c166d2020-11-14 23:38:31 -0800155 } else if let Some((result, langle, rangle)) = self.throws_tokens {
156 Token![->](self.paren_token.span).to_tokens(tokens);
157 result.to_tokens(tokens);
158 langle.to_tokens(tokens);
159 token::Paren(langle.span).surround(tokens, |_| ());
160 rangle.to_tokens(tokens);
David Tolnaye3a48152020-04-08 19:38:05 -0700161 }
David Tolnayc071b892020-03-18 16:59:53 -0700162 }
163}
David Tolnayfb6e3862020-04-20 01:33:23 -0700164
Adrian Taylorc8713432020-10-21 18:20:55 -0700165impl ToTokens for ResolvableName {
166 fn to_tokens(&self, tokens: &mut TokenStream) {
167 self.rust.to_tokens(tokens);
168 }
169}
170
David Tolnay18ba92c2020-04-22 16:17:30 -0700171pub struct ReceiverType<'a>(&'a Receiver);
172
173impl Receiver {
174 // &TheType
175 pub fn ty(&self) -> ReceiverType {
176 ReceiverType(self)
177 }
178}
179
180impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700181 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayc9673842020-11-15 16:26:10 -0800182 if let Some((pin, langle, _rangle)) = self.0.pin_tokens {
183 tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
184 langle.to_tokens(tokens);
185 }
David Tolnay18ba92c2020-04-22 16:17:30 -0700186 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700187 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700188 self.0.mutability.to_tokens(tokens);
189 self.0.ty.to_tokens(tokens);
David Tolnayc9673842020-11-15 16:26:10 -0800190 if let Some((_pin, _langle, rangle)) = self.0.pin_tokens {
191 rangle.to_tokens(tokens);
192 }
David Tolnayfb6e3862020-04-20 01:33:23 -0700193 }
194}