blob: 88575a80f0f61ff582d96168d4ca65b6de34f800 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07002use super::*;
David Tolnay2ae520a2017-12-29 11:19:50 -05003use proc_macro2::TokenStream;
4#[cfg(feature = "extra-traits")]
5use std::hash::{Hash, Hasher};
6#[cfg(feature = "extra-traits")]
7use mac::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -07008
Alex Crichton62a0a592017-05-22 13:58:53 -07009ast_enum_of_structs! {
10 /// The different kinds of types recognized by the compiler
David Tolnayfd6bf5c2017-11-12 09:41:14 -080011 pub enum Type {
Alex Crichton62a0a592017-05-22 13:58:53 -070012 /// A variable-length array (`[T]`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080013 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -050014 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050015 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070016 }),
17 /// A fixed length array (`[T; n]`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080018 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050019 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050020 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050022 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070023 }),
24 /// A raw pointer (`*const T` or `*mut T`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080025 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080026 pub star_token: Token![*],
27 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050028 pub mutability: Option<Token![mut]>,
29 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
31 /// A reference (`&'a T` or `&'a mut T`)
David Tolnay0a89b4d2017-11-13 00:55:45 -080032 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050035 pub mutability: Option<Token![mut]>,
36 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070037 }),
38 /// A bare function (e.g. `fn(usize) -> bool`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080039 pub BareFn(TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -050040 pub unsafety: Option<Token![unsafe]>,
41 pub abi: Option<Abi>,
42 pub fn_token: Token![fn],
43 pub lifetimes: Option<BoundLifetimes>,
44 pub paren_token: token::Paren,
45 pub inputs: Delimited<BareFnArg, Token![,]>,
46 pub variadic: Option<Token![...]>,
47 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070048 }),
49 /// The never type (`!`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080050 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070053 /// A tuple (`(A, B, C, D, ...)`)
David Tolnay05362582017-12-26 01:33:57 -050054 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050055 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050056 pub elems: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// A path (`module::module::...::Type`), optionally
59 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
60 ///
Nika Layzellc08227a2017-12-04 16:30:17 -050061 /// Type arguments are stored in the Path itself
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -070063 pub qself: Option<QSelf>,
64 pub path: Path,
65 }),
66 /// A trait object type `Bound1 + Bound2 + Bound3`
67 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080068 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -050069 pub dyn_token: Option<Token![dyn]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080070 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 }),
72 /// An `impl Bound1 + Bound2 + Bound3` type
73 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080074 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -080075 pub impl_token: Token![impl],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080076 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 }),
78 /// No-op; kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080079 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -050080 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050081 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070082 }),
Michael Layzell93c36282017-06-04 20:43:14 -040083 /// No-op: kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080084 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -050085 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -050086 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -040087 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -080088 /// TypeKind::Infer means the type should be inferred instead of it having been
Alex Crichton62a0a592017-05-22 13:58:53 -070089 /// specified. This can appear anywhere in a type.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080090 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070092 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070093 /// A macro in the type position.
David Tolnay323279a2017-12-29 11:26:32 -050094 pub Macro(TypeMacro {
95 pub mac: Macro,
96 }),
David Tolnay2ae520a2017-12-29 11:19:50 -050097 pub Verbatim(TypeVerbatim #manual_extra_traits {
98 pub tts: TokenStream,
99 }),
100 }
101}
102
103#[cfg(feature = "extra-traits")]
104impl Eq for TypeVerbatim {}
105
106#[cfg(feature = "extra-traits")]
107impl PartialEq for TypeVerbatim {
108 fn eq(&self, other: &Self) -> bool {
109 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
110 }
111}
112
113#[cfg(feature = "extra-traits")]
114impl Hash for TypeVerbatim {
115 fn hash<H>(&self, state: &mut H)
116 where
117 H: Hasher,
118 {
119 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700120 }
121}
122
123ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 /// A "Path" is essentially Rust's notion of a name.
David Tolnayb79ee962016-09-04 09:39:20 -0700125 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 /// It's represented as a sequence of identifiers,
127 /// along with a bunch of supporting information.
128 ///
129 /// E.g. `std::cmp::PartialEq`
130 pub struct Path {
131 /// A `::foo` path, is relative to the crate root rather than current
132 /// module (like paths in an import).
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 pub leading_colon: Option<Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 /// The segments in the path: the things separated by `::`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 pub segments: Delimited<PathSegment, Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }
David Tolnayb79ee962016-09-04 09:39:20 -0700137}
138
David Tolnay570695e2017-06-03 16:15:13 -0700139impl Path {
140 pub fn global(&self) -> bool {
141 self.leading_colon.is_some()
142 }
143}
144
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700145#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400146#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
147#[cfg_attr(feature = "clone-impls", derive(Clone))]
148pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700149
David Tolnaydaaf7742016-10-03 11:11:43 -0700150impl<T> From<T> for Path
David Tolnay51382052017-12-27 13:46:21 -0500151where
152 T: Into<PathSegment>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700153{
David Tolnay84aa0752016-10-02 23:01:13 -0700154 fn from(segment: T) -> Self {
155 Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 leading_colon: None,
157 segments: vec![(segment.into(), None)].into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700158 }
159 }
160}
161
Alex Crichton62a0a592017-05-22 13:58:53 -0700162ast_struct! {
163 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
164 ///
165 /// E.g. `std`, `String` or `Box<T>`
166 pub struct PathSegment {
167 /// The identifier portion of this path segment.
168 pub ident: Ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500169 /// Type/lifetime arguments attached to this path. They come in
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
171 /// this is more than just simple syntactic sugar; the use of
172 /// parens affects the region binding rules, so we preserve the
173 /// distinction.
Nika Layzellc08227a2017-12-04 16:30:17 -0500174 pub arguments: PathArguments,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }
David Tolnayb79ee962016-09-04 09:39:20 -0700176}
177
David Tolnaydaaf7742016-10-03 11:11:43 -0700178impl<T> From<T> for PathSegment
David Tolnay51382052017-12-27 13:46:21 -0500179where
180 T: Into<Ident>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700181{
David Tolnay84aa0752016-10-02 23:01:13 -0700182 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700183 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700184 ident: ident.into(),
Nika Layzellc08227a2017-12-04 16:30:17 -0500185 arguments: PathArguments::None,
David Tolnayb79ee962016-09-04 09:39:20 -0700186 }
187 }
188}
189
Alex Crichton62a0a592017-05-22 13:58:53 -0700190ast_enum! {
Nika Layzellc08227a2017-12-04 16:30:17 -0500191 /// Arguments of a path segment.
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 ///
193 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
Nika Layzellc08227a2017-12-04 16:30:17 -0500194 pub enum PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700195 None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500197 AngleBracketed(AngleBracketedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500199 Parenthesized(ParenthesizedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 }
David Tolnayb79ee962016-09-04 09:39:20 -0700201}
202
Nika Layzellc08227a2017-12-04 16:30:17 -0500203impl Default for PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700204 fn default() -> Self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500205 PathArguments::None
David Tolnayb79ee962016-09-04 09:39:20 -0700206 }
David Tolnay570695e2017-06-03 16:15:13 -0700207}
David Tolnay5332d4b2016-10-30 14:25:22 -0700208
Nika Layzellc08227a2017-12-04 16:30:17 -0500209impl PathArguments {
David Tolnay5332d4b2016-10-30 14:25:22 -0700210 pub fn is_empty(&self) -> bool {
211 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500212 PathArguments::None => true,
213 PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(),
214 PathArguments::Parenthesized(_) => false,
David Tolnay5332d4b2016-10-30 14:25:22 -0700215 }
216 }
David Tolnayb79ee962016-09-04 09:39:20 -0700217}
218
Nika Layzell357885a2017-12-04 15:47:07 -0500219ast_enum! {
220 /// A individual generic argument, like `'a`, `T`, or `Item=T`.
Nika Layzellc08227a2017-12-04 16:30:17 -0500221 pub enum GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500222 /// The lifetime parameters for this path segment.
223 Lifetime(Lifetime),
224 /// The type parameters for this path segment, if present.
225 Type(Type),
226 /// Bindings (equality constraints) on associated types, if present.
227 ///
228 /// E.g., `Foo<A=Bar>`.
David Tolnay506e43a2017-12-29 11:34:36 -0500229 Binding(Binding),
Nika Layzellc680e612017-12-04 19:07:20 -0500230 /// Const expression. Must be inside of a block.
231 ///
232 /// NOTE: Identity expressions are represented as Type arguments, as
233 /// they are indistinguishable syntactically.
Nika Layzellce37f332017-12-05 12:01:22 -0500234 Const(Expr),
Nika Layzell357885a2017-12-04 15:47:07 -0500235 }
236}
237
Alex Crichton62a0a592017-05-22 13:58:53 -0700238ast_struct! {
239 /// A path like `Foo<'a, T>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500240 pub struct AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500241 pub colon2_token: Option<Token![::]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800242 pub lt_token: Token![<],
Nika Layzellc08227a2017-12-04 16:30:17 -0500243 pub args: Delimited<GenericArgument, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 }
246}
247
248ast_struct! {
249 /// Bind a type to an associated type: `A=Foo`.
David Tolnay506e43a2017-12-29 11:34:36 -0500250 pub struct Binding {
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800252 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800253 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 }
255}
256
Alex Crichton62a0a592017-05-22 13:58:53 -0700257ast_struct! {
258 /// A path like `Foo(A,B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500259 pub struct ParenthesizedGenericArguments {
David Tolnay32954ef2017-12-26 22:43:16 -0500260 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 /// `(A, B)`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800262 pub inputs: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700263 /// `C`
David Tolnayf93b90d2017-11-11 19:21:26 -0800264 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 }
266}
267
268ast_struct! {
269 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700270 /// The `for<'a>` in `for<'a> Foo<&'a T>`
271 pub bound_lifetimes: Option<BoundLifetimes>,
David Tolnay7d38c7e2017-12-25 22:31:50 -0500272 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 pub trait_ref: Path,
274 }
275}
276
277ast_struct! {
278 /// The explicit Self type in a "qualified path". The actual
279 /// path, including the trait and the associated item, is stored
280 /// separately. `position` represents the index of the associated
281 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700282 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500283 /// ```text
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 /// <Vec<T> as a::b::Trait>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500285 /// ^~~~~~ ~~~~~~~~~~~~~~^
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700287 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 /// <Vec<T>>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500289 /// ^~~~~~ ^
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 /// ty position = 0
291 /// ```
292 pub struct QSelf {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800293 pub lt_token: Token![<],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800294 pub ty: Box<Type>,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400295 pub position: usize,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800296 pub as_token: Option<Token![as]>,
297 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }
299}
300
301ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700302 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800303 pub extern_token: Token![extern],
David Tolnayd5125762017-12-29 02:42:17 -0500304 pub name: Option<Lit>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 }
306}
307
308ast_struct! {
309 /// An argument in a function type.
310 ///
311 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
312 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800313 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800314 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700315 }
316}
317
Alex Crichton23a15f62017-08-28 12:34:23 -0700318ast_enum! {
319 /// Names of arguments in the `BareFnArg` structure
320 pub enum BareFnArgName {
321 /// Argument with the provided name
322 Named(Ident),
323 /// Argument matched with `_`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800324 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700325 }
326}
Alex Crichton62a0a592017-05-22 13:58:53 -0700327
328ast_enum! {
David Tolnayf93b90d2017-11-11 19:21:26 -0800329 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 /// Return type is not specified.
331 ///
332 /// Functions default to `()` and
333 /// closures default to inference. Span points to where return
334 /// type would be inserted.
335 Default,
336 /// Everything else
David Tolnay4a3f59a2017-12-28 21:21:12 -0500337 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 }
David Tolnayb79ee962016-09-04 09:39:20 -0700339}
340
David Tolnay86eca752016-09-04 11:26:41 -0700341#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700342pub mod parsing {
343 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400344 use synom::Synom;
David Tolnayda4049b2016-09-04 10:59:23 -0700345
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800346 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400347 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700348
Alex Crichton954046c2017-05-30 21:49:42 -0700349 fn description() -> Option<&'static str> {
350 Some("type")
351 }
352 }
David Tolnay0047c712016-12-21 21:59:25 -0500353
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800354 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400355 /// In some positions, types may not contain the `+` character, to
356 /// disambiguate them. For example in the expression `1 as T`, T may not
357 /// contain a `+` character.
358 ///
359 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400360 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400361 }
362
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800363 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
364 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400365 |
David Tolnay05362582017-12-26 01:33:57 -0500366 // must be before TypeTuple
David Tolnay0a169d42017-12-29 17:57:29 -0500367 call!(TypeParen::parse, allow_plus) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400368 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500369 // must be before TypePath
David Tolnay323279a2017-12-29 11:26:32 -0500370 syn!(TypeMacro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400371 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500372 // must be before TypeTraitObject
373 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400374 |
David Tolnay0a169d42017-12-29 17:57:29 -0500375 // Don't try parsing more than one trait bound if we aren't allowing it.
376 // must be before TypeTuple
377 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
378 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800379 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400380 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800381 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400382 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800383 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400384 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800385 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400386 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800387 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400388 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800389 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400390 |
David Tolnay05362582017-12-26 01:33:57 -0500391 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400392 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800393 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700394 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800395 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400396 ));
397
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800398 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400399 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800400 brackets!(syn!(Type)),
401 |(ty, b)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500402 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400403 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700404 }
Michael Layzell92639a52017-06-01 00:07:44 -0400405 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700406 }
David Tolnayb79ee962016-09-04 09:39:20 -0700407
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800408 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400409 named!(parse -> Self, map!(
410 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800411 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400413 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700414 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400415 )),
416 |((elem, semi, len), brackets)| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800417 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500418 elem: Box::new(elem),
419 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400420 bracket_token: brackets,
421 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700422 }
423 }
Michael Layzell92639a52017-06-01 00:07:44 -0400424 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700425 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700426
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800427 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400428 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400430 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500431 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400432 |
David Tolnay24237fb2017-12-29 02:15:26 -0500433 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400434 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800435 target: call!(Type::without_plus) >>
436 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400437 const_token: mutability.1,
438 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500439 mutability: mutability.0,
440 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400441 })
442 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700443 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700444
David Tolnay0a89b4d2017-11-13 00:55:45 -0800445 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400446 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800447 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400448 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500449 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400450 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800451 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800452 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400453 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500454 mutability: mutability,
455 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400456 and_token: amp,
457 })
458 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700459 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700460
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800461 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400462 named!(parse -> Self, do_parse!(
463 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500464 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400465 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800466 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400467 parens: parens!(do_parse!(
468 inputs: call!(Delimited::parse_terminated) >>
469 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800470 punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400471 (inputs, variadic)
472 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800473 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800474 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500475 unsafety: unsafety,
476 abi: abi,
477 lifetimes: lifetimes,
478 output: output,
479 variadic: (parens.0).1,
480 fn_token: fn_,
481 paren_token: parens.1,
482 inputs: (parens.0).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400483 })
484 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700485 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700486
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800487 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400488 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800489 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800490 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400491 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700492 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700493
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800494 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700495 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800496 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800497 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700498 ));
499 }
500
David Tolnay05362582017-12-26 01:33:57 -0500501 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400502 named!(parse -> Self, do_parse!(
503 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -0500504 (TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -0500505 elems: data.0,
Michael Layzell92639a52017-06-01 00:07:44 -0400506 paren_token: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400507 })
508 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700509 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700510
David Tolnay323279a2017-12-29 11:26:32 -0500511 impl Synom for TypeMacro {
512 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
513 }
514
David Tolnay0a169d42017-12-29 17:57:29 -0500515 impl Synom for TypePath {
516 named!(parse -> Self, call!(Self::parse, false));
517 }
518
David Tolnay7d38c7e2017-12-25 22:31:50 -0500519 impl TypePath {
520 named!(parse(allow_plus: bool) -> Self, do_parse!(
521 qpath: qpath >>
522 parenthesized: cond!(
523 qpath.1.segments.last().unwrap().item().arguments.is_empty(),
524 option!(syn!(ParenthesizedGenericArguments))
525 ) >>
526 cond!(allow_plus, not!(peek!(punct!(+)))) >>
527 ({
528 let (qself, mut path) = qpath;
529 if let Some(Some(parenthesized)) = parenthesized {
530 let parenthesized = PathArguments::Parenthesized(parenthesized);
531 path.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
532 }
533 TypePath { qself: qself, path: path }
534 })
535 ));
536 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700537
David Tolnay9636c052016-10-02 17:11:17 -0700538 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700539 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700540 |
541 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800543 this: syn!(Type) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500544 path: option!(tuple!(keyword!(as), syn!(Path))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800545 gt: punct!(>) >>
546 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700547 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700548 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400549 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700550 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700551 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700552 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700553 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700554 }
Alex Crichton954046c2017-05-30 21:49:42 -0700555 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 path.segments.push(item);
557 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400558 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700559 }
560 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400561 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700562 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700563 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700564 })
David Tolnay9636c052016-10-02 17:11:17 -0700565 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700566 };
567 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700568 lt_token: lt,
569 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400571 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700572 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700573 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700574 })
575 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700576 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800577 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700578 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700579
Nika Layzellc08227a2017-12-04 16:30:17 -0500580 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400581 named!(parse -> Self, do_parse!(
582 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800583 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500584 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400585 paren_token: data.1,
586 inputs: data.0,
587 output: output,
588 })
589 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700590 }
591
David Tolnayf93b90d2017-11-11 19:21:26 -0800592 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400593 named!(parse -> Self, alt!(
594 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800596 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500597 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400598 )
599 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800600 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400601 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700602 }
603
David Tolnay0a169d42017-12-29 17:57:29 -0500604 impl Synom for TypeTraitObject {
605 named!(parse -> Self, call!(Self::parse, true));
606 }
607
David Tolnay7d38c7e2017-12-25 22:31:50 -0500608 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500609 named!(pub without_plus -> Self, call!(Self::parse, false));
610
David Tolnay7d38c7e2017-12-25 22:31:50 -0500611 // Only allow multiple trait references if allow_plus is true.
612 named!(parse(allow_plus: bool) -> Self, do_parse!(
613 dyn_token: option!(keyword!(dyn)) >>
614 bounds: alt!(
615 cond_reduce!(allow_plus, call!(Delimited::parse_terminated_nonempty))
616 |
617 syn!(TypeParamBound) => { |x| vec![x].into() }
618 ) >>
619 (TypeTraitObject {
620 dyn_token: dyn_token,
621 bounds: bounds,
622 })
623 ));
624 }
David Tolnay6414da72016-10-08 00:55:17 -0700625
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800626 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400627 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400629 // NOTE: rust-lang/rust#34511 includes discussion about whether or
630 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500631 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800632 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400633 impl_token: impl_,
634 bounds: elem,
635 })
636 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700637 }
David Tolnayb79ee962016-09-04 09:39:20 -0700638
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800639 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400640 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800641 data: grouped!(syn!(Type)) >>
642 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400643 group_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500644 elem: Box::new(data.0),
Michael Layzell93c36282017-06-04 20:43:14 -0400645 })
646 ));
647 }
648
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800649 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500650 named!(parse -> Self, call!(Self::parse, false));
651 }
652
653 impl TypeParen {
654 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800655 data: parens!(syn!(Type)) >>
David Tolnay0a169d42017-12-29 17:57:29 -0500656 cond!(allow_plus, not!(peek!(punct!(+)))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800657 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400658 paren_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500659 elem: Box::new(data.0),
Michael Layzell92639a52017-06-01 00:07:44 -0400660 })
661 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700662 }
David Tolnayb79ee962016-09-04 09:39:20 -0700663
Alex Crichton954046c2017-05-30 21:49:42 -0700664 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400665 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666 colon: option!(punct!(::)) >>
David Tolnaye45b59f2017-12-25 18:44:49 -0500667 segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >>
668 cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400669 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700670 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400671 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400672 })
673 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700674
675 fn description() -> Option<&'static str> {
676 Some("path")
677 }
Alex Crichton954046c2017-05-30 21:49:42 -0700678 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700679
Nika Layzellc680e612017-12-04 19:07:20 -0500680 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500681 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500682 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500683 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500684 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500685 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500686 |
David Tolnay506e43a2017-12-29 11:34:36 -0500687 syn!(Binding) => { GenericArgument::Binding }
Nika Layzell357885a2017-12-04 15:47:07 -0500688 ));
689 }
690
Nika Layzellc680e612017-12-04 19:07:20 -0500691 #[cfg(feature = "full")]
692 impl Synom for GenericArgument {
693 named!(parse -> Self, alt!(
694 call!(ty_no_eq_after) => { GenericArgument::Type }
695 |
696 syn!(Lifetime) => { GenericArgument::Lifetime }
697 |
David Tolnay506e43a2017-12-29 11:34:36 -0500698 syn!(Binding) => { GenericArgument::Binding }
Nika Layzellc680e612017-12-04 19:07:20 -0500699 |
David Tolnay8c91b882017-12-28 23:04:32 -0500700 syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) }
Nika Layzellce37f332017-12-05 12:01:22 -0500701 |
David Tolnay8c91b882017-12-28 23:04:32 -0500702 syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500703 ));
704 }
705
Nika Layzellc08227a2017-12-04 16:30:17 -0500706 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500707 named!(parse -> Self, do_parse!(
David Tolnay2d4e08a2017-12-28 23:54:07 -0500708 colon2: option!(punct!(::)) >>
Nika Layzell357885a2017-12-04 15:47:07 -0500709 lt: punct!(<) >>
710 args: call!(Delimited::parse_terminated) >>
711 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500712 (AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500713 colon2_token: colon2,
Nika Layzell357885a2017-12-04 15:47:07 -0500714 lt_token: lt,
715 args: args,
716 gt_token: gt,
717 })
718 ));
719 }
720
Alex Crichton954046c2017-05-30 21:49:42 -0700721 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400722 named!(parse -> Self, alt!(
723 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700724 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500725 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400726 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700727 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500728 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400729 })
730 )
731 |
732 mod_style_path_segment
733 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700734 }
David Tolnay570695e2017-06-03 16:15:13 -0700735
David Tolnayd60cfec2017-12-29 00:21:38 -0500736 named!(pub ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700737
Alex Crichton954046c2017-05-30 21:49:42 -0700738 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400739 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400741 segments: call!(Delimited::parse_separated_nonempty_with,
742 mod_style_path_segment) >>
743 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700744 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400745 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400746 })
747 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700748 }
Arnavionf2dada12017-04-20 23:55:20 -0700749
750 named!(mod_style_path_segment -> PathSegment, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500751 syn!(Ident) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700752 |
David Tolnay5f332a92017-12-26 00:42:45 -0500753 keyword!(super) => { Into::into }
754 |
755 keyword!(self) => { Into::into }
756 |
757 keyword!(Self) => { Into::into }
758 |
759 keyword!(crate) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700760 ));
761
David Tolnay506e43a2017-12-29 11:34:36 -0500762 impl Synom for Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400763 named!(parse -> Self, do_parse!(
764 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800765 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800766 ty: syn!(Type) >>
David Tolnay506e43a2017-12-29 11:34:36 -0500767 (Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400768 ident: id,
769 eq_token: eq,
770 ty: ty,
771 })
772 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700773 }
774
775 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400776 named!(parse -> Self, do_parse!(
777 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
778 trait_ref: syn!(Path) >>
779 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500780 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
781 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400782 )) >>
783 ({
784 let mut trait_ref = trait_ref;
785 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500786 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400787 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500788 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400789 }
790 PolyTraitRef {
791 bound_lifetimes: bound_lifetimes,
792 trait_ref: trait_ref,
793 }
794 })
795 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700796 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700797
Alex Crichton954046c2017-05-30 21:49:42 -0700798 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400799 named!(parse -> Self, do_parse!(
800 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700801 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800802 not!(punct!(::)) >>
803 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400804 (name, colon)
805 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800806 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400807 (BareFnArg {
808 name: name,
809 ty: ty,
810 })
811 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700812 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700813
Alex Crichton23a15f62017-08-28 12:34:23 -0700814 impl Synom for BareFnArgName {
815 named!(parse -> Self, alt!(
816 map!(syn!(Ident), BareFnArgName::Named)
817 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800818 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700819 ));
820 }
821
Alex Crichton954046c2017-05-30 21:49:42 -0700822 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400823 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800824 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400825 // TODO: this parses all literals, not just strings
826 name: option!(syn!(Lit)) >>
827 (Abi {
828 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500829 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400830 })
831 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700832 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700833}
David Tolnay87d0b442016-09-04 11:52:12 -0700834
835#[cfg(feature = "printing")]
836mod printing {
837 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500838 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700839
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800840 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700841 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700842 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500843 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700844 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700845 }
846 }
847
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800848 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700849 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700850 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500851 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700852 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500853 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700855 }
856 }
857
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800858 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700859 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500861 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500862 Some(ref tok) => tok.to_tokens(tokens),
863 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700864 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400865 }
866 }
David Tolnay136aaa32017-12-29 02:37:36 -0500867 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700868 }
869 }
870
David Tolnay0a89b4d2017-11-13 00:55:45 -0800871 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700872 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700874 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500875 self.mutability.to_tokens(tokens);
876 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700877 }
878 }
879
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800880 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700881 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500882 self.lifetimes.to_tokens(tokens);
883 self.unsafety.to_tokens(tokens);
884 self.abi.to_tokens(tokens);
885 self.fn_token.to_tokens(tokens);
886 self.paren_token.surround(tokens, |tokens| {
887 self.inputs.to_tokens(tokens);
888 if let Some(ref variadic) = self.variadic {
889 if !self.inputs.empty_or_trailing() {
890 let span = variadic.0[0];
891 <Token![,]>::new(span).to_tokens(tokens);
892 }
893 variadic.to_tokens(tokens);
894 }
895 });
896 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700897 }
898 }
899
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800900 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700901 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700902 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700903 }
904 }
905
David Tolnay05362582017-12-26 01:33:57 -0500906 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700907 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500909 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700910 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700911 }
912 }
913
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800914 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700915 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 PathTokens(&self.qself, &self.path).to_tokens(tokens);
917 }
918 }
919
920 impl<'a> ToTokens for PathTokens<'a> {
921 fn to_tokens(&self, tokens: &mut Tokens) {
922 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700923 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700924 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700925 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700926 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700927 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400928
929 // XXX: Gross.
930 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
931 self.1.segments.len() - 1
932 } else {
933 qself.position
934 };
David Tolnay570695e2017-06-03 16:15:13 -0700935 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400936 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700937 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700938 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700939 for (i, segment) in (&mut segments).take(pos).enumerate() {
940 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700941 segment.item().to_tokens(tokens);
942 qself.gt_token.to_tokens(tokens);
943 segment.delimiter().to_tokens(tokens);
944 } else {
945 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700946 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700947 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 } else {
949 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700950 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 }
David Tolnay570695e2017-06-03 16:15:13 -0700952 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700953 segment.to_tokens(tokens);
954 }
955 }
956 }
957
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800958 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700959 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500960 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700961 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 }
963 }
964
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800965 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700966 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700967 self.impl_token.to_tokens(tokens);
968 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700969 }
970 }
971
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800972 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400973 fn to_tokens(&self, tokens: &mut Tokens) {
974 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500975 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400976 });
977 }
978 }
979
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800980 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700982 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500983 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700984 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700985 }
986 }
987
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800988 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -0700989 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700990 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700991 }
992 }
993
David Tolnay323279a2017-12-29 11:26:32 -0500994 impl ToTokens for TypeMacro {
995 fn to_tokens(&self, tokens: &mut Tokens) {
996 self.mac.to_tokens(tokens);
997 }
998 }
999
David Tolnay2ae520a2017-12-29 11:19:50 -05001000 impl ToTokens for TypeVerbatim {
1001 fn to_tokens(&self, tokens: &mut Tokens) {
1002 self.tts.to_tokens(tokens);
1003 }
1004 }
1005
David Tolnay87d0b442016-09-04 11:52:12 -07001006 impl ToTokens for Path {
1007 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 self.leading_colon.to_tokens(tokens);
1009 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001010 }
1011 }
1012
1013 impl ToTokens for PathSegment {
1014 fn to_tokens(&self, tokens: &mut Tokens) {
1015 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001016 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001017 }
1018 }
1019
Nika Layzellc08227a2017-12-04 16:30:17 -05001020 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001021 fn to_tokens(&self, tokens: &mut Tokens) {
1022 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001023 PathArguments::None => {}
1024 PathArguments::AngleBracketed(ref arguments) => {
1025 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001026 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001027 PathArguments::Parenthesized(ref arguments) => {
1028 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001029 }
1030 }
1031 }
1032 }
1033
Nika Layzellc08227a2017-12-04 16:30:17 -05001034 impl ToTokens for GenericArgument {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001035 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Nika Layzell357885a2017-12-04 15:47:07 -05001036 fn to_tokens(&self, tokens: &mut Tokens) {
1037 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001038 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1039 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
David Tolnay506e43a2017-12-29 11:34:36 -05001040 GenericArgument::Binding(ref tb) => tb.to_tokens(tokens),
David Tolnay8c91b882017-12-28 23:04:32 -05001041 GenericArgument::Const(ref e) => match *e {
1042 Expr::Lit(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001043
1044 // NOTE: We should probably support parsing blocks with only
1045 // expressions in them without the full feature for const
1046 // generics.
1047 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001048 Expr::Block(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001049
1050 // ERROR CORRECTION: Add braces to make sure that the
1051 // generated code is valid.
David Tolnay32954ef2017-12-26 22:43:16 -05001052 _ => token::Brace::default().surround(tokens, |tokens| {
Nika Layzellce37f332017-12-05 12:01:22 -05001053 e.to_tokens(tokens);
1054 }),
David Tolnay51382052017-12-27 13:46:21 -05001055 },
Nika Layzell357885a2017-12-04 15:47:07 -05001056 }
1057 }
1058 }
1059
Nika Layzellc08227a2017-12-04 16:30:17 -05001060 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001061 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay2d4e08a2017-12-28 23:54:07 -05001062 self.colon2_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001063 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001064 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001065 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001066 }
1067 }
1068
David Tolnay506e43a2017-12-29 11:34:36 -05001069 impl ToTokens for Binding {
David Tolnay87d0b442016-09-04 11:52:12 -07001070 fn to_tokens(&self, tokens: &mut Tokens) {
1071 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001072 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001073 self.ty.to_tokens(tokens);
1074 }
1075 }
1076
Nika Layzellc08227a2017-12-04 16:30:17 -05001077 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001078 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001079 self.paren_token.surround(tokens, |tokens| {
1080 self.inputs.to_tokens(tokens);
1081 });
1082 self.output.to_tokens(tokens);
1083 }
1084 }
1085
David Tolnayf93b90d2017-11-11 19:21:26 -08001086 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001087 fn to_tokens(&self, tokens: &mut Tokens) {
1088 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001089 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -05001090 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001091 arrow.to_tokens(tokens);
1092 ty.to_tokens(tokens);
1093 }
David Tolnay87d0b442016-09-04 11:52:12 -07001094 }
1095 }
1096 }
1097
1098 impl ToTokens for PolyTraitRef {
1099 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001100 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001101 self.trait_ref.to_tokens(tokens);
1102 }
1103 }
1104
David Tolnay62f374c2016-10-02 13:37:00 -07001105 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001106 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001107 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001108 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001110 }
1111 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001112 }
1113 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001114
Alex Crichton23a15f62017-08-28 12:34:23 -07001115 impl ToTokens for BareFnArgName {
1116 fn to_tokens(&self, tokens: &mut Tokens) {
1117 match *self {
1118 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1119 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1120 }
1121 }
1122 }
1123
David Tolnayb8d8ef52016-10-29 14:30:08 -07001124 impl ToTokens for Abi {
1125 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001126 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -05001127 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001128 }
1129 }
David Tolnay87d0b442016-09-04 11:52:12 -07001130}