blob: 2d29deed7cd768b6c380cd774fb4eb25e418b025 [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,
174}
175
David Tolnayb8d8ef52016-10-29 14:30:08 -0700176#[derive(Debug, Copy, Clone, Eq, PartialEq)]
177pub enum Unsafety {
178 Unsafe,
179 Normal,
180}
181
182#[derive(Debug, Clone, Eq, PartialEq)]
183pub enum Abi {
184 Named(String),
185 Extern,
186}
187
David Tolnay62f374c2016-10-02 13:37:00 -0700188/// An argument in a function type.
David Tolnayb79ee962016-09-04 09:39:20 -0700189///
190/// E.g. `bar: usize` as in `fn foo(bar: usize)`
191#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay62f374c2016-10-02 13:37:00 -0700192pub struct BareFnArg {
193 pub name: Option<Ident>,
David Tolnayb79ee962016-09-04 09:39:20 -0700194 pub ty: Ty,
195}
196
197#[derive(Debug, Clone, Eq, PartialEq)]
198pub enum FunctionRetTy {
199 /// Return type is not specified.
200 ///
201 /// Functions default to `()` and
202 /// closures default to inference. Span points to where return
203 /// type would be inserted.
204 Default,
205 /// Everything else
206 Ty(Ty),
207}
208
David Tolnay86eca752016-09-04 11:26:41 -0700209#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700210pub mod parsing {
211 use super::*;
David Tolnay6414da72016-10-08 00:55:17 -0700212 use {TraitBoundModifier, TyParamBound};
David Tolnay3cb23a92016-10-07 23:02:21 -0700213 use constant::parsing::const_expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700214 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700215 use ident::parsing::ident;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700216 use lit::parsing::quoted_string;
David Tolnay9d8f1972016-09-04 11:58:48 -0700217 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700218
David Tolnayb5a7b142016-09-13 22:46:39 -0700219 named!(pub ty -> Ty, alt!(
David Tolnay6414da72016-10-08 00:55:17 -0700220 ty_poly_trait_ref // must be before ty_path
221 |
David Tolnayd040d772016-10-25 21:33:51 -0700222 ty_paren // must be before ty_tup
223 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700224 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700225 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700226 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700227 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700228 ty_ptr
229 |
230 ty_rptr
231 |
232 ty_bare_fn
233 |
234 ty_never
235 |
236 ty_tup
237 |
238 ty_path
239 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700240 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700241 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700242
David Tolnayb5a7b142016-09-13 22:46:39 -0700243 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700244 punct!("[") >>
245 elem: ty >>
246 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700247 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700248 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700249
David Tolnayfa94b6f2016-10-05 23:26:11 -0700250 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700251 punct!("[") >>
252 elem: ty >>
253 punct!(";") >>
David Tolnay3cb23a92016-10-07 23:02:21 -0700254 len: const_expr >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700255 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700256 (Ty::Array(Box::new(elem), len))
257 ));
258
David Tolnayb5a7b142016-09-13 22:46:39 -0700259 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700260 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700261 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700262 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700263 |
David Tolnay10413f02016-09-30 09:12:02 -0700264 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700265 ) >>
266 target: ty >>
267 (Ty::Ptr(Box::new(MutTy {
268 ty: target,
269 mutability: mutability,
270 })))
271 ));
272
David Tolnayb5a7b142016-09-13 22:46:39 -0700273 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700274 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700275 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700276 mutability: mutability >>
277 target: ty >>
278 (Ty::Rptr(life, Box::new(MutTy {
279 ty: target,
280 mutability: mutability,
281 })))
282 ));
283
David Tolnayb5a7b142016-09-13 22:46:39 -0700284 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay4f121832016-10-25 21:33:36 -0700285 lifetimes: opt_vec!(do_parse!(
286 keyword!("for") >>
287 punct!("<") >>
288 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
289 punct!(">") >>
290 (lifetimes)
David Tolnay6b7aaf02016-09-04 10:39:25 -0700291 )) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700292 unsafety: unsafety >>
293 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700294 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700295 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700296 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700297 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700298 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700299 punct!("->"),
300 ty
301 )) >>
302 (Ty::BareFn(Box::new(BareFnTy {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700303 unsafety: unsafety,
304 abi: abi,
David Tolnay9d8f1972016-09-04 11:58:48 -0700305 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700306 inputs: inputs,
307 output: match output {
308 Some(ty) => FunctionRetTy::Ty(ty),
309 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700310 },
311 })))
312 ));
313
David Tolnayb5a7b142016-09-13 22:46:39 -0700314 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700315
David Tolnayb5a7b142016-09-13 22:46:39 -0700316 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700317 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700318 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700319 punct!(")") >>
320 (Ty::Tup(elems))
321 ));
322
David Tolnay6414da72016-10-08 00:55:17 -0700323 named!(ty_path -> Ty, do_parse!(
324 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700325 parenthesized: cond!(
326 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
327 option!(parenthesized_parameter_data)
328 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700329 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
330 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700331 let (qself, mut path) = qpath;
332 if let Some(Some(parenthesized)) = parenthesized {
333 path.segments.last_mut().unwrap().parameters = parenthesized;
334 }
335 let path = Ty::Path(qself, path);
David Tolnay6414da72016-10-08 00:55:17 -0700336 if bounds.is_empty() {
337 path
338 } else {
339 Ty::ObjectSum(Box::new(path), bounds)
340 }
341 })
342 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700343
David Tolnayf6c74402016-10-08 02:31:26 -0700344 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
345 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700346 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700347 punct!(")") >>
348 output: option!(preceded!(
349 punct!("->"),
350 ty
351 )) >>
352 (PathParameters::Parenthesized(
353 ParenthesizedParameterData {
354 inputs: inputs,
355 output: output,
356 },
357 ))
358 ));
359
David Tolnay9636c052016-10-02 17:11:17 -0700360 named!(pub qpath -> (Option<QSelf>, Path), alt!(
361 map!(path, |p| (None, p))
362 |
363 do_parse!(
364 punct!("<") >>
365 this: map!(ty, Box::new) >>
366 path: option!(preceded!(
367 keyword!("as"),
368 path
369 )) >>
370 punct!(">") >>
371 punct!("::") >>
372 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
373 ({
374 match path {
375 Some(mut path) => {
376 let pos = path.segments.len();
377 path.segments.extend(rest);
378 (Some(QSelf { ty: this, position: pos }), path)
379 }
380 None => {
381 (Some(QSelf { ty: this, position: 0 }), Path {
382 global: false,
383 segments: rest,
384 })
385 }
David Tolnayb79ee962016-09-04 09:39:20 -0700386 }
David Tolnay9636c052016-10-02 17:11:17 -0700387 })
388 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700389 |
390 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700391 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700392
David Tolnay6414da72016-10-08 00:55:17 -0700393 named!(ty_poly_trait_ref -> Ty, do_parse!(
394 keyword!("for") >>
395 punct!("<") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700396 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
David Tolnay6414da72016-10-08 00:55:17 -0700397 punct!(">") >>
398 trait_ref: path >>
399 (Ty::PolyTraitRef(vec![
400 TyParamBound::Trait(
401 PolyTraitRef {
402 bound_lifetimes: lifetimes,
403 trait_ref: trait_ref,
404 },
405 TraitBoundModifier::None,
406 ),
407 ]))
408 ));
409
David Tolnayb5a7b142016-09-13 22:46:39 -0700410 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700411 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700412 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
413 (Ty::ImplTrait(elem))
414 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700415
David Tolnayb5a7b142016-09-13 22:46:39 -0700416 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700417 punct!("(") >>
418 elem: ty >>
419 punct!(")") >>
420 (Ty::Paren(Box::new(elem)))
421 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700422
David Tolnay47a877c2016-10-01 16:50:55 -0700423 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700424 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700425 |
426 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700427 ));
428
David Tolnayb5a7b142016-09-13 22:46:39 -0700429 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700430 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700431 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
432 (Path {
433 global: global.is_some(),
434 segments: segments,
435 })
436 ));
437
David Tolnay9636c052016-10-02 17:11:17 -0700438 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700439 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700440 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700441 punct!("<") >>
442 lifetimes: separated_list!(punct!(","), lifetime) >>
443 types: opt_vec!(preceded!(
444 cond!(!lifetimes.is_empty(), punct!(",")),
445 separated_nonempty_list!(
446 punct!(","),
447 terminated!(ty, not!(peek!(punct!("="))))
448 )
449 )) >>
450 bindings: opt_vec!(preceded!(
451 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
452 separated_nonempty_list!(punct!(","), type_binding)
453 )) >>
454 punct!(">") >>
455 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700456 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700457 parameters: PathParameters::AngleBracketed(
458 AngleBracketedParameterData {
459 lifetimes: lifetimes,
460 types: types,
461 bindings: bindings,
462 }
463 ),
464 })
465 )
466 |
David Tolnay84aa0752016-10-02 23:01:13 -0700467 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700468 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700469 map!(alt!(
470 keyword!("super")
471 |
472 keyword!("self")
473 |
474 keyword!("Self")
475 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700476 ));
477
David Tolnayb5a7b142016-09-13 22:46:39 -0700478 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700479 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700480 punct!("=") >>
481 ty: ty >>
482 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700483 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700484 ty: ty,
485 })
486 ));
487
David Tolnayb5a7b142016-09-13 22:46:39 -0700488 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700489 bound_lifetimes: bound_lifetimes >>
490 trait_ref: path >>
David Tolnayf6c74402016-10-08 02:31:26 -0700491 parenthesized: cond!(
492 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
493 option!(parenthesized_parameter_data)
494 ) >>
495 ({
496 let mut trait_ref = trait_ref;
497 if let Some(Some(parenthesized)) = parenthesized {
498 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
499 }
500 PolyTraitRef {
501 bound_lifetimes: bound_lifetimes,
502 trait_ref: trait_ref,
503 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700504 })
505 ));
506
David Tolnay62f374c2016-10-02 13:37:00 -0700507 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700508 name: option!(do_parse!(
509 name: ident >>
510 punct!(":") >>
511 not!(peek!(tag!(":"))) >> // not ::
512 (name)
513 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700514 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700515 (BareFnArg {
516 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700517 ty: ty,
518 })
519 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700520
521 named!(pub unsafety -> Unsafety, alt!(
522 keyword!("unsafe") => { |_| Unsafety::Unsafe }
523 |
524 epsilon!() => { |_| Unsafety::Normal }
525 ));
526
527 named!(pub abi -> Abi, do_parse!(
528 keyword!("extern") >>
529 name: option!(quoted_string) >>
530 (match name {
531 Some(name) => Abi::Named(name),
532 None => Abi::Extern,
533 })
534 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700535}
David Tolnay87d0b442016-09-04 11:52:12 -0700536
537#[cfg(feature = "printing")]
538mod printing {
539 use super::*;
540 use quote::{Tokens, ToTokens};
541
542 impl ToTokens for Ty {
543 fn to_tokens(&self, tokens: &mut Tokens) {
544 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700545 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700546 tokens.append("[");
547 inner.to_tokens(tokens);
548 tokens.append("]");
549 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700550 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700551 tokens.append("[");
552 inner.to_tokens(tokens);
553 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700554 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700555 tokens.append("]");
556 }
557 Ty::Ptr(ref target) => {
558 tokens.append("*");
559 match target.mutability {
560 Mutability::Mutable => tokens.append("mut"),
561 Mutability::Immutable => tokens.append("const"),
562 }
563 target.ty.to_tokens(tokens);
564 }
565 Ty::Rptr(ref lifetime, ref target) => {
566 tokens.append("&");
567 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700568 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700569 target.ty.to_tokens(tokens);
570 }
571 Ty::BareFn(ref func) => {
572 func.to_tokens(tokens);
573 }
574 Ty::Never => {
575 tokens.append("!");
576 }
577 Ty::Tup(ref elems) => {
578 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700579 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700580 if elems.len() == 1 {
581 tokens.append(",");
582 }
583 tokens.append(")");
584 }
David Tolnayf69904a2016-09-04 14:46:07 -0700585 Ty::Path(None, ref path) => {
586 path.to_tokens(tokens);
587 }
588 Ty::Path(Some(ref qself), ref path) => {
589 tokens.append("<");
590 qself.ty.to_tokens(tokens);
591 if qself.position > 0 {
592 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700593 for (i, segment) in path.segments
594 .iter()
595 .take(qself.position)
596 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700597 if i > 0 || path.global {
598 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700599 }
David Tolnayf69904a2016-09-04 14:46:07 -0700600 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700601 }
David Tolnayf69904a2016-09-04 14:46:07 -0700602 }
603 tokens.append(">");
604 for segment in path.segments.iter().skip(qself.position) {
605 tokens.append("::");
606 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700607 }
608 }
David Tolnay6414da72016-10-08 00:55:17 -0700609 Ty::ObjectSum(ref ty, ref bounds) => {
610 ty.to_tokens(tokens);
611 for bound in bounds {
612 tokens.append("+");
613 bound.to_tokens(tokens);
614 }
615 }
616 Ty::PolyTraitRef(ref bounds) => {
617 tokens.append_separated(bounds, "+");
618 }
David Tolnay87d0b442016-09-04 11:52:12 -0700619 Ty::ImplTrait(ref bounds) => {
620 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700621 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700622 }
623 Ty::Paren(ref inner) => {
624 tokens.append("(");
625 inner.to_tokens(tokens);
626 tokens.append(")");
627 }
628 Ty::Infer => {
629 tokens.append("_");
630 }
631 }
632 }
633 }
634
David Tolnay47a877c2016-10-01 16:50:55 -0700635 impl ToTokens for Mutability {
636 fn to_tokens(&self, tokens: &mut Tokens) {
637 if let Mutability::Mutable = *self {
638 tokens.append("mut");
639 }
640 }
641 }
642
David Tolnay87d0b442016-09-04 11:52:12 -0700643 impl ToTokens for Path {
644 fn to_tokens(&self, tokens: &mut Tokens) {
645 for (i, segment) in self.segments.iter().enumerate() {
646 if i > 0 || self.global {
647 tokens.append("::");
648 }
649 segment.to_tokens(tokens);
650 }
651 }
652 }
653
654 impl ToTokens for PathSegment {
655 fn to_tokens(&self, tokens: &mut Tokens) {
656 self.ident.to_tokens(tokens);
657 self.parameters.to_tokens(tokens);
658 }
659 }
660
661 impl ToTokens for PathParameters {
662 fn to_tokens(&self, tokens: &mut Tokens) {
663 match *self {
664 PathParameters::AngleBracketed(ref parameters) => {
665 parameters.to_tokens(tokens);
666 }
667 PathParameters::Parenthesized(ref parameters) => {
668 parameters.to_tokens(tokens);
669 }
670 }
671 }
672 }
673
674 impl ToTokens for AngleBracketedParameterData {
675 fn to_tokens(&self, tokens: &mut Tokens) {
676 let has_lifetimes = !self.lifetimes.is_empty();
677 let has_types = !self.types.is_empty();
678 let has_bindings = !self.bindings.is_empty();
679 if !has_lifetimes && !has_types && !has_bindings {
680 return;
681 }
682
683 tokens.append("<");
684
685 let mut first = true;
686 for lifetime in &self.lifetimes {
687 if !first {
688 tokens.append(",");
689 }
690 lifetime.to_tokens(tokens);
691 first = false;
692 }
693 for ty in &self.types {
694 if !first {
695 tokens.append(",");
696 }
697 ty.to_tokens(tokens);
698 first = false;
699 }
700 for binding in &self.bindings {
701 if !first {
702 tokens.append(",");
703 }
704 binding.to_tokens(tokens);
705 first = false;
706 }
707
708 tokens.append(">");
709 }
710 }
711
712 impl ToTokens for TypeBinding {
713 fn to_tokens(&self, tokens: &mut Tokens) {
714 self.ident.to_tokens(tokens);
715 tokens.append("=");
716 self.ty.to_tokens(tokens);
717 }
718 }
719
720 impl ToTokens for ParenthesizedParameterData {
721 fn to_tokens(&self, tokens: &mut Tokens) {
722 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700723 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700724 tokens.append(")");
725 if let Some(ref output) = self.output {
726 tokens.append("->");
727 output.to_tokens(tokens);
728 }
729 }
730 }
731
732 impl ToTokens for PolyTraitRef {
733 fn to_tokens(&self, tokens: &mut Tokens) {
734 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700735 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700736 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700737 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700738 tokens.append(">");
739 }
740 self.trait_ref.to_tokens(tokens);
741 }
742 }
743
744 impl ToTokens for BareFnTy {
745 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700746 if !self.lifetimes.is_empty() {
David Tolnay4f121832016-10-25 21:33:36 -0700747 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700748 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700749 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700750 tokens.append(">");
751 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700752 self.unsafety.to_tokens(tokens);
753 self.abi.to_tokens(tokens);
David Tolnay4f121832016-10-25 21:33:36 -0700754 tokens.append("fn");
David Tolnay87d0b442016-09-04 11:52:12 -0700755 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700756 tokens.append_separated(&self.inputs, ",");
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}