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