blob: 5eab736a7646f6450e1633d0c0a187722137d354 [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 Tolnay3cb23a92016-10-07 23:02:21 -0700214 use constant::parsing::const_expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700215 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700216 use ident::parsing::ident;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700217 use lit::parsing::quoted_string;
David Tolnay9d8f1972016-09-04 11:58:48 -0700218 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700219
David Tolnayb5a7b142016-09-13 22:46:39 -0700220 named!(pub ty -> Ty, alt!(
David Tolnay6414da72016-10-08 00:55:17 -0700221 ty_poly_trait_ref // must be before ty_path
222 |
David Tolnayd040d772016-10-25 21:33:51 -0700223 ty_paren // must be before ty_tup
224 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700225 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700226 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700227 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700228 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700229 ty_ptr
230 |
231 ty_rptr
232 |
233 ty_bare_fn
234 |
235 ty_never
236 |
237 ty_tup
238 |
239 ty_path
240 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700241 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700242 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700243
David Tolnayb5a7b142016-09-13 22:46:39 -0700244 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700245 punct!("[") >>
246 elem: ty >>
247 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700248 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700249 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700250
David Tolnayfa94b6f2016-10-05 23:26:11 -0700251 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700252 punct!("[") >>
253 elem: ty >>
254 punct!(";") >>
David Tolnay3cb23a92016-10-07 23:02:21 -0700255 len: const_expr >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700256 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700257 (Ty::Array(Box::new(elem), len))
258 ));
259
David Tolnayb5a7b142016-09-13 22:46:39 -0700260 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700261 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700262 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700263 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700264 |
David Tolnay10413f02016-09-30 09:12:02 -0700265 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700266 ) >>
267 target: ty >>
268 (Ty::Ptr(Box::new(MutTy {
269 ty: target,
270 mutability: mutability,
271 })))
272 ));
273
David Tolnayb5a7b142016-09-13 22:46:39 -0700274 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700275 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700276 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700277 mutability: mutability >>
278 target: ty >>
279 (Ty::Rptr(life, Box::new(MutTy {
280 ty: target,
281 mutability: mutability,
282 })))
283 ));
284
David Tolnayb5a7b142016-09-13 22:46:39 -0700285 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay4f121832016-10-25 21:33:36 -0700286 lifetimes: opt_vec!(do_parse!(
287 keyword!("for") >>
288 punct!("<") >>
289 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
290 punct!(">") >>
291 (lifetimes)
David Tolnay6b7aaf02016-09-04 10:39:25 -0700292 )) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700293 unsafety: unsafety >>
294 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700295 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700296 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700297 inputs: separated_list!(punct!(","), fn_arg) >>
298 trailing_comma: option!(punct!(",")) >>
299 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700300 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700301 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700302 punct!("->"),
303 ty
304 )) >>
305 (Ty::BareFn(Box::new(BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700306 unsafety: unsafety,
307 abi: abi,
David Tolnay9d8f1972016-09-04 11:58:48 -0700308 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700309 inputs: inputs,
310 output: match output {
311 Some(ty) => FunctionRetTy::Ty(ty),
312 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700313 },
David Tolnay292e6002016-10-29 22:03:51 -0700314 variadic: variadic.is_some(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700315 })))
316 ));
317
David Tolnayb5a7b142016-09-13 22:46:39 -0700318 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700319
David Tolnayb5a7b142016-09-13 22:46:39 -0700320 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700321 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700322 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700323 punct!(")") >>
324 (Ty::Tup(elems))
325 ));
326
David Tolnay6414da72016-10-08 00:55:17 -0700327 named!(ty_path -> Ty, do_parse!(
328 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700329 parenthesized: cond!(
330 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
331 option!(parenthesized_parameter_data)
332 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700333 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
334 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700335 let (qself, mut path) = qpath;
336 if let Some(Some(parenthesized)) = parenthesized {
337 path.segments.last_mut().unwrap().parameters = parenthesized;
338 }
339 let path = Ty::Path(qself, path);
David Tolnay6414da72016-10-08 00:55:17 -0700340 if bounds.is_empty() {
341 path
342 } else {
343 Ty::ObjectSum(Box::new(path), bounds)
344 }
345 })
346 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700347
David Tolnayf6c74402016-10-08 02:31:26 -0700348 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
349 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700350 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700351 punct!(")") >>
352 output: option!(preceded!(
353 punct!("->"),
354 ty
355 )) >>
356 (PathParameters::Parenthesized(
357 ParenthesizedParameterData {
358 inputs: inputs,
359 output: output,
360 },
361 ))
362 ));
363
David Tolnay9636c052016-10-02 17:11:17 -0700364 named!(pub qpath -> (Option<QSelf>, Path), alt!(
365 map!(path, |p| (None, p))
366 |
367 do_parse!(
368 punct!("<") >>
369 this: map!(ty, Box::new) >>
370 path: option!(preceded!(
371 keyword!("as"),
372 path
373 )) >>
374 punct!(">") >>
375 punct!("::") >>
376 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
377 ({
378 match path {
379 Some(mut path) => {
380 let pos = path.segments.len();
381 path.segments.extend(rest);
382 (Some(QSelf { ty: this, position: pos }), path)
383 }
384 None => {
385 (Some(QSelf { ty: this, position: 0 }), Path {
386 global: false,
387 segments: rest,
388 })
389 }
David Tolnayb79ee962016-09-04 09:39:20 -0700390 }
David Tolnay9636c052016-10-02 17:11:17 -0700391 })
392 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700393 |
394 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700395 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700396
David Tolnay6414da72016-10-08 00:55:17 -0700397 named!(ty_poly_trait_ref -> Ty, do_parse!(
398 keyword!("for") >>
399 punct!("<") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700400 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
David Tolnay6414da72016-10-08 00:55:17 -0700401 punct!(">") >>
402 trait_ref: path >>
403 (Ty::PolyTraitRef(vec![
404 TyParamBound::Trait(
405 PolyTraitRef {
406 bound_lifetimes: lifetimes,
407 trait_ref: trait_ref,
408 },
409 TraitBoundModifier::None,
410 ),
411 ]))
412 ));
413
David Tolnayb5a7b142016-09-13 22:46:39 -0700414 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700415 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700416 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
417 (Ty::ImplTrait(elem))
418 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700419
David Tolnayb5a7b142016-09-13 22:46:39 -0700420 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700421 punct!("(") >>
422 elem: ty >>
423 punct!(")") >>
424 (Ty::Paren(Box::new(elem)))
425 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700426
David Tolnay47a877c2016-10-01 16:50:55 -0700427 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700428 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700429 |
430 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700431 ));
432
David Tolnayb5a7b142016-09-13 22:46:39 -0700433 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700434 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700435 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
436 (Path {
437 global: global.is_some(),
438 segments: segments,
439 })
440 ));
441
David Tolnay9636c052016-10-02 17:11:17 -0700442 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700443 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700444 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700445 punct!("<") >>
446 lifetimes: separated_list!(punct!(","), lifetime) >>
447 types: opt_vec!(preceded!(
448 cond!(!lifetimes.is_empty(), punct!(",")),
449 separated_nonempty_list!(
450 punct!(","),
451 terminated!(ty, not!(peek!(punct!("="))))
452 )
453 )) >>
454 bindings: opt_vec!(preceded!(
455 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
456 separated_nonempty_list!(punct!(","), type_binding)
457 )) >>
458 punct!(">") >>
459 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700460 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700461 parameters: PathParameters::AngleBracketed(
462 AngleBracketedParameterData {
463 lifetimes: lifetimes,
464 types: types,
465 bindings: bindings,
466 }
467 ),
468 })
469 )
470 |
David Tolnay84aa0752016-10-02 23:01:13 -0700471 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700472 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700473 map!(alt!(
474 keyword!("super")
475 |
476 keyword!("self")
477 |
478 keyword!("Self")
479 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700480 ));
481
David Tolnayb5a7b142016-09-13 22:46:39 -0700482 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700483 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700484 punct!("=") >>
485 ty: ty >>
486 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700487 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700488 ty: ty,
489 })
490 ));
491
David Tolnayb5a7b142016-09-13 22:46:39 -0700492 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700493 bound_lifetimes: bound_lifetimes >>
494 trait_ref: path >>
David Tolnayf6c74402016-10-08 02:31:26 -0700495 parenthesized: cond!(
496 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
497 option!(parenthesized_parameter_data)
498 ) >>
499 ({
500 let mut trait_ref = trait_ref;
501 if let Some(Some(parenthesized)) = parenthesized {
502 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
503 }
504 PolyTraitRef {
505 bound_lifetimes: bound_lifetimes,
506 trait_ref: trait_ref,
507 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700508 })
509 ));
510
David Tolnay62f374c2016-10-02 13:37:00 -0700511 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700512 name: option!(do_parse!(
513 name: ident >>
514 punct!(":") >>
515 not!(peek!(tag!(":"))) >> // not ::
516 (name)
517 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700518 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700519 (BareFnArg {
520 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700521 ty: ty,
522 })
523 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700524
525 named!(pub unsafety -> Unsafety, alt!(
526 keyword!("unsafe") => { |_| Unsafety::Unsafe }
527 |
528 epsilon!() => { |_| Unsafety::Normal }
529 ));
530
531 named!(pub abi -> Abi, do_parse!(
532 keyword!("extern") >>
533 name: option!(quoted_string) >>
534 (match name {
535 Some(name) => Abi::Named(name),
536 None => Abi::Extern,
537 })
538 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700539}
David Tolnay87d0b442016-09-04 11:52:12 -0700540
541#[cfg(feature = "printing")]
542mod printing {
543 use super::*;
544 use quote::{Tokens, ToTokens};
545
546 impl ToTokens for Ty {
547 fn to_tokens(&self, tokens: &mut Tokens) {
548 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700549 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700550 tokens.append("[");
551 inner.to_tokens(tokens);
552 tokens.append("]");
553 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700554 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700555 tokens.append("[");
556 inner.to_tokens(tokens);
557 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700558 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700559 tokens.append("]");
560 }
561 Ty::Ptr(ref target) => {
562 tokens.append("*");
563 match target.mutability {
564 Mutability::Mutable => tokens.append("mut"),
565 Mutability::Immutable => tokens.append("const"),
566 }
567 target.ty.to_tokens(tokens);
568 }
569 Ty::Rptr(ref lifetime, ref target) => {
570 tokens.append("&");
571 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700572 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700573 target.ty.to_tokens(tokens);
574 }
575 Ty::BareFn(ref func) => {
576 func.to_tokens(tokens);
577 }
578 Ty::Never => {
579 tokens.append("!");
580 }
581 Ty::Tup(ref elems) => {
582 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700583 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700584 if elems.len() == 1 {
585 tokens.append(",");
586 }
587 tokens.append(")");
588 }
David Tolnayf69904a2016-09-04 14:46:07 -0700589 Ty::Path(None, ref path) => {
590 path.to_tokens(tokens);
591 }
592 Ty::Path(Some(ref qself), ref path) => {
593 tokens.append("<");
594 qself.ty.to_tokens(tokens);
595 if qself.position > 0 {
596 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700597 for (i, segment) in path.segments
598 .iter()
599 .take(qself.position)
600 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700601 if i > 0 || path.global {
602 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700603 }
David Tolnayf69904a2016-09-04 14:46:07 -0700604 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700605 }
David Tolnayf69904a2016-09-04 14:46:07 -0700606 }
607 tokens.append(">");
608 for segment in path.segments.iter().skip(qself.position) {
609 tokens.append("::");
610 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700611 }
612 }
David Tolnay6414da72016-10-08 00:55:17 -0700613 Ty::ObjectSum(ref ty, ref bounds) => {
614 ty.to_tokens(tokens);
615 for bound in bounds {
616 tokens.append("+");
617 bound.to_tokens(tokens);
618 }
619 }
620 Ty::PolyTraitRef(ref bounds) => {
621 tokens.append_separated(bounds, "+");
622 }
David Tolnay87d0b442016-09-04 11:52:12 -0700623 Ty::ImplTrait(ref bounds) => {
624 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700625 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700626 }
627 Ty::Paren(ref inner) => {
628 tokens.append("(");
629 inner.to_tokens(tokens);
630 tokens.append(")");
631 }
632 Ty::Infer => {
633 tokens.append("_");
634 }
635 }
636 }
637 }
638
David Tolnay47a877c2016-10-01 16:50:55 -0700639 impl ToTokens for Mutability {
640 fn to_tokens(&self, tokens: &mut Tokens) {
641 if let Mutability::Mutable = *self {
642 tokens.append("mut");
643 }
644 }
645 }
646
David Tolnay87d0b442016-09-04 11:52:12 -0700647 impl ToTokens for Path {
648 fn to_tokens(&self, tokens: &mut Tokens) {
649 for (i, segment) in self.segments.iter().enumerate() {
650 if i > 0 || self.global {
651 tokens.append("::");
652 }
653 segment.to_tokens(tokens);
654 }
655 }
656 }
657
658 impl ToTokens for PathSegment {
659 fn to_tokens(&self, tokens: &mut Tokens) {
660 self.ident.to_tokens(tokens);
661 self.parameters.to_tokens(tokens);
662 }
663 }
664
665 impl ToTokens for PathParameters {
666 fn to_tokens(&self, tokens: &mut Tokens) {
667 match *self {
668 PathParameters::AngleBracketed(ref parameters) => {
669 parameters.to_tokens(tokens);
670 }
671 PathParameters::Parenthesized(ref parameters) => {
672 parameters.to_tokens(tokens);
673 }
674 }
675 }
676 }
677
678 impl ToTokens for AngleBracketedParameterData {
679 fn to_tokens(&self, tokens: &mut Tokens) {
680 let has_lifetimes = !self.lifetimes.is_empty();
681 let has_types = !self.types.is_empty();
682 let has_bindings = !self.bindings.is_empty();
683 if !has_lifetimes && !has_types && !has_bindings {
684 return;
685 }
686
687 tokens.append("<");
688
689 let mut first = true;
690 for lifetime in &self.lifetimes {
691 if !first {
692 tokens.append(",");
693 }
694 lifetime.to_tokens(tokens);
695 first = false;
696 }
697 for ty in &self.types {
698 if !first {
699 tokens.append(",");
700 }
701 ty.to_tokens(tokens);
702 first = false;
703 }
704 for binding in &self.bindings {
705 if !first {
706 tokens.append(",");
707 }
708 binding.to_tokens(tokens);
709 first = false;
710 }
711
712 tokens.append(">");
713 }
714 }
715
716 impl ToTokens for TypeBinding {
717 fn to_tokens(&self, tokens: &mut Tokens) {
718 self.ident.to_tokens(tokens);
719 tokens.append("=");
720 self.ty.to_tokens(tokens);
721 }
722 }
723
724 impl ToTokens for ParenthesizedParameterData {
725 fn to_tokens(&self, tokens: &mut Tokens) {
726 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700727 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700728 tokens.append(")");
729 if let Some(ref output) = self.output {
730 tokens.append("->");
731 output.to_tokens(tokens);
732 }
733 }
734 }
735
736 impl ToTokens for PolyTraitRef {
737 fn to_tokens(&self, tokens: &mut Tokens) {
738 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700739 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700740 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700741 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700742 tokens.append(">");
743 }
744 self.trait_ref.to_tokens(tokens);
745 }
746 }
747
748 impl ToTokens for BareFnTy {
749 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700750 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700751 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700752 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700753 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700754 tokens.append(">");
755 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700756 self.unsafety.to_tokens(tokens);
757 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700758 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700759 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700760 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700761 if self.variadic {
762 if !self.inputs.is_empty() {
763 tokens.append(",");
764 }
765 tokens.append("...");
766 }
David Tolnay87d0b442016-09-04 11:52:12 -0700767 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700768 if let FunctionRetTy::Ty(ref ty) = self.output {
769 tokens.append("->");
770 ty.to_tokens(tokens);
771 }
772 }
773 }
774
David Tolnay62f374c2016-10-02 13:37:00 -0700775 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700776 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700777 if let Some(ref name) = self.name {
778 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700779 tokens.append(":");
780 }
781 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700782 }
783 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700784
785 impl ToTokens for Unsafety {
786 fn to_tokens(&self, tokens: &mut Tokens) {
787 match *self {
788 Unsafety::Unsafe => tokens.append("unsafe"),
789 Unsafety::Normal => {
790 // nothing
791 }
792 }
793 }
794 }
795
796 impl ToTokens for Abi {
797 fn to_tokens(&self, tokens: &mut Tokens) {
798 tokens.append("extern");
799 match *self {
800 Abi::Named(ref named) => named.to_tokens(tokens),
801 Abi::Extern => {}
802 }
803 }
804 }
David Tolnay87d0b442016-09-04 11:52:12 -0700805}