blob: 81e4df5208ffc462a30087507bfbfb4173a9cd51 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay2ae520a2017-12-29 11:19:50 -050010use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050012#[cfg(feature = "extra-traits")]
13use std::hash::{Hash, Hasher};
14#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070016
Alex Crichton62a0a592017-05-22 13:58:53 -070017ast_enum_of_structs! {
David Tolnay7949ae22018-01-07 00:14:51 -080018 /// The possible types that a Rust value could have.
David Tolnay614a0142018-01-07 10:25:43 -080019 ///
David Tolnay461d98e2018-01-07 11:07:19 -080020 /// *This type is available if Syn is built with the `"derive"` or `"full"`
21 /// feature.*
22 ///
David Tolnay614a0142018-01-07 10:25:43 -080023 /// # Syntax tree enum
24 ///
25 /// This type is a [syntax tree enum].
26 ///
27 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayfd6bf5c2017-11-12 09:41:14 -080028 pub enum Type {
David Tolnay7949ae22018-01-07 00:14:51 -080029 /// A dynamically sized slice type: `[T]`.
David Tolnay461d98e2018-01-07 11:07:19 -080030 ///
31 /// *This type is available if Syn is built with the `"derive"` or
32 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080033 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -050034 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050035 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080037
David Tolnay7949ae22018-01-07 00:14:51 -080038 /// A fixed size array type: `[T; n]`.
David Tolnay461d98e2018-01-07 11:07:19 -080039 ///
40 /// *This type is available if Syn is built with the `"derive"` or
41 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080042 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050043 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050044 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050046 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080048
David Tolnay7949ae22018-01-07 00:14:51 -080049 /// A raw pointer type: `*const T` or `*mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080050 ///
51 /// *This type is available if Syn is built with the `"derive"` or
52 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub star_token: Token![*],
55 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050056 pub mutability: Option<Token![mut]>,
57 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070058 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080059
David Tolnay7949ae22018-01-07 00:14:51 -080060 /// A reference type: `&'a T` or `&'a mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080061 ///
62 /// *This type is available if Syn is built with the `"derive"` or
63 /// `"full"` feature.*
David Tolnay0a89b4d2017-11-13 00:55:45 -080064 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050067 pub mutability: Option<Token![mut]>,
68 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080070
David Tolnay7949ae22018-01-07 00:14:51 -080071 /// A bare function type: `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -080072 ///
73 /// *This type is available if Syn is built with the `"derive"` or
74 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080075 pub BareFn(TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -050076 pub unsafety: Option<Token![unsafe]>,
77 pub abi: Option<Abi>,
78 pub fn_token: Token![fn],
79 pub lifetimes: Option<BoundLifetimes>,
80 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050081 pub inputs: Punctuated<BareFnArg, Token![,]>,
David Tolnaybe7a9592017-12-29 02:39:53 -050082 pub variadic: Option<Token![...]>,
83 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070084 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080085
David Tolnay7949ae22018-01-07 00:14:51 -080086 /// The never type: `!`.
David Tolnay461d98e2018-01-07 11:07:19 -080087 ///
88 /// *This type is available if Syn is built with the `"derive"` or
89 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080090 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070092 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080093
David Tolnay7949ae22018-01-07 00:14:51 -080094 /// A tuple type: `(A, B, C, String)`.
David Tolnay461d98e2018-01-07 11:07:19 -080095 ///
96 /// *This type is available if Syn is built with the `"derive"` or
97 /// `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -050098 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050099 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500100 pub elems: Punctuated<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800102
103 /// A path like `std::slice::Iter`, optionally qualified with a
David Tolnay7949ae22018-01-07 00:14:51 -0800104 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800106 /// Type arguments are stored in the Path itself.
David Tolnay461d98e2018-01-07 11:07:19 -0800107 ///
108 /// *This type is available if Syn is built with the `"derive"` or
109 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800110 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 pub qself: Option<QSelf>,
112 pub path: Path,
113 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800114
David Tolnay7949ae22018-01-07 00:14:51 -0800115 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
116 /// trait or a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"derive"` or
119 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800120 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -0500121 pub dyn_token: Option<Token![dyn]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500122 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800124
David Tolnay7949ae22018-01-07 00:14:51 -0800125 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
126 /// a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"derive"` or
129 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800130 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub impl_token: Token![impl],
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800134
135 /// A parenthesized type equivalent to the inner type.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -0500140 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -0500141 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800143
144 /// A type contained within invisible delimiters.
David Tolnay461d98e2018-01-07 11:07:19 -0800145 ///
146 /// *This type is available if Syn is built with the `"derive"` or
147 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800148 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500149 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -0500150 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -0400151 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800152
David Tolnay7949ae22018-01-07 00:14:51 -0800153 /// Indication that a type should be inferred by the compiler: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800154 ///
155 /// *This type is available if Syn is built with the `"derive"` or
156 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800157 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700159 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A macro in the type position.
David Tolnay461d98e2018-01-07 11:07:19 -0800162 ///
163 /// *This type is available if Syn is built with the `"derive"` or
164 /// `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500165 pub Macro(TypeMacro {
166 pub mac: Macro,
167 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800168
David Tolnay7949ae22018-01-07 00:14:51 -0800169 /// Tokens in type position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800170 ///
171 /// *This type is available if Syn is built with the `"derive"` or
172 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500173 pub Verbatim(TypeVerbatim #manual_extra_traits {
174 pub tts: TokenStream,
175 }),
176 }
177}
178
179#[cfg(feature = "extra-traits")]
180impl Eq for TypeVerbatim {}
181
182#[cfg(feature = "extra-traits")]
183impl PartialEq for TypeVerbatim {
184 fn eq(&self, other: &Self) -> bool {
185 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
186 }
187}
188
189#[cfg(feature = "extra-traits")]
190impl Hash for TypeVerbatim {
191 fn hash<H>(&self, state: &mut H)
192 where
193 H: Hasher,
194 {
195 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 }
197}
198
199ast_struct! {
David Tolnayf01e37b2018-01-06 23:38:26 -0800200 /// The binary interface of a function: `extern "C"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800201 ///
202 /// *This type is available if Syn is built with the `"derive"` or `"full"`
203 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub extern_token: Token![extern],
David Tolnayf01e37b2018-01-06 23:38:26 -0800206 pub name: Option<LitStr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }
208}
209
210ast_struct! {
David Tolnay7949ae22018-01-07 00:14:51 -0800211 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -0800212 ///
213 /// *This type is available if Syn is built with the `"derive"` or `"full"`
214 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800216 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800217 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 }
219}
220
Alex Crichton23a15f62017-08-28 12:34:23 -0700221ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800222 /// Name of an argument in a function type: the `n` in `fn(n: usize)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800223 ///
224 /// *This type is available if Syn is built with the `"derive"` or `"full"`
225 /// feature.*
Alex Crichton23a15f62017-08-28 12:34:23 -0700226 pub enum BareFnArgName {
David Tolnay7949ae22018-01-07 00:14:51 -0800227 /// Argument given a name.
Alex Crichton23a15f62017-08-28 12:34:23 -0700228 Named(Ident),
David Tolnay7949ae22018-01-07 00:14:51 -0800229 /// Argument not given a name, matched with `_`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800230 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700231 }
232}
Alex Crichton62a0a592017-05-22 13:58:53 -0700233
234ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800235 /// Return type of a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800236 ///
237 /// *This type is available if Syn is built with the `"derive"` or `"full"`
238 /// feature.*
David Tolnayf93b90d2017-11-11 19:21:26 -0800239 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 /// Return type is not specified.
241 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800242 /// Functions default to `()` and closures default to type inference.
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 Default,
David Tolnay7949ae22018-01-07 00:14:51 -0800244 /// A particular type is returned.
David Tolnay4a3f59a2017-12-28 21:21:12 -0500245 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }
David Tolnayb79ee962016-09-04 09:39:20 -0700247}
248
David Tolnay86eca752016-09-04 11:26:41 -0700249#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700250pub mod parsing {
251 use super::*;
David Tolnay056de302018-01-05 14:29:05 -0800252 use path::parsing::qpath;
David Tolnay94d2b792018-04-29 12:26:10 -0700253 use synom::Synom;
David Tolnayda4049b2016-09-04 10:59:23 -0700254
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800255 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400256 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700257
Alex Crichton954046c2017-05-30 21:49:42 -0700258 fn description() -> Option<&'static str> {
259 Some("type")
260 }
261 }
David Tolnay0047c712016-12-21 21:59:25 -0500262
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800263 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400264 /// In some positions, types may not contain the `+` character, to
265 /// disambiguate them. For example in the expression `1 as T`, T may not
266 /// contain a `+` character.
267 ///
268 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400269 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400270 }
271
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800272 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
273 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400274 |
David Tolnay05362582017-12-26 01:33:57 -0500275 // must be before TypeTuple
David Tolnay0a169d42017-12-29 17:57:29 -0500276 call!(TypeParen::parse, allow_plus) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400277 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500278 // must be before TypePath
David Tolnay323279a2017-12-29 11:26:32 -0500279 syn!(TypeMacro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400280 |
David Tolnay0a4d4e92018-07-21 15:31:45 -0700281 // must be before TypePath
282 syn!(TypeBareFn) => { Type::BareFn }
283 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500284 // must be before TypeTraitObject
285 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400286 |
David Tolnay0a169d42017-12-29 17:57:29 -0500287 // Don't try parsing more than one trait bound if we aren't allowing it.
288 // must be before TypeTuple
289 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
290 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800291 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400292 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800293 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400294 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800295 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400296 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800297 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400298 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800299 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400300 |
David Tolnay05362582017-12-26 01:33:57 -0500301 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400302 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800303 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700304 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800305 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400306 ));
307
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800308 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400309 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800310 brackets!(syn!(Type)),
David Tolnay8875fca2017-12-31 13:52:37 -0500311 |(b, ty)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500312 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400313 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700314 }
Michael Layzell92639a52017-06-01 00:07:44 -0400315 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800316
317 fn description() -> Option<&'static str> {
318 Some("slice type")
319 }
Alex Crichton954046c2017-05-30 21:49:42 -0700320 }
David Tolnayb79ee962016-09-04 09:39:20 -0700321
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800322 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400323 named!(parse -> Self, map!(
324 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800325 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800326 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400327 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700328 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400329 )),
David Tolnay8875fca2017-12-31 13:52:37 -0500330 |(brackets, (elem, semi, len))| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800331 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500332 elem: Box::new(elem),
333 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400334 bracket_token: brackets,
335 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700336 }
337 }
Michael Layzell92639a52017-06-01 00:07:44 -0400338 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800339
340 fn description() -> Option<&'static str> {
341 Some("array type")
342 }
Alex Crichton954046c2017-05-30 21:49:42 -0700343 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700344
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800345 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400346 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400348 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500349 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400350 |
David Tolnay24237fb2017-12-29 02:15:26 -0500351 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400352 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800353 target: call!(Type::without_plus) >>
354 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400355 const_token: mutability.1,
356 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500357 mutability: mutability.0,
358 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400359 })
360 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800361
362 fn description() -> Option<&'static str> {
363 Some("raw pointer type")
364 }
Alex Crichton954046c2017-05-30 21:49:42 -0700365 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700366
David Tolnay0a89b4d2017-11-13 00:55:45 -0800367 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400368 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800369 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400370 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500371 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400372 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800373 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800374 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400375 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500376 mutability: mutability,
377 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400378 and_token: amp,
379 })
380 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800381
382 fn description() -> Option<&'static str> {
383 Some("reference type")
384 }
Alex Crichton954046c2017-05-30 21:49:42 -0700385 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700386
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800387 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400388 named!(parse -> Self, do_parse!(
389 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500390 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400391 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800392 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400393 parens: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500394 inputs: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500395 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400396 (inputs, variadic)
397 )) >>
Geoffry Songac02b182018-05-19 22:11:31 -0700398 output: call!(ReturnType::without_plus) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800399 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500400 unsafety: unsafety,
401 abi: abi,
402 lifetimes: lifetimes,
403 output: output,
David Tolnay8875fca2017-12-31 13:52:37 -0500404 variadic: (parens.1).1,
David Tolnaybe7a9592017-12-29 02:39:53 -0500405 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500406 paren_token: parens.0,
407 inputs: (parens.1).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400408 })
409 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800410
411 fn description() -> Option<&'static str> {
412 Some("`fn` type")
413 }
Alex Crichton954046c2017-05-30 21:49:42 -0700414 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700415
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800416 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400417 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800419 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400420 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800421
422 fn description() -> Option<&'static str> {
423 Some("never type: `!`")
424 }
Alex Crichton954046c2017-05-30 21:49:42 -0700425 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700426
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800427 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700428 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800430 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700431 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800432
433 fn description() -> Option<&'static str> {
434 Some("inferred type: `_`")
435 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700436 }
437
David Tolnay05362582017-12-26 01:33:57 -0500438 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400439 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500440 data: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500441 (TypeTuple {
David Tolnay8875fca2017-12-31 13:52:37 -0500442 paren_token: data.0,
443 elems: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400444 })
445 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800446
447 fn description() -> Option<&'static str> {
448 Some("tuple type")
449 }
Alex Crichton954046c2017-05-30 21:49:42 -0700450 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700451
David Tolnay323279a2017-12-29 11:26:32 -0500452 impl Synom for TypeMacro {
453 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
David Tolnay79777332018-01-07 10:04:42 -0800454
455 fn description() -> Option<&'static str> {
456 Some("macro invocation")
457 }
David Tolnay323279a2017-12-29 11:26:32 -0500458 }
459
David Tolnay0a169d42017-12-29 17:57:29 -0500460 impl Synom for TypePath {
461 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800462
463 fn description() -> Option<&'static str> {
464 Some("type path")
465 }
David Tolnay0a169d42017-12-29 17:57:29 -0500466 }
467
David Tolnay7d38c7e2017-12-25 22:31:50 -0500468 impl TypePath {
469 named!(parse(allow_plus: bool) -> Self, do_parse!(
470 qpath: qpath >>
David Tolnay660fd1f2017-12-31 01:52:57 -0500471 parenthesized: option!(cond_reduce!(
David Tolnay56080682018-01-06 14:01:52 -0800472 qpath.1.segments.last().unwrap().value().arguments.is_empty(),
David Tolnay660fd1f2017-12-31 01:52:57 -0500473 syn!(ParenthesizedGenericArguments)
474 )) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500475 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500476 ({
477 let (qself, mut path) = qpath;
David Tolnay660fd1f2017-12-31 01:52:57 -0500478 if let Some(parenthesized) = parenthesized {
David Tolnay7d38c7e2017-12-25 22:31:50 -0500479 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay56080682018-01-06 14:01:52 -0800480 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
David Tolnay7d38c7e2017-12-25 22:31:50 -0500481 }
482 TypePath { qself: qself, path: path }
483 })
484 ));
485 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700486
Geoffry Songac02b182018-05-19 22:11:31 -0700487 impl ReturnType {
488 named!(pub without_plus -> Self, call!(Self::parse, false));
489 named!(parse(allow_plus: bool) -> Self, alt!(
Michael Layzell92639a52017-06-01 00:07:44 -0400490 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800491 arrow: punct!(->) >>
Geoffry Songac02b182018-05-19 22:11:31 -0700492 ty: call!(ambig_ty, allow_plus) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500493 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400494 )
495 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800496 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400497 ));
Geoffry Songac02b182018-05-19 22:11:31 -0700498 }
499
500 impl Synom for ReturnType {
501 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800502
503 fn description() -> Option<&'static str> {
504 Some("return type")
505 }
Alex Crichton954046c2017-05-30 21:49:42 -0700506 }
507
David Tolnay0a169d42017-12-29 17:57:29 -0500508 impl Synom for TypeTraitObject {
509 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800510
511 fn description() -> Option<&'static str> {
512 Some("trait object type")
513 }
David Tolnay0a169d42017-12-29 17:57:29 -0500514 }
515
David Tolnaye39b0702018-01-09 17:57:31 -0800516 fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool {
517 for bound in bounds {
518 if let TypeParamBound::Trait(_) = *bound {
519 return true;
520 }
521 }
522 false
523 }
524
David Tolnay7d38c7e2017-12-25 22:31:50 -0500525 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500526 named!(pub without_plus -> Self, call!(Self::parse, false));
527
David Tolnay7d38c7e2017-12-25 22:31:50 -0500528 // Only allow multiple trait references if allow_plus is true.
529 named!(parse(allow_plus: bool) -> Self, do_parse!(
530 dyn_token: option!(keyword!(dyn)) >>
531 bounds: alt!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500532 cond_reduce!(allow_plus, Punctuated::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500533 |
David Tolnay660fd1f2017-12-31 01:52:57 -0500534 syn!(TypeParamBound) => {|x| {
David Tolnayf2cfd722017-12-31 18:02:51 -0500535 let mut bounds = Punctuated::new();
David Tolnay56080682018-01-06 14:01:52 -0800536 bounds.push_value(x);
David Tolnayf2cfd722017-12-31 18:02:51 -0500537 bounds
David Tolnay660fd1f2017-12-31 01:52:57 -0500538 }}
David Tolnay7d38c7e2017-12-25 22:31:50 -0500539 ) >>
David Tolnaye39b0702018-01-09 17:57:31 -0800540 // Just lifetimes like `'a + 'b` is not a TraitObject.
David Tolnay96c6fbe2018-01-11 17:51:56 -0800541 cond_reduce!(at_least_one_type(&bounds)) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500542 (TypeTraitObject {
543 dyn_token: dyn_token,
544 bounds: bounds,
545 })
546 ));
547 }
David Tolnay6414da72016-10-08 00:55:17 -0700548
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800549 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400550 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400552 // NOTE: rust-lang/rust#34511 includes discussion about whether or
553 // not + should be allowed in ImplTrait directly without ().
David Tolnayf2cfd722017-12-31 18:02:51 -0500554 elem: call!(Punctuated::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800555 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400556 impl_token: impl_,
557 bounds: elem,
558 })
559 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800560
561 fn description() -> Option<&'static str> {
562 Some("`impl Trait` type")
563 }
Alex Crichton954046c2017-05-30 21:49:42 -0700564 }
David Tolnayb79ee962016-09-04 09:39:20 -0700565
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800566 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400567 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800568 data: grouped!(syn!(Type)) >>
569 (TypeGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500570 group_token: data.0,
571 elem: Box::new(data.1),
Michael Layzell93c36282017-06-04 20:43:14 -0400572 })
573 ));
David Tolnay79777332018-01-07 10:04:42 -0800574
575 fn description() -> Option<&'static str> {
576 Some("type surrounded by invisible delimiters")
577 }
Michael Layzell93c36282017-06-04 20:43:14 -0400578 }
579
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800580 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500581 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800582
583 fn description() -> Option<&'static str> {
584 Some("parenthesized type")
585 }
David Tolnay0a169d42017-12-29 17:57:29 -0500586 }
587
588 impl TypeParen {
589 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800590 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500591 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800592 (TypeParen {
David Tolnay8875fca2017-12-31 13:52:37 -0500593 paren_token: data.0,
594 elem: Box::new(data.1),
Michael Layzell92639a52017-06-01 00:07:44 -0400595 })
596 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700597 }
David Tolnayb79ee962016-09-04 09:39:20 -0700598
Alex Crichton954046c2017-05-30 21:49:42 -0700599 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400600 named!(parse -> Self, do_parse!(
601 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700602 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800603 not!(punct!(::)) >>
604 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400605 (name, colon)
606 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800607 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400608 (BareFnArg {
609 name: name,
610 ty: ty,
611 })
612 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800613
614 fn description() -> Option<&'static str> {
615 Some("function type argument")
616 }
Alex Crichton954046c2017-05-30 21:49:42 -0700617 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700618
Alex Crichton23a15f62017-08-28 12:34:23 -0700619 impl Synom for BareFnArgName {
620 named!(parse -> Self, alt!(
621 map!(syn!(Ident), BareFnArgName::Named)
622 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800623 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700624 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800625
626 fn description() -> Option<&'static str> {
627 Some("function argument name")
628 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700629 }
630
Alex Crichton954046c2017-05-30 21:49:42 -0700631 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400632 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800633 extern_: keyword!(extern) >>
David Tolnayf01e37b2018-01-06 23:38:26 -0800634 name: option!(syn!(LitStr)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400635 (Abi {
636 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500637 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400638 })
639 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800640
641 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -0800642 Some("`extern` ABI qualifier")
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800643 }
Alex Crichton954046c2017-05-30 21:49:42 -0700644 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700645}
David Tolnay87d0b442016-09-04 11:52:12 -0700646
647#[cfg(feature = "printing")]
648mod printing {
649 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700650 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700651 use quote::ToTokens;
David Tolnay87d0b442016-09-04 11:52:12 -0700652
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800653 impl ToTokens for TypeSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700654 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700655 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500656 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700657 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700658 }
659 }
660
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800661 impl ToTokens for TypeArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700662 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700663 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500664 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700665 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500666 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700667 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700668 }
669 }
670
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800671 impl ToTokens for TypePtr {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700672 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700673 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500674 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500675 Some(ref tok) => tok.to_tokens(tokens),
676 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700677 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400678 }
679 }
David Tolnay136aaa32017-12-29 02:37:36 -0500680 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700681 }
682 }
683
David Tolnay0a89b4d2017-11-13 00:55:45 -0800684 impl ToTokens for TypeReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700685 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700686 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500688 self.mutability.to_tokens(tokens);
689 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 }
691 }
692
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800693 impl ToTokens for TypeBareFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700694 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500695 self.lifetimes.to_tokens(tokens);
696 self.unsafety.to_tokens(tokens);
697 self.abi.to_tokens(tokens);
698 self.fn_token.to_tokens(tokens);
699 self.paren_token.surround(tokens, |tokens| {
700 self.inputs.to_tokens(tokens);
701 if let Some(ref variadic) = self.variadic {
702 if !self.inputs.empty_or_trailing() {
David Tolnay7ac699c2018-08-24 14:00:58 -0400703 let span = variadic.spans[0];
704 Token![,](span).to_tokens(tokens);
David Tolnaybe7a9592017-12-29 02:39:53 -0500705 }
706 variadic.to_tokens(tokens);
707 }
708 });
709 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 }
711 }
712
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800713 impl ToTokens for TypeNever {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700714 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700715 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 }
717 }
718
David Tolnay05362582017-12-26 01:33:57 -0500719 impl ToTokens for TypeTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700720 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700721 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500722 self.elems.to_tokens(tokens);
David Tolnaydb402062018-03-31 22:23:30 +0200723 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700724 }
725 }
726
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800727 impl ToTokens for TypePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700728 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700729 PathTokens(&self.qself, &self.path).to_tokens(tokens);
730 }
731 }
732
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800733 impl ToTokens for TypeTraitObject {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700734 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500735 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700736 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 }
738 }
739
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800740 impl ToTokens for TypeImplTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700741 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700742 self.impl_token.to_tokens(tokens);
743 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }
745 }
746
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800747 impl ToTokens for TypeGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700748 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell93c36282017-06-04 20:43:14 -0400749 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500750 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400751 });
752 }
753 }
754
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800755 impl ToTokens for TypeParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700756 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700757 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500758 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700759 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700760 }
761 }
762
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800763 impl ToTokens for TypeInfer {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700764 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700765 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700766 }
767 }
768
David Tolnay323279a2017-12-29 11:26:32 -0500769 impl ToTokens for TypeMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700770 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -0500771 self.mac.to_tokens(tokens);
772 }
773 }
774
David Tolnay2ae520a2017-12-29 11:19:50 -0500775 impl ToTokens for TypeVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700776 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -0500777 self.tts.to_tokens(tokens);
778 }
779 }
780
David Tolnayf93b90d2017-11-11 19:21:26 -0800781 impl ToTokens for ReturnType {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700782 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700783 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800784 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500785 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700786 arrow.to_tokens(tokens);
787 ty.to_tokens(tokens);
788 }
David Tolnay87d0b442016-09-04 11:52:12 -0700789 }
790 }
791 }
792
David Tolnay62f374c2016-10-02 13:37:00 -0700793 impl ToTokens for BareFnArg {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700794 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700796 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700797 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700798 }
799 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700800 }
801 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700802
Alex Crichton23a15f62017-08-28 12:34:23 -0700803 impl ToTokens for BareFnArgName {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700804 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton23a15f62017-08-28 12:34:23 -0700805 match *self {
806 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
807 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
808 }
809 }
810 }
811
David Tolnayb8d8ef52016-10-29 14:30:08 -0700812 impl ToTokens for Abi {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700813 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700814 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500815 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700816 }
817 }
David Tolnay87d0b442016-09-04 11:52:12 -0700818}