blob: 9caf7509f9a34f6296eb234c8ad1ca8fd0c0ed22 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// Represents lifetimes and type parameters attached to a declaration
6 /// of a function, enum, trait, etc.
7 #[derive(Default)]
8 pub struct Generics {
David Tolnayf8db7ba2017-11-11 22:52:16 -08009 pub lt_token: Option<Token![<]>,
David Tolnayc2f1aba2017-11-12 20:29:22 -080010 pub params: Delimited<GenericParam, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080011 pub gt_token: Option<Token![>]>,
David Tolnayac997dd2017-12-27 23:18:22 -050012 pub where_clause: Option<WhereClause>,
Alex Crichton62a0a592017-05-22 13:58:53 -070013 }
David Tolnayb79ee962016-09-04 09:39:20 -070014}
15
David Tolnayc2f1aba2017-11-12 20:29:22 -080016ast_enum_of_structs! {
17 pub enum GenericParam {
18 /// A lifetime definition, e.g. `'a: 'b+'c+'d`
19 pub Lifetime(LifetimeDef {
20 pub attrs: Vec<Attribute>,
21 pub lifetime: Lifetime,
22 pub colon_token: Option<Token![:]>,
23 pub bounds: Delimited<Lifetime, Token![+]>,
24 }),
25 /// A generic type parameter, e.g. `T: Into<String>`.
26 pub Type(TypeParam {
27 pub attrs: Vec<Attribute>,
28 pub ident: Ident,
29 pub colon_token: Option<Token![:]>,
30 pub bounds: Delimited<TypeParamBound, Token![+]>,
31 pub eq_token: Option<Token![=]>,
32 pub default: Option<Type>,
33 }),
Nika Layzellf1fdc0b2017-12-04 19:58:32 -050034 /// A generic const parameter, e.g. `const LENGTH: usize`.
35 pub Const(ConstParam {
36 pub attrs: Vec<Attribute>,
37 pub const_token: Token![const],
38 pub ident: Ident,
39 pub colon_token: Token![:],
40 pub ty: Type,
41 pub eq_token: Option<Token![=]>,
42 pub default: Option<Expr>,
43 }),
David Tolnayc2f1aba2017-11-12 20:29:22 -080044 }
45}
46
David Tolnaye7678922016-10-13 20:44:03 -070047#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040048#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
49#[cfg_attr(feature = "clone-impls", derive(Clone))]
50/// Returned by `Generics::split_for_impl`.
51pub struct ImplGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070052
53#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040054#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
55#[cfg_attr(feature = "clone-impls", derive(Clone))]
56/// Returned by `Generics::split_for_impl`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080057pub struct TypeGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070058
David Tolnayc879a502017-01-25 15:51:32 -080059#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040060#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
61#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062/// Returned by `TypeGenerics::as_turbofish`.
Nika Layzell6b38b132017-10-24 23:09:39 -040063pub struct Turbofish<'a>(&'a Generics);
David Tolnayc879a502017-01-25 15:51:32 -080064
David Tolnaye95cc9f2017-01-25 15:57:09 -080065#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -070066impl Generics {
67 /// Split a type's generics into the pieces required for impl'ing a trait
68 /// for that type.
69 ///
70 /// ```
71 /// # extern crate syn;
72 /// # #[macro_use]
73 /// # extern crate quote;
74 /// # fn main() {
75 /// # let generics: syn::Generics = Default::default();
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 /// # let name = syn::Ident::from("MyType");
David Tolnayb153dbc2016-10-04 23:39:10 -070077 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
78 /// quote! {
79 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
80 /// // ...
81 /// }
82 /// }
83 /// # ;
84 /// # }
85 /// ```
David Tolnayac997dd2017-12-27 23:18:22 -050086 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
87 (ImplGenerics(self), TypeGenerics(self), self.where_clause.as_ref())
David Tolnayb153dbc2016-10-04 23:39:10 -070088 }
89}
90
David Tolnaye95cc9f2017-01-25 15:57:09 -080091#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -080093 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
94 pub fn as_turbofish(&self) -> Turbofish {
95 Turbofish(self.0)
96 }
97}
98
Alex Crichton62a0a592017-05-22 13:58:53 -070099ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 /// A set of bound lifetimes, e.g. `for<'a, 'b, 'c>`
101 #[derive(Default)]
102 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800103 pub for_token: Token![for],
104 pub lt_token: Token![<],
105 pub lifetimes: Delimited<LifetimeDef, Token![,]>,
106 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107 }
108}
109
David Tolnayf9505b52016-10-02 09:18:52 -0700110impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700111 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700112 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700113 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700114 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 colon_token: None,
116 bounds: Delimited::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700117 }
118 }
119}
120
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800121impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700122 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800123 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700124 attrs: vec![],
125 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 colon_token: None,
127 bounds: Delimited::new(),
128 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700129 default: None,
130 }
131 }
132}
133
Alex Crichton62a0a592017-05-22 13:58:53 -0700134ast_enum! {
135 /// The AST represents all type param bounds as types.
136 /// `typeck::collect::compute_bounds` matches these against
137 /// the "special" built-in traits (see `middle::lang_items`) and
138 /// detects Copy, Send and Sync.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub enum TypeParamBound {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 Trait(PolyTraitRef, TraitBoundModifier),
141 Region(Lifetime),
142 }
David Tolnay55337722016-09-11 12:58:56 -0700143}
144
Alex Crichton62a0a592017-05-22 13:58:53 -0700145ast_enum! {
146 /// A modifier on a bound, currently this is only used for `?Sized`, where the
147 /// modifier is `Maybe`. Negative bounds should also be handled here.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700148 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 pub enum TraitBoundModifier {
150 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800151 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 }
David Tolnay55337722016-09-11 12:58:56 -0700153}
154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155ast_struct! {
156 /// A `where` clause in a definition
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub struct WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500158 pub where_token: Token![where],
David Tolnayf8db7ba2017-11-11 22:52:16 -0800159 pub predicates: Delimited<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 }
David Tolnayb79ee962016-09-04 09:39:20 -0700161}
162
Alex Crichton62a0a592017-05-22 13:58:53 -0700163ast_enum_of_structs! {
164 /// A single predicate in a `where` clause
165 pub enum WherePredicate {
166 /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
167 pub BoundPredicate(WhereBoundPredicate {
168 /// Any lifetimes from a `for` binding
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700169 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800171 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800172 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800174 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700176
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// A lifetime predicate, e.g. `'a: 'b+'c`
178 pub RegionPredicate(WhereRegionPredicate {
179 pub lifetime: Lifetime,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800180 pub colon_token: Option<Token![:]>,
181 pub bounds: Delimited<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700183
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 /// An equality predicate (unsupported)
185 pub EqPredicate(WhereEqPredicate {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800186 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800188 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }),
190 }
David Tolnayf8e08832017-01-23 00:04:32 -0800191}
192
David Tolnay86eca752016-09-04 11:26:41 -0700193#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700194pub mod parsing {
195 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700196
David Tolnay63e3dee2017-06-03 20:13:17 -0700197 use synom::Synom;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500198 use delimited::Element;
Alex Crichton954046c2017-05-30 21:49:42 -0700199
200 impl Synom for Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400201 named!(parse -> Self, map!(
202 alt!(
203 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800204 lt: punct!(<) >>
David Tolnayc2f1aba2017-11-12 20:29:22 -0800205 lifetimes: call!(Delimited::<LifetimeDef, Token![,]>::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400206 ty_params: cond!(
David Tolnayc2f1aba2017-11-12 20:29:22 -0800207 lifetimes.empty_or_trailing(),
David Tolnaye64213b2017-12-30 00:24:20 -0500208 Delimited::<TypeParam, Token![,]>::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -0400209 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800210 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400211 (lifetimes, ty_params, Some(lt), Some(gt))
212 )
213 |
214 epsilon!() => { |_| (Delimited::new(), None, None, None) }
215 ),
David Tolnaybc7d7d92017-06-03 20:54:05 -0700216 |(lifetimes, ty_params, lt, gt)| Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400217 lt_token: lt,
David Tolnayc2f1aba2017-11-12 20:29:22 -0800218 params: lifetimes.into_iter()
219 .map(Element::into_tuple)
220 .map(|(lifetime, comma)| (GenericParam::Lifetime(lifetime), comma))
221 .chain(ty_params.unwrap_or_default()
222 .into_iter()
223 .map(Element::into_tuple)
224 .map(|(ty_param, comma)| (GenericParam::Type(ty_param), comma)))
225 .collect::<Vec<_>>()
226 .into(),
227 gt_token: gt,
David Tolnayac997dd2017-12-27 23:18:22 -0500228 where_clause: None,
Michael Layzell416724e2017-05-24 21:12:34 -0400229 }
Michael Layzell92639a52017-06-01 00:07:44 -0400230 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800231
232 fn description() -> Option<&'static str> {
233 Some("generic parameters in declaration")
234 }
Alex Crichton954046c2017-05-30 21:49:42 -0700235 }
236
Alex Crichton954046c2017-05-30 21:49:42 -0700237 impl Synom for LifetimeDef {
Michael Layzell92639a52017-06-01 00:07:44 -0400238 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500239 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400240 life: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800241 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400242 bounds: cond!(
243 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500244 Delimited::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400245 ) >>
246 (LifetimeDef {
247 attrs: attrs,
248 lifetime: life,
249 bounds: bounds.unwrap_or_default(),
David Tolnay6af8f1d2017-12-27 23:08:43 -0500250 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400251 })
252 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800253
254 fn description() -> Option<&'static str> {
255 Some("lifetime definition")
256 }
Alex Crichton954046c2017-05-30 21:49:42 -0700257 }
258
259 impl Synom for BoundLifetimes {
Michael Layzell92639a52017-06-01 00:07:44 -0400260 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800261 for_: keyword!(for) >>
262 lt: punct!(<) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400263 lifetimes: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800264 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400265 (BoundLifetimes {
266 for_token: for_,
267 lt_token: lt,
268 gt_token: gt,
269 lifetimes: lifetimes,
270 })
271 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800272
273 fn description() -> Option<&'static str> {
274 Some("bound lifetimes")
275 }
Alex Crichton954046c2017-05-30 21:49:42 -0700276 }
David Tolnay55337722016-09-11 12:58:56 -0700277
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800278 impl Synom for TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400279 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500280 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400281 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800282 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400283 bounds: cond!(
284 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500285 Delimited::parse_separated_nonempty
Michael Layzell92639a52017-06-01 00:07:44 -0400286 ) >>
287 default: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800289 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400290 (eq, ty)
291 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800292 (TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400293 attrs: attrs,
294 ident: id,
295 bounds: bounds.unwrap_or_default(),
296 colon_token: colon,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800297 eq_token: default.as_ref().map(|d| Token![=]((d.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400298 default: default.map(|d| d.1),
299 })
300 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800301
302 fn description() -> Option<&'static str> {
303 Some("type parameter")
304 }
Alex Crichton954046c2017-05-30 21:49:42 -0700305 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700306
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800307 impl Synom for TypeParamBound {
Michael Layzell92639a52017-06-01 00:07:44 -0400308 named!(parse -> Self, alt!(
309 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800310 question: punct!(?) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400311 poly: syn!(PolyTraitRef) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800312 (TypeParamBound::Trait(poly, TraitBoundModifier::Maybe(question)))
Michael Layzell92639a52017-06-01 00:07:44 -0400313 )
314 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800315 syn!(Lifetime) => { TypeParamBound::Region }
Michael Layzell92639a52017-06-01 00:07:44 -0400316 |
317 syn!(PolyTraitRef) => {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800318 |poly| TypeParamBound::Trait(poly, TraitBoundModifier::None)
Alex Crichton954046c2017-05-30 21:49:42 -0700319 }
David Tolnayd9faf442017-12-25 23:36:05 -0500320 |
321 parens!(syn!(PolyTraitRef)) => {
322 |poly| TypeParamBound::Trait(poly.0, TraitBoundModifier::None)
323 }
Michael Layzell92639a52017-06-01 00:07:44 -0400324 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700325
326 fn description() -> Option<&'static str> {
Nika Layzellc691cb42017-12-04 13:44:38 -0500327 Some("type parameter bound")
Alex Crichton954046c2017-05-30 21:49:42 -0700328 }
329 }
330
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500331 impl Synom for ConstParam {
332 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500333 attrs: many0!(Attribute::parse_outer) >>
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500334 const_: keyword!(const) >>
335 ident: syn!(Ident) >>
336 colon: punct!(:) >>
337 ty: syn!(Type) >>
338 eq_def: option!(tuple!(punct!(=), syn!(Expr))) >>
David Tolnay78ee5202017-12-04 22:17:54 -0800339 ({
340 let (eq_token, default) = match eq_def {
341 Some((eq_token, default)) => (Some(eq_token), Some(default)),
342 None => (None, None),
343 };
344 ConstParam {
345 attrs: attrs,
346 const_token: const_,
347 ident: ident,
348 colon_token: colon,
349 ty: ty,
350 eq_token: eq_token,
351 default: default,
352 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500353 })
354 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800355
356 fn description() -> Option<&'static str> {
357 Some("generic `const` parameter")
358 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500359 }
360
Alex Crichton954046c2017-05-30 21:49:42 -0700361 impl Synom for WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500362 named!(parse -> Self, do_parse!(
363 where_: keyword!(where) >>
364 predicates: call!(Delimited::parse_terminated) >>
365 (WhereClause {
366 predicates: predicates,
367 where_token: where_,
368 })
Michael Layzell92639a52017-06-01 00:07:44 -0400369 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700370
371 fn description() -> Option<&'static str> {
372 Some("where clause")
373 }
374 }
375
376 impl Synom for WherePredicate {
Michael Layzell92639a52017-06-01 00:07:44 -0400377 named!(parse -> Self, alt!(
378 do_parse!(
379 ident: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800380 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400381 bounds: cond!(
382 colon.is_some(),
David Tolnaye64213b2017-12-30 00:24:20 -0500383 Delimited::parse_separated
Michael Layzell92639a52017-06-01 00:07:44 -0400384 ) >>
385 (WherePredicate::RegionPredicate(WhereRegionPredicate {
386 lifetime: ident,
387 bounds: bounds.unwrap_or_default(),
388 colon_token: colon,
389 }))
390 )
391 |
392 do_parse!(
393 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800394 bounded_ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800395 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400396 bounds: call!(Delimited::parse_separated_nonempty) >>
397 (WherePredicate::BoundPredicate(WhereBoundPredicate {
398 bound_lifetimes: bound_lifetimes,
399 bounded_ty: bounded_ty,
400 bounds: bounds,
401 colon_token: colon,
402 }))
403 )
404 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800405
406 fn description() -> Option<&'static str> {
407 Some("predicate in where clause")
408 }
Alex Crichton954046c2017-05-30 21:49:42 -0700409 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700410}
David Tolnay87d0b442016-09-04 11:52:12 -0700411
412#[cfg(feature = "printing")]
413mod printing {
414 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700415 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -0500416 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700417
Michael Layzell3936ceb2017-07-08 00:28:36 -0400418 /// Returns true if the generics object has no lifetimes or ty_params.
419 fn empty_normal_generics(generics: &Generics) -> bool {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800420 generics.params.is_empty()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400421 }
422
David Tolnay8ef93042016-09-04 14:08:40 -0700423 impl ToTokens for Generics {
424 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400425 if empty_normal_generics(self) {
426 return;
427 }
428
Alex Crichton259ee532017-07-14 06:51:02 -0700429 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800430 self.params.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700431 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700432 }
433 }
434
David Tolnaye7678922016-10-13 20:44:03 -0700435 impl<'a> ToTokens for ImplGenerics<'a> {
436 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500437 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400438 return;
439 }
440
Alex Crichton259ee532017-07-14 06:51:02 -0700441 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800442 for param in self.0.params.iter() {
443 match **param.item() {
444 GenericParam::Lifetime(ref param) => {
445 param.to_tokens(tokens);
446 }
447 GenericParam::Type(ref param) => {
448 // Leave off the type parameter defaults
449 tokens.append_all(param.attrs.outer());
450 param.ident.to_tokens(tokens);
451 if !param.bounds.is_empty() {
452 TokensOrDefault(&param.colon_token).to_tokens(tokens);
453 param.bounds.to_tokens(tokens);
454 }
455 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500456 GenericParam::Const(ref param) => {
457 // Leave off the const parameter defaults
458 tokens.append_all(param.attrs.outer());
459 param.const_token.to_tokens(tokens);
460 param.ident.to_tokens(tokens);
461 param.colon_token.to_tokens(tokens);
462 param.ty.to_tokens(tokens);
463 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400464 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700465 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700466 }
Alex Crichton259ee532017-07-14 06:51:02 -0700467 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700468 }
469 }
470
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 impl<'a> ToTokens for TypeGenerics<'a> {
David Tolnaye7678922016-10-13 20:44:03 -0700472 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500473 if empty_normal_generics(self.0) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400474 return;
475 }
476
Alex Crichton259ee532017-07-14 06:51:02 -0700477 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800478 for param in self.0.params.iter() {
479 match **param.item() {
480 GenericParam::Lifetime(ref param) => {
481 // Leave off the lifetime bounds and attributes
482 param.lifetime.to_tokens(tokens);
483 }
484 GenericParam::Type(ref param) => {
485 // Leave off the type parameter defaults
486 param.ident.to_tokens(tokens);
487 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500488 GenericParam::Const(ref param) => {
489 // Leave off the const parameter defaults
490 param.ident.to_tokens(tokens);
491 }
David Tolnayc2f1aba2017-11-12 20:29:22 -0800492 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700493 param.delimiter().to_tokens(tokens);
494 }
Alex Crichton259ee532017-07-14 06:51:02 -0700495 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700496 }
497 }
498
David Tolnayc879a502017-01-25 15:51:32 -0800499 impl<'a> ToTokens for Turbofish<'a> {
500 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500501 if !empty_normal_generics(self.0) {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800503 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800504 }
505 }
506 }
507
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700508 impl ToTokens for BoundLifetimes {
509 fn to_tokens(&self, tokens: &mut Tokens) {
510 self.for_token.to_tokens(tokens);
511 self.lt_token.to_tokens(tokens);
512 self.lifetimes.to_tokens(tokens);
513 self.gt_token.to_tokens(tokens);
514 }
515 }
516
David Tolnay87d0b442016-09-04 11:52:12 -0700517 impl ToTokens for LifetimeDef {
518 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700519 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700520 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400521 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700522 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400523 self.bounds.to_tokens(tokens);
524 }
David Tolnay87d0b442016-09-04 11:52:12 -0700525 }
526 }
527
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800528 impl ToTokens for TypeParam {
David Tolnay8ef93042016-09-04 14:08:40 -0700529 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700530 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700531 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400532 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700533 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400534 self.bounds.to_tokens(tokens);
535 }
536 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -0700537 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400538 self.default.to_tokens(tokens);
539 }
David Tolnay8ef93042016-09-04 14:08:40 -0700540 }
541 }
542
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800543 impl ToTokens for TypeParamBound {
David Tolnay87d0b442016-09-04 11:52:12 -0700544 fn to_tokens(&self, tokens: &mut Tokens) {
545 match *self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800546 TypeParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens),
547 TypeParamBound::Trait(ref trait_ref, ref modifier) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700548 modifier.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700549 trait_ref.to_tokens(tokens);
550 }
551 }
552 }
553 }
554
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 impl ToTokens for TraitBoundModifier {
556 fn to_tokens(&self, tokens: &mut Tokens) {
557 match *self {
558 TraitBoundModifier::None => {}
559 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
560 }
561 }
562 }
563
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500564 impl ToTokens for ConstParam {
565 fn to_tokens(&self, tokens: &mut Tokens) {
566 tokens.append_all(self.attrs.outer());
567 self.const_token.to_tokens(tokens);
568 self.ident.to_tokens(tokens);
569 self.colon_token.to_tokens(tokens);
570 self.ty.to_tokens(tokens);
571 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -0800572 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500573 self.default.to_tokens(tokens);
574 }
575 }
576 }
577
David Tolnay55337722016-09-11 12:58:56 -0700578 impl ToTokens for WhereClause {
579 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayac997dd2017-12-27 23:18:22 -0500580 self.where_token.to_tokens(tokens);
581 self.predicates.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700582 }
583 }
David Tolnay8ef93042016-09-04 14:08:40 -0700584
David Tolnay8ef93042016-09-04 14:08:40 -0700585 impl ToTokens for WhereBoundPredicate {
586 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 self.bound_lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700588 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700589 self.colon_token.to_tokens(tokens);
590 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700591 }
592 }
593
594 impl ToTokens for WhereRegionPredicate {
595 fn to_tokens(&self, tokens: &mut Tokens) {
596 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400597 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700598 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400599 self.bounds.to_tokens(tokens);
600 }
David Tolnay8ef93042016-09-04 14:08:40 -0700601 }
602 }
David Tolnayf8e08832017-01-23 00:04:32 -0800603
604 impl ToTokens for WhereEqPredicate {
605 fn to_tokens(&self, tokens: &mut Tokens) {
606 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700607 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800608 self.rhs_ty.to_tokens(tokens);
609 }
610 }
David Tolnay87d0b442016-09-04 11:52:12 -0700611}