blob: cfab4c3a83a2ae7b0913825525d63da16f3644f7 [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!("<"),
David Tolnayff46fd22016-10-08 13:53:28 -0700273 terminated_list!(punct!(","), lifetime_def),
David Tolnay9d8f1972016-09-04 11:58:48 -0700274 punct!(">")
David Tolnay6b7aaf02016-09-04 10:39:25 -0700275 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700276 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700277 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700278 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!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700297 elems: terminated_list!(punct!(","), ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700298 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!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700325 inputs: terminated_list!(punct!(","), ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700326 punct!(")") >>
327 output: option!(preceded!(
328 punct!("->"),
329 ty
330 )) >>
331 (PathParameters::Parenthesized(
332 ParenthesizedParameterData {
333 inputs: inputs,
334 output: output,
335 },
336 ))
337 ));
338
David Tolnay9636c052016-10-02 17:11:17 -0700339 named!(pub qpath -> (Option<QSelf>, Path), alt!(
340 map!(path, |p| (None, p))
341 |
342 do_parse!(
343 punct!("<") >>
344 this: map!(ty, Box::new) >>
345 path: option!(preceded!(
346 keyword!("as"),
347 path
348 )) >>
349 punct!(">") >>
350 punct!("::") >>
351 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
352 ({
353 match path {
354 Some(mut path) => {
355 let pos = path.segments.len();
356 path.segments.extend(rest);
357 (Some(QSelf { ty: this, position: pos }), path)
358 }
359 None => {
360 (Some(QSelf { ty: this, position: 0 }), Path {
361 global: false,
362 segments: rest,
363 })
364 }
David Tolnayb79ee962016-09-04 09:39:20 -0700365 }
David Tolnay9636c052016-10-02 17:11:17 -0700366 })
367 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700368 |
369 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700370 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700371
David Tolnay6414da72016-10-08 00:55:17 -0700372 named!(ty_poly_trait_ref -> Ty, do_parse!(
373 keyword!("for") >>
374 punct!("<") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700375 lifetimes: terminated_list!(punct!(","), lifetime_def) >>
David Tolnay6414da72016-10-08 00:55:17 -0700376 punct!(">") >>
377 trait_ref: path >>
378 (Ty::PolyTraitRef(vec![
379 TyParamBound::Trait(
380 PolyTraitRef {
381 bound_lifetimes: lifetimes,
382 trait_ref: trait_ref,
383 },
384 TraitBoundModifier::None,
385 ),
386 ]))
387 ));
388
David Tolnayb5a7b142016-09-13 22:46:39 -0700389 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700390 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700391 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
392 (Ty::ImplTrait(elem))
393 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700394
David Tolnayb5a7b142016-09-13 22:46:39 -0700395 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700396 punct!("(") >>
397 elem: ty >>
398 punct!(")") >>
399 (Ty::Paren(Box::new(elem)))
400 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700401
David Tolnay47a877c2016-10-01 16:50:55 -0700402 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700403 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700404 |
405 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700406 ));
407
David Tolnayb5a7b142016-09-13 22:46:39 -0700408 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700409 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700410 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
411 (Path {
412 global: global.is_some(),
413 segments: segments,
414 })
415 ));
416
David Tolnay9636c052016-10-02 17:11:17 -0700417 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700418 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700419 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700420 punct!("<") >>
421 lifetimes: separated_list!(punct!(","), lifetime) >>
422 types: opt_vec!(preceded!(
423 cond!(!lifetimes.is_empty(), punct!(",")),
424 separated_nonempty_list!(
425 punct!(","),
426 terminated!(ty, not!(peek!(punct!("="))))
427 )
428 )) >>
429 bindings: opt_vec!(preceded!(
430 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
431 separated_nonempty_list!(punct!(","), type_binding)
432 )) >>
433 punct!(">") >>
434 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700435 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700436 parameters: PathParameters::AngleBracketed(
437 AngleBracketedParameterData {
438 lifetimes: lifetimes,
439 types: types,
440 bindings: bindings,
441 }
442 ),
443 })
444 )
445 |
David Tolnay84aa0752016-10-02 23:01:13 -0700446 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700447 |
448 map!(keyword!("super"), Into::into)
449 |
450 map!(keyword!("Self"), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700451 ));
452
David Tolnayb5a7b142016-09-13 22:46:39 -0700453 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700454 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700455 punct!("=") >>
456 ty: ty >>
457 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700458 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700459 ty: ty,
460 })
461 ));
462
David Tolnayb5a7b142016-09-13 22:46:39 -0700463 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700464 bound_lifetimes: bound_lifetimes >>
465 trait_ref: path >>
David Tolnayf6c74402016-10-08 02:31:26 -0700466 parenthesized: cond!(
467 trait_ref.segments.last().unwrap().parameters == PathParameters::none(),
468 option!(parenthesized_parameter_data)
469 ) >>
470 ({
471 let mut trait_ref = trait_ref;
472 if let Some(Some(parenthesized)) = parenthesized {
473 trait_ref.segments.last_mut().unwrap().parameters = parenthesized;
474 }
475 PolyTraitRef {
476 bound_lifetimes: bound_lifetimes,
477 trait_ref: trait_ref,
478 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700479 })
480 ));
481
David Tolnay62f374c2016-10-02 13:37:00 -0700482 named!(pub fn_arg -> BareFnArg, do_parse!(
483 name: option!(terminated!(ident, punct!(":"))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700484 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700485 (BareFnArg {
486 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700487 ty: ty,
488 })
489 ));
490}
David Tolnay87d0b442016-09-04 11:52:12 -0700491
492#[cfg(feature = "printing")]
493mod printing {
494 use super::*;
495 use quote::{Tokens, ToTokens};
496
497 impl ToTokens for Ty {
498 fn to_tokens(&self, tokens: &mut Tokens) {
499 match *self {
David Tolnay16709ba2016-10-05 23:11:32 -0700500 Ty::Slice(ref inner) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700501 tokens.append("[");
502 inner.to_tokens(tokens);
503 tokens.append("]");
504 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700505 Ty::Array(ref inner, ref len) => {
David Tolnay87d0b442016-09-04 11:52:12 -0700506 tokens.append("[");
507 inner.to_tokens(tokens);
508 tokens.append(";");
David Tolnayfa94b6f2016-10-05 23:26:11 -0700509 len.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700510 tokens.append("]");
511 }
512 Ty::Ptr(ref target) => {
513 tokens.append("*");
514 match target.mutability {
515 Mutability::Mutable => tokens.append("mut"),
516 Mutability::Immutable => tokens.append("const"),
517 }
518 target.ty.to_tokens(tokens);
519 }
520 Ty::Rptr(ref lifetime, ref target) => {
521 tokens.append("&");
522 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700523 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700524 target.ty.to_tokens(tokens);
525 }
526 Ty::BareFn(ref func) => {
527 func.to_tokens(tokens);
528 }
529 Ty::Never => {
530 tokens.append("!");
531 }
532 Ty::Tup(ref elems) => {
533 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700534 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700535 if elems.len() == 1 {
536 tokens.append(",");
537 }
538 tokens.append(")");
539 }
David Tolnayf69904a2016-09-04 14:46:07 -0700540 Ty::Path(None, ref path) => {
541 path.to_tokens(tokens);
542 }
543 Ty::Path(Some(ref qself), ref path) => {
544 tokens.append("<");
545 qself.ty.to_tokens(tokens);
546 if qself.position > 0 {
547 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -0700548 for (i, segment) in path.segments
549 .iter()
550 .take(qself.position)
551 .enumerate() {
David Tolnayf69904a2016-09-04 14:46:07 -0700552 if i > 0 || path.global {
553 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700554 }
David Tolnayf69904a2016-09-04 14:46:07 -0700555 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700556 }
David Tolnayf69904a2016-09-04 14:46:07 -0700557 }
558 tokens.append(">");
559 for segment in path.segments.iter().skip(qself.position) {
560 tokens.append("::");
561 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700562 }
563 }
David Tolnay6414da72016-10-08 00:55:17 -0700564 Ty::ObjectSum(ref ty, ref bounds) => {
565 ty.to_tokens(tokens);
566 for bound in bounds {
567 tokens.append("+");
568 bound.to_tokens(tokens);
569 }
570 }
571 Ty::PolyTraitRef(ref bounds) => {
572 tokens.append_separated(bounds, "+");
573 }
David Tolnay87d0b442016-09-04 11:52:12 -0700574 Ty::ImplTrait(ref bounds) => {
575 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700576 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700577 }
578 Ty::Paren(ref inner) => {
579 tokens.append("(");
580 inner.to_tokens(tokens);
581 tokens.append(")");
582 }
583 Ty::Infer => {
584 tokens.append("_");
585 }
586 }
587 }
588 }
589
David Tolnay47a877c2016-10-01 16:50:55 -0700590 impl ToTokens for Mutability {
591 fn to_tokens(&self, tokens: &mut Tokens) {
592 if let Mutability::Mutable = *self {
593 tokens.append("mut");
594 }
595 }
596 }
597
David Tolnay87d0b442016-09-04 11:52:12 -0700598 impl ToTokens for Path {
599 fn to_tokens(&self, tokens: &mut Tokens) {
600 for (i, segment) in self.segments.iter().enumerate() {
601 if i > 0 || self.global {
602 tokens.append("::");
603 }
604 segment.to_tokens(tokens);
605 }
606 }
607 }
608
609 impl ToTokens for PathSegment {
610 fn to_tokens(&self, tokens: &mut Tokens) {
611 self.ident.to_tokens(tokens);
612 self.parameters.to_tokens(tokens);
613 }
614 }
615
616 impl ToTokens for PathParameters {
617 fn to_tokens(&self, tokens: &mut Tokens) {
618 match *self {
619 PathParameters::AngleBracketed(ref parameters) => {
620 parameters.to_tokens(tokens);
621 }
622 PathParameters::Parenthesized(ref parameters) => {
623 parameters.to_tokens(tokens);
624 }
625 }
626 }
627 }
628
629 impl ToTokens for AngleBracketedParameterData {
630 fn to_tokens(&self, tokens: &mut Tokens) {
631 let has_lifetimes = !self.lifetimes.is_empty();
632 let has_types = !self.types.is_empty();
633 let has_bindings = !self.bindings.is_empty();
634 if !has_lifetimes && !has_types && !has_bindings {
635 return;
636 }
637
638 tokens.append("<");
639
640 let mut first = true;
641 for lifetime in &self.lifetimes {
642 if !first {
643 tokens.append(",");
644 }
645 lifetime.to_tokens(tokens);
646 first = false;
647 }
648 for ty in &self.types {
649 if !first {
650 tokens.append(",");
651 }
652 ty.to_tokens(tokens);
653 first = false;
654 }
655 for binding in &self.bindings {
656 if !first {
657 tokens.append(",");
658 }
659 binding.to_tokens(tokens);
660 first = false;
661 }
662
663 tokens.append(">");
664 }
665 }
666
667 impl ToTokens for TypeBinding {
668 fn to_tokens(&self, tokens: &mut Tokens) {
669 self.ident.to_tokens(tokens);
670 tokens.append("=");
671 self.ty.to_tokens(tokens);
672 }
673 }
674
675 impl ToTokens for ParenthesizedParameterData {
676 fn to_tokens(&self, tokens: &mut Tokens) {
677 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700678 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700679 tokens.append(")");
680 if let Some(ref output) = self.output {
681 tokens.append("->");
682 output.to_tokens(tokens);
683 }
684 }
685 }
686
687 impl ToTokens for PolyTraitRef {
688 fn to_tokens(&self, tokens: &mut Tokens) {
689 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700690 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700691 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700692 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700693 tokens.append(">");
694 }
695 self.trait_ref.to_tokens(tokens);
696 }
697 }
698
699 impl ToTokens for BareFnTy {
700 fn to_tokens(&self, tokens: &mut Tokens) {
701 tokens.append("fn");
702 if !self.lifetimes.is_empty() {
703 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700704 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700705 tokens.append(">");
706 }
707 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700708 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700709 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700710 if let FunctionRetTy::Ty(ref ty) = self.output {
711 tokens.append("->");
712 ty.to_tokens(tokens);
713 }
714 }
715 }
716
David Tolnay62f374c2016-10-02 13:37:00 -0700717 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700718 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700719 if let Some(ref name) = self.name {
720 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700721 tokens.append(":");
722 }
723 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700724 }
725 }
726}