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