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>, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 157 | pub inputs: Vec<BareFnArg>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 158 | pub output: FunctionRetTy, |
| 159 | } |
| 160 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 161 | /// An argument in a function type. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 162 | /// |
| 163 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 164 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 165 | pub struct BareFnArg { |
| 166 | pub name: Option<Ident>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 167 | pub ty: Ty, |
| 168 | } |
| 169 | |
| 170 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 171 | pub enum FunctionRetTy { |
| 172 | /// Return type is not specified. |
| 173 | /// |
| 174 | /// Functions default to `()` and |
| 175 | /// closures default to inference. Span points to where return |
| 176 | /// type would be inserted. |
| 177 | Default, |
| 178 | /// Everything else |
| 179 | Ty(Ty), |
| 180 | } |
| 181 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 182 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 183 | pub mod parsing { |
| 184 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 185 | use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 186 | use ident::parsing::ident; |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 187 | use lit::parsing::int; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 188 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 189 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 190 | named!(pub ty -> Ty, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 191 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 192 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 193 | ty_fixed_length_vec |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 194 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 195 | ty_ptr |
| 196 | | |
| 197 | ty_rptr |
| 198 | | |
| 199 | ty_bare_fn |
| 200 | | |
| 201 | ty_never |
| 202 | | |
| 203 | ty_tup |
| 204 | | |
| 205 | ty_path |
| 206 | | |
| 207 | ty_qpath |
| 208 | | |
| 209 | ty_impl_trait |
| 210 | | |
| 211 | ty_paren |
| 212 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 213 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 214 | named!(ty_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 215 | punct!("[") >> |
| 216 | elem: ty >> |
| 217 | punct!("]") >> |
| 218 | (Ty::Vec(Box::new(elem))) |
| 219 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 220 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 221 | named!(ty_fixed_length_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 222 | punct!("[") >> |
| 223 | elem: ty >> |
| 224 | punct!(";") >> |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 225 | len: int >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 226 | punct!("]") >> |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 227 | (Ty::FixedLengthVec(Box::new(elem), len.0 as usize)) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 228 | )); |
| 229 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 230 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 231 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 232 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 233 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 234 | | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 235 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 236 | ) >> |
| 237 | target: ty >> |
| 238 | (Ty::Ptr(Box::new(MutTy { |
| 239 | ty: target, |
| 240 | mutability: mutability, |
| 241 | }))) |
| 242 | )); |
| 243 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 244 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 245 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 246 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 247 | mutability: mutability >> |
| 248 | target: ty >> |
| 249 | (Ty::Rptr(life, Box::new(MutTy { |
| 250 | ty: target, |
| 251 | mutability: mutability, |
| 252 | }))) |
| 253 | )); |
| 254 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 255 | named!(ty_bare_fn -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 256 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 257 | lifetimes: opt_vec!(delimited!( |
| 258 | punct!("<"), |
| 259 | separated_list!(punct!(","), lifetime_def), |
| 260 | punct!(">") |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 261 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 262 | punct!("(") >> |
| 263 | inputs: separated_list!(punct!(","), fn_arg) >> |
| 264 | punct!(")") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 265 | output: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 266 | punct!("->"), |
| 267 | ty |
| 268 | )) >> |
| 269 | (Ty::BareFn(Box::new(BareFnTy { |
| 270 | lifetimes: lifetimes, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 271 | inputs: inputs, |
| 272 | output: match output { |
| 273 | Some(ty) => FunctionRetTy::Ty(ty), |
| 274 | None => FunctionRetTy::Default, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 275 | }, |
| 276 | }))) |
| 277 | )); |
| 278 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 279 | named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never)); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 280 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 281 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 282 | punct!("(") >> |
| 283 | elems: separated_list!(punct!(","), ty) >> |
| 284 | punct!(")") >> |
| 285 | (Ty::Tup(elems)) |
| 286 | )); |
| 287 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 288 | named!(ty_path -> Ty, map!(path, |p| Ty::Path(None, p))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 289 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 290 | named!(ty_qpath -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 291 | punct!("<") >> |
| 292 | this: map!(ty, Box::new) >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 293 | path: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 294 | keyword!("as"), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 295 | path |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 296 | )) >> |
| 297 | punct!(">") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 298 | punct!("::") >> |
| 299 | rest: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 300 | ({ |
| 301 | match path { |
| 302 | Some(mut path) => { |
| 303 | let pos = path.segments.len(); |
| 304 | path.segments.extend(rest); |
| 305 | Ty::Path(Some(QSelf { ty: this, position: pos }), path) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 306 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 307 | None => { |
| 308 | Ty::Path(Some(QSelf { ty: this, position: 0 }), Path { |
| 309 | global: false, |
| 310 | segments: rest, |
| 311 | }) |
| 312 | } |
| 313 | } |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 314 | }) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 315 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 316 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 317 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 318 | keyword!("impl") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 319 | elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 320 | (Ty::ImplTrait(elem)) |
| 321 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 322 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 323 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 324 | punct!("(") >> |
| 325 | elem: ty >> |
| 326 | punct!(")") >> |
| 327 | (Ty::Paren(Box::new(elem))) |
| 328 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 329 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 330 | named!(pub mutability -> Mutability, alt!( |
David Tolnay | bd76e57 | 2016-10-02 13:43:16 -0700 | [diff] [blame] | 331 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 332 | | |
| 333 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 334 | )); |
| 335 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 336 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 337 | global: option!(punct!("::")) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 338 | segments: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 339 | (Path { |
| 340 | global: global.is_some(), |
| 341 | segments: segments, |
| 342 | }) |
| 343 | )); |
| 344 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 345 | named!(pub path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 346 | do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 347 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 348 | punct!("<") >> |
| 349 | lifetimes: separated_list!(punct!(","), lifetime) >> |
| 350 | types: opt_vec!(preceded!( |
| 351 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 352 | separated_nonempty_list!( |
| 353 | punct!(","), |
| 354 | terminated!(ty, not!(peek!(punct!("=")))) |
| 355 | ) |
| 356 | )) >> |
| 357 | bindings: opt_vec!(preceded!( |
| 358 | cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")), |
| 359 | separated_nonempty_list!(punct!(","), type_binding) |
| 360 | )) >> |
| 361 | punct!(">") >> |
| 362 | (PathSegment { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 363 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 364 | parameters: PathParameters::AngleBracketed( |
| 365 | AngleBracketedParameterData { |
| 366 | lifetimes: lifetimes, |
| 367 | types: types, |
| 368 | bindings: bindings, |
| 369 | } |
| 370 | ), |
| 371 | }) |
| 372 | ) |
| 373 | | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 374 | map!(ident, PathSegment::ident) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 375 | )); |
| 376 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 377 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 378 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 379 | punct!("=") >> |
| 380 | ty: ty >> |
| 381 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 382 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 383 | ty: ty, |
| 384 | }) |
| 385 | )); |
| 386 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 387 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 388 | bound_lifetimes: bound_lifetimes >> |
| 389 | trait_ref: path >> |
| 390 | (PolyTraitRef { |
| 391 | bound_lifetimes: bound_lifetimes, |
| 392 | trait_ref: trait_ref, |
| 393 | }) |
| 394 | )); |
| 395 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 396 | named!(pub fn_arg -> BareFnArg, do_parse!( |
| 397 | name: option!(terminated!(ident, punct!(":"))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 398 | ty: ty >> |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 399 | (BareFnArg { |
| 400 | name: name, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 401 | ty: ty, |
| 402 | }) |
| 403 | )); |
| 404 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 405 | |
| 406 | #[cfg(feature = "printing")] |
| 407 | mod printing { |
| 408 | use super::*; |
| 409 | use quote::{Tokens, ToTokens}; |
| 410 | |
| 411 | impl ToTokens for Ty { |
| 412 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 413 | match *self { |
| 414 | Ty::Vec(ref inner) => { |
| 415 | tokens.append("["); |
| 416 | inner.to_tokens(tokens); |
| 417 | tokens.append("]"); |
| 418 | } |
| 419 | Ty::FixedLengthVec(ref inner, len) => { |
| 420 | tokens.append("["); |
| 421 | inner.to_tokens(tokens); |
| 422 | tokens.append(";"); |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 423 | tokens.append(&len.to_string()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 424 | tokens.append("]"); |
| 425 | } |
| 426 | Ty::Ptr(ref target) => { |
| 427 | tokens.append("*"); |
| 428 | match target.mutability { |
| 429 | Mutability::Mutable => tokens.append("mut"), |
| 430 | Mutability::Immutable => tokens.append("const"), |
| 431 | } |
| 432 | target.ty.to_tokens(tokens); |
| 433 | } |
| 434 | Ty::Rptr(ref lifetime, ref target) => { |
| 435 | tokens.append("&"); |
| 436 | lifetime.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 437 | target.mutability.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 438 | target.ty.to_tokens(tokens); |
| 439 | } |
| 440 | Ty::BareFn(ref func) => { |
| 441 | func.to_tokens(tokens); |
| 442 | } |
| 443 | Ty::Never => { |
| 444 | tokens.append("!"); |
| 445 | } |
| 446 | Ty::Tup(ref elems) => { |
| 447 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 448 | tokens.append_separated(elems, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 449 | if elems.len() == 1 { |
| 450 | tokens.append(","); |
| 451 | } |
| 452 | tokens.append(")"); |
| 453 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 454 | Ty::Path(None, ref path) => { |
| 455 | path.to_tokens(tokens); |
| 456 | } |
| 457 | Ty::Path(Some(ref qself), ref path) => { |
| 458 | tokens.append("<"); |
| 459 | qself.ty.to_tokens(tokens); |
| 460 | if qself.position > 0 { |
| 461 | tokens.append("as"); |
| 462 | for (i, segment) in path.segments.iter() |
| 463 | .take(qself.position) |
| 464 | .enumerate() |
| 465 | { |
| 466 | if i > 0 || path.global { |
| 467 | tokens.append("::"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 468 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 469 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 470 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 471 | } |
| 472 | tokens.append(">"); |
| 473 | for segment in path.segments.iter().skip(qself.position) { |
| 474 | tokens.append("::"); |
| 475 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | Ty::ObjectSum(_, _) => unimplemented!(), |
| 479 | Ty::PolyTraitRef(_) => unimplemented!(), |
| 480 | Ty::ImplTrait(ref bounds) => { |
| 481 | tokens.append("impl"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 482 | tokens.append_separated(bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 483 | } |
| 484 | Ty::Paren(ref inner) => { |
| 485 | tokens.append("("); |
| 486 | inner.to_tokens(tokens); |
| 487 | tokens.append(")"); |
| 488 | } |
| 489 | Ty::Infer => { |
| 490 | tokens.append("_"); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 496 | impl ToTokens for Mutability { |
| 497 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 498 | if let Mutability::Mutable = *self { |
| 499 | tokens.append("mut"); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 504 | impl ToTokens for Path { |
| 505 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 506 | for (i, segment) in self.segments.iter().enumerate() { |
| 507 | if i > 0 || self.global { |
| 508 | tokens.append("::"); |
| 509 | } |
| 510 | segment.to_tokens(tokens); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | impl ToTokens for PathSegment { |
| 516 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 517 | self.ident.to_tokens(tokens); |
| 518 | self.parameters.to_tokens(tokens); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | impl ToTokens for PathParameters { |
| 523 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 524 | match *self { |
| 525 | PathParameters::AngleBracketed(ref parameters) => { |
| 526 | parameters.to_tokens(tokens); |
| 527 | } |
| 528 | PathParameters::Parenthesized(ref parameters) => { |
| 529 | parameters.to_tokens(tokens); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | impl ToTokens for AngleBracketedParameterData { |
| 536 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 537 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 538 | let has_types = !self.types.is_empty(); |
| 539 | let has_bindings = !self.bindings.is_empty(); |
| 540 | if !has_lifetimes && !has_types && !has_bindings { |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | tokens.append("<"); |
| 545 | |
| 546 | let mut first = true; |
| 547 | for lifetime in &self.lifetimes { |
| 548 | if !first { |
| 549 | tokens.append(","); |
| 550 | } |
| 551 | lifetime.to_tokens(tokens); |
| 552 | first = false; |
| 553 | } |
| 554 | for ty in &self.types { |
| 555 | if !first { |
| 556 | tokens.append(","); |
| 557 | } |
| 558 | ty.to_tokens(tokens); |
| 559 | first = false; |
| 560 | } |
| 561 | for binding in &self.bindings { |
| 562 | if !first { |
| 563 | tokens.append(","); |
| 564 | } |
| 565 | binding.to_tokens(tokens); |
| 566 | first = false; |
| 567 | } |
| 568 | |
| 569 | tokens.append(">"); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | impl ToTokens for TypeBinding { |
| 574 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 575 | self.ident.to_tokens(tokens); |
| 576 | tokens.append("="); |
| 577 | self.ty.to_tokens(tokens); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | impl ToTokens for ParenthesizedParameterData { |
| 582 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 583 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 584 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 585 | tokens.append(")"); |
| 586 | if let Some(ref output) = self.output { |
| 587 | tokens.append("->"); |
| 588 | output.to_tokens(tokens); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | impl ToTokens for PolyTraitRef { |
| 594 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 595 | if !self.bound_lifetimes.is_empty() { |
David Tolnay | e8796aa | 2016-09-04 14:48:22 -0700 | [diff] [blame] | 596 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 597 | tokens.append("<"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 598 | tokens.append_separated(&self.bound_lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 599 | tokens.append(">"); |
| 600 | } |
| 601 | self.trait_ref.to_tokens(tokens); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | impl ToTokens for BareFnTy { |
| 606 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 607 | tokens.append("fn"); |
| 608 | if !self.lifetimes.is_empty() { |
| 609 | tokens.append("<"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 610 | tokens.append_separated(&self.lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 611 | tokens.append(">"); |
| 612 | } |
| 613 | tokens.append("("); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 614 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 615 | tokens.append(")"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 616 | if let FunctionRetTy::Ty(ref ty) = self.output { |
| 617 | tokens.append("->"); |
| 618 | ty.to_tokens(tokens); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 623 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 624 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 625 | if let Some(ref name) = self.name { |
| 626 | name.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 627 | tokens.append(":"); |
| 628 | } |
| 629 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | } |