blob: efc5cca5a3c1052cd3195256d548069a4b84a554 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay771ecf42016-09-23 19:26:37 -07003/// The different kinds of types recognized by the compiler
David Tolnay9bf4af82017-01-07 11:17:46 -08004#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -07005pub enum Ty {
6 /// A variable-length array (`[T]`)
David Tolnay16709ba2016-10-05 23:11:32 -07007 Slice(Box<Ty>),
David Tolnayb79ee962016-09-04 09:39:20 -07008 /// A fixed length array (`[T; n]`)
David Tolnay3cb23a92016-10-07 23:02:21 -07009 Array(Box<Ty>, ConstExpr),
David Tolnayb79ee962016-09-04 09:39:20 -070010 /// 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),
David Tolnay02c907f2017-01-23 00:06:37 -080025 /// A trait object type `Bound1 + Bound2 + Bound3`
26 /// where `Bound` is a trait or a lifetime.
27 TraitObject(Vec<TyParamBound>),
28 /// An `impl Bound1 + Bound2 + Bound3` type
29 /// where `Bound` is a trait or a lifetime.
David Tolnayb79ee962016-09-04 09:39:20 -070030 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,
David Tolnay0047c712016-12-21 21:59:25 -050036 /// A macro in the type position.
37 Mac(Mac),
38}
39
David Tolnay9bf4af82017-01-07 11:17:46 -080040#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -070041pub struct MutTy {
42 pub ty: Ty,
43 pub mutability: Mutability,
44}
45
David Tolnay9bf4af82017-01-07 11:17:46 -080046#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -070047pub enum Mutability {
48 Mutable,
49 Immutable,
50}
51
David Tolnay771ecf42016-09-23 19:26:37 -070052/// A "Path" is essentially Rust's notion of a name.
53///
54/// It's represented as a sequence of identifiers,
55/// along with a bunch of supporting information.
56///
57/// E.g. `std::cmp::PartialEq`
David Tolnay9bf4af82017-01-07 11:17:46 -080058#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -070059pub struct Path {
David Tolnayf06fdde2016-12-22 17:07:59 -050060 /// A `::foo` path, is relative to the crate root rather than current
61 /// module (like paths in an import).
David Tolnayb79ee962016-09-04 09:39:20 -070062 pub global: bool,
David Tolnayf06fdde2016-12-22 17:07:59 -050063 /// The segments in the path: the things separated by `::`.
David Tolnayb79ee962016-09-04 09:39:20 -070064 pub segments: Vec<PathSegment>,
65}
66
David Tolnaydaaf7742016-10-03 11:11:43 -070067impl<T> From<T> for Path
68 where T: Into<PathSegment>
69{
David Tolnay84aa0752016-10-02 23:01:13 -070070 fn from(segment: T) -> Self {
71 Path {
72 global: false,
73 segments: vec![segment.into()],
74 }
75 }
76}
77
David Tolnayb79ee962016-09-04 09:39:20 -070078/// A segment of a path: an identifier, an optional lifetime, and a set of types.
79///
80/// E.g. `std`, `String` or `Box<T>`
David Tolnay9bf4af82017-01-07 11:17:46 -080081#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -070082pub struct PathSegment {
David Tolnayf06fdde2016-12-22 17:07:59 -050083 /// The identifier portion of this path segment.
David Tolnayb79ee962016-09-04 09:39:20 -070084 pub ident: Ident,
David Tolnayf06fdde2016-12-22 17:07:59 -050085 /// Type/lifetime parameters attached to this path. They come in
86 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
87 /// this is more than just simple syntactic sugar; the use of
88 /// parens affects the region binding rules, so we preserve the
89 /// distinction.
David Tolnayb79ee962016-09-04 09:39:20 -070090 pub parameters: PathParameters,
91}
92
David Tolnaydaaf7742016-10-03 11:11:43 -070093impl<T> From<T> for PathSegment
94 where T: Into<Ident>
95{
David Tolnay84aa0752016-10-02 23:01:13 -070096 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -070097 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -070098 ident: ident.into(),
David Tolnayb79ee962016-09-04 09:39:20 -070099 parameters: PathParameters::none(),
100 }
101 }
102}
103
104/// Parameters of a path segment.
105///
106/// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
David Tolnay9bf4af82017-01-07 11:17:46 -0800107#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700108pub enum PathParameters {
109 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
110 AngleBracketed(AngleBracketedParameterData),
111 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
112 Parenthesized(ParenthesizedParameterData),
113}
114
115impl PathParameters {
116 pub fn none() -> Self {
117 PathParameters::AngleBracketed(AngleBracketedParameterData::default())
118 }
David Tolnay5332d4b2016-10-30 14:25:22 -0700119
120 pub fn is_empty(&self) -> bool {
121 match *self {
122 PathParameters::AngleBracketed(ref bracketed) => {
David Tolnayc1fea502016-10-30 17:54:02 -0700123 bracketed.lifetimes.is_empty() && bracketed.types.is_empty() &&
124 bracketed.bindings.is_empty()
David Tolnay5332d4b2016-10-30 14:25:22 -0700125 }
126 PathParameters::Parenthesized(_) => false,
127 }
128 }
David Tolnayb79ee962016-09-04 09:39:20 -0700129}
130
131/// A path like `Foo<'a, T>`
David Tolnay9bf4af82017-01-07 11:17:46 -0800132#[derive(Debug, Clone, Eq, PartialEq, Default, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700133pub struct AngleBracketedParameterData {
134 /// The lifetime parameters for this path segment.
135 pub lifetimes: Vec<Lifetime>,
136 /// The type parameters for this path segment, if present.
137 pub types: Vec<Ty>,
138 /// Bindings (equality constraints) on associated types, if present.
139 ///
140 /// E.g., `Foo<A=Bar>`.
141 pub bindings: Vec<TypeBinding>,
142}
143
144/// Bind a type to an associated type: `A=Foo`.
David Tolnay9bf4af82017-01-07 11:17:46 -0800145#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700146pub struct TypeBinding {
147 pub ident: Ident,
148 pub ty: Ty,
149}
150
151/// A path like `Foo(A,B) -> C`
David Tolnay9bf4af82017-01-07 11:17:46 -0800152#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700153pub struct ParenthesizedParameterData {
154 /// `(A, B)`
155 pub inputs: Vec<Ty>,
156 /// `C`
157 pub output: Option<Ty>,
158}
159
David Tolnay9bf4af82017-01-07 11:17:46 -0800160#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700161pub struct PolyTraitRef {
162 /// The `'a` in `<'a> Foo<&'a T>`
163 pub bound_lifetimes: Vec<LifetimeDef>,
164 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
165 pub trait_ref: Path,
166}
167
168/// The explicit Self type in a "qualified path". The actual
169/// path, including the trait and the associated item, is stored
170/// separately. `position` represents the index of the associated
171/// item qualified with this Self type.
172///
173/// ```rust,ignore
174/// <Vec<T> as a::b::Trait>::AssociatedItem
175/// ^~~~~ ~~~~~~~~~~~~~~^
176/// ty position = 3
177///
178/// <Vec<T>>::AssociatedItem
179/// ^~~~~ ^
180/// ty position = 0
181/// ```
David Tolnay9bf4af82017-01-07 11:17:46 -0800182#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700183pub struct QSelf {
184 pub ty: Box<Ty>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700185 pub position: usize,
David Tolnayb79ee962016-09-04 09:39:20 -0700186}
187
David Tolnay9bf4af82017-01-07 11:17:46 -0800188#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700189pub struct BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700190 pub unsafety: Unsafety,
191 pub abi: Option<Abi>,
David Tolnayb79ee962016-09-04 09:39:20 -0700192 pub lifetimes: Vec<LifetimeDef>,
David Tolnay62f374c2016-10-02 13:37:00 -0700193 pub inputs: Vec<BareFnArg>,
David Tolnayb79ee962016-09-04 09:39:20 -0700194 pub output: FunctionRetTy,
David Tolnay292e6002016-10-29 22:03:51 -0700195 pub variadic: bool,
David Tolnayb79ee962016-09-04 09:39:20 -0700196}
197
David Tolnay9bf4af82017-01-07 11:17:46 -0800198#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
David Tolnayb8d8ef52016-10-29 14:30:08 -0700199pub enum Unsafety {
200 Unsafe,
201 Normal,
202}
203
David Tolnay9bf4af82017-01-07 11:17:46 -0800204#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb8d8ef52016-10-29 14:30:08 -0700205pub enum Abi {
206 Named(String),
David Tolnay76195ce2016-10-30 16:53:48 -0700207 Rust,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700208}
209
David Tolnay62f374c2016-10-02 13:37:00 -0700210/// An argument in a function type.
David Tolnayb79ee962016-09-04 09:39:20 -0700211///
212/// E.g. `bar: usize` as in `fn foo(bar: usize)`
David Tolnay9bf4af82017-01-07 11:17:46 -0800213#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnay62f374c2016-10-02 13:37:00 -0700214pub struct BareFnArg {
215 pub name: Option<Ident>,
David Tolnayb79ee962016-09-04 09:39:20 -0700216 pub ty: Ty,
217}
218
David Tolnay9bf4af82017-01-07 11:17:46 -0800219#[derive(Debug, Clone, Eq, PartialEq, Hash)]
David Tolnayb79ee962016-09-04 09:39:20 -0700220pub enum FunctionRetTy {
221 /// Return type is not specified.
222 ///
223 /// Functions default to `()` and
224 /// closures default to inference. Span points to where return
225 /// type would be inserted.
226 Default,
227 /// Everything else
228 Ty(Ty),
229}
230
David Tolnay86eca752016-09-04 11:26:41 -0700231#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700232pub mod parsing {
233 use super::*;
David Tolnaya6ac1812017-01-23 00:29:11 -0800234 use {TyParamBound, TraitBoundModifier};
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700235 #[cfg(feature = "full")]
236 use ConstExpr;
David Tolnay3cb23a92016-10-07 23:02:21 -0700237 use constant::parsing::const_expr;
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700238 #[cfg(feature = "full")]
239 use expr::parsing::expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700240 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700241 use ident::parsing::ident;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700242 use lit::parsing::quoted_string;
David Tolnay0047c712016-12-21 21:59:25 -0500243 use mac::parsing::mac;
David Tolnay9d8f1972016-09-04 11:58:48 -0700244 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700245
David Tolnayb5a7b142016-09-13 22:46:39 -0700246 named!(pub ty -> Ty, alt!(
David Tolnayd040d772016-10-25 21:33:51 -0700247 ty_paren // must be before ty_tup
248 |
David Tolnay0047c712016-12-21 21:59:25 -0500249 ty_mac // must be before ty_path
250 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700251 ty_path // must be before ty_poly_trait_ref
252 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700253 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700254 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700255 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700256 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700257 ty_ptr
258 |
259 ty_rptr
260 |
261 ty_bare_fn
262 |
263 ty_never
264 |
265 ty_tup
266 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700267 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700268 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700269 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700270 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700271
David Tolnay0047c712016-12-21 21:59:25 -0500272 named!(ty_mac -> Ty, map!(mac, Ty::Mac));
273
David Tolnayb5a7b142016-09-13 22:46:39 -0700274 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700275 punct!("[") >>
276 elem: ty >>
277 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700278 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700279 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700280
David Tolnayfa94b6f2016-10-05 23:26:11 -0700281 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700282 punct!("[") >>
283 elem: ty >>
284 punct!(";") >>
David Tolnay514f1292017-02-27 12:30:57 -0800285 len: array_len >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700286 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700287 (Ty::Array(Box::new(elem), len))
288 ));
289
David Tolnay514f1292017-02-27 12:30:57 -0800290 #[cfg(not(feature = "full"))]
291 use self::const_expr as array_len;
292
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700293 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800294 named!(array_len -> ConstExpr, alt!(
295 terminated!(const_expr, after_array_len)
296 |
297 terminated!(expr, after_array_len) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700298 ));
299
David Tolnay514f1292017-02-27 12:30:57 -0800300 #[cfg(feature = "full")]
301 named!(after_array_len -> &str, peek!(punct!("]")));
302
David Tolnayb5a7b142016-09-13 22:46:39 -0700303 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700304 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700305 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700306 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700307 |
David Tolnay10413f02016-09-30 09:12:02 -0700308 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700309 ) >>
310 target: ty >>
311 (Ty::Ptr(Box::new(MutTy {
312 ty: target,
313 mutability: mutability,
314 })))
315 ));
316
David Tolnayb5a7b142016-09-13 22:46:39 -0700317 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700318 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700319 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700320 mutability: mutability >>
321 target: ty >>
322 (Ty::Rptr(life, Box::new(MutTy {
323 ty: target,
324 mutability: mutability,
325 })))
326 ));
327
David Tolnayb5a7b142016-09-13 22:46:39 -0700328 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay4f121832016-10-25 21:33:36 -0700329 lifetimes: opt_vec!(do_parse!(
330 keyword!("for") >>
331 punct!("<") >>
332 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
333 punct!(">") >>
334 (lifetimes)
David Tolnay6b7aaf02016-09-04 10:39:25 -0700335 )) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700336 unsafety: unsafety >>
337 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700338 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700339 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700340 inputs: separated_list!(punct!(","), fn_arg) >>
341 trailing_comma: option!(punct!(",")) >>
342 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700343 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700344 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700345 punct!("->"),
346 ty
347 )) >>
348 (Ty::BareFn(Box::new(BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700349 unsafety: unsafety,
350 abi: abi,
David Tolnay9d8f1972016-09-04 11:58:48 -0700351 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700352 inputs: inputs,
353 output: match output {
354 Some(ty) => FunctionRetTy::Ty(ty),
355 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700356 },
David Tolnay292e6002016-10-29 22:03:51 -0700357 variadic: variadic.is_some(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700358 })))
359 ));
360
David Tolnayb5a7b142016-09-13 22:46:39 -0700361 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700362
David Tolnayb5a7b142016-09-13 22:46:39 -0700363 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700364 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700365 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700366 punct!(")") >>
367 (Ty::Tup(elems))
368 ));
369
David Tolnay6414da72016-10-08 00:55:17 -0700370 named!(ty_path -> Ty, do_parse!(
371 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700372 parenthesized: cond!(
373 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
374 option!(parenthesized_parameter_data)
375 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700376 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
377 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700378 let (qself, mut path) = qpath;
379 if let Some(Some(parenthesized)) = parenthesized {
380 path.segments.last_mut().unwrap().parameters = parenthesized;
381 }
David Tolnay6414da72016-10-08 00:55:17 -0700382 if bounds.is_empty() {
David Tolnay02c907f2017-01-23 00:06:37 -0800383 Ty::Path(qself, path)
David Tolnay6414da72016-10-08 00:55:17 -0700384 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800385 let path = TyParamBound::Trait(
386 PolyTraitRef {
387 bound_lifetimes: Vec::new(),
388 trait_ref: path,
389 },
390 TraitBoundModifier::None,
391 );
392 let bounds = Some(path).into_iter().chain(bounds).collect();
393 Ty::TraitObject(bounds)
David Tolnay6414da72016-10-08 00:55:17 -0700394 }
395 })
396 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700397
David Tolnayf6c74402016-10-08 02:31:26 -0700398 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
399 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700400 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700401 punct!(")") >>
402 output: option!(preceded!(
403 punct!("->"),
404 ty
405 )) >>
406 (PathParameters::Parenthesized(
407 ParenthesizedParameterData {
408 inputs: inputs,
409 output: output,
410 },
411 ))
412 ));
413
David Tolnay9636c052016-10-02 17:11:17 -0700414 named!(pub qpath -> (Option<QSelf>, Path), alt!(
415 map!(path, |p| (None, p))
416 |
417 do_parse!(
418 punct!("<") >>
419 this: map!(ty, Box::new) >>
420 path: option!(preceded!(
421 keyword!("as"),
422 path
423 )) >>
424 punct!(">") >>
425 punct!("::") >>
426 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
427 ({
428 match path {
429 Some(mut path) => {
430 let pos = path.segments.len();
431 path.segments.extend(rest);
432 (Some(QSelf { ty: this, position: pos }), path)
433 }
434 None => {
435 (Some(QSelf { ty: this, position: 0 }), Path {
436 global: false,
437 segments: rest,
438 })
439 }
David Tolnayb79ee962016-09-04 09:39:20 -0700440 }
David Tolnay9636c052016-10-02 17:11:17 -0700441 })
442 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700443 |
444 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700445 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700446
David Tolnay4f0f2512016-10-30 09:28:14 -0700447 named!(ty_poly_trait_ref -> Ty, map!(
David Tolnay8eb6c452016-10-30 13:19:15 -0700448 separated_nonempty_list!(punct!("+"), ty_param_bound),
David Tolnay02c907f2017-01-23 00:06:37 -0800449 Ty::TraitObject
David Tolnay6414da72016-10-08 00:55:17 -0700450 ));
451
David Tolnayb5a7b142016-09-13 22:46:39 -0700452 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700453 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700454 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
455 (Ty::ImplTrait(elem))
456 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700457
David Tolnayb5a7b142016-09-13 22:46:39 -0700458 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700459 punct!("(") >>
460 elem: ty >>
461 punct!(")") >>
462 (Ty::Paren(Box::new(elem)))
463 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700464
David Tolnay47a877c2016-10-01 16:50:55 -0700465 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700466 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700467 |
468 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700469 ));
470
David Tolnayb5a7b142016-09-13 22:46:39 -0700471 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700472 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700473 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
474 (Path {
475 global: global.is_some(),
476 segments: segments,
477 })
478 ));
479
David Tolnay9636c052016-10-02 17:11:17 -0700480 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700481 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700482 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700483 punct!("<") >>
484 lifetimes: separated_list!(punct!(","), lifetime) >>
485 types: opt_vec!(preceded!(
486 cond!(!lifetimes.is_empty(), punct!(",")),
487 separated_nonempty_list!(
488 punct!(","),
David Tolnay1f16b602017-02-07 20:06:55 -0500489 terminated!(ty, not!(punct!("=")))
David Tolnay9d8f1972016-09-04 11:58:48 -0700490 )
491 )) >>
492 bindings: opt_vec!(preceded!(
493 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
494 separated_nonempty_list!(punct!(","), type_binding)
495 )) >>
David Tolnay82a47d52016-10-30 13:01:38 -0700496 cond!(!lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty(), option!(punct!(","))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700497 punct!(">") >>
498 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700499 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700500 parameters: PathParameters::AngleBracketed(
501 AngleBracketedParameterData {
502 lifetimes: lifetimes,
503 types: types,
504 bindings: bindings,
505 }
506 ),
507 })
508 )
509 |
David Tolnay84aa0752016-10-02 23:01:13 -0700510 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700511 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700512 map!(alt!(
513 keyword!("super")
514 |
515 keyword!("self")
516 |
517 keyword!("Self")
518 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700519 ));
520
David Tolnayb5a7b142016-09-13 22:46:39 -0700521 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700522 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700523 punct!("=") >>
524 ty: ty >>
525 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700526 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700527 ty: ty,
528 })
529 ));
530
David Tolnayb5a7b142016-09-13 22:46:39 -0700531 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700532 bound_lifetimes: bound_lifetimes >>
533 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700534 parenthesized: option!(cond_reduce!(
David Tolnayf6c74402016-10-08 02:31:26 -0700535 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700536 parenthesized_parameter_data
537 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700538 ({
539 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700540 if let Some(parenthesized) = parenthesized {
David Tolnayf6c74402016-10-08 02:31:26 -0700541 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
542 }
543 PolyTraitRef {
544 bound_lifetimes: bound_lifetimes,
545 trait_ref: trait_ref,
546 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700547 })
548 ));
549
David Tolnay62f374c2016-10-02 13:37:00 -0700550 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700551 name: option!(do_parse!(
552 name: ident >>
553 punct!(":") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500554 not!(tag!(":")) >> // not ::
David Tolnayb0417d72016-10-25 21:46:35 -0700555 (name)
556 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700557 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700558 (BareFnArg {
559 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700560 ty: ty,
561 })
562 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700563
564 named!(pub unsafety -> Unsafety, alt!(
565 keyword!("unsafe") => { |_| Unsafety::Unsafe }
566 |
567 epsilon!() => { |_| Unsafety::Normal }
568 ));
569
570 named!(pub abi -> Abi, do_parse!(
571 keyword!("extern") >>
572 name: option!(quoted_string) >>
573 (match name {
574 Some(name) => Abi::Named(name),
David Tolnay76195ce2016-10-30 16:53:48 -0700575 None => Abi::Rust,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700576 })
577 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700578}
David Tolnay87d0b442016-09-04 11:52:12 -0700579
580#[cfg(feature = "printing")]
581mod printing {
582 use super::*;
583 use quote::{Tokens, ToTokens};
584
585 impl ToTokens for Ty {
586 fn to_tokens(&self, tokens: &mut Tokens) {
587 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700588 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700589 tokens.append("[");
590 inner.to_tokens(tokens);
591 tokens.append("]");
592 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700593 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700594 tokens.append("[");
595 inner.to_tokens(tokens);
596 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700597 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700598 tokens.append("]");
599 }
600 Ty::Ptr(ref target) => {
601 tokens.append("*");
602 match target.mutability {
603 Mutability::Mutable => tokens.append("mut"),
604 Mutability::Immutable => tokens.append("const"),
605 }
606 target.ty.to_tokens(tokens);
607 }
608 Ty::Rptr(ref lifetime, ref target) => {
609 tokens.append("&");
610 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700611 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700612 target.ty.to_tokens(tokens);
613 }
614 Ty::BareFn(ref func) => {
615 func.to_tokens(tokens);
616 }
617 Ty::Never => {
618 tokens.append("!");
619 }
620 Ty::Tup(ref elems) => {
621 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700622 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700623 if elems.len() == 1 {
624 tokens.append(",");
625 }
626 tokens.append(")");
627 }
David Tolnayf69904a2016-09-04 14:46:07 -0700628 Ty::Path(None, ref path) => {
629 path.to_tokens(tokens);
630 }
631 Ty::Path(Some(ref qself), ref path) => {
632 tokens.append("<");
633 qself.ty.to_tokens(tokens);
634 if qself.position > 0 {
635 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700636 for (i, segment) in path.segments
637 .iter()
638 .take(qself.position)
639 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700640 if i > 0 || path.global {
641 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700642 }
David Tolnayf69904a2016-09-04 14:46:07 -0700643 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700644 }
David Tolnayf69904a2016-09-04 14:46:07 -0700645 }
646 tokens.append(">");
647 for segment in path.segments.iter().skip(qself.position) {
648 tokens.append("::");
649 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700650 }
651 }
David Tolnay02c907f2017-01-23 00:06:37 -0800652 Ty::TraitObject(ref bounds) => {
David Tolnay6414da72016-10-08 00:55:17 -0700653 tokens.append_separated(bounds, "+");
654 }
David Tolnay87d0b442016-09-04 11:52:12 -0700655 Ty::ImplTrait(ref bounds) => {
656 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700657 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700658 }
659 Ty::Paren(ref inner) => {
660 tokens.append("(");
661 inner.to_tokens(tokens);
662 tokens.append(")");
663 }
664 Ty::Infer => {
665 tokens.append("_");
666 }
David Tolnay0047c712016-12-21 21:59:25 -0500667 Ty::Mac(ref mac) => mac.to_tokens(tokens),
David Tolnay87d0b442016-09-04 11:52:12 -0700668 }
669 }
670 }
671
David Tolnay47a877c2016-10-01 16:50:55 -0700672 impl ToTokens for Mutability {
673 fn to_tokens(&self, tokens: &mut Tokens) {
674 if let Mutability::Mutable = *self {
675 tokens.append("mut");
676 }
677 }
678 }
679
David Tolnay87d0b442016-09-04 11:52:12 -0700680 impl ToTokens for Path {
681 fn to_tokens(&self, tokens: &mut Tokens) {
682 for (i, segment) in self.segments.iter().enumerate() {
683 if i > 0 || self.global {
684 tokens.append("::");
685 }
686 segment.to_tokens(tokens);
687 }
688 }
689 }
690
691 impl ToTokens for PathSegment {
692 fn to_tokens(&self, tokens: &mut Tokens) {
693 self.ident.to_tokens(tokens);
David Tolnay5332d4b2016-10-30 14:25:22 -0700694 if self.ident.as_ref().is_empty() && self.parameters.is_empty() {
695 tokens.append("<");
696 tokens.append(">");
697 } else {
698 self.parameters.to_tokens(tokens);
699 }
David Tolnay87d0b442016-09-04 11:52:12 -0700700 }
701 }
702
703 impl ToTokens for PathParameters {
704 fn to_tokens(&self, tokens: &mut Tokens) {
705 match *self {
706 PathParameters::AngleBracketed(ref parameters) => {
707 parameters.to_tokens(tokens);
708 }
709 PathParameters::Parenthesized(ref parameters) => {
710 parameters.to_tokens(tokens);
711 }
712 }
713 }
714 }
715
716 impl ToTokens for AngleBracketedParameterData {
717 fn to_tokens(&self, tokens: &mut Tokens) {
718 let has_lifetimes = !self.lifetimes.is_empty();
719 let has_types = !self.types.is_empty();
720 let has_bindings = !self.bindings.is_empty();
721 if !has_lifetimes && !has_types && !has_bindings {
722 return;
723 }
724
725 tokens.append("<");
726
727 let mut first = true;
728 for lifetime in &self.lifetimes {
729 if !first {
730 tokens.append(",");
731 }
732 lifetime.to_tokens(tokens);
733 first = false;
734 }
735 for ty in &self.types {
736 if !first {
737 tokens.append(",");
738 }
739 ty.to_tokens(tokens);
740 first = false;
741 }
742 for binding in &self.bindings {
743 if !first {
744 tokens.append(",");
745 }
746 binding.to_tokens(tokens);
747 first = false;
748 }
749
750 tokens.append(">");
751 }
752 }
753
754 impl ToTokens for TypeBinding {
755 fn to_tokens(&self, tokens: &mut Tokens) {
756 self.ident.to_tokens(tokens);
757 tokens.append("=");
758 self.ty.to_tokens(tokens);
759 }
760 }
761
762 impl ToTokens for ParenthesizedParameterData {
763 fn to_tokens(&self, tokens: &mut Tokens) {
764 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700765 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700766 tokens.append(")");
767 if let Some(ref output) = self.output {
768 tokens.append("->");
769 output.to_tokens(tokens);
770 }
771 }
772 }
773
774 impl ToTokens for PolyTraitRef {
775 fn to_tokens(&self, tokens: &mut Tokens) {
776 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700777 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700778 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700779 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700780 tokens.append(">");
781 }
782 self.trait_ref.to_tokens(tokens);
783 }
784 }
785
786 impl ToTokens for BareFnTy {
787 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700788 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700789 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700790 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700791 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700792 tokens.append(">");
793 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700794 self.unsafety.to_tokens(tokens);
795 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700796 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700797 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700798 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700799 if self.variadic {
800 if !self.inputs.is_empty() {
801 tokens.append(",");
802 }
803 tokens.append("...");
804 }
David Tolnay87d0b442016-09-04 11:52:12 -0700805 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700806 if let FunctionRetTy::Ty(ref ty) = self.output {
807 tokens.append("->");
808 ty.to_tokens(tokens);
809 }
810 }
811 }
812
David Tolnay62f374c2016-10-02 13:37:00 -0700813 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700814 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700815 if let Some(ref name) = self.name {
816 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700817 tokens.append(":");
818 }
819 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700820 }
821 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700822
823 impl ToTokens for Unsafety {
824 fn to_tokens(&self, tokens: &mut Tokens) {
825 match *self {
826 Unsafety::Unsafe => tokens.append("unsafe"),
827 Unsafety::Normal => {
828 // nothing
829 }
830 }
831 }
832 }
833
834 impl ToTokens for Abi {
835 fn to_tokens(&self, tokens: &mut Tokens) {
836 tokens.append("extern");
837 match *self {
838 Abi::Named(ref named) => named.to_tokens(tokens),
David Tolnay76195ce2016-10-30 16:53:48 -0700839 Abi::Rust => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -0700840 }
841 }
842 }
David Tolnay87d0b442016-09-04 11:52:12 -0700843}