David Tolnay | a52602b | 2020-03-06 10:24:34 -0800 | [diff] [blame] | 1 | use crate::syntax::atom::Atom::*; |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | use crate::syntax::{Derive, ExternFn, Ref, Ty1, Type, Var}; |
| 3 | use proc_macro2::{Ident, Span, TokenStream}; |
| 4 | use quote::{quote_spanned, ToTokens}; |
| 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 | d0bb364 | 2020-03-15 23:27:11 -0700 | [diff] [blame^] | 19 | Type::Void(span) => tokens.extend(quote_spanned!(*span=> ())), |
David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl ToTokens for Var { |
| 25 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 26 | self.ident.to_tokens(tokens); |
| 27 | Token).to_tokens(tokens); |
| 28 | self.ty.to_tokens(tokens); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl ToTokens for Ty1 { |
| 33 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 34 | if self.name == "UniquePtr" { |
| 35 | let span = self.name.span(); |
| 36 | tokens.extend(quote_spanned!(span=> ::cxx::)); |
| 37 | } |
| 38 | self.name.to_tokens(tokens); |
| 39 | self.langle.to_tokens(tokens); |
| 40 | self.inner.to_tokens(tokens); |
| 41 | self.rangle.to_tokens(tokens); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl ToTokens for Ref { |
| 46 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 47 | self.ampersand.to_tokens(tokens); |
| 48 | self.mutability.to_tokens(tokens); |
| 49 | self.inner.to_tokens(tokens); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl ToTokens for Derive { |
| 54 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 55 | let name = match self { |
| 56 | Derive::Clone => "Clone", |
| 57 | Derive::Copy => "Copy", |
| 58 | }; |
| 59 | Ident::new(name, Span::call_site()).to_tokens(tokens); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl ToTokens for ExternFn { |
| 64 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 65 | self.fn_token.to_tokens(tokens); |
| 66 | self.ident.to_tokens(tokens); |
| 67 | self.semi_token.to_tokens(tokens); |
| 68 | } |
| 69 | } |