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