David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 11 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 12 | ast_struct! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 13 | /// Lifetimes and type parameters attached to a declaration of a function, |
| 14 | /// enum, trait, etc. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 15 | /// |
| 16 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 17 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | #[derive(Default)] |
| 19 | pub struct Generics { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 20 | pub lt_token: Option<Token![<]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 21 | pub params: Punctuated<GenericParam, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 22 | pub gt_token: Option<Token![>]>, |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 23 | pub where_clause: Option<WhereClause>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 24 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 25 | } |
| 26 | |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 27 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 28 | /// A generic type parameter, lifetime, or const generic: `T: Into<String>`, |
| 29 | /// `'a: 'b`, `const LEN: usize`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 30 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 31 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 32 | /// feature.* |
| 33 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 34 | /// # Syntax tree enum |
| 35 | /// |
| 36 | /// This type is a [syntax tree enum]. |
| 37 | /// |
| 38 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 39 | pub enum GenericParam { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 40 | /// A generic type parameter: `T: Into<String>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 41 | /// |
| 42 | /// *This type is available if Syn is built with the `"derive"` or |
| 43 | /// `"full"` feature.* |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 44 | pub Type(TypeParam { |
| 45 | pub attrs: Vec<Attribute>, |
| 46 | pub ident: Ident, |
| 47 | pub colon_token: Option<Token![:]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 48 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 49 | pub eq_token: Option<Token![=]>, |
| 50 | pub default: Option<Type>, |
| 51 | }), |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 52 | |
| 53 | /// A lifetime definition: `'a: 'b + 'c + 'd`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 54 | /// |
| 55 | /// *This type is available if Syn is built with the `"derive"` or |
| 56 | /// `"full"` feature.* |
David Tolnay | 517f369 | 2018-01-01 20:17:23 -0800 | [diff] [blame] | 57 | pub Lifetime(LifetimeDef { |
| 58 | pub attrs: Vec<Attribute>, |
| 59 | pub lifetime: Lifetime, |
| 60 | pub colon_token: Option<Token![:]>, |
| 61 | pub bounds: Punctuated<Lifetime, Token![+]>, |
| 62 | }), |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 63 | |
| 64 | /// A const generic parameter: `const LENGTH: usize`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 65 | /// |
| 66 | /// *This type is available if Syn is built with the `"derive"` or |
| 67 | /// `"full"` feature.* |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 68 | pub Const(ConstParam { |
| 69 | pub attrs: Vec<Attribute>, |
| 70 | pub const_token: Token![const], |
| 71 | pub ident: Ident, |
| 72 | pub colon_token: Token![:], |
| 73 | pub ty: Type, |
| 74 | pub eq_token: Option<Token![=]>, |
| 75 | pub default: Option<Expr>, |
| 76 | }), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 80 | /// Returned by `Generics::split_for_impl`. |
| 81 | /// |
| 82 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 83 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 84 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 85 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 86 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 87 | pub struct ImplGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 88 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 89 | /// Returned by `Generics::split_for_impl`. |
| 90 | /// |
| 91 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 92 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 93 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 94 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 95 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 96 | pub struct TypeGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 97 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 98 | /// Returned by `TypeGenerics::as_turbofish`. |
| 99 | /// |
| 100 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 101 | /// feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 102 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 103 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 104 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 105 | pub struct Turbofish<'a>(&'a Generics); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 106 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 107 | #[cfg(feature = "printing")] |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 108 | impl Generics { |
| 109 | /// Split a type's generics into the pieces required for impl'ing a trait |
| 110 | /// for that type. |
| 111 | /// |
| 112 | /// ``` |
| 113 | /// # extern crate syn; |
| 114 | /// # #[macro_use] |
| 115 | /// # extern crate quote; |
| 116 | /// # fn main() { |
| 117 | /// # let generics: syn::Generics = Default::default(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 118 | /// # let name = syn::Ident::from("MyType"); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 119 | /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 120 | /// quote! { |
| 121 | /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { |
| 122 | /// // ... |
| 123 | /// } |
| 124 | /// } |
| 125 | /// # ; |
| 126 | /// # } |
| 127 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 128 | /// |
| 129 | /// *This method is available if Syn is built with the `"derive"` or |
| 130 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 131 | pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 132 | ( |
| 133 | ImplGenerics(self), |
| 134 | TypeGenerics(self), |
| 135 | self.where_clause.as_ref(), |
| 136 | ) |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 140 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 141 | impl<'a> TypeGenerics<'a> { |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 142 | /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 143 | /// |
| 144 | /// *This method is available if Syn is built with the `"derive"` or |
| 145 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 146 | pub fn as_turbofish(&self) -> Turbofish { |
| 147 | Turbofish(self.0) |
| 148 | } |
| 149 | } |
| 150 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 151 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 152 | /// A set of bound lifetimes: `for<'a, 'b, 'c>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 153 | /// |
| 154 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 155 | /// feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 156 | #[derive(Default)] |
| 157 | pub struct BoundLifetimes { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 158 | pub for_token: Token![for], |
| 159 | pub lt_token: Token![<], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 160 | pub lifetimes: Punctuated<LifetimeDef, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 161 | pub gt_token: Token![>], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 165 | impl LifetimeDef { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 166 | pub fn new(lifetime: Lifetime) -> Self { |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 167 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 168 | attrs: Vec::new(), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 169 | lifetime: lifetime, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 170 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 171 | bounds: Punctuated::new(), |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 176 | impl From<Ident> for TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 177 | fn from(ident: Ident) -> Self { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 178 | TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 179 | attrs: vec![], |
| 180 | ident: ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 181 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 182 | bounds: Punctuated::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | eq_token: None, |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 184 | default: None, |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 189 | ast_enum_of_structs! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 190 | /// A trait or lifetime used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 191 | /// |
| 192 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 193 | /// feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 194 | pub enum TypeParamBound { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 195 | pub Trait(TraitBound), |
| 196 | pub Lifetime(Lifetime), |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | ast_struct! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 201 | /// A trait used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 202 | /// |
| 203 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 204 | /// feature.* |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 205 | pub struct TraitBound { |
| 206 | pub modifier: TraitBoundModifier, |
| 207 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 208 | pub lifetimes: Option<BoundLifetimes>, |
| 209 | /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>` |
| 210 | pub path: Path, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 211 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 214 | ast_enum! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 215 | /// A modifier on a trait bound, currently only used for the `?` in |
| 216 | /// `?Sized`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 217 | /// |
| 218 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 219 | /// feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 220 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 221 | pub enum TraitBoundModifier { |
| 222 | None, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 223 | Maybe(Token![?]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 224 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 227 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 228 | /// A `where` clause in a definition: `where T: Deserialize<'de>, D: |
| 229 | /// 'static`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 230 | /// |
| 231 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 232 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 233 | pub struct WhereClause { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 234 | pub where_token: Token![where], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 235 | pub predicates: Punctuated<WherePredicate, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 236 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 239 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 240 | /// A single predicate in a `where` clause: `T: Deserialize<'de>`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 241 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 242 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 243 | /// feature.* |
| 244 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 245 | /// # Syntax tree enum |
| 246 | /// |
| 247 | /// This type is a [syntax tree enum]. |
| 248 | /// |
| 249 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 250 | pub enum WherePredicate { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 251 | /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 252 | /// |
| 253 | /// *This type is available if Syn is built with the `"derive"` or |
| 254 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 255 | pub Type(PredicateType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 256 | /// Any lifetimes from a `for` binding |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 257 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 258 | /// The type being bounded |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 259 | pub bounded_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 260 | pub colon_token: Token![:], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 261 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 262 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 263 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 264 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 265 | /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 266 | /// |
| 267 | /// *This type is available if Syn is built with the `"derive"` or |
| 268 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 269 | pub Lifetime(PredicateLifetime { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 270 | pub lifetime: Lifetime, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 271 | pub colon_token: Option<Token![:]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 272 | pub bounds: Punctuated<Lifetime, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 273 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 274 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 275 | /// An equality predicate in a `where` clause (unsupported). |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 276 | /// |
| 277 | /// *This type is available if Syn is built with the `"derive"` or |
| 278 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 279 | pub Eq(PredicateEq { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 280 | pub lhs_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 281 | pub eq_token: Token![=], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 282 | pub rhs_ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | }), |
| 284 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 285 | } |
| 286 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 287 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 288 | pub mod parsing { |
| 289 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 290 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 291 | use synom::Synom; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 292 | use punctuated::Pair; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 293 | |
| 294 | impl Synom for Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 295 | named!(parse -> Self, map!( |
| 296 | alt!( |
| 297 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 298 | lt: punct!(<) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 299 | lifetimes: call!(Punctuated::<LifetimeDef, Token![,]>::parse_terminated) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 300 | ty_params: cond!( |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 301 | lifetimes.empty_or_trailing(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 302 | Punctuated::<TypeParam, Token![,]>::parse_terminated |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 303 | ) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 304 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 305 | (lifetimes, ty_params, Some(lt), Some(gt)) |
| 306 | ) |
| 307 | | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 308 | epsilon!() => { |_| (Punctuated::new(), None, None, None) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 309 | ), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 310 | |(lifetimes, ty_params, lt, gt)| Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 311 | lt_token: lt, |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 312 | params: lifetimes.into_pairs() |
| 313 | .map(Pair::into_tuple) |
| 314 | .map(|(life, comma)| Pair::new(GenericParam::Lifetime(life), comma)) |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 315 | .chain(ty_params.unwrap_or_default() |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 316 | .into_pairs() |
| 317 | .map(Pair::into_tuple) |
| 318 | .map(|(ty, comma)| Pair::new(GenericParam::Type(ty), comma))) |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 319 | .collect(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 320 | gt_token: gt, |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 321 | where_clause: None, |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 322 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 323 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 324 | |
| 325 | fn description() -> Option<&'static str> { |
| 326 | Some("generic parameters in declaration") |
| 327 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 328 | } |
| 329 | |
David Tolnay | 36454bb | 2018-01-07 22:40:02 -0800 | [diff] [blame^] | 330 | impl Synom for GenericParam { |
| 331 | named!(parse -> Self, alt!( |
| 332 | syn!(TypeParam) => { GenericParam::Type } |
| 333 | | |
| 334 | syn!(LifetimeDef) => { GenericParam::Lifetime } |
| 335 | | |
| 336 | syn!(ConstParam) => { GenericParam::Const } |
| 337 | )); |
| 338 | |
| 339 | fn description() -> Option<&'static str> { |
| 340 | Some("generic parameter") |
| 341 | } |
| 342 | } |
| 343 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 344 | impl Synom for LifetimeDef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 345 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 346 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 347 | life: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 348 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 349 | bounds: cond!( |
| 350 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 351 | Punctuated::parse_separated_nonempty |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 352 | ) >> |
| 353 | (LifetimeDef { |
| 354 | attrs: attrs, |
| 355 | lifetime: life, |
| 356 | bounds: bounds.unwrap_or_default(), |
David Tolnay | 6af8f1d | 2017-12-27 23:08:43 -0500 | [diff] [blame] | 357 | colon_token: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 358 | }) |
| 359 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 360 | |
| 361 | fn description() -> Option<&'static str> { |
| 362 | Some("lifetime definition") |
| 363 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | impl Synom for BoundLifetimes { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 367 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 368 | for_: keyword!(for) >> |
| 369 | lt: punct!(<) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 370 | lifetimes: call!(Punctuated::parse_terminated) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 371 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 372 | (BoundLifetimes { |
| 373 | for_token: for_, |
| 374 | lt_token: lt, |
| 375 | gt_token: gt, |
| 376 | lifetimes: lifetimes, |
| 377 | }) |
| 378 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 379 | |
| 380 | fn description() -> Option<&'static str> { |
| 381 | Some("bound lifetimes") |
| 382 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 383 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 384 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 385 | impl Synom for TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 386 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 387 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 388 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 389 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 390 | bounds: cond!( |
| 391 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 392 | Punctuated::parse_separated_nonempty |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 393 | ) >> |
| 394 | default: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 395 | eq: punct!(=) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 396 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 397 | (eq, ty) |
| 398 | )) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 399 | (TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 400 | attrs: attrs, |
| 401 | ident: id, |
| 402 | bounds: bounds.unwrap_or_default(), |
| 403 | colon_token: colon, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 404 | eq_token: default.as_ref().map(|d| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 405 | default: default.map(|d| d.1), |
| 406 | }) |
| 407 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 408 | |
| 409 | fn description() -> Option<&'static str> { |
| 410 | Some("type parameter") |
| 411 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 412 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 413 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 414 | impl Synom for TypeParamBound { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 415 | named!(parse -> Self, alt!( |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 416 | syn!(Lifetime) => { TypeParamBound::Lifetime } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 417 | | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 418 | syn!(TraitBound) => { TypeParamBound::Trait } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 419 | | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 420 | parens!(syn!(TraitBound)) => { |bound| TypeParamBound::Trait(bound.1) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 421 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 422 | |
| 423 | fn description() -> Option<&'static str> { |
Nika Layzell | c691cb4 | 2017-12-04 13:44:38 -0500 | [diff] [blame] | 424 | Some("type parameter bound") |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 428 | impl Synom for TraitBound { |
| 429 | named!(parse -> Self, do_parse!( |
| 430 | modifier: syn!(TraitBoundModifier) >> |
| 431 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 432 | mut path: syn!(Path) >> |
| 433 | parenthesized: option!(cond_reduce!( |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 434 | path.segments.last().unwrap().value().arguments.is_empty(), |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 435 | syn!(ParenthesizedGenericArguments) |
| 436 | )) >> |
| 437 | ({ |
| 438 | if let Some(parenthesized) = parenthesized { |
| 439 | let parenthesized = PathArguments::Parenthesized(parenthesized); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 440 | path.segments.last_mut().unwrap().value_mut().arguments = parenthesized; |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 441 | } |
| 442 | TraitBound { |
| 443 | modifier: modifier, |
| 444 | lifetimes: lifetimes, |
| 445 | path: path, |
| 446 | } |
| 447 | }) |
| 448 | )); |
| 449 | |
| 450 | fn description() -> Option<&'static str> { |
| 451 | Some("trait bound") |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | impl Synom for TraitBoundModifier { |
| 456 | named!(parse -> Self, alt!( |
| 457 | punct!(?) => { TraitBoundModifier::Maybe } |
| 458 | | |
| 459 | epsilon!() => { |_| TraitBoundModifier::None } |
| 460 | )); |
| 461 | |
| 462 | fn description() -> Option<&'static str> { |
| 463 | Some("trait bound modifier") |
| 464 | } |
| 465 | } |
| 466 | |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 467 | impl Synom for ConstParam { |
| 468 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 469 | attrs: many0!(Attribute::parse_outer) >> |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 470 | const_: keyword!(const) >> |
| 471 | ident: syn!(Ident) >> |
| 472 | colon: punct!(:) >> |
| 473 | ty: syn!(Type) >> |
| 474 | eq_def: option!(tuple!(punct!(=), syn!(Expr))) >> |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 475 | ({ |
| 476 | let (eq_token, default) = match eq_def { |
| 477 | Some((eq_token, default)) => (Some(eq_token), Some(default)), |
| 478 | None => (None, None), |
| 479 | }; |
| 480 | ConstParam { |
| 481 | attrs: attrs, |
| 482 | const_token: const_, |
| 483 | ident: ident, |
| 484 | colon_token: colon, |
| 485 | ty: ty, |
| 486 | eq_token: eq_token, |
| 487 | default: default, |
| 488 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 489 | }) |
| 490 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 491 | |
| 492 | fn description() -> Option<&'static str> { |
| 493 | Some("generic `const` parameter") |
| 494 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 497 | impl Synom for WhereClause { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 498 | named!(parse -> Self, do_parse!( |
| 499 | where_: keyword!(where) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 500 | predicates: call!(Punctuated::parse_terminated) >> |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 501 | (WhereClause { |
| 502 | predicates: predicates, |
| 503 | where_token: where_, |
| 504 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 505 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 506 | |
| 507 | fn description() -> Option<&'static str> { |
| 508 | Some("where clause") |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | impl Synom for WherePredicate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 513 | named!(parse -> Self, alt!( |
| 514 | do_parse!( |
| 515 | ident: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 516 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 517 | bounds: cond!( |
| 518 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 519 | Punctuated::parse_separated |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 520 | ) >> |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 521 | (WherePredicate::Lifetime(PredicateLifetime { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 522 | lifetime: ident, |
| 523 | bounds: bounds.unwrap_or_default(), |
| 524 | colon_token: colon, |
| 525 | })) |
| 526 | ) |
| 527 | | |
| 528 | do_parse!( |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 529 | lifetimes: option!(syn!(BoundLifetimes)) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 530 | bounded_ty: syn!(Type) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 531 | colon: punct!(:) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 532 | bounds: call!(Punctuated::parse_separated_nonempty) >> |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 533 | (WherePredicate::Type(PredicateType { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 534 | lifetimes: lifetimes, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 535 | bounded_ty: bounded_ty, |
| 536 | bounds: bounds, |
| 537 | colon_token: colon, |
| 538 | })) |
| 539 | ) |
| 540 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 541 | |
| 542 | fn description() -> Option<&'static str> { |
| 543 | Some("predicate in where clause") |
| 544 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 545 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 546 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 547 | |
| 548 | #[cfg(feature = "printing")] |
| 549 | mod printing { |
| 550 | use super::*; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 551 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 552 | use quote::{ToTokens, Tokens}; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 553 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 554 | /// Returns true if the generics object has no lifetimes or ty_params. |
| 555 | fn empty_normal_generics(generics: &Generics) -> bool { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 556 | generics.params.is_empty() |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 557 | } |
| 558 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 559 | impl ToTokens for Generics { |
| 560 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 561 | if empty_normal_generics(self) { |
| 562 | return; |
| 563 | } |
| 564 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 565 | TokensOrDefault(&self.lt_token).to_tokens(tokens); |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 566 | self.params.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 567 | TokensOrDefault(&self.gt_token).to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 571 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 572 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 573 | if empty_normal_generics(self.0) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 577 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 578 | for param in self.0.params.pairs() { |
| 579 | match **param.value() { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 580 | GenericParam::Lifetime(ref param) => { |
| 581 | param.to_tokens(tokens); |
| 582 | } |
| 583 | GenericParam::Type(ref param) => { |
| 584 | // Leave off the type parameter defaults |
| 585 | tokens.append_all(param.attrs.outer()); |
| 586 | param.ident.to_tokens(tokens); |
| 587 | if !param.bounds.is_empty() { |
| 588 | TokensOrDefault(¶m.colon_token).to_tokens(tokens); |
| 589 | param.bounds.to_tokens(tokens); |
| 590 | } |
| 591 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 592 | GenericParam::Const(ref param) => { |
| 593 | // Leave off the const parameter defaults |
| 594 | tokens.append_all(param.attrs.outer()); |
| 595 | param.const_token.to_tokens(tokens); |
| 596 | param.ident.to_tokens(tokens); |
| 597 | param.colon_token.to_tokens(tokens); |
| 598 | param.ty.to_tokens(tokens); |
| 599 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 600 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 601 | param.punct().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 602 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 603 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 607 | impl<'a> ToTokens for TypeGenerics<'a> { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 608 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 609 | if empty_normal_generics(self.0) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 613 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 614 | for param in self.0.params.pairs() { |
| 615 | match **param.value() { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 616 | GenericParam::Lifetime(ref param) => { |
| 617 | // Leave off the lifetime bounds and attributes |
| 618 | param.lifetime.to_tokens(tokens); |
| 619 | } |
| 620 | GenericParam::Type(ref param) => { |
| 621 | // Leave off the type parameter defaults |
| 622 | param.ident.to_tokens(tokens); |
| 623 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 624 | GenericParam::Const(ref param) => { |
| 625 | // Leave off the const parameter defaults |
| 626 | param.ident.to_tokens(tokens); |
| 627 | } |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 628 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 629 | param.punct().to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 630 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 631 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 635 | impl<'a> ToTokens for Turbofish<'a> { |
| 636 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 637 | if !empty_normal_generics(self.0) { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 638 | <Token![::]>::default().to_tokens(tokens); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 639 | TypeGenerics(self.0).to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 644 | impl ToTokens for BoundLifetimes { |
| 645 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 646 | self.for_token.to_tokens(tokens); |
| 647 | self.lt_token.to_tokens(tokens); |
| 648 | self.lifetimes.to_tokens(tokens); |
| 649 | self.gt_token.to_tokens(tokens); |
| 650 | } |
| 651 | } |
| 652 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 653 | impl ToTokens for LifetimeDef { |
| 654 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 655 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 656 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 657 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 658 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 659 | self.bounds.to_tokens(tokens); |
| 660 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 664 | impl ToTokens for TypeParam { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 665 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 666 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 667 | self.ident.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 668 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 669 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 670 | self.bounds.to_tokens(tokens); |
| 671 | } |
| 672 | if self.default.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 673 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 674 | self.default.to_tokens(tokens); |
| 675 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 679 | impl ToTokens for TraitBound { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 680 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 681 | self.modifier.to_tokens(tokens); |
| 682 | self.lifetimes.to_tokens(tokens); |
| 683 | self.path.to_tokens(tokens); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 687 | impl ToTokens for TraitBoundModifier { |
| 688 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 689 | match *self { |
| 690 | TraitBoundModifier::None => {} |
| 691 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 696 | impl ToTokens for ConstParam { |
| 697 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 698 | tokens.append_all(self.attrs.outer()); |
| 699 | self.const_token.to_tokens(tokens); |
| 700 | self.ident.to_tokens(tokens); |
| 701 | self.colon_token.to_tokens(tokens); |
| 702 | self.ty.to_tokens(tokens); |
| 703 | if self.default.is_some() { |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 704 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 705 | self.default.to_tokens(tokens); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 710 | impl ToTokens for WhereClause { |
| 711 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 712 | self.where_token.to_tokens(tokens); |
| 713 | self.predicates.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 716 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 717 | impl ToTokens for PredicateType { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 718 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 719 | self.lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 720 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 721 | self.colon_token.to_tokens(tokens); |
| 722 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 726 | impl ToTokens for PredicateLifetime { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 727 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 728 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 729 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 730 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 731 | self.bounds.to_tokens(tokens); |
| 732 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 735 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 736 | impl ToTokens for PredicateEq { |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 737 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 738 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 739 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 740 | self.rhs_ty.to_tokens(tokens); |
| 741 | } |
| 742 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 743 | } |