blob: 9a1fee05a502fdd2ed9374e15b1a9640a52a1331 [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 Tolnay99383812020-05-04 02:34:33 -07003 Derive, Enum, ExternFn, ExternType, Receiver, Ref, Signature, Slice, Struct, Ty1, Type,
4 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) => {
David Tolnaya52602b2020-03-06 10:24:34 -080014 if ident == CxxString {
David Tolnay7db73692019-10-20 14:51:12 -040015 let span = ident.span();
16 tokens.extend(quote_spanned!(span=> ::cxx::));
17 }
18 ident.to_tokens(tokens);
19 }
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();
42 let name = self.name.to_string();
43 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 Tolnay83496eb2020-05-04 00:36:53 -070078impl ToTokens for ExternType {
79 fn to_tokens(&self, tokens: &mut TokenStream) {
80 // Notional token range for error reporting purposes.
81 self.type_token.to_tokens(tokens);
82 self.ident.to_tokens(tokens);
83 }
84}
85
David Tolnay99383812020-05-04 02:34:33 -070086impl ToTokens for TypeAlias {
87 fn to_tokens(&self, tokens: &mut TokenStream) {
88 // Notional token range for error reporting purposes.
89 self.type_token.to_tokens(tokens);
90 self.ident.to_tokens(tokens);
91 }
92}
93
David Tolnay83496eb2020-05-04 00:36:53 -070094impl ToTokens for Struct {
95 fn to_tokens(&self, tokens: &mut TokenStream) {
96 // Notional token range for error reporting purposes.
97 self.struct_token.to_tokens(tokens);
98 self.ident.to_tokens(tokens);
99 }
100}
101
102impl ToTokens for Enum {
103 fn to_tokens(&self, tokens: &mut TokenStream) {
104 // Notional token range for error reporting purposes.
105 self.enum_token.to_tokens(tokens);
106 self.ident.to_tokens(tokens);
107 }
108}
109
David Tolnay7db73692019-10-20 14:51:12 -0400110impl ToTokens for ExternFn {
111 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700112 // Notional token range for error reporting purposes.
113 self.sig.fn_token.to_tokens(tokens);
114 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -0400115 }
116}
David Tolnayc071b892020-03-18 16:59:53 -0700117
118impl ToTokens for Signature {
119 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700120 self.fn_token.to_tokens(tokens);
121 self.paren_token.surround(tokens, |tokens| {
122 self.args.to_tokens(tokens);
123 });
124 if let Some(ret) = &self.ret {
125 Token![->](self.paren_token.span).to_tokens(tokens);
126 if let Some((result, langle, rangle)) = self.throws_tokens {
127 result.to_tokens(tokens);
128 langle.to_tokens(tokens);
129 ret.to_tokens(tokens);
130 rangle.to_tokens(tokens);
131 } else {
132 ret.to_tokens(tokens);
133 }
134 }
David Tolnayc071b892020-03-18 16:59:53 -0700135 }
136}
David Tolnayfb6e3862020-04-20 01:33:23 -0700137
David Tolnay18ba92c2020-04-22 16:17:30 -0700138pub struct ReceiverType<'a>(&'a Receiver);
139
140impl Receiver {
141 // &TheType
142 pub fn ty(&self) -> ReceiverType {
143 ReceiverType(self)
144 }
145}
146
147impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700148 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay18ba92c2020-04-22 16:17:30 -0700149 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700150 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700151 self.0.mutability.to_tokens(tokens);
152 self.0.ty.to_tokens(tokens);
David Tolnayfb6e3862020-04-20 01:33:23 -0700153 }
154}