blob: bb61e6c5a4f9152f553c7167deaca74b12bdc39d [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 Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnayb79ee962016-09-04 09:39:20 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_struct! {
David Tolnayed906d12018-01-07 01:20:29 -080013 /// Lifetimes and type parameters attached to a declaration of a function,
14 /// enum, trait, etc.
David Tolnay461d98e2018-01-07 11:07:19 -080015 ///
16 /// *This type is available if Syn is built with the `"derive"` or `"full"`
17 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070018 #[derive(Default)]
19 pub struct Generics {
David Tolnayf8db7ba2017-11-11 22:52:16 -080020 pub lt_token: Option<Token![<]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050021 pub params: Punctuated<GenericParam, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 pub gt_token: Option<Token![>]>,
David Tolnayac997dd2017-12-27 23:18:22 -050023 pub where_clause: Option<WhereClause>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }
David Tolnayb79ee962016-09-04 09:39:20 -070025}
26
David Tolnayc2f1aba2017-11-12 20:29:22 -080027ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -080028 /// A generic type parameter, lifetime, or const generic: `T: Into<String>`,
29 /// `'a: 'b`, `const LEN: usize`.
David Tolnay614a0142018-01-07 10:25:43 -080030 ///
David Tolnay461d98e2018-01-07 11:07:19 -080031 /// *This type is available if Syn is built with the `"derive"` or `"full"`
32 /// feature.*
33 ///
David Tolnay614a0142018-01-07 10:25:43 -080034 /// # Syntax tree enum
35 ///
36 /// This type is a [syntax tree enum].
37 ///
38 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc2f1aba2017-11-12 20:29:22 -080039 pub enum GenericParam {
David Tolnayed906d12018-01-07 01:20:29 -080040 /// A generic type parameter: `T: Into<String>`.
David Tolnay461d98e2018-01-07 11:07:19 -080041 ///
42 /// *This type is available if Syn is built with the `"derive"` or
43 /// `"full"` feature.*
David Tolnayc2f1aba2017-11-12 20:29:22 -080044 pub Type(TypeParam {
45 pub attrs: Vec<Attribute>,
46 pub ident: Ident,
47 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050048 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayc2f1aba2017-11-12 20:29:22 -080049 pub eq_token: Option<Token![=]>,
50 pub default: Option<Type>,
51 }),
David Tolnayed906d12018-01-07 01:20:29 -080052
53 /// A lifetime definition: `'a: 'b + 'c + 'd`.
David Tolnay461d98e2018-01-07 11:07:19 -080054 ///
55 /// *This type is available if Syn is built with the `"derive"` or
56 /// `"full"` feature.*
David Tolnay517f3692018-01-01 20:17:23 -080057 pub Lifetime(LifetimeDef {
58 pub attrs: Vec<Attribute>,
59 pub lifetime: Lifetime,
60 pub colon_token: Option<Token![:]>,
61 pub bounds: Punctuated<Lifetime, Token![+]>,
62 }),
David Tolnayed906d12018-01-07 01:20:29 -080063
64 /// A const generic parameter: `const LENGTH: usize`.
David Tolnay461d98e2018-01-07 11:07:19 -080065 ///
66 /// *This type is available if Syn is built with the `"derive"` or
67 /// `"full"` feature.*
Nika Layzellf1fdc0b2017-12-04 19:58:32 -050068 pub Const(ConstParam {
69 pub attrs: Vec<Attribute>,
70 pub const_token: Token![const],
71 pub ident: Ident,
72 pub colon_token: Token![:],
73 pub ty: Type,
74 pub eq_token: Option<Token![=]>,
75 pub default: Option<Expr>,
76 }),
David Tolnayc2f1aba2017-11-12 20:29:22 -080077 }
78}
79
David Tolnay461d98e2018-01-07 11:07:19 -080080/// Returned by `Generics::split_for_impl`.
81///
82/// *This type is available if Syn is built with the `"derive"` or `"full"`
83/// feature and the `"printing"` feature.*
David Tolnaye7678922016-10-13 20:44:03 -070084#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040085#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
86#[cfg_attr(feature = "clone-impls", derive(Clone))]
Nika Layzell6b38b132017-10-24 23:09:39 -040087pub struct ImplGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070088
David Tolnay461d98e2018-01-07 11:07:19 -080089/// Returned by `Generics::split_for_impl`.
90///
91/// *This type is available if Syn is built with the `"derive"` or `"full"`
92/// feature and the `"printing"` feature.*
David Tolnaye7678922016-10-13 20:44:03 -070093#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040094#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
95#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080096pub struct TypeGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070097
David Tolnay461d98e2018-01-07 11:07:19 -080098/// Returned by `TypeGenerics::as_turbofish`.
99///
100/// *This type is available if Syn is built with the `"derive"` or `"full"`
101/// feature and the `"printing"` feature.*
David Tolnayc879a502017-01-25 15:51:32 -0800102#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400103#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
104#[cfg_attr(feature = "clone-impls", derive(Clone))]
Nika Layzell6b38b132017-10-24 23:09:39 -0400105pub struct Turbofish<'a>(&'a Generics);
David Tolnayc879a502017-01-25 15:51:32 -0800106
David Tolnaye95cc9f2017-01-25 15:57:09 -0800107#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -0700108impl Generics {
109 /// Split a type's generics into the pieces required for impl'ing a trait
110 /// for that type.
111 ///
112 /// ```
113 /// # extern crate syn;
114 /// # #[macro_use]
115 /// # extern crate quote;
116 /// # fn main() {
117 /// # let generics: syn::Generics = Default::default();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 /// # let name = syn::Ident::from("MyType");
David Tolnayb153dbc2016-10-04 23:39:10 -0700119 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
120 /// quote! {
121 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
122 /// // ...
123 /// }
124 /// }
125 /// # ;
126 /// # }
127 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800128 ///
129 /// *This method is available if Syn is built with the `"derive"` or
130 /// `"full"` feature and the `"printing"` feature.*
David Tolnayac997dd2017-12-27 23:18:22 -0500131 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
David Tolnay61037c62018-01-05 16:21:03 -0800132 (
133 ImplGenerics(self),
134 TypeGenerics(self),
135 self.where_clause.as_ref(),
136 )
David Tolnayb153dbc2016-10-04 23:39:10 -0700137 }
138}
139
David Tolnaye95cc9f2017-01-25 15:57:09 -0800140#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800141impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -0800142 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800143 ///
144 /// *This method is available if Syn is built with the `"derive"` or
145 /// `"full"` feature and the `"printing"` feature.*
David Tolnayc879a502017-01-25 15:51:32 -0800146 pub fn as_turbofish(&self) -> Turbofish {
147 Turbofish(self.0)
148 }
149}
150
Alex Crichton62a0a592017-05-22 13:58:53 -0700151ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800152 /// A set of bound lifetimes: `for<'a, 'b, 'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800153 ///
154 /// *This type is available if Syn is built with the `"derive"` or `"full"`
155 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 #[derive(Default)]
157 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub for_token: Token![for],
159 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500160 pub lifetimes: Punctuated<LifetimeDef, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800161 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700162 }
163}
164
David Tolnayf9505b52016-10-02 09:18:52 -0700165impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700166 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700167 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700168 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700169 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700170 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500171 bounds: Punctuated::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700172 }
173 }
174}
175
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800176impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700177 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800178 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700179 attrs: vec![],
180 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700181 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500182 bounds: Punctuated::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700183 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700184 default: None,
185 }
186 }
187}
188
David Tolnay40fb8ce2018-01-02 10:53:46 -0800189ast_enum_of_structs! {
David Tolnayed906d12018-01-07 01:20:29 -0800190 /// A trait or lifetime used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800191 ///
192 /// *This type is available if Syn is built with the `"derive"` or `"full"`
193 /// feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800194 pub enum TypeParamBound {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800195 pub Trait(TraitBound),
196 pub Lifetime(Lifetime),
197 }
198}
199
200ast_struct! {
David Tolnayed906d12018-01-07 01:20:29 -0800201 /// A trait used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800202 ///
203 /// *This type is available if Syn is built with the `"derive"` or `"full"`
204 /// feature.*
David Tolnay40fb8ce2018-01-02 10:53:46 -0800205 pub struct TraitBound {
206 pub modifier: TraitBoundModifier,
207 /// The `for<'a>` in `for<'a> Foo<&'a T>`
208 pub lifetimes: Option<BoundLifetimes>,
209 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
210 pub path: Path,
Alex Crichton62a0a592017-05-22 13:58:53 -0700211 }
David Tolnay55337722016-09-11 12:58:56 -0700212}
213
Alex Crichton62a0a592017-05-22 13:58:53 -0700214ast_enum! {
David Tolnayed906d12018-01-07 01:20:29 -0800215 /// A modifier on a trait bound, currently only used for the `?` in
216 /// `?Sized`.
David Tolnay461d98e2018-01-07 11:07:19 -0800217 ///
218 /// *This type is available if Syn is built with the `"derive"` or `"full"`
219 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700220 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 pub enum TraitBoundModifier {
222 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800223 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 }
David Tolnay55337722016-09-11 12:58:56 -0700225}
226
Alex Crichton62a0a592017-05-22 13:58:53 -0700227ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800228 /// A `where` clause in a definition: `where T: Deserialize<'de>, D:
229 /// 'static`.
David Tolnay461d98e2018-01-07 11:07:19 -0800230 ///
231 /// *This type is available if Syn is built with the `"derive"` or `"full"`
232 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700233 pub struct WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500234 pub where_token: Token![where],
David Tolnayf2cfd722017-12-31 18:02:51 -0500235 pub predicates: Punctuated<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 }
David Tolnayb79ee962016-09-04 09:39:20 -0700237}
238
Alex Crichton62a0a592017-05-22 13:58:53 -0700239ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800240 /// A single predicate in a `where` clause: `T: Deserialize<'de>`.
David Tolnay614a0142018-01-07 10:25:43 -0800241 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800242 /// *This type is available if Syn is built with the `"derive"` or `"full"`
243 /// feature.*
244 ///
David Tolnay614a0142018-01-07 10:25:43 -0800245 /// # Syntax tree enum
246 ///
247 /// This type is a [syntax tree enum].
248 ///
249 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 pub enum WherePredicate {
David Tolnayed906d12018-01-07 01:20:29 -0800251 /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800252 ///
253 /// *This type is available if Syn is built with the `"derive"` or
254 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800255 pub Type(PredicateType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 /// Any lifetimes from a `for` binding
David Tolnay40fb8ce2018-01-02 10:53:46 -0800257 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800259 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800260 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayf2cfd722017-12-31 18:02:51 -0500262 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700263 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700264
David Tolnayed906d12018-01-07 01:20:29 -0800265 /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`.
David Tolnay461d98e2018-01-07 11:07:19 -0800266 ///
267 /// *This type is available if Syn is built with the `"derive"` or
268 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800269 pub Lifetime(PredicateLifetime {
Alex Crichton62a0a592017-05-22 13:58:53 -0700270 pub lifetime: Lifetime,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800271 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500272 pub bounds: Punctuated<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700274
David Tolnayed906d12018-01-07 01:20:29 -0800275 /// An equality predicate in a `where` clause (unsupported).
David Tolnay461d98e2018-01-07 11:07:19 -0800276 ///
277 /// *This type is available if Syn is built with the `"derive"` or
278 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800279 pub Eq(PredicateEq {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800280 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800281 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800282 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700283 }),
284 }
David Tolnayf8e08832017-01-23 00:04:32 -0800285}
286
David Tolnay86eca752016-09-04 11:26:41 -0700287#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700288pub mod parsing {
289 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700290
David Tolnay63e3dee2017-06-03 20:13:17 -0700291 use synom::Synom;
David Tolnay56080682018-01-06 14:01:52 -0800292 use punctuated::Pair;
Alex Crichton954046c2017-05-30 21:49:42 -0700293
294 impl Synom for Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400295 named!(parse -> Self, map!(
296 alt!(
297 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800298 lt: punct!(<) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500299 lifetimes: call!(Punctuated::<LifetimeDef, Token![,]>::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400300 ty_params: cond!(
David Tolnayc2f1aba2017-11-12 20:29:22 -0800301 lifetimes.empty_or_trailing(),
David Tolnayf2cfd722017-12-31 18:02:51 -0500302 Punctuated::<TypeParam, Token![,]>::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -0400303 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800304 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400305 (lifetimes, ty_params, Some(lt), Some(gt))
306 )
307 |
David Tolnayf2cfd722017-12-31 18:02:51 -0500308 epsilon!() => { |_| (Punctuated::new(), None, None, None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400309 ),
David Tolnaybc7d7d92017-06-03 20:54:05 -0700310 |(lifetimes, ty_params, lt, gt)| Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400311 lt_token: lt,
David Tolnay56080682018-01-06 14:01:52 -0800312 params: lifetimes.into_pairs()
313 .map(Pair::into_tuple)
314 .map(|(life, comma)| Pair::new(GenericParam::Lifetime(life), comma))
David Tolnayc2f1aba2017-11-12 20:29:22 -0800315 .chain(ty_params.unwrap_or_default()
David Tolnay56080682018-01-06 14:01:52 -0800316 .into_pairs()
317 .map(Pair::into_tuple)
318 .map(|(ty, comma)| Pair::new(GenericParam::Type(ty), comma)))
David Tolnay660fd1f2017-12-31 01:52:57 -0500319 .collect(),
David Tolnayc2f1aba2017-11-12 20:29:22 -0800320 gt_token: gt,
David Tolnayac997dd2017-12-27 23:18:22 -0500321 where_clause: None,
Michael Layzell416724e2017-05-24 21:12:34 -0400322 }
Michael Layzell92639a52017-06-01 00:07:44 -0400323 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800324
325 fn description() -> Option<&'static str> {
326 Some("generic parameters in declaration")
327 }
Alex Crichton954046c2017-05-30 21:49:42 -0700328 }
329
Alex Crichton954046c2017-05-30 21:49:42 -0700330 impl Synom for LifetimeDef {
Michael Layzell92639a52017-06-01 00:07:44 -0400331 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500332 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400333 life: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800334 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400335 bounds: cond!(
336 colon.is_some(),
David Tolnayf2cfd722017-12-31 18:02:51 -0500337 Punctuated::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400338 ) >>
339 (LifetimeDef {
340 attrs: attrs,
341 lifetime: life,
342 bounds: bounds.unwrap_or_default(),
David Tolnay6af8f1d2017-12-27 23:08:43 -0500343 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400344 })
345 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800346
347 fn description() -> Option<&'static str> {
348 Some("lifetime definition")
349 }
Alex Crichton954046c2017-05-30 21:49:42 -0700350 }
351
352 impl Synom for BoundLifetimes {
Michael Layzell92639a52017-06-01 00:07:44 -0400353 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800354 for_: keyword!(for) >>
355 lt: punct!(<) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500356 lifetimes: call!(Punctuated::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800357 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400358 (BoundLifetimes {
359 for_token: for_,
360 lt_token: lt,
361 gt_token: gt,
362 lifetimes: lifetimes,
363 })
364 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800365
366 fn description() -> Option<&'static str> {
367 Some("bound lifetimes")
368 }
Alex Crichton954046c2017-05-30 21:49:42 -0700369 }
David Tolnay55337722016-09-11 12:58:56 -0700370
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800371 impl Synom for TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400372 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500373 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400374 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400376 bounds: cond!(
377 colon.is_some(),
David Tolnayf2cfd722017-12-31 18:02:51 -0500378 Punctuated::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400379 ) >>
380 default: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800381 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800382 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400383 (eq, ty)
384 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800385 (TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400386 attrs: attrs,
387 ident: id,
388 bounds: bounds.unwrap_or_default(),
389 colon_token: colon,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800390 eq_token: default.as_ref().map(|d| Token![=]((d.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400391 default: default.map(|d| d.1),
392 })
393 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800394
395 fn description() -> Option<&'static str> {
396 Some("type parameter")
397 }
Alex Crichton954046c2017-05-30 21:49:42 -0700398 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700399
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800400 impl Synom for TypeParamBound {
Michael Layzell92639a52017-06-01 00:07:44 -0400401 named!(parse -> Self, alt!(
David Tolnay40fb8ce2018-01-02 10:53:46 -0800402 syn!(Lifetime) => { TypeParamBound::Lifetime }
Michael Layzell92639a52017-06-01 00:07:44 -0400403 |
David Tolnay40fb8ce2018-01-02 10:53:46 -0800404 syn!(TraitBound) => { TypeParamBound::Trait }
Michael Layzell92639a52017-06-01 00:07:44 -0400405 |
David Tolnay40fb8ce2018-01-02 10:53:46 -0800406 parens!(syn!(TraitBound)) => { |bound| TypeParamBound::Trait(bound.1) }
Michael Layzell92639a52017-06-01 00:07:44 -0400407 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700408
409 fn description() -> Option<&'static str> {
Nika Layzellc691cb42017-12-04 13:44:38 -0500410 Some("type parameter bound")
Alex Crichton954046c2017-05-30 21:49:42 -0700411 }
412 }
413
David Tolnay40fb8ce2018-01-02 10:53:46 -0800414 impl Synom for TraitBound {
415 named!(parse -> Self, do_parse!(
416 modifier: syn!(TraitBoundModifier) >>
417 lifetimes: option!(syn!(BoundLifetimes)) >>
418 mut path: syn!(Path) >>
419 parenthesized: option!(cond_reduce!(
David Tolnay56080682018-01-06 14:01:52 -0800420 path.segments.last().unwrap().value().arguments.is_empty(),
David Tolnay40fb8ce2018-01-02 10:53:46 -0800421 syn!(ParenthesizedGenericArguments)
422 )) >>
423 ({
424 if let Some(parenthesized) = parenthesized {
425 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay56080682018-01-06 14:01:52 -0800426 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
David Tolnay40fb8ce2018-01-02 10:53:46 -0800427 }
428 TraitBound {
429 modifier: modifier,
430 lifetimes: lifetimes,
431 path: path,
432 }
433 })
434 ));
435
436 fn description() -> Option<&'static str> {
437 Some("trait bound")
438 }
439 }
440
441 impl Synom for TraitBoundModifier {
442 named!(parse -> Self, alt!(
443 punct!(?) => { TraitBoundModifier::Maybe }
444 |
445 epsilon!() => { |_| TraitBoundModifier::None }
446 ));
447
448 fn description() -> Option<&'static str> {
449 Some("trait bound modifier")
450 }
451 }
452
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500453 impl Synom for ConstParam {
454 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500455 attrs: many0!(Attribute::parse_outer) >>
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500456 const_: keyword!(const) >>
457 ident: syn!(Ident) >>
458 colon: punct!(:) >>
459 ty: syn!(Type) >>
460 eq_def: option!(tuple!(punct!(=), syn!(Expr))) >>
David Tolnay78ee5202017-12-04 22:17:54 -0800461 ({
462 let (eq_token, default) = match eq_def {
463 Some((eq_token, default)) => (Some(eq_token), Some(default)),
464 None => (None, None),
465 };
466 ConstParam {
467 attrs: attrs,
468 const_token: const_,
469 ident: ident,
470 colon_token: colon,
471 ty: ty,
472 eq_token: eq_token,
473 default: default,
474 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500475 })
476 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800477
478 fn description() -> Option<&'static str> {
479 Some("generic `const` parameter")
480 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500481 }
482
Alex Crichton954046c2017-05-30 21:49:42 -0700483 impl Synom for WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500484 named!(parse -> Self, do_parse!(
485 where_: keyword!(where) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500486 predicates: call!(Punctuated::parse_terminated) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500487 (WhereClause {
488 predicates: predicates,
489 where_token: where_,
490 })
Michael Layzell92639a52017-06-01 00:07:44 -0400491 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700492
493 fn description() -> Option<&'static str> {
494 Some("where clause")
495 }
496 }
497
498 impl Synom for WherePredicate {
Michael Layzell92639a52017-06-01 00:07:44 -0400499 named!(parse -> Self, alt!(
500 do_parse!(
501 ident: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400503 bounds: cond!(
504 colon.is_some(),
David Tolnayf2cfd722017-12-31 18:02:51 -0500505 Punctuated::parse_separated
Michael Layzell92639a52017-06-01 00:07:44 -0400506 ) >>
David Tolnayd4add852018-01-01 20:13:24 -0800507 (WherePredicate::Lifetime(PredicateLifetime {
Michael Layzell92639a52017-06-01 00:07:44 -0400508 lifetime: ident,
509 bounds: bounds.unwrap_or_default(),
510 colon_token: colon,
511 }))
512 )
513 |
514 do_parse!(
David Tolnay40fb8ce2018-01-02 10:53:46 -0800515 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800516 bounded_ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 colon: punct!(:) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500518 bounds: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayd4add852018-01-01 20:13:24 -0800519 (WherePredicate::Type(PredicateType {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800520 lifetimes: lifetimes,
Michael Layzell92639a52017-06-01 00:07:44 -0400521 bounded_ty: bounded_ty,
522 bounds: bounds,
523 colon_token: colon,
524 }))
525 )
526 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800527
528 fn description() -> Option<&'static str> {
529 Some("predicate in where clause")
530 }
Alex Crichton954046c2017-05-30 21:49:42 -0700531 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700532}
David Tolnay87d0b442016-09-04 11:52:12 -0700533
534#[cfg(feature = "printing")]
535mod printing {
536 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700537 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500538 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700539
Michael Layzell3936ceb2017-07-08 00:28:36 -0400540 /// Returns true if the generics object has no lifetimes or ty_params.
541 fn empty_normal_generics(generics: &Generics) -> bool {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800542 generics.params.is_empty()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400543 }
544
David Tolnay8ef93042016-09-04 14:08:40 -0700545 impl ToTokens for Generics {
546 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400547 if empty_normal_generics(self) {
548 return;
549 }
550
Alex Crichton259ee532017-07-14 06:51:02 -0700551 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800552 self.params.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700553 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700554 }
555 }
556
David Tolnaye7678922016-10-13 20:44:03 -0700557 impl<'a> ToTokens for ImplGenerics<'a> {
558 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500559 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400560 return;
561 }
562
Alex Crichton259ee532017-07-14 06:51:02 -0700563 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -0800564 for param in self.0.params.pairs() {
565 match **param.value() {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800566 GenericParam::Lifetime(ref param) => {
567 param.to_tokens(tokens);
568 }
569 GenericParam::Type(ref param) => {
570 // Leave off the type parameter defaults
571 tokens.append_all(param.attrs.outer());
572 param.ident.to_tokens(tokens);
573 if !param.bounds.is_empty() {
574 TokensOrDefault(&param.colon_token).to_tokens(tokens);
575 param.bounds.to_tokens(tokens);
576 }
577 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500578 GenericParam::Const(ref param) => {
579 // Leave off the const parameter defaults
580 tokens.append_all(param.attrs.outer());
581 param.const_token.to_tokens(tokens);
582 param.ident.to_tokens(tokens);
583 param.colon_token.to_tokens(tokens);
584 param.ty.to_tokens(tokens);
585 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400586 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500587 param.punct().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700588 }
Alex Crichton259ee532017-07-14 06:51:02 -0700589 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700590 }
591 }
592
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800593 impl<'a> ToTokens for TypeGenerics<'a> {
David Tolnaye7678922016-10-13 20:44:03 -0700594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500595 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400596 return;
597 }
598
Alex Crichton259ee532017-07-14 06:51:02 -0700599 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -0800600 for param in self.0.params.pairs() {
601 match **param.value() {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800602 GenericParam::Lifetime(ref param) => {
603 // Leave off the lifetime bounds and attributes
604 param.lifetime.to_tokens(tokens);
605 }
606 GenericParam::Type(ref param) => {
607 // Leave off the type parameter defaults
608 param.ident.to_tokens(tokens);
609 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500610 GenericParam::Const(ref param) => {
611 // Leave off the const parameter defaults
612 param.ident.to_tokens(tokens);
613 }
David Tolnayc2f1aba2017-11-12 20:29:22 -0800614 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500615 param.punct().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700616 }
Alex Crichton259ee532017-07-14 06:51:02 -0700617 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700618 }
619 }
620
David Tolnayc879a502017-01-25 15:51:32 -0800621 impl<'a> ToTokens for Turbofish<'a> {
622 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500623 if !empty_normal_generics(self.0) {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800624 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800625 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800626 }
627 }
628 }
629
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700630 impl ToTokens for BoundLifetimes {
631 fn to_tokens(&self, tokens: &mut Tokens) {
632 self.for_token.to_tokens(tokens);
633 self.lt_token.to_tokens(tokens);
634 self.lifetimes.to_tokens(tokens);
635 self.gt_token.to_tokens(tokens);
636 }
637 }
638
David Tolnay87d0b442016-09-04 11:52:12 -0700639 impl ToTokens for LifetimeDef {
640 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700641 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700642 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400643 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700644 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400645 self.bounds.to_tokens(tokens);
646 }
David Tolnay87d0b442016-09-04 11:52:12 -0700647 }
648 }
649
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800650 impl ToTokens for TypeParam {
David Tolnay8ef93042016-09-04 14:08:40 -0700651 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700652 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700653 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400654 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700655 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400656 self.bounds.to_tokens(tokens);
657 }
658 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -0700659 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400660 self.default.to_tokens(tokens);
661 }
David Tolnay8ef93042016-09-04 14:08:40 -0700662 }
663 }
664
David Tolnay40fb8ce2018-01-02 10:53:46 -0800665 impl ToTokens for TraitBound {
David Tolnay87d0b442016-09-04 11:52:12 -0700666 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800667 self.modifier.to_tokens(tokens);
668 self.lifetimes.to_tokens(tokens);
669 self.path.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700670 }
671 }
672
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700673 impl ToTokens for TraitBoundModifier {
674 fn to_tokens(&self, tokens: &mut Tokens) {
675 match *self {
676 TraitBoundModifier::None => {}
677 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
678 }
679 }
680 }
681
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500682 impl ToTokens for ConstParam {
683 fn to_tokens(&self, tokens: &mut Tokens) {
684 tokens.append_all(self.attrs.outer());
685 self.const_token.to_tokens(tokens);
686 self.ident.to_tokens(tokens);
687 self.colon_token.to_tokens(tokens);
688 self.ty.to_tokens(tokens);
689 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -0800690 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500691 self.default.to_tokens(tokens);
692 }
693 }
694 }
695
David Tolnay55337722016-09-11 12:58:56 -0700696 impl ToTokens for WhereClause {
697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayac997dd2017-12-27 23:18:22 -0500698 self.where_token.to_tokens(tokens);
699 self.predicates.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700700 }
701 }
David Tolnay8ef93042016-09-04 14:08:40 -0700702
David Tolnayd4add852018-01-01 20:13:24 -0800703 impl ToTokens for PredicateType {
David Tolnay8ef93042016-09-04 14:08:40 -0700704 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800705 self.lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700706 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700707 self.colon_token.to_tokens(tokens);
708 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700709 }
710 }
711
David Tolnayd4add852018-01-01 20:13:24 -0800712 impl ToTokens for PredicateLifetime {
David Tolnay8ef93042016-09-04 14:08:40 -0700713 fn to_tokens(&self, tokens: &mut Tokens) {
714 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400715 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700716 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400717 self.bounds.to_tokens(tokens);
718 }
David Tolnay8ef93042016-09-04 14:08:40 -0700719 }
720 }
David Tolnayf8e08832017-01-23 00:04:32 -0800721
David Tolnayd4add852018-01-01 20:13:24 -0800722 impl ToTokens for PredicateEq {
David Tolnayf8e08832017-01-23 00:04:32 -0800723 fn to_tokens(&self, tokens: &mut Tokens) {
724 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700725 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800726 self.rhs_ty.to_tokens(tokens);
727 }
728 }
David Tolnay87d0b442016-09-04 11:52:12 -0700729}