blob: 51592dd8602a85aa71692ed2d45e0ad92cf1094f [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 Tolnaye3a48152020-04-08 19:38:05 -070066 // Notional token range for error reporting purposes.
67 self.sig.fn_token.to_tokens(tokens);
68 self.semi_token.to_tokens(tokens);
David Tolnay7db73692019-10-20 14:51:12 -040069 }
70}
David Tolnayc071b892020-03-18 16:59:53 -070071
72impl ToTokens for Signature {
73 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3a48152020-04-08 19:38:05 -070074 self.fn_token.to_tokens(tokens);
75 self.paren_token.surround(tokens, |tokens| {
76 self.args.to_tokens(tokens);
77 });
78 if let Some(ret) = &self.ret {
79 Token![->](self.paren_token.span).to_tokens(tokens);
80 if let Some((result, langle, rangle)) = self.throws_tokens {
81 result.to_tokens(tokens);
82 langle.to_tokens(tokens);
83 ret.to_tokens(tokens);
84 rangle.to_tokens(tokens);
85 } else {
86 ret.to_tokens(tokens);
87 }
88 }
David Tolnayc071b892020-03-18 16:59:53 -070089 }
90}