David Tolnay | a52602b | 2020-03-06 10:24:34 -0800 | [diff] [blame] | 1 | use crate::syntax::atom::Atom::*; |
David Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame^] | 2 | use crate::syntax::{Derive, ExternFn, Ref, Signature, Ty1, Type, Var}; |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 3 | use proc_macro2::{Ident, Span, TokenStream}; |
David Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame^] | 4 | use quote::{quote_spanned, ToTokens}; |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | use syn::Token; |
| 6 | |
| 7 | impl ToTokens for Type { |
| 8 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 9 | match self { |
| 10 | Type::Ident(ident) => { |
David Tolnay | a52602b | 2020-03-06 10:24:34 -0800 | [diff] [blame] | 11 | if ident == CxxString { |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 12 | 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 Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame^] | 19 | Type::Fn(f) => f.to_tokens(tokens), |
David Tolnay | d0bb364 | 2020-03-15 23:27:11 -0700 | [diff] [blame] | 20 | Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())), |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl ToTokens for Var { |
| 26 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 27 | self.ident.to_tokens(tokens); |
| 28 | Token).to_tokens(tokens); |
| 29 | self.ty.to_tokens(tokens); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl 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 | |
| 46 | impl 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 | |
| 54 | impl 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 | |
| 64 | impl ToTokens for ExternFn { |
| 65 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d95b119 | 2020-03-18 20:07:46 -0700 | [diff] [blame] | 66 | self.sig.tokens.to_tokens(tokens); |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
David Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame^] | 69 | |
| 70 | impl ToTokens for Signature { |
| 71 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 72 | self.tokens.to_tokens(tokens); |
| 73 | } |
| 74 | } |