David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 4 | pub struct Generics { |
| 5 | pub lifetimes: Vec<LifetimeDef>, |
| 6 | pub ty_params: Vec<TyParam>, |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 7 | pub where_clause: WhereClause, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 11 | pub struct Lifetime { |
| 12 | pub ident: Ident, |
| 13 | } |
| 14 | |
| 15 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 16 | pub struct LifetimeDef { |
| 17 | pub lifetime: Lifetime, |
| 18 | pub bounds: Vec<Lifetime>, |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 22 | pub struct TyParam { |
| 23 | pub ident: Ident, |
| 24 | pub bounds: Vec<TyParamBound>, |
| 25 | pub default: Option<Ty>, |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 29 | pub enum TyParamBound { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 30 | Trait(PolyTraitRef, TraitBoundModifier), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 31 | Region(Lifetime), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 35 | pub enum TraitBoundModifier { |
| 36 | None, |
| 37 | Maybe, |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 41 | pub struct WhereClause { |
| 42 | pub predicates: Vec<WherePredicate>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /// A single predicate in a `where` clause |
| 46 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 47 | pub enum WherePredicate { |
| 48 | /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c` |
| 49 | BoundPredicate(WhereBoundPredicate), |
| 50 | /// A lifetime predicate, e.g. `'a: 'b+'c` |
| 51 | RegionPredicate(WhereRegionPredicate), |
| 52 | } |
| 53 | |
| 54 | /// A type bound. |
| 55 | /// |
| 56 | /// E.g. `for<'c> Foo: Send+Clone+'c` |
| 57 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 58 | pub struct WhereBoundPredicate { |
| 59 | /// Any lifetimes from a `for` binding |
| 60 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 61 | /// The type being bounded |
| 62 | pub bounded_ty: Ty, |
| 63 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
| 64 | pub bounds: Vec<TyParamBound>, |
| 65 | } |
| 66 | |
| 67 | /// A lifetime predicate. |
| 68 | /// |
| 69 | /// E.g. `'a: 'b+'c` |
| 70 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 71 | pub struct WhereRegionPredicate { |
| 72 | pub lifetime: Lifetime, |
| 73 | pub bounds: Vec<Lifetime>, |
| 74 | } |
| 75 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 76 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 77 | pub mod parsing { |
| 78 | use super::*; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 79 | use ident::parsing::ident; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 80 | use ty::parsing::{ty, poly_trait_ref}; |
| 81 | use nom::multispace; |
| 82 | |
| 83 | named!(pub generics<&str, Generics>, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 84 | bracketed: alt_complete!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 85 | do_parse!( |
| 86 | punct!("<") >> |
| 87 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
| 88 | ty_params: opt_vec!(preceded!( |
| 89 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 90 | separated_nonempty_list!(punct!(","), ty_param) |
| 91 | )) >> |
| 92 | punct!(">") >> |
| 93 | (lifetimes, ty_params) |
| 94 | ) |
| 95 | | |
| 96 | epsilon!() => { |_| (Vec::new(), Vec::new()) } |
| 97 | ) >> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 98 | where_clause: where_clause >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 99 | (Generics { |
| 100 | lifetimes: bracketed.0, |
| 101 | ty_params: bracketed.1, |
| 102 | where_clause: where_clause, |
| 103 | }) |
| 104 | )); |
| 105 | |
| 106 | named!(pub lifetime<&str, Lifetime>, preceded!( |
| 107 | punct!("'"), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 108 | map!(ident, |id| Lifetime { |
| 109 | ident: format!("'{}", id).into(), |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 110 | }) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 111 | )); |
| 112 | |
| 113 | named!(pub lifetime_def<&str, LifetimeDef>, do_parse!( |
| 114 | life: lifetime >> |
| 115 | bounds: opt_vec!(preceded!( |
| 116 | punct!(":"), |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 117 | separated_nonempty_list!(punct!("+"), lifetime) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 118 | )) >> |
| 119 | (LifetimeDef { |
| 120 | lifetime: life, |
| 121 | bounds: bounds, |
| 122 | }) |
| 123 | )); |
| 124 | |
| 125 | named!(pub bound_lifetimes<&str, Vec<LifetimeDef> >, opt_vec!(do_parse!( |
| 126 | punct!("for") >> |
| 127 | punct!("<") >> |
| 128 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
| 129 | punct!(">") >> |
| 130 | (lifetimes) |
| 131 | ))); |
| 132 | |
| 133 | named!(ty_param<&str, TyParam>, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 134 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 135 | bounds: opt_vec!(preceded!( |
| 136 | punct!(":"), |
| 137 | separated_nonempty_list!(punct!("+"), ty_param_bound) |
| 138 | )) >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 139 | default: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 140 | punct!("="), |
| 141 | ty |
| 142 | )) >> |
| 143 | (TyParam { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 144 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 145 | bounds: bounds, |
| 146 | default: default, |
| 147 | }) |
| 148 | )); |
| 149 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 150 | named!(pub ty_param_bound<&str, TyParamBound>, alt_complete!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 151 | preceded!(punct!("?"), poly_trait_ref) => { |
| 152 | |poly| TyParamBound::Trait(poly, TraitBoundModifier::Maybe) |
| 153 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 154 | | |
| 155 | lifetime => { TyParamBound::Region } |
| 156 | | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 157 | poly_trait_ref => { |
| 158 | |poly| TyParamBound::Trait(poly, TraitBoundModifier::None) |
| 159 | } |
| 160 | )); |
| 161 | |
| 162 | named!(pub where_clause<&str, WhereClause>, alt_complete!( |
| 163 | do_parse!( |
| 164 | punct!("where") >> |
| 165 | multispace >> |
| 166 | predicates: separated_nonempty_list!(punct!(","), where_predicate) >> |
| 167 | option!(punct!(",")) >> |
| 168 | (WhereClause { predicates: predicates }) |
| 169 | ) |
| 170 | | |
| 171 | epsilon!() => { |_| Default::default() } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 172 | )); |
| 173 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 174 | named!(where_predicate<&str, WherePredicate>, alt_complete!( |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 175 | do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 176 | ident: lifetime >> |
| 177 | punct!(":") >> |
| 178 | bounds: separated_nonempty_list!(punct!("+"), lifetime) >> |
| 179 | (WherePredicate::RegionPredicate(WhereRegionPredicate { |
| 180 | lifetime: ident, |
| 181 | bounds: bounds, |
| 182 | })) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 183 | ) |
| 184 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 185 | do_parse!( |
| 186 | bound_lifetimes: bound_lifetimes >> |
| 187 | bounded_ty: ty >> |
| 188 | punct!(":") >> |
| 189 | bounds: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 190 | (WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 191 | bound_lifetimes: bound_lifetimes, |
| 192 | bounded_ty: bounded_ty, |
| 193 | bounds: bounds, |
| 194 | })) |
| 195 | ) |
| 196 | )); |
| 197 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 198 | |
| 199 | #[cfg(feature = "printing")] |
| 200 | mod printing { |
| 201 | use super::*; |
| 202 | use quote::{Tokens, ToTokens}; |
| 203 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 204 | impl ToTokens for Generics { |
| 205 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 206 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 207 | let has_ty_params = !self.ty_params.is_empty(); |
| 208 | if has_lifetimes || has_ty_params { |
| 209 | tokens.append("<"); |
| 210 | tokens.append_separated(&self.lifetimes, ","); |
| 211 | if has_lifetimes && has_ty_params { |
| 212 | tokens.append(","); |
| 213 | } |
| 214 | tokens.append_separated(&self.ty_params, ","); |
| 215 | tokens.append(">"); |
| 216 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 220 | impl ToTokens for Lifetime { |
| 221 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 222 | self.ident.to_tokens(tokens); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl ToTokens for LifetimeDef { |
| 227 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 228 | self.lifetime.to_tokens(tokens); |
| 229 | if !self.bounds.is_empty() { |
| 230 | tokens.append(":"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 231 | tokens.append_separated(&self.bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 236 | impl ToTokens for TyParam { |
| 237 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 238 | self.ident.to_tokens(tokens); |
| 239 | if !self.bounds.is_empty() { |
| 240 | tokens.append(":"); |
David Tolnay | 686b59b | 2016-09-04 14:39:50 -0700 | [diff] [blame] | 241 | tokens.append_separated(&self.bounds, "+"); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 242 | } |
| 243 | if let Some(ref default) = self.default { |
| 244 | tokens.append("="); |
| 245 | default.to_tokens(tokens); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 250 | impl ToTokens for TyParamBound { |
| 251 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 252 | match *self { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 253 | TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 254 | TyParamBound::Trait(ref trait_ref, modifier) => { |
| 255 | match modifier { |
| 256 | TraitBoundModifier::None => {} |
| 257 | TraitBoundModifier::Maybe => tokens.append("?"), |
| 258 | } |
| 259 | trait_ref.to_tokens(tokens); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl ToTokens for WhereClause { |
| 266 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 267 | if !self.predicates.is_empty() { |
| 268 | tokens.append("where"); |
| 269 | tokens.append_separated(&self.predicates, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 273 | |
| 274 | impl ToTokens for WherePredicate { |
| 275 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 276 | match *self { |
| 277 | WherePredicate::BoundPredicate(ref predicate) => { |
| 278 | predicate.to_tokens(tokens); |
| 279 | } |
| 280 | WherePredicate::RegionPredicate(ref predicate) => { |
| 281 | predicate.to_tokens(tokens); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | impl ToTokens for WhereBoundPredicate { |
| 288 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 289 | if !self.bound_lifetimes.is_empty() { |
| 290 | tokens.append("for"); |
| 291 | tokens.append("<"); |
| 292 | tokens.append_separated(&self.bound_lifetimes, ","); |
| 293 | tokens.append(">"); |
| 294 | } |
| 295 | self.bounded_ty.to_tokens(tokens); |
| 296 | if !self.bounds.is_empty() { |
| 297 | tokens.append(":"); |
| 298 | tokens.append_separated(&self.bounds, "+"); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | impl ToTokens for WhereRegionPredicate { |
| 304 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 305 | self.lifetime.to_tokens(tokens); |
| 306 | if !self.bounds.is_empty() { |
| 307 | tokens.append(":"); |
| 308 | tokens.append_separated(&self.bounds, "+"); |
| 309 | } |
| 310 | } |
| 311 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 312 | } |