blob: 2bf359abc93054094177b2cf49f9b3feb3eba106 [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! {
18 /// The different kinds of types recognized by the compiler
David Tolnayfd6bf5c2017-11-12 09:41:14 -080019 pub enum Type {
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// A variable-length array (`[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 }),
25 /// A fixed length array (`[T; n]`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080026 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050027 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050028 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050030 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 }),
32 /// A raw pointer (`*const T` or `*mut T`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080033 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080034 pub star_token: Token![*],
35 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050036 pub mutability: Option<Token![mut]>,
37 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070038 }),
39 /// A reference (`&'a T` or `&'a mut T`)
David Tolnay0a89b4d2017-11-13 00:55:45 -080040 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050043 pub mutability: Option<Token![mut]>,
44 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070045 }),
46 /// A bare function (e.g. `fn(usize) -> bool`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 pub BareFn(TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -050048 pub unsafety: Option<Token![unsafe]>,
49 pub abi: Option<Abi>,
50 pub fn_token: Token![fn],
51 pub lifetimes: Option<BoundLifetimes>,
52 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050053 pub inputs: Punctuated<BareFnArg, Token![,]>,
David Tolnaybe7a9592017-12-29 02:39:53 -050054 pub variadic: Option<Token![...]>,
55 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070056 }),
57 /// The never type (`!`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080058 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070060 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070061 /// A tuple (`(A, B, C, D, ...)`)
David Tolnay05362582017-12-26 01:33:57 -050062 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050063 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050064 pub elems: Punctuated<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }),
66 /// A path (`module::module::...::Type`), optionally
67 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
68 ///
Nika Layzellc08227a2017-12-04 16:30:17 -050069 /// Type arguments are stored in the Path itself
David Tolnayfd6bf5c2017-11-12 09:41:14 -080070 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub qself: Option<QSelf>,
72 pub path: Path,
73 }),
74 /// A trait object type `Bound1 + Bound2 + Bound3`
75 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080076 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -050077 pub dyn_token: Option<Token![dyn]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050078 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070079 }),
80 /// An `impl Bound1 + Bound2 + Bound3` type
81 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080082 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -080083 pub impl_token: Token![impl],
David Tolnayf2cfd722017-12-31 18:02:51 -050084 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
86 /// No-op; kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080087 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -050088 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050089 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 }),
Michael Layzell93c36282017-06-04 20:43:14 -040091 /// No-op: kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -050093 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -050094 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -040095 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -080096 /// TypeKind::Infer means the type should be inferred instead of it having been
Alex Crichton62a0a592017-05-22 13:58:53 -070097 /// specified. This can appear anywhere in a type.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080098 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 /// A macro in the type position.
David Tolnay323279a2017-12-29 11:26:32 -0500102 pub Macro(TypeMacro {
103 pub mac: Macro,
104 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500105 pub Verbatim(TypeVerbatim #manual_extra_traits {
106 pub tts: TokenStream,
107 }),
108 }
109}
110
111#[cfg(feature = "extra-traits")]
112impl Eq for TypeVerbatim {}
113
114#[cfg(feature = "extra-traits")]
115impl PartialEq for TypeVerbatim {
116 fn eq(&self, other: &Self) -> bool {
117 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
118 }
119}
120
121#[cfg(feature = "extra-traits")]
122impl Hash for TypeVerbatim {
123 fn hash<H>(&self, state: &mut H)
124 where
125 H: Hasher,
126 {
127 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 }
129}
130
131ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700132 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 pub extern_token: Token![extern],
David Tolnayd5125762017-12-29 02:42:17 -0500134 pub name: Option<Lit>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700135 }
136}
137
138ast_struct! {
139 /// An argument in a function type.
140 ///
141 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
142 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800143 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800144 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 }
146}
147
Alex Crichton23a15f62017-08-28 12:34:23 -0700148ast_enum! {
149 /// Names of arguments in the `BareFnArg` structure
150 pub enum BareFnArgName {
151 /// Argument with the provided name
152 Named(Ident),
153 /// Argument matched with `_`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800154 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700155 }
156}
Alex Crichton62a0a592017-05-22 13:58:53 -0700157
158ast_enum! {
David Tolnayf93b90d2017-11-11 19:21:26 -0800159 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 /// Return type is not specified.
161 ///
162 /// Functions default to `()` and
163 /// closures default to inference. Span points to where return
164 /// type would be inserted.
165 Default,
166 /// Everything else
David Tolnay4a3f59a2017-12-28 21:21:12 -0500167 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 }
David Tolnayb79ee962016-09-04 09:39:20 -0700169}
170
David Tolnay86eca752016-09-04 11:26:41 -0700171#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700172pub mod parsing {
173 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400174 use synom::Synom;
David Tolnay056de302018-01-05 14:29:05 -0800175 use path::parsing::qpath;
David Tolnayda4049b2016-09-04 10:59:23 -0700176
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800177 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400178 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700179
Alex Crichton954046c2017-05-30 21:49:42 -0700180 fn description() -> Option<&'static str> {
181 Some("type")
182 }
183 }
David Tolnay0047c712016-12-21 21:59:25 -0500184
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800185 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400186 /// In some positions, types may not contain the `+` character, to
187 /// disambiguate them. For example in the expression `1 as T`, T may not
188 /// contain a `+` character.
189 ///
190 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400191 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400192 }
193
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800194 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
195 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400196 |
David Tolnay05362582017-12-26 01:33:57 -0500197 // must be before TypeTuple
David Tolnay0a169d42017-12-29 17:57:29 -0500198 call!(TypeParen::parse, allow_plus) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400199 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500200 // must be before TypePath
David Tolnay323279a2017-12-29 11:26:32 -0500201 syn!(TypeMacro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400202 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500203 // must be before TypeTraitObject
204 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400205 |
David Tolnay0a169d42017-12-29 17:57:29 -0500206 // Don't try parsing more than one trait bound if we aren't allowing it.
207 // must be before TypeTuple
208 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
209 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800210 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400211 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800212 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400213 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800214 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400215 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800216 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400217 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800218 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400219 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800220 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400221 |
David Tolnay05362582017-12-26 01:33:57 -0500222 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400223 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800224 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700225 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800226 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400227 ));
228
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800229 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400230 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800231 brackets!(syn!(Type)),
David Tolnay8875fca2017-12-31 13:52:37 -0500232 |(b, ty)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500233 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400234 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700235 }
Michael Layzell92639a52017-06-01 00:07:44 -0400236 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800237
238 fn description() -> Option<&'static str> {
239 Some("slice type")
240 }
Alex Crichton954046c2017-05-30 21:49:42 -0700241 }
David Tolnayb79ee962016-09-04 09:39:20 -0700242
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800243 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400244 named!(parse -> Self, map!(
245 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800246 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800247 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400248 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700249 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400250 )),
David Tolnay8875fca2017-12-31 13:52:37 -0500251 |(brackets, (elem, semi, len))| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800252 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500253 elem: Box::new(elem),
254 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400255 bracket_token: brackets,
256 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700257 }
258 }
Michael Layzell92639a52017-06-01 00:07:44 -0400259 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800260
261 fn description() -> Option<&'static str> {
262 Some("array type")
263 }
Alex Crichton954046c2017-05-30 21:49:42 -0700264 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700265
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800266 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400267 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800268 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400269 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500270 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400271 |
David Tolnay24237fb2017-12-29 02:15:26 -0500272 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400273 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800274 target: call!(Type::without_plus) >>
275 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400276 const_token: mutability.1,
277 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500278 mutability: mutability.0,
279 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400280 })
281 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800282
283 fn description() -> Option<&'static str> {
284 Some("raw pointer type")
285 }
Alex Crichton954046c2017-05-30 21:49:42 -0700286 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700287
David Tolnay0a89b4d2017-11-13 00:55:45 -0800288 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400289 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800290 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400291 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500292 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400293 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800294 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800295 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400296 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500297 mutability: mutability,
298 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400299 and_token: amp,
300 })
301 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800302
303 fn description() -> Option<&'static str> {
304 Some("reference type")
305 }
Alex Crichton954046c2017-05-30 21:49:42 -0700306 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700307
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800308 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400309 named!(parse -> Self, do_parse!(
310 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500311 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400312 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800313 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400314 parens: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500315 inputs: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500316 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400317 (inputs, variadic)
318 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800319 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800320 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500321 unsafety: unsafety,
322 abi: abi,
323 lifetimes: lifetimes,
324 output: output,
David Tolnay8875fca2017-12-31 13:52:37 -0500325 variadic: (parens.1).1,
David Tolnaybe7a9592017-12-29 02:39:53 -0500326 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500327 paren_token: parens.0,
328 inputs: (parens.1).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400329 })
330 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800331
332 fn description() -> Option<&'static str> {
333 Some("`fn` type")
334 }
Alex Crichton954046c2017-05-30 21:49:42 -0700335 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700336
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800337 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400338 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800339 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800340 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400341 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800342
343 fn description() -> Option<&'static str> {
344 Some("never type: `!`")
345 }
Alex Crichton954046c2017-05-30 21:49:42 -0700346 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700347
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800348 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700349 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800351 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700352 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800353
354 fn description() -> Option<&'static str> {
355 Some("inferred type: `_`")
356 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700357 }
358
David Tolnay05362582017-12-26 01:33:57 -0500359 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400360 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500361 data: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500362 (TypeTuple {
David Tolnay8875fca2017-12-31 13:52:37 -0500363 paren_token: data.0,
364 elems: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400365 })
366 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800367
368 fn description() -> Option<&'static str> {
369 Some("tuple type")
370 }
Alex Crichton954046c2017-05-30 21:49:42 -0700371 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700372
David Tolnay323279a2017-12-29 11:26:32 -0500373 impl Synom for TypeMacro {
374 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
375 }
376
David Tolnay0a169d42017-12-29 17:57:29 -0500377 impl Synom for TypePath {
378 named!(parse -> Self, call!(Self::parse, false));
379 }
380
David Tolnay7d38c7e2017-12-25 22:31:50 -0500381 impl TypePath {
382 named!(parse(allow_plus: bool) -> Self, do_parse!(
383 qpath: qpath >>
David Tolnay660fd1f2017-12-31 01:52:57 -0500384 parenthesized: option!(cond_reduce!(
David Tolnay56080682018-01-06 14:01:52 -0800385 qpath.1.segments.last().unwrap().value().arguments.is_empty(),
David Tolnay660fd1f2017-12-31 01:52:57 -0500386 syn!(ParenthesizedGenericArguments)
387 )) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500388 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500389 ({
390 let (qself, mut path) = qpath;
David Tolnay660fd1f2017-12-31 01:52:57 -0500391 if let Some(parenthesized) = parenthesized {
David Tolnay7d38c7e2017-12-25 22:31:50 -0500392 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay56080682018-01-06 14:01:52 -0800393 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
David Tolnay7d38c7e2017-12-25 22:31:50 -0500394 }
395 TypePath { qself: qself, path: path }
396 })
397 ));
398 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700399
David Tolnayf93b90d2017-11-11 19:21:26 -0800400 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400401 named!(parse -> Self, alt!(
402 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800403 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800404 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500405 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400406 )
407 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800408 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400409 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800410
411 fn description() -> Option<&'static str> {
412 Some("return type")
413 }
Alex Crichton954046c2017-05-30 21:49:42 -0700414 }
415
David Tolnay0a169d42017-12-29 17:57:29 -0500416 impl Synom for TypeTraitObject {
417 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800418
419 fn description() -> Option<&'static str> {
420 Some("trait object type")
421 }
David Tolnay0a169d42017-12-29 17:57:29 -0500422 }
423
David Tolnay7d38c7e2017-12-25 22:31:50 -0500424 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500425 named!(pub without_plus -> Self, call!(Self::parse, false));
426
David Tolnay7d38c7e2017-12-25 22:31:50 -0500427 // Only allow multiple trait references if allow_plus is true.
428 named!(parse(allow_plus: bool) -> Self, do_parse!(
429 dyn_token: option!(keyword!(dyn)) >>
430 bounds: alt!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500431 cond_reduce!(allow_plus, Punctuated::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500432 |
David Tolnay660fd1f2017-12-31 01:52:57 -0500433 syn!(TypeParamBound) => {|x| {
David Tolnayf2cfd722017-12-31 18:02:51 -0500434 let mut bounds = Punctuated::new();
David Tolnay56080682018-01-06 14:01:52 -0800435 bounds.push_value(x);
David Tolnayf2cfd722017-12-31 18:02:51 -0500436 bounds
David Tolnay660fd1f2017-12-31 01:52:57 -0500437 }}
David Tolnay7d38c7e2017-12-25 22:31:50 -0500438 ) >>
439 (TypeTraitObject {
440 dyn_token: dyn_token,
441 bounds: bounds,
442 })
443 ));
444 }
David Tolnay6414da72016-10-08 00:55:17 -0700445
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800446 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400447 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800448 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400449 // NOTE: rust-lang/rust#34511 includes discussion about whether or
450 // not + should be allowed in ImplTrait directly without ().
David Tolnayf2cfd722017-12-31 18:02:51 -0500451 elem: call!(Punctuated::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800452 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400453 impl_token: impl_,
454 bounds: elem,
455 })
456 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800457
458 fn description() -> Option<&'static str> {
459 Some("`impl Trait` type")
460 }
Alex Crichton954046c2017-05-30 21:49:42 -0700461 }
David Tolnayb79ee962016-09-04 09:39:20 -0700462
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800463 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400464 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800465 data: grouped!(syn!(Type)) >>
466 (TypeGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500467 group_token: data.0,
468 elem: Box::new(data.1),
Michael Layzell93c36282017-06-04 20:43:14 -0400469 })
470 ));
471 }
472
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800473 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500474 named!(parse -> Self, call!(Self::parse, false));
475 }
476
477 impl TypeParen {
478 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800479 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500480 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800481 (TypeParen {
David Tolnay8875fca2017-12-31 13:52:37 -0500482 paren_token: data.0,
483 elem: Box::new(data.1),
Michael Layzell92639a52017-06-01 00:07:44 -0400484 })
485 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700486 }
David Tolnayb79ee962016-09-04 09:39:20 -0700487
Alex Crichton954046c2017-05-30 21:49:42 -0700488 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400489 named!(parse -> Self, do_parse!(
490 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700491 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800492 not!(punct!(::)) >>
493 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400494 (name, colon)
495 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800496 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400497 (BareFnArg {
498 name: name,
499 ty: ty,
500 })
501 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800502
503 fn description() -> Option<&'static str> {
504 Some("function type argument")
505 }
Alex Crichton954046c2017-05-30 21:49:42 -0700506 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700507
Alex Crichton23a15f62017-08-28 12:34:23 -0700508 impl Synom for BareFnArgName {
509 named!(parse -> Self, alt!(
510 map!(syn!(Ident), BareFnArgName::Named)
511 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800512 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700513 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800514
515 fn description() -> Option<&'static str> {
516 Some("function argument name")
517 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700518 }
519
Alex Crichton954046c2017-05-30 21:49:42 -0700520 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400521 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800522 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400523 // TODO: this parses all literals, not just strings
524 name: option!(syn!(Lit)) >>
525 (Abi {
526 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500527 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400528 })
529 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800530
531 fn description() -> Option<&'static str> {
532 Some("ABI qualifier")
533 }
Alex Crichton954046c2017-05-30 21:49:42 -0700534 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700535}
David Tolnay87d0b442016-09-04 11:52:12 -0700536
537#[cfg(feature = "printing")]
538mod printing {
539 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500540 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700541
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800542 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700543 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700544 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500545 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 }
548 }
549
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800550 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700551 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700552 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500553 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700554 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500555 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 }
558 }
559
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800560 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700561 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700562 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500563 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500564 Some(ref tok) => tok.to_tokens(tokens),
565 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700566 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400567 }
568 }
David Tolnay136aaa32017-12-29 02:37:36 -0500569 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700570 }
571 }
572
David Tolnay0a89b4d2017-11-13 00:55:45 -0800573 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700575 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700576 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500577 self.mutability.to_tokens(tokens);
578 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700579 }
580 }
581
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800582 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500584 self.lifetimes.to_tokens(tokens);
585 self.unsafety.to_tokens(tokens);
586 self.abi.to_tokens(tokens);
587 self.fn_token.to_tokens(tokens);
588 self.paren_token.surround(tokens, |tokens| {
589 self.inputs.to_tokens(tokens);
590 if let Some(ref variadic) = self.variadic {
591 if !self.inputs.empty_or_trailing() {
592 let span = variadic.0[0];
593 <Token![,]>::new(span).to_tokens(tokens);
594 }
595 variadic.to_tokens(tokens);
596 }
597 });
598 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700599 }
600 }
601
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800602 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700603 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 }
606 }
607
David Tolnay05362582017-12-26 01:33:57 -0500608 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700610 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500611 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 }
614 }
615
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800616 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700617 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 PathTokens(&self.qself, &self.path).to_tokens(tokens);
619 }
620 }
621
622 impl<'a> ToTokens for PathTokens<'a> {
623 fn to_tokens(&self, tokens: &mut Tokens) {
624 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700626 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700627 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700628 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700629 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400630
631 // XXX: Gross.
632 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
633 self.1.segments.len() - 1
634 } else {
635 qself.position
636 };
David Tolnay56080682018-01-06 14:01:52 -0800637 let mut segments = self.1.segments.pairs();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400638 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700639 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700640 self.1.leading_colon.to_tokens(tokens);
David Tolnay6eff4da2018-01-01 20:27:45 -0800641 for (i, segment) in segments.by_ref().take(pos).enumerate() {
David Tolnay570695e2017-06-03 16:15:13 -0700642 if i + 1 == pos {
David Tolnay56080682018-01-06 14:01:52 -0800643 segment.value().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700644 qself.gt_token.to_tokens(tokens);
David Tolnayf2cfd722017-12-31 18:02:51 -0500645 segment.punct().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700646 } else {
647 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700648 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700649 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700650 } else {
651 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700652 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 }
David Tolnay570695e2017-06-03 16:15:13 -0700654 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 segment.to_tokens(tokens);
656 }
657 }
658 }
659
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800660 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700661 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500662 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700663 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700664 }
665 }
666
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800667 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700668 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700669 self.impl_token.to_tokens(tokens);
670 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700671 }
672 }
673
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800674 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400675 fn to_tokens(&self, tokens: &mut Tokens) {
676 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500677 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400678 });
679 }
680 }
681
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800682 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700683 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700684 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500685 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700686 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 }
688 }
689
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800690 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -0700691 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700692 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700693 }
694 }
695
David Tolnay323279a2017-12-29 11:26:32 -0500696 impl ToTokens for TypeMacro {
697 fn to_tokens(&self, tokens: &mut Tokens) {
698 self.mac.to_tokens(tokens);
699 }
700 }
701
David Tolnay2ae520a2017-12-29 11:19:50 -0500702 impl ToTokens for TypeVerbatim {
703 fn to_tokens(&self, tokens: &mut Tokens) {
704 self.tts.to_tokens(tokens);
705 }
706 }
707
David Tolnayf93b90d2017-11-11 19:21:26 -0800708 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700709 fn to_tokens(&self, tokens: &mut Tokens) {
710 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800711 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500712 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700713 arrow.to_tokens(tokens);
714 ty.to_tokens(tokens);
715 }
David Tolnay87d0b442016-09-04 11:52:12 -0700716 }
717 }
718 }
719
David Tolnay62f374c2016-10-02 13:37:00 -0700720 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700721 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700722 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700723 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700724 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700725 }
726 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700727 }
728 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700729
Alex Crichton23a15f62017-08-28 12:34:23 -0700730 impl ToTokens for BareFnArgName {
731 fn to_tokens(&self, tokens: &mut Tokens) {
732 match *self {
733 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
734 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
735 }
736 }
737 }
738
David Tolnayb8d8ef52016-10-29 14:30:08 -0700739 impl ToTokens for Abi {
740 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700741 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500742 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700743 }
744 }
David Tolnay87d0b442016-09-04 11:52:12 -0700745}