blob: 45fb6374db9f55e6500b933d27805eb9a7cf4409 [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;
David Tolnay9d8f1972016-09-04 11:58:48 -0700328 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700329
David Tolnayb5a7b142016-09-13 22:46:39 -0700330 named!(pub ty -> Ty, alt!(
David Tolnayd040d772016-10-25 21:33:51 -0700331 ty_paren // must be before ty_tup
332 |
David Tolnay0047c712016-12-21 21:59:25 -0500333 ty_mac // must be before ty_path
334 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700335 ty_path // must be before ty_poly_trait_ref
336 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700337 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700338 |
David Tolnayfa94b6f2016-10-05 23:26:11 -0700339 ty_array
David Tolnayb79ee962016-09-04 09:39:20 -0700340 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700341 ty_ptr
342 |
343 ty_rptr
344 |
345 ty_bare_fn
346 |
347 ty_never
348 |
349 ty_tup
350 |
David Tolnay4f0f2512016-10-30 09:28:14 -0700351 ty_poly_trait_ref
David Tolnay9d8f1972016-09-04 11:58:48 -0700352 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700353 ty_impl_trait
David Tolnay9d8f1972016-09-04 11:58:48 -0700354 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700355
David Tolnay0047c712016-12-21 21:59:25 -0500356 named!(ty_mac -> Ty, map!(mac, Ty::Mac));
357
David Tolnayb5a7b142016-09-13 22:46:39 -0700358 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700359 punct!("[") >>
360 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
David Tolnayfa94b6f2016-10-05 23:26:11 -0700368 named!(ty_array -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700369 punct!("[") >>
370 elem: ty >>
371 punct!(";") >>
David Tolnay514f1292017-02-27 12:30:57 -0800372 len: array_len >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700373 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700374 (TyArray {
375 ty: Box::new(elem),
376 amt: len,
377 bracket_token: tokens::Bracket::default(),
378 semi_token: tokens::Semi::default(),
379 }.into())
David Tolnayfa94b6f2016-10-05 23:26:11 -0700380 ));
381
David Tolnay514f1292017-02-27 12:30:57 -0800382 #[cfg(not(feature = "full"))]
David Tolnayf945fb52017-02-27 12:53:54 -0800383 use constant::parsing::const_expr as array_len;
David Tolnay514f1292017-02-27 12:30:57 -0800384
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700385 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800386 named!(array_len -> ConstExpr, alt!(
387 terminated!(const_expr, after_array_len)
388 |
389 terminated!(expr, after_array_len) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700390 ));
391
David Tolnay514f1292017-02-27 12:30:57 -0800392 #[cfg(feature = "full")]
393 named!(after_array_len -> &str, peek!(punct!("]")));
394
David Tolnayb5a7b142016-09-13 22:46:39 -0700395 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700396 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700397 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700398 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700399 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700400 keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) }
David Tolnay9d8f1972016-09-04 11:58:48 -0700401 ) >>
402 target: ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 (TyPtr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700404 const_token: match mutability {
405 Mutability::Mutable(_) => None,
406 Mutability::Immutable => Some(tokens::Const::default()),
407 },
408 star_token: tokens::Star::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700409 ty: Box::new(MutTy {
410 ty: target,
411 mutability: mutability,
412 }),
413 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700414 ));
415
David Tolnayb5a7b142016-09-13 22:46:39 -0700416 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700417 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700418 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700419 mutability: mutability >>
420 target: ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 (TyRptr {
422 lifetime: life,
423 ty: Box::new(MutTy {
424 ty: target,
425 mutability: mutability,
426 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700427 and_token: tokens::And::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700428 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700429 ));
430
David Tolnayb5a7b142016-09-13 22:46:39 -0700431 named!(ty_bare_fn -> Ty, do_parse!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700432 lifetimes: bound_lifetimes >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700433 unsafety: unsafety >>
434 abi: option!(abi) >>
David Tolnay4f121832016-10-25 21:33:36 -0700435 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700436 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700437 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
438 fn_arg) >>
439 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
440 punct!("..."))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700441 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700442 output: fn_ret_ty >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 (TyBareFn {
444 ty: Box::new(BareFnTy {
445 unsafety: unsafety,
446 abi: abi,
447 lifetimes: lifetimes,
448 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700449 output: output,
450 variadic: variadic.map(|_| tokens::Dot3::default()),
451 fn_token: tokens::Fn::default(),
452 paren_token: tokens::Paren::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700453 }),
454 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700455 ));
456
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700457 named!(ty_never -> Ty, map!(punct!("!"), |_| TyNever {
458 bang_token: tokens::Bang::default(),
459 }.into()));
David Tolnay9d8f1972016-09-04 11:58:48 -0700460
David Tolnayb5a7b142016-09-13 22:46:39 -0700461 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700462 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700463 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
464 ty) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700465 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700466 (TyTup {
467 tys: elems,
468 paren_token: tokens::Paren::default(),
469 lone_comma: None, // TODO: does this just not parse?
470 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700471 ));
472
David Tolnay6414da72016-10-08 00:55:17 -0700473 named!(ty_path -> Ty, do_parse!(
474 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700475 parenthesized: cond!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700476 qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(),
David Tolnayf6c74402016-10-08 02:31:26 -0700477 option!(parenthesized_parameter_data)
478 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700479 bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
480 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700481 let (qself, mut path) = qpath;
482 if let Some(Some(parenthesized)) = parenthesized {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700483 let len = path.segments.len();
484 path.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700485 }
David Tolnay6414da72016-10-08 00:55:17 -0700486 if bounds.is_empty() {
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 TyPath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700488 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800489 let path = TyParamBound::Trait(
490 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700491 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800492 trait_ref: path,
493 },
494 TraitBoundModifier::None,
495 );
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700496 let mut new_bounds = Delimited::new();
497 new_bounds.push_first(path);
498 for bound in bounds {
499 new_bounds.push_default(bound);
500 }
501 TyTraitObject { bounds: new_bounds }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700502 }
503 })
504 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700505
David Tolnayf6c74402016-10-08 02:31:26 -0700506 named!(parenthesized_parameter_data -> PathParameters, do_parse!(
507 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700508 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
509 ty) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700510 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700511 output: fn_ret_ty >>
David Tolnayf6c74402016-10-08 02:31:26 -0700512 (PathParameters::Parenthesized(
513 ParenthesizedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700514 paren_token: tokens::Paren::default(),
David Tolnayf6c74402016-10-08 02:31:26 -0700515 inputs: inputs,
516 output: output,
517 },
518 ))
519 ));
520
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700521 named!(pub fn_ret_ty -> FunctionRetTy, map!(
522 option!(preceded!(
523 punct!("->"),
524 ty
525 )),
526 |ty| {
527 match ty {
528 Some(ty) => FunctionRetTy::Ty(ty, tokens::RArrow::default()),
529 None => FunctionRetTy::Default,
530 }
531 }
532 ));
533
David Tolnay9636c052016-10-02 17:11:17 -0700534 named!(pub qpath -> (Option<QSelf>, Path), alt!(
535 map!(path, |p| (None, p))
536 |
537 do_parse!(
538 punct!("<") >>
539 this: map!(ty, Box::new) >>
540 path: option!(preceded!(
541 keyword!("as"),
542 path
543 )) >>
544 punct!(">") >>
545 punct!("::") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546 rest: separated_nonempty_list!(
547 map!(punct!("::"), |_| tokens::Colon2::default()),
548 path_segment
549 ) >>
David Tolnay9636c052016-10-02 17:11:17 -0700550 ({
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700551 let as_token = path.as_ref().map(|_| tokens::As::default());
552 let (pos, path) = match path {
David Tolnay9636c052016-10-02 17:11:17 -0700553 Some(mut path) => {
554 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 if !path.segments.is_empty() && !path.segments.trailing_delim() {
556 path.segments.push_trailing(tokens::Colon2::default());
557 }
558 for item in rest.into_iter() {
559 path.segments.push(item);
560 }
561 (pos, path)
David Tolnay9636c052016-10-02 17:11:17 -0700562 }
563 None => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700564 (0, Path {
565 leading_colon: None,
David Tolnay9636c052016-10-02 17:11:17 -0700566 global: false,
567 segments: rest,
568 })
569 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 };
571 (Some(QSelf {
572 ty: this,
573 position: pos,
574 gt_token: tokens::Gt::default(),
575 lt_token: tokens::Lt::default(),
576 as_token: as_token,
577 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700578 })
579 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700580 |
581 map!(keyword!("self"), |_| (None, "self".into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700582 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700583
David Tolnay4f0f2512016-10-30 09:28:14 -0700584 named!(ty_poly_trait_ref -> Ty, map!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700585 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
586 ty_param_bound),
Alex Crichton62a0a592017-05-22 13:58:53 -0700587 |x| TyTraitObject { bounds: x }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700588 ));
589
David Tolnayb5a7b142016-09-13 22:46:39 -0700590 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700591 keyword!("impl") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700592 elem: separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
593 ty_param_bound) >>
594 (TyImplTrait {
595 impl_token: tokens::Impl::default(),
596 bounds: elem,
597 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700598 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700599
David Tolnayb5a7b142016-09-13 22:46:39 -0700600 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700601 punct!("(") >>
602 elem: ty >>
603 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 (TyParen {
605 paren_token: tokens::Paren::default(),
606 ty: Box::new(elem),
607 }.into())
David Tolnay9d8f1972016-09-04 11:58:48 -0700608 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700609
David Tolnay47a877c2016-10-01 16:50:55 -0700610 named!(pub mutability -> Mutability, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700611 keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) }
David Tolnayf6ccb832016-09-04 15:00:56 -0700612 |
613 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700614 ));
615
David Tolnayb5a7b142016-09-13 22:46:39 -0700616 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700617 global: option!(punct!("::")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()),
619 path_segment) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700620 (Path {
621 global: global.is_some(),
622 segments: segments,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 leading_colon: global.map(|_| tokens::Colon2::default()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700624 })
625 ));
626
David Tolnay9636c052016-10-02 17:11:17 -0700627 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700628 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700629 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700630 punct!("<") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700631 lifetimes: terminated_list!(
632 map!(punct!(","), |_| tokens::Comma::default()),
633 lifetime
634 ) >>
635 types: cond!(
636 lifetimes.is_empty() || lifetimes.trailing_delim(),
637 terminated_list!(
638 map!(punct!(","), |_| tokens::Comma::default()),
David Tolnay1f16b602017-02-07 20:06:55 -0500639 terminated!(ty, not!(punct!("=")))
David Tolnay9d8f1972016-09-04 11:58:48 -0700640 )
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700641 ) >>
642 bindings: cond!(
643 match types {
644 Some(ref t) => t.is_empty() || t.trailing_delim(),
645 None => lifetimes.is_empty() || lifetimes.trailing_delim(),
646 },
647 terminated_list!(
648 map!(punct!(","), |_| tokens::Comma::default()),
649 type_binding
650 )
651 ) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700652 punct!(">") >>
653 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700654 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700655 parameters: PathParameters::AngleBracketed(
656 AngleBracketedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700657 gt_token: Some(tokens::Gt::default()),
658 lt_token: Some(tokens::Lt::default()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700659 lifetimes: lifetimes,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700660 types: types.unwrap_or_default(),
661 bindings: bindings.unwrap_or_default(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700662 }
663 ),
664 })
665 )
666 |
David Tolnay84aa0752016-10-02 23:01:13 -0700667 map!(ident, Into::into)
David Tolnay77807222016-10-24 22:30:15 -0700668 |
David Tolnaye14e3be2016-10-24 22:53:07 -0700669 map!(alt!(
670 keyword!("super")
671 |
672 keyword!("self")
673 |
674 keyword!("Self")
675 ), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -0700676 ));
677
Arnavionf2dada12017-04-20 23:55:20 -0700678 named!(pub mod_style_path -> Path, do_parse!(
679 global: option!(punct!("::")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700680 segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()),
681 mod_style_path_segment) >>
Arnavionf2dada12017-04-20 23:55:20 -0700682 (Path {
683 global: global.is_some(),
684 segments: segments,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700685 leading_colon: global.map(|_| tokens::Colon2::default()),
Arnavionf2dada12017-04-20 23:55:20 -0700686 })
687 ));
688
689 named!(mod_style_path_segment -> PathSegment, alt!(
690 map!(ident, Into::into)
691 |
692 map!(alt!(
693 keyword!("super")
694 |
695 keyword!("self")
696 |
697 keyword!("Self")
698 ), Into::into)
699 ));
700
David Tolnayb5a7b142016-09-13 22:46:39 -0700701 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700702 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700703 punct!("=") >>
704 ty: ty >>
705 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700706 ident: id,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700707 eq_token: tokens::Eq::default(),
David Tolnay9d8f1972016-09-04 11:58:48 -0700708 ty: ty,
709 })
710 ));
711
David Tolnayb5a7b142016-09-13 22:46:39 -0700712 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700713 bound_lifetimes: bound_lifetimes >>
714 trait_ref: path >>
David Tolnay4f0f2512016-10-30 09:28:14 -0700715 parenthesized: option!(cond_reduce!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700716 trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(),
David Tolnay4f0f2512016-10-30 09:28:14 -0700717 parenthesized_parameter_data
718 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700719 ({
720 let mut trait_ref = trait_ref;
David Tolnay4f0f2512016-10-30 09:28:14 -0700721 if let Some(parenthesized) = parenthesized {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700722 let len = trait_ref.segments.len();
723 trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700724 }
725 PolyTraitRef {
726 bound_lifetimes: bound_lifetimes,
727 trait_ref: trait_ref,
728 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700729 })
730 ));
731
David Tolnay62f374c2016-10-02 13:37:00 -0700732 named!(pub fn_arg -> BareFnArg, do_parse!(
David Tolnayb0417d72016-10-25 21:46:35 -0700733 name: option!(do_parse!(
734 name: ident >>
735 punct!(":") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500736 not!(tag!(":")) >> // not ::
David Tolnayb0417d72016-10-25 21:46:35 -0700737 (name)
738 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700739 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700740 (BareFnArg {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700741 name: name.map(|t| (t, tokens::Colon::default())),
David Tolnay9d8f1972016-09-04 11:58:48 -0700742 ty: ty,
743 })
744 ));
David Tolnayb8d8ef52016-10-29 14:30:08 -0700745
746 named!(pub unsafety -> Unsafety, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700747 keyword!("unsafe") => { |_| Unsafety::Unsafe(tokens::Unsafe::default()) }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700748 |
749 epsilon!() => { |_| Unsafety::Normal }
750 ));
751
752 named!(pub abi -> Abi, do_parse!(
753 keyword!("extern") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700754 name: option!(string) >>
755 (Abi {
756 extern_token: tokens::Extern::default(),
757 kind: match name {
758 Some(name) => AbiKind::Named(name),
759 None => AbiKind::Default,
760 },
David Tolnayb8d8ef52016-10-29 14:30:08 -0700761 })
762 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700763}
David Tolnay87d0b442016-09-04 11:52:12 -0700764
765#[cfg(feature = "printing")]
766mod printing {
767 use super::*;
768 use quote::{Tokens, ToTokens};
769
Alex Crichton62a0a592017-05-22 13:58:53 -0700770 impl ToTokens for TySlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700771 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700772 self.bracket_token.surround(tokens, |tokens| {
773 self.ty.to_tokens(tokens);
774 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700775 }
776 }
777
778 impl ToTokens for TyArray {
779 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700780 self.bracket_token.surround(tokens, |tokens| {
781 self.ty.to_tokens(tokens);
782 self.semi_token.to_tokens(tokens);
783 self.amt.to_tokens(tokens);
784 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700785 }
786 }
787
788 impl ToTokens for TyPtr {
789 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700790 self.star_token.to_tokens(tokens);
791 self.const_token.to_tokens(tokens);
792 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700793 self.ty.ty.to_tokens(tokens);
794 }
795 }
796
797 impl ToTokens for TyRptr {
798 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700799 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700800 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700801 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700802 self.ty.ty.to_tokens(tokens);
803 }
804 }
805
806 impl ToTokens for TyBareFn {
807 fn to_tokens(&self, tokens: &mut Tokens) {
808 self.ty.to_tokens(tokens)
809 }
810 }
811
812 impl ToTokens for TyNever {
813 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700814 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700815 }
816 }
817
818 impl ToTokens for TyTup {
819 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700820 self.paren_token.surround(tokens, |tokens| {
821 self.tys.to_tokens(tokens);
822 self.lone_comma.to_tokens(tokens);
823 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700824 }
825 }
826
827 impl ToTokens for TyPath {
828 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700829 PathTokens(&self.qself, &self.path).to_tokens(tokens);
830 }
831 }
832
833 impl<'a> ToTokens for PathTokens<'a> {
834 fn to_tokens(&self, tokens: &mut Tokens) {
835 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700836 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700837 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700838 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700839 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700840 qself.ty.to_tokens(tokens);
841 if qself.position > 0 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700842 qself.as_token.to_tokens(tokens);
843 self.1.leading_colon.to_tokens(tokens);
844 for (i, segment) in self.1.segments
Alex Crichton62a0a592017-05-22 13:58:53 -0700845 .iter()
846 .take(qself.position)
847 .enumerate() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700848 if i == qself.position - 1 {
849 segment.item().to_tokens(tokens);
850 qself.gt_token.to_tokens(tokens);
851 segment.delimiter().to_tokens(tokens);
852 } else {
853 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700854 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700855 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700856 } else {
857 qself.gt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700858 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700859 for segment in self.1.segments.iter().skip(qself.position) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700860 segment.to_tokens(tokens);
861 }
862 }
863 }
864
865 impl ToTokens for TyTraitObject {
866 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700867 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700868 }
869 }
870
871 impl ToTokens for TyImplTrait {
872 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 self.impl_token.to_tokens(tokens);
874 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700875 }
876 }
877
878 impl ToTokens for TyParen {
879 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 self.paren_token.surround(tokens, |tokens| {
881 self.ty.to_tokens(tokens);
882 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700883 }
884 }
885
886 impl ToTokens for TyInfer {
887 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700888 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700889 }
890 }
891
David Tolnay47a877c2016-10-01 16:50:55 -0700892 impl ToTokens for Mutability {
893 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700894 if let Mutability::Mutable(ref t) = *self {
895 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700896 }
897 }
898 }
899
David Tolnay87d0b442016-09-04 11:52:12 -0700900 impl ToTokens for Path {
901 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700902 self.leading_colon.to_tokens(tokens);
903 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700904 }
905 }
906
907 impl ToTokens for PathSegment {
908 fn to_tokens(&self, tokens: &mut Tokens) {
909 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700910 self.parameters.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700911 }
912 }
913
914 impl ToTokens for PathParameters {
915 fn to_tokens(&self, tokens: &mut Tokens) {
916 match *self {
917 PathParameters::AngleBracketed(ref parameters) => {
918 parameters.to_tokens(tokens);
919 }
920 PathParameters::Parenthesized(ref parameters) => {
921 parameters.to_tokens(tokens);
922 }
923 }
924 }
925 }
926
927 impl ToTokens for AngleBracketedParameterData {
928 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700929 self.lt_token.to_tokens(tokens);
930 self.lifetimes.to_tokens(tokens);
931 self.types.to_tokens(tokens);
932 self.bindings.to_tokens(tokens);
933 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700934 }
935 }
936
937 impl ToTokens for TypeBinding {
938 fn to_tokens(&self, tokens: &mut Tokens) {
939 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700940 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700941 self.ty.to_tokens(tokens);
942 }
943 }
944
945 impl ToTokens for ParenthesizedParameterData {
946 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700947 self.paren_token.surround(tokens, |tokens| {
948 self.inputs.to_tokens(tokens);
949 });
950 self.output.to_tokens(tokens);
951 }
952 }
953
954 impl ToTokens for FunctionRetTy {
955 fn to_tokens(&self, tokens: &mut Tokens) {
956 match *self {
957 FunctionRetTy::Default => {}
958 FunctionRetTy::Ty(ref ty, ref arrow) => {
959 arrow.to_tokens(tokens);
960 ty.to_tokens(tokens);
961 }
David Tolnay87d0b442016-09-04 11:52:12 -0700962 }
963 }
964 }
965
966 impl ToTokens for PolyTraitRef {
967 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700968 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700969 self.trait_ref.to_tokens(tokens);
970 }
971 }
972
973 impl ToTokens for BareFnTy {
974 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700975 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700976 self.unsafety.to_tokens(tokens);
977 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700978 self.fn_token.to_tokens(tokens);
979 self.paren_token.surround(tokens, |tokens| {
980 self.inputs.to_tokens(tokens);
981 self.variadic.to_tokens(tokens);
982 });
983 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700984 }
985 }
986
David Tolnay62f374c2016-10-02 13:37:00 -0700987 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700988 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700989 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700990 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700992 }
993 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700994 }
995 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700996
997 impl ToTokens for Unsafety {
998 fn to_tokens(&self, tokens: &mut Tokens) {
999 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001000 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001001 Unsafety::Normal => {
1002 // nothing
1003 }
1004 }
1005 }
1006 }
1007
1008 impl ToTokens for Abi {
1009 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001010 self.extern_token.to_tokens(tokens);
1011 match self.kind {
1012 AbiKind::Named(ref named) => named.to_tokens(tokens),
1013 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001014 }
1015 }
1016 }
David Tolnay87d0b442016-09-04 11:52:12 -07001017}