blob: 2aefaaae7ad1054ed187fae0b6ef61ceeba46b41 [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) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500469 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400470 (inputs, variadic)
471 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800472 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800473 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500474 unsafety: unsafety,
475 abi: abi,
476 lifetimes: lifetimes,
477 output: output,
478 variadic: (parens.0).1,
479 fn_token: fn_,
480 paren_token: parens.1,
481 inputs: (parens.0).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400482 })
483 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700484 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700485
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800486 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400487 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800488 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800489 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400490 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700491 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700492
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800493 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700494 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800495 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800496 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700497 ));
498 }
499
David Tolnay05362582017-12-26 01:33:57 -0500500 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400501 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500502 data: parens!(Delimited::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500503 (TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -0500504 elems: data.0,
Michael Layzell92639a52017-06-01 00:07:44 -0400505 paren_token: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400506 })
507 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700508 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700509
David Tolnay323279a2017-12-29 11:26:32 -0500510 impl Synom for TypeMacro {
511 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
512 }
513
David Tolnay0a169d42017-12-29 17:57:29 -0500514 impl Synom for TypePath {
515 named!(parse -> Self, call!(Self::parse, false));
516 }
517
David Tolnay7d38c7e2017-12-25 22:31:50 -0500518 impl TypePath {
519 named!(parse(allow_plus: bool) -> Self, do_parse!(
520 qpath: qpath >>
521 parenthesized: cond!(
522 qpath.1.segments.last().unwrap().item().arguments.is_empty(),
523 option!(syn!(ParenthesizedGenericArguments))
524 ) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500525 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500526 ({
527 let (qself, mut path) = qpath;
528 if let Some(Some(parenthesized)) = parenthesized {
529 let parenthesized = PathArguments::Parenthesized(parenthesized);
530 path.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
531 }
532 TypePath { qself: qself, path: path }
533 })
534 ));
535 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700536
David Tolnay9636c052016-10-02 17:11:17 -0700537 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700538 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700539 |
540 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800541 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800542 this: syn!(Type) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500543 path: option!(tuple!(keyword!(as), syn!(Path))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800544 gt: punct!(>) >>
545 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700546 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700547 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400548 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700549 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700550 let pos = path.segments.len();
David Tolnaydc03aec2017-12-30 01:54:18 -0500551 if !path.segments.empty_or_trailing() {
Alex Crichton954046c2017-05-30 21:49:42 -0700552 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700553 }
Alex Crichton954046c2017-05-30 21:49:42 -0700554 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 path.segments.push(item);
556 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400557 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700558 }
559 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400560 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700561 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700562 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700563 })
David Tolnay9636c052016-10-02 17:11:17 -0700564 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 };
566 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700567 lt_token: lt,
568 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400570 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700571 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700572 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700573 })
574 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700575 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700577 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700578
Nika Layzellc08227a2017-12-04 16:30:17 -0500579 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400580 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500581 data: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800582 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500583 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400584 paren_token: data.1,
585 inputs: data.0,
586 output: output,
587 })
588 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700589 }
590
David Tolnayf93b90d2017-11-11 19:21:26 -0800591 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400592 named!(parse -> Self, alt!(
593 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800595 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500596 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400597 )
598 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800599 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400600 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700601 }
602
David Tolnay0a169d42017-12-29 17:57:29 -0500603 impl Synom for TypeTraitObject {
604 named!(parse -> Self, call!(Self::parse, true));
605 }
606
David Tolnay7d38c7e2017-12-25 22:31:50 -0500607 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500608 named!(pub without_plus -> Self, call!(Self::parse, false));
609
David Tolnay7d38c7e2017-12-25 22:31:50 -0500610 // Only allow multiple trait references if allow_plus is true.
611 named!(parse(allow_plus: bool) -> Self, do_parse!(
612 dyn_token: option!(keyword!(dyn)) >>
613 bounds: alt!(
David Tolnaye64213b2017-12-30 00:24:20 -0500614 cond_reduce!(allow_plus, Delimited::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500615 |
616 syn!(TypeParamBound) => { |x| vec![x].into() }
617 ) >>
618 (TypeTraitObject {
619 dyn_token: dyn_token,
620 bounds: bounds,
621 })
622 ));
623 }
David Tolnay6414da72016-10-08 00:55:17 -0700624
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800625 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400626 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400628 // NOTE: rust-lang/rust#34511 includes discussion about whether or
629 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500630 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800631 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400632 impl_token: impl_,
633 bounds: elem,
634 })
635 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700636 }
David Tolnayb79ee962016-09-04 09:39:20 -0700637
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800638 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400639 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800640 data: grouped!(syn!(Type)) >>
641 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400642 group_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500643 elem: Box::new(data.0),
Michael Layzell93c36282017-06-04 20:43:14 -0400644 })
645 ));
646 }
647
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800648 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500649 named!(parse -> Self, call!(Self::parse, false));
650 }
651
652 impl TypeParen {
653 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800654 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500655 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800656 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400657 paren_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500658 elem: Box::new(data.0),
Michael Layzell92639a52017-06-01 00:07:44 -0400659 })
660 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700661 }
David Tolnayb79ee962016-09-04 09:39:20 -0700662
Alex Crichton954046c2017-05-30 21:49:42 -0700663 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400664 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 colon: option!(punct!(::)) >>
David Tolnaye45b59f2017-12-25 18:44:49 -0500666 segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >>
667 cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400668 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700669 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400670 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400671 })
672 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700673
674 fn description() -> Option<&'static str> {
675 Some("path")
676 }
Alex Crichton954046c2017-05-30 21:49:42 -0700677 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700678
Nika Layzellc680e612017-12-04 19:07:20 -0500679 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500680 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500681 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500682 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500683 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500684 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500685 |
David Tolnay506e43a2017-12-29 11:34:36 -0500686 syn!(Binding) => { GenericArgument::Binding }
Nika Layzell357885a2017-12-04 15:47:07 -0500687 ));
688 }
689
Nika Layzellc680e612017-12-04 19:07:20 -0500690 #[cfg(feature = "full")]
691 impl Synom for GenericArgument {
692 named!(parse -> Self, alt!(
693 call!(ty_no_eq_after) => { GenericArgument::Type }
694 |
695 syn!(Lifetime) => { GenericArgument::Lifetime }
696 |
David Tolnay506e43a2017-12-29 11:34:36 -0500697 syn!(Binding) => { GenericArgument::Binding }
Nika Layzellc680e612017-12-04 19:07:20 -0500698 |
David Tolnay8c91b882017-12-28 23:04:32 -0500699 syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) }
Nika Layzellce37f332017-12-05 12:01:22 -0500700 |
David Tolnay8c91b882017-12-28 23:04:32 -0500701 syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500702 ));
703 }
704
Nika Layzellc08227a2017-12-04 16:30:17 -0500705 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500706 named!(parse -> Self, do_parse!(
David Tolnay2d4e08a2017-12-28 23:54:07 -0500707 colon2: option!(punct!(::)) >>
Nika Layzell357885a2017-12-04 15:47:07 -0500708 lt: punct!(<) >>
709 args: call!(Delimited::parse_terminated) >>
710 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500711 (AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500712 colon2_token: colon2,
Nika Layzell357885a2017-12-04 15:47:07 -0500713 lt_token: lt,
714 args: args,
715 gt_token: gt,
716 })
717 ));
718 }
719
Alex Crichton954046c2017-05-30 21:49:42 -0700720 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400721 named!(parse -> Self, alt!(
722 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700723 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500724 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400725 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700726 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500727 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400728 })
729 )
730 |
731 mod_style_path_segment
732 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700733 }
David Tolnay570695e2017-06-03 16:15:13 -0700734
David Tolnaydc03aec2017-12-30 01:54:18 -0500735 named!(pub ty_no_eq_after -> Type, do_parse!(
736 ty: syn!(Type) >>
737 not!(punct!(=)) >>
738 (ty)
739 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700740
Alex Crichton954046c2017-05-30 21:49:42 -0700741 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400742 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800743 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400744 segments: call!(Delimited::parse_separated_nonempty_with,
745 mod_style_path_segment) >>
746 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700747 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400748 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400749 })
750 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700751 }
Arnavionf2dada12017-04-20 23:55:20 -0700752
753 named!(mod_style_path_segment -> PathSegment, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500754 syn!(Ident) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700755 |
David Tolnay5f332a92017-12-26 00:42:45 -0500756 keyword!(super) => { Into::into }
757 |
758 keyword!(self) => { Into::into }
759 |
760 keyword!(Self) => { Into::into }
761 |
762 keyword!(crate) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700763 ));
764
David Tolnay506e43a2017-12-29 11:34:36 -0500765 impl Synom for Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400766 named!(parse -> Self, do_parse!(
767 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800768 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800769 ty: syn!(Type) >>
David Tolnay506e43a2017-12-29 11:34:36 -0500770 (Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400771 ident: id,
772 eq_token: eq,
773 ty: ty,
774 })
775 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700776 }
777
778 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400779 named!(parse -> Self, do_parse!(
780 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
781 trait_ref: syn!(Path) >>
782 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500783 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
784 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400785 )) >>
786 ({
787 let mut trait_ref = trait_ref;
788 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500789 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400790 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500791 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400792 }
793 PolyTraitRef {
794 bound_lifetimes: bound_lifetimes,
795 trait_ref: trait_ref,
796 }
797 })
798 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700799 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700800
Alex Crichton954046c2017-05-30 21:49:42 -0700801 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400802 named!(parse -> Self, do_parse!(
803 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700804 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800805 not!(punct!(::)) >>
806 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400807 (name, colon)
808 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800809 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400810 (BareFnArg {
811 name: name,
812 ty: ty,
813 })
814 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700815 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700816
Alex Crichton23a15f62017-08-28 12:34:23 -0700817 impl Synom for BareFnArgName {
818 named!(parse -> Self, alt!(
819 map!(syn!(Ident), BareFnArgName::Named)
820 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800821 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700822 ));
823 }
824
Alex Crichton954046c2017-05-30 21:49:42 -0700825 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400826 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800827 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400828 // TODO: this parses all literals, not just strings
829 name: option!(syn!(Lit)) >>
830 (Abi {
831 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500832 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400833 })
834 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700835 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700836}
David Tolnay87d0b442016-09-04 11:52:12 -0700837
838#[cfg(feature = "printing")]
839mod printing {
840 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500841 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700842
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800843 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700844 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700845 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500846 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700847 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700848 }
849 }
850
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800851 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700852 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700853 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500854 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500856 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700857 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700858 }
859 }
860
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800861 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700862 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700863 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500864 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500865 Some(ref tok) => tok.to_tokens(tokens),
866 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700867 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400868 }
869 }
David Tolnay136aaa32017-12-29 02:37:36 -0500870 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700871 }
872 }
873
David Tolnay0a89b4d2017-11-13 00:55:45 -0800874 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700875 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700876 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700877 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500878 self.mutability.to_tokens(tokens);
879 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700880 }
881 }
882
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800883 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700884 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500885 self.lifetimes.to_tokens(tokens);
886 self.unsafety.to_tokens(tokens);
887 self.abi.to_tokens(tokens);
888 self.fn_token.to_tokens(tokens);
889 self.paren_token.surround(tokens, |tokens| {
890 self.inputs.to_tokens(tokens);
891 if let Some(ref variadic) = self.variadic {
892 if !self.inputs.empty_or_trailing() {
893 let span = variadic.0[0];
894 <Token![,]>::new(span).to_tokens(tokens);
895 }
896 variadic.to_tokens(tokens);
897 }
898 });
899 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700900 }
901 }
902
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800903 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700904 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700905 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700906 }
907 }
908
David Tolnay05362582017-12-26 01:33:57 -0500909 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700910 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700911 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500912 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700913 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700914 }
915 }
916
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800917 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700918 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700919 PathTokens(&self.qself, &self.path).to_tokens(tokens);
920 }
921 }
922
923 impl<'a> ToTokens for PathTokens<'a> {
924 fn to_tokens(&self, tokens: &mut Tokens) {
925 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700926 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700927 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700928 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700929 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700930 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400931
932 // XXX: Gross.
933 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
934 self.1.segments.len() - 1
935 } else {
936 qself.position
937 };
David Tolnay570695e2017-06-03 16:15:13 -0700938 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400939 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700940 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700941 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700942 for (i, segment) in (&mut segments).take(pos).enumerate() {
943 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700944 segment.item().to_tokens(tokens);
945 qself.gt_token.to_tokens(tokens);
946 segment.delimiter().to_tokens(tokens);
947 } else {
948 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700951 } else {
952 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700953 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 }
David Tolnay570695e2017-06-03 16:15:13 -0700955 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700956 segment.to_tokens(tokens);
957 }
958 }
959 }
960
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800961 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500963 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700965 }
966 }
967
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800968 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700969 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700970 self.impl_token.to_tokens(tokens);
971 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 }
973 }
974
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800975 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400976 fn to_tokens(&self, tokens: &mut Tokens) {
977 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500978 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400979 });
980 }
981 }
982
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800983 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700985 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500986 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700987 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700988 }
989 }
990
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800991 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -0700992 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700993 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700994 }
995 }
996
David Tolnay323279a2017-12-29 11:26:32 -0500997 impl ToTokens for TypeMacro {
998 fn to_tokens(&self, tokens: &mut Tokens) {
999 self.mac.to_tokens(tokens);
1000 }
1001 }
1002
David Tolnay2ae520a2017-12-29 11:19:50 -05001003 impl ToTokens for TypeVerbatim {
1004 fn to_tokens(&self, tokens: &mut Tokens) {
1005 self.tts.to_tokens(tokens);
1006 }
1007 }
1008
David Tolnay87d0b442016-09-04 11:52:12 -07001009 impl ToTokens for Path {
1010 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001011 self.leading_colon.to_tokens(tokens);
1012 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001013 }
1014 }
1015
1016 impl ToTokens for PathSegment {
1017 fn to_tokens(&self, tokens: &mut Tokens) {
1018 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001019 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001020 }
1021 }
1022
Nika Layzellc08227a2017-12-04 16:30:17 -05001023 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001024 fn to_tokens(&self, tokens: &mut Tokens) {
1025 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001026 PathArguments::None => {}
1027 PathArguments::AngleBracketed(ref arguments) => {
1028 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001029 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001030 PathArguments::Parenthesized(ref arguments) => {
1031 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001032 }
1033 }
1034 }
1035 }
1036
Nika Layzellc08227a2017-12-04 16:30:17 -05001037 impl ToTokens for GenericArgument {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001038 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Nika Layzell357885a2017-12-04 15:47:07 -05001039 fn to_tokens(&self, tokens: &mut Tokens) {
1040 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001041 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1042 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
David Tolnay506e43a2017-12-29 11:34:36 -05001043 GenericArgument::Binding(ref tb) => tb.to_tokens(tokens),
David Tolnay8c91b882017-12-28 23:04:32 -05001044 GenericArgument::Const(ref e) => match *e {
1045 Expr::Lit(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001046
1047 // NOTE: We should probably support parsing blocks with only
1048 // expressions in them without the full feature for const
1049 // generics.
1050 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001051 Expr::Block(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001052
1053 // ERROR CORRECTION: Add braces to make sure that the
1054 // generated code is valid.
David Tolnay32954ef2017-12-26 22:43:16 -05001055 _ => token::Brace::default().surround(tokens, |tokens| {
Nika Layzellce37f332017-12-05 12:01:22 -05001056 e.to_tokens(tokens);
1057 }),
David Tolnay51382052017-12-27 13:46:21 -05001058 },
Nika Layzell357885a2017-12-04 15:47:07 -05001059 }
1060 }
1061 }
1062
Nika Layzellc08227a2017-12-04 16:30:17 -05001063 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001064 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay2d4e08a2017-12-28 23:54:07 -05001065 self.colon2_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001066 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001067 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001068 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001069 }
1070 }
1071
David Tolnay506e43a2017-12-29 11:34:36 -05001072 impl ToTokens for Binding {
David Tolnay87d0b442016-09-04 11:52:12 -07001073 fn to_tokens(&self, tokens: &mut Tokens) {
1074 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001075 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001076 self.ty.to_tokens(tokens);
1077 }
1078 }
1079
Nika Layzellc08227a2017-12-04 16:30:17 -05001080 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001081 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001082 self.paren_token.surround(tokens, |tokens| {
1083 self.inputs.to_tokens(tokens);
1084 });
1085 self.output.to_tokens(tokens);
1086 }
1087 }
1088
David Tolnayf93b90d2017-11-11 19:21:26 -08001089 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 fn to_tokens(&self, tokens: &mut Tokens) {
1091 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001092 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -05001093 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001094 arrow.to_tokens(tokens);
1095 ty.to_tokens(tokens);
1096 }
David Tolnay87d0b442016-09-04 11:52:12 -07001097 }
1098 }
1099 }
1100
1101 impl ToTokens for PolyTraitRef {
1102 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001103 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001104 self.trait_ref.to_tokens(tokens);
1105 }
1106 }
1107
David Tolnay62f374c2016-10-02 13:37:00 -07001108 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001109 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001110 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001111 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001112 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001113 }
1114 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001115 }
1116 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001117
Alex Crichton23a15f62017-08-28 12:34:23 -07001118 impl ToTokens for BareFnArgName {
1119 fn to_tokens(&self, tokens: &mut Tokens) {
1120 match *self {
1121 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1122 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1123 }
1124 }
1125 }
1126
David Tolnayb8d8ef52016-10-29 14:30:08 -07001127 impl ToTokens for Abi {
1128 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001129 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -05001130 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001131 }
1132 }
David Tolnay87d0b442016-09-04 11:52:12 -07001133}