blob: 1fb0e8f86238d9073be0e1fbc8a6ef1acc0d5c66 [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::*;
Sean Griffindadd0b32018-01-16 10:35:40 -070010use punctuated::{Iter, 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
Sean Griffindadd0b32018-01-16 10:35:40 -070078impl Fields {
79 /// Returns an iterator over the fields
Sean Griffinc8ef4842018-01-16 10:39:41 -070080 pub fn iter(&self) -> Iter<Field, Token![,]> {
Sean Griffindadd0b32018-01-16 10:35:40 -070081 match *self {
Sean Griffinc8ef4842018-01-16 10:39:41 -070082 Fields::Unit => Iter::empty(),
83 Fields::Named(FieldsNamed { ref named, .. }) => named.iter(),
84 Fields::Unnamed(FieldsUnnamed { ref unnamed, .. }) => unnamed.iter(),
Sean Griffinca93a772018-01-15 13:33:17 -070085 }
86 }
Sean Griffindadd0b32018-01-16 10:35:40 -070087}
Sean Griffinca93a772018-01-15 13:33:17 -070088
Sean Griffindadd0b32018-01-16 10:35:40 -070089impl<'a> IntoIterator for &'a Fields {
90 type Item = &'a Field;
Sean Griffinc8ef4842018-01-16 10:39:41 -070091 type IntoIter = Iter<'a, Field, Token![,]>;
Sean Griffindadd0b32018-01-16 10:35:40 -070092
93 fn into_iter(self) -> Self::IntoIter {
94 self.iter()
Sean Griffinca93a772018-01-15 13:33:17 -070095 }
96}
97
David Tolnayf38cdf62016-09-23 19:07:09 -070098#[cfg(feature = "parsing")]
99pub mod parsing {
100 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700101
Michael Layzell92639a52017-06-01 00:07:44 -0400102 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700103
104 impl Synom for DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400105 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500106 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400107 vis: syn!(Visibility) >>
108 which: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 keyword!(struct) => { Ok }
Michael Layzell92639a52017-06-01 00:07:44 -0400110 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 keyword!(enum) => { Err }
Michael Layzell92639a52017-06-01 00:07:44 -0400112 ) >>
113 id: syn!(Ident) >>
114 generics: syn!(Generics) >>
115 item: switch!(value!(which),
David Tolnaye3d41b72017-12-31 15:24:00 -0500116 Ok(s) => map!(data_struct, move |(wh, fields, semi)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400117 ident: id,
118 vis: vis,
119 attrs: attrs,
120 generics: Generics {
121 where_clause: wh,
122 .. generics
123 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500124 data: Data::Struct(DataStruct {
Michael Layzell92639a52017-06-01 00:07:44 -0400125 struct_token: s,
David Tolnaye3d41b72017-12-31 15:24:00 -0500126 fields: fields,
Michael Layzell92639a52017-06-01 00:07:44 -0400127 semi_token: semi,
128 }),
129 })
130 |
David Tolnaye3d41b72017-12-31 15:24:00 -0500131 Err(e) => map!(data_enum, move |(wh, brace, variants)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400132 ident: id,
133 vis: vis,
134 attrs: attrs,
135 generics: Generics {
136 where_clause: wh,
137 .. generics
138 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500139 data: Data::Enum(DataEnum {
140 variants: variants,
Michael Layzell92639a52017-06-01 00:07:44 -0400141 brace_token: brace,
142 enum_token: e,
143 }),
144 })
145 ) >>
146 (item)
147 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700148
149 fn description() -> Option<&'static str> {
150 Some("derive input")
151 }
152 }
153
David Tolnaye3d41b72017-12-31 15:24:00 -0500154 named!(data_struct -> (Option<WhereClause>, Fields, Option<Token![;]>), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700155 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500156 wh: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500157 fields: syn!(FieldsNamed) >>
158 (wh, Fields::Named(fields), None)
Alex Crichton954046c2017-05-30 21:49:42 -0700159 )
160 |
161 do_parse!(
David Tolnaye3d41b72017-12-31 15:24:00 -0500162 fields: syn!(FieldsUnnamed) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500163 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800164 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500165 (wh, Fields::Unnamed(fields), Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700166 )
167 |
168 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500169 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800170 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500171 (wh, Fields::Unit, Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700172 )
David Tolnayf38cdf62016-09-23 19:07:09 -0700173 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700174
David Tolnayf2cfd722017-12-31 18:02:51 -0500175 named!(data_enum -> (Option<WhereClause>, token::Brace, Punctuated<Variant, Token![,]>), do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500176 wh: option!(syn!(WhereClause)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500177 data: braces!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700178 (wh, data.0, data.1)
179 ));
David Tolnayf38cdf62016-09-23 19:07:09 -0700180}
181
David Tolnayc2dfbf42016-09-23 23:52:15 -0700182#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -0700183mod printing {
184 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -0700185 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500186 use quote::{ToTokens, Tokens};
David Tolnayf38cdf62016-09-23 19:07:09 -0700187
David Tolnay0e837402016-12-22 17:25:55 -0500188 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700189 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700190 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700191 attr.to_tokens(tokens);
192 }
David Tolnay47a877c2016-10-01 16:50:55 -0700193 self.vis.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500194 match self.data {
195 Data::Struct(ref d) => d.struct_token.to_tokens(tokens),
196 Data::Enum(ref d) => d.enum_token.to_tokens(tokens),
197 Data::Union(ref d) => d.union_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700198 }
199 self.ident.to_tokens(tokens);
200 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500201 match self.data {
David Tolnay61037c62018-01-05 16:21:03 -0800202 Data::Struct(ref data) => match data.fields {
203 Fields::Named(ref fields) => {
204 self.generics.where_clause.to_tokens(tokens);
205 fields.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500206 }
David Tolnay61037c62018-01-05 16:21:03 -0800207 Fields::Unnamed(ref fields) => {
208 fields.to_tokens(tokens);
209 self.generics.where_clause.to_tokens(tokens);
210 TokensOrDefault(&data.semi_token).to_tokens(tokens);
211 }
212 Fields::Unit => {
213 self.generics.where_clause.to_tokens(tokens);
214 TokensOrDefault(&data.semi_token).to_tokens(tokens);
215 }
216 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500217 Data::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700218 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700219 data.brace_token.surround(tokens, |tokens| {
220 data.variants.to_tokens(tokens);
221 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700222 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500223 Data::Union(ref data) => {
224 self.generics.where_clause.to_tokens(tokens);
225 data.fields.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700226 }
227 }
228 }
229 }
230}