blob: f0cc88c953edcc87afc25e5e101ba73c3b6c0c9c [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 Tolnayfe2cc9a2016-10-30 12:47:36 -0700281 #[cfg(not(feature = "full"))]
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 Tolnay3cb23a92016-10-07 23:02:21 -0700286 len: const_expr >>
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 Tolnayfe2cc9a2016-10-30 12:47:36 -0700291 #[cfg(feature = "full")]
292 named!(ty_array -> Ty, do_parse!(
293 punct!("[") >>
294 elem: ty >>
295 punct!(";") >>
296 len: alt!(
297 terminated!(const_expr, punct!("]"))
298 |
299 terminated!(expr, punct!("]")) => { ConstExpr::Other }
300 ) >>
301 (Ty::Array(Box::new(elem), len))
302 ));
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
David Tolnayb5a7b142016-09-13 22:46:39 -0700522 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700523 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700524 punct!("=") >>
525 ty: ty >>
526 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700527 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700528 ty: ty,
529 })
530 ));
531
David Tolnayb5a7b142016-09-13 22:46:39 -0700532 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700533 bound_lifetimes: bound_lifetimes >>
534 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700535 parenthesized: option!(cond_reduce!(
David Tolnayf6c74402016-10-08 02:31:26 -0700536 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700537 parenthesized_parameter_data
538 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700539 ({
540 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700541 if let Some(parenthesized) = parenthesized {
David Tolnayf6c74402016-10-08 02:31:26 -0700542 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
543 }
544 PolyTraitRef {
545 bound_lifetimes: bound_lifetimes,
546 trait_ref: trait_ref,
547 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700548 })
549 ));
550
David Tolnay62f374c2016-10-02 13:37:00 -0700551 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700552 name: option!(do_parse!(
553 name: ident >>
554 punct!(":") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500555 not!(tag!(":")) >> // not ::
David Tolnayb0417d72016-10-25 21:46:35 -0700556 (name)
557 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700558 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700559 (BareFnArg {
560 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700561 ty: ty,
562 })
563 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700564
565 named!(pub unsafety -> Unsafety, alt!(
566 keyword!("unsafe") => { |_| Unsafety::Unsafe }
567 |
568 epsilon!() => { |_| Unsafety::Normal }
569 ));
570
571 named!(pub abi -> Abi, do_parse!(
572 keyword!("extern") >>
573 name: option!(quoted_string) >>
574 (match name {
575 Some(name) => Abi::Named(name),
David Tolnay76195ce2016-10-30 16:53:48 -0700576 None => Abi::Rust,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700577 })
578 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700579}
David Tolnay87d0b442016-09-04 11:52:12 -0700580
581#[cfg(feature = "printing")]
582mod printing {
583 use super::*;
584 use quote::{Tokens, ToTokens};
585
586 impl ToTokens for Ty {
587 fn to_tokens(&self, tokens: &mut Tokens) {
588 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700589 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700590 tokens.append("[");
591 inner.to_tokens(tokens);
592 tokens.append("]");
593 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700594 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700595 tokens.append("[");
596 inner.to_tokens(tokens);
597 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700598 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700599 tokens.append("]");
600 }
601 Ty::Ptr(ref target) => {
602 tokens.append("*");
603 match target.mutability {
604 Mutability::Mutable => tokens.append("mut"),
605 Mutability::Immutable => tokens.append("const"),
606 }
607 target.ty.to_tokens(tokens);
608 }
609 Ty::Rptr(ref lifetime, ref target) => {
610 tokens.append("&");
611 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700612 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700613 target.ty.to_tokens(tokens);
614 }
615 Ty::BareFn(ref func) => {
616 func.to_tokens(tokens);
617 }
618 Ty::Never => {
619 tokens.append("!");
620 }
621 Ty::Tup(ref elems) => {
622 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700623 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700624 if elems.len() == 1 {
625 tokens.append(",");
626 }
627 tokens.append(")");
628 }
David Tolnayf69904a2016-09-04 14:46:07 -0700629 Ty::Path(None, ref path) => {
630 path.to_tokens(tokens);
631 }
632 Ty::Path(Some(ref qself), ref path) => {
633 tokens.append("<");
634 qself.ty.to_tokens(tokens);
635 if qself.position > 0 {
636 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700637 for (i, segment) in path.segments
638 .iter()
639 .take(qself.position)
640 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700641 if i > 0 || path.global {
642 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700643 }
David Tolnayf69904a2016-09-04 14:46:07 -0700644 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700645 }
David Tolnayf69904a2016-09-04 14:46:07 -0700646 }
647 tokens.append(">");
648 for segment in path.segments.iter().skip(qself.position) {
649 tokens.append("::");
650 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700651 }
652 }
David Tolnay02c907f2017-01-23 00:06:37 -0800653 Ty::TraitObject(ref bounds) => {
David Tolnay6414da72016-10-08 00:55:17 -0700654 tokens.append_separated(bounds, "+");
655 }
David Tolnay87d0b442016-09-04 11:52:12 -0700656 Ty::ImplTrait(ref bounds) => {
657 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700658 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700659 }
660 Ty::Paren(ref inner) => {
661 tokens.append("(");
662 inner.to_tokens(tokens);
663 tokens.append(")");
664 }
665 Ty::Infer => {
666 tokens.append("_");
667 }
David Tolnay0047c712016-12-21 21:59:25 -0500668 Ty::Mac(ref mac) => mac.to_tokens(tokens),
David Tolnay87d0b442016-09-04 11:52:12 -0700669 }
670 }
671 }
672
David Tolnay47a877c2016-10-01 16:50:55 -0700673 impl ToTokens for Mutability {
674 fn to_tokens(&self, tokens: &mut Tokens) {
675 if let Mutability::Mutable = *self {
676 tokens.append("mut");
677 }
678 }
679 }
680
David Tolnay87d0b442016-09-04 11:52:12 -0700681 impl ToTokens for Path {
682 fn to_tokens(&self, tokens: &mut Tokens) {
683 for (i, segment) in self.segments.iter().enumerate() {
684 if i > 0 || self.global {
685 tokens.append("::");
686 }
687 segment.to_tokens(tokens);
688 }
689 }
690 }
691
692 impl ToTokens for PathSegment {
693 fn to_tokens(&self, tokens: &mut Tokens) {
694 self.ident.to_tokens(tokens);
David Tolnay5332d4b2016-10-30 14:25:22 -0700695 if self.ident.as_ref().is_empty() && self.parameters.is_empty() {
696 tokens.append("<");
697 tokens.append(">");
698 } else {
699 self.parameters.to_tokens(tokens);
700 }
David Tolnay87d0b442016-09-04 11:52:12 -0700701 }
702 }
703
704 impl ToTokens for PathParameters {
705 fn to_tokens(&self, tokens: &mut Tokens) {
706 match *self {
707 PathParameters::AngleBracketed(ref parameters) => {
708 parameters.to_tokens(tokens);
709 }
710 PathParameters::Parenthesized(ref parameters) => {
711 parameters.to_tokens(tokens);
712 }
713 }
714 }
715 }
716
717 impl ToTokens for AngleBracketedParameterData {
718 fn to_tokens(&self, tokens: &mut Tokens) {
719 let has_lifetimes = !self.lifetimes.is_empty();
720 let has_types = !self.types.is_empty();
721 let has_bindings = !self.bindings.is_empty();
722 if !has_lifetimes && !has_types && !has_bindings {
723 return;
724 }
725
726 tokens.append("<");
727
728 let mut first = true;
729 for lifetime in &self.lifetimes {
730 if !first {
731 tokens.append(",");
732 }
733 lifetime.to_tokens(tokens);
734 first = false;
735 }
736 for ty in &self.types {
737 if !first {
738 tokens.append(",");
739 }
740 ty.to_tokens(tokens);
741 first = false;
742 }
743 for binding in &self.bindings {
744 if !first {
745 tokens.append(",");
746 }
747 binding.to_tokens(tokens);
748 first = false;
749 }
750
751 tokens.append(">");
752 }
753 }
754
755 impl ToTokens for TypeBinding {
756 fn to_tokens(&self, tokens: &mut Tokens) {
757 self.ident.to_tokens(tokens);
758 tokens.append("=");
759 self.ty.to_tokens(tokens);
760 }
761 }
762
763 impl ToTokens for ParenthesizedParameterData {
764 fn to_tokens(&self, tokens: &mut Tokens) {
765 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700766 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700767 tokens.append(")");
768 if let Some(ref output) = self.output {
769 tokens.append("->");
770 output.to_tokens(tokens);
771 }
772 }
773 }
774
775 impl ToTokens for PolyTraitRef {
776 fn to_tokens(&self, tokens: &mut Tokens) {
777 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700778 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700779 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700780 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700781 tokens.append(">");
782 }
783 self.trait_ref.to_tokens(tokens);
784 }
785 }
786
787 impl ToTokens for BareFnTy {
788 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700789 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700790 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700791 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700792 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700793 tokens.append(">");
794 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700795 self.unsafety.to_tokens(tokens);
796 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700797 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700798 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700799 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700800 if self.variadic {
801 if !self.inputs.is_empty() {
802 tokens.append(",");
803 }
804 tokens.append("...");
805 }
David Tolnay87d0b442016-09-04 11:52:12 -0700806 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700807 if let FunctionRetTy::Ty(ref ty) = self.output {
808 tokens.append("->");
809 ty.to_tokens(tokens);
810 }
811 }
812 }
813
David Tolnay62f374c2016-10-02 13:37:00 -0700814 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700815 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700816 if let Some(ref name) = self.name {
817 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700818 tokens.append(":");
819 }
820 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700821 }
822 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700823
824 impl ToTokens for Unsafety {
825 fn to_tokens(&self, tokens: &mut Tokens) {
826 match *self {
827 Unsafety::Unsafe => tokens.append("unsafe"),
828 Unsafety::Normal => {
829 // nothing
830 }
831 }
832 }
833 }
834
835 impl ToTokens for Abi {
836 fn to_tokens(&self, tokens: &mut Tokens) {
837 tokens.append("extern");
838 match *self {
839 Abi::Named(ref named) => named.to_tokens(tokens),
David Tolnay76195ce2016-10-30 16:53:48 -0700840 Abi::Rust => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -0700841 }
842 }
843 }
David Tolnay87d0b442016-09-04 11:52:12 -0700844}