blob: 7b7d3afbb197dfd71f6664d464d7e5157440b568 [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 Tolnayf945fb52017-02-27 12:53:54 -0800237 #[cfg(feature = "full")]
David Tolnay3cb23a92016-10-07 23:02:21 -0700238 use constant::parsing::const_expr;
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700239 #[cfg(feature = "full")]
240 use expr::parsing::expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700241 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700242 use ident::parsing::ident;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700243 use lit::parsing::quoted_string;
David Tolnay0047c712016-12-21 21:59:25 -0500244 use mac::parsing::mac;
David Tolnay9d8f1972016-09-04 11:58:48 -0700245 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700246
David Tolnayb5a7b142016-09-13 22:46:39 -0700247 named!(pub ty -> Ty, alt!(
David Tolnayd040d772016-10-25 21:33:51 -0700248 ty_paren // must be before ty_tup
249 |
David Tolnay0047c712016-12-21 21:59:25 -0500250 ty_mac // must be before ty_path
251 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700252 ty_path // must be before ty_poly_trait_ref
253 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700254 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700255 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700256 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700257 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700258 ty_ptr
259 |
260 ty_rptr
261 |
262 ty_bare_fn
263 |
264 ty_never
265 |
266 ty_tup
267 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700268 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700269 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700270 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700271 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700272
David Tolnay0047c712016-12-21 21:59:25 -0500273 named!(ty_mac -> Ty, map!(mac, Ty::Mac));
274
David Tolnayb5a7b142016-09-13 22:46:39 -0700275 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700276 punct!("[") >>
277 elem: ty >>
278 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700279 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700280 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700281
David Tolnayfa94b6f2016-10-05 23:26:11 -0700282 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700283 punct!("[") >>
284 elem: ty >>
285 punct!(";") >>
David Tolnay514f1292017-02-27 12:30:57 -0800286 len: array_len >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700287 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700288 (Ty::Array(Box::new(elem), len))
289 ));
290
David Tolnay514f1292017-02-27 12:30:57 -0800291 #[cfg(not(feature = "full"))]
David Tolnayf945fb52017-02-27 12:53:54 -0800292 use constant::parsing::const_expr as array_len;
David Tolnay514f1292017-02-27 12:30:57 -0800293
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700294 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800295 named!(array_len -> ConstExpr, alt!(
296 terminated!(const_expr, after_array_len)
297 |
298 terminated!(expr, after_array_len) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700299 ));
300
David Tolnay514f1292017-02-27 12:30:57 -0800301 #[cfg(feature = "full")]
302 named!(after_array_len -> &str, peek!(punct!("]")));
303
David Tolnayb5a7b142016-09-13 22:46:39 -0700304 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700305 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700306 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700307 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700308 |
David Tolnay10413f02016-09-30 09:12:02 -0700309 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700310 ) >>
311 target: ty >>
312 (Ty::Ptr(Box::new(MutTy {
313 ty: target,
314 mutability: mutability,
315 })))
316 ));
317
David Tolnayb5a7b142016-09-13 22:46:39 -0700318 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700319 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700320 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700321 mutability: mutability >>
322 target: ty >>
323 (Ty::Rptr(life, Box::new(MutTy {
324 ty: target,
325 mutability: mutability,
326 })))
327 ));
328
David Tolnayb5a7b142016-09-13 22:46:39 -0700329 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay4f121832016-10-25 21:33:36 -0700330 lifetimes: opt_vec!(do_parse!(
331 keyword!("for") >>
332 punct!("<") >>
333 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
334 punct!(">") >>
335 (lifetimes)
David Tolnay6b7aaf02016-09-04 10:39:25 -0700336 )) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700337 unsafety: unsafety >>
338 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700339 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700340 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700341 inputs: separated_list!(punct!(","), fn_arg) >>
342 trailing_comma: option!(punct!(",")) >>
343 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700344 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700345 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700346 punct!("->"),
347 ty
348 )) >>
349 (Ty::BareFn(Box::new(BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700350 unsafety: unsafety,
351 abi: abi,
David Tolnay9d8f1972016-09-04 11:58:48 -0700352 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700353 inputs: inputs,
354 output: match output {
355 Some(ty) => FunctionRetTy::Ty(ty),
356 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700357 },
David Tolnay292e6002016-10-29 22:03:51 -0700358 variadic: variadic.is_some(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700359 })))
360 ));
361
David Tolnayb5a7b142016-09-13 22:46:39 -0700362 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700363
David Tolnayb5a7b142016-09-13 22:46:39 -0700364 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700365 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700366 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700367 punct!(")") >>
368 (Ty::Tup(elems))
369 ));
370
David Tolnay6414da72016-10-08 00:55:17 -0700371 named!(ty_path -> Ty, do_parse!(
372 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700373 parenthesized: cond!(
374 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
375 option!(parenthesized_parameter_data)
376 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700377 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
378 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700379 let (qself, mut path) = qpath;
380 if let Some(Some(parenthesized)) = parenthesized {
381 path.segments.last_mut().unwrap().parameters = parenthesized;
382 }
David Tolnay6414da72016-10-08 00:55:17 -0700383 if bounds.is_empty() {
David Tolnay02c907f2017-01-23 00:06:37 -0800384 Ty::Path(qself, path)
David Tolnay6414da72016-10-08 00:55:17 -0700385 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800386 let path = TyParamBound::Trait(
387 PolyTraitRef {
388 bound_lifetimes: Vec::new(),
389 trait_ref: path,
390 },
391 TraitBoundModifier::None,
392 );
393 let bounds = Some(path).into_iter().chain(bounds).collect();
394 Ty::TraitObject(bounds)
David Tolnay6414da72016-10-08 00:55:17 -0700395 }
396 })
397 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700398
David Tolnayf6c74402016-10-08 02:31:26 -0700399 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
400 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700401 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700402 punct!(")") >>
403 output: option!(preceded!(
404 punct!("->"),
405 ty
406 )) >>
407 (PathParameters::Parenthesized(
408 ParenthesizedParameterData {
409 inputs: inputs,
410 output: output,
411 },
412 ))
413 ));
414
David Tolnay9636c052016-10-02 17:11:17 -0700415 named!(pub qpath -> (Option<QSelf>, Path), alt!(
416 map!(path, |p| (None, p))
417 |
418 do_parse!(
419 punct!("<") >>
420 this: map!(ty, Box::new) >>
421 path: option!(preceded!(
422 keyword!("as"),
423 path
424 )) >>
425 punct!(">") >>
426 punct!("::") >>
427 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
428 ({
429 match path {
430 Some(mut path) => {
431 let pos = path.segments.len();
432 path.segments.extend(rest);
433 (Some(QSelf { ty: this, position: pos }), path)
434 }
435 None => {
436 (Some(QSelf { ty: this, position: 0 }), Path {
437 global: false,
438 segments: rest,
439 })
440 }
David Tolnayb79ee962016-09-04 09:39:20 -0700441 }
David Tolnay9636c052016-10-02 17:11:17 -0700442 })
443 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700444 |
445 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700446 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700447
David Tolnay4f0f2512016-10-30 09:28:14 -0700448 named!(ty_poly_trait_ref -> Ty, map!(
David Tolnay8eb6c452016-10-30 13:19:15 -0700449 separated_nonempty_list!(punct!("+"), ty_param_bound),
David Tolnay02c907f2017-01-23 00:06:37 -0800450 Ty::TraitObject
David Tolnay6414da72016-10-08 00:55:17 -0700451 ));
452
David Tolnayb5a7b142016-09-13 22:46:39 -0700453 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700454 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700455 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
456 (Ty::ImplTrait(elem))
457 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700458
David Tolnayb5a7b142016-09-13 22:46:39 -0700459 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700460 punct!("(") >>
461 elem: ty >>
462 punct!(")") >>
463 (Ty::Paren(Box::new(elem)))
464 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700465
David Tolnay47a877c2016-10-01 16:50:55 -0700466 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700467 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700468 |
469 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700470 ));
471
David Tolnayb5a7b142016-09-13 22:46:39 -0700472 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700473 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700474 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
475 (Path {
476 global: global.is_some(),
477 segments: segments,
478 })
479 ));
480
David Tolnay9636c052016-10-02 17:11:17 -0700481 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700482 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700483 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700484 punct!("<") >>
485 lifetimes: separated_list!(punct!(","), lifetime) >>
486 types: opt_vec!(preceded!(
487 cond!(!lifetimes.is_empty(), punct!(",")),
488 separated_nonempty_list!(
489 punct!(","),
David Tolnay1f16b602017-02-07 20:06:55 -0500490 terminated!(ty, not!(punct!("=")))
David Tolnay9d8f1972016-09-04 11:58:48 -0700491 )
492 )) >>
493 bindings: opt_vec!(preceded!(
494 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
495 separated_nonempty_list!(punct!(","), type_binding)
496 )) >>
David Tolnay82a47d52016-10-30 13:01:38 -0700497 cond!(!lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty(), option!(punct!(","))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700498 punct!(">") >>
499 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700500 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700501 parameters: PathParameters::AngleBracketed(
502 AngleBracketedParameterData {
503 lifetimes: lifetimes,
504 types: types,
505 bindings: bindings,
506 }
507 ),
508 })
509 )
510 |
David Tolnay84aa0752016-10-02 23:01:13 -0700511 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700512 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700513 map!(alt!(
514 keyword!("super")
515 |
516 keyword!("self")
517 |
518 keyword!("Self")
519 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700520 ));
521
Arnavionf2dada12017-04-20 23:55:20 -0700522 named!(pub mod_style_path -> Path, do_parse!(
523 global: option!(punct!("::")) >>
524 segments: separated_nonempty_list!(punct!("::"), mod_style_path_segment) >>
525 (Path {
526 global: global.is_some(),
527 segments: segments,
528 })
529 ));
530
531 named!(mod_style_path_segment -> PathSegment, alt!(
532 map!(ident, Into::into)
533 |
534 map!(alt!(
535 keyword!("super")
536 |
537 keyword!("self")
538 |
539 keyword!("Self")
540 ), Into::into)
541 ));
542
David Tolnayb5a7b142016-09-13 22:46:39 -0700543 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700544 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700545 punct!("=") >>
546 ty: ty >>
547 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700548 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700549 ty: ty,
550 })
551 ));
552
David Tolnayb5a7b142016-09-13 22:46:39 -0700553 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700554 bound_lifetimes: bound_lifetimes >>
555 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700556 parenthesized: option!(cond_reduce!(
David Tolnayf6c74402016-10-08 02:31:26 -0700557 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700558 parenthesized_parameter_data
559 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700560 ({
561 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700562 if let Some(parenthesized) = parenthesized {
David Tolnayf6c74402016-10-08 02:31:26 -0700563 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
564 }
565 PolyTraitRef {
566 bound_lifetimes: bound_lifetimes,
567 trait_ref: trait_ref,
568 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700569 })
570 ));
571
David Tolnay62f374c2016-10-02 13:37:00 -0700572 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700573 name: option!(do_parse!(
574 name: ident >>
575 punct!(":") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500576 not!(tag!(":")) >> // not ::
David Tolnayb0417d72016-10-25 21:46:35 -0700577 (name)
578 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700579 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700580 (BareFnArg {
581 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700582 ty: ty,
583 })
584 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700585
586 named!(pub unsafety -> Unsafety, alt!(
587 keyword!("unsafe") => { |_| Unsafety::Unsafe }
588 |
589 epsilon!() => { |_| Unsafety::Normal }
590 ));
591
592 named!(pub abi -> Abi, do_parse!(
593 keyword!("extern") >>
594 name: option!(quoted_string) >>
595 (match name {
596 Some(name) => Abi::Named(name),
David Tolnay76195ce2016-10-30 16:53:48 -0700597 None => Abi::Rust,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700598 })
599 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700600}
David Tolnay87d0b442016-09-04 11:52:12 -0700601
602#[cfg(feature = "printing")]
603mod printing {
604 use super::*;
605 use quote::{Tokens, ToTokens};
606
607 impl ToTokens for Ty {
608 fn to_tokens(&self, tokens: &mut Tokens) {
609 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700610 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700611 tokens.append("[");
612 inner.to_tokens(tokens);
613 tokens.append("]");
614 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700615 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700616 tokens.append("[");
617 inner.to_tokens(tokens);
618 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700619 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700620 tokens.append("]");
621 }
622 Ty::Ptr(ref target) => {
623 tokens.append("*");
624 match target.mutability {
625 Mutability::Mutable => tokens.append("mut"),
626 Mutability::Immutable => tokens.append("const"),
627 }
628 target.ty.to_tokens(tokens);
629 }
630 Ty::Rptr(ref lifetime, ref target) => {
631 tokens.append("&");
632 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700633 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700634 target.ty.to_tokens(tokens);
635 }
636 Ty::BareFn(ref func) => {
637 func.to_tokens(tokens);
638 }
639 Ty::Never => {
640 tokens.append("!");
641 }
642 Ty::Tup(ref elems) => {
643 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700644 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700645 if elems.len() == 1 {
646 tokens.append(",");
647 }
648 tokens.append(")");
649 }
David Tolnayf69904a2016-09-04 14:46:07 -0700650 Ty::Path(None, ref path) => {
651 path.to_tokens(tokens);
652 }
653 Ty::Path(Some(ref qself), ref path) => {
654 tokens.append("<");
655 qself.ty.to_tokens(tokens);
656 if qself.position > 0 {
657 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700658 for (i, segment) in path.segments
David Tolnay05120ef2017-03-12 18:29:26 -0700659 .iter()
660 .take(qself.position)
661 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700662 if i > 0 || path.global {
663 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700664 }
David Tolnayf69904a2016-09-04 14:46:07 -0700665 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700666 }
David Tolnayf69904a2016-09-04 14:46:07 -0700667 }
668 tokens.append(">");
669 for segment in path.segments.iter().skip(qself.position) {
670 tokens.append("::");
671 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700672 }
673 }
David Tolnay02c907f2017-01-23 00:06:37 -0800674 Ty::TraitObject(ref bounds) => {
David Tolnay6414da72016-10-08 00:55:17 -0700675 tokens.append_separated(bounds, "+");
676 }
David Tolnay87d0b442016-09-04 11:52:12 -0700677 Ty::ImplTrait(ref bounds) => {
678 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700679 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700680 }
681 Ty::Paren(ref inner) => {
682 tokens.append("(");
683 inner.to_tokens(tokens);
684 tokens.append(")");
685 }
686 Ty::Infer => {
687 tokens.append("_");
688 }
David Tolnay0047c712016-12-21 21:59:25 -0500689 Ty::Mac(ref mac) => mac.to_tokens(tokens),
David Tolnay87d0b442016-09-04 11:52:12 -0700690 }
691 }
692 }
693
David Tolnay47a877c2016-10-01 16:50:55 -0700694 impl ToTokens for Mutability {
695 fn to_tokens(&self, tokens: &mut Tokens) {
696 if let Mutability::Mutable = *self {
697 tokens.append("mut");
698 }
699 }
700 }
701
David Tolnay87d0b442016-09-04 11:52:12 -0700702 impl ToTokens for Path {
703 fn to_tokens(&self, tokens: &mut Tokens) {
704 for (i, segment) in self.segments.iter().enumerate() {
705 if i > 0 || self.global {
706 tokens.append("::");
707 }
708 segment.to_tokens(tokens);
709 }
710 }
711 }
712
713 impl ToTokens for PathSegment {
714 fn to_tokens(&self, tokens: &mut Tokens) {
715 self.ident.to_tokens(tokens);
David Tolnay5332d4b2016-10-30 14:25:22 -0700716 if self.ident.as_ref().is_empty() && self.parameters.is_empty() {
717 tokens.append("<");
718 tokens.append(">");
719 } else {
720 self.parameters.to_tokens(tokens);
721 }
David Tolnay87d0b442016-09-04 11:52:12 -0700722 }
723 }
724
725 impl ToTokens for PathParameters {
726 fn to_tokens(&self, tokens: &mut Tokens) {
727 match *self {
728 PathParameters::AngleBracketed(ref parameters) => {
729 parameters.to_tokens(tokens);
730 }
731 PathParameters::Parenthesized(ref parameters) => {
732 parameters.to_tokens(tokens);
733 }
734 }
735 }
736 }
737
738 impl ToTokens for AngleBracketedParameterData {
739 fn to_tokens(&self, tokens: &mut Tokens) {
740 let has_lifetimes = !self.lifetimes.is_empty();
741 let has_types = !self.types.is_empty();
742 let has_bindings = !self.bindings.is_empty();
743 if !has_lifetimes && !has_types && !has_bindings {
744 return;
745 }
746
747 tokens.append("<");
748
749 let mut first = true;
750 for lifetime in &self.lifetimes {
751 if !first {
752 tokens.append(",");
753 }
754 lifetime.to_tokens(tokens);
755 first = false;
756 }
757 for ty in &self.types {
758 if !first {
759 tokens.append(",");
760 }
761 ty.to_tokens(tokens);
762 first = false;
763 }
764 for binding in &self.bindings {
765 if !first {
766 tokens.append(",");
767 }
768 binding.to_tokens(tokens);
769 first = false;
770 }
771
772 tokens.append(">");
773 }
774 }
775
776 impl ToTokens for TypeBinding {
777 fn to_tokens(&self, tokens: &mut Tokens) {
778 self.ident.to_tokens(tokens);
779 tokens.append("=");
780 self.ty.to_tokens(tokens);
781 }
782 }
783
784 impl ToTokens for ParenthesizedParameterData {
785 fn to_tokens(&self, tokens: &mut Tokens) {
786 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700787 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700788 tokens.append(")");
789 if let Some(ref output) = self.output {
790 tokens.append("->");
791 output.to_tokens(tokens);
792 }
793 }
794 }
795
796 impl ToTokens for PolyTraitRef {
797 fn to_tokens(&self, tokens: &mut Tokens) {
798 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700799 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700800 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700801 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700802 tokens.append(">");
803 }
804 self.trait_ref.to_tokens(tokens);
805 }
806 }
807
808 impl ToTokens for BareFnTy {
809 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700810 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700811 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700812 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700813 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700814 tokens.append(">");
815 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700816 self.unsafety.to_tokens(tokens);
817 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700818 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700819 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700820 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700821 if self.variadic {
822 if !self.inputs.is_empty() {
823 tokens.append(",");
824 }
825 tokens.append("...");
826 }
David Tolnay87d0b442016-09-04 11:52:12 -0700827 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700828 if let FunctionRetTy::Ty(ref ty) = self.output {
829 tokens.append("->");
830 ty.to_tokens(tokens);
831 }
832 }
833 }
834
David Tolnay62f374c2016-10-02 13:37:00 -0700835 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700836 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700837 if let Some(ref name) = self.name {
838 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700839 tokens.append(":");
840 }
841 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700842 }
843 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700844
845 impl ToTokens for Unsafety {
846 fn to_tokens(&self, tokens: &mut Tokens) {
847 match *self {
848 Unsafety::Unsafe => tokens.append("unsafe"),
849 Unsafety::Normal => {
850 // nothing
851 }
852 }
853 }
854 }
855
856 impl ToTokens for Abi {
857 fn to_tokens(&self, tokens: &mut Tokens) {
858 tokens.append("extern");
859 match *self {
860 Abi::Named(ref named) => named.to_tokens(tokens),
David Tolnay76195ce2016-10-30 16:53:48 -0700861 Abi::Rust => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -0700862 }
863 }
864 }
David Tolnay87d0b442016-09-04 11:52:12 -0700865}