blob: c6e853964103de10549499d0701e29f1d0db0a3c [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).
104 pub global: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700105 pub leading_colon: Option<tokens::Colon2>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 /// The segments in the path: the things separated by `::`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107 pub segments: Delimited<PathSegment, tokens::Colon2>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700108 }
David Tolnayb79ee962016-09-04 09:39:20 -0700109}
110
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111#[cfg(feature = "printing")]
112ast_struct! {
113 pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);
114}
115
David Tolnaydaaf7742016-10-03 11:11:43 -0700116impl<T> From<T> for Path
117 where T: Into<PathSegment>
118{
David Tolnay84aa0752016-10-02 23:01:13 -0700119 fn from(segment: T) -> Self {
120 Path {
121 global: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 leading_colon: None,
123 segments: vec![(segment.into(), None)].into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700124 }
125 }
126}
127
Alex Crichton62a0a592017-05-22 13:58:53 -0700128ast_struct! {
129 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
130 ///
131 /// E.g. `std`, `String` or `Box<T>`
132 pub struct PathSegment {
133 /// The identifier portion of this path segment.
134 pub ident: Ident,
135 /// Type/lifetime parameters attached to this path. They come in
136 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
137 /// this is more than just simple syntactic sugar; the use of
138 /// parens affects the region binding rules, so we preserve the
139 /// distinction.
140 pub parameters: PathParameters,
141 }
David Tolnayb79ee962016-09-04 09:39:20 -0700142}
143
David Tolnaydaaf7742016-10-03 11:11:43 -0700144impl<T> From<T> for PathSegment
145 where T: Into<Ident>
146{
David Tolnay84aa0752016-10-02 23:01:13 -0700147 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700148 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700149 ident: ident.into(),
David Tolnayb79ee962016-09-04 09:39:20 -0700150 parameters: PathParameters::none(),
151 }
152 }
153}
154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155ast_enum! {
156 /// Parameters of a path segment.
157 ///
158 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
159 pub enum PathParameters {
160 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
161 AngleBracketed(AngleBracketedParameterData),
162 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
163 Parenthesized(ParenthesizedParameterData),
164 }
David Tolnayb79ee962016-09-04 09:39:20 -0700165}
166
167impl PathParameters {
168 pub fn none() -> Self {
169 PathParameters::AngleBracketed(AngleBracketedParameterData::default())
170 }
David Tolnay5332d4b2016-10-30 14:25:22 -0700171
172 pub fn is_empty(&self) -> bool {
173 match *self {
174 PathParameters::AngleBracketed(ref bracketed) => {
David Tolnayc1fea502016-10-30 17:54:02 -0700175 bracketed.lifetimes.is_empty() && bracketed.types.is_empty() &&
176 bracketed.bindings.is_empty()
David Tolnay5332d4b2016-10-30 14:25:22 -0700177 }
178 PathParameters::Parenthesized(_) => false,
179 }
180 }
David Tolnayb79ee962016-09-04 09:39:20 -0700181}
182
Alex Crichton62a0a592017-05-22 13:58:53 -0700183ast_struct! {
184 /// A path like `Foo<'a, T>`
185 #[derive(Default)]
186 pub struct AngleBracketedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700187 pub lt_token: Option<tokens::Lt>,
188 pub gt_token: Option<tokens::Gt>,
189
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 /// The lifetime parameters for this path segment.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700191 pub lifetimes: Delimited<Lifetime, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 /// The type parameters for this path segment, if present.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700193 pub types: Delimited<Ty, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 /// Bindings (equality constraints) on associated types, if present.
195 ///
196 /// E.g., `Foo<A=Bar>`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700197 pub bindings: Delimited<TypeBinding, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 }
199}
200
201ast_struct! {
202 /// Bind a type to an associated type: `A=Foo`.
203 pub struct TypeBinding {
204 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700205 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub ty: Ty,
207 }
208}
209
210
211ast_struct! {
212 /// A path like `Foo(A,B) -> C`
213 pub struct ParenthesizedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700214 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 /// `(A, B)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700216 pub inputs: Delimited<Ty, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 /// `C`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700218 pub output: FunctionRetTy,
Alex Crichton62a0a592017-05-22 13:58:53 -0700219 }
220}
221
222ast_struct! {
223 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700224 /// The `for<'a>` in `for<'a> Foo<&'a T>`
225 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700226 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
227 pub trait_ref: Path,
228 }
229}
230
231ast_struct! {
232 /// The explicit Self type in a "qualified path". The actual
233 /// path, including the trait and the associated item, is stored
234 /// separately. `position` represents the index of the associated
235 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700236 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 /// ```rust,ignore
238 /// <Vec<T> as a::b::Trait>::AssociatedItem
239 /// ^~~~~ ~~~~~~~~~~~~~~^
240 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700241 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 /// <Vec<T>>::AssociatedItem
243 /// ^~~~~ ^
244 /// ty position = 0
245 /// ```
246 pub struct QSelf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700247 pub lt_token: tokens::Lt,
248 pub gt_token: tokens::Gt,
249 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 pub ty: Box<Ty>,
251 pub position: usize,
252 }
253}
254
255ast_struct! {
256 pub struct BareFnTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700257 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 pub unsafety: Unsafety,
259 pub abi: Option<Abi>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700260 pub fn_token: tokens::Fn,
261 pub paren_token: tokens::Paren,
262 pub inputs: Delimited<BareFnArg, tokens::Comma>,
263 pub variadic: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 pub output: FunctionRetTy,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 }
266}
267
268ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700269 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700270 pub enum Unsafety {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700271 Unsafe(tokens::Unsafe),
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 Normal,
273 }
274}
275
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700276ast_struct! {
277 pub struct Abi {
278 pub extern_token: tokens::Extern,
279 pub kind: AbiKind,
280 }
281}
282
Alex Crichton62a0a592017-05-22 13:58:53 -0700283ast_enum! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284 pub enum AbiKind {
285 Named(Lit),
286 Default,
Alex Crichton62a0a592017-05-22 13:58:53 -0700287 }
288}
289
290ast_struct! {
291 /// An argument in a function type.
292 ///
293 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
294 pub struct BareFnArg {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700295 pub name: Option<(Ident, tokens::Colon)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 pub ty: Ty,
297 }
298}
299
300
301ast_enum! {
302 pub enum FunctionRetTy {
303 /// Return type is not specified.
304 ///
305 /// Functions default to `()` and
306 /// closures default to inference. Span points to where return
307 /// type would be inserted.
308 Default,
309 /// Everything else
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700310 Ty(Ty, tokens::RArrow),
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 }
David Tolnayb79ee962016-09-04 09:39:20 -0700312}
313
David Tolnay86eca752016-09-04 11:26:41 -0700314#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700315pub mod parsing {
316 use super::*;
David Tolnaya6ac1812017-01-23 00:29:11 -0800317 use {TyParamBound, TraitBoundModifier};
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700318 #[cfg(feature = "full")]
319 use ConstExpr;
David Tolnayf945fb52017-02-27 12:53:54 -0800320 #[cfg(feature = "full")]
David Tolnay3cb23a92016-10-07 23:02:21 -0700321 use constant::parsing::const_expr;
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700322 #[cfg(feature = "full")]
323 use expr::parsing::expr;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700324 use generics::parsing::{lifetime, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700325 use ident::parsing::ident;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700326 use lit::parsing::string;
David Tolnay0047c712016-12-21 21:59:25 -0500327 use mac::parsing::mac;
Michael Layzell416724e2017-05-24 21:12:34 -0400328 #[cfg(feature = "full")]
329 use synom::{IResult, TokenTree};
David Tolnayda4049b2016-09-04 10:59:23 -0700330
David Tolnayb5a7b142016-09-13 22:46:39 -0700331 named!(pub ty -> Ty, alt!(
David Tolnayd040d772016-10-25 21:33:51 -0700332 ty_paren // must be before ty_tup
333 |
David Tolnay0047c712016-12-21 21:59:25 -0500334 ty_mac // must be before ty_path
335 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700336 ty_path // must be before ty_poly_trait_ref
337 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700338 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700339 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700340 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700341 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700342 ty_ptr
343 |
344 ty_rptr
345 |
346 ty_bare_fn
347 |
348 ty_never
349 |
350 ty_tup
351 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700352 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700353 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700354 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700355 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700356
David Tolnay0047c712016-12-21 21:59:25 -0500357 named!(ty_mac -> Ty, map!(mac, Ty::Mac));
358
Michael Layzell416724e2017-05-24 21:12:34 -0400359 named!(ty_vec -> Ty, delim!(Bracket, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700360 elem: ty >>
361 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700362 (TySlice {
363 ty: Box::new(elem),
364 bracket_token: tokens::Bracket::default(),
365 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700366 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700367
Michael Layzell416724e2017-05-24 21:12:34 -0400368 named!(ty_array -> Ty, delim!(Bracket, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700369 elem: ty >>
370 punct!(";") >>
David Tolnay514f1292017-02-27 12:30:57 -0800371 len: array_len >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700372 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700373 (TyArray {
374 ty: Box::new(elem),
375 amt: len,
376 bracket_token: tokens::Bracket::default(),
377 semi_token: tokens::Semi::default(),
378 }.into())
David Tolnayfa94b6f2016-10-05 23:26:11 -0700379 ));
380
David Tolnay514f1292017-02-27 12:30:57 -0800381 #[cfg(not(feature = "full"))]
David Tolnayf945fb52017-02-27 12:53:54 -0800382 use constant::parsing::const_expr as array_len;
David Tolnay514f1292017-02-27 12:30:57 -0800383
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700384 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800385 named!(array_len -> ConstExpr, alt!(
Michael Layzell416724e2017-05-24 21:12:34 -0400386 terminated!(const_expr, input_end!())
David Tolnay514f1292017-02-27 12:30:57 -0800387 |
Michael Layzell416724e2017-05-24 21:12:34 -0400388 terminated!(expr, input_end!()) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700389 ));
390
David Tolnayb5a7b142016-09-13 22:46:39 -0700391 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700392 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700393 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700394 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700395 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700396 keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) }
David Tolnay9d8f1972016-09-04 11:58:48 -0700397 ) >>
398 target: ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 (TyPtr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700400 const_token: match mutability {
401 Mutability::Mutable(_) => None,
402 Mutability::Immutable => Some(tokens::Const::default()),
403 },
404 star_token: tokens::Star::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 ty: Box::new(MutTy {
406 ty: target,
407 mutability: mutability,
408 }),
409 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700410 ));
411
David Tolnayb5a7b142016-09-13 22:46:39 -0700412 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700413 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700414 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700415 mutability: mutability >>
416 target: ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 (TyRptr {
418 lifetime: life,
419 ty: Box::new(MutTy {
420 ty: target,
421 mutability: mutability,
422 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700423 and_token: tokens::And::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700425 ));
426
David Tolnayb5a7b142016-09-13 22:46:39 -0700427 named!(ty_bare_fn -> Ty, do_parse!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700428 lifetimes: bound_lifetimes >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700429 unsafety: unsafety >>
430 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700431 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700432 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700433 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
434 fn_arg) >>
435 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
436 punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700437 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700438 output: fn_ret_ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 (TyBareFn {
440 ty: Box::new(BareFnTy {
441 unsafety: unsafety,
442 abi: abi,
443 lifetimes: lifetimes,
444 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700445 output: output,
446 variadic: variadic.map(|_| tokens::Dot3::default()),
447 fn_token: tokens::Fn::default(),
448 paren_token: tokens::Paren::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700449 }),
450 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700451 ));
452
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700453 named!(ty_never -> Ty, map!(punct!("!"), |_| TyNever {
454 bang_token: tokens::Bang::default(),
455 }.into()));
David Tolnay9d8f1972016-09-04 11:58:48 -0700456
David Tolnayb5a7b142016-09-13 22:46:39 -0700457 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700458 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700459 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
460 ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700461 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700462 (TyTup {
463 tys: elems,
464 paren_token: tokens::Paren::default(),
465 lone_comma: None, // TODO: does this just not parse?
466 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700467 ));
468
David Tolnay6414da72016-10-08 00:55:17 -0700469 named!(ty_path -> Ty, do_parse!(
470 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700471 parenthesized: cond!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700472 qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(),
David Tolnayf6c74402016-10-08 02:31:26 -0700473 option!(parenthesized_parameter_data)
474 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700475 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
476 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700477 let (qself, mut path) = qpath;
478 if let Some(Some(parenthesized)) = parenthesized {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700479 let len = path.segments.len();
480 path.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700481 }
David Tolnay6414da72016-10-08 00:55:17 -0700482 if bounds.is_empty() {
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 TyPath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700484 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800485 let path = TyParamBound::Trait(
486 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700487 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800488 trait_ref: path,
489 },
490 TraitBoundModifier::None,
491 );
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700492 let mut new_bounds = Delimited::new();
493 new_bounds.push_first(path);
494 for bound in bounds {
495 new_bounds.push_default(bound);
496 }
497 TyTraitObject { bounds: new_bounds }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700498 }
499 })
500 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700501
David Tolnayf6c74402016-10-08 02:31:26 -0700502 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
503 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700504 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
505 ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700506 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 output: fn_ret_ty >>
David Tolnayf6c74402016-10-08 02:31:26 -0700508 (PathParameters::Parenthesized(
509 ParenthesizedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510 paren_token: tokens::Paren::default(),
David Tolnayf6c74402016-10-08 02:31:26 -0700511 inputs: inputs,
512 output: output,
513 },
514 ))
515 ));
516
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700517 named!(pub fn_ret_ty -> FunctionRetTy, map!(
518 option!(preceded!(
519 punct!("->"),
520 ty
521 )),
522 |ty| {
523 match ty {
524 Some(ty) => FunctionRetTy::Ty(ty, tokens::RArrow::default()),
525 None => FunctionRetTy::Default,
526 }
527 }
528 ));
529
David Tolnay9636c052016-10-02 17:11:17 -0700530 named!(pub qpath -> (Option<QSelf>, Path), alt!(
531 map!(path, |p| (None, p))
532 |
533 do_parse!(
534 punct!("<") >>
535 this: map!(ty, Box::new) >>
536 path: option!(preceded!(
537 keyword!("as"),
538 path
539 )) >>
540 punct!(">") >>
541 punct!("::") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 rest: separated_nonempty_list!(
543 map!(punct!("::"), |_| tokens::Colon2::default()),
544 path_segment
545 ) >>
David Tolnay9636c052016-10-02 17:11:17 -0700546 ({
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700547 let as_token = path.as_ref().map(|_| tokens::As::default());
548 let (pos, path) = match path {
David Tolnay9636c052016-10-02 17:11:17 -0700549 Some(mut path) => {
550 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700551 if !path.segments.is_empty() && !path.segments.trailing_delim() {
552 path.segments.push_trailing(tokens::Colon2::default());
553 }
554 for item in rest.into_iter() {
555 path.segments.push(item);
556 }
557 (pos, path)
David Tolnay9636c052016-10-02 17:11:17 -0700558 }
559 None => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700560 (0, Path {
561 leading_colon: None,
David Tolnay9636c052016-10-02 17:11:17 -0700562 global: false,
563 segments: rest,
564 })
565 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700566 };
567 (Some(QSelf {
568 ty: this,
569 position: pos,
570 gt_token: tokens::Gt::default(),
571 lt_token: tokens::Lt::default(),
572 as_token: as_token,
573 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700574 })
575 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700576 |
577 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700578 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700579
David Tolnay4f0f2512016-10-30 09:28:14 -0700580 named!(ty_poly_trait_ref -> Ty, map!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
582 ty_param_bound),
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 |x| TyTraitObject { bounds: x }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700584 ));
585
David Tolnayb5a7b142016-09-13 22:46:39 -0700586 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700587 keyword!("impl") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700588 elem: separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
589 ty_param_bound) >>
590 (TyImplTrait {
591 impl_token: tokens::Impl::default(),
592 bounds: elem,
593 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700594 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700595
David Tolnayb5a7b142016-09-13 22:46:39 -0700596 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700597 punct!("(") >>
598 elem: ty >>
599 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700600 (TyParen {
601 paren_token: tokens::Paren::default(),
602 ty: Box::new(elem),
603 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700604 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700605
David Tolnay47a877c2016-10-01 16:50:55 -0700606 named!(pub mutability -> Mutability, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700607 keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) }
David Tolnayf6ccb832016-09-04 15:00:56 -0700608 |
609 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700610 ));
611
David Tolnayb5a7b142016-09-13 22:46:39 -0700612 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700613 global: option!(punct!("::")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700614 segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()),
615 path_segment) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700616 (Path {
617 global: global.is_some(),
618 segments: segments,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 leading_colon: global.map(|_| tokens::Colon2::default()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700620 })
621 ));
622
David Tolnay9636c052016-10-02 17:11:17 -0700623 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700624 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700625 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700626 punct!("<") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700627 lifetimes: terminated_list!(
628 map!(punct!(","), |_| tokens::Comma::default()),
629 lifetime
630 ) >>
631 types: cond!(
632 lifetimes.is_empty() || lifetimes.trailing_delim(),
633 terminated_list!(
634 map!(punct!(","), |_| tokens::Comma::default()),
David Tolnay1f16b602017-02-07 20:06:55 -0500635 terminated!(ty, not!(punct!("=")))
David Tolnay9d8f1972016-09-04 11:58:48 -0700636 )
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700637 ) >>
638 bindings: cond!(
639 match types {
640 Some(ref t) => t.is_empty() || t.trailing_delim(),
641 None => lifetimes.is_empty() || lifetimes.trailing_delim(),
642 },
643 terminated_list!(
644 map!(punct!(","), |_| tokens::Comma::default()),
645 type_binding
646 )
647 ) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700648 punct!(">") >>
649 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700650 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700651 parameters: PathParameters::AngleBracketed(
652 AngleBracketedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700653 gt_token: Some(tokens::Gt::default()),
654 lt_token: Some(tokens::Lt::default()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700655 lifetimes: lifetimes,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700656 types: types.unwrap_or_default(),
657 bindings: bindings.unwrap_or_default(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700658 }
659 ),
660 })
661 )
662 |
David Tolnay84aa0752016-10-02 23:01:13 -0700663 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700664 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700665 map!(alt!(
666 keyword!("super")
667 |
668 keyword!("self")
669 |
670 keyword!("Self")
671 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700672 ));
673
Arnavionf2dada12017-04-20 23:55:20 -0700674 named!(pub mod_style_path -> Path, do_parse!(
675 global: option!(punct!("::")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700676 segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()),
677 mod_style_path_segment) >>
Arnavionf2dada12017-04-20 23:55:20 -0700678 (Path {
679 global: global.is_some(),
680 segments: segments,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700681 leading_colon: global.map(|_| tokens::Colon2::default()),
Arnavionf2dada12017-04-20 23:55:20 -0700682 })
683 ));
684
685 named!(mod_style_path_segment -> PathSegment, alt!(
686 map!(ident, Into::into)
687 |
688 map!(alt!(
689 keyword!("super")
690 |
691 keyword!("self")
692 |
693 keyword!("Self")
694 ), Into::into)
695 ));
696
David Tolnayb5a7b142016-09-13 22:46:39 -0700697 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700698 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700699 punct!("=") >>
700 ty: ty >>
701 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700702 ident: id,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700703 eq_token: tokens::Eq::default(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700704 ty: ty,
705 })
706 ));
707
David Tolnayb5a7b142016-09-13 22:46:39 -0700708 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700709 bound_lifetimes: bound_lifetimes >>
710 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700711 parenthesized: option!(cond_reduce!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700712 trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700713 parenthesized_parameter_data
714 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700715 ({
716 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700717 if let Some(parenthesized) = parenthesized {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700718 let len = trait_ref.segments.len();
719 trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700720 }
721 PolyTraitRef {
722 bound_lifetimes: bound_lifetimes,
723 trait_ref: trait_ref,
724 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700725 })
726 ));
727
David Tolnay62f374c2016-10-02 13:37:00 -0700728 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700729 name: option!(do_parse!(
730 name: ident >>
Michael Layzell416724e2017-05-24 21:12:34 -0400731 not!(punct!("::")) >>
David Tolnayb0417d72016-10-25 21:46:35 -0700732 punct!(":") >>
David Tolnayb0417d72016-10-25 21:46:35 -0700733 (name)
734 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700735 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700736 (BareFnArg {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700737 name: name.map(|t| (t, tokens::Colon::default())),
David Tolnay9d8f1972016-09-04 11:58:48 -0700738 ty: ty,
739 })
740 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700741
742 named!(pub unsafety -> Unsafety, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700743 keyword!("unsafe") => { |_| Unsafety::Unsafe(tokens::Unsafe::default()) }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700744 |
745 epsilon!() => { |_| Unsafety::Normal }
746 ));
747
748 named!(pub abi -> Abi, do_parse!(
749 keyword!("extern") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700750 name: option!(string) >>
751 (Abi {
752 extern_token: tokens::Extern::default(),
753 kind: match name {
754 Some(name) => AbiKind::Named(name),
755 None => AbiKind::Default,
756 },
David Tolnayb8d8ef52016-10-29 14:30:08 -0700757 })
758 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700759}
David Tolnay87d0b442016-09-04 11:52:12 -0700760
761#[cfg(feature = "printing")]
762mod printing {
763 use super::*;
764 use quote::{Tokens, ToTokens};
765
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 impl ToTokens for TySlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700767 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700768 self.bracket_token.surround(tokens, |tokens| {
769 self.ty.to_tokens(tokens);
770 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700771 }
772 }
773
774 impl ToTokens for TyArray {
775 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700776 self.bracket_token.surround(tokens, |tokens| {
777 self.ty.to_tokens(tokens);
778 self.semi_token.to_tokens(tokens);
779 self.amt.to_tokens(tokens);
780 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700781 }
782 }
783
784 impl ToTokens for TyPtr {
785 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700786 self.star_token.to_tokens(tokens);
787 self.const_token.to_tokens(tokens);
788 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700789 self.ty.ty.to_tokens(tokens);
790 }
791 }
792
793 impl ToTokens for TyRptr {
794 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700796 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700797 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700798 self.ty.ty.to_tokens(tokens);
799 }
800 }
801
802 impl ToTokens for TyBareFn {
803 fn to_tokens(&self, tokens: &mut Tokens) {
804 self.ty.to_tokens(tokens)
805 }
806 }
807
808 impl ToTokens for TyNever {
809 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700810 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700811 }
812 }
813
814 impl ToTokens for TyTup {
815 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700816 self.paren_token.surround(tokens, |tokens| {
817 self.tys.to_tokens(tokens);
818 self.lone_comma.to_tokens(tokens);
819 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700820 }
821 }
822
823 impl ToTokens for TyPath {
824 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700825 PathTokens(&self.qself, &self.path).to_tokens(tokens);
826 }
827 }
828
829 impl<'a> ToTokens for PathTokens<'a> {
830 fn to_tokens(&self, tokens: &mut Tokens) {
831 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700832 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700833 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700834 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700835 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700836 qself.ty.to_tokens(tokens);
837 if qself.position > 0 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700838 qself.as_token.to_tokens(tokens);
839 self.1.leading_colon.to_tokens(tokens);
840 for (i, segment) in self.1.segments
Alex Crichton62a0a592017-05-22 13:58:53 -0700841 .iter()
842 .take(qself.position)
843 .enumerate() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700844 if i == qself.position - 1 {
845 segment.item().to_tokens(tokens);
846 qself.gt_token.to_tokens(tokens);
847 segment.delimiter().to_tokens(tokens);
848 } else {
849 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700850 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700851 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700852 } else {
853 qself.gt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700854 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 for segment in self.1.segments.iter().skip(qself.position) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700856 segment.to_tokens(tokens);
857 }
858 }
859 }
860
861 impl ToTokens for TyTraitObject {
862 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700863 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700864 }
865 }
866
867 impl ToTokens for TyImplTrait {
868 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700869 self.impl_token.to_tokens(tokens);
870 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700871 }
872 }
873
874 impl ToTokens for TyParen {
875 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700876 self.paren_token.surround(tokens, |tokens| {
877 self.ty.to_tokens(tokens);
878 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700879 }
880 }
881
882 impl ToTokens for TyInfer {
883 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700884 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700885 }
886 }
887
David Tolnay47a877c2016-10-01 16:50:55 -0700888 impl ToTokens for Mutability {
889 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 if let Mutability::Mutable(ref t) = *self {
891 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700892 }
893 }
894 }
895
David Tolnay87d0b442016-09-04 11:52:12 -0700896 impl ToTokens for Path {
897 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700898 self.leading_colon.to_tokens(tokens);
899 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700900 }
901 }
902
903 impl ToTokens for PathSegment {
904 fn to_tokens(&self, tokens: &mut Tokens) {
905 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700906 self.parameters.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700907 }
908 }
909
910 impl ToTokens for PathParameters {
911 fn to_tokens(&self, tokens: &mut Tokens) {
912 match *self {
913 PathParameters::AngleBracketed(ref parameters) => {
914 parameters.to_tokens(tokens);
915 }
916 PathParameters::Parenthesized(ref parameters) => {
917 parameters.to_tokens(tokens);
918 }
919 }
920 }
921 }
922
923 impl ToTokens for AngleBracketedParameterData {
924 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700925 self.lt_token.to_tokens(tokens);
926 self.lifetimes.to_tokens(tokens);
927 self.types.to_tokens(tokens);
928 self.bindings.to_tokens(tokens);
929 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700930 }
931 }
932
933 impl ToTokens for TypeBinding {
934 fn to_tokens(&self, tokens: &mut Tokens) {
935 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700936 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700937 self.ty.to_tokens(tokens);
938 }
939 }
940
941 impl ToTokens for ParenthesizedParameterData {
942 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700943 self.paren_token.surround(tokens, |tokens| {
944 self.inputs.to_tokens(tokens);
945 });
946 self.output.to_tokens(tokens);
947 }
948 }
949
950 impl ToTokens for FunctionRetTy {
951 fn to_tokens(&self, tokens: &mut Tokens) {
952 match *self {
953 FunctionRetTy::Default => {}
954 FunctionRetTy::Ty(ref ty, ref arrow) => {
955 arrow.to_tokens(tokens);
956 ty.to_tokens(tokens);
957 }
David Tolnay87d0b442016-09-04 11:52:12 -0700958 }
959 }
960 }
961
962 impl ToTokens for PolyTraitRef {
963 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700965 self.trait_ref.to_tokens(tokens);
966 }
967 }
968
969 impl ToTokens for BareFnTy {
970 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700971 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700972 self.unsafety.to_tokens(tokens);
973 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700974 self.fn_token.to_tokens(tokens);
975 self.paren_token.surround(tokens, |tokens| {
976 self.inputs.to_tokens(tokens);
977 self.variadic.to_tokens(tokens);
978 });
979 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700980 }
981 }
982
David Tolnay62f374c2016-10-02 13:37:00 -0700983 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700984 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700985 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700986 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700987 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700988 }
989 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700990 }
991 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700992
993 impl ToTokens for Unsafety {
994 fn to_tokens(&self, tokens: &mut Tokens) {
995 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700996 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -0700997 Unsafety::Normal => {
998 // nothing
999 }
1000 }
1001 }
1002 }
1003
1004 impl ToTokens for Abi {
1005 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001006 self.extern_token.to_tokens(tokens);
1007 match self.kind {
1008 AbiKind::Named(ref named) => named.to_tokens(tokens),
1009 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001010 }
1011 }
1012 }
David Tolnay87d0b442016-09-04 11:52:12 -07001013}