blob: 0ee4188af4afe9e74d8fd56d4f5e6ddddfe66082 [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 Crichton954046c2017-05-30 21:49:42 -0700260 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700261 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::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700317 // use {TyParamBound, TraitBoundModifier};
318 // #[cfg(feature = "full")]
319 // use ConstExpr;
320 // #[cfg(feature = "full")]
321 // use constant::parsing::const_expr;
322 // #[cfg(feature = "full")]
323 // use expr::parsing::expr;
324 // use generics::parsing::{lifetime, ty_param_bound, bound_lifetimes};
325 // use ident::parsing::ident;
326 // use lit::parsing::string;
327 // use mac::parsing::mac;
328 use proc_macro2::TokenTree;
329 use synom::{IResult, Synom};
330 use synom::tokens::*;
David Tolnayda4049b2016-09-04 10:59:23 -0700331
Alex Crichton954046c2017-05-30 21:49:42 -0700332 impl Synom for Ty {
333 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
334 alt! {
335 input,
336 // must be before mac
337 syn!(TyParen) => { Ty::Paren }
338 |
339 // must be before path
340 syn!(Mac) => { Ty::Mac }
341 |
342 // must be before ty_poly_trait_ref
343 ty_path
344 |
345 syn!(TySlice) => { Ty::Slice }
346 |
347 syn!(TyArray) => { Ty::Array }
348 |
349 syn!(TyPtr) => { Ty::Ptr }
350 |
351 syn!(TyRptr) => { Ty::Rptr }
352 |
353 syn!(TyBareFn) => { Ty::BareFn }
354 |
355 syn!(TyNever) => { Ty::Never }
356 |
357 syn!(TyTup) => { Ty::Tup }
358 |
359 ty_poly_trait_ref
360 |
361 syn!(TyImplTrait) => { Ty::ImplTrait }
362 }
363 }
David Tolnayb79ee962016-09-04 09:39:20 -0700364
Alex Crichton954046c2017-05-30 21:49:42 -0700365 fn description() -> Option<&'static str> {
366 Some("type")
367 }
368 }
David Tolnay0047c712016-12-21 21:59:25 -0500369
Alex Crichton954046c2017-05-30 21:49:42 -0700370 impl Synom for TySlice {
371 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
372 map! {
373 input,
374 brackets!(syn!(Ty)),
375 |(ty, b)| TySlice {
376 ty: Box::new(ty),
377 bracket_token: b,
378 }
379 }
380 }
381 }
David Tolnayb79ee962016-09-04 09:39:20 -0700382
Alex Crichton954046c2017-05-30 21:49:42 -0700383 impl Synom for TyArray {
384 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
385 map! {
386 input,
387 brackets!(do_parse!(
388 elem: syn!(Ty) >>
389 semi: syn!(Semi) >>
390 len: array_len >>
391 (elem, semi, len)
392 )),
393 |((elem, semi, len), brackets)| {
394 TyArray {
395 ty: Box::new(elem),
396 amt: len,
397 bracket_token: brackets,
398 semi_token: semi,
399 }
400 }
401 }
402 }
403 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700404
David Tolnay514f1292017-02-27 12:30:57 -0800405 #[cfg(not(feature = "full"))]
Alex Crichton954046c2017-05-30 21:49:42 -0700406 named!(array_len -> ConstExpr, syn!(ConstExpr));
David Tolnay514f1292017-02-27 12:30:57 -0800407
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700408 #[cfg(feature = "full")]
David Tolnay514f1292017-02-27 12:30:57 -0800409 named!(array_len -> ConstExpr, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700410 terminated!(syn!(ConstExpr), input_end!())
David Tolnay514f1292017-02-27 12:30:57 -0800411 |
Alex Crichton954046c2017-05-30 21:49:42 -0700412 terminated!(syn!(Expr), input_end!()) => { ConstExpr::Other }
David Tolnayfe2cc9a2016-10-30 12:47:36 -0700413 ));
414
Alex Crichton954046c2017-05-30 21:49:42 -0700415 impl Synom for TyPtr {
416 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
417 do_parse! {
418 input,
419 star: syn!(Star) >>
420 mutability: alt!(
421 syn!(Const) => { |c| (Mutability::Immutable, Some(c)) }
422 |
423 syn!(Mut) => { |m| (Mutability::Mutable(m), None) }
424 ) >>
425 target: syn!(Ty) >>
426 (TyPtr {
427 const_token: mutability.1,
428 star_token: star,
429 ty: Box::new(MutTy {
430 ty: target,
431 mutability: mutability.0,
432 }),
433 })
434 }
435 }
436 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700437
Alex Crichton954046c2017-05-30 21:49:42 -0700438 impl Synom for TyRptr {
439 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
440 do_parse! {
441 input,
442 amp: syn!(And) >>
443 life: option!(syn!(Lifetime)) >>
444 mutability: syn!(Mutability) >>
445 target: syn!(Ty) >>
446 (TyRptr {
447 lifetime: life,
448 ty: Box::new(MutTy {
449 ty: target,
450 mutability: mutability,
451 }),
452 and_token: amp,
453 })
454 }
455 }
456 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700457
Alex Crichton954046c2017-05-30 21:49:42 -0700458 impl Synom for TyBareFn {
459 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
460 do_parse! {
461 input,
462 lifetimes: option!(syn!(BoundLifetimes)) >>
463 unsafety: syn!(Unsafety) >>
464 abi: option!(syn!(Abi)) >>
465 fn_: syn!(Fn_) >>
466 parens: parens!(do_parse!(
467 inputs: call!(Delimited::parse_terminated) >>
468 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
469 syn!(Dot3))) >>
470 (inputs, variadic)
471 )) >>
472 output: syn!(FunctionRetTy) >>
473 (TyBareFn {
474 ty: Box::new(BareFnTy {
475 unsafety: unsafety,
476 abi: abi,
477 lifetimes: lifetimes,
478 output: output,
479 variadic: (parens.0).1,
480 fn_token: fn_,
481 paren_token: parens.1,
482 inputs: (parens.0).0,
483 }),
484 })
485 }
486 }
487 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700488
Alex Crichton954046c2017-05-30 21:49:42 -0700489 impl Synom for TyNever {
490 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
491 map! {
492 input,
493 syn!(Bang),
494 |b| TyNever { bang_token: b }
495 }
496 }
497 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700498
Alex Crichton954046c2017-05-30 21:49:42 -0700499 impl Synom for TyTup {
500 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
501 do_parse! {
502 input,
503 data: parens!(call!(Delimited::parse_terminated)) >>
504 (TyTup {
505 tys: data.0,
506 paren_token: data.1,
507 lone_comma: None, // TODO: does this just not parse?
508 })
509 }
510 }
511 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700512
David Tolnay6414da72016-10-08 00:55:17 -0700513 named!(ty_path -> Ty, do_parse!(
514 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700515 parenthesized: cond!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700516 qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(),
Alex Crichton954046c2017-05-30 21:49:42 -0700517 option!(syn!(ParenthesizedParameterData))
David Tolnayf6c74402016-10-08 02:31:26 -0700518 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700519 bounds: many0!(tuple!(syn!(Add), syn!(TyParamBound))) >>
David Tolnay6414da72016-10-08 00:55:17 -0700520 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700521 let (qself, mut path) = qpath;
522 if let Some(Some(parenthesized)) = parenthesized {
Alex Crichton954046c2017-05-30 21:49:42 -0700523 let parenthesized = PathParameters::Parenthesized(parenthesized);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700524 let len = path.segments.len();
525 path.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700526 }
David Tolnay6414da72016-10-08 00:55:17 -0700527 if bounds.is_empty() {
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 TyPath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700529 } else {
David Tolnay02c907f2017-01-23 00:06:37 -0800530 let path = TyParamBound::Trait(
531 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700532 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800533 trait_ref: path,
534 },
535 TraitBoundModifier::None,
536 );
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700537 let mut new_bounds = Delimited::new();
538 new_bounds.push_first(path);
Alex Crichton954046c2017-05-30 21:49:42 -0700539 for (_tok, bound) in bounds {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700540 new_bounds.push_default(bound);
541 }
542 TyTraitObject { bounds: new_bounds }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700543 }
544 })
545 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700546
David Tolnay9636c052016-10-02 17:11:17 -0700547 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700548 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700549 |
550 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700551 lt: syn!(Lt) >>
552 this: map!(syn!(Ty), Box::new) >>
553 path: option!(do_parse!(
554 as_: syn!(As) >>
555 path: syn!(Path) >>
556 (as_, path)
David Tolnay9636c052016-10-02 17:11:17 -0700557 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700558 gt: syn!(Gt) >>
559 colon2: syn!(Colon2) >>
560 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700561 ({
Alex Crichton954046c2017-05-30 21:49:42 -0700562 let (pos, path, as_) = match path {
563 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700564 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700566 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700567 }
Alex Crichton954046c2017-05-30 21:49:42 -0700568 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 path.segments.push(item);
570 }
Alex Crichton954046c2017-05-30 21:49:42 -0700571 (pos, path, Some(as_))
David Tolnay9636c052016-10-02 17:11:17 -0700572 }
573 None => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700574 (0, Path {
575 leading_colon: None,
David Tolnay9636c052016-10-02 17:11:17 -0700576 global: false,
577 segments: rest,
Alex Crichton954046c2017-05-30 21:49:42 -0700578 }, None)
David Tolnay9636c052016-10-02 17:11:17 -0700579 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700580 };
581 (Some(QSelf {
582 ty: this,
583 position: pos,
Alex Crichton954046c2017-05-30 21:49:42 -0700584 gt_token: gt,
585 lt_token: lt,
586 as_token: as_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700588 })
589 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700590 |
Alex Crichton954046c2017-05-30 21:49:42 -0700591 map!(syn!(Self_), |s: Self_| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700592 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700593
Alex Crichton954046c2017-05-30 21:49:42 -0700594 impl Synom for ParenthesizedParameterData {
595 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
596 do_parse! {
597 input,
598 data: parens!(call!(Delimited::parse_terminated)) >>
599 output: syn!(FunctionRetTy) >>
600 (ParenthesizedParameterData {
601 paren_token: data.1,
602 inputs: data.0,
603 output: output,
604 })
605 }
606 }
607 }
608
609 impl Synom for FunctionRetTy {
610 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
611 alt! {
612 input,
613 do_parse!(
614 arrow: syn!(RArrow) >>
615 ty: syn!(Ty) >>
616 (FunctionRetTy::Ty(ty, arrow))
617 )
618 |
619 epsilon!() => { |_| FunctionRetTy::Default }
620 }
621 }
622 }
623
David Tolnay4f0f2512016-10-30 09:28:14 -0700624 named!(ty_poly_trait_ref -> Ty, map!(
Alex Crichton954046c2017-05-30 21:49:42 -0700625 call!(Delimited::parse_separated_nonempty),
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 |x| TyTraitObject { bounds: x }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700627 ));
628
Alex Crichton954046c2017-05-30 21:49:42 -0700629 impl Synom for TyImplTrait {
630 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
631 do_parse! {
632 input,
633 impl_: syn!(Impl) >>
634 elem: call!(Delimited::parse_separated_nonempty) >>
635 (TyImplTrait {
636 impl_token: impl_,
637 bounds: elem,
638 })
639 }
640 }
641 }
David Tolnayb79ee962016-09-04 09:39:20 -0700642
Alex Crichton954046c2017-05-30 21:49:42 -0700643 impl Synom for TyParen {
644 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
645 do_parse! {
646 input,
647 data: parens!(syn!(Ty)) >>
648 (TyParen {
649 paren_token: data.1,
650 ty: Box::new(data.0),
651 })
652 }
653 }
654 }
David Tolnayb79ee962016-09-04 09:39:20 -0700655
Alex Crichton954046c2017-05-30 21:49:42 -0700656 impl Synom for Mutability {
657 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
658 alt! {
659 input,
660 syn!(Mut) => { Mutability::Mutable }
661 |
662 epsilon!() => { |_| Mutability::Immutable }
663 }
664 }
665 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700666
Alex Crichton954046c2017-05-30 21:49:42 -0700667 impl Synom for Path {
668 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
669 do_parse! {
670 input,
671 global: option!(syn!(Colon2)) >>
672 segments: call!(Delimited::parse_separated_nonempty) >>
673 (Path {
674 global: global.is_some(),
675 segments: segments,
676 leading_colon: global,
677 })
678 }
679 }
680 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700681
Alex Crichton954046c2017-05-30 21:49:42 -0700682 impl Synom for PathSegment {
683 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
684 alt! {
685 input,
686 do_parse!(
687 id: option!(syn!(Ident)) >>
688 lt: syn!(Lt) >>
689 lifetimes: call!(Delimited::parse_terminated) >>
690 types: cond!(
691 lifetimes.is_empty() || lifetimes.trailing_delim(),
692 call!(Delimited::parse_terminated_with,
693 ty_no_eq_after)
694 ) >>
695 bindings: cond!(
696 match types {
697 Some(ref t) => t.is_empty() || t.trailing_delim(),
698 None => lifetimes.is_empty() || lifetimes.trailing_delim(),
699 },
700 call!(Delimited::parse_terminated)
701 ) >>
702 gt: syn!(Gt) >>
703 (PathSegment {
704 ident: id.unwrap_or_else(|| "".into()),
705 parameters: PathParameters::AngleBracketed(
706 AngleBracketedParameterData {
707 gt_token: Some(gt),
708 lt_token: Some(lt),
709 lifetimes: lifetimes,
710 types: types.unwrap_or_default(),
711 bindings: bindings.unwrap_or_default(),
712 }
713 ),
714 })
David Tolnay9d8f1972016-09-04 11:58:48 -0700715 )
Alex Crichton954046c2017-05-30 21:49:42 -0700716 |
717 mod_style_path_segment
718 }
719 }
720 }
721 named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700722
Alex Crichton954046c2017-05-30 21:49:42 -0700723 impl Path {
724 pub fn parse_mod_style(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
725 do_parse! {
726 input,
727 global: option!(syn!(Colon2)) >>
728 segments: call!(Delimited::parse_separated_nonempty_with,
729 mod_style_path_segment) >>
730 (Path {
731 global: global.is_some(),
732 segments: segments,
733 leading_colon: global,
734 })
735 }
736 }
737 }
Arnavionf2dada12017-04-20 23:55:20 -0700738
739 named!(mod_style_path_segment -> PathSegment, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700740 map!(syn!(Ident), Into::into)
Arnavionf2dada12017-04-20 23:55:20 -0700741 |
Alex Crichton954046c2017-05-30 21:49:42 -0700742 alt!(
743 syn!(Super) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700744 |
Alex Crichton954046c2017-05-30 21:49:42 -0700745 syn!(Self_) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700746 |
Alex Crichton954046c2017-05-30 21:49:42 -0700747 syn!(CapSelf) => { Into::into }
748 )
Arnavionf2dada12017-04-20 23:55:20 -0700749 ));
750
Alex Crichton954046c2017-05-30 21:49:42 -0700751 impl Synom for TypeBinding {
752 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
753 do_parse! {
754 input,
755 id: syn!(Ident) >>
756 eq: syn!(Eq) >>
757 ty: syn!(Ty) >>
758 (TypeBinding {
759 ident: id,
760 eq_token: eq,
761 ty: ty,
762 })
David Tolnayf6c74402016-10-08 02:31:26 -0700763 }
Alex Crichton954046c2017-05-30 21:49:42 -0700764 }
765 }
766
767 impl Synom for PolyTraitRef {
768 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
769 do_parse! {
770 input,
771 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
772 trait_ref: syn!(Path) >>
773 parenthesized: option!(cond_reduce!(
774 trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(),
775 syn!(ParenthesizedParameterData)
776 )) >>
777 ({
778 let mut trait_ref = trait_ref;
779 if let Some(parenthesized) = parenthesized {
780 let parenthesized = PathParameters::Parenthesized(parenthesized);
781 let len = trait_ref.segments.len();
782 trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
783 }
784 PolyTraitRef {
785 bound_lifetimes: bound_lifetimes,
786 trait_ref: trait_ref,
787 }
788 })
David Tolnayf6c74402016-10-08 02:31:26 -0700789 }
Alex Crichton954046c2017-05-30 21:49:42 -0700790 }
791 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700792
Alex Crichton954046c2017-05-30 21:49:42 -0700793 impl Synom for BareFnArg {
794 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
795 do_parse! {
796 input,
797 name: option!(do_parse!(
798 name: syn!(Ident) >>
799 not!(syn!(Colon2)) >>
800 colon: syn!(Colon) >>
801 (name, colon)
802 )) >>
803 ty: syn!(Ty) >>
804 (BareFnArg {
805 name: name,
806 ty: ty,
807 })
808 }
809 }
810 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700811
Alex Crichton954046c2017-05-30 21:49:42 -0700812 impl Synom for Unsafety {
813 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
814 alt! {
815 input,
816 syn!(Unsafe) => { Unsafety::Unsafe }
817 |
818 epsilon!() => { |_| Unsafety::Normal }
819 }
820 }
821 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700822
Alex Crichton954046c2017-05-30 21:49:42 -0700823 impl Synom for Abi {
824 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
825 do_parse! {
826 input,
827 extern_: syn!(Extern) >>
828 // TODO: this parses all literals, not just strings
829 name: option!(syn!(Lit)) >>
830 (Abi {
831 extern_token: extern_,
832 kind: match name {
833 Some(name) => AbiKind::Named(name),
834 None => AbiKind::Default,
835 },
836 })
837 }
838 }
839 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700840}
David Tolnay87d0b442016-09-04 11:52:12 -0700841
842#[cfg(feature = "printing")]
843mod printing {
844 use super::*;
845 use quote::{Tokens, ToTokens};
846
Alex Crichton62a0a592017-05-22 13:58:53 -0700847 impl ToTokens for TySlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700848 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700849 self.bracket_token.surround(tokens, |tokens| {
850 self.ty.to_tokens(tokens);
851 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700852 }
853 }
854
855 impl ToTokens for TyArray {
856 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700857 self.bracket_token.surround(tokens, |tokens| {
858 self.ty.to_tokens(tokens);
859 self.semi_token.to_tokens(tokens);
860 self.amt.to_tokens(tokens);
861 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700862 }
863 }
864
865 impl ToTokens for TyPtr {
866 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700867 self.star_token.to_tokens(tokens);
868 self.const_token.to_tokens(tokens);
869 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700870 self.ty.ty.to_tokens(tokens);
871 }
872 }
873
874 impl ToTokens for TyRptr {
875 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700876 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700877 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700878 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700879 self.ty.ty.to_tokens(tokens);
880 }
881 }
882
883 impl ToTokens for TyBareFn {
884 fn to_tokens(&self, tokens: &mut Tokens) {
885 self.ty.to_tokens(tokens)
886 }
887 }
888
889 impl ToTokens for TyNever {
890 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700892 }
893 }
894
895 impl ToTokens for TyTup {
896 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700897 self.paren_token.surround(tokens, |tokens| {
898 self.tys.to_tokens(tokens);
899 self.lone_comma.to_tokens(tokens);
900 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700901 }
902 }
903
904 impl ToTokens for TyPath {
905 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700906 PathTokens(&self.qself, &self.path).to_tokens(tokens);
907 }
908 }
909
910 impl<'a> ToTokens for PathTokens<'a> {
911 fn to_tokens(&self, tokens: &mut Tokens) {
912 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700913 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700914 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700915 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700917 qself.ty.to_tokens(tokens);
918 if qself.position > 0 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700919 qself.as_token.to_tokens(tokens);
920 self.1.leading_colon.to_tokens(tokens);
921 for (i, segment) in self.1.segments
Alex Crichton62a0a592017-05-22 13:58:53 -0700922 .iter()
923 .take(qself.position)
924 .enumerate() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700925 if i == qself.position - 1 {
926 segment.item().to_tokens(tokens);
927 qself.gt_token.to_tokens(tokens);
928 segment.delimiter().to_tokens(tokens);
929 } else {
930 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700931 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700932 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700933 } else {
934 qself.gt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700935 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700936 for segment in self.1.segments.iter().skip(qself.position) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700937 segment.to_tokens(tokens);
938 }
939 }
940 }
941
942 impl ToTokens for TyTraitObject {
943 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700944 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700945 }
946 }
947
948 impl ToTokens for TyImplTrait {
949 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700950 self.impl_token.to_tokens(tokens);
951 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700952 }
953 }
954
955 impl ToTokens for TyParen {
956 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700957 self.paren_token.surround(tokens, |tokens| {
958 self.ty.to_tokens(tokens);
959 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700960 }
961 }
962
963 impl ToTokens for TyInfer {
964 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700965 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700966 }
967 }
968
David Tolnay47a877c2016-10-01 16:50:55 -0700969 impl ToTokens for Mutability {
970 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700971 if let Mutability::Mutable(ref t) = *self {
972 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700973 }
974 }
975 }
976
David Tolnay87d0b442016-09-04 11:52:12 -0700977 impl ToTokens for Path {
978 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700979 self.leading_colon.to_tokens(tokens);
980 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700981 }
982 }
983
984 impl ToTokens for PathSegment {
985 fn to_tokens(&self, tokens: &mut Tokens) {
986 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700987 self.parameters.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700988 }
989 }
990
991 impl ToTokens for PathParameters {
992 fn to_tokens(&self, tokens: &mut Tokens) {
993 match *self {
994 PathParameters::AngleBracketed(ref parameters) => {
995 parameters.to_tokens(tokens);
996 }
997 PathParameters::Parenthesized(ref parameters) => {
998 parameters.to_tokens(tokens);
999 }
1000 }
1001 }
1002 }
1003
1004 impl ToTokens for AngleBracketedParameterData {
1005 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001006 self.lt_token.to_tokens(tokens);
1007 self.lifetimes.to_tokens(tokens);
1008 self.types.to_tokens(tokens);
1009 self.bindings.to_tokens(tokens);
1010 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001011 }
1012 }
1013
1014 impl ToTokens for TypeBinding {
1015 fn to_tokens(&self, tokens: &mut Tokens) {
1016 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001017 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001018 self.ty.to_tokens(tokens);
1019 }
1020 }
1021
1022 impl ToTokens for ParenthesizedParameterData {
1023 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001024 self.paren_token.surround(tokens, |tokens| {
1025 self.inputs.to_tokens(tokens);
1026 });
1027 self.output.to_tokens(tokens);
1028 }
1029 }
1030
1031 impl ToTokens for FunctionRetTy {
1032 fn to_tokens(&self, tokens: &mut Tokens) {
1033 match *self {
1034 FunctionRetTy::Default => {}
1035 FunctionRetTy::Ty(ref ty, ref arrow) => {
1036 arrow.to_tokens(tokens);
1037 ty.to_tokens(tokens);
1038 }
David Tolnay87d0b442016-09-04 11:52:12 -07001039 }
1040 }
1041 }
1042
1043 impl ToTokens for PolyTraitRef {
1044 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001045 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001046 self.trait_ref.to_tokens(tokens);
1047 }
1048 }
1049
1050 impl ToTokens for BareFnTy {
1051 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001052 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001053 self.unsafety.to_tokens(tokens);
1054 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001055 self.fn_token.to_tokens(tokens);
1056 self.paren_token.surround(tokens, |tokens| {
1057 self.inputs.to_tokens(tokens);
1058 self.variadic.to_tokens(tokens);
1059 });
1060 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001061 }
1062 }
1063
David Tolnay62f374c2016-10-02 13:37:00 -07001064 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001065 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001066 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001067 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001068 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001069 }
1070 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001071 }
1072 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001073
1074 impl ToTokens for Unsafety {
1075 fn to_tokens(&self, tokens: &mut Tokens) {
1076 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001077 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001078 Unsafety::Normal => {
1079 // nothing
1080 }
1081 }
1082 }
1083 }
1084
1085 impl ToTokens for Abi {
1086 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001087 self.extern_token.to_tokens(tokens);
1088 match self.kind {
1089 AbiKind::Named(ref named) => named.to_tokens(tokens),
1090 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001091 }
1092 }
1093 }
David Tolnay87d0b442016-09-04 11:52:12 -07001094}