David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2 | use delimited::Delimited; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_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 Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 9 | pub lt_token: Option<Token![<]>, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 10 | pub params: Delimited<GenericParam, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 11 | pub gt_token: Option<Token![>]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 12 | pub where_clause: WhereClause, |
| 13 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 14 | } |
| 15 | |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 16 | ast_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 | }), |
| 34 | } |
| 35 | } |
| 36 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 37 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 38 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 39 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 40 | /// Returned by `Generics::split_for_impl`. |
| 41 | pub struct ImplGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 42 | |
| 43 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 44 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 45 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 46 | /// Returned by `Generics::split_for_impl`. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 47 | pub struct TypeGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 48 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 49 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 50 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 51 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 52 | /// Returned by `TypeGenerics::as_turbofish`. |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 53 | pub struct Turbofish<'a>(&'a Generics); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 54 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 55 | #[cfg(feature = "printing")] |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 56 | impl Generics { |
| 57 | /// Split a type's generics into the pieces required for impl'ing a trait |
| 58 | /// for that type. |
| 59 | /// |
| 60 | /// ``` |
| 61 | /// # extern crate syn; |
| 62 | /// # #[macro_use] |
| 63 | /// # extern crate quote; |
| 64 | /// # fn main() { |
| 65 | /// # let generics: syn::Generics = Default::default(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | /// # let name = syn::Ident::from("MyType"); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 67 | /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 68 | /// quote! { |
| 69 | /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { |
| 70 | /// // ... |
| 71 | /// } |
| 72 | /// } |
| 73 | /// # ; |
| 74 | /// # } |
| 75 | /// ``` |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 76 | pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, &WhereClause) { |
| 77 | (ImplGenerics(self), TypeGenerics(self), &self.where_clause) |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 81 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 82 | impl<'a> TypeGenerics<'a> { |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 83 | /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`. |
| 84 | pub fn as_turbofish(&self) -> Turbofish { |
| 85 | Turbofish(self.0) |
| 86 | } |
| 87 | } |
| 88 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 89 | ast_struct! { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 90 | /// A set of bound lifetimes, e.g. `for<'a, 'b, 'c>` |
| 91 | #[derive(Default)] |
| 92 | pub struct BoundLifetimes { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 93 | pub for_token: Token![for], |
| 94 | pub lt_token: Token![<], |
| 95 | pub lifetimes: Delimited<LifetimeDef, Token![,]>, |
| 96 | pub gt_token: Token![>], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 100 | impl LifetimeDef { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 101 | pub fn new(lifetime: Lifetime) -> Self { |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 102 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 103 | attrs: Vec::new(), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 104 | lifetime: lifetime, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 105 | colon_token: None, |
| 106 | bounds: Delimited::new(), |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 111 | impl From<Ident> for TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 112 | fn from(ident: Ident) -> Self { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 113 | TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 114 | attrs: vec![], |
| 115 | ident: ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 116 | colon_token: None, |
| 117 | bounds: Delimited::new(), |
| 118 | eq_token: None, |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 119 | default: None, |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 124 | ast_enum! { |
| 125 | /// The AST represents all type param bounds as types. |
| 126 | /// `typeck::collect::compute_bounds` matches these against |
| 127 | /// the "special" built-in traits (see `middle::lang_items`) and |
| 128 | /// detects Copy, Send and Sync. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 129 | pub enum TypeParamBound { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 130 | Trait(PolyTraitRef, TraitBoundModifier), |
| 131 | Region(Lifetime), |
| 132 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 135 | ast_enum! { |
| 136 | /// A modifier on a bound, currently this is only used for `?Sized`, where the |
| 137 | /// modifier is `Maybe`. Negative bounds should also be handled here. |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 138 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 139 | pub enum TraitBoundModifier { |
| 140 | None, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 141 | Maybe(Token![?]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 142 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 145 | ast_struct! { |
| 146 | /// A `where` clause in a definition |
| 147 | #[derive(Default)] |
| 148 | pub struct WhereClause { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 149 | pub where_token: Option<Token![where]>, |
| 150 | pub predicates: Delimited<WherePredicate, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 151 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 154 | impl WhereClause { |
| 155 | pub fn none() -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 156 | WhereClause::default() |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 160 | ast_enum_of_structs! { |
| 161 | /// A single predicate in a `where` clause |
| 162 | pub enum WherePredicate { |
| 163 | /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c` |
| 164 | pub BoundPredicate(WhereBoundPredicate { |
| 165 | /// Any lifetimes from a `for` binding |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 166 | pub bound_lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 167 | /// The type being bounded |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 168 | pub bounded_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 169 | pub colon_token: Token![:], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 170 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 171 | pub bounds: Delimited<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 172 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 173 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 174 | /// A lifetime predicate, e.g. `'a: 'b+'c` |
| 175 | pub RegionPredicate(WhereRegionPredicate { |
| 176 | pub lifetime: Lifetime, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 177 | pub colon_token: Option<Token![:]>, |
| 178 | pub bounds: Delimited<Lifetime, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 179 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 180 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 181 | /// An equality predicate (unsupported) |
| 182 | pub EqPredicate(WhereEqPredicate { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 183 | pub lhs_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 184 | pub eq_token: Token![=], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 185 | pub rhs_ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 186 | }), |
| 187 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 188 | } |
| 189 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 190 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 191 | pub mod parsing { |
| 192 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 193 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 194 | use synom::Synom; |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 195 | use synom::delimited::Element; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 196 | |
| 197 | impl Synom for Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 198 | named!(parse -> Self, map!( |
| 199 | alt!( |
| 200 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 201 | lt: punct!(<) >> |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 202 | lifetimes: call!(Delimited::<LifetimeDef, Token![,]>::parse_terminated) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 203 | ty_params: cond!( |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 204 | lifetimes.empty_or_trailing(), |
| 205 | call!(Delimited::<TypeParam, Token![,]>::parse_terminated) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 206 | ) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 207 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 208 | (lifetimes, ty_params, Some(lt), Some(gt)) |
| 209 | ) |
| 210 | | |
| 211 | epsilon!() => { |_| (Delimited::new(), None, None, None) } |
| 212 | ), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 213 | |(lifetimes, ty_params, lt, gt)| Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 214 | lt_token: lt, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 215 | params: lifetimes.into_iter() |
| 216 | .map(Element::into_tuple) |
| 217 | .map(|(lifetime, comma)| (GenericParam::Lifetime(lifetime), comma)) |
| 218 | .chain(ty_params.unwrap_or_default() |
| 219 | .into_iter() |
| 220 | .map(Element::into_tuple) |
| 221 | .map(|(ty_param, comma)| (GenericParam::Type(ty_param), comma))) |
| 222 | .collect::<Vec<_>>() |
| 223 | .into(), |
| 224 | gt_token: gt, |
| 225 | where_clause: WhereClause::default(), |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 226 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 227 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 230 | impl Synom for LifetimeDef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 231 | named!(parse -> Self, do_parse!( |
| 232 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 233 | life: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 234 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 235 | bounds: cond!( |
| 236 | colon.is_some(), |
| 237 | call!(Delimited::parse_separated_nonempty) |
| 238 | ) >> |
| 239 | (LifetimeDef { |
| 240 | attrs: attrs, |
| 241 | lifetime: life, |
| 242 | bounds: bounds.unwrap_or_default(), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 243 | colon_token: colon.map(|_| <Token![:]>::default()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 244 | }) |
| 245 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | impl Synom for BoundLifetimes { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 249 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 250 | for_: keyword!(for) >> |
| 251 | lt: punct!(<) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 252 | lifetimes: call!(Delimited::parse_terminated) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 253 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 254 | (BoundLifetimes { |
| 255 | for_token: for_, |
| 256 | lt_token: lt, |
| 257 | gt_token: gt, |
| 258 | lifetimes: lifetimes, |
| 259 | }) |
| 260 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 261 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 262 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 263 | impl Synom for TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 264 | named!(parse -> Self, do_parse!( |
| 265 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 266 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 267 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 268 | bounds: cond!( |
| 269 | colon.is_some(), |
| 270 | call!(Delimited::parse_separated_nonempty) |
| 271 | ) >> |
| 272 | default: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 273 | eq: punct!(=) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 274 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 275 | (eq, ty) |
| 276 | )) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 277 | (TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 278 | attrs: attrs, |
| 279 | ident: id, |
| 280 | bounds: bounds.unwrap_or_default(), |
| 281 | colon_token: colon, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 282 | eq_token: default.as_ref().map(|d| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 283 | default: default.map(|d| d.1), |
| 284 | }) |
| 285 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 286 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 287 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 288 | impl Synom for TypeParamBound { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 289 | named!(parse -> Self, alt!( |
| 290 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 291 | question: punct!(?) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 292 | poly: syn!(PolyTraitRef) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 293 | (TypeParamBound::Trait(poly, TraitBoundModifier::Maybe(question))) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 294 | ) |
| 295 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 296 | syn!(Lifetime) => { TypeParamBound::Region } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 297 | | |
| 298 | syn!(PolyTraitRef) => { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 299 | |poly| TypeParamBound::Trait(poly, TraitBoundModifier::None) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 300 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 301 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 302 | |
| 303 | fn description() -> Option<&'static str> { |
| 304 | Some("type parameter buond") |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl Synom for WhereClause { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 309 | named!(parse -> Self, alt!( |
| 310 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 311 | where_: keyword!(where) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 312 | predicates: call!(Delimited::parse_terminated) >> |
| 313 | (WhereClause { |
| 314 | predicates: predicates, |
| 315 | where_token: Some(where_), |
| 316 | }) |
| 317 | ) |
| 318 | | |
| 319 | epsilon!() => { |_| WhereClause::default() } |
| 320 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 321 | |
| 322 | fn description() -> Option<&'static str> { |
| 323 | Some("where clause") |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl Synom for WherePredicate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 328 | named!(parse -> Self, alt!( |
| 329 | do_parse!( |
| 330 | ident: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 331 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 332 | bounds: cond!( |
| 333 | colon.is_some(), |
| 334 | call!(Delimited::parse_separated) |
| 335 | ) >> |
| 336 | (WherePredicate::RegionPredicate(WhereRegionPredicate { |
| 337 | lifetime: ident, |
| 338 | bounds: bounds.unwrap_or_default(), |
| 339 | colon_token: colon, |
| 340 | })) |
| 341 | ) |
| 342 | | |
| 343 | do_parse!( |
| 344 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 345 | bounded_ty: syn!(Type) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 346 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 347 | bounds: call!(Delimited::parse_separated_nonempty) >> |
| 348 | (WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 349 | bound_lifetimes: bound_lifetimes, |
| 350 | bounded_ty: bounded_ty, |
| 351 | bounds: bounds, |
| 352 | colon_token: colon, |
| 353 | })) |
| 354 | ) |
| 355 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 356 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 357 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 358 | |
| 359 | #[cfg(feature = "printing")] |
| 360 | mod printing { |
| 361 | use super::*; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 362 | use attr::FilterAttrs; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 363 | use quote::{Tokens, ToTokens}; |
| 364 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 365 | /// Returns true if the generics object has no lifetimes or ty_params. |
| 366 | fn empty_normal_generics(generics: &Generics) -> bool { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 367 | generics.params.is_empty() |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 368 | } |
| 369 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 370 | impl ToTokens for Generics { |
| 371 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 372 | if empty_normal_generics(self) { |
| 373 | return; |
| 374 | } |
| 375 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 376 | TokensOrDefault(&self.lt_token).to_tokens(tokens); |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 377 | self.params.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 378 | TokensOrDefault(&self.gt_token).to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 382 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 383 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 384 | if empty_normal_generics(&self.0) { |
| 385 | return; |
| 386 | } |
| 387 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 388 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 389 | for param in self.0.params.iter() { |
| 390 | match **param.item() { |
| 391 | GenericParam::Lifetime(ref param) => { |
| 392 | param.to_tokens(tokens); |
| 393 | } |
| 394 | GenericParam::Type(ref param) => { |
| 395 | // Leave off the type parameter defaults |
| 396 | tokens.append_all(param.attrs.outer()); |
| 397 | param.ident.to_tokens(tokens); |
| 398 | if !param.bounds.is_empty() { |
| 399 | TokensOrDefault(¶m.colon_token).to_tokens(tokens); |
| 400 | param.bounds.to_tokens(tokens); |
| 401 | } |
| 402 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 403 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 404 | param.delimiter().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 405 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 406 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 410 | impl<'a> ToTokens for TypeGenerics<'a> { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 411 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 412 | if empty_normal_generics(&self.0) { |
| 413 | return; |
| 414 | } |
| 415 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 416 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 417 | for param in self.0.params.iter() { |
| 418 | match **param.item() { |
| 419 | GenericParam::Lifetime(ref param) => { |
| 420 | // Leave off the lifetime bounds and attributes |
| 421 | param.lifetime.to_tokens(tokens); |
| 422 | } |
| 423 | GenericParam::Type(ref param) => { |
| 424 | // Leave off the type parameter defaults |
| 425 | param.ident.to_tokens(tokens); |
| 426 | } |
| 427 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 428 | param.delimiter().to_tokens(tokens); |
| 429 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 430 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 434 | impl<'a> ToTokens for Turbofish<'a> { |
| 435 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 436 | if !empty_normal_generics(&self.0) { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 437 | <Token![::]>::default().to_tokens(tokens); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 438 | TypeGenerics(self.0).to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 443 | impl ToTokens for BoundLifetimes { |
| 444 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 445 | self.for_token.to_tokens(tokens); |
| 446 | self.lt_token.to_tokens(tokens); |
| 447 | self.lifetimes.to_tokens(tokens); |
| 448 | self.gt_token.to_tokens(tokens); |
| 449 | } |
| 450 | } |
| 451 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 452 | impl ToTokens for LifetimeDef { |
| 453 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 454 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 455 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 456 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 457 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 458 | self.bounds.to_tokens(tokens); |
| 459 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 463 | impl ToTokens for TypeParam { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 464 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 465 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 466 | self.ident.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 467 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 468 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 469 | self.bounds.to_tokens(tokens); |
| 470 | } |
| 471 | if self.default.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 472 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 473 | self.default.to_tokens(tokens); |
| 474 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 478 | impl ToTokens for TypeParamBound { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 479 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 480 | match *self { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 481 | TypeParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens), |
| 482 | TypeParamBound::Trait(ref trait_ref, ref modifier) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 483 | modifier.to_tokens(tokens); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 484 | trait_ref.to_tokens(tokens); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 490 | impl ToTokens for TraitBoundModifier { |
| 491 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 492 | match *self { |
| 493 | TraitBoundModifier::None => {} |
| 494 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 499 | impl ToTokens for WhereClause { |
| 500 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 501 | if !self.predicates.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 502 | TokensOrDefault(&self.where_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 503 | self.predicates.to_tokens(tokens); |
| 504 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 507 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 508 | impl ToTokens for WhereBoundPredicate { |
| 509 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 510 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 511 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 512 | self.colon_token.to_tokens(tokens); |
| 513 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | impl ToTokens for WhereRegionPredicate { |
| 518 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 519 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 520 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 521 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 522 | self.bounds.to_tokens(tokens); |
| 523 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 526 | |
| 527 | impl ToTokens for WhereEqPredicate { |
| 528 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 529 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 530 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 531 | self.rhs_ty.to_tokens(tokens); |
| 532 | } |
| 533 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 534 | } |