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 | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 9 | Array(Box<Ty>, ConstExpr), |
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 | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 198 | use {TraitBoundModifier, TyParamBound}; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 199 | use constant::parsing::const_expr; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 200 | use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 201 | use ident::parsing::ident; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 202 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 203 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 204 | named!(pub ty -> Ty, alt!( |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 205 | ty_poly_trait_ref // must be before ty_path |
| 206 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 207 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 208 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 209 | ty_array |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 210 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 211 | ty_ptr |
| 212 | | |
| 213 | ty_rptr |
| 214 | | |
| 215 | ty_bare_fn |
| 216 | | |
| 217 | ty_never |
| 218 | | |
| 219 | ty_tup |
| 220 | | |
| 221 | ty_path |
| 222 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 223 | ty_impl_trait |
| 224 | | |
| 225 | ty_paren |
| 226 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 227 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 228 | named!(ty_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 229 | punct!("[") >> |
| 230 | elem: ty >> |
| 231 | punct!("]") >> |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 232 | (Ty::Slice(Box::new(elem))) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 233 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 234 | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 235 | named!(ty_array -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 236 | punct!("[") >> |
| 237 | elem: ty >> |
| 238 | punct!(";") >> |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 239 | len: const_expr >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 240 | punct!("]") >> |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 241 | (Ty::Array(Box::new(elem), len)) |
| 242 | )); |
| 243 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 244 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 245 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 246 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 247 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 248 | | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 249 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 250 | ) >> |
| 251 | target: ty >> |
| 252 | (Ty::Ptr(Box::new(MutTy { |
| 253 | ty: target, |
| 254 | mutability: mutability, |
| 255 | }))) |
| 256 | )); |
| 257 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 258 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 259 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 260 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 261 | mutability: mutability >> |
| 262 | target: ty >> |
| 263 | (Ty::Rptr(life, Box::new(MutTy { |
| 264 | ty: target, |
| 265 | mutability: mutability, |
| 266 | }))) |
| 267 | )); |
| 268 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 269 | named!(ty_bare_fn -> Ty, do_parse!( |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame^] | 270 | lifetimes: opt_vec!(do_parse!( |
| 271 | keyword!("for") >> |
| 272 | punct!("<") >> |
| 273 | lifetimes: terminated_list!(punct!(","), lifetime_def) >> |
| 274 | punct!(">") >> |
| 275 | (lifetimes) |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 276 | )) >> |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame^] | 277 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 278 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 279 | inputs: terminated_list!(punct!(","), fn_arg) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 280 | punct!(")") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 281 | output: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 282 | punct!("->"), |
| 283 | ty |
| 284 | )) >> |
| 285 | (Ty::BareFn(Box::new(BareFnTy { |
| 286 | lifetimes: lifetimes, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 287 | inputs: inputs, |
| 288 | output: match output { |
| 289 | Some(ty) => FunctionRetTy::Ty(ty), |
| 290 | None => FunctionRetTy::Default, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 291 | }, |
| 292 | }))) |
| 293 | )); |
| 294 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 295 | named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never)); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 296 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 297 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 298 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 299 | elems: terminated_list!(punct!(","), ty) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 300 | punct!(")") >> |
| 301 | (Ty::Tup(elems)) |
| 302 | )); |
| 303 | |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 304 | named!(ty_path -> Ty, do_parse!( |
| 305 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 306 | parenthesized: cond!( |
| 307 | qpath.1.segments.last().unwrap().parameters == PathParameters::none(), |
| 308 | option!(parenthesized_parameter_data) |
| 309 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 310 | bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >> |
| 311 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 312 | let (qself, mut path) = qpath; |
| 313 | if let Some(Some(parenthesized)) = parenthesized { |
| 314 | path.segments.last_mut().unwrap().parameters = parenthesized; |
| 315 | } |
| 316 | let path = Ty::Path(qself, path); |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 317 | if bounds.is_empty() { |
| 318 | path |
| 319 | } else { |
| 320 | Ty::ObjectSum(Box::new(path), bounds) |
| 321 | } |
| 322 | }) |
| 323 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 324 | |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 325 | named!(parenthesized_parameter_data -> PathParameters, do_parse!( |
| 326 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 327 | inputs: terminated_list!(punct!(","), ty) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 328 | punct!(")") >> |
| 329 | output: option!(preceded!( |
| 330 | punct!("->"), |
| 331 | ty |
| 332 | )) >> |
| 333 | (PathParameters::Parenthesized( |
| 334 | ParenthesizedParameterData { |
| 335 | inputs: inputs, |
| 336 | output: output, |
| 337 | }, |
| 338 | )) |
| 339 | )); |
| 340 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 341 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
| 342 | map!(path, |p| (None, p)) |
| 343 | | |
| 344 | do_parse!( |
| 345 | punct!("<") >> |
| 346 | this: map!(ty, Box::new) >> |
| 347 | path: option!(preceded!( |
| 348 | keyword!("as"), |
| 349 | path |
| 350 | )) >> |
| 351 | punct!(">") >> |
| 352 | punct!("::") >> |
| 353 | rest: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 354 | ({ |
| 355 | match path { |
| 356 | Some(mut path) => { |
| 357 | let pos = path.segments.len(); |
| 358 | path.segments.extend(rest); |
| 359 | (Some(QSelf { ty: this, position: pos }), path) |
| 360 | } |
| 361 | None => { |
| 362 | (Some(QSelf { ty: this, position: 0 }), Path { |
| 363 | global: false, |
| 364 | segments: rest, |
| 365 | }) |
| 366 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 367 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 368 | }) |
| 369 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 370 | | |
| 371 | map!(keyword!("self"), |_| (None, "self".into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 372 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 373 | |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 374 | named!(ty_poly_trait_ref -> Ty, do_parse!( |
| 375 | keyword!("for") >> |
| 376 | punct!("<") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 377 | lifetimes: terminated_list!(punct!(","), lifetime_def) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 378 | punct!(">") >> |
| 379 | trait_ref: path >> |
| 380 | (Ty::PolyTraitRef(vec![ |
| 381 | TyParamBound::Trait( |
| 382 | PolyTraitRef { |
| 383 | bound_lifetimes: lifetimes, |
| 384 | trait_ref: trait_ref, |
| 385 | }, |
| 386 | TraitBoundModifier::None, |
| 387 | ), |
| 388 | ])) |
| 389 | )); |
| 390 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 391 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 392 | keyword!("impl") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 393 | elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 394 | (Ty::ImplTrait(elem)) |
| 395 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 396 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 397 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 398 | punct!("(") >> |
| 399 | elem: ty >> |
| 400 | punct!(")") >> |
| 401 | (Ty::Paren(Box::new(elem))) |
| 402 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 403 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 404 | named!(pub mutability -> Mutability, alt!( |
David Tolnay | bd76e57 | 2016-10-02 13:43:16 -0700 | [diff] [blame] | 405 | keyword!("mut") => { |_| Mutability::Mutable } |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 406 | | |
| 407 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 408 | )); |
| 409 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 410 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 411 | global: option!(punct!("::")) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 412 | segments: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 413 | (Path { |
| 414 | global: global.is_some(), |
| 415 | segments: segments, |
| 416 | }) |
| 417 | )); |
| 418 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 419 | named!(path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 420 | do_parse!( |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 421 | id: option!(ident) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 422 | punct!("<") >> |
| 423 | lifetimes: separated_list!(punct!(","), lifetime) >> |
| 424 | types: opt_vec!(preceded!( |
| 425 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 426 | separated_nonempty_list!( |
| 427 | punct!(","), |
| 428 | terminated!(ty, not!(peek!(punct!("=")))) |
| 429 | ) |
| 430 | )) >> |
| 431 | bindings: opt_vec!(preceded!( |
| 432 | cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")), |
| 433 | separated_nonempty_list!(punct!(","), type_binding) |
| 434 | )) >> |
| 435 | punct!(">") >> |
| 436 | (PathSegment { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 437 | ident: id.unwrap_or_else(|| "".into()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 438 | parameters: PathParameters::AngleBracketed( |
| 439 | AngleBracketedParameterData { |
| 440 | lifetimes: lifetimes, |
| 441 | types: types, |
| 442 | bindings: bindings, |
| 443 | } |
| 444 | ), |
| 445 | }) |
| 446 | ) |
| 447 | | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 448 | map!(ident, Into::into) |
David Tolnay | 7780722 | 2016-10-24 22:30:15 -0700 | [diff] [blame] | 449 | | |
David Tolnay | e14e3be | 2016-10-24 22:53:07 -0700 | [diff] [blame] | 450 | map!(alt!( |
| 451 | keyword!("super") |
| 452 | | |
| 453 | keyword!("self") |
| 454 | | |
| 455 | keyword!("Self") |
| 456 | ), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 457 | )); |
| 458 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 459 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 460 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 461 | punct!("=") >> |
| 462 | ty: ty >> |
| 463 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 464 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 465 | ty: ty, |
| 466 | }) |
| 467 | )); |
| 468 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 469 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 470 | bound_lifetimes: bound_lifetimes >> |
| 471 | trait_ref: path >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 472 | parenthesized: cond!( |
| 473 | trait_ref.segments.last().unwrap().parameters == PathParameters::none(), |
| 474 | option!(parenthesized_parameter_data) |
| 475 | ) >> |
| 476 | ({ |
| 477 | let mut trait_ref = trait_ref; |
| 478 | if let Some(Some(parenthesized)) = parenthesized { |
| 479 | trait_ref.segments.last_mut().unwrap().parameters = parenthesized; |
| 480 | } |
| 481 | PolyTraitRef { |
| 482 | bound_lifetimes: bound_lifetimes, |
| 483 | trait_ref: trait_ref, |
| 484 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 485 | }) |
| 486 | )); |
| 487 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 488 | named!(pub fn_arg -> BareFnArg, do_parse!( |
| 489 | name: option!(terminated!(ident, punct!(":"))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 490 | ty: ty >> |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 491 | (BareFnArg { |
| 492 | name: name, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 493 | ty: ty, |
| 494 | }) |
| 495 | )); |
| 496 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 497 | |
| 498 | #[cfg(feature = "printing")] |
| 499 | mod printing { |
| 500 | use super::*; |
| 501 | use quote::{Tokens, ToTokens}; |
| 502 | |
| 503 | impl ToTokens for Ty { |
| 504 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 505 | match *self { |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 506 | Ty::Slice(ref inner) => { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 507 | tokens.append("["); |
| 508 | inner.to_tokens(tokens); |
| 509 | tokens.append("]"); |
| 510 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 511 | Ty::Array(ref inner, ref len) => { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 512 | tokens.append("["); |
| 513 | inner.to_tokens(tokens); |
| 514 | tokens.append(";"); |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 515 | len.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 516 | tokens.append("]"); |
| 517 | } |
| 518 | Ty::Ptr(ref target) => { |
| 519 | tokens.append("*"); |
| 520 | match target.mutability { |
| 521 | Mutability::Mutable => tokens.append("mut"), |
| 522 | Mutability::Immutable => tokens.append("const"), |
| 523 | } |
| 524 | target.ty.to_tokens(tokens); |
| 525 | } |
| 526 | Ty::Rptr(ref lifetime, ref target) => { |
| 527 | tokens.append("&"); |
| 528 | lifetime.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 529 | target.mutability.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 530 | target.ty.to_tokens(tokens); |
| 531 | } |
| 532 | Ty::BareFn(ref func) => { |
| 533 | func.to_tokens(tokens); |
| 534 | } |
| 535 | Ty::Never => { |
| 536 | tokens.append("!"); |
| 537 | } |
| 538 | Ty::Tup(ref elems) => { |
| 539 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 540 | tokens.append_separated(elems, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 541 | if elems.len() == 1 { |
| 542 | tokens.append(","); |
| 543 | } |
| 544 | tokens.append(")"); |
| 545 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 546 | Ty::Path(None, ref path) => { |
| 547 | path.to_tokens(tokens); |
| 548 | } |
| 549 | Ty::Path(Some(ref qself), ref path) => { |
| 550 | tokens.append("<"); |
| 551 | qself.ty.to_tokens(tokens); |
| 552 | if qself.position > 0 { |
| 553 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 554 | for (i, segment) in path.segments |
| 555 | .iter() |
| 556 | .take(qself.position) |
| 557 | .enumerate() { |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 558 | if i > 0 || path.global { |
| 559 | tokens.append("::"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 560 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 561 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 562 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 563 | } |
| 564 | tokens.append(">"); |
| 565 | for segment in path.segments.iter().skip(qself.position) { |
| 566 | tokens.append("::"); |
| 567 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 570 | Ty::ObjectSum(ref ty, ref bounds) => { |
| 571 | ty.to_tokens(tokens); |
| 572 | for bound in bounds { |
| 573 | tokens.append("+"); |
| 574 | bound.to_tokens(tokens); |
| 575 | } |
| 576 | } |
| 577 | Ty::PolyTraitRef(ref bounds) => { |
| 578 | tokens.append_separated(bounds, "+"); |
| 579 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 580 | Ty::ImplTrait(ref bounds) => { |
| 581 | tokens.append("impl"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 582 | tokens.append_separated(bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 583 | } |
| 584 | Ty::Paren(ref inner) => { |
| 585 | tokens.append("("); |
| 586 | inner.to_tokens(tokens); |
| 587 | tokens.append(")"); |
| 588 | } |
| 589 | Ty::Infer => { |
| 590 | tokens.append("_"); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 596 | impl ToTokens for Mutability { |
| 597 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 598 | if let Mutability::Mutable = *self { |
| 599 | tokens.append("mut"); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 604 | impl ToTokens for Path { |
| 605 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 606 | for (i, segment) in self.segments.iter().enumerate() { |
| 607 | if i > 0 || self.global { |
| 608 | tokens.append("::"); |
| 609 | } |
| 610 | segment.to_tokens(tokens); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | impl ToTokens for PathSegment { |
| 616 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 617 | self.ident.to_tokens(tokens); |
| 618 | self.parameters.to_tokens(tokens); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | impl ToTokens for PathParameters { |
| 623 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 624 | match *self { |
| 625 | PathParameters::AngleBracketed(ref parameters) => { |
| 626 | parameters.to_tokens(tokens); |
| 627 | } |
| 628 | PathParameters::Parenthesized(ref parameters) => { |
| 629 | parameters.to_tokens(tokens); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | impl ToTokens for AngleBracketedParameterData { |
| 636 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 637 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 638 | let has_types = !self.types.is_empty(); |
| 639 | let has_bindings = !self.bindings.is_empty(); |
| 640 | if !has_lifetimes && !has_types && !has_bindings { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | tokens.append("<"); |
| 645 | |
| 646 | let mut first = true; |
| 647 | for lifetime in &self.lifetimes { |
| 648 | if !first { |
| 649 | tokens.append(","); |
| 650 | } |
| 651 | lifetime.to_tokens(tokens); |
| 652 | first = false; |
| 653 | } |
| 654 | for ty in &self.types { |
| 655 | if !first { |
| 656 | tokens.append(","); |
| 657 | } |
| 658 | ty.to_tokens(tokens); |
| 659 | first = false; |
| 660 | } |
| 661 | for binding in &self.bindings { |
| 662 | if !first { |
| 663 | tokens.append(","); |
| 664 | } |
| 665 | binding.to_tokens(tokens); |
| 666 | first = false; |
| 667 | } |
| 668 | |
| 669 | tokens.append(">"); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | impl ToTokens for TypeBinding { |
| 674 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 675 | self.ident.to_tokens(tokens); |
| 676 | tokens.append("="); |
| 677 | self.ty.to_tokens(tokens); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | impl ToTokens for ParenthesizedParameterData { |
| 682 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 683 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 684 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 685 | tokens.append(")"); |
| 686 | if let Some(ref output) = self.output { |
| 687 | tokens.append("->"); |
| 688 | output.to_tokens(tokens); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | impl ToTokens for PolyTraitRef { |
| 694 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 695 | if !self.bound_lifetimes.is_empty() { |
David Tolnay | e8796aa | 2016-09-04 14:48:22 -0700 | [diff] [blame] | 696 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 697 | tokens.append("<"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 698 | tokens.append_separated(&self.bound_lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 699 | tokens.append(">"); |
| 700 | } |
| 701 | self.trait_ref.to_tokens(tokens); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | impl ToTokens for BareFnTy { |
| 706 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 707 | if !self.lifetimes.is_empty() { |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame^] | 708 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 709 | tokens.append("<"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 710 | tokens.append_separated(&self.lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 711 | tokens.append(">"); |
| 712 | } |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame^] | 713 | tokens.append("fn"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 714 | tokens.append("("); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 715 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 716 | tokens.append(")"); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 717 | if let FunctionRetTy::Ty(ref ty) = self.output { |
| 718 | tokens.append("->"); |
| 719 | ty.to_tokens(tokens); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 724 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 725 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 726 | if let Some(ref name) = self.name { |
| 727 | name.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 728 | tokens.append(":"); |
| 729 | } |
| 730 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |