blob: cf44b4d74977d886e6e0057b699746b1de8631ba [file] [log] [blame]
David Tolnayf38cdf62016-09-23 19:07:09 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayf38cdf62016-09-23 19:07:09 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// Struct or enum sent to a `proc_macro_derive` macro.
6 pub struct DeriveInput {
7 /// Name of the struct or enum.
8 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -05009
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// Visibility of the struct or enum.
11 pub vis: Visibility,
Clar Charrd22b5702017-03-10 15:24:56 -050012
Alex Crichton62a0a592017-05-22 13:58:53 -070013 /// Attributes tagged on the whole struct or enum.
14 pub attrs: Vec<Attribute>,
Clar Charrd22b5702017-03-10 15:24:56 -050015
Alex Crichton62a0a592017-05-22 13:58:53 -070016 /// Generics required to complete the definition.
17 pub generics: Generics,
Clar Charrd22b5702017-03-10 15:24:56 -050018
Alex Crichton62a0a592017-05-22 13:58:53 -070019 /// Data within the struct or enum.
20 pub body: Body,
21 }
David Tolnayf38cdf62016-09-23 19:07:09 -070022}
23
Clar Charrd22b5702017-03-10 15:24:56 -050024
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// Body of a derived struct or enum.
27 pub enum Body {
28 /// It's an enum.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 pub Enum(BodyEnum {
30 pub enum_token: tokens::Enum,
31 pub brace_token: tokens::Brace,
32 pub variants: Delimited<Variant, tokens::Comma>,
33 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070034
35 /// It's a struct.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070036 pub Struct(BodyStruct {
37 pub data: VariantData,
38 pub struct_token: tokens::Struct,
39 pub semi_token: Option<tokens::Semi>,
40 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070041 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070042
43 do_not_generate_to_tokens
David Tolnayf38cdf62016-09-23 19:07:09 -070044}
45
46#[cfg(feature = "parsing")]
47pub mod parsing {
48 use super::*;
David Tolnay3cf52982016-10-01 17:11:37 -070049 use Generics;
David Tolnay4a51dc72016-10-01 00:40:31 -070050 use attr::parsing::outer_attr;
David Tolnayf38cdf62016-09-23 19:07:09 -070051 use data::parsing::{visibility, struct_body, enum_body};
David Tolnay28c1db62016-10-27 22:48:18 -070052 use generics::parsing::generics;
David Tolnayf38cdf62016-09-23 19:07:09 -070053 use ident::parsing::ident;
David Tolnayf38cdf62016-09-23 19:07:09 -070054
David Tolnay0e837402016-12-22 17:25:55 -050055 named!(pub derive_input -> DeriveInput, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -070056 attrs: many0!(outer_attr) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070057 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -070058 which: alt!(keyword!("struct") | keyword!("enum")) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070059 id: ident >>
60 generics: generics >>
61 item: switch!(value!(which),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070062 "struct" => map!(struct_body, move |(wh, body, semi)| DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070063 ident: id,
64 vis: vis,
65 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070066 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070067 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070068 .. generics
69 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -070070 body: Body::Struct(BodyStruct {
71 struct_token: tokens::Struct::default(),
72 data: body,
73 semi_token: semi,
74 }),
David Tolnayf38cdf62016-09-23 19:07:09 -070075 })
76 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 "enum" => map!(enum_body, move |(wh, body, brace)| DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -070078 ident: id,
79 vis: vis,
80 attrs: attrs,
David Tolnay3cf52982016-10-01 17:11:37 -070081 generics: Generics {
David Tolnay28c1db62016-10-27 22:48:18 -070082 where_clause: wh,
David Tolnay3cf52982016-10-01 17:11:37 -070083 .. generics
84 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 body: Body::Enum(BodyEnum {
86 variants: body,
87 brace_token: brace,
88 enum_token: tokens::Enum::default(),
89 }),
David Tolnayf38cdf62016-09-23 19:07:09 -070090 })
91 ) >>
David Tolnayf38cdf62016-09-23 19:07:09 -070092 (item)
93 ));
94}
95
David Tolnayc2dfbf42016-09-23 23:52:15 -070096#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -070097mod printing {
98 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -070099 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700100 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -0700101 use quote::{Tokens, ToTokens};
102
David Tolnay0e837402016-12-22 17:25:55 -0500103 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700104 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700105 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700106 attr.to_tokens(tokens);
107 }
David Tolnay47a877c2016-10-01 16:50:55 -0700108 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700109 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700110 Body::Enum(ref d) => d.enum_token.to_tokens(tokens),
111 Body::Struct(ref d) => d.struct_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700112 }
113 self.ident.to_tokens(tokens);
114 self.generics.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700115 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700116 Body::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700117 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 data.brace_token.surround(tokens, |tokens| {
119 data.variants.to_tokens(tokens);
120 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700121 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 Body::Struct(ref data) => {
123 match data.data {
124 VariantData::Struct(..) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700125 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 data.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -0700127 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700128 VariantData::Tuple(..) => {
129 data.data.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700130 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700131 }
132 VariantData::Unit => {
133 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700134 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700135 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700136 data.semi_token.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700137 }
138 }
139 }
140 }
141}