blob: b33206a075c3a81da2b399cb3d698d258bda83a1 [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 Tolnayd040d772016-10-25 21:33:51 -0700221 ty_paren // must be before ty_tup
222 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700223 ty_path // must be before ty_poly_trait_ref
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 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700239 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700240 |
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 Tolnay4f0f2512016-10-30 09:28:14 -0700397 named!(ty_poly_trait_ref -> Ty, map!(
398 poly_trait_ref,
399 |trait_ref| Ty::PolyTraitRef(vec![
400 TyParamBound::Trait(trait_ref, TraitBoundModifier::None),
401 ])
David Tolnay6414da72016-10-08 00:55:17 -0700402 ));
403
David Tolnayb5a7b142016-09-13 22:46:39 -0700404 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700405 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700406 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
407 (Ty::ImplTrait(elem))
408 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700409
David Tolnayb5a7b142016-09-13 22:46:39 -0700410 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700411 punct!("(") >>
412 elem: ty >>
413 punct!(")") >>
414 (Ty::Paren(Box::new(elem)))
415 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700416
David Tolnay47a877c2016-10-01 16:50:55 -0700417 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700418 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700419 |
420 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700421 ));
422
David Tolnayb5a7b142016-09-13 22:46:39 -0700423 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700424 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700425 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
426 (Path {
427 global: global.is_some(),
428 segments: segments,
429 })
430 ));
431
David Tolnay9636c052016-10-02 17:11:17 -0700432 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700433 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700434 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700435 punct!("<") >>
436 lifetimes: separated_list!(punct!(","), lifetime) >>
437 types: opt_vec!(preceded!(
438 cond!(!lifetimes.is_empty(), punct!(",")),
439 separated_nonempty_list!(
440 punct!(","),
441 terminated!(ty, not!(peek!(punct!("="))))
442 )
443 )) >>
444 bindings: opt_vec!(preceded!(
445 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
446 separated_nonempty_list!(punct!(","), type_binding)
447 )) >>
448 punct!(">") >>
449 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700450 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700451 parameters: PathParameters::AngleBracketed(
452 AngleBracketedParameterData {
453 lifetimes: lifetimes,
454 types: types,
455 bindings: bindings,
456 }
457 ),
458 })
459 )
460 |
David Tolnay84aa0752016-10-02 23:01:13 -0700461 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700462 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700463 map!(alt!(
464 keyword!("super")
465 |
466 keyword!("self")
467 |
468 keyword!("Self")
469 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700470 ));
471
David Tolnayb5a7b142016-09-13 22:46:39 -0700472 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700473 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700474 punct!("=") >>
475 ty: ty >>
476 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700477 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700478 ty: ty,
479 })
480 ));
481
David Tolnayb5a7b142016-09-13 22:46:39 -0700482 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700483 bound_lifetimes: bound_lifetimes >>
484 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700485 parenthesized: option!(cond_reduce!(
David Tolnayf6c74402016-10-08 02:31:26 -0700486 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700487 parenthesized_parameter_data
488 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700489 ({
490 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700491 if let Some(parenthesized) = parenthesized {
David Tolnayf6c74402016-10-08 02:31:26 -0700492 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
493 }
494 PolyTraitRef {
495 bound_lifetimes: bound_lifetimes,
496 trait_ref: trait_ref,
497 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700498 })
499 ));
500
David Tolnay62f374c2016-10-02 13:37:00 -0700501 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700502 name: option!(do_parse!(
503 name: ident >>
504 punct!(":") >>
505 not!(peek!(tag!(":"))) >> // not ::
506 (name)
507 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700508 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700509 (BareFnArg {
510 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700511 ty: ty,
512 })
513 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700514
515 named!(pub unsafety -> Unsafety, alt!(
516 keyword!("unsafe") => { |_| Unsafety::Unsafe }
517 |
518 epsilon!() => { |_| Unsafety::Normal }
519 ));
520
521 named!(pub abi -> Abi, do_parse!(
522 keyword!("extern") >>
523 name: option!(quoted_string) >>
524 (match name {
525 Some(name) => Abi::Named(name),
526 None => Abi::Extern,
527 })
528 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700529}
David Tolnay87d0b442016-09-04 11:52:12 -0700530
531#[cfg(feature = "printing")]
532mod printing {
533 use super::*;
534 use quote::{Tokens, ToTokens};
535
536 impl ToTokens for Ty {
537 fn to_tokens(&self, tokens: &mut Tokens) {
538 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700539 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700540 tokens.append("[");
541 inner.to_tokens(tokens);
542 tokens.append("]");
543 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700544 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700545 tokens.append("[");
546 inner.to_tokens(tokens);
547 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700548 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700549 tokens.append("]");
550 }
551 Ty::Ptr(ref target) => {
552 tokens.append("*");
553 match target.mutability {
554 Mutability::Mutable => tokens.append("mut"),
555 Mutability::Immutable => tokens.append("const"),
556 }
557 target.ty.to_tokens(tokens);
558 }
559 Ty::Rptr(ref lifetime, ref target) => {
560 tokens.append("&");
561 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700562 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700563 target.ty.to_tokens(tokens);
564 }
565 Ty::BareFn(ref func) => {
566 func.to_tokens(tokens);
567 }
568 Ty::Never => {
569 tokens.append("!");
570 }
571 Ty::Tup(ref elems) => {
572 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700573 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700574 if elems.len() == 1 {
575 tokens.append(",");
576 }
577 tokens.append(")");
578 }
David Tolnayf69904a2016-09-04 14:46:07 -0700579 Ty::Path(None, ref path) => {
580 path.to_tokens(tokens);
581 }
582 Ty::Path(Some(ref qself), ref path) => {
583 tokens.append("<");
584 qself.ty.to_tokens(tokens);
585 if qself.position > 0 {
586 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700587 for (i, segment) in path.segments
588 .iter()
589 .take(qself.position)
590 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700591 if i > 0 || path.global {
592 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700593 }
David Tolnayf69904a2016-09-04 14:46:07 -0700594 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700595 }
David Tolnayf69904a2016-09-04 14:46:07 -0700596 }
597 tokens.append(">");
598 for segment in path.segments.iter().skip(qself.position) {
599 tokens.append("::");
600 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700601 }
602 }
David Tolnay6414da72016-10-08 00:55:17 -0700603 Ty::ObjectSum(ref ty, ref bounds) => {
604 ty.to_tokens(tokens);
605 for bound in bounds {
606 tokens.append("+");
607 bound.to_tokens(tokens);
608 }
609 }
610 Ty::PolyTraitRef(ref bounds) => {
611 tokens.append_separated(bounds, "+");
612 }
David Tolnay87d0b442016-09-04 11:52:12 -0700613 Ty::ImplTrait(ref bounds) => {
614 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700615 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700616 }
617 Ty::Paren(ref inner) => {
618 tokens.append("(");
619 inner.to_tokens(tokens);
620 tokens.append(")");
621 }
622 Ty::Infer => {
623 tokens.append("_");
624 }
625 }
626 }
627 }
628
David Tolnay47a877c2016-10-01 16:50:55 -0700629 impl ToTokens for Mutability {
630 fn to_tokens(&self, tokens: &mut Tokens) {
631 if let Mutability::Mutable = *self {
632 tokens.append("mut");
633 }
634 }
635 }
636
David Tolnay87d0b442016-09-04 11:52:12 -0700637 impl ToTokens for Path {
638 fn to_tokens(&self, tokens: &mut Tokens) {
639 for (i, segment) in self.segments.iter().enumerate() {
640 if i > 0 || self.global {
641 tokens.append("::");
642 }
643 segment.to_tokens(tokens);
644 }
645 }
646 }
647
648 impl ToTokens for PathSegment {
649 fn to_tokens(&self, tokens: &mut Tokens) {
650 self.ident.to_tokens(tokens);
651 self.parameters.to_tokens(tokens);
652 }
653 }
654
655 impl ToTokens for PathParameters {
656 fn to_tokens(&self, tokens: &mut Tokens) {
657 match *self {
658 PathParameters::AngleBracketed(ref parameters) => {
659 parameters.to_tokens(tokens);
660 }
661 PathParameters::Parenthesized(ref parameters) => {
662 parameters.to_tokens(tokens);
663 }
664 }
665 }
666 }
667
668 impl ToTokens for AngleBracketedParameterData {
669 fn to_tokens(&self, tokens: &mut Tokens) {
670 let has_lifetimes = !self.lifetimes.is_empty();
671 let has_types = !self.types.is_empty();
672 let has_bindings = !self.bindings.is_empty();
673 if !has_lifetimes && !has_types && !has_bindings {
674 return;
675 }
676
677 tokens.append("<");
678
679 let mut first = true;
680 for lifetime in &self.lifetimes {
681 if !first {
682 tokens.append(",");
683 }
684 lifetime.to_tokens(tokens);
685 first = false;
686 }
687 for ty in &self.types {
688 if !first {
689 tokens.append(",");
690 }
691 ty.to_tokens(tokens);
692 first = false;
693 }
694 for binding in &self.bindings {
695 if !first {
696 tokens.append(",");
697 }
698 binding.to_tokens(tokens);
699 first = false;
700 }
701
702 tokens.append(">");
703 }
704 }
705
706 impl ToTokens for TypeBinding {
707 fn to_tokens(&self, tokens: &mut Tokens) {
708 self.ident.to_tokens(tokens);
709 tokens.append("=");
710 self.ty.to_tokens(tokens);
711 }
712 }
713
714 impl ToTokens for ParenthesizedParameterData {
715 fn to_tokens(&self, tokens: &mut Tokens) {
716 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700717 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700718 tokens.append(")");
719 if let Some(ref output) = self.output {
720 tokens.append("->");
721 output.to_tokens(tokens);
722 }
723 }
724 }
725
726 impl ToTokens for PolyTraitRef {
727 fn to_tokens(&self, tokens: &mut Tokens) {
728 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700729 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700730 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700731 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700732 tokens.append(">");
733 }
734 self.trait_ref.to_tokens(tokens);
735 }
736 }
737
738 impl ToTokens for BareFnTy {
739 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700740 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700741 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700742 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700743 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700744 tokens.append(">");
745 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700746 self.unsafety.to_tokens(tokens);
747 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700748 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700749 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700750 tokens.append_separated(&self.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -0700751 if self.variadic {
752 if !self.inputs.is_empty() {
753 tokens.append(",");
754 }
755 tokens.append("...");
756 }
David Tolnay87d0b442016-09-04 11:52:12 -0700757 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700758 if let FunctionRetTy::Ty(ref ty) = self.output {
759 tokens.append("->");
760 ty.to_tokens(tokens);
761 }
762 }
763 }
764
David Tolnay62f374c2016-10-02 13:37:00 -0700765 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700766 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700767 if let Some(ref name) = self.name {
768 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700769 tokens.append(":");
770 }
771 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700772 }
773 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700774
775 impl ToTokens for Unsafety {
776 fn to_tokens(&self, tokens: &mut Tokens) {
777 match *self {
778 Unsafety::Unsafe => tokens.append("unsafe"),
779 Unsafety::Normal => {
780 // nothing
781 }
782 }
783 }
784 }
785
786 impl ToTokens for Abi {
787 fn to_tokens(&self, tokens: &mut Tokens) {
788 tokens.append("extern");
789 match *self {
790 Abi::Named(ref named) => named.to_tokens(tokens),
791 Abi::Extern => {}
792 }
793 }
794 }
David Tolnay87d0b442016-09-04 11:52:12 -0700795}