blob: 59bb0dea99db85761bfbffe0bb1ea920473944fd [file] [log] [blame]
David Tolnaya52602b2020-03-06 10:24:34 -08001use crate::syntax::atom::Atom::*;
David Tolnayc071b892020-03-18 16:59:53 -07002use crate::syntax::{Derive, ExternFn, Ref, Signature, Ty1, Type, Var};
David Tolnay7db73692019-10-20 14:51:12 -04003use proc_macro2::{Ident, Span, TokenStream};
David Tolnayc071b892020-03-18 16:59:53 -07004use quote::{quote_spanned, ToTokens};
David Tolnay7db73692019-10-20 14:51:12 -04005use syn::Token;
6
7impl ToTokens for Type {
8 fn to_tokens(&self, tokens: &mut TokenStream) {
9 match self {
10 Type::Ident(ident) => {
David Tolnaya52602b2020-03-06 10:24:34 -080011 if ident == CxxString {
David Tolnay7db73692019-10-20 14:51:12 -040012 let span = ident.span();
13 tokens.extend(quote_spanned!(span=> ::cxx::));
14 }
15 ident.to_tokens(tokens);
16 }
17 Type::RustBox(ty) | Type::UniquePtr(ty) => ty.to_tokens(tokens),
18 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
David Tolnayc071b892020-03-18 16:59:53 -070019 Type::Fn(f) => f.to_tokens(tokens),
David Tolnayd0bb3642020-03-15 23:27:11 -070020 Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())),
David Tolnay7db73692019-10-20 14:51:12 -040021 }
22 }
23}
24
25impl ToTokens for Var {
26 fn to_tokens(&self, tokens: &mut TokenStream) {
27 self.ident.to_tokens(tokens);
28 Token![:](self.ident.span()).to_tokens(tokens);
29 self.ty.to_tokens(tokens);
30 }
31}
32
33impl ToTokens for Ty1 {
34 fn to_tokens(&self, tokens: &mut TokenStream) {
35 if self.name == "UniquePtr" {
36 let span = self.name.span();
37 tokens.extend(quote_spanned!(span=> ::cxx::));
38 }
39 self.name.to_tokens(tokens);
40 self.langle.to_tokens(tokens);
41 self.inner.to_tokens(tokens);
42 self.rangle.to_tokens(tokens);
43 }
44}
45
46impl ToTokens for Ref {
47 fn to_tokens(&self, tokens: &mut TokenStream) {
48 self.ampersand.to_tokens(tokens);
49 self.mutability.to_tokens(tokens);
50 self.inner.to_tokens(tokens);
51 }
52}
53
54impl ToTokens for Derive {
55 fn to_tokens(&self, tokens: &mut TokenStream) {
56 let name = match self {
57 Derive::Clone => "Clone",
58 Derive::Copy => "Copy",
59 };
60 Ident::new(name, Span::call_site()).to_tokens(tokens);
61 }
62}
63
64impl ToTokens for ExternFn {
65 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd95b1192020-03-18 20:07:46 -070066 self.sig.tokens.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68}
David Tolnayc071b892020-03-18 16:59:53 -070069
70impl ToTokens for Signature {
71 fn to_tokens(&self, tokens: &mut TokenStream) {
72 self.tokens.to_tokens(tokens);
73 }
74}