blob: 57db8eb7edb9e8260d163e17240a02024bd91c2c [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::{
Adrian Taylorc8713432020-10-21 18:20:55 -07003 Atom, Derive, Enum, ExternFn, ExternType, Impl, Pair, Receiver, Ref, ResolvableName, Signature,
4 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 Tolnay7db73692019-10-20 14:51:12 -04008use syn::Token;
9
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();
Adrian Taylorc8713432020-10-21 18:20:55 -070042 let name = self.name.rust.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) {
57 self.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -070058 self.lifetime.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040059 self.mutability.to_tokens(tokens);
60 self.inner.to_tokens(tokens);
61 }
62}
63
Adrian Taylorf5dd5522020-04-13 16:50:14 -070064impl ToTokens for Slice {
65 fn to_tokens(&self, tokens: &mut TokenStream) {
66 self.bracket.surround(tokens, |tokens| {
67 self.inner.to_tokens(tokens);
68 });
69 }
70}
71
David Tolnay7db73692019-10-20 14:51:12 -040072impl ToTokens for Derive {
73 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye86b9cf2020-05-10 14:24:29 -070074 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040075 }
76}
77
David Tolnayc605e6f2020-05-10 23:37:12 -070078impl ToTokens for Atom {
79 fn to_tokens(&self, tokens: &mut TokenStream) {
80 Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
81 }
82}
83
David Tolnay83496eb2020-05-04 00:36:53 -070084impl ToTokens for ExternType {
85 fn to_tokens(&self, tokens: &mut TokenStream) {
86 // Notional token range for error reporting purposes.
87 self.type_token.to_tokens(tokens);
88 self.ident.to_tokens(tokens);
89 }
90}
91
David Tolnay99383812020-05-04 02:34:33 -070092impl ToTokens for TypeAlias {
93 fn to_tokens(&self, tokens: &mut TokenStream) {
94 // Notional token range for error reporting purposes.
95 self.type_token.to_tokens(tokens);
96 self.ident.to_tokens(tokens);
97 }
98}
99
David Tolnay83496eb2020-05-04 00:36:53 -0700100impl ToTokens for Struct {
101 fn to_tokens(&self, tokens: &mut TokenStream) {
102 // Notional token range for error reporting purposes.
103 self.struct_token.to_tokens(tokens);
104 self.ident.to_tokens(tokens);
105 }
106}
107
108impl ToTokens for Enum {
109 fn to_tokens(&self, tokens: &mut TokenStream) {
110 // Notional token range for error reporting purposes.
111 self.enum_token.to_tokens(tokens);
112 self.ident.to_tokens(tokens);
113 }
114}
115
David Tolnay7db73692019-10-20 14:51:12 -0400116impl ToTokens for ExternFn {
117 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700118 // Notional token range for error reporting purposes.
119 self.sig.fn_token.to_tokens(tokens);
120 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400121 }
122}
David Tolnayc071b892020-03-18 16:59:53 -0700123
Adrian Taylorc8713432020-10-21 18:20:55 -0700124impl ToTokens for Pair {
125 fn to_tokens(&self, tokens: &mut TokenStream) {
126 self.rust.to_tokens(tokens);
127 }
128}
129
David Tolnay7e69f892020-10-03 22:20:22 -0700130impl ToTokens for Impl {
131 fn to_tokens(&self, tokens: &mut TokenStream) {
132 self.impl_token.to_tokens(tokens);
133 self.ty.to_tokens(tokens);
134 self.brace_token.surround(tokens, |_tokens| {});
135 }
136}
137
David Tolnayc071b892020-03-18 16:59:53 -0700138impl ToTokens for Signature {
139 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700140 self.fn_token.to_tokens(tokens);
141 self.paren_token.surround(tokens, |tokens| {
142 self.args.to_tokens(tokens);
143 });
144 if let Some(ret) = &self.ret {
145 Token![->](self.paren_token.span).to_tokens(tokens);
146 if let Some((result, langle, rangle)) = self.throws_tokens {
147 result.to_tokens(tokens);
148 langle.to_tokens(tokens);
149 ret.to_tokens(tokens);
150 rangle.to_tokens(tokens);
151 } else {
152 ret.to_tokens(tokens);
153 }
154 }
David Tolnayc071b892020-03-18 16:59:53 -0700155 }
156}
David Tolnayfb6e3862020-04-20 01:33:23 -0700157
Adrian Taylorc8713432020-10-21 18:20:55 -0700158impl ToTokens for ResolvableName {
159 fn to_tokens(&self, tokens: &mut TokenStream) {
160 self.rust.to_tokens(tokens);
161 }
162}
163
David Tolnay18ba92c2020-04-22 16:17:30 -0700164pub struct ReceiverType<'a>(&'a Receiver);
165
166impl Receiver {
167 // &TheType
168 pub fn ty(&self) -> ReceiverType {
169 ReceiverType(self)
170 }
171}
172
173impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700174 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay18ba92c2020-04-22 16:17:30 -0700175 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700176 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700177 self.0.mutability.to_tokens(tokens);
178 self.0.ty.to_tokens(tokens);
David Tolnayfb6e3862020-04-20 01:33:23 -0700179 }
180}