blob: ab0823075556189e09ff383aab14c79c7e577a44 [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")]
David Tolnayc43b44e2017-12-30 23:55:54 -05007use tt::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 {
David Tolnay660fd1f2017-12-31 01:52:57 -0500155 let mut path = Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 leading_colon: None,
David Tolnay660fd1f2017-12-31 01:52:57 -0500157 segments: Delimited::new(),
158 };
159 path.segments.push(segment.into());
160 path
David Tolnay84aa0752016-10-02 23:01:13 -0700161 }
162}
163
Alex Crichton62a0a592017-05-22 13:58:53 -0700164ast_struct! {
165 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
166 ///
167 /// E.g. `std`, `String` or `Box<T>`
168 pub struct PathSegment {
169 /// The identifier portion of this path segment.
170 pub ident: Ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500171 /// Type/lifetime arguments attached to this path. They come in
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
173 /// this is more than just simple syntactic sugar; the use of
174 /// parens affects the region binding rules, so we preserve the
175 /// distinction.
Nika Layzellc08227a2017-12-04 16:30:17 -0500176 pub arguments: PathArguments,
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 }
David Tolnayb79ee962016-09-04 09:39:20 -0700178}
179
David Tolnaydaaf7742016-10-03 11:11:43 -0700180impl<T> From<T> for PathSegment
David Tolnay51382052017-12-27 13:46:21 -0500181where
182 T: Into<Ident>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700183{
David Tolnay84aa0752016-10-02 23:01:13 -0700184 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700185 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700186 ident: ident.into(),
Nika Layzellc08227a2017-12-04 16:30:17 -0500187 arguments: PathArguments::None,
David Tolnayb79ee962016-09-04 09:39:20 -0700188 }
189 }
190}
191
Alex Crichton62a0a592017-05-22 13:58:53 -0700192ast_enum! {
Nika Layzellc08227a2017-12-04 16:30:17 -0500193 /// Arguments of a path segment.
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 ///
195 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
Nika Layzellc08227a2017-12-04 16:30:17 -0500196 pub enum PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700197 None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500199 AngleBracketed(AngleBracketedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500201 Parenthesized(ParenthesizedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 }
David Tolnayb79ee962016-09-04 09:39:20 -0700203}
204
Nika Layzellc08227a2017-12-04 16:30:17 -0500205impl Default for PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700206 fn default() -> Self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500207 PathArguments::None
David Tolnayb79ee962016-09-04 09:39:20 -0700208 }
David Tolnay570695e2017-06-03 16:15:13 -0700209}
David Tolnay5332d4b2016-10-30 14:25:22 -0700210
Nika Layzellc08227a2017-12-04 16:30:17 -0500211impl PathArguments {
David Tolnay5332d4b2016-10-30 14:25:22 -0700212 pub fn is_empty(&self) -> bool {
213 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500214 PathArguments::None => true,
215 PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(),
216 PathArguments::Parenthesized(_) => false,
David Tolnay5332d4b2016-10-30 14:25:22 -0700217 }
218 }
David Tolnayb79ee962016-09-04 09:39:20 -0700219}
220
Nika Layzell357885a2017-12-04 15:47:07 -0500221ast_enum! {
222 /// A individual generic argument, like `'a`, `T`, or `Item=T`.
Nika Layzellc08227a2017-12-04 16:30:17 -0500223 pub enum GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500224 /// The lifetime parameters for this path segment.
225 Lifetime(Lifetime),
226 /// The type parameters for this path segment, if present.
227 Type(Type),
228 /// Bindings (equality constraints) on associated types, if present.
229 ///
230 /// E.g., `Foo<A=Bar>`.
David Tolnay506e43a2017-12-29 11:34:36 -0500231 Binding(Binding),
Nika Layzellc680e612017-12-04 19:07:20 -0500232 /// Const expression. Must be inside of a block.
233 ///
234 /// NOTE: Identity expressions are represented as Type arguments, as
235 /// they are indistinguishable syntactically.
Nika Layzellce37f332017-12-05 12:01:22 -0500236 Const(Expr),
Nika Layzell357885a2017-12-04 15:47:07 -0500237 }
238}
239
Alex Crichton62a0a592017-05-22 13:58:53 -0700240ast_struct! {
241 /// A path like `Foo<'a, T>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500242 pub struct AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500243 pub colon2_token: Option<Token![::]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 pub lt_token: Token![<],
Nika Layzellc08227a2017-12-04 16:30:17 -0500245 pub args: Delimited<GenericArgument, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700247 }
248}
249
250ast_struct! {
251 /// Bind a type to an associated type: `A=Foo`.
David Tolnay506e43a2017-12-29 11:34:36 -0500252 pub struct Binding {
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800254 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800255 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }
257}
258
Alex Crichton62a0a592017-05-22 13:58:53 -0700259ast_struct! {
260 /// A path like `Foo(A,B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500261 pub struct ParenthesizedGenericArguments {
David Tolnay32954ef2017-12-26 22:43:16 -0500262 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700263 /// `(A, B)`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800264 pub inputs: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 /// `C`
David Tolnayf93b90d2017-11-11 19:21:26 -0800266 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 }
268}
269
270ast_struct! {
271 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700272 /// The `for<'a>` in `for<'a> Foo<&'a T>`
273 pub bound_lifetimes: Option<BoundLifetimes>,
David Tolnay7d38c7e2017-12-25 22:31:50 -0500274 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 pub trait_ref: Path,
276 }
277}
278
279ast_struct! {
280 /// The explicit Self type in a "qualified path". The actual
281 /// path, including the trait and the associated item, is stored
282 /// separately. `position` represents the index of the associated
283 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700284 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500285 /// ```text
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 /// <Vec<T> as a::b::Trait>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500287 /// ^~~~~~ ~~~~~~~~~~~~~~^
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700289 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 /// <Vec<T>>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500291 /// ^~~~~~ ^
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 /// ty position = 0
293 /// ```
294 pub struct QSelf {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub lt_token: Token![<],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800296 pub ty: Box<Type>,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400297 pub position: usize,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800298 pub as_token: Option<Token![as]>,
299 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 }
301}
302
303ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700304 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub extern_token: Token![extern],
David Tolnayd5125762017-12-29 02:42:17 -0500306 pub name: Option<Lit>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 }
308}
309
310ast_struct! {
311 /// An argument in a function type.
312 ///
313 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
314 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800315 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800316 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 }
318}
319
Alex Crichton23a15f62017-08-28 12:34:23 -0700320ast_enum! {
321 /// Names of arguments in the `BareFnArg` structure
322 pub enum BareFnArgName {
323 /// Argument with the provided name
324 Named(Ident),
325 /// Argument matched with `_`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800326 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700327 }
328}
Alex Crichton62a0a592017-05-22 13:58:53 -0700329
330ast_enum! {
David Tolnayf93b90d2017-11-11 19:21:26 -0800331 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 /// Return type is not specified.
333 ///
334 /// Functions default to `()` and
335 /// closures default to inference. Span points to where return
336 /// type would be inserted.
337 Default,
338 /// Everything else
David Tolnay4a3f59a2017-12-28 21:21:12 -0500339 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 }
David Tolnayb79ee962016-09-04 09:39:20 -0700341}
342
David Tolnay86eca752016-09-04 11:26:41 -0700343#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700344pub mod parsing {
345 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400346 use synom::Synom;
David Tolnayda4049b2016-09-04 10:59:23 -0700347
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800348 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400349 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700350
Alex Crichton954046c2017-05-30 21:49:42 -0700351 fn description() -> Option<&'static str> {
352 Some("type")
353 }
354 }
David Tolnay0047c712016-12-21 21:59:25 -0500355
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800356 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400357 /// In some positions, types may not contain the `+` character, to
358 /// disambiguate them. For example in the expression `1 as T`, T may not
359 /// contain a `+` character.
360 ///
361 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400362 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400363 }
364
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800365 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
366 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400367 |
David Tolnay05362582017-12-26 01:33:57 -0500368 // must be before TypeTuple
David Tolnay0a169d42017-12-29 17:57:29 -0500369 call!(TypeParen::parse, allow_plus) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400370 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500371 // must be before TypePath
David Tolnay323279a2017-12-29 11:26:32 -0500372 syn!(TypeMacro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400373 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500374 // must be before TypeTraitObject
375 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400376 |
David Tolnay0a169d42017-12-29 17:57:29 -0500377 // Don't try parsing more than one trait bound if we aren't allowing it.
378 // must be before TypeTuple
379 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
380 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800381 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400382 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800383 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400384 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800385 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400386 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800387 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400388 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800389 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400390 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800391 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400392 |
David Tolnay05362582017-12-26 01:33:57 -0500393 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400394 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800395 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700396 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800397 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400398 ));
399
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800400 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400401 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800402 brackets!(syn!(Type)),
403 |(ty, b)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500404 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400405 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700406 }
Michael Layzell92639a52017-06-01 00:07:44 -0400407 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800408
409 fn description() -> Option<&'static str> {
410 Some("slice type")
411 }
Alex Crichton954046c2017-05-30 21:49:42 -0700412 }
David Tolnayb79ee962016-09-04 09:39:20 -0700413
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800414 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400415 named!(parse -> Self, map!(
416 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800417 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400419 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700420 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400421 )),
422 |((elem, semi, len), brackets)| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800423 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500424 elem: Box::new(elem),
425 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400426 bracket_token: brackets,
427 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700428 }
429 }
Michael Layzell92639a52017-06-01 00:07:44 -0400430 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800431
432 fn description() -> Option<&'static str> {
433 Some("array type")
434 }
Alex Crichton954046c2017-05-30 21:49:42 -0700435 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700436
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800437 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400438 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800439 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400440 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500441 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400442 |
David Tolnay24237fb2017-12-29 02:15:26 -0500443 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400444 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800445 target: call!(Type::without_plus) >>
446 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400447 const_token: mutability.1,
448 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500449 mutability: mutability.0,
450 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400451 })
452 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800453
454 fn description() -> Option<&'static str> {
455 Some("raw pointer type")
456 }
Alex Crichton954046c2017-05-30 21:49:42 -0700457 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700458
David Tolnay0a89b4d2017-11-13 00:55:45 -0800459 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400460 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800461 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400462 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500463 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400464 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800465 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800466 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400467 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500468 mutability: mutability,
469 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400470 and_token: amp,
471 })
472 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800473
474 fn description() -> Option<&'static str> {
475 Some("reference type")
476 }
Alex Crichton954046c2017-05-30 21:49:42 -0700477 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700478
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800479 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400480 named!(parse -> Self, do_parse!(
481 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500482 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400483 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800484 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400485 parens: parens!(do_parse!(
486 inputs: call!(Delimited::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500487 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400488 (inputs, variadic)
489 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800490 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800491 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500492 unsafety: unsafety,
493 abi: abi,
494 lifetimes: lifetimes,
495 output: output,
496 variadic: (parens.0).1,
497 fn_token: fn_,
498 paren_token: parens.1,
499 inputs: (parens.0).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400500 })
501 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800502
503 fn description() -> Option<&'static str> {
504 Some("`fn` type")
505 }
Alex Crichton954046c2017-05-30 21:49:42 -0700506 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700507
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800508 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400509 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800511 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400512 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800513
514 fn description() -> Option<&'static str> {
515 Some("never type: `!`")
516 }
Alex Crichton954046c2017-05-30 21:49:42 -0700517 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700518
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800519 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700520 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800521 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800522 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700523 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800524
525 fn description() -> Option<&'static str> {
526 Some("inferred type: `_`")
527 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700528 }
529
David Tolnay05362582017-12-26 01:33:57 -0500530 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400531 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500532 data: parens!(Delimited::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500533 (TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -0500534 elems: data.0,
Michael Layzell92639a52017-06-01 00:07:44 -0400535 paren_token: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400536 })
537 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800538
539 fn description() -> Option<&'static str> {
540 Some("tuple type")
541 }
Alex Crichton954046c2017-05-30 21:49:42 -0700542 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700543
David Tolnay323279a2017-12-29 11:26:32 -0500544 impl Synom for TypeMacro {
545 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
546 }
547
David Tolnay0a169d42017-12-29 17:57:29 -0500548 impl Synom for TypePath {
549 named!(parse -> Self, call!(Self::parse, false));
550 }
551
David Tolnay7d38c7e2017-12-25 22:31:50 -0500552 impl TypePath {
553 named!(parse(allow_plus: bool) -> Self, do_parse!(
554 qpath: qpath >>
David Tolnay660fd1f2017-12-31 01:52:57 -0500555 parenthesized: option!(cond_reduce!(
David Tolnay7d38c7e2017-12-25 22:31:50 -0500556 qpath.1.segments.last().unwrap().item().arguments.is_empty(),
David Tolnay660fd1f2017-12-31 01:52:57 -0500557 syn!(ParenthesizedGenericArguments)
558 )) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500559 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500560 ({
561 let (qself, mut path) = qpath;
David Tolnay660fd1f2017-12-31 01:52:57 -0500562 if let Some(parenthesized) = parenthesized {
David Tolnay7d38c7e2017-12-25 22:31:50 -0500563 let parenthesized = PathArguments::Parenthesized(parenthesized);
564 path.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
565 }
566 TypePath { qself: qself, path: path }
567 })
568 ));
569 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700570
David Tolnay9636c052016-10-02 17:11:17 -0700571 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700572 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700573 |
574 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800575 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800576 this: syn!(Type) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500577 path: option!(tuple!(keyword!(as), syn!(Path))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800578 gt: punct!(>) >>
579 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700580 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700581 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400582 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700583 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700584 let pos = path.segments.len();
David Tolnay660fd1f2017-12-31 01:52:57 -0500585 path.segments.push_trailing(colon2);
586 path.segments.extend(rest);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400587 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700588 }
589 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400590 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700591 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700592 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700593 })
David Tolnay9636c052016-10-02 17:11:17 -0700594 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595 };
596 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700597 lt_token: lt,
598 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700599 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400600 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700601 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700602 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700603 })
604 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700605 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700607 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700608
Nika Layzellc08227a2017-12-04 16:30:17 -0500609 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400610 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500611 data: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800612 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500613 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400614 paren_token: data.1,
615 inputs: data.0,
616 output: output,
617 })
618 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800619
620 fn description() -> Option<&'static str> {
621 Some("parenthesized generic arguments: `Foo(A, B, ..) -> T`")
622 }
Alex Crichton954046c2017-05-30 21:49:42 -0700623 }
624
David Tolnayf93b90d2017-11-11 19:21:26 -0800625 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400626 named!(parse -> Self, alt!(
627 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800629 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500630 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400631 )
632 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800633 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400634 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800635
636 fn description() -> Option<&'static str> {
637 Some("return type")
638 }
Alex Crichton954046c2017-05-30 21:49:42 -0700639 }
640
David Tolnay0a169d42017-12-29 17:57:29 -0500641 impl Synom for TypeTraitObject {
642 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800643
644 fn description() -> Option<&'static str> {
645 Some("trait object type")
646 }
David Tolnay0a169d42017-12-29 17:57:29 -0500647 }
648
David Tolnay7d38c7e2017-12-25 22:31:50 -0500649 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500650 named!(pub without_plus -> Self, call!(Self::parse, false));
651
David Tolnay7d38c7e2017-12-25 22:31:50 -0500652 // Only allow multiple trait references if allow_plus is true.
653 named!(parse(allow_plus: bool) -> Self, do_parse!(
654 dyn_token: option!(keyword!(dyn)) >>
655 bounds: alt!(
David Tolnaye64213b2017-12-30 00:24:20 -0500656 cond_reduce!(allow_plus, Delimited::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500657 |
David Tolnay660fd1f2017-12-31 01:52:57 -0500658 syn!(TypeParamBound) => {|x| {
659 let mut delimited = Delimited::new();
660 delimited.push(x);
661 delimited
662 }}
David Tolnay7d38c7e2017-12-25 22:31:50 -0500663 ) >>
664 (TypeTraitObject {
665 dyn_token: dyn_token,
666 bounds: bounds,
667 })
668 ));
669 }
David Tolnay6414da72016-10-08 00:55:17 -0700670
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800671 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400672 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800673 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400674 // NOTE: rust-lang/rust#34511 includes discussion about whether or
675 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500676 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800677 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400678 impl_token: impl_,
679 bounds: elem,
680 })
681 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800682
683 fn description() -> Option<&'static str> {
684 Some("`impl Trait` type")
685 }
Alex Crichton954046c2017-05-30 21:49:42 -0700686 }
David Tolnayb79ee962016-09-04 09:39:20 -0700687
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800688 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400689 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800690 data: grouped!(syn!(Type)) >>
691 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400692 group_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500693 elem: Box::new(data.0),
Michael Layzell93c36282017-06-04 20:43:14 -0400694 })
695 ));
696 }
697
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800698 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500699 named!(parse -> Self, call!(Self::parse, false));
700 }
701
702 impl TypeParen {
703 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800704 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500705 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800706 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400707 paren_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500708 elem: Box::new(data.0),
Michael Layzell92639a52017-06-01 00:07:44 -0400709 })
710 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700711 }
David Tolnayb79ee962016-09-04 09:39:20 -0700712
Alex Crichton954046c2017-05-30 21:49:42 -0700713 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400714 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800715 colon: option!(punct!(::)) >>
David Tolnaye45b59f2017-12-25 18:44:49 -0500716 segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >>
717 cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400718 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700719 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400720 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400721 })
722 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700723
724 fn description() -> Option<&'static str> {
725 Some("path")
726 }
Alex Crichton954046c2017-05-30 21:49:42 -0700727 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700728
Nika Layzellc680e612017-12-04 19:07:20 -0500729 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500730 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500731 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500732 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500733 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500734 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500735 |
David Tolnay506e43a2017-12-29 11:34:36 -0500736 syn!(Binding) => { GenericArgument::Binding }
Nika Layzell357885a2017-12-04 15:47:07 -0500737 ));
738 }
739
Nika Layzellc680e612017-12-04 19:07:20 -0500740 #[cfg(feature = "full")]
741 impl Synom for GenericArgument {
742 named!(parse -> Self, alt!(
743 call!(ty_no_eq_after) => { GenericArgument::Type }
744 |
745 syn!(Lifetime) => { GenericArgument::Lifetime }
746 |
David Tolnay506e43a2017-12-29 11:34:36 -0500747 syn!(Binding) => { GenericArgument::Binding }
Nika Layzellc680e612017-12-04 19:07:20 -0500748 |
David Tolnay8c91b882017-12-28 23:04:32 -0500749 syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) }
Nika Layzellce37f332017-12-05 12:01:22 -0500750 |
David Tolnay8c91b882017-12-28 23:04:32 -0500751 syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500752 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800753
754 fn description() -> Option<&'static str> {
755 Some("generic argument")
756 }
Nika Layzellc680e612017-12-04 19:07:20 -0500757 }
758
Nika Layzellc08227a2017-12-04 16:30:17 -0500759 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500760 named!(parse -> Self, do_parse!(
David Tolnay2d4e08a2017-12-28 23:54:07 -0500761 colon2: option!(punct!(::)) >>
Nika Layzell357885a2017-12-04 15:47:07 -0500762 lt: punct!(<) >>
763 args: call!(Delimited::parse_terminated) >>
764 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500765 (AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500766 colon2_token: colon2,
Nika Layzell357885a2017-12-04 15:47:07 -0500767 lt_token: lt,
768 args: args,
769 gt_token: gt,
770 })
771 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800772
773 fn description() -> Option<&'static str> {
774 Some("angle bracketed generic arguments")
775 }
Nika Layzell357885a2017-12-04 15:47:07 -0500776 }
777
Alex Crichton954046c2017-05-30 21:49:42 -0700778 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400779 named!(parse -> Self, alt!(
780 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700781 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500782 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400783 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700784 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500785 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400786 })
787 )
788 |
789 mod_style_path_segment
790 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800791
792 fn description() -> Option<&'static str> {
793 Some("path segment")
794 }
Alex Crichton954046c2017-05-30 21:49:42 -0700795 }
David Tolnay570695e2017-06-03 16:15:13 -0700796
David Tolnaydc03aec2017-12-30 01:54:18 -0500797 named!(pub ty_no_eq_after -> Type, do_parse!(
798 ty: syn!(Type) >>
799 not!(punct!(=)) >>
800 (ty)
801 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700802
Alex Crichton954046c2017-05-30 21:49:42 -0700803 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400804 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800805 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400806 segments: call!(Delimited::parse_separated_nonempty_with,
807 mod_style_path_segment) >>
808 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700809 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400810 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400811 })
812 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700813 }
Arnavionf2dada12017-04-20 23:55:20 -0700814
815 named!(mod_style_path_segment -> PathSegment, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500816 syn!(Ident) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700817 |
David Tolnay5f332a92017-12-26 00:42:45 -0500818 keyword!(super) => { Into::into }
819 |
820 keyword!(self) => { Into::into }
821 |
822 keyword!(Self) => { Into::into }
823 |
824 keyword!(crate) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700825 ));
826
David Tolnay506e43a2017-12-29 11:34:36 -0500827 impl Synom for Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400828 named!(parse -> Self, do_parse!(
829 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800830 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800831 ty: syn!(Type) >>
David Tolnay506e43a2017-12-29 11:34:36 -0500832 (Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400833 ident: id,
834 eq_token: eq,
835 ty: ty,
836 })
837 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800838
839 fn description() -> Option<&'static str> {
840 Some("associated type binding")
841 }
Alex Crichton954046c2017-05-30 21:49:42 -0700842 }
843
844 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400845 named!(parse -> Self, do_parse!(
846 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
847 trait_ref: syn!(Path) >>
848 parenthesized: option!(cond_reduce!(
David Tolnay660fd1f2017-12-31 01:52:57 -0500849 trait_ref.segments.last().unwrap().item().arguments.is_empty(),
Nika Layzellc08227a2017-12-04 16:30:17 -0500850 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400851 )) >>
852 ({
853 let mut trait_ref = trait_ref;
854 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500855 let parenthesized = PathArguments::Parenthesized(parenthesized);
David Tolnay660fd1f2017-12-31 01:52:57 -0500856 trait_ref.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400857 }
858 PolyTraitRef {
859 bound_lifetimes: bound_lifetimes,
860 trait_ref: trait_ref,
861 }
862 })
863 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800864
865 fn description() -> Option<&'static str> {
866 Some("poly trait reference")
867 }
Alex Crichton954046c2017-05-30 21:49:42 -0700868 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700869
Alex Crichton954046c2017-05-30 21:49:42 -0700870 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400871 named!(parse -> Self, do_parse!(
872 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700873 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 not!(punct!(::)) >>
875 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400876 (name, colon)
877 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800878 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400879 (BareFnArg {
880 name: name,
881 ty: ty,
882 })
883 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800884
885 fn description() -> Option<&'static str> {
886 Some("function type argument")
887 }
Alex Crichton954046c2017-05-30 21:49:42 -0700888 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700889
Alex Crichton23a15f62017-08-28 12:34:23 -0700890 impl Synom for BareFnArgName {
891 named!(parse -> Self, alt!(
892 map!(syn!(Ident), BareFnArgName::Named)
893 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800894 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700895 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800896
897 fn description() -> Option<&'static str> {
898 Some("function argument name")
899 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700900 }
901
Alex Crichton954046c2017-05-30 21:49:42 -0700902 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400903 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800904 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400905 // TODO: this parses all literals, not just strings
906 name: option!(syn!(Lit)) >>
907 (Abi {
908 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500909 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400910 })
911 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800912
913 fn description() -> Option<&'static str> {
914 Some("ABI qualifier")
915 }
Alex Crichton954046c2017-05-30 21:49:42 -0700916 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700917}
David Tolnay87d0b442016-09-04 11:52:12 -0700918
919#[cfg(feature = "printing")]
920mod printing {
921 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500922 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700923
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800924 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700925 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700926 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500927 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700928 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700929 }
930 }
931
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800932 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700933 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700934 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500935 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700936 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500937 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700938 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700939 }
940 }
941
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800942 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700944 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500945 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500946 Some(ref tok) => tok.to_tokens(tokens),
947 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700948 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400949 }
950 }
David Tolnay136aaa32017-12-29 02:37:36 -0500951 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700952 }
953 }
954
David Tolnay0a89b4d2017-11-13 00:55:45 -0800955 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700956 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700957 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700958 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500959 self.mutability.to_tokens(tokens);
960 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 }
962 }
963
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800964 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700965 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500966 self.lifetimes.to_tokens(tokens);
967 self.unsafety.to_tokens(tokens);
968 self.abi.to_tokens(tokens);
969 self.fn_token.to_tokens(tokens);
970 self.paren_token.surround(tokens, |tokens| {
971 self.inputs.to_tokens(tokens);
972 if let Some(ref variadic) = self.variadic {
973 if !self.inputs.empty_or_trailing() {
974 let span = variadic.0[0];
975 <Token![,]>::new(span).to_tokens(tokens);
976 }
977 variadic.to_tokens(tokens);
978 }
979 });
980 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 }
982 }
983
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800984 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700985 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700986 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700987 }
988 }
989
David Tolnay05362582017-12-26 01:33:57 -0500990 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700991 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700992 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500993 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700994 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700995 }
996 }
997
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800998 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700999 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001000 PathTokens(&self.qself, &self.path).to_tokens(tokens);
1001 }
1002 }
1003
1004 impl<'a> ToTokens for PathTokens<'a> {
1005 fn to_tokens(&self, tokens: &mut Tokens) {
1006 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -07001007 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -07001009 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001010 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001011 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001012
1013 // XXX: Gross.
1014 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
1015 self.1.segments.len() - 1
1016 } else {
1017 qself.position
1018 };
David Tolnay570695e2017-06-03 16:15:13 -07001019 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -04001020 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -07001021 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001022 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001023 for (i, segment) in (&mut segments).take(pos).enumerate() {
1024 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001025 segment.item().to_tokens(tokens);
1026 qself.gt_token.to_tokens(tokens);
1027 segment.delimiter().to_tokens(tokens);
1028 } else {
1029 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001030 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001031 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001032 } else {
1033 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001034 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001035 }
David Tolnay570695e2017-06-03 16:15:13 -07001036 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -07001037 segment.to_tokens(tokens);
1038 }
1039 }
1040 }
1041
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001042 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -07001043 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -05001044 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001045 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001046 }
1047 }
1048
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001049 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -07001050 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001051 self.impl_token.to_tokens(tokens);
1052 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001053 }
1054 }
1055
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001056 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -04001057 fn to_tokens(&self, tokens: &mut Tokens) {
1058 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -05001059 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04001060 });
1061 }
1062 }
1063
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001064 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -07001065 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001066 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -05001067 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001068 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001069 }
1070 }
1071
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001072 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -07001073 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001074 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001075 }
1076 }
1077
David Tolnay323279a2017-12-29 11:26:32 -05001078 impl ToTokens for TypeMacro {
1079 fn to_tokens(&self, tokens: &mut Tokens) {
1080 self.mac.to_tokens(tokens);
1081 }
1082 }
1083
David Tolnay2ae520a2017-12-29 11:19:50 -05001084 impl ToTokens for TypeVerbatim {
1085 fn to_tokens(&self, tokens: &mut Tokens) {
1086 self.tts.to_tokens(tokens);
1087 }
1088 }
1089
David Tolnay87d0b442016-09-04 11:52:12 -07001090 impl ToTokens for Path {
1091 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001092 self.leading_colon.to_tokens(tokens);
1093 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001094 }
1095 }
1096
1097 impl ToTokens for PathSegment {
1098 fn to_tokens(&self, tokens: &mut Tokens) {
1099 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001100 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001101 }
1102 }
1103
Nika Layzellc08227a2017-12-04 16:30:17 -05001104 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001105 fn to_tokens(&self, tokens: &mut Tokens) {
1106 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001107 PathArguments::None => {}
1108 PathArguments::AngleBracketed(ref arguments) => {
1109 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001110 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001111 PathArguments::Parenthesized(ref arguments) => {
1112 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001113 }
1114 }
1115 }
1116 }
1117
Nika Layzellc08227a2017-12-04 16:30:17 -05001118 impl ToTokens for GenericArgument {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001119 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Nika Layzell357885a2017-12-04 15:47:07 -05001120 fn to_tokens(&self, tokens: &mut Tokens) {
1121 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001122 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1123 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
David Tolnay506e43a2017-12-29 11:34:36 -05001124 GenericArgument::Binding(ref tb) => tb.to_tokens(tokens),
David Tolnay8c91b882017-12-28 23:04:32 -05001125 GenericArgument::Const(ref e) => match *e {
1126 Expr::Lit(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001127
1128 // NOTE: We should probably support parsing blocks with only
1129 // expressions in them without the full feature for const
1130 // generics.
1131 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001132 Expr::Block(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001133
1134 // ERROR CORRECTION: Add braces to make sure that the
1135 // generated code is valid.
David Tolnay32954ef2017-12-26 22:43:16 -05001136 _ => token::Brace::default().surround(tokens, |tokens| {
Nika Layzellce37f332017-12-05 12:01:22 -05001137 e.to_tokens(tokens);
1138 }),
David Tolnay51382052017-12-27 13:46:21 -05001139 },
Nika Layzell357885a2017-12-04 15:47:07 -05001140 }
1141 }
1142 }
1143
Nika Layzellc08227a2017-12-04 16:30:17 -05001144 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001145 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay2d4e08a2017-12-28 23:54:07 -05001146 self.colon2_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001147 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001148 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001149 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001150 }
1151 }
1152
David Tolnay506e43a2017-12-29 11:34:36 -05001153 impl ToTokens for Binding {
David Tolnay87d0b442016-09-04 11:52:12 -07001154 fn to_tokens(&self, tokens: &mut Tokens) {
1155 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001156 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001157 self.ty.to_tokens(tokens);
1158 }
1159 }
1160
Nika Layzellc08227a2017-12-04 16:30:17 -05001161 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001162 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001163 self.paren_token.surround(tokens, |tokens| {
1164 self.inputs.to_tokens(tokens);
1165 });
1166 self.output.to_tokens(tokens);
1167 }
1168 }
1169
David Tolnayf93b90d2017-11-11 19:21:26 -08001170 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001171 fn to_tokens(&self, tokens: &mut Tokens) {
1172 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001173 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -05001174 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001175 arrow.to_tokens(tokens);
1176 ty.to_tokens(tokens);
1177 }
David Tolnay87d0b442016-09-04 11:52:12 -07001178 }
1179 }
1180 }
1181
1182 impl ToTokens for PolyTraitRef {
1183 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001184 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001185 self.trait_ref.to_tokens(tokens);
1186 }
1187 }
1188
David Tolnay62f374c2016-10-02 13:37:00 -07001189 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001190 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001191 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001192 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001193 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001194 }
1195 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001196 }
1197 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001198
Alex Crichton23a15f62017-08-28 12:34:23 -07001199 impl ToTokens for BareFnArgName {
1200 fn to_tokens(&self, tokens: &mut Tokens) {
1201 match *self {
1202 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1203 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1204 }
1205 }
1206 }
1207
David Tolnayb8d8ef52016-10-29 14:30:08 -07001208 impl ToTokens for Abi {
1209 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001210 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -05001211 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001212 }
1213 }
David Tolnay87d0b442016-09-04 11:52:12 -07001214}