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