blob: 4ed264a7816679ed372f556c850498777b4dc5cd [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 Tolnayc605e6f2020-05-10 23:37:12 -07003 Atom, Derive, Enum, ExternFn, ExternType, Receiver, Ref, Signature, Slice, Struct, Ty1, Type,
David Tolnay99383812020-05-04 02:34:33 -07004 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 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
124impl ToTokens for Signature {
125 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -0700126 self.fn_token.to_tokens(tokens);
127 self.paren_token.surround(tokens, |tokens| {
128 self.args.to_tokens(tokens);
129 });
130 if let Some(ret) = &self.ret {
131 Token![->](self.paren_token.span).to_tokens(tokens);
132 if let Some((result, langle, rangle)) = self.throws_tokens {
133 result.to_tokens(tokens);
134 langle.to_tokens(tokens);
135 ret.to_tokens(tokens);
136 rangle.to_tokens(tokens);
137 } else {
138 ret.to_tokens(tokens);
139 }
140 }
David Tolnayc071b892020-03-18 16:59:53 -0700141 }
142}
David Tolnayfb6e3862020-04-20 01:33:23 -0700143
David Tolnay18ba92c2020-04-22 16:17:30 -0700144pub struct ReceiverType<'a>(&'a Receiver);
145
146impl Receiver {
147 // &TheType
148 pub fn ty(&self) -> ReceiverType {
149 ReceiverType(self)
150 }
151}
152
153impl ToTokens for ReceiverType<'_> {
David Tolnayfb6e3862020-04-20 01:33:23 -0700154 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay18ba92c2020-04-22 16:17:30 -0700155 self.0.ampersand.to_tokens(tokens);
David Tolnay0bd50fa2020-04-22 15:31:33 -0700156 self.0.lifetime.to_tokens(tokens);
David Tolnay18ba92c2020-04-22 16:17:30 -0700157 self.0.mutability.to_tokens(tokens);
158 self.0.ty.to_tokens(tokens);
David Tolnayfb6e3862020-04-20 01:33:23 -0700159 }
160}