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 | /// The different kinds of types recognized by the compiler |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 4 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 5 | pub enum Ty { |
| 6 | /// A variable-length array (`[T]`) |
| 7 | Vec(Box<Ty>), |
| 8 | /// A fixed length array (`[T; n]`) |
| 9 | FixedLengthVec(Box<Ty>, usize), |
| 10 | /// A raw pointer (`*const T` or `*mut T`) |
| 11 | Ptr(Box<MutTy>), |
| 12 | /// A reference (`&'a T` or `&'a mut T`) |
| 13 | Rptr(Option<Lifetime>, Box<MutTy>), |
| 14 | /// A bare function (e.g. `fn(usize) -> bool`) |
| 15 | BareFn(Box<BareFnTy>), |
| 16 | /// The never type (`!`) |
| 17 | Never, |
| 18 | /// A tuple (`(A, B, C, D, ...)`) |
| 19 | Tup(Vec<Ty>), |
| 20 | /// A path (`module::module::...::Type`), optionally |
| 21 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 22 | /// |
| 23 | /// Type parameters are stored in the Path itself |
| 24 | Path(Option<QSelf>, Path), |
| 25 | /// Something like `A+B`. Note that `B` must always be a path. |
| 26 | ObjectSum(Box<Ty>, Vec<TyParamBound>), |
| 27 | /// A type like `for<'a> Foo<&'a Bar>` |
| 28 | PolyTraitRef(Vec<TyParamBound>), |
| 29 | /// An `impl TraitA+TraitB` type. |
| 30 | ImplTrait(Vec<TyParamBound>), |
| 31 | /// No-op; kept solely so that we can pretty-print faithfully |
| 32 | Paren(Box<Ty>), |
| 33 | /// TyKind::Infer means the type should be inferred instead of it having been |
| 34 | /// specified. This can appear anywhere in a type. |
| 35 | Infer, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 39 | pub struct MutTy { |
| 40 | pub ty: Ty, |
| 41 | pub mutability: Mutability, |
| 42 | } |
| 43 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 44 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 45 | pub enum Mutability { |
| 46 | Mutable, |
| 47 | Immutable, |
| 48 | } |
| 49 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 50 | /// A "Path" is essentially Rust's notion of a name. |
| 51 | /// |
| 52 | /// It's represented as a sequence of identifiers, |
| 53 | /// along with a bunch of supporting information. |
| 54 | /// |
| 55 | /// E.g. `std::cmp::PartialEq` |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 56 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 57 | pub struct Path { |
| 58 | pub global: bool, |
| 59 | pub segments: Vec<PathSegment>, |
| 60 | } |
| 61 | |
| 62 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 63 | /// |
| 64 | /// E.g. `std`, `String` or `Box<T>` |
| 65 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 66 | pub struct PathSegment { |
| 67 | pub ident: Ident, |
| 68 | pub parameters: PathParameters, |
| 69 | } |
| 70 | |
| 71 | impl PathSegment { |
| 72 | pub fn ident(ident: Ident) -> Self { |
| 73 | PathSegment { |
| 74 | ident: ident, |
| 75 | parameters: PathParameters::none(), |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Parameters of a path segment. |
| 81 | /// |
| 82 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
| 83 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 84 | pub enum PathParameters { |
| 85 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
| 86 | AngleBracketed(AngleBracketedParameterData), |
| 87 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
| 88 | Parenthesized(ParenthesizedParameterData), |
| 89 | } |
| 90 | |
| 91 | impl PathParameters { |
| 92 | pub fn none() -> Self { |
| 93 | PathParameters::AngleBracketed(AngleBracketedParameterData::default()) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// A path like `Foo<'a, T>` |
| 98 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 99 | pub struct AngleBracketedParameterData { |
| 100 | /// The lifetime parameters for this path segment. |
| 101 | pub lifetimes: Vec<Lifetime>, |
| 102 | /// The type parameters for this path segment, if present. |
| 103 | pub types: Vec<Ty>, |
| 104 | /// Bindings (equality constraints) on associated types, if present. |
| 105 | /// |
| 106 | /// E.g., `Foo<A=Bar>`. |
| 107 | pub bindings: Vec<TypeBinding>, |
| 108 | } |
| 109 | |
| 110 | /// Bind a type to an associated type: `A=Foo`. |
| 111 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 112 | pub struct TypeBinding { |
| 113 | pub ident: Ident, |
| 114 | pub ty: Ty, |
| 115 | } |
| 116 | |
| 117 | /// A path like `Foo(A,B) -> C` |
| 118 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 119 | pub struct ParenthesizedParameterData { |
| 120 | /// `(A, B)` |
| 121 | pub inputs: Vec<Ty>, |
| 122 | /// `C` |
| 123 | pub output: Option<Ty>, |
| 124 | } |
| 125 | |
| 126 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 127 | pub struct PolyTraitRef { |
| 128 | /// The `'a` in `<'a> Foo<&'a T>` |
| 129 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 130 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
| 131 | pub trait_ref: Path, |
| 132 | } |
| 133 | |
| 134 | /// The explicit Self type in a "qualified path". The actual |
| 135 | /// path, including the trait and the associated item, is stored |
| 136 | /// separately. `position` represents the index of the associated |
| 137 | /// item qualified with this Self type. |
| 138 | /// |
| 139 | /// ```rust,ignore |
| 140 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 141 | /// ^~~~~ ~~~~~~~~~~~~~~^ |
| 142 | /// ty position = 3 |
| 143 | /// |
| 144 | /// <Vec<T>>::AssociatedItem |
| 145 | /// ^~~~~ ^ |
| 146 | /// ty position = 0 |
| 147 | /// ``` |
| 148 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 149 | pub struct QSelf { |
| 150 | pub ty: Box<Ty>, |
| 151 | pub position: usize |
| 152 | } |
| 153 | |
| 154 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 155 | pub struct BareFnTy { |
| 156 | pub lifetimes: Vec<LifetimeDef>, |
| 157 | pub decl: FnDecl |
| 158 | } |
| 159 | |
| 160 | /// Header (not the body) of a function declaration. |
| 161 | /// |
| 162 | /// E.g. `fn foo(bar: baz)` |
| 163 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 164 | pub struct FnDecl { |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame] | 165 | pub inputs: Vec<FnArg>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 166 | pub output: FunctionRetTy, |
| 167 | } |
| 168 | |
| 169 | /// An argument in a function header. |
| 170 | /// |
| 171 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 172 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame] | 173 | pub struct FnArg { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 174 | pub pat: Option<Ident>, |
| 175 | pub ty: Ty, |
| 176 | } |
| 177 | |
| 178 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 179 | pub enum FunctionRetTy { |
| 180 | /// Return type is not specified. |
| 181 | /// |
| 182 | /// Functions default to `()` and |
| 183 | /// closures default to inference. Span points to where return |
| 184 | /// type would be inserted. |
| 185 | Default, |
| 186 | /// Everything else |
| 187 | Ty(Ty), |
| 188 | } |
| 189 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 190 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 191 | pub mod parsing { |
| 192 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 193 | use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 194 | use ident::parsing::ident; |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 195 | use lit::parsing::int; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 196 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 197 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 198 | named!(pub ty -> Ty, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 199 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 200 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 201 | ty_fixed_length_vec |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 202 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 203 | ty_ptr |
| 204 | | |
| 205 | ty_rptr |
| 206 | | |
| 207 | ty_bare_fn |
| 208 | | |
| 209 | ty_never |
| 210 | | |
| 211 | ty_tup |
| 212 | | |
| 213 | ty_path |
| 214 | | |
| 215 | ty_qpath |
| 216 | | |
| 217 | ty_impl_trait |
| 218 | | |
| 219 | ty_paren |
| 220 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 221 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 222 | named!(ty_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 223 | punct!("[") >> |
| 224 | elem: ty >> |
| 225 | punct!("]") >> |
| 226 | (Ty::Vec(Box::new(elem))) |
| 227 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 228 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 229 | named!(ty_fixed_length_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 230 | punct!("[") >> |
| 231 | elem: ty >> |
| 232 | punct!(";") >> |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 233 | len: int >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 234 | punct!("]") >> |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 235 | (Ty::FixedLengthVec(Box::new(elem), len.0 as usize)) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 236 | )); |
| 237 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 238 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 239 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 240 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 241 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 242 | | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 243 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 244 | ) >> |
| 245 | target: ty >> |
| 246 | (Ty::Ptr(Box::new(MutTy { |
| 247 | ty: target, |
| 248 | mutability: mutability, |
| 249 | }))) |
| 250 | )); |
| 251 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 252 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 253 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 254 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 255 | mutability: mutability >> |
| 256 | target: ty >> |
| 257 | (Ty::Rptr(life, Box::new(MutTy { |
| 258 | ty: target, |
| 259 | mutability: mutability, |
| 260 | }))) |
| 261 | )); |
| 262 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 263 | named!(ty_bare_fn -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 264 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 265 | lifetimes: opt_vec!(delimited!( |
| 266 | punct!("<"), |
| 267 | separated_list!(punct!(","), lifetime_def), |
| 268 | punct!(">") |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 269 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 270 | punct!("(") >> |
| 271 | inputs: separated_list!(punct!(","), fn_arg) >> |
| 272 | punct!(")") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 273 | output: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 274 | punct!("->"), |
| 275 | ty |
| 276 | )) >> |
| 277 | (Ty::BareFn(Box::new(BareFnTy { |
| 278 | lifetimes: lifetimes, |
| 279 | decl: FnDecl { |
| 280 | inputs: inputs, |
| 281 | output: match output { |
| 282 | Some(ty) => FunctionRetTy::Ty(ty), |
| 283 | None => FunctionRetTy::Default, |
| 284 | }, |
| 285 | }, |
| 286 | }))) |
| 287 | )); |
| 288 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 289 | named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never)); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 290 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 291 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 292 | punct!("(") >> |
| 293 | elems: separated_list!(punct!(","), ty) >> |
| 294 | punct!(")") >> |
| 295 | (Ty::Tup(elems)) |
| 296 | )); |
| 297 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 298 | named!(ty_path -> Ty, map!(path, |p| Ty::Path(None, p))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 299 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 300 | named!(ty_qpath -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 301 | punct!("<") >> |
| 302 | this: map!(ty, Box::new) >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 303 | path: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 304 | keyword!("as"), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 305 | path |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 306 | )) >> |
| 307 | punct!(">") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 308 | punct!("::") >> |
| 309 | rest: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 310 | ({ |
| 311 | match path { |
| 312 | Some(mut path) => { |
| 313 | let pos = path.segments.len(); |
| 314 | path.segments.extend(rest); |
| 315 | Ty::Path(Some(QSelf { ty: this, position: pos }), path) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 316 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 317 | None => { |
| 318 | Ty::Path(Some(QSelf { ty: this, position: 0 }), Path { |
| 319 | global: false, |
| 320 | segments: rest, |
| 321 | }) |
| 322 | } |
| 323 | } |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 324 | }) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 325 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 326 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 327 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 328 | keyword!("impl") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 329 | elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 330 | (Ty::ImplTrait(elem)) |
| 331 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 332 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 333 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 334 | punct!("(") >> |
| 335 | elem: ty >> |
| 336 | punct!(")") >> |
| 337 | (Ty::Paren(Box::new(elem))) |
| 338 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 339 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 340 | named!(pub mutability -> Mutability, alt!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 341 | do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 342 | keyword!("mut") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 343 | (Mutability::Mutable) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 344 | ) |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 345 | | |
| 346 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 347 | )); |
| 348 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 349 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 350 | global: option!(punct!("::")) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 351 | segments: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 352 | (Path { |
| 353 | global: global.is_some(), |
| 354 | segments: segments, |
| 355 | }) |
| 356 | )); |
| 357 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 358 | named!(path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 359 | do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 360 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 361 | punct!("<") >> |
| 362 | lifetimes: separated_list!(punct!(","), lifetime) >> |
| 363 | types: opt_vec!(preceded!( |
| 364 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 365 | separated_nonempty_list!( |
| 366 | punct!(","), |
| 367 | terminated!(ty, not!(peek!(punct!("=")))) |
| 368 | ) |
| 369 | )) >> |
| 370 | bindings: opt_vec!(preceded!( |
| 371 | cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")), |
| 372 | separated_nonempty_list!(punct!(","), type_binding) |
| 373 | )) >> |
| 374 | punct!(">") >> |
| 375 | (PathSegment { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 376 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 377 | parameters: PathParameters::AngleBracketed( |
| 378 | AngleBracketedParameterData { |
| 379 | lifetimes: lifetimes, |
| 380 | types: types, |
| 381 | bindings: bindings, |
| 382 | } |
| 383 | ), |
| 384 | }) |
| 385 | ) |
| 386 | | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 387 | map!(ident, PathSegment::ident) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 388 | )); |
| 389 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 390 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 391 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 392 | punct!("=") >> |
| 393 | ty: ty >> |
| 394 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 395 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 396 | ty: ty, |
| 397 | }) |
| 398 | )); |
| 399 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 400 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 401 | bound_lifetimes: bound_lifetimes >> |
| 402 | trait_ref: path >> |
| 403 | (PolyTraitRef { |
| 404 | bound_lifetimes: bound_lifetimes, |
| 405 | trait_ref: trait_ref, |
| 406 | }) |
| 407 | )); |
| 408 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 409 | named!(fn_arg -> FnArg, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 410 | pat: option!(terminated!(ident, punct!(":"))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 411 | ty: ty >> |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame] | 412 | (FnArg { |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 413 | pat: pat, |
| 414 | ty: ty, |
| 415 | }) |
| 416 | )); |
| 417 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 418 | |
| 419 | #[cfg(feature = "printing")] |
| 420 | mod printing { |
| 421 | use super::*; |
| 422 | use quote::{Tokens, ToTokens}; |
| 423 | |
| 424 | impl ToTokens for Ty { |
| 425 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 426 | match *self { |
| 427 | Ty::Vec(ref inner) => { |
| 428 | tokens.append("["); |
| 429 | inner.to_tokens(tokens); |
| 430 | tokens.append("]"); |
| 431 | } |
| 432 | Ty::FixedLengthVec(ref inner, len) => { |
| 433 | tokens.append("["); |
| 434 | inner.to_tokens(tokens); |
| 435 | tokens.append(";"); |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 436 | tokens.append(&len.to_string()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 437 | tokens.append("]"); |
| 438 | } |
| 439 | Ty::Ptr(ref target) => { |
| 440 | tokens.append("*"); |
| 441 | match target.mutability { |
| 442 | Mutability::Mutable => tokens.append("mut"), |
| 443 | Mutability::Immutable => tokens.append("const"), |
| 444 | } |
| 445 | target.ty.to_tokens(tokens); |
| 446 | } |
| 447 | Ty::Rptr(ref lifetime, ref target) => { |
| 448 | tokens.append("&"); |
| 449 | lifetime.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 450 | target.mutability.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 451 | target.ty.to_tokens(tokens); |
| 452 | } |
| 453 | Ty::BareFn(ref func) => { |
| 454 | func.to_tokens(tokens); |
| 455 | } |
| 456 | Ty::Never => { |
| 457 | tokens.append("!"); |
| 458 | } |
| 459 | Ty::Tup(ref elems) => { |
| 460 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 461 | tokens.append_separated(elems, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 462 | if elems.len() == 1 { |
| 463 | tokens.append(","); |
| 464 | } |
| 465 | tokens.append(")"); |
| 466 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 467 | Ty::Path(None, ref path) => { |
| 468 | path.to_tokens(tokens); |
| 469 | } |
| 470 | Ty::Path(Some(ref qself), ref path) => { |
| 471 | tokens.append("<"); |
| 472 | qself.ty.to_tokens(tokens); |
| 473 | if qself.position > 0 { |
| 474 | tokens.append("as"); |
| 475 | for (i, segment) in path.segments.iter() |
| 476 | .take(qself.position) |
| 477 | .enumerate() |
| 478 | { |
| 479 | if i > 0 || path.global { |
| 480 | tokens.append("::"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 481 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 482 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 483 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 484 | } |
| 485 | tokens.append(">"); |
| 486 | for segment in path.segments.iter().skip(qself.position) { |
| 487 | tokens.append("::"); |
| 488 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | Ty::ObjectSum(_, _) => unimplemented!(), |
| 492 | Ty::PolyTraitRef(_) => unimplemented!(), |
| 493 | Ty::ImplTrait(ref bounds) => { |
| 494 | tokens.append("impl"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 495 | tokens.append_separated(bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 496 | } |
| 497 | Ty::Paren(ref inner) => { |
| 498 | tokens.append("("); |
| 499 | inner.to_tokens(tokens); |
| 500 | tokens.append(")"); |
| 501 | } |
| 502 | Ty::Infer => { |
| 503 | tokens.append("_"); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 509 | impl ToTokens for Mutability { |
| 510 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 511 | if let Mutability::Mutable = *self { |
| 512 | tokens.append("mut"); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 517 | impl ToTokens for Path { |
| 518 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 519 | for (i, segment) in self.segments.iter().enumerate() { |
| 520 | if i > 0 || self.global { |
| 521 | tokens.append("::"); |
| 522 | } |
| 523 | segment.to_tokens(tokens); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | impl ToTokens for PathSegment { |
| 529 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 530 | self.ident.to_tokens(tokens); |
| 531 | self.parameters.to_tokens(tokens); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | impl ToTokens for PathParameters { |
| 536 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 537 | match *self { |
| 538 | PathParameters::AngleBracketed(ref parameters) => { |
| 539 | parameters.to_tokens(tokens); |
| 540 | } |
| 541 | PathParameters::Parenthesized(ref parameters) => { |
| 542 | parameters.to_tokens(tokens); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | impl ToTokens for AngleBracketedParameterData { |
| 549 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 550 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 551 | let has_types = !self.types.is_empty(); |
| 552 | let has_bindings = !self.bindings.is_empty(); |
| 553 | if !has_lifetimes && !has_types && !has_bindings { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | tokens.append("<"); |
| 558 | |
| 559 | let mut first = true; |
| 560 | for lifetime in &self.lifetimes { |
| 561 | if !first { |
| 562 | tokens.append(","); |
| 563 | } |
| 564 | lifetime.to_tokens(tokens); |
| 565 | first = false; |
| 566 | } |
| 567 | for ty in &self.types { |
| 568 | if !first { |
| 569 | tokens.append(","); |
| 570 | } |
| 571 | ty.to_tokens(tokens); |
| 572 | first = false; |
| 573 | } |
| 574 | for binding in &self.bindings { |
| 575 | if !first { |
| 576 | tokens.append(","); |
| 577 | } |
| 578 | binding.to_tokens(tokens); |
| 579 | first = false; |
| 580 | } |
| 581 | |
| 582 | tokens.append(">"); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | impl ToTokens for TypeBinding { |
| 587 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 588 | self.ident.to_tokens(tokens); |
| 589 | tokens.append("="); |
| 590 | self.ty.to_tokens(tokens); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | impl ToTokens for ParenthesizedParameterData { |
| 595 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 596 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 597 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 598 | tokens.append(")"); |
| 599 | if let Some(ref output) = self.output { |
| 600 | tokens.append("->"); |
| 601 | output.to_tokens(tokens); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | impl ToTokens for PolyTraitRef { |
| 607 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 608 | if !self.bound_lifetimes.is_empty() { |
David Tolnay | e8796aa | 2016-09-04 14:48:22 -0700 | [diff] [blame] | 609 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 610 | tokens.append("<"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 611 | tokens.append_separated(&self.bound_lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 612 | tokens.append(">"); |
| 613 | } |
| 614 | self.trait_ref.to_tokens(tokens); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | impl ToTokens for BareFnTy { |
| 619 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 620 | tokens.append("fn"); |
| 621 | if !self.lifetimes.is_empty() { |
| 622 | tokens.append("<"); |
| 623 | for (i, lifetime) in self.lifetimes.iter().enumerate() { |
| 624 | if i > 0 { |
| 625 | tokens.append(","); |
| 626 | } |
| 627 | lifetime.to_tokens(tokens); |
| 628 | } |
| 629 | tokens.append(">"); |
| 630 | } |
| 631 | tokens.append("("); |
| 632 | tokens.append(")"); |
| 633 | } |
| 634 | } |
| 635 | } |