blob: 3ad893d8bb3943be3bf6ede55bef9685e0dba323 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf38cdf62016-09-23 19:07:09 -07009use super::*;
Nika Layzell5680dcc2018-01-16 15:14:27 -050010use punctuated::Punctuated;
David Tolnayf38cdf62016-09-23 19:07:09 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_struct! {
David Tolnaye3d41b72017-12-31 15:24:00 -050013 /// Data structure sent to a `proc_macro_derive` macro.
David Tolnay461d98e2018-01-07 11:07:19 -080014 ///
15 /// *This type is available if Syn is built with the `"derive"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070016 pub struct DeriveInput {
David Tolnay4a3f59a2017-12-28 21:21:12 -050017 /// Attributes tagged on the whole struct or enum.
18 pub attrs: Vec<Attribute>,
Clar Charrd22b5702017-03-10 15:24:56 -050019
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// Visibility of the struct or enum.
21 pub vis: Visibility,
Clar Charrd22b5702017-03-10 15:24:56 -050022
David Tolnay4a3f59a2017-12-28 21:21:12 -050023 /// Name of the struct or enum.
24 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -050025
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// Generics required to complete the definition.
27 pub generics: Generics,
Clar Charrd22b5702017-03-10 15:24:56 -050028
Alex Crichton62a0a592017-05-22 13:58:53 -070029 /// Data within the struct or enum.
David Tolnaye3d41b72017-12-31 15:24:00 -050030 pub data: Data,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 }
David Tolnayf38cdf62016-09-23 19:07:09 -070032}
33
Alex Crichtonccbb45d2017-05-23 10:58:24 -070034ast_enum_of_structs! {
David Tolnaye3d41b72017-12-31 15:24:00 -050035 /// The storage of a struct, enum or union data structure.
David Tolnay614a0142018-01-07 10:25:43 -080036 ///
David Tolnay461d98e2018-01-07 11:07:19 -080037 /// *This type is available if Syn is built with the `"derive"` feature.*
38 ///
David Tolnay614a0142018-01-07 10:25:43 -080039 /// # Syntax tree enum
40 ///
41 /// This type is a [syntax tree enum].
42 ///
43 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnaye3d41b72017-12-31 15:24:00 -050044 pub enum Data {
David Tolnayd2aa8832018-01-07 01:05:09 -080045 /// A struct input to a `proc_macro_derive` macro.
David Tolnay461d98e2018-01-07 11:07:19 -080046 ///
47 /// *This type is available if Syn is built with the `"derive"`
48 /// feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -050049 pub Struct(DataStruct {
50 pub struct_token: Token![struct],
51 pub fields: Fields,
52 pub semi_token: Option<Token![;]>,
53 }),
54
David Tolnayd2aa8832018-01-07 01:05:09 -080055 /// An enum input to a `proc_macro_derive` macro.
David Tolnay461d98e2018-01-07 11:07:19 -080056 ///
57 /// *This type is available if Syn is built with the `"derive"`
58 /// feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -050059 pub Enum(DataEnum {
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub enum_token: Token![enum],
David Tolnay32954ef2017-12-26 22:43:16 -050061 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -050062 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070064
David Tolnayd2aa8832018-01-07 01:05:09 -080065 /// A tagged union input to a `proc_macro_derive` macro.
David Tolnay461d98e2018-01-07 11:07:19 -080066 ///
67 /// *This type is available if Syn is built with the `"derive"`
68 /// feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -050069 pub Union(DataUnion {
70 pub union_token: Token![union],
71 pub fields: FieldsNamed,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070073 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070074
75 do_not_generate_to_tokens
David Tolnayf38cdf62016-09-23 19:07:09 -070076}
77
78#[cfg(feature = "parsing")]
79pub mod parsing {
80 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -070081
Michael Layzell92639a52017-06-01 00:07:44 -040082 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -070083
84 impl Synom for DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -040085 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -050086 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -040087 vis: syn!(Visibility) >>
88 which: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -080089 keyword!(struct) => { Ok }
Michael Layzell92639a52017-06-01 00:07:44 -040090 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 keyword!(enum) => { Err }
Michael Layzell92639a52017-06-01 00:07:44 -040092 ) >>
93 id: syn!(Ident) >>
94 generics: syn!(Generics) >>
95 item: switch!(value!(which),
David Tolnaye3d41b72017-12-31 15:24:00 -050096 Ok(s) => map!(data_struct, move |(wh, fields, semi)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -040097 ident: id,
98 vis: vis,
99 attrs: attrs,
100 generics: Generics {
101 where_clause: wh,
102 .. generics
103 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500104 data: Data::Struct(DataStruct {
Michael Layzell92639a52017-06-01 00:07:44 -0400105 struct_token: s,
David Tolnaye3d41b72017-12-31 15:24:00 -0500106 fields: fields,
Michael Layzell92639a52017-06-01 00:07:44 -0400107 semi_token: semi,
108 }),
109 })
110 |
David Tolnaye3d41b72017-12-31 15:24:00 -0500111 Err(e) => map!(data_enum, move |(wh, brace, variants)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400112 ident: id,
113 vis: vis,
114 attrs: attrs,
115 generics: Generics {
116 where_clause: wh,
117 .. generics
118 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500119 data: Data::Enum(DataEnum {
120 variants: variants,
Michael Layzell92639a52017-06-01 00:07:44 -0400121 brace_token: brace,
122 enum_token: e,
123 }),
124 })
125 ) >>
126 (item)
127 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700128
129 fn description() -> Option<&'static str> {
130 Some("derive input")
131 }
132 }
133
David Tolnaye3d41b72017-12-31 15:24:00 -0500134 named!(data_struct -> (Option<WhereClause>, Fields, Option<Token![;]>), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700135 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500136 wh: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500137 fields: syn!(FieldsNamed) >>
138 (wh, Fields::Named(fields), None)
Alex Crichton954046c2017-05-30 21:49:42 -0700139 )
140 |
141 do_parse!(
David Tolnaye3d41b72017-12-31 15:24:00 -0500142 fields: syn!(FieldsUnnamed) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500143 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800144 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500145 (wh, Fields::Unnamed(fields), Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700146 )
147 |
148 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500149 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800150 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500151 (wh, Fields::Unit, Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700152 )
David Tolnayf38cdf62016-09-23 19:07:09 -0700153 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700154
David Tolnayf2cfd722017-12-31 18:02:51 -0500155 named!(data_enum -> (Option<WhereClause>, token::Brace, Punctuated<Variant, Token![,]>), do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500156 wh: option!(syn!(WhereClause)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500157 data: braces!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700158 (wh, data.0, data.1)
159 ));
David Tolnayf38cdf62016-09-23 19:07:09 -0700160}
161
David Tolnayc2dfbf42016-09-23 23:52:15 -0700162#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -0700163mod printing {
164 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -0700165 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500166 use quote::{ToTokens, Tokens};
David Tolnayf38cdf62016-09-23 19:07:09 -0700167
David Tolnay0e837402016-12-22 17:25:55 -0500168 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700169 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700170 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700171 attr.to_tokens(tokens);
172 }
David Tolnay47a877c2016-10-01 16:50:55 -0700173 self.vis.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500174 match self.data {
175 Data::Struct(ref d) => d.struct_token.to_tokens(tokens),
176 Data::Enum(ref d) => d.enum_token.to_tokens(tokens),
177 Data::Union(ref d) => d.union_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700178 }
179 self.ident.to_tokens(tokens);
180 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500181 match self.data {
David Tolnay61037c62018-01-05 16:21:03 -0800182 Data::Struct(ref data) => match data.fields {
183 Fields::Named(ref fields) => {
184 self.generics.where_clause.to_tokens(tokens);
185 fields.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500186 }
David Tolnay61037c62018-01-05 16:21:03 -0800187 Fields::Unnamed(ref fields) => {
188 fields.to_tokens(tokens);
189 self.generics.where_clause.to_tokens(tokens);
190 TokensOrDefault(&data.semi_token).to_tokens(tokens);
191 }
192 Fields::Unit => {
193 self.generics.where_clause.to_tokens(tokens);
194 TokensOrDefault(&data.semi_token).to_tokens(tokens);
195 }
196 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500197 Data::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700198 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 data.brace_token.surround(tokens, |tokens| {
200 data.variants.to_tokens(tokens);
201 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700202 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500203 Data::Union(ref data) => {
204 self.generics.where_clause.to_tokens(tokens);
205 data.fields.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700206 }
207 }
208 }
209 }
210}