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