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