blob: bd6fd060def4f68073d26eeb26cc0e0547ed7ec4 [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::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -070010use proc_macro2::{TokenStream, Ident};
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 Tolnay7d38c7e2017-12-25 22:31:50 -0500281 // must be before TypeTraitObject
282 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400283 |
David Tolnay0a169d42017-12-29 17:57:29 -0500284 // Don't try parsing more than one trait bound if we aren't allowing it.
285 // must be before TypeTuple
286 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
287 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800288 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400289 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800290 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400291 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800292 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400293 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800294 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400295 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800296 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400297 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800298 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400299 |
David Tolnay05362582017-12-26 01:33:57 -0500300 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400301 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800302 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700303 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800304 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400305 ));
306
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800307 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400308 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800309 brackets!(syn!(Type)),
David Tolnay8875fca2017-12-31 13:52:37 -0500310 |(b, ty)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500311 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400312 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700313 }
Michael Layzell92639a52017-06-01 00:07:44 -0400314 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800315
316 fn description() -> Option<&'static str> {
317 Some("slice type")
318 }
Alex Crichton954046c2017-05-30 21:49:42 -0700319 }
David Tolnayb79ee962016-09-04 09:39:20 -0700320
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800321 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400322 named!(parse -> Self, map!(
323 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800324 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800325 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400326 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700327 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400328 )),
David Tolnay8875fca2017-12-31 13:52:37 -0500329 |(brackets, (elem, semi, len))| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800330 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500331 elem: Box::new(elem),
332 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400333 bracket_token: brackets,
334 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700335 }
336 }
Michael Layzell92639a52017-06-01 00:07:44 -0400337 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800338
339 fn description() -> Option<&'static str> {
340 Some("array type")
341 }
Alex Crichton954046c2017-05-30 21:49:42 -0700342 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700343
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800344 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400345 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800346 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400347 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500348 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400349 |
David Tolnay24237fb2017-12-29 02:15:26 -0500350 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400351 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800352 target: call!(Type::without_plus) >>
353 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400354 const_token: mutability.1,
355 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500356 mutability: mutability.0,
357 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400358 })
359 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800360
361 fn description() -> Option<&'static str> {
362 Some("raw pointer type")
363 }
Alex Crichton954046c2017-05-30 21:49:42 -0700364 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700365
David Tolnay0a89b4d2017-11-13 00:55:45 -0800366 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400367 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400369 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500370 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400371 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800372 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800373 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400374 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500375 mutability: mutability,
376 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400377 and_token: amp,
378 })
379 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800380
381 fn description() -> Option<&'static str> {
382 Some("reference type")
383 }
Alex Crichton954046c2017-05-30 21:49:42 -0700384 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700385
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800386 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400387 named!(parse -> Self, do_parse!(
388 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500389 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400390 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800391 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400392 parens: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500393 inputs: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500394 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400395 (inputs, variadic)
396 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800397 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800398 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500399 unsafety: unsafety,
400 abi: abi,
401 lifetimes: lifetimes,
402 output: output,
David Tolnay8875fca2017-12-31 13:52:37 -0500403 variadic: (parens.1).1,
David Tolnaybe7a9592017-12-29 02:39:53 -0500404 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500405 paren_token: parens.0,
406 inputs: (parens.1).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400407 })
408 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800409
410 fn description() -> Option<&'static str> {
411 Some("`fn` type")
412 }
Alex Crichton954046c2017-05-30 21:49:42 -0700413 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700414
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800415 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400416 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800418 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400419 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800420
421 fn description() -> Option<&'static str> {
422 Some("never type: `!`")
423 }
Alex Crichton954046c2017-05-30 21:49:42 -0700424 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700425
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800426 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700427 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800428 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800429 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700430 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800431
432 fn description() -> Option<&'static str> {
433 Some("inferred type: `_`")
434 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700435 }
436
David Tolnay05362582017-12-26 01:33:57 -0500437 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400438 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500439 data: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500440 (TypeTuple {
David Tolnay8875fca2017-12-31 13:52:37 -0500441 paren_token: data.0,
442 elems: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400443 })
444 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800445
446 fn description() -> Option<&'static str> {
447 Some("tuple type")
448 }
Alex Crichton954046c2017-05-30 21:49:42 -0700449 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700450
David Tolnay323279a2017-12-29 11:26:32 -0500451 impl Synom for TypeMacro {
452 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
David Tolnay79777332018-01-07 10:04:42 -0800453
454 fn description() -> Option<&'static str> {
455 Some("macro invocation")
456 }
David Tolnay323279a2017-12-29 11:26:32 -0500457 }
458
David Tolnay0a169d42017-12-29 17:57:29 -0500459 impl Synom for TypePath {
460 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800461
462 fn description() -> Option<&'static str> {
463 Some("type path")
464 }
David Tolnay0a169d42017-12-29 17:57:29 -0500465 }
466
David Tolnay7d38c7e2017-12-25 22:31:50 -0500467 impl TypePath {
468 named!(parse(allow_plus: bool) -> Self, do_parse!(
469 qpath: qpath >>
David Tolnay660fd1f2017-12-31 01:52:57 -0500470 parenthesized: option!(cond_reduce!(
David Tolnay56080682018-01-06 14:01:52 -0800471 qpath.1.segments.last().unwrap().value().arguments.is_empty(),
David Tolnay660fd1f2017-12-31 01:52:57 -0500472 syn!(ParenthesizedGenericArguments)
473 )) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500474 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500475 ({
476 let (qself, mut path) = qpath;
David Tolnay660fd1f2017-12-31 01:52:57 -0500477 if let Some(parenthesized) = parenthesized {
David Tolnay7d38c7e2017-12-25 22:31:50 -0500478 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay56080682018-01-06 14:01:52 -0800479 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
David Tolnay7d38c7e2017-12-25 22:31:50 -0500480 }
481 TypePath { qself: qself, path: path }
482 })
483 ));
484 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700485
David Tolnayf93b90d2017-11-11 19:21:26 -0800486 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400487 named!(parse -> Self, alt!(
488 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800489 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800490 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500491 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400492 )
493 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800494 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400495 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800496
497 fn description() -> Option<&'static str> {
498 Some("return type")
499 }
Alex Crichton954046c2017-05-30 21:49:42 -0700500 }
501
David Tolnay0a169d42017-12-29 17:57:29 -0500502 impl Synom for TypeTraitObject {
503 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800504
505 fn description() -> Option<&'static str> {
506 Some("trait object type")
507 }
David Tolnay0a169d42017-12-29 17:57:29 -0500508 }
509
David Tolnaye39b0702018-01-09 17:57:31 -0800510 fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool {
511 for bound in bounds {
512 if let TypeParamBound::Trait(_) = *bound {
513 return true;
514 }
515 }
516 false
517 }
518
David Tolnay7d38c7e2017-12-25 22:31:50 -0500519 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500520 named!(pub without_plus -> Self, call!(Self::parse, false));
521
David Tolnay7d38c7e2017-12-25 22:31:50 -0500522 // Only allow multiple trait references if allow_plus is true.
523 named!(parse(allow_plus: bool) -> Self, do_parse!(
524 dyn_token: option!(keyword!(dyn)) >>
525 bounds: alt!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500526 cond_reduce!(allow_plus, Punctuated::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500527 |
David Tolnay660fd1f2017-12-31 01:52:57 -0500528 syn!(TypeParamBound) => {|x| {
David Tolnayf2cfd722017-12-31 18:02:51 -0500529 let mut bounds = Punctuated::new();
David Tolnay56080682018-01-06 14:01:52 -0800530 bounds.push_value(x);
David Tolnayf2cfd722017-12-31 18:02:51 -0500531 bounds
David Tolnay660fd1f2017-12-31 01:52:57 -0500532 }}
David Tolnay7d38c7e2017-12-25 22:31:50 -0500533 ) >>
David Tolnaye39b0702018-01-09 17:57:31 -0800534 // Just lifetimes like `'a + 'b` is not a TraitObject.
David Tolnay96c6fbe2018-01-11 17:51:56 -0800535 cond_reduce!(at_least_one_type(&bounds)) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500536 (TypeTraitObject {
537 dyn_token: dyn_token,
538 bounds: bounds,
539 })
540 ));
541 }
David Tolnay6414da72016-10-08 00:55:17 -0700542
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800543 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400544 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800545 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400546 // NOTE: rust-lang/rust#34511 includes discussion about whether or
547 // not + should be allowed in ImplTrait directly without ().
David Tolnayf2cfd722017-12-31 18:02:51 -0500548 elem: call!(Punctuated::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800549 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400550 impl_token: impl_,
551 bounds: elem,
552 })
553 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800554
555 fn description() -> Option<&'static str> {
556 Some("`impl Trait` type")
557 }
Alex Crichton954046c2017-05-30 21:49:42 -0700558 }
David Tolnayb79ee962016-09-04 09:39:20 -0700559
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800560 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400561 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800562 data: grouped!(syn!(Type)) >>
563 (TypeGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500564 group_token: data.0,
565 elem: Box::new(data.1),
Michael Layzell93c36282017-06-04 20:43:14 -0400566 })
567 ));
David Tolnay79777332018-01-07 10:04:42 -0800568
569 fn description() -> Option<&'static str> {
570 Some("type surrounded by invisible delimiters")
571 }
Michael Layzell93c36282017-06-04 20:43:14 -0400572 }
573
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800574 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500575 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800576
577 fn description() -> Option<&'static str> {
578 Some("parenthesized type")
579 }
David Tolnay0a169d42017-12-29 17:57:29 -0500580 }
581
582 impl TypeParen {
583 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800584 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500585 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800586 (TypeParen {
David Tolnay8875fca2017-12-31 13:52:37 -0500587 paren_token: data.0,
588 elem: Box::new(data.1),
Michael Layzell92639a52017-06-01 00:07:44 -0400589 })
590 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700591 }
David Tolnayb79ee962016-09-04 09:39:20 -0700592
Alex Crichton954046c2017-05-30 21:49:42 -0700593 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400594 named!(parse -> Self, do_parse!(
595 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700596 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800597 not!(punct!(::)) >>
598 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400599 (name, colon)
600 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800601 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400602 (BareFnArg {
603 name: name,
604 ty: ty,
605 })
606 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800607
608 fn description() -> Option<&'static str> {
609 Some("function type argument")
610 }
Alex Crichton954046c2017-05-30 21:49:42 -0700611 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700612
Alex Crichton23a15f62017-08-28 12:34:23 -0700613 impl Synom for BareFnArgName {
614 named!(parse -> Self, alt!(
615 map!(syn!(Ident), BareFnArgName::Named)
616 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700618 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800619
620 fn description() -> Option<&'static str> {
621 Some("function argument name")
622 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700623 }
624
Alex Crichton954046c2017-05-30 21:49:42 -0700625 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400626 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 extern_: keyword!(extern) >>
David Tolnayf01e37b2018-01-06 23:38:26 -0800628 name: option!(syn!(LitStr)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400629 (Abi {
630 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500631 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400632 })
633 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800634
635 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -0800636 Some("`extern` ABI qualifier")
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800637 }
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700639}
David Tolnay87d0b442016-09-04 11:52:12 -0700640
641#[cfg(feature = "printing")]
642mod printing {
643 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700644 use quote::ToTokens;
645 use proc_macro2::TokenStream;
David Tolnay87d0b442016-09-04 11:52:12 -0700646
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800647 impl ToTokens for TypeSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700648 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700649 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500650 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700651 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 }
653 }
654
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800655 impl ToTokens for TypeArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700656 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700657 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500658 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700659 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500660 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700661 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700662 }
663 }
664
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800665 impl ToTokens for TypePtr {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700666 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700667 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500668 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500669 Some(ref tok) => tok.to_tokens(tokens),
670 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700671 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400672 }
673 }
David Tolnay136aaa32017-12-29 02:37:36 -0500674 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700675 }
676 }
677
David Tolnay0a89b4d2017-11-13 00:55:45 -0800678 impl ToTokens for TypeReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700679 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700680 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700681 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500682 self.mutability.to_tokens(tokens);
683 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700684 }
685 }
686
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800687 impl ToTokens for TypeBareFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700688 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500689 self.lifetimes.to_tokens(tokens);
690 self.unsafety.to_tokens(tokens);
691 self.abi.to_tokens(tokens);
692 self.fn_token.to_tokens(tokens);
693 self.paren_token.surround(tokens, |tokens| {
694 self.inputs.to_tokens(tokens);
695 if let Some(ref variadic) = self.variadic {
696 if !self.inputs.empty_or_trailing() {
697 let span = variadic.0[0];
698 <Token![,]>::new(span).to_tokens(tokens);
699 }
700 variadic.to_tokens(tokens);
701 }
702 });
703 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 }
705 }
706
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800707 impl ToTokens for TypeNever {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700708 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700709 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 }
711 }
712
David Tolnay05362582017-12-26 01:33:57 -0500713 impl ToTokens for TypeTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700714 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700715 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500716 self.elems.to_tokens(tokens);
David Tolnaydb402062018-03-31 22:23:30 +0200717 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 }
719 }
720
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800721 impl ToTokens for TypePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700722 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700723 PathTokens(&self.qself, &self.path).to_tokens(tokens);
724 }
725 }
726
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800727 impl ToTokens for TypeTraitObject {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700728 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500729 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700730 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 }
732 }
733
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800734 impl ToTokens for TypeImplTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700735 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700736 self.impl_token.to_tokens(tokens);
737 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700738 }
739 }
740
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800741 impl ToTokens for TypeGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700742 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell93c36282017-06-04 20:43:14 -0400743 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500744 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400745 });
746 }
747 }
748
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800749 impl ToTokens for TypeParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700750 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700751 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500752 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700753 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700754 }
755 }
756
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800757 impl ToTokens for TypeInfer {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700758 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700759 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700760 }
761 }
762
David Tolnay323279a2017-12-29 11:26:32 -0500763 impl ToTokens for TypeMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700764 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -0500765 self.mac.to_tokens(tokens);
766 }
767 }
768
David Tolnay2ae520a2017-12-29 11:19:50 -0500769 impl ToTokens for TypeVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700770 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -0500771 self.tts.to_tokens(tokens);
772 }
773 }
774
David Tolnayf93b90d2017-11-11 19:21:26 -0800775 impl ToTokens for ReturnType {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700776 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700777 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800778 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500779 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700780 arrow.to_tokens(tokens);
781 ty.to_tokens(tokens);
782 }
David Tolnay87d0b442016-09-04 11:52:12 -0700783 }
784 }
785 }
786
David Tolnay62f374c2016-10-02 13:37:00 -0700787 impl ToTokens for BareFnArg {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700788 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700789 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700790 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700791 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700792 }
793 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700794 }
795 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700796
Alex Crichton23a15f62017-08-28 12:34:23 -0700797 impl ToTokens for BareFnArgName {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700798 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton23a15f62017-08-28 12:34:23 -0700799 match *self {
800 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
801 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
802 }
803 }
804 }
805
David Tolnayb8d8ef52016-10-29 14:30:08 -0700806 impl ToTokens for Abi {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700807 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700808 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500809 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700810 }
811 }
David Tolnay87d0b442016-09-04 11:52:12 -0700812}