blob: 011f0f90e3d80e98434729a89dcbba90c74e3a44 [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 {
169 pub lifetimes: Vec<LifetimeDef>,
David Tolnay62f374c2016-10-02 13:37:00 -0700170 pub inputs: Vec<BareFnArg>,
David Tolnayb79ee962016-09-04 09:39:20 -0700171 pub output: FunctionRetTy,
172}
173
David Tolnay62f374c2016-10-02 13:37:00 -0700174/// An argument in a function type.
David Tolnayb79ee962016-09-04 09:39:20 -0700175///
176/// E.g. `bar: usize` as in `fn foo(bar: usize)`
177#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay62f374c2016-10-02 13:37:00 -0700178pub struct BareFnArg {
179 pub name: Option<Ident>,
David Tolnayb79ee962016-09-04 09:39:20 -0700180 pub ty: Ty,
181}
182
183#[derive(Debug, Clone, Eq, PartialEq)]
184pub enum FunctionRetTy {
185 /// Return type is not specified.
186 ///
187 /// Functions default to `()` and
188 /// closures default to inference. Span points to where return
189 /// type would be inserted.
190 Default,
191 /// Everything else
192 Ty(Ty),
193}
194
David Tolnay86eca752016-09-04 11:26:41 -0700195#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700196pub mod parsing {
197 use super::*;
David Tolnay6414da72016-10-08 00:55:17 -0700198 use {TraitBoundModifier, TyParamBound};
David Tolnay3cb23a92016-10-07 23:02:21 -0700199 use constant::parsing::const_expr;
David Tolnay9d8f1972016-09-04 11:58:48 -0700200 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700201 use ident::parsing::ident;
David Tolnay9d8f1972016-09-04 11:58:48 -0700202 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700203
David Tolnayb5a7b142016-09-13 22:46:39 -0700204 named!(pub ty -> Ty, alt!(
David Tolnay6414da72016-10-08 00:55:17 -0700205 ty_poly_trait_ref // must be before ty_path
206 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700207 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700208 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700209 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700210 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700211 ty_ptr
212 |
213 ty_rptr
214 |
215 ty_bare_fn
216 |
217 ty_never
218 |
219 ty_tup
220 |
221 ty_path
222 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700223 ty_impl_trait
224 |
225 ty_paren
226 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700227
David Tolnayb5a7b142016-09-13 22:46:39 -0700228 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700229 punct!("[") >>
230 elem: ty >>
231 punct!("]") >>
David Tolnay16709ba2016-10-05 23:11:32 -0700232 (Ty::Slice(Box::new(elem)))
David Tolnay9d8f1972016-09-04 11:58:48 -0700233 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700234
David Tolnayfa94b6f2016-10-05 23:26:11 -0700235 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700236 punct!("[") >>
237 elem: ty >>
238 punct!(";") >>
David Tolnay3cb23a92016-10-07 23:02:21 -0700239 len: const_expr >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700240 punct!("]") >>
David Tolnayfa94b6f2016-10-05 23:26:11 -0700241 (Ty::Array(Box::new(elem), len))
242 ));
243
David Tolnayb5a7b142016-09-13 22:46:39 -0700244 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700245 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700246 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700247 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700248 |
David Tolnay10413f02016-09-30 09:12:02 -0700249 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700250 ) >>
251 target: ty >>
252 (Ty::Ptr(Box::new(MutTy {
253 ty: target,
254 mutability: mutability,
255 })))
256 ));
257
David Tolnayb5a7b142016-09-13 22:46:39 -0700258 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700259 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700260 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700261 mutability: mutability >>
262 target: ty >>
263 (Ty::Rptr(life, Box::new(MutTy {
264 ty: target,
265 mutability: mutability,
266 })))
267 ));
268
David Tolnayb5a7b142016-09-13 22:46:39 -0700269 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700270 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700271 lifetimes: opt_vec!(delimited!(
272 punct!("<"),
273 separated_list!(punct!(","), lifetime_def),
274 punct!(">")
David Tolnay6b7aaf02016-09-04 10:39:25 -0700275 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700276 punct!("(") >>
277 inputs: separated_list!(punct!(","), fn_arg) >>
278 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700279 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700280 punct!("->"),
281 ty
282 )) >>
283 (Ty::BareFn(Box::new(BareFnTy {
284 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700285 inputs: inputs,
286 output: match output {
287 Some(ty) => FunctionRetTy::Ty(ty),
288 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700289 },
290 })))
291 ));
292
David Tolnayb5a7b142016-09-13 22:46:39 -0700293 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700294
David Tolnayb5a7b142016-09-13 22:46:39 -0700295 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700296 punct!("(") >>
297 elems: separated_list!(punct!(","), ty) >>
298 punct!(")") >>
299 (Ty::Tup(elems))
300 ));
301
David Tolnay6414da72016-10-08 00:55:17 -0700302 named!(ty_path -> Ty, do_parse!(
303 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700304 parenthesized: cond!(
305 qpath.1.segments.last().unwrap().parameters == PathParameters::none(),
306 option!(parenthesized_parameter_data)
307 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700308 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
309 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700310 let (qself, mut path) = qpath;
311 if let Some(Some(parenthesized)) = parenthesized {
312 path.segments.last_mut().unwrap().parameters = parenthesized;
313 }
314 let path = Ty::Path(qself, path);
David Tolnay6414da72016-10-08 00:55:17 -0700315 if bounds.is_empty() {
316 path
317 } else {
318 Ty::ObjectSum(Box::new(path), bounds)
319 }
320 })
321 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700322
David Tolnayf6c74402016-10-08 02:31:26 -0700323 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
324 punct!("(") >>
325 inputs: separated_list!(punct!(","), ty) >>
326 cond!(!inputs.is_empty(), option!(punct!(","))) >>
327 punct!(")") >>
328 output: option!(preceded!(
329 punct!("->"),
330 ty
331 )) >>
332 (PathParameters::Parenthesized(
333 ParenthesizedParameterData {
334 inputs: inputs,
335 output: output,
336 },
337 ))
338 ));
339
David Tolnay9636c052016-10-02 17:11:17 -0700340 named!(pub qpath -> (Option<QSelf>, Path), alt!(
341 map!(path, |p| (None, p))
342 |
343 do_parse!(
344 punct!("<") >>
345 this: map!(ty, Box::new) >>
346 path: option!(preceded!(
347 keyword!("as"),
348 path
349 )) >>
350 punct!(">") >>
351 punct!("::") >>
352 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
353 ({
354 match path {
355 Some(mut path) => {
356 let pos = path.segments.len();
357 path.segments.extend(rest);
358 (Some(QSelf { ty: this, position: pos }), path)
359 }
360 None => {
361 (Some(QSelf { ty: this, position: 0 }), Path {
362 global: false,
363 segments: rest,
364 })
365 }
David Tolnayb79ee962016-09-04 09:39:20 -0700366 }
David Tolnay9636c052016-10-02 17:11:17 -0700367 })
368 )
David Tolnay9d8f1972016-09-04 11:58:48 -0700369 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700370
David Tolnay6414da72016-10-08 00:55:17 -0700371 named!(ty_poly_trait_ref -> Ty, do_parse!(
372 keyword!("for") >>
373 punct!("<") >>
374 lifetimes: separated_list!(punct!(","), lifetime_def) >>
375 punct!(">") >>
376 trait_ref: path >>
377 (Ty::PolyTraitRef(vec![
378 TyParamBound::Trait(
379 PolyTraitRef {
380 bound_lifetimes: lifetimes,
381 trait_ref: trait_ref,
382 },
383 TraitBoundModifier::None,
384 ),
385 ]))
386 ));
387
David Tolnayb5a7b142016-09-13 22:46:39 -0700388 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700389 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700390 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
391 (Ty::ImplTrait(elem))
392 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700393
David Tolnayb5a7b142016-09-13 22:46:39 -0700394 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700395 punct!("(") >>
396 elem: ty >>
397 punct!(")") >>
398 (Ty::Paren(Box::new(elem)))
399 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700400
David Tolnay47a877c2016-10-01 16:50:55 -0700401 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700402 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700403 |
404 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700405 ));
406
David Tolnayb5a7b142016-09-13 22:46:39 -0700407 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700408 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700409 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
410 (Path {
411 global: global.is_some(),
412 segments: segments,
413 })
414 ));
415
David Tolnay9636c052016-10-02 17:11:17 -0700416 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700417 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700418 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700419 punct!("<") >>
420 lifetimes: separated_list!(punct!(","), lifetime) >>
421 types: opt_vec!(preceded!(
422 cond!(!lifetimes.is_empty(), punct!(",")),
423 separated_nonempty_list!(
424 punct!(","),
425 terminated!(ty, not!(peek!(punct!("="))))
426 )
427 )) >>
428 bindings: opt_vec!(preceded!(
429 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
430 separated_nonempty_list!(punct!(","), type_binding)
431 )) >>
432 punct!(">") >>
433 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700434 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700435 parameters: PathParameters::AngleBracketed(
436 AngleBracketedParameterData {
437 lifetimes: lifetimes,
438 types: types,
439 bindings: bindings,
440 }
441 ),
442 })
443 )
444 |
David Tolnay84aa0752016-10-02 23:01:13 -0700445 map!(ident, Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700446 ));
447
David Tolnayb5a7b142016-09-13 22:46:39 -0700448 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700449 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700450 punct!("=") >>
451 ty: ty >>
452 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700453 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700454 ty: ty,
455 })
456 ));
457
David Tolnayb5a7b142016-09-13 22:46:39 -0700458 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700459 bound_lifetimes: bound_lifetimes >>
460 trait_ref: path >>
David Tolnayf6c74402016-10-08 02:31:26 -0700461 parenthesized: cond!(
462 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
463 option!(parenthesized_parameter_data)
464 ) >>
465 ({
466 let mut trait_ref = trait_ref;
467 if let Some(Some(parenthesized)) = parenthesized {
468 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
469 }
470 PolyTraitRef {
471 bound_lifetimes: bound_lifetimes,
472 trait_ref: trait_ref,
473 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700474 })
475 ));
476
David Tolnay62f374c2016-10-02 13:37:00 -0700477 named!(pub fn_arg -> BareFnArg, do_parse!(
478 name: option!(terminated!(ident, punct!(":"))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700479 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700480 (BareFnArg {
481 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700482 ty: ty,
483 })
484 ));
485}
David Tolnay87d0b442016-09-04 11:52:12 -0700486
487#[cfg(feature = "printing")]
488mod printing {
489 use super::*;
490 use quote::{Tokens, ToTokens};
491
492 impl ToTokens for Ty {
493 fn to_tokens(&self, tokens: &mut Tokens) {
494 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700495 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700496 tokens.append("[");
497 inner.to_tokens(tokens);
498 tokens.append("]");
499 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700500 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700501 tokens.append("[");
502 inner.to_tokens(tokens);
503 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700504 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700505 tokens.append("]");
506 }
507 Ty::Ptr(ref target) => {
508 tokens.append("*");
509 match target.mutability {
510 Mutability::Mutable => tokens.append("mut"),
511 Mutability::Immutable => tokens.append("const"),
512 }
513 target.ty.to_tokens(tokens);
514 }
515 Ty::Rptr(ref lifetime, ref target) => {
516 tokens.append("&");
517 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700518 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700519 target.ty.to_tokens(tokens);
520 }
521 Ty::BareFn(ref func) => {
522 func.to_tokens(tokens);
523 }
524 Ty::Never => {
525 tokens.append("!");
526 }
527 Ty::Tup(ref elems) => {
528 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700529 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700530 if elems.len() == 1 {
531 tokens.append(",");
532 }
533 tokens.append(")");
534 }
David Tolnayf69904a2016-09-04 14:46:07 -0700535 Ty::Path(None, ref path) => {
536 path.to_tokens(tokens);
537 }
538 Ty::Path(Some(ref qself), ref path) => {
539 tokens.append("<");
540 qself.ty.to_tokens(tokens);
541 if qself.position > 0 {
542 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700543 for (i, segment) in path.segments
544 .iter()
545 .take(qself.position)
546 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700547 if i > 0 || path.global {
548 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700549 }
David Tolnayf69904a2016-09-04 14:46:07 -0700550 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700551 }
David Tolnayf69904a2016-09-04 14:46:07 -0700552 }
553 tokens.append(">");
554 for segment in path.segments.iter().skip(qself.position) {
555 tokens.append("::");
556 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700557 }
558 }
David Tolnay6414da72016-10-08 00:55:17 -0700559 Ty::ObjectSum(ref ty, ref bounds) => {
560 ty.to_tokens(tokens);
561 for bound in bounds {
562 tokens.append("+");
563 bound.to_tokens(tokens);
564 }
565 }
566 Ty::PolyTraitRef(ref bounds) => {
567 tokens.append_separated(bounds, "+");
568 }
David Tolnay87d0b442016-09-04 11:52:12 -0700569 Ty::ImplTrait(ref bounds) => {
570 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700571 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700572 }
573 Ty::Paren(ref inner) => {
574 tokens.append("(");
575 inner.to_tokens(tokens);
576 tokens.append(")");
577 }
578 Ty::Infer => {
579 tokens.append("_");
580 }
581 }
582 }
583 }
584
David Tolnay47a877c2016-10-01 16:50:55 -0700585 impl ToTokens for Mutability {
586 fn to_tokens(&self, tokens: &mut Tokens) {
587 if let Mutability::Mutable = *self {
588 tokens.append("mut");
589 }
590 }
591 }
592
David Tolnay87d0b442016-09-04 11:52:12 -0700593 impl ToTokens for Path {
594 fn to_tokens(&self, tokens: &mut Tokens) {
595 for (i, segment) in self.segments.iter().enumerate() {
596 if i > 0 || self.global {
597 tokens.append("::");
598 }
599 segment.to_tokens(tokens);
600 }
601 }
602 }
603
604 impl ToTokens for PathSegment {
605 fn to_tokens(&self, tokens: &mut Tokens) {
606 self.ident.to_tokens(tokens);
607 self.parameters.to_tokens(tokens);
608 }
609 }
610
611 impl ToTokens for PathParameters {
612 fn to_tokens(&self, tokens: &mut Tokens) {
613 match *self {
614 PathParameters::AngleBracketed(ref parameters) => {
615 parameters.to_tokens(tokens);
616 }
617 PathParameters::Parenthesized(ref parameters) => {
618 parameters.to_tokens(tokens);
619 }
620 }
621 }
622 }
623
624 impl ToTokens for AngleBracketedParameterData {
625 fn to_tokens(&self, tokens: &mut Tokens) {
626 let has_lifetimes = !self.lifetimes.is_empty();
627 let has_types = !self.types.is_empty();
628 let has_bindings = !self.bindings.is_empty();
629 if !has_lifetimes && !has_types && !has_bindings {
630 return;
631 }
632
633 tokens.append("<");
634
635 let mut first = true;
636 for lifetime in &self.lifetimes {
637 if !first {
638 tokens.append(",");
639 }
640 lifetime.to_tokens(tokens);
641 first = false;
642 }
643 for ty in &self.types {
644 if !first {
645 tokens.append(",");
646 }
647 ty.to_tokens(tokens);
648 first = false;
649 }
650 for binding in &self.bindings {
651 if !first {
652 tokens.append(",");
653 }
654 binding.to_tokens(tokens);
655 first = false;
656 }
657
658 tokens.append(">");
659 }
660 }
661
662 impl ToTokens for TypeBinding {
663 fn to_tokens(&self, tokens: &mut Tokens) {
664 self.ident.to_tokens(tokens);
665 tokens.append("=");
666 self.ty.to_tokens(tokens);
667 }
668 }
669
670 impl ToTokens for ParenthesizedParameterData {
671 fn to_tokens(&self, tokens: &mut Tokens) {
672 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700673 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700674 tokens.append(")");
675 if let Some(ref output) = self.output {
676 tokens.append("->");
677 output.to_tokens(tokens);
678 }
679 }
680 }
681
682 impl ToTokens for PolyTraitRef {
683 fn to_tokens(&self, tokens: &mut Tokens) {
684 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700685 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700686 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700687 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700688 tokens.append(">");
689 }
690 self.trait_ref.to_tokens(tokens);
691 }
692 }
693
694 impl ToTokens for BareFnTy {
695 fn to_tokens(&self, tokens: &mut Tokens) {
696 tokens.append("fn");
697 if !self.lifetimes.is_empty() {
698 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700699 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700700 tokens.append(">");
701 }
702 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700703 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700704 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700705 if let FunctionRetTy::Ty(ref ty) = self.output {
706 tokens.append("->");
707 ty.to_tokens(tokens);
708 }
709 }
710 }
711
David Tolnay62f374c2016-10-02 13:37:00 -0700712 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700713 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700714 if let Some(ref name) = self.name {
715 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700716 tokens.append(":");
717 }
718 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700719 }
720 }
721}