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")] |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 18 | ast_struct! { |
| 19 | /// Returned by `Generics::split_for_impl`. |
| 20 | pub struct ImplGenerics<'a>(&'a Generics); |
| 21 | } |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 22 | |
| 23 | #[cfg(feature = "printing")] |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 24 | ast_struct! { |
| 25 | /// Returned by `Generics::split_for_impl`. |
| 26 | pub struct TyGenerics<'a>(&'a Generics); |
| 27 | } |
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")] |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 30 | ast_struct! { |
| 31 | /// Returned by `TyGenerics::as_turbofish`. |
| 32 | pub struct Turbofish<'a>(&'a Generics); |
| 33 | } |
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 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 360 | impl ToTokens for Generics { |
| 361 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 362 | self.lt_token.to_tokens(tokens); |
| 363 | self.lifetimes.to_tokens(tokens); |
| 364 | self.ty_params.to_tokens(tokens); |
| 365 | self.gt_token.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 369 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 370 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 371 | self.0.lt_token.to_tokens(tokens); |
| 372 | self.0.lifetimes.to_tokens(tokens); |
| 373 | for param in self.0.ty_params.iter() { |
| 374 | // Leave off the type parameter defaults |
| 375 | let item = param.item(); |
| 376 | tokens.append_all(item.attrs.outer()); |
| 377 | item.ident.to_tokens(tokens); |
| 378 | item.colon_token.to_tokens(tokens); |
| 379 | item.bounds.to_tokens(tokens); |
| 380 | param.delimiter().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 381 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 382 | self.0.gt_token.to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | impl<'a> ToTokens for TyGenerics<'a> { |
| 387 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 388 | self.0.lt_token.to_tokens(tokens); |
| 389 | // Leave off the lifetime bounds and attributes |
| 390 | for param in self.0.lifetimes.iter() { |
| 391 | param.item().lifetime.to_tokens(tokens); |
| 392 | param.delimiter().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 393 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 394 | // Leave off the type parameter defaults |
| 395 | for param in self.0.ty_params.iter() { |
| 396 | param.item().ident.to_tokens(tokens); |
| 397 | param.delimiter().to_tokens(tokens); |
| 398 | } |
| 399 | self.0.gt_token.to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 403 | impl<'a> ToTokens for Turbofish<'a> { |
| 404 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 405 | let has_lifetimes = !self.0.lifetimes.is_empty(); |
| 406 | let has_ty_params = !self.0.ty_params.is_empty(); |
| 407 | if has_lifetimes || has_ty_params { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 408 | tokens::Colon2::default().to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 409 | TyGenerics(self.0).to_tokens(tokens); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 414 | impl ToTokens for BoundLifetimes { |
| 415 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 416 | self.for_token.to_tokens(tokens); |
| 417 | self.lt_token.to_tokens(tokens); |
| 418 | self.lifetimes.to_tokens(tokens); |
| 419 | self.gt_token.to_tokens(tokens); |
| 420 | } |
| 421 | } |
| 422 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 423 | impl ToTokens for LifetimeDef { |
| 424 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 425 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 426 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 427 | self.colon_token.to_tokens(tokens); |
| 428 | self.bounds.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 432 | impl ToTokens for TyParam { |
| 433 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 434 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 435 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 436 | self.colon_token.to_tokens(tokens); |
| 437 | self.bounds.to_tokens(tokens); |
| 438 | self.eq_token.to_tokens(tokens); |
| 439 | self.default.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 443 | impl ToTokens for TyParamBound { |
| 444 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 445 | match *self { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 446 | TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 447 | TyParamBound::Trait(ref trait_ref, ref modifier) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 448 | modifier.to_tokens(tokens); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 449 | trait_ref.to_tokens(tokens); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 455 | impl ToTokens for TraitBoundModifier { |
| 456 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 457 | match *self { |
| 458 | TraitBoundModifier::None => {} |
| 459 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 464 | impl ToTokens for WhereClause { |
| 465 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 466 | self.where_token.to_tokens(tokens); |
| 467 | self.predicates.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 470 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 471 | impl ToTokens for WhereBoundPredicate { |
| 472 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 473 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 474 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 475 | self.colon_token.to_tokens(tokens); |
| 476 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | impl ToTokens for WhereRegionPredicate { |
| 481 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 482 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 483 | self.colon_token.to_tokens(tokens); |
| 484 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 487 | |
| 488 | impl ToTokens for WhereEqPredicate { |
| 489 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 490 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 491 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 492 | self.rhs_ty.to_tokens(tokens); |
| 493 | } |
| 494 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 495 | } |