blob: 875d6f244c848a35418e2566488571a8ac79eb3d [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 Griffinca93a772018-01-15 13:33:17 -070010use punctuated::{Iter, IterMut, 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 Griffinca93a772018-01-15 13:33:17 -070078impl DataStruct {
79 /// Returns an iterator over the fields of this struct
80 pub fn fields(&self) -> MaybeEmpty<Iter<Field, Token![,]>> {
81 match self.fields {
82 Fields::Unit => MaybeEmpty::None,
83 Fields::Named(FieldsNamed { ref named, .. }) => MaybeEmpty::Some(named.iter()),
84 Fields::Unnamed(FieldsUnnamed { ref unnamed, .. }) => MaybeEmpty::Some(unnamed.iter()),
85 }
86 }
87
88 /// Returns an iterator over mutable references to the fields of this struct
89 pub fn fields_mut(&mut self) -> MaybeEmpty<IterMut<Field, Token![,]>> {
90 match self.fields {
91 Fields::Unit => MaybeEmpty::None,
92 Fields::Named(FieldsNamed { ref mut named, .. }) => MaybeEmpty::Some(named.iter_mut()),
93 Fields::Unnamed(FieldsUnnamed { ref mut unnamed, .. }) => MaybeEmpty::Some(unnamed.iter_mut()),
94 }
95 }
96}
97
98pub enum MaybeEmpty<T> {
99 Some(T),
100 None,
101}
102
103impl<T: Iterator> Iterator for MaybeEmpty<T> {
104 type Item = T::Item;
105
106 fn next(&mut self) -> Option<Self::Item> {
107 match *self {
108 MaybeEmpty::Some(ref mut x) => x.next(),
109 MaybeEmpty::None => None,
110 }
111 }
112}
113
David Tolnayf38cdf62016-09-23 19:07:09 -0700114#[cfg(feature = "parsing")]
115pub mod parsing {
116 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700117
Michael Layzell92639a52017-06-01 00:07:44 -0400118 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700119
120 impl Synom for DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400121 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500122 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400123 vis: syn!(Visibility) >>
124 which: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800125 keyword!(struct) => { Ok }
Michael Layzell92639a52017-06-01 00:07:44 -0400126 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800127 keyword!(enum) => { Err }
Michael Layzell92639a52017-06-01 00:07:44 -0400128 ) >>
129 id: syn!(Ident) >>
130 generics: syn!(Generics) >>
131 item: switch!(value!(which),
David Tolnaye3d41b72017-12-31 15:24:00 -0500132 Ok(s) => map!(data_struct, move |(wh, fields, semi)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400133 ident: id,
134 vis: vis,
135 attrs: attrs,
136 generics: Generics {
137 where_clause: wh,
138 .. generics
139 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500140 data: Data::Struct(DataStruct {
Michael Layzell92639a52017-06-01 00:07:44 -0400141 struct_token: s,
David Tolnaye3d41b72017-12-31 15:24:00 -0500142 fields: fields,
Michael Layzell92639a52017-06-01 00:07:44 -0400143 semi_token: semi,
144 }),
145 })
146 |
David Tolnaye3d41b72017-12-31 15:24:00 -0500147 Err(e) => map!(data_enum, move |(wh, brace, variants)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -0400148 ident: id,
149 vis: vis,
150 attrs: attrs,
151 generics: Generics {
152 where_clause: wh,
153 .. generics
154 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500155 data: Data::Enum(DataEnum {
156 variants: variants,
Michael Layzell92639a52017-06-01 00:07:44 -0400157 brace_token: brace,
158 enum_token: e,
159 }),
160 })
161 ) >>
162 (item)
163 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700164
165 fn description() -> Option<&'static str> {
166 Some("derive input")
167 }
168 }
169
David Tolnaye3d41b72017-12-31 15:24:00 -0500170 named!(data_struct -> (Option<WhereClause>, Fields, Option<Token![;]>), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700171 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500172 wh: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500173 fields: syn!(FieldsNamed) >>
174 (wh, Fields::Named(fields), None)
Alex Crichton954046c2017-05-30 21:49:42 -0700175 )
176 |
177 do_parse!(
David Tolnaye3d41b72017-12-31 15:24:00 -0500178 fields: syn!(FieldsUnnamed) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500179 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800180 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500181 (wh, Fields::Unnamed(fields), Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700182 )
183 |
184 do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500185 wh: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800186 semi: punct!(;) >>
David Tolnaye3d41b72017-12-31 15:24:00 -0500187 (wh, Fields::Unit, Some(semi))
Alex Crichton954046c2017-05-30 21:49:42 -0700188 )
David Tolnayf38cdf62016-09-23 19:07:09 -0700189 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700190
David Tolnayf2cfd722017-12-31 18:02:51 -0500191 named!(data_enum -> (Option<WhereClause>, token::Brace, Punctuated<Variant, Token![,]>), do_parse!(
David Tolnayac997dd2017-12-27 23:18:22 -0500192 wh: option!(syn!(WhereClause)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500193 data: braces!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700194 (wh, data.0, data.1)
195 ));
David Tolnayf38cdf62016-09-23 19:07:09 -0700196}
197
David Tolnayc2dfbf42016-09-23 23:52:15 -0700198#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -0700199mod printing {
200 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -0700201 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500202 use quote::{ToTokens, Tokens};
David Tolnayf38cdf62016-09-23 19:07:09 -0700203
David Tolnay0e837402016-12-22 17:25:55 -0500204 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700205 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700206 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700207 attr.to_tokens(tokens);
208 }
David Tolnay47a877c2016-10-01 16:50:55 -0700209 self.vis.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500210 match self.data {
211 Data::Struct(ref d) => d.struct_token.to_tokens(tokens),
212 Data::Enum(ref d) => d.enum_token.to_tokens(tokens),
213 Data::Union(ref d) => d.union_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700214 }
215 self.ident.to_tokens(tokens);
216 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500217 match self.data {
David Tolnay61037c62018-01-05 16:21:03 -0800218 Data::Struct(ref data) => match data.fields {
219 Fields::Named(ref fields) => {
220 self.generics.where_clause.to_tokens(tokens);
221 fields.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500222 }
David Tolnay61037c62018-01-05 16:21:03 -0800223 Fields::Unnamed(ref fields) => {
224 fields.to_tokens(tokens);
225 self.generics.where_clause.to_tokens(tokens);
226 TokensOrDefault(&data.semi_token).to_tokens(tokens);
227 }
228 Fields::Unit => {
229 self.generics.where_clause.to_tokens(tokens);
230 TokensOrDefault(&data.semi_token).to_tokens(tokens);
231 }
232 },
David Tolnaye3d41b72017-12-31 15:24:00 -0500233 Data::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700234 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700235 data.brace_token.surround(tokens, |tokens| {
236 data.variants.to_tokens(tokens);
237 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700238 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500239 Data::Union(ref data) => {
240 self.generics.where_clause.to_tokens(tokens);
241 data.fields.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700242 }
243 }
244 }
245 }
246}