blob: e767852c5eb2d9ee5b2bcf46e7f813860b00e4d7 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07002use super::*;
3
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_enum_of_structs! {
5 /// The different kinds of types recognized by the compiler
6 pub enum Ty {
7 /// A variable-length array (`[T]`)
8 pub Slice(TySlice {
9 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070010 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070011 }),
12 /// A fixed length array (`[T; n]`)
13 pub Array(TyArray {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070014 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070015 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070016 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070017 pub amt: ConstExpr,
18 }),
19 /// A raw pointer (`*const T` or `*mut T`)
20 pub Ptr(TyPtr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070021 pub star_token: tokens::Star,
22 pub const_token: Option<tokens::Const>,
Alex Crichton62a0a592017-05-22 13:58:53 -070023 pub ty: Box<MutTy>,
24 }),
25 /// A reference (`&'a T` or `&'a mut T`)
26 pub Rptr(TyRptr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070027 pub and_token: tokens::And,
Alex Crichton62a0a592017-05-22 13:58:53 -070028 pub lifetime: Option<Lifetime>,
29 pub ty: Box<MutTy>,
30 }),
31 /// A bare function (e.g. `fn(usize) -> bool`)
32 pub BareFn(TyBareFn {
33 pub ty: Box<BareFnTy>,
34 }),
35 /// The never type (`!`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070036 pub Never(TyNever {
37 pub bang_token: tokens::Bang,
38 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070039 /// A tuple (`(A, B, C, D, ...)`)
40 pub Tup(TyTup {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070041 pub paren_token: tokens::Paren,
42 pub tys: Delimited<Ty, tokens::Comma>,
43 pub lone_comma: Option<tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -070044 }),
45 /// A path (`module::module::...::Type`), optionally
46 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
47 ///
48 /// Type parameters are stored in the Path itself
49 pub Path(TyPath {
50 pub qself: Option<QSelf>,
51 pub path: Path,
52 }),
53 /// A trait object type `Bound1 + Bound2 + Bound3`
54 /// where `Bound` is a trait or a lifetime.
55 pub TraitObject(TyTraitObject {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070056 pub bounds: Delimited<TyParamBound, tokens::Add>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// An `impl Bound1 + Bound2 + Bound3` type
59 /// where `Bound` is a trait or a lifetime.
60 pub ImplTrait(TyImplTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061 pub impl_token: tokens::Impl,
62 pub bounds: Delimited<TyParamBound, tokens::Add>,
Alex Crichton62a0a592017-05-22 13:58:53 -070063 }),
64 /// No-op; kept solely so that we can pretty-print faithfully
65 pub Paren(TyParen {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub ty: Box<Ty>,
68 }),
69 /// TyKind::Infer means the type should be inferred instead of it having been
70 /// specified. This can appear anywhere in a type.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 pub Infer(TyInfer {
72 pub underscore_token: tokens::Underscore
73 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A macro in the type position.
75 pub Mac(Mac),
76 }
77}
78
79ast_struct! {
80 pub struct MutTy {
81 pub ty: Ty,
82 pub mutability: Mutability,
83 }
84}
85
86ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -070087 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070088 pub enum Mutability {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070089 Mutable(tokens::Mut),
Alex Crichton62a0a592017-05-22 13:58:53 -070090 Immutable,
91 }
92}
93
94ast_struct! {
95 /// A "Path" is essentially Rust's notion of a name.
David Tolnayb79ee962016-09-04 09:39:20 -070096 ///
Alex Crichton62a0a592017-05-22 13:58:53 -070097 /// It's represented as a sequence of identifiers,
98 /// along with a bunch of supporting information.
99 ///
100 /// E.g. `std::cmp::PartialEq`
101 pub struct Path {
102 /// A `::foo` path, is relative to the crate root rather than current
103 /// module (like paths in an import).
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700104 pub leading_colon: Option<tokens::Colon2>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 /// The segments in the path: the things separated by `::`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700106 pub segments: Delimited<PathSegment, tokens::Colon2>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 }
David Tolnayb79ee962016-09-04 09:39:20 -0700108}
109
David Tolnay570695e2017-06-03 16:15:13 -0700110impl Path {
111 pub fn global(&self) -> bool {
112 self.leading_colon.is_some()
113 }
114}
115
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700116#[cfg(feature = "printing")]
117ast_struct! {
118 pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);
119}
120
David Tolnaydaaf7742016-10-03 11:11:43 -0700121impl<T> From<T> for Path
122 where T: Into<PathSegment>
123{
David Tolnay84aa0752016-10-02 23:01:13 -0700124 fn from(segment: T) -> Self {
125 Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 leading_colon: None,
127 segments: vec![(segment.into(), None)].into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700128 }
129 }
130}
131
Alex Crichton62a0a592017-05-22 13:58:53 -0700132ast_struct! {
133 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
134 ///
135 /// E.g. `std`, `String` or `Box<T>`
136 pub struct PathSegment {
137 /// The identifier portion of this path segment.
138 pub ident: Ident,
139 /// Type/lifetime parameters attached to this path. They come in
140 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
141 /// this is more than just simple syntactic sugar; the use of
142 /// parens affects the region binding rules, so we preserve the
143 /// distinction.
144 pub parameters: PathParameters,
145 }
David Tolnayb79ee962016-09-04 09:39:20 -0700146}
147
David Tolnaydaaf7742016-10-03 11:11:43 -0700148impl<T> From<T> for PathSegment
149 where T: Into<Ident>
150{
David Tolnay84aa0752016-10-02 23:01:13 -0700151 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700152 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700153 ident: ident.into(),
David Tolnay570695e2017-06-03 16:15:13 -0700154 parameters: PathParameters::None,
David Tolnayb79ee962016-09-04 09:39:20 -0700155 }
156 }
157}
158
Alex Crichton62a0a592017-05-22 13:58:53 -0700159ast_enum! {
160 /// Parameters of a path segment.
161 ///
162 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
163 pub enum PathParameters {
David Tolnay570695e2017-06-03 16:15:13 -0700164 None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
166 AngleBracketed(AngleBracketedParameterData),
167 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
168 Parenthesized(ParenthesizedParameterData),
169 }
David Tolnayb79ee962016-09-04 09:39:20 -0700170}
171
David Tolnay570695e2017-06-03 16:15:13 -0700172impl Default for PathParameters {
173 fn default() -> Self {
174 PathParameters::None
David Tolnayb79ee962016-09-04 09:39:20 -0700175 }
David Tolnay570695e2017-06-03 16:15:13 -0700176}
David Tolnay5332d4b2016-10-30 14:25:22 -0700177
David Tolnay570695e2017-06-03 16:15:13 -0700178impl PathParameters {
David Tolnay5332d4b2016-10-30 14:25:22 -0700179 pub fn is_empty(&self) -> bool {
180 match *self {
David Tolnay570695e2017-06-03 16:15:13 -0700181 PathParameters::None => true,
David Tolnay5332d4b2016-10-30 14:25:22 -0700182 PathParameters::AngleBracketed(ref bracketed) => {
David Tolnayc1fea502016-10-30 17:54:02 -0700183 bracketed.lifetimes.is_empty() && bracketed.types.is_empty() &&
184 bracketed.bindings.is_empty()
David Tolnay5332d4b2016-10-30 14:25:22 -0700185 }
186 PathParameters::Parenthesized(_) => false,
187 }
188 }
David Tolnayb79ee962016-09-04 09:39:20 -0700189}
190
Alex Crichton62a0a592017-05-22 13:58:53 -0700191ast_struct! {
192 /// A path like `Foo<'a, T>`
Alex Crichton62a0a592017-05-22 13:58:53 -0700193 pub struct AngleBracketedParameterData {
David Tolnay570695e2017-06-03 16:15:13 -0700194 pub turbofish: Option<tokens::Colon2>,
195 pub lt_token: tokens::Lt,
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 /// The lifetime parameters for this path segment.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700197 pub lifetimes: Delimited<Lifetime, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 /// The type parameters for this path segment, if present.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 pub types: Delimited<Ty, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 /// Bindings (equality constraints) on associated types, if present.
201 ///
202 /// E.g., `Foo<A=Bar>`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700203 pub bindings: Delimited<TypeBinding, tokens::Comma>,
David Tolnay570695e2017-06-03 16:15:13 -0700204 pub gt_token: tokens::Gt,
Alex Crichton62a0a592017-05-22 13:58:53 -0700205 }
206}
207
208ast_struct! {
209 /// Bind a type to an associated type: `A=Foo`.
210 pub struct TypeBinding {
211 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700212 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub ty: Ty,
214 }
215}
216
217
218ast_struct! {
219 /// A path like `Foo(A,B) -> C`
220 pub struct ParenthesizedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700221 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700222 /// `(A, B)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700223 pub inputs: Delimited<Ty, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 /// `C`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700225 pub output: FunctionRetTy,
Alex Crichton62a0a592017-05-22 13:58:53 -0700226 }
227}
228
229ast_struct! {
230 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 /// The `for<'a>` in `for<'a> Foo<&'a T>`
232 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700233 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
234 pub trait_ref: Path,
235 }
236}
237
238ast_struct! {
239 /// The explicit Self type in a "qualified path". The actual
240 /// path, including the trait and the associated item, is stored
241 /// separately. `position` represents the index of the associated
242 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700243 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700244 /// ```rust,ignore
245 /// <Vec<T> as a::b::Trait>::AssociatedItem
246 /// ^~~~~ ~~~~~~~~~~~~~~^
247 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700248 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 /// <Vec<T>>::AssociatedItem
250 /// ^~~~~ ^
251 /// ty position = 0
252 /// ```
253 pub struct QSelf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700254 pub lt_token: tokens::Lt,
Alex Crichton62a0a592017-05-22 13:58:53 -0700255 pub ty: Box<Ty>,
David Tolnay570695e2017-06-03 16:15:13 -0700256 pub position: Option<(tokens::As, usize)>,
257 pub gt_token: tokens::Gt,
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
259}
260
261ast_struct! {
262 pub struct BareFnTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700263 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 pub unsafety: Unsafety,
265 pub abi: Option<Abi>,
Alex Crichton954046c2017-05-30 21:49:42 -0700266 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700267 pub paren_token: tokens::Paren,
268 pub inputs: Delimited<BareFnArg, tokens::Comma>,
269 pub variadic: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700270 pub output: FunctionRetTy,
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 }
272}
273
274ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700275 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 pub enum Unsafety {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700277 Unsafe(tokens::Unsafe),
Alex Crichton62a0a592017-05-22 13:58:53 -0700278 Normal,
279 }
280}
281
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700282ast_struct! {
283 pub struct Abi {
284 pub extern_token: tokens::Extern,
285 pub kind: AbiKind,
286 }
287}
288
Alex Crichton62a0a592017-05-22 13:58:53 -0700289ast_enum! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700290 pub enum AbiKind {
291 Named(Lit),
292 Default,
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 }
294}
295
296ast_struct! {
297 /// An argument in a function type.
298 ///
299 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
300 pub struct BareFnArg {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700301 pub name: Option<(Ident, tokens::Colon)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 pub ty: Ty,
303 }
304}
305
306
307ast_enum! {
308 pub enum FunctionRetTy {
309 /// Return type is not specified.
310 ///
311 /// Functions default to `()` and
312 /// closures default to inference. Span points to where return
313 /// type would be inserted.
314 Default,
315 /// Everything else
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700316 Ty(Ty, tokens::RArrow),
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 }
David Tolnayb79ee962016-09-04 09:39:20 -0700318}
319
David Tolnay86eca752016-09-04 11:26:41 -0700320#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700321pub mod parsing {
322 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400323 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700324 use synom::tokens::*;
David Tolnayda4049b2016-09-04 10:59:23 -0700325
Alex Crichton954046c2017-05-30 21:49:42 -0700326 impl Synom for Ty {
Michael Layzell92639a52017-06-01 00:07:44 -0400327 named!(parse -> Self, alt!(
328 // must be before mac
329 syn!(TyParen) => { Ty::Paren }
330 |
331 // must be before path
332 syn!(Mac) => { Ty::Mac }
333 |
334 // must be before ty_poly_trait_ref
335 ty_path
336 |
337 syn!(TySlice) => { Ty::Slice }
338 |
339 syn!(TyArray) => { Ty::Array }
340 |
341 syn!(TyPtr) => { Ty::Ptr }
342 |
343 syn!(TyRptr) => { Ty::Rptr }
344 |
345 syn!(TyBareFn) => { Ty::BareFn }
346 |
347 syn!(TyNever) => { Ty::Never }
348 |
349 syn!(TyTup) => { Ty::Tup }
350 |
351 ty_poly_trait_ref
352 |
353 syn!(TyImplTrait) => { Ty::ImplTrait }
354 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700355
Alex Crichton954046c2017-05-30 21:49:42 -0700356 fn description() -> Option<&'static str> {
357 Some("type")
358 }
359 }
David Tolnay0047c712016-12-21 21:59:25 -0500360
Alex Crichton954046c2017-05-30 21:49:42 -0700361 impl Synom for TySlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400362 named!(parse -> Self, map!(
363 brackets!(syn!(Ty)),
364 |(ty, b)| TySlice {
365 ty: Box::new(ty),
366 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700367 }
Michael Layzell92639a52017-06-01 00:07:44 -0400368 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700369 }
David Tolnayb79ee962016-09-04 09:39:20 -0700370
Alex Crichton954046c2017-05-30 21:49:42 -0700371 impl Synom for TyArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400372 named!(parse -> Self, map!(
373 brackets!(do_parse!(
374 elem: syn!(Ty) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700375 semi: syn!(Semi) >>
376 len: array_len >>
377 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400378 )),
379 |((elem, semi, len), brackets)| {
380 TyArray {
381 ty: Box::new(elem),
382 amt: len,
383 bracket_token: brackets,
384 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700385 }
386 }
Michael Layzell92639a52017-06-01 00:07:44 -0400387 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700388 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700389
David Tolnay514f1292017-02-27 12:30:57 -0800390 #[cfg(not(feature = "full"))]
Alex Crichton954046c2017-05-30 21:49:42 -0700391 named!(array_len -> ConstExpr, syn!(ConstExpr));
David Tolnay514f1292017-02-27 12:30:57 -0800392
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700393 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800394 named!(array_len -> ConstExpr, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700395 terminated!(syn!(ConstExpr), input_end!())
David Tolnay514f1292017-02-27 12:30:57 -0800396 |
Alex Crichton954046c2017-05-30 21:49:42 -0700397 terminated!(syn!(Expr), input_end!()) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700398 ));
399
Alex Crichton954046c2017-05-30 21:49:42 -0700400 impl Synom for TyPtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400401 named!(parse -> Self, do_parse!(
402 star: syn!(Star) >>
403 mutability: alt!(
404 syn!(Const) => { |c| (Mutability::Immutable, Some(c)) }
405 |
406 syn!(Mut) => { |m| (Mutability::Mutable(m), None) }
407 ) >>
408 target: syn!(Ty) >>
409 (TyPtr {
410 const_token: mutability.1,
411 star_token: star,
412 ty: Box::new(MutTy {
413 ty: target,
414 mutability: mutability.0,
415 }),
416 })
417 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700418 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700419
Alex Crichton954046c2017-05-30 21:49:42 -0700420 impl Synom for TyRptr {
Michael Layzell92639a52017-06-01 00:07:44 -0400421 named!(parse -> Self, do_parse!(
422 amp: syn!(And) >>
423 life: option!(syn!(Lifetime)) >>
424 mutability: syn!(Mutability) >>
425 target: syn!(Ty) >>
426 (TyRptr {
427 lifetime: life,
428 ty: Box::new(MutTy {
429 ty: target,
430 mutability: mutability,
431 }),
432 and_token: amp,
433 })
434 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700435 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700436
Alex Crichton954046c2017-05-30 21:49:42 -0700437 impl Synom for TyBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400438 named!(parse -> Self, do_parse!(
439 lifetimes: option!(syn!(BoundLifetimes)) >>
440 unsafety: syn!(Unsafety) >>
441 abi: option!(syn!(Abi)) >>
442 fn_: syn!(Fn_) >>
443 parens: parens!(do_parse!(
444 inputs: call!(Delimited::parse_terminated) >>
445 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
446 syn!(Dot3))) >>
447 (inputs, variadic)
448 )) >>
449 output: syn!(FunctionRetTy) >>
450 (TyBareFn {
451 ty: Box::new(BareFnTy {
452 unsafety: unsafety,
453 abi: abi,
454 lifetimes: lifetimes,
455 output: output,
456 variadic: (parens.0).1,
457 fn_token: fn_,
458 paren_token: parens.1,
459 inputs: (parens.0).0,
460 }),
461 })
462 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700463 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700464
Alex Crichton954046c2017-05-30 21:49:42 -0700465 impl Synom for TyNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400466 named!(parse -> Self, map!(
467 syn!(Bang),
468 |b| TyNever { bang_token: b }
469 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700470 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700471
Alex Crichton954046c2017-05-30 21:49:42 -0700472 impl Synom for TyTup {
Michael Layzell92639a52017-06-01 00:07:44 -0400473 named!(parse -> Self, do_parse!(
474 data: parens!(call!(Delimited::parse_terminated)) >>
475 (TyTup {
476 tys: data.0,
477 paren_token: data.1,
478 lone_comma: None, // TODO: does this just not parse?
479 })
480 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700481 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700482
David Tolnay6414da72016-10-08 00:55:17 -0700483 named!(ty_path -> Ty, do_parse!(
484 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700485 parenthesized: cond!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700486 qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(),
Alex Crichton954046c2017-05-30 21:49:42 -0700487 option!(syn!(ParenthesizedParameterData))
David Tolnayf6c74402016-10-08 02:31:26 -0700488 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700489 bounds: many0!(tuple!(syn!(Add), syn!(TyParamBound))) >>
David Tolnay6414da72016-10-08 00:55:17 -0700490 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700491 let (qself, mut path) = qpath;
492 if let Some(Some(parenthesized)) = parenthesized {
Alex Crichton954046c2017-05-30 21:49:42 -0700493 let parenthesized = PathParameters::Parenthesized(parenthesized);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700494 let len = path.segments.len();
495 path.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700496 }
David Tolnay6414da72016-10-08 00:55:17 -0700497 if bounds.is_empty() {
Alex Crichton62a0a592017-05-22 13:58:53 -0700498 TyPath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700499 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800500 let path = TyParamBound::Trait(
501 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700502 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800503 trait_ref: path,
504 },
505 TraitBoundModifier::None,
506 );
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 let mut new_bounds = Delimited::new();
508 new_bounds.push_first(path);
Alex Crichton954046c2017-05-30 21:49:42 -0700509 for (_tok, bound) in bounds {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510 new_bounds.push_default(bound);
511 }
512 TyTraitObject { bounds: new_bounds }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700513 }
514 })
515 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700516
David Tolnay9636c052016-10-02 17:11:17 -0700517 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700518 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700519 |
520 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700521 lt: syn!(Lt) >>
David Tolnay570695e2017-06-03 16:15:13 -0700522 this: syn!(Ty) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700523 path: option!(do_parse!(
524 as_: syn!(As) >>
525 path: syn!(Path) >>
526 (as_, path)
David Tolnay9636c052016-10-02 17:11:17 -0700527 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700528 gt: syn!(Gt) >>
529 colon2: syn!(Colon2) >>
530 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700531 ({
David Tolnay570695e2017-06-03 16:15:13 -0700532 let (pos, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700533 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700534 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700535 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700536 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700537 }
Alex Crichton954046c2017-05-30 21:49:42 -0700538 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700539 path.segments.push(item);
540 }
David Tolnay570695e2017-06-03 16:15:13 -0700541 (Some((as_, pos)), path)
David Tolnay9636c052016-10-02 17:11:17 -0700542 }
543 None => {
David Tolnay570695e2017-06-03 16:15:13 -0700544 (None, Path {
545 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700546 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700547 })
David Tolnay9636c052016-10-02 17:11:17 -0700548 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700549 };
550 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700551 lt_token: lt,
552 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700553 position: pos,
Alex Crichton954046c2017-05-30 21:49:42 -0700554 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700556 })
557 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700558 |
David Tolnaybc7d7d92017-06-03 20:54:05 -0700559 map!(syn!(Self_), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700560 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700561
Alex Crichton954046c2017-05-30 21:49:42 -0700562 impl Synom for ParenthesizedParameterData {
Michael Layzell92639a52017-06-01 00:07:44 -0400563 named!(parse -> Self, do_parse!(
564 data: parens!(call!(Delimited::parse_terminated)) >>
565 output: syn!(FunctionRetTy) >>
566 (ParenthesizedParameterData {
567 paren_token: data.1,
568 inputs: data.0,
569 output: output,
570 })
571 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700572 }
573
574 impl Synom for FunctionRetTy {
Michael Layzell92639a52017-06-01 00:07:44 -0400575 named!(parse -> Self, alt!(
576 do_parse!(
577 arrow: syn!(RArrow) >>
578 ty: syn!(Ty) >>
579 (FunctionRetTy::Ty(ty, arrow))
580 )
581 |
582 epsilon!() => { |_| FunctionRetTy::Default }
583 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700584 }
585
David Tolnay4f0f2512016-10-30 09:28:14 -0700586 named!(ty_poly_trait_ref -> Ty, map!(
Alex Crichton954046c2017-05-30 21:49:42 -0700587 call!(Delimited::parse_separated_nonempty),
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 |x| TyTraitObject { bounds: x }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700589 ));
590
Alex Crichton954046c2017-05-30 21:49:42 -0700591 impl Synom for TyImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400592 named!(parse -> Self, do_parse!(
593 impl_: syn!(Impl) >>
594 elem: call!(Delimited::parse_separated_nonempty) >>
595 (TyImplTrait {
596 impl_token: impl_,
597 bounds: elem,
598 })
599 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700600 }
David Tolnayb79ee962016-09-04 09:39:20 -0700601
Alex Crichton954046c2017-05-30 21:49:42 -0700602 impl Synom for TyParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400603 named!(parse -> Self, do_parse!(
604 data: parens!(syn!(Ty)) >>
605 (TyParen {
606 paren_token: data.1,
607 ty: Box::new(data.0),
608 })
609 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700610 }
David Tolnayb79ee962016-09-04 09:39:20 -0700611
Alex Crichton954046c2017-05-30 21:49:42 -0700612 impl Synom for Mutability {
Michael Layzell92639a52017-06-01 00:07:44 -0400613 named!(parse -> Self, alt!(
614 syn!(Mut) => { Mutability::Mutable }
615 |
616 epsilon!() => { |_| Mutability::Immutable }
617 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700618 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700619
Alex Crichton954046c2017-05-30 21:49:42 -0700620 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400621 named!(parse -> Self, do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700622 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400623 segments: call!(Delimited::parse_separated_nonempty) >>
624 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700625 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400626 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400627 })
628 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700629 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700630
Alex Crichton954046c2017-05-30 21:49:42 -0700631 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400632 named!(parse -> Self, alt!(
633 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700634 ident: syn!(Ident) >>
635 turbofish: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400636 lt: syn!(Lt) >>
637 lifetimes: call!(Delimited::parse_terminated) >>
638 types: cond!(
639 lifetimes.is_empty() || lifetimes.trailing_delim(),
640 call!(Delimited::parse_terminated_with,
641 ty_no_eq_after)
642 ) >>
643 bindings: cond!(
644 match types {
645 Some(ref t) => t.is_empty() || t.trailing_delim(),
646 None => lifetimes.is_empty() || lifetimes.trailing_delim(),
647 },
648 call!(Delimited::parse_terminated)
649 ) >>
650 gt: syn!(Gt) >>
651 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700652 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -0400653 parameters: PathParameters::AngleBracketed(
654 AngleBracketedParameterData {
David Tolnay570695e2017-06-03 16:15:13 -0700655 turbofish: turbofish,
656 lt_token: lt,
Michael Layzell92639a52017-06-01 00:07:44 -0400657 lifetimes: lifetimes,
658 types: types.unwrap_or_default(),
659 bindings: bindings.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -0700660 gt_token: gt,
Michael Layzell92639a52017-06-01 00:07:44 -0400661 }
662 ),
663 })
664 )
665 |
666 mod_style_path_segment
667 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700668 }
David Tolnay570695e2017-06-03 16:15:13 -0700669
Alex Crichton954046c2017-05-30 21:49:42 -0700670 named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700671
Alex Crichton954046c2017-05-30 21:49:42 -0700672 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400673 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700674 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400675 segments: call!(Delimited::parse_separated_nonempty_with,
676 mod_style_path_segment) >>
677 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700678 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400679 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400680 })
681 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700682 }
Arnavionf2dada12017-04-20 23:55:20 -0700683
684 named!(mod_style_path_segment -> PathSegment, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700685 map!(syn!(Ident), Into::into)
Arnavionf2dada12017-04-20 23:55:20 -0700686 |
Alex Crichton954046c2017-05-30 21:49:42 -0700687 alt!(
688 syn!(Super) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700689 |
Alex Crichton954046c2017-05-30 21:49:42 -0700690 syn!(Self_) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700691 |
Alex Crichton954046c2017-05-30 21:49:42 -0700692 syn!(CapSelf) => { Into::into }
693 )
Arnavionf2dada12017-04-20 23:55:20 -0700694 ));
695
Alex Crichton954046c2017-05-30 21:49:42 -0700696 impl Synom for TypeBinding {
Michael Layzell92639a52017-06-01 00:07:44 -0400697 named!(parse -> Self, do_parse!(
698 id: syn!(Ident) >>
699 eq: syn!(Eq) >>
700 ty: syn!(Ty) >>
701 (TypeBinding {
702 ident: id,
703 eq_token: eq,
704 ty: ty,
705 })
706 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700707 }
708
709 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400710 named!(parse -> Self, do_parse!(
711 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
712 trait_ref: syn!(Path) >>
713 parenthesized: option!(cond_reduce!(
714 trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(),
715 syn!(ParenthesizedParameterData)
716 )) >>
717 ({
718 let mut trait_ref = trait_ref;
719 if let Some(parenthesized) = parenthesized {
720 let parenthesized = PathParameters::Parenthesized(parenthesized);
721 let len = trait_ref.segments.len();
722 trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
723 }
724 PolyTraitRef {
725 bound_lifetimes: bound_lifetimes,
726 trait_ref: trait_ref,
727 }
728 })
729 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700730 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700731
Alex Crichton954046c2017-05-30 21:49:42 -0700732 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400733 named!(parse -> Self, do_parse!(
734 name: option!(do_parse!(
735 name: syn!(Ident) >>
736 not!(syn!(Colon2)) >>
737 colon: syn!(Colon) >>
738 (name, colon)
739 )) >>
740 ty: syn!(Ty) >>
741 (BareFnArg {
742 name: name,
743 ty: ty,
744 })
745 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700746 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700747
Alex Crichton954046c2017-05-30 21:49:42 -0700748 impl Synom for Unsafety {
Michael Layzell92639a52017-06-01 00:07:44 -0400749 named!(parse -> Self, alt!(
750 syn!(Unsafe) => { Unsafety::Unsafe }
751 |
752 epsilon!() => { |_| Unsafety::Normal }
753 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700754 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700755
Alex Crichton954046c2017-05-30 21:49:42 -0700756 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400757 named!(parse -> Self, do_parse!(
758 extern_: syn!(Extern) >>
759 // TODO: this parses all literals, not just strings
760 name: option!(syn!(Lit)) >>
761 (Abi {
762 extern_token: extern_,
763 kind: match name {
764 Some(name) => AbiKind::Named(name),
765 None => AbiKind::Default,
766 },
767 })
768 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700769 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700770}
David Tolnay87d0b442016-09-04 11:52:12 -0700771
772#[cfg(feature = "printing")]
773mod printing {
774 use super::*;
775 use quote::{Tokens, ToTokens};
776
Alex Crichton62a0a592017-05-22 13:58:53 -0700777 impl ToTokens for TySlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700778 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700779 self.bracket_token.surround(tokens, |tokens| {
780 self.ty.to_tokens(tokens);
781 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700782 }
783 }
784
785 impl ToTokens for TyArray {
786 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700787 self.bracket_token.surround(tokens, |tokens| {
788 self.ty.to_tokens(tokens);
789 self.semi_token.to_tokens(tokens);
790 self.amt.to_tokens(tokens);
791 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700792 }
793 }
794
795 impl ToTokens for TyPtr {
796 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700797 self.star_token.to_tokens(tokens);
798 self.const_token.to_tokens(tokens);
799 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700800 self.ty.ty.to_tokens(tokens);
801 }
802 }
803
804 impl ToTokens for TyRptr {
805 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700806 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700807 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700808 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700809 self.ty.ty.to_tokens(tokens);
810 }
811 }
812
813 impl ToTokens for TyBareFn {
814 fn to_tokens(&self, tokens: &mut Tokens) {
815 self.ty.to_tokens(tokens)
816 }
817 }
818
819 impl ToTokens for TyNever {
820 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700821 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700822 }
823 }
824
825 impl ToTokens for TyTup {
826 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 self.paren_token.surround(tokens, |tokens| {
828 self.tys.to_tokens(tokens);
829 self.lone_comma.to_tokens(tokens);
830 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700831 }
832 }
833
834 impl ToTokens for TyPath {
835 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700836 PathTokens(&self.qself, &self.path).to_tokens(tokens);
837 }
838 }
839
840 impl<'a> ToTokens for PathTokens<'a> {
841 fn to_tokens(&self, tokens: &mut Tokens) {
842 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700843 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700844 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700845 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700847 qself.ty.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700848 let mut segments = self.1.segments.iter();
David Tolnayb99e1b02017-06-03 19:00:55 -0700849 if let Some((ref as_token, pos)) = qself.position {
David Tolnay570695e2017-06-03 16:15:13 -0700850 as_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700851 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700852 for (i, segment) in (&mut segments).take(pos).enumerate() {
853 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 segment.item().to_tokens(tokens);
855 qself.gt_token.to_tokens(tokens);
856 segment.delimiter().to_tokens(tokens);
857 } else {
858 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700859 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700860 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700861 } else {
862 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700863 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700864 }
David Tolnay570695e2017-06-03 16:15:13 -0700865 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700866 segment.to_tokens(tokens);
867 }
868 }
869 }
870
871 impl ToTokens for TyTraitObject {
872 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700874 }
875 }
876
877 impl ToTokens for TyImplTrait {
878 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700879 self.impl_token.to_tokens(tokens);
880 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700881 }
882 }
883
884 impl ToTokens for TyParen {
885 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700886 self.paren_token.surround(tokens, |tokens| {
887 self.ty.to_tokens(tokens);
888 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700889 }
890 }
891
892 impl ToTokens for TyInfer {
893 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700894 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700895 }
896 }
897
David Tolnay47a877c2016-10-01 16:50:55 -0700898 impl ToTokens for Mutability {
899 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 if let Mutability::Mutable(ref t) = *self {
901 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700902 }
903 }
904 }
905
David Tolnay87d0b442016-09-04 11:52:12 -0700906 impl ToTokens for Path {
907 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 self.leading_colon.to_tokens(tokens);
909 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700910 }
911 }
912
913 impl ToTokens for PathSegment {
914 fn to_tokens(&self, tokens: &mut Tokens) {
915 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 self.parameters.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700917 }
918 }
919
920 impl ToTokens for PathParameters {
921 fn to_tokens(&self, tokens: &mut Tokens) {
922 match *self {
David Tolnay570695e2017-06-03 16:15:13 -0700923 PathParameters::None => {}
David Tolnay87d0b442016-09-04 11:52:12 -0700924 PathParameters::AngleBracketed(ref parameters) => {
925 parameters.to_tokens(tokens);
926 }
927 PathParameters::Parenthesized(ref parameters) => {
928 parameters.to_tokens(tokens);
929 }
930 }
931 }
932 }
933
934 impl ToTokens for AngleBracketedParameterData {
935 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -0700936 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700937 self.lt_token.to_tokens(tokens);
938 self.lifetimes.to_tokens(tokens);
939 self.types.to_tokens(tokens);
940 self.bindings.to_tokens(tokens);
941 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700942 }
943 }
944
945 impl ToTokens for TypeBinding {
946 fn to_tokens(&self, tokens: &mut Tokens) {
947 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700949 self.ty.to_tokens(tokens);
950 }
951 }
952
953 impl ToTokens for ParenthesizedParameterData {
954 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700955 self.paren_token.surround(tokens, |tokens| {
956 self.inputs.to_tokens(tokens);
957 });
958 self.output.to_tokens(tokens);
959 }
960 }
961
962 impl ToTokens for FunctionRetTy {
963 fn to_tokens(&self, tokens: &mut Tokens) {
964 match *self {
965 FunctionRetTy::Default => {}
966 FunctionRetTy::Ty(ref ty, ref arrow) => {
967 arrow.to_tokens(tokens);
968 ty.to_tokens(tokens);
969 }
David Tolnay87d0b442016-09-04 11:52:12 -0700970 }
971 }
972 }
973
974 impl ToTokens for PolyTraitRef {
975 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700976 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700977 self.trait_ref.to_tokens(tokens);
978 }
979 }
980
981 impl ToTokens for BareFnTy {
982 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700983 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700984 self.unsafety.to_tokens(tokens);
985 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700986 self.fn_token.to_tokens(tokens);
987 self.paren_token.surround(tokens, |tokens| {
988 self.inputs.to_tokens(tokens);
989 self.variadic.to_tokens(tokens);
990 });
991 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700992 }
993 }
994
David Tolnay62f374c2016-10-02 13:37:00 -0700995 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700996 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700997 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700998 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700999 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001000 }
1001 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001002 }
1003 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001004
1005 impl ToTokens for Unsafety {
1006 fn to_tokens(&self, tokens: &mut Tokens) {
1007 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001009 Unsafety::Normal => {
1010 // nothing
1011 }
1012 }
1013 }
1014 }
1015
1016 impl ToTokens for Abi {
1017 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001018 self.extern_token.to_tokens(tokens);
1019 match self.kind {
1020 AbiKind::Named(ref named) => named.to_tokens(tokens),
1021 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001022 }
1023 }
1024 }
David Tolnay87d0b442016-09-04 11:52:12 -07001025}