blob: f9e3a2dd423a48a43527448c3fe7b0d3af57e1a6 [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 Tolnayb79ee962016-09-04 09:39:20 -07004#[derive(Debug, Clone, Eq, PartialEq)]
5pub 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),
25 /// Something like `A+B`. Note that `B` must always be a path.
26 ObjectSum(Box<Ty>, Vec<TyParamBound>),
27 /// A type like `for<'a> Foo<&'a Bar>`
28 PolyTraitRef(Vec<TyParamBound>),
29 /// An `impl TraitA+TraitB` type.
30 ImplTrait(Vec<TyParamBound>),
31 /// No-op; kept solely so that we can pretty-print faithfully
32 Paren(Box<Ty>),
33 /// TyKind::Infer means the type should be inferred instead of it having been
34 /// specified. This can appear anywhere in a type.
35 Infer,
36}
37
38#[derive(Debug, Clone, Eq, PartialEq)]
39pub struct MutTy {
40 pub ty: Ty,
41 pub mutability: Mutability,
42}
43
David Tolnayf4bbbd92016-09-23 14:41:55 -070044#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayb79ee962016-09-04 09:39:20 -070045pub enum Mutability {
46 Mutable,
47 Immutable,
48}
49
David Tolnay771ecf42016-09-23 19:26:37 -070050/// A "Path" is essentially Rust's notion of a name.
51///
52/// It's represented as a sequence of identifiers,
53/// along with a bunch of supporting information.
54///
55/// E.g. `std::cmp::PartialEq`
David Tolnayb79ee962016-09-04 09:39:20 -070056#[derive(Debug, Clone, Eq, PartialEq)]
57pub struct Path {
58 pub global: bool,
59 pub segments: Vec<PathSegment>,
60}
61
David Tolnaydaaf7742016-10-03 11:11:43 -070062impl<T> From<T> for Path
63 where T: Into<PathSegment>
64{
David Tolnay84aa0752016-10-02 23:01:13 -070065 fn from(segment: T) -> Self {
66 Path {
67 global: false,
68 segments: vec![segment.into()],
69 }
70 }
71}
72
David Tolnayb79ee962016-09-04 09:39:20 -070073/// A segment of a path: an identifier, an optional lifetime, and a set of types.
74///
75/// E.g. `std`, `String` or `Box<T>`
76#[derive(Debug, Clone, Eq, PartialEq)]
77pub struct PathSegment {
78 pub ident: Ident,
79 pub parameters: PathParameters,
80}
81
David Tolnaydaaf7742016-10-03 11:11:43 -070082impl<T> From<T> for PathSegment
83 where T: Into<Ident>
84{
David Tolnay84aa0752016-10-02 23:01:13 -070085 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -070086 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -070087 ident: ident.into(),
David Tolnayb79ee962016-09-04 09:39:20 -070088 parameters: PathParameters::none(),
89 }
90 }
91}
92
93/// Parameters of a path segment.
94///
95/// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
96#[derive(Debug, Clone, Eq, PartialEq)]
97pub enum PathParameters {
98 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
99 AngleBracketed(AngleBracketedParameterData),
100 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
101 Parenthesized(ParenthesizedParameterData),
102}
103
104impl PathParameters {
105 pub fn none() -> Self {
106 PathParameters::AngleBracketed(AngleBracketedParameterData::default())
107 }
108}
109
110/// A path like `Foo<'a, T>`
111#[derive(Debug, Clone, Eq, PartialEq, Default)]
112pub struct AngleBracketedParameterData {
113 /// The lifetime parameters for this path segment.
114 pub lifetimes: Vec<Lifetime>,
115 /// The type parameters for this path segment, if present.
116 pub types: Vec<Ty>,
117 /// Bindings (equality constraints) on associated types, if present.
118 ///
119 /// E.g., `Foo<A=Bar>`.
120 pub bindings: Vec<TypeBinding>,
121}
122
123/// Bind a type to an associated type: `A=Foo`.
124#[derive(Debug, Clone, Eq, PartialEq)]
125pub struct TypeBinding {
126 pub ident: Ident,
127 pub ty: Ty,
128}
129
130/// A path like `Foo(A,B) -> C`
131#[derive(Debug, Clone, Eq, PartialEq)]
132pub struct ParenthesizedParameterData {
133 /// `(A, B)`
134 pub inputs: Vec<Ty>,
135 /// `C`
136 pub output: Option<Ty>,
137}
138
139#[derive(Debug, Clone, Eq, PartialEq)]
140pub struct PolyTraitRef {
141 /// The `'a` in `<'a> Foo<&'a T>`
142 pub bound_lifetimes: Vec<LifetimeDef>,
143 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
144 pub trait_ref: Path,
145}
146
147/// The explicit Self type in a "qualified path". The actual
148/// path, including the trait and the associated item, is stored
149/// separately. `position` represents the index of the associated
150/// item qualified with this Self type.
151///
152/// ```rust,ignore
153/// <Vec<T> as a::b::Trait>::AssociatedItem
154/// ^~~~~ ~~~~~~~~~~~~~~^
155/// ty position = 3
156///
157/// <Vec<T>>::AssociatedItem
158/// ^~~~~ ^
159/// ty position = 0
160/// ```
161#[derive(Debug, Clone, Eq, PartialEq)]
162pub struct QSelf {
163 pub ty: Box<Ty>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700164 pub position: usize,
David Tolnayb79ee962016-09-04 09:39:20 -0700165}
166
167#[derive(Debug, Clone, Eq, PartialEq)]
168pub struct BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700169 pub unsafety: Unsafety,
170 pub abi: Option<Abi>,
David Tolnayb79ee962016-09-04 09:39:20 -0700171 pub lifetimes: Vec<LifetimeDef>,
David Tolnay62f374c2016-10-02 13:37:00 -0700172 pub inputs: Vec<BareFnArg>,
David Tolnayb79ee962016-09-04 09:39:20 -0700173 pub output: FunctionRetTy,
David Tolnay292e6002016-10-29 22:03:51 -0700174 pub variadic: bool,
David Tolnayb79ee962016-09-04 09:39:20 -0700175}
176
David Tolnayb8d8ef52016-10-29 14:30:08 -0700177#[derive(Debug, Copy, Clone, Eq, PartialEq)]
178pub enum Unsafety {
179 Unsafe,
180 Normal,
181}
182
183#[derive(Debug, Clone, Eq, PartialEq)]
184pub enum Abi {
185 Named(String),
186 Extern,
187}
188
David Tolnay62f374c2016-10-02 13:37:00 -0700189/// An argument in a function type.
David Tolnayb79ee962016-09-04 09:39:20 -0700190///
191/// E.g. `bar: usize` as in `fn foo(bar: usize)`
192#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay62f374c2016-10-02 13:37:00 -0700193pub struct BareFnArg {
194 pub name: Option<Ident>,
David Tolnayb79ee962016-09-04 09:39:20 -0700195 pub ty: Ty,
196}
197
198#[derive(Debug, Clone, Eq, PartialEq)]
199pub enum FunctionRetTy {
200 /// Return type is not specified.
201 ///
202 /// Functions default to `()` and
203 /// closures default to inference. Span points to where return
204 /// type would be inserted.
205 Default,
206 /// Everything else
207 Ty(Ty),
208}
209
David Tolnay86eca752016-09-04 11:26:41 -0700210#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700211pub mod parsing {
212 use super::*;
David Tolnay6414da72016-10-08 00:55:17 -0700213 use {TraitBoundModifier, TyParamBound};
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700214 #[cfg(feature = "full")]
215 use ConstExpr;
David Tolnay3cb23a92016-10-07 23:02:21 -0700216 use constant::parsing::const_expr;
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700217 #[cfg(feature = "full")]
218 use expr::parsing::expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700219 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700220 use ident::parsing::ident;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700221 use lit::parsing::quoted_string;
David Tolnay9d8f1972016-09-04 11:58:48 -0700222 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700223
David Tolnayb5a7b142016-09-13 22:46:39 -0700224 named!(pub ty -> Ty, alt!(
David Tolnayd040d772016-10-25 21:33:51 -0700225 ty_paren // must be before ty_tup
226 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700227 ty_path // must be before ty_poly_trait_ref
228 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700229 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700230 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700231 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700232 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700233 ty_ptr
234 |
235 ty_rptr
236 |
237 ty_bare_fn
238 |
239 ty_never
240 |
241 ty_tup
242 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700243 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700244 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700245 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700246 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700247
David Tolnayb5a7b142016-09-13 22:46:39 -0700248 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700249 punct!("[") >>
250 elem: ty >>
251 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700252 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700253 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700254
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700255 #[cfg(not(feature = "full"))]
David Tolnayfa94b6f2016-10-05 23:26:11 -0700256 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700257 punct!("[") >>
258 elem: ty >>
259 punct!(";") >>
David Tolnay3cb23a92016-10-07 23:02:21 -0700260 len: const_expr >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700261 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700262 (Ty::Array(Box::new(elem), len))
263 ));
264
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700265 #[cfg(feature = "full")]
266 named!(ty_array -> Ty, do_parse!(
267 punct!("[") >>
268 elem: ty >>
269 punct!(";") >>
270 len: alt!(
271 terminated!(const_expr, punct!("]"))
272 |
273 terminated!(expr, punct!("]")) => { ConstExpr::Other }
274 ) >>
275 (Ty::Array(Box::new(elem), len))
276 ));
277
David Tolnayb5a7b142016-09-13 22:46:39 -0700278 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700279 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700280 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700281 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700282 |
David Tolnay10413f02016-09-30 09:12:02 -0700283 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700284 ) >>
285 target: ty >>
286 (Ty::Ptr(Box::new(MutTy {
287 ty: target,
288 mutability: mutability,
289 })))
290 ));
291
David Tolnayb5a7b142016-09-13 22:46:39 -0700292 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700293 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700294 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700295 mutability: mutability >>
296 target: ty >>
297 (Ty::Rptr(life, Box::new(MutTy {
298 ty: target,
299 mutability: mutability,
300 })))
301 ));
302
David Tolnayb5a7b142016-09-13 22:46:39 -0700303 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay4f121832016-10-25 21:33:36 -0700304 lifetimes: opt_vec!(do_parse!(
305 keyword!("for") >>
306 punct!("<") >>
307 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
308 punct!(">") >>
309 (lifetimes)
David Tolnay6b7aaf02016-09-04 10:39:25 -0700310 )) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700311 unsafety: unsafety >>
312 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700313 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700314 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700315 inputs: separated_list!(punct!(","), fn_arg) >>
316 trailing_comma: option!(punct!(",")) >>
317 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700318 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700319 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700320 punct!("->"),
321 ty
322 )) >>
323 (Ty::BareFn(Box::new(BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700324 unsafety: unsafety,
325 abi: abi,
David Tolnay9d8f1972016-09-04 11:58:48 -0700326 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700327 inputs: inputs,
328 output: match output {
329 Some(ty) => FunctionRetTy::Ty(ty),
330 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700331 },
David Tolnay292e6002016-10-29 22:03:51 -0700332 variadic: variadic.is_some(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700333 })))
334 ));
335
David Tolnayb5a7b142016-09-13 22:46:39 -0700336 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700337
David Tolnayb5a7b142016-09-13 22:46:39 -0700338 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700339 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700340 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700341 punct!(")") >>
342 (Ty::Tup(elems))
343 ));
344
David Tolnay6414da72016-10-08 00:55:17 -0700345 named!(ty_path -> Ty, do_parse!(
346 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700347 parenthesized: cond!(
348 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
349 option!(parenthesized_parameter_data)
350 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700351 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
352 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700353 let (qself, mut path) = qpath;
354 if let Some(Some(parenthesized)) = parenthesized {
355 path.segments.last_mut().unwrap().parameters = parenthesized;
356 }
357 let path = Ty::Path(qself, path);
David Tolnay6414da72016-10-08 00:55:17 -0700358 if bounds.is_empty() {
359 path
360 } else {
361 Ty::ObjectSum(Box::new(path), bounds)
362 }
363 })
364 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700365
David Tolnayf6c74402016-10-08 02:31:26 -0700366 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
367 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700368 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700369 punct!(")") >>
370 output: option!(preceded!(
371 punct!("->"),
372 ty
373 )) >>
374 (PathParameters::Parenthesized(
375 ParenthesizedParameterData {
376 inputs: inputs,
377 output: output,
378 },
379 ))
380 ));
381
David Tolnay9636c052016-10-02 17:11:17 -0700382 named!(pub qpath -> (Option<QSelf>, Path), alt!(
383 map!(path, |p| (None, p))
384 |
385 do_parse!(
386 punct!("<") >>
387 this: map!(ty, Box::new) >>
388 path: option!(preceded!(
389 keyword!("as"),
390 path
391 )) >>
392 punct!(">") >>
393 punct!("::") >>
394 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
395 ({
396 match path {
397 Some(mut path) => {
398 let pos = path.segments.len();
399 path.segments.extend(rest);
400 (Some(QSelf { ty: this, position: pos }), path)
401 }
402 None => {
403 (Some(QSelf { ty: this, position: 0 }), Path {
404 global: false,
405 segments: rest,
406 })
407 }
David Tolnayb79ee962016-09-04 09:39:20 -0700408 }
David Tolnay9636c052016-10-02 17:11:17 -0700409 })
410 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700411 |
412 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700413 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700414
David Tolnay4f0f2512016-10-30 09:28:14 -0700415 named!(ty_poly_trait_ref -> Ty, map!(
416 poly_trait_ref,
417 |trait_ref| Ty::PolyTraitRef(vec![
418 TyParamBound::Trait(trait_ref, TraitBoundModifier::None),
419 ])
David Tolnay6414da72016-10-08 00:55:17 -0700420 ));
421
David Tolnayb5a7b142016-09-13 22:46:39 -0700422 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700423 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700424 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
425 (Ty::ImplTrait(elem))
426 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700427
David Tolnayb5a7b142016-09-13 22:46:39 -0700428 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700429 punct!("(") >>
430 elem: ty >>
431 punct!(")") >>
432 (Ty::Paren(Box::new(elem)))
433 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700434
David Tolnay47a877c2016-10-01 16:50:55 -0700435 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700436 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700437 |
438 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700439 ));
440
David Tolnayb5a7b142016-09-13 22:46:39 -0700441 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700442 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700443 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
444 (Path {
445 global: global.is_some(),
446 segments: segments,
447 })
448 ));
449
David Tolnay9636c052016-10-02 17:11:17 -0700450 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700451 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700452 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700453 punct!("<") >>
454 lifetimes: separated_list!(punct!(","), lifetime) >>
455 types: opt_vec!(preceded!(
456 cond!(!lifetimes.is_empty(), punct!(",")),
457 separated_nonempty_list!(
458 punct!(","),
459 terminated!(ty, not!(peek!(punct!("="))))
460 )
461 )) >>
462 bindings: opt_vec!(preceded!(
463 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
464 separated_nonempty_list!(punct!(","), type_binding)
465 )) >>
466 punct!(">") >>
467 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700468 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700469 parameters: PathParameters::AngleBracketed(
470 AngleBracketedParameterData {
471 lifetimes: lifetimes,
472 types: types,
473 bindings: bindings,
474 }
475 ),
476 })
477 )
478 |
David Tolnay84aa0752016-10-02 23:01:13 -0700479 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700480 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700481 map!(alt!(
482 keyword!("super")
483 |
484 keyword!("self")
485 |
486 keyword!("Self")
487 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700488 ));
489
David Tolnayb5a7b142016-09-13 22:46:39 -0700490 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700491 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700492 punct!("=") >>
493 ty: ty >>
494 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700495 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700496 ty: ty,
497 })
498 ));
499
David Tolnayb5a7b142016-09-13 22:46:39 -0700500 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700501 bound_lifetimes: bound_lifetimes >>
502 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700503 parenthesized: option!(cond_reduce!(
David Tolnayf6c74402016-10-08 02:31:26 -0700504 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700505 parenthesized_parameter_data
506 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700507 ({
508 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700509 if let Some(parenthesized) = parenthesized {
David Tolnayf6c74402016-10-08 02:31:26 -0700510 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
511 }
512 PolyTraitRef {
513 bound_lifetimes: bound_lifetimes,
514 trait_ref: trait_ref,
515 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700516 })
517 ));
518
David Tolnay62f374c2016-10-02 13:37:00 -0700519 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700520 name: option!(do_parse!(
521 name: ident >>
522 punct!(":") >>
523 not!(peek!(tag!(":"))) >> // not ::
524 (name)
525 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700526 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700527 (BareFnArg {
528 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700529 ty: ty,
530 })
531 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700532
533 named!(pub unsafety -> Unsafety, alt!(
534 keyword!("unsafe") => { |_| Unsafety::Unsafe }
535 |
536 epsilon!() => { |_| Unsafety::Normal }
537 ));
538
539 named!(pub abi -> Abi, do_parse!(
540 keyword!("extern") >>
541 name: option!(quoted_string) >>
542 (match name {
543 Some(name) => Abi::Named(name),
544 None => Abi::Extern,
545 })
546 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700547}
David Tolnay87d0b442016-09-04 11:52:12 -0700548
549#[cfg(feature = "printing")]
550mod printing {
551 use super::*;
552 use quote::{Tokens, ToTokens};
553
554 impl ToTokens for Ty {
555 fn to_tokens(&self, tokens: &mut Tokens) {
556 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700557 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700558 tokens.append("[");
559 inner.to_tokens(tokens);
560 tokens.append("]");
561 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700562 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700563 tokens.append("[");
564 inner.to_tokens(tokens);
565 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700566 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700567 tokens.append("]");
568 }
569 Ty::Ptr(ref target) => {
570 tokens.append("*");
571 match target.mutability {
572 Mutability::Mutable => tokens.append("mut"),
573 Mutability::Immutable => tokens.append("const"),
574 }
575 target.ty.to_tokens(tokens);
576 }
577 Ty::Rptr(ref lifetime, ref target) => {
578 tokens.append("&");
579 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700580 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700581 target.ty.to_tokens(tokens);
582 }
583 Ty::BareFn(ref func) => {
584 func.to_tokens(tokens);
585 }
586 Ty::Never => {
587 tokens.append("!");
588 }
589 Ty::Tup(ref elems) => {
590 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700591 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700592 if elems.len() == 1 {
593 tokens.append(",");
594 }
595 tokens.append(")");
596 }
David Tolnayf69904a2016-09-04 14:46:07 -0700597 Ty::Path(None, ref path) => {
598 path.to_tokens(tokens);
599 }
600 Ty::Path(Some(ref qself), ref path) => {
601 tokens.append("<");
602 qself.ty.to_tokens(tokens);
603 if qself.position > 0 {
604 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700605 for (i, segment) in path.segments
606 .iter()
607 .take(qself.position)
608 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700609 if i > 0 || path.global {
610 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700611 }
David Tolnayf69904a2016-09-04 14:46:07 -0700612 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700613 }
David Tolnayf69904a2016-09-04 14:46:07 -0700614 }
615 tokens.append(">");
616 for segment in path.segments.iter().skip(qself.position) {
617 tokens.append("::");
618 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700619 }
620 }
David Tolnay6414da72016-10-08 00:55:17 -0700621 Ty::ObjectSum(ref ty, ref bounds) => {
622 ty.to_tokens(tokens);
623 for bound in bounds {
624 tokens.append("+");
625 bound.to_tokens(tokens);
626 }
627 }
628 Ty::PolyTraitRef(ref bounds) => {
629 tokens.append_separated(bounds, "+");
630 }
David Tolnay87d0b442016-09-04 11:52:12 -0700631 Ty::ImplTrait(ref bounds) => {
632 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700633 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700634 }
635 Ty::Paren(ref inner) => {
636 tokens.append("(");
637 inner.to_tokens(tokens);
638 tokens.append(")");
639 }
640 Ty::Infer => {
641 tokens.append("_");
642 }
643 }
644 }
645 }
646
David Tolnay47a877c2016-10-01 16:50:55 -0700647 impl ToTokens for Mutability {
648 fn to_tokens(&self, tokens: &mut Tokens) {
649 if let Mutability::Mutable = *self {
650 tokens.append("mut");
651 }
652 }
653 }
654
David Tolnay87d0b442016-09-04 11:52:12 -0700655 impl ToTokens for Path {
656 fn to_tokens(&self, tokens: &mut Tokens) {
657 for (i, segment) in self.segments.iter().enumerate() {
658 if i > 0 || self.global {
659 tokens.append("::");
660 }
661 segment.to_tokens(tokens);
662 }
663 }
664 }
665
666 impl ToTokens for PathSegment {
667 fn to_tokens(&self, tokens: &mut Tokens) {
668 self.ident.to_tokens(tokens);
669 self.parameters.to_tokens(tokens);
670 }
671 }
672
673 impl ToTokens for PathParameters {
674 fn to_tokens(&self, tokens: &mut Tokens) {
675 match *self {
676 PathParameters::AngleBracketed(ref parameters) => {
677 parameters.to_tokens(tokens);
678 }
679 PathParameters::Parenthesized(ref parameters) => {
680 parameters.to_tokens(tokens);
681 }
682 }
683 }
684 }
685
686 impl ToTokens for AngleBracketedParameterData {
687 fn to_tokens(&self, tokens: &mut Tokens) {
688 let has_lifetimes = !self.lifetimes.is_empty();
689 let has_types = !self.types.is_empty();
690 let has_bindings = !self.bindings.is_empty();
691 if !has_lifetimes && !has_types && !has_bindings {
692 return;
693 }
694
695 tokens.append("<");
696
697 let mut first = true;
698 for lifetime in &self.lifetimes {
699 if !first {
700 tokens.append(",");
701 }
702 lifetime.to_tokens(tokens);
703 first = false;
704 }
705 for ty in &self.types {
706 if !first {
707 tokens.append(",");
708 }
709 ty.to_tokens(tokens);
710 first = false;
711 }
712 for binding in &self.bindings {
713 if !first {
714 tokens.append(",");
715 }
716 binding.to_tokens(tokens);
717 first = false;
718 }
719
720 tokens.append(">");
721 }
722 }
723
724 impl ToTokens for TypeBinding {
725 fn to_tokens(&self, tokens: &mut Tokens) {
726 self.ident.to_tokens(tokens);
727 tokens.append("=");
728 self.ty.to_tokens(tokens);
729 }
730 }
731
732 impl ToTokens for ParenthesizedParameterData {
733 fn to_tokens(&self, tokens: &mut Tokens) {
734 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700735 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700736 tokens.append(")");
737 if let Some(ref output) = self.output {
738 tokens.append("->");
739 output.to_tokens(tokens);
740 }
741 }
742 }
743
744 impl ToTokens for PolyTraitRef {
745 fn to_tokens(&self, tokens: &mut Tokens) {
746 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700747 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700748 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700749 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700750 tokens.append(">");
751 }
752 self.trait_ref.to_tokens(tokens);
753 }
754 }
755
756 impl ToTokens for BareFnTy {
757 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700758 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700759 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700760 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700761 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700762 tokens.append(">");
763 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700764 self.unsafety.to_tokens(tokens);
765 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700766 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700767 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700768 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700769 if self.variadic {
770 if !self.inputs.is_empty() {
771 tokens.append(",");
772 }
773 tokens.append("...");
774 }
David Tolnay87d0b442016-09-04 11:52:12 -0700775 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700776 if let FunctionRetTy::Ty(ref ty) = self.output {
777 tokens.append("->");
778 ty.to_tokens(tokens);
779 }
780 }
781 }
782
David Tolnay62f374c2016-10-02 13:37:00 -0700783 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700784 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700785 if let Some(ref name) = self.name {
786 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700787 tokens.append(":");
788 }
789 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700790 }
791 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700792
793 impl ToTokens for Unsafety {
794 fn to_tokens(&self, tokens: &mut Tokens) {
795 match *self {
796 Unsafety::Unsafe => tokens.append("unsafe"),
797 Unsafety::Normal => {
798 // nothing
799 }
800 }
801 }
802 }
803
804 impl ToTokens for Abi {
805 fn to_tokens(&self, tokens: &mut Tokens) {
806 tokens.append("extern");
807 match *self {
808 Abi::Named(ref named) => named.to_tokens(tokens),
809 Abi::Extern => {}
810 }
811 }
812 }
David Tolnay87d0b442016-09-04 11:52:12 -0700813}