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