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 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 214 | use synom::{PResult, Cursor, Synom, parse_error}; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 215 | use synom::tokens::*; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 216 | |
| 217 | impl Synom for Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 218 | named!(parse -> Self, map!( |
| 219 | alt!( |
| 220 | do_parse!( |
| 221 | lt: syn!(Lt) >> |
| 222 | lifetimes: call!(Delimited::parse_terminated) >> |
| 223 | ty_params: cond!( |
| 224 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 225 | call!(Delimited::parse_terminated) |
| 226 | ) >> |
| 227 | gt: syn!(Gt) >> |
| 228 | (lifetimes, ty_params, Some(lt), Some(gt)) |
| 229 | ) |
| 230 | | |
| 231 | epsilon!() => { |_| (Delimited::new(), None, None, None) } |
| 232 | ), |
| 233 | |(lifetimes, ty_params, lt, gt): (_, Option<_>, _, _)| Generics { |
| 234 | lifetimes: lifetimes, |
| 235 | ty_params: ty_params.unwrap_or_default(), |
| 236 | where_clause: WhereClause::default(), |
| 237 | gt_token: gt, |
| 238 | lt_token: lt, |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 239 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 240 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | impl Synom for Lifetime { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 244 | fn parse(input: Cursor) -> PResult<Self> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame^] | 245 | match input.word() { |
| 246 | Some((rest, span, sym)) => { |
| 247 | if sym.as_str().starts_with('\'') { |
| 248 | return Ok((rest, Lifetime { |
| 249 | ident: Ident { |
| 250 | span: Span(span), |
| 251 | sym: sym |
| 252 | } |
| 253 | })); |
| 254 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 255 | } |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame^] | 256 | _ => {} |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 257 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 258 | parse_error() |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 261 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 262 | impl Synom for LifetimeDef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 263 | named!(parse -> Self, do_parse!( |
| 264 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 265 | life: syn!(Lifetime) >> |
| 266 | colon: option!(syn!(Colon)) >> |
| 267 | bounds: cond!( |
| 268 | colon.is_some(), |
| 269 | call!(Delimited::parse_separated_nonempty) |
| 270 | ) >> |
| 271 | (LifetimeDef { |
| 272 | attrs: attrs, |
| 273 | lifetime: life, |
| 274 | bounds: bounds.unwrap_or_default(), |
| 275 | colon_token: colon.map(|_| tokens::Colon::default()), |
| 276 | }) |
| 277 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | impl Synom for BoundLifetimes { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 281 | named!(parse -> Self, do_parse!( |
| 282 | for_: syn!(For) >> |
| 283 | lt: syn!(Lt) >> |
| 284 | lifetimes: call!(Delimited::parse_terminated) >> |
| 285 | gt: syn!(Gt) >> |
| 286 | (BoundLifetimes { |
| 287 | for_token: for_, |
| 288 | lt_token: lt, |
| 289 | gt_token: gt, |
| 290 | lifetimes: lifetimes, |
| 291 | }) |
| 292 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 293 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 294 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 295 | impl Synom for TyParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 296 | named!(parse -> Self, do_parse!( |
| 297 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 298 | id: syn!(Ident) >> |
| 299 | colon: option!(syn!(Colon)) >> |
| 300 | bounds: cond!( |
| 301 | colon.is_some(), |
| 302 | call!(Delimited::parse_separated_nonempty) |
| 303 | ) >> |
| 304 | default: option!(do_parse!( |
| 305 | eq: syn!(Eq) >> |
| 306 | ty: syn!(Ty) >> |
| 307 | (eq, ty) |
| 308 | )) >> |
| 309 | (TyParam { |
| 310 | attrs: attrs, |
| 311 | ident: id, |
| 312 | bounds: bounds.unwrap_or_default(), |
| 313 | colon_token: colon, |
| 314 | eq_token: default.as_ref().map(|d| tokens::Eq((d.0).0)), |
| 315 | default: default.map(|d| d.1), |
| 316 | }) |
| 317 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 318 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 319 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 320 | impl Synom for TyParamBound { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 321 | named!(parse -> Self, alt!( |
| 322 | do_parse!( |
| 323 | question: syn!(Question) >> |
| 324 | poly: syn!(PolyTraitRef) >> |
| 325 | (TyParamBound::Trait(poly, TraitBoundModifier::Maybe(question))) |
| 326 | ) |
| 327 | | |
| 328 | syn!(Lifetime) => { TyParamBound::Region } |
| 329 | | |
| 330 | syn!(PolyTraitRef) => { |
| 331 | |poly| TyParamBound::Trait(poly, TraitBoundModifier::None) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 332 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 333 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 334 | |
| 335 | fn description() -> Option<&'static str> { |
| 336 | Some("type parameter buond") |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | impl Synom for WhereClause { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 341 | named!(parse -> Self, alt!( |
| 342 | do_parse!( |
| 343 | where_: syn!(Where) >> |
| 344 | predicates: call!(Delimited::parse_terminated) >> |
| 345 | (WhereClause { |
| 346 | predicates: predicates, |
| 347 | where_token: Some(where_), |
| 348 | }) |
| 349 | ) |
| 350 | | |
| 351 | epsilon!() => { |_| WhereClause::default() } |
| 352 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 353 | |
| 354 | fn description() -> Option<&'static str> { |
| 355 | Some("where clause") |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | impl Synom for WherePredicate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 360 | named!(parse -> Self, alt!( |
| 361 | do_parse!( |
| 362 | ident: syn!(Lifetime) >> |
| 363 | colon: option!(syn!(Colon)) >> |
| 364 | bounds: cond!( |
| 365 | colon.is_some(), |
| 366 | call!(Delimited::parse_separated) |
| 367 | ) >> |
| 368 | (WherePredicate::RegionPredicate(WhereRegionPredicate { |
| 369 | lifetime: ident, |
| 370 | bounds: bounds.unwrap_or_default(), |
| 371 | colon_token: colon, |
| 372 | })) |
| 373 | ) |
| 374 | | |
| 375 | do_parse!( |
| 376 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
| 377 | bounded_ty: syn!(Ty) >> |
| 378 | colon: syn!(Colon) >> |
| 379 | bounds: call!(Delimited::parse_separated_nonempty) >> |
| 380 | (WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 381 | bound_lifetimes: bound_lifetimes, |
| 382 | bounded_ty: bounded_ty, |
| 383 | bounds: bounds, |
| 384 | colon_token: colon, |
| 385 | })) |
| 386 | ) |
| 387 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 388 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 389 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 390 | |
| 391 | #[cfg(feature = "printing")] |
| 392 | mod printing { |
| 393 | use super::*; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 394 | use attr::FilterAttrs; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 395 | use quote::{Tokens, ToTokens}; |
| 396 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 397 | impl ToTokens for Generics { |
| 398 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 399 | self.lt_token.to_tokens(tokens); |
| 400 | self.lifetimes.to_tokens(tokens); |
| 401 | self.ty_params.to_tokens(tokens); |
| 402 | self.gt_token.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 406 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 407 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 408 | self.0.lt_token.to_tokens(tokens); |
| 409 | self.0.lifetimes.to_tokens(tokens); |
| 410 | for param in self.0.ty_params.iter() { |
| 411 | // Leave off the type parameter defaults |
| 412 | let item = param.item(); |
| 413 | tokens.append_all(item.attrs.outer()); |
| 414 | item.ident.to_tokens(tokens); |
| 415 | item.colon_token.to_tokens(tokens); |
| 416 | item.bounds.to_tokens(tokens); |
| 417 | param.delimiter().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 418 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 419 | self.0.gt_token.to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | impl<'a> ToTokens for TyGenerics<'a> { |
| 424 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 425 | self.0.lt_token.to_tokens(tokens); |
| 426 | // Leave off the lifetime bounds and attributes |
| 427 | for param in self.0.lifetimes.iter() { |
| 428 | param.item().lifetime.to_tokens(tokens); |
| 429 | param.delimiter().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 430 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 431 | // Leave off the type parameter defaults |
| 432 | for param in self.0.ty_params.iter() { |
| 433 | param.item().ident.to_tokens(tokens); |
| 434 | param.delimiter().to_tokens(tokens); |
| 435 | } |
| 436 | self.0.gt_token.to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 440 | impl<'a> ToTokens for Turbofish<'a> { |
| 441 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 442 | let has_lifetimes = !self.0.lifetimes.is_empty(); |
| 443 | let has_ty_params = !self.0.ty_params.is_empty(); |
| 444 | if has_lifetimes || has_ty_params { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 445 | tokens::Colon2::default().to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 446 | TyGenerics(self.0).to_tokens(tokens); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 451 | impl ToTokens for Lifetime { |
| 452 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 453 | self.ident.to_tokens(tokens); |
| 454 | } |
| 455 | } |
| 456 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 457 | impl ToTokens for BoundLifetimes { |
| 458 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 459 | self.for_token.to_tokens(tokens); |
| 460 | self.lt_token.to_tokens(tokens); |
| 461 | self.lifetimes.to_tokens(tokens); |
| 462 | self.gt_token.to_tokens(tokens); |
| 463 | } |
| 464 | } |
| 465 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 466 | impl ToTokens for LifetimeDef { |
| 467 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 468 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 469 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 470 | self.colon_token.to_tokens(tokens); |
| 471 | self.bounds.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 475 | impl ToTokens for TyParam { |
| 476 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 477 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 478 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 479 | self.colon_token.to_tokens(tokens); |
| 480 | self.bounds.to_tokens(tokens); |
| 481 | self.eq_token.to_tokens(tokens); |
| 482 | self.default.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 486 | impl ToTokens for TyParamBound { |
| 487 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 488 | match *self { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 489 | TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 490 | TyParamBound::Trait(ref trait_ref, ref modifier) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 491 | modifier.to_tokens(tokens); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 492 | trait_ref.to_tokens(tokens); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 498 | impl ToTokens for TraitBoundModifier { |
| 499 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 500 | match *self { |
| 501 | TraitBoundModifier::None => {} |
| 502 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 507 | impl ToTokens for WhereClause { |
| 508 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 509 | self.where_token.to_tokens(tokens); |
| 510 | self.predicates.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 513 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 514 | impl ToTokens for WhereBoundPredicate { |
| 515 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 516 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 517 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 518 | self.colon_token.to_tokens(tokens); |
| 519 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | impl ToTokens for WhereRegionPredicate { |
| 524 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 525 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 526 | self.colon_token.to_tokens(tokens); |
| 527 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 530 | |
| 531 | impl ToTokens for WhereEqPredicate { |
| 532 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 533 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 534 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 535 | self.rhs_ty.to_tokens(tokens); |
| 536 | } |
| 537 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 538 | } |