blob: 9538260b7feb6252dfd382a900ecf6afb0ec4343 [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 Tolnayf2cfd722017-12-31 18:02:51 -05009use punctuated::Punctuated;
David Tolnayb79ee962016-09-04 09:39:20 -070010use super::*;
David Tolnay2ae520a2017-12-29 11:19:50 -050011use proc_macro2::TokenStream;
12#[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 Tolnayfd6bf5c2017-11-12 09:41:14 -080019 pub enum Type {
David Tolnay7949ae22018-01-07 00:14:51 -080020 /// A dynamically sized slice type: `[T]`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080021 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -050022 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050023 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080025
David Tolnay7949ae22018-01-07 00:14:51 -080026 /// A fixed size array type: `[T; n]`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080027 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050028 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050029 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080030 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050031 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070032 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080033
David Tolnay7949ae22018-01-07 00:14:51 -080034 /// A raw pointer type: `*const T` or `*mut T`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080035 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080036 pub star_token: Token![*],
37 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050038 pub mutability: Option<Token![mut]>,
39 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070040 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080041
David Tolnay7949ae22018-01-07 00:14:51 -080042 /// A reference type: `&'a T` or `&'a mut T`.
David Tolnay0a89b4d2017-11-13 00:55:45 -080043 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080044 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070045 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050046 pub mutability: Option<Token![mut]>,
47 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070048 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080049
David Tolnay7949ae22018-01-07 00:14:51 -080050 /// A bare function type: `fn(usize) -> bool`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080051 pub BareFn(TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -050052 pub unsafety: Option<Token![unsafe]>,
53 pub abi: Option<Abi>,
54 pub fn_token: Token![fn],
55 pub lifetimes: Option<BoundLifetimes>,
56 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050057 pub inputs: Punctuated<BareFnArg, Token![,]>,
David Tolnaybe7a9592017-12-29 02:39:53 -050058 pub variadic: Option<Token![...]>,
59 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070060 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080061
David Tolnay7949ae22018-01-07 00:14:51 -080062 /// The never type: `!`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080063 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080066
David Tolnay7949ae22018-01-07 00:14:51 -080067 /// A tuple type: `(A, B, C, String)`.
David Tolnay05362582017-12-26 01:33:57 -050068 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050069 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050070 pub elems: Punctuated<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080072
73 /// A path like `std::slice::Iter`, optionally qualified with a
David Tolnay7949ae22018-01-07 00:14:51 -080074 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
Alex Crichton62a0a592017-05-22 13:58:53 -070075 ///
David Tolnay7949ae22018-01-07 00:14:51 -080076 /// Type arguments are stored in the Path itself.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080077 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -070078 pub qself: Option<QSelf>,
79 pub path: Path,
80 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080081
David Tolnay7949ae22018-01-07 00:14:51 -080082 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
83 /// trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080084 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -050085 pub dyn_token: Option<Token![dyn]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050086 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070087 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080088
David Tolnay7949ae22018-01-07 00:14:51 -080089 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
90 /// a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080091 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -080092 pub impl_token: Token![impl],
David Tolnayf2cfd722017-12-31 18:02:51 -050093 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070094 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080095
96 /// A parenthesized type equivalent to the inner type.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080097 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -050098 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050099 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800101
102 /// A type contained within invisible delimiters.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800103 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500104 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -0500105 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -0400106 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800107
David Tolnay7949ae22018-01-07 00:14:51 -0800108 /// Indication that a type should be inferred by the compiler: `_`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800109 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800112
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 /// A macro in the type position.
David Tolnay323279a2017-12-29 11:26:32 -0500114 pub Macro(TypeMacro {
115 pub mac: Macro,
116 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800117
David Tolnay7949ae22018-01-07 00:14:51 -0800118 /// Tokens in type position not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500119 pub Verbatim(TypeVerbatim #manual_extra_traits {
120 pub tts: TokenStream,
121 }),
122 }
123}
124
125#[cfg(feature = "extra-traits")]
126impl Eq for TypeVerbatim {}
127
128#[cfg(feature = "extra-traits")]
129impl PartialEq for TypeVerbatim {
130 fn eq(&self, other: &Self) -> bool {
131 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
132 }
133}
134
135#[cfg(feature = "extra-traits")]
136impl Hash for TypeVerbatim {
137 fn hash<H>(&self, state: &mut H)
138 where
139 H: Hasher,
140 {
141 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }
143}
144
145ast_struct! {
David Tolnayf01e37b2018-01-06 23:38:26 -0800146 /// The binary interface of a function: `extern "C"`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700147 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800148 pub extern_token: Token![extern],
David Tolnayf01e37b2018-01-06 23:38:26 -0800149 pub name: Option<LitStr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700150 }
151}
152
153ast_struct! {
David Tolnay7949ae22018-01-07 00:14:51 -0800154 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800156 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800157 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700158 }
159}
160
Alex Crichton23a15f62017-08-28 12:34:23 -0700161ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800162 /// Name of an argument in a function type: the `n` in `fn(n: usize)`.
Alex Crichton23a15f62017-08-28 12:34:23 -0700163 pub enum BareFnArgName {
David Tolnay7949ae22018-01-07 00:14:51 -0800164 /// Argument given a name.
Alex Crichton23a15f62017-08-28 12:34:23 -0700165 Named(Ident),
David Tolnay7949ae22018-01-07 00:14:51 -0800166 /// Argument not given a name, matched with `_`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800167 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700168 }
169}
Alex Crichton62a0a592017-05-22 13:58:53 -0700170
171ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800172 /// Return type of a function signature.
David Tolnayf93b90d2017-11-11 19:21:26 -0800173 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 /// Return type is not specified.
175 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800176 /// Functions default to `()` and closures default to type inference.
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 Default,
David Tolnay7949ae22018-01-07 00:14:51 -0800178 /// A particular type is returned.
David Tolnay4a3f59a2017-12-28 21:21:12 -0500179 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 }
David Tolnayb79ee962016-09-04 09:39:20 -0700181}
182
David Tolnay86eca752016-09-04 11:26:41 -0700183#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700184pub mod parsing {
185 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400186 use synom::Synom;
David Tolnay056de302018-01-05 14:29:05 -0800187 use path::parsing::qpath;
David Tolnayda4049b2016-09-04 10:59:23 -0700188
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800189 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400190 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700191
Alex Crichton954046c2017-05-30 21:49:42 -0700192 fn description() -> Option<&'static str> {
193 Some("type")
194 }
195 }
David Tolnay0047c712016-12-21 21:59:25 -0500196
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800197 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400198 /// In some positions, types may not contain the `+` character, to
199 /// disambiguate them. For example in the expression `1 as T`, T may not
200 /// contain a `+` character.
201 ///
202 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400203 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400204 }
205
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
207 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400208 |
David Tolnay05362582017-12-26 01:33:57 -0500209 // must be before TypeTuple
David Tolnay0a169d42017-12-29 17:57:29 -0500210 call!(TypeParen::parse, allow_plus) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400211 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500212 // must be before TypePath
David Tolnay323279a2017-12-29 11:26:32 -0500213 syn!(TypeMacro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400214 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500215 // must be before TypeTraitObject
216 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400217 |
David Tolnay0a169d42017-12-29 17:57:29 -0500218 // Don't try parsing more than one trait bound if we aren't allowing it.
219 // must be before TypeTuple
220 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
221 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800222 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400223 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800224 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400225 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800226 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400227 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800228 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400229 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800230 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400231 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800232 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400233 |
David Tolnay05362582017-12-26 01:33:57 -0500234 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400235 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800236 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700237 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800238 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400239 ));
240
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800241 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400242 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800243 brackets!(syn!(Type)),
David Tolnay8875fca2017-12-31 13:52:37 -0500244 |(b, ty)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500245 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400246 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700247 }
Michael Layzell92639a52017-06-01 00:07:44 -0400248 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800249
250 fn description() -> Option<&'static str> {
251 Some("slice type")
252 }
Alex Crichton954046c2017-05-30 21:49:42 -0700253 }
David Tolnayb79ee962016-09-04 09:39:20 -0700254
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800255 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400256 named!(parse -> Self, map!(
257 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800258 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800259 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400260 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700261 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400262 )),
David Tolnay8875fca2017-12-31 13:52:37 -0500263 |(brackets, (elem, semi, len))| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800264 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500265 elem: Box::new(elem),
266 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400267 bracket_token: brackets,
268 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700269 }
270 }
Michael Layzell92639a52017-06-01 00:07:44 -0400271 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800272
273 fn description() -> Option<&'static str> {
274 Some("array type")
275 }
Alex Crichton954046c2017-05-30 21:49:42 -0700276 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700277
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800278 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400279 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800280 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400281 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500282 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400283 |
David Tolnay24237fb2017-12-29 02:15:26 -0500284 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400285 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800286 target: call!(Type::without_plus) >>
287 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400288 const_token: mutability.1,
289 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500290 mutability: mutability.0,
291 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400292 })
293 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800294
295 fn description() -> Option<&'static str> {
296 Some("raw pointer type")
297 }
Alex Crichton954046c2017-05-30 21:49:42 -0700298 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700299
David Tolnay0a89b4d2017-11-13 00:55:45 -0800300 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400301 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800302 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400303 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500304 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400305 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800306 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800307 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400308 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500309 mutability: mutability,
310 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400311 and_token: amp,
312 })
313 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800314
315 fn description() -> Option<&'static str> {
316 Some("reference type")
317 }
Alex Crichton954046c2017-05-30 21:49:42 -0700318 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700319
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800320 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400321 named!(parse -> Self, do_parse!(
322 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500323 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400324 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800325 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400326 parens: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500327 inputs: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500328 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400329 (inputs, variadic)
330 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800331 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800332 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500333 unsafety: unsafety,
334 abi: abi,
335 lifetimes: lifetimes,
336 output: output,
David Tolnay8875fca2017-12-31 13:52:37 -0500337 variadic: (parens.1).1,
David Tolnaybe7a9592017-12-29 02:39:53 -0500338 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500339 paren_token: parens.0,
340 inputs: (parens.1).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400341 })
342 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800343
344 fn description() -> Option<&'static str> {
345 Some("`fn` type")
346 }
Alex Crichton954046c2017-05-30 21:49:42 -0700347 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700348
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800349 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400350 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800351 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800352 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400353 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800354
355 fn description() -> Option<&'static str> {
356 Some("never type: `!`")
357 }
Alex Crichton954046c2017-05-30 21:49:42 -0700358 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700359
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800360 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700361 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800362 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800363 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700364 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800365
366 fn description() -> Option<&'static str> {
367 Some("inferred type: `_`")
368 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700369 }
370
David Tolnay05362582017-12-26 01:33:57 -0500371 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400372 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500373 data: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500374 (TypeTuple {
David Tolnay8875fca2017-12-31 13:52:37 -0500375 paren_token: data.0,
376 elems: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400377 })
378 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800379
380 fn description() -> Option<&'static str> {
381 Some("tuple type")
382 }
Alex Crichton954046c2017-05-30 21:49:42 -0700383 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700384
David Tolnay323279a2017-12-29 11:26:32 -0500385 impl Synom for TypeMacro {
386 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
David Tolnay79777332018-01-07 10:04:42 -0800387
388 fn description() -> Option<&'static str> {
389 Some("macro invocation")
390 }
David Tolnay323279a2017-12-29 11:26:32 -0500391 }
392
David Tolnay0a169d42017-12-29 17:57:29 -0500393 impl Synom for TypePath {
394 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800395
396 fn description() -> Option<&'static str> {
397 Some("type path")
398 }
David Tolnay0a169d42017-12-29 17:57:29 -0500399 }
400
David Tolnay7d38c7e2017-12-25 22:31:50 -0500401 impl TypePath {
402 named!(parse(allow_plus: bool) -> Self, do_parse!(
403 qpath: qpath >>
David Tolnay660fd1f2017-12-31 01:52:57 -0500404 parenthesized: option!(cond_reduce!(
David Tolnay56080682018-01-06 14:01:52 -0800405 qpath.1.segments.last().unwrap().value().arguments.is_empty(),
David Tolnay660fd1f2017-12-31 01:52:57 -0500406 syn!(ParenthesizedGenericArguments)
407 )) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500408 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500409 ({
410 let (qself, mut path) = qpath;
David Tolnay660fd1f2017-12-31 01:52:57 -0500411 if let Some(parenthesized) = parenthesized {
David Tolnay7d38c7e2017-12-25 22:31:50 -0500412 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay56080682018-01-06 14:01:52 -0800413 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
David Tolnay7d38c7e2017-12-25 22:31:50 -0500414 }
415 TypePath { qself: qself, path: path }
416 })
417 ));
418 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700419
David Tolnayf93b90d2017-11-11 19:21:26 -0800420 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400421 named!(parse -> Self, alt!(
422 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800423 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800424 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500425 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400426 )
427 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800428 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400429 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800430
431 fn description() -> Option<&'static str> {
432 Some("return type")
433 }
Alex Crichton954046c2017-05-30 21:49:42 -0700434 }
435
David Tolnay0a169d42017-12-29 17:57:29 -0500436 impl Synom for TypeTraitObject {
437 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800438
439 fn description() -> Option<&'static str> {
440 Some("trait object type")
441 }
David Tolnay0a169d42017-12-29 17:57:29 -0500442 }
443
David Tolnay7d38c7e2017-12-25 22:31:50 -0500444 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500445 named!(pub without_plus -> Self, call!(Self::parse, false));
446
David Tolnay7d38c7e2017-12-25 22:31:50 -0500447 // Only allow multiple trait references if allow_plus is true.
448 named!(parse(allow_plus: bool) -> Self, do_parse!(
449 dyn_token: option!(keyword!(dyn)) >>
450 bounds: alt!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500451 cond_reduce!(allow_plus, Punctuated::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500452 |
David Tolnay660fd1f2017-12-31 01:52:57 -0500453 syn!(TypeParamBound) => {|x| {
David Tolnayf2cfd722017-12-31 18:02:51 -0500454 let mut bounds = Punctuated::new();
David Tolnay56080682018-01-06 14:01:52 -0800455 bounds.push_value(x);
David Tolnayf2cfd722017-12-31 18:02:51 -0500456 bounds
David Tolnay660fd1f2017-12-31 01:52:57 -0500457 }}
David Tolnay7d38c7e2017-12-25 22:31:50 -0500458 ) >>
459 (TypeTraitObject {
460 dyn_token: dyn_token,
461 bounds: bounds,
462 })
463 ));
464 }
David Tolnay6414da72016-10-08 00:55:17 -0700465
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800466 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400467 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800468 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400469 // NOTE: rust-lang/rust#34511 includes discussion about whether or
470 // not + should be allowed in ImplTrait directly without ().
David Tolnayf2cfd722017-12-31 18:02:51 -0500471 elem: call!(Punctuated::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800472 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400473 impl_token: impl_,
474 bounds: elem,
475 })
476 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800477
478 fn description() -> Option<&'static str> {
479 Some("`impl Trait` type")
480 }
Alex Crichton954046c2017-05-30 21:49:42 -0700481 }
David Tolnayb79ee962016-09-04 09:39:20 -0700482
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800483 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400484 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800485 data: grouped!(syn!(Type)) >>
486 (TypeGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500487 group_token: data.0,
488 elem: Box::new(data.1),
Michael Layzell93c36282017-06-04 20:43:14 -0400489 })
490 ));
David Tolnay79777332018-01-07 10:04:42 -0800491
492 fn description() -> Option<&'static str> {
493 Some("type surrounded by invisible delimiters")
494 }
Michael Layzell93c36282017-06-04 20:43:14 -0400495 }
496
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800497 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500498 named!(parse -> Self, call!(Self::parse, false));
David Tolnay79777332018-01-07 10:04:42 -0800499
500 fn description() -> Option<&'static str> {
501 Some("parenthesized type")
502 }
David Tolnay0a169d42017-12-29 17:57:29 -0500503 }
504
505 impl TypeParen {
506 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800507 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500508 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800509 (TypeParen {
David Tolnay8875fca2017-12-31 13:52:37 -0500510 paren_token: data.0,
511 elem: Box::new(data.1),
Michael Layzell92639a52017-06-01 00:07:44 -0400512 })
513 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700514 }
David Tolnayb79ee962016-09-04 09:39:20 -0700515
Alex Crichton954046c2017-05-30 21:49:42 -0700516 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400517 named!(parse -> Self, do_parse!(
518 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700519 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800520 not!(punct!(::)) >>
521 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400522 (name, colon)
523 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800524 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400525 (BareFnArg {
526 name: name,
527 ty: ty,
528 })
529 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800530
531 fn description() -> Option<&'static str> {
532 Some("function type argument")
533 }
Alex Crichton954046c2017-05-30 21:49:42 -0700534 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700535
Alex Crichton23a15f62017-08-28 12:34:23 -0700536 impl Synom for BareFnArgName {
537 named!(parse -> Self, alt!(
538 map!(syn!(Ident), BareFnArgName::Named)
539 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800540 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700541 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800542
543 fn description() -> Option<&'static str> {
544 Some("function argument name")
545 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700546 }
547
Alex Crichton954046c2017-05-30 21:49:42 -0700548 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400549 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800550 extern_: keyword!(extern) >>
David Tolnayf01e37b2018-01-06 23:38:26 -0800551 name: option!(syn!(LitStr)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400552 (Abi {
553 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500554 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400555 })
556 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800557
558 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -0800559 Some("`extern` ABI qualifier")
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800560 }
Alex Crichton954046c2017-05-30 21:49:42 -0700561 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700562}
David Tolnay87d0b442016-09-04 11:52:12 -0700563
564#[cfg(feature = "printing")]
565mod printing {
566 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500567 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700568
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800569 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700570 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700571 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500572 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700573 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 }
575 }
576
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800577 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700578 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700579 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500580 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500582 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700583 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 }
585 }
586
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800587 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700589 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500590 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500591 Some(ref tok) => tok.to_tokens(tokens),
592 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700593 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400594 }
595 }
David Tolnay136aaa32017-12-29 02:37:36 -0500596 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }
598 }
599
David Tolnay0a89b4d2017-11-13 00:55:45 -0800600 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700601 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700602 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700603 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500604 self.mutability.to_tokens(tokens);
605 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 }
607 }
608
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800609 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700610 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500611 self.lifetimes.to_tokens(tokens);
612 self.unsafety.to_tokens(tokens);
613 self.abi.to_tokens(tokens);
614 self.fn_token.to_tokens(tokens);
615 self.paren_token.surround(tokens, |tokens| {
616 self.inputs.to_tokens(tokens);
617 if let Some(ref variadic) = self.variadic {
618 if !self.inputs.empty_or_trailing() {
619 let span = variadic.0[0];
620 <Token![,]>::new(span).to_tokens(tokens);
621 }
622 variadic.to_tokens(tokens);
623 }
624 });
625 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 }
627 }
628
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800629 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700630 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700631 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700632 }
633 }
634
David Tolnay05362582017-12-26 01:33:57 -0500635 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700636 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700637 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500638 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700639 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700640 }
641 }
642
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800643 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700645 PathTokens(&self.qself, &self.path).to_tokens(tokens);
646 }
647 }
648
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800649 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500651 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700652 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 }
654 }
655
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800656 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700658 self.impl_token.to_tokens(tokens);
659 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700660 }
661 }
662
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800663 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400664 fn to_tokens(&self, tokens: &mut Tokens) {
665 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500666 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400667 });
668 }
669 }
670
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800671 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700673 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500674 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700675 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700676 }
677 }
678
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800679 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -0700680 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700681 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700682 }
683 }
684
David Tolnay323279a2017-12-29 11:26:32 -0500685 impl ToTokens for TypeMacro {
686 fn to_tokens(&self, tokens: &mut Tokens) {
687 self.mac.to_tokens(tokens);
688 }
689 }
690
David Tolnay2ae520a2017-12-29 11:19:50 -0500691 impl ToTokens for TypeVerbatim {
692 fn to_tokens(&self, tokens: &mut Tokens) {
693 self.tts.to_tokens(tokens);
694 }
695 }
696
David Tolnayf93b90d2017-11-11 19:21:26 -0800697 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700698 fn to_tokens(&self, tokens: &mut Tokens) {
699 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800700 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500701 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700702 arrow.to_tokens(tokens);
703 ty.to_tokens(tokens);
704 }
David Tolnay87d0b442016-09-04 11:52:12 -0700705 }
706 }
707 }
708
David Tolnay62f374c2016-10-02 13:37:00 -0700709 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700710 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700711 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700712 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700713 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700714 }
715 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700716 }
717 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700718
Alex Crichton23a15f62017-08-28 12:34:23 -0700719 impl ToTokens for BareFnArgName {
720 fn to_tokens(&self, tokens: &mut Tokens) {
721 match *self {
722 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
723 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
724 }
725 }
726 }
727
David Tolnayb8d8ef52016-10-29 14:30:08 -0700728 impl ToTokens for Abi {
729 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700730 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500731 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700732 }
733 }
David Tolnay87d0b442016-09-04 11:52:12 -0700734}