blob: f7b9011fc4a4638c6352b6bb22b34aa6b19dc98f [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 {
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 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800406
407 fn description() -> Option<&'static str> {
408 Some("slice type")
409 }
Alex Crichton954046c2017-05-30 21:49:42 -0700410 }
David Tolnayb79ee962016-09-04 09:39:20 -0700411
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800412 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400413 named!(parse -> Self, map!(
414 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800415 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800416 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400417 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700418 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400419 )),
420 |((elem, semi, len), brackets)| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800421 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500422 elem: Box::new(elem),
423 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400424 bracket_token: brackets,
425 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700426 }
427 }
Michael Layzell92639a52017-06-01 00:07:44 -0400428 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800429
430 fn description() -> Option<&'static str> {
431 Some("array type")
432 }
Alex Crichton954046c2017-05-30 21:49:42 -0700433 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700434
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800435 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400436 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400438 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500439 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400440 |
David Tolnay24237fb2017-12-29 02:15:26 -0500441 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400442 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800443 target: call!(Type::without_plus) >>
444 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400445 const_token: mutability.1,
446 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500447 mutability: mutability.0,
448 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400449 })
450 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800451
452 fn description() -> Option<&'static str> {
453 Some("raw pointer type")
454 }
Alex Crichton954046c2017-05-30 21:49:42 -0700455 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700456
David Tolnay0a89b4d2017-11-13 00:55:45 -0800457 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400458 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800459 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400460 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500461 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400462 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800463 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800464 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400465 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500466 mutability: mutability,
467 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400468 and_token: amp,
469 })
470 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800471
472 fn description() -> Option<&'static str> {
473 Some("reference type")
474 }
Alex Crichton954046c2017-05-30 21:49:42 -0700475 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700476
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800477 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400478 named!(parse -> Self, do_parse!(
479 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500480 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400481 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800482 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400483 parens: parens!(do_parse!(
484 inputs: call!(Delimited::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500485 variadic: option!(cond_reduce!(inputs.empty_or_trailing(), punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400486 (inputs, variadic)
487 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800488 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800489 (TypeBareFn {
David Tolnaybe7a9592017-12-29 02:39:53 -0500490 unsafety: unsafety,
491 abi: abi,
492 lifetimes: lifetimes,
493 output: output,
494 variadic: (parens.0).1,
495 fn_token: fn_,
496 paren_token: parens.1,
497 inputs: (parens.0).0,
Michael Layzell92639a52017-06-01 00:07:44 -0400498 })
499 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800500
501 fn description() -> Option<&'static str> {
502 Some("`fn` type")
503 }
Alex Crichton954046c2017-05-30 21:49:42 -0700504 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700505
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800506 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400507 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800508 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800509 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400510 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800511
512 fn description() -> Option<&'static str> {
513 Some("never type: `!`")
514 }
Alex Crichton954046c2017-05-30 21:49:42 -0700515 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700516
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800517 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700518 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800519 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800520 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700521 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800522
523 fn description() -> Option<&'static str> {
524 Some("inferred type: `_`")
525 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700526 }
527
David Tolnay05362582017-12-26 01:33:57 -0500528 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400529 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500530 data: parens!(Delimited::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -0500531 (TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -0500532 elems: data.0,
Michael Layzell92639a52017-06-01 00:07:44 -0400533 paren_token: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400534 })
535 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800536
537 fn description() -> Option<&'static str> {
538 Some("tuple type")
539 }
Alex Crichton954046c2017-05-30 21:49:42 -0700540 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700541
David Tolnay323279a2017-12-29 11:26:32 -0500542 impl Synom for TypeMacro {
543 named!(parse -> Self, map!(syn!(Macro), |mac| TypeMacro { mac: mac }));
544 }
545
David Tolnay0a169d42017-12-29 17:57:29 -0500546 impl Synom for TypePath {
547 named!(parse -> Self, call!(Self::parse, false));
548 }
549
David Tolnay7d38c7e2017-12-25 22:31:50 -0500550 impl TypePath {
551 named!(parse(allow_plus: bool) -> Self, do_parse!(
552 qpath: qpath >>
553 parenthesized: cond!(
554 qpath.1.segments.last().unwrap().item().arguments.is_empty(),
555 option!(syn!(ParenthesizedGenericArguments))
556 ) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500557 cond!(allow_plus, not!(punct!(+))) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500558 ({
559 let (qself, mut path) = qpath;
560 if let Some(Some(parenthesized)) = parenthesized {
561 let parenthesized = PathArguments::Parenthesized(parenthesized);
562 path.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
563 }
564 TypePath { qself: qself, path: path }
565 })
566 ));
567 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700568
David Tolnay9636c052016-10-02 17:11:17 -0700569 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700570 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700571 |
572 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800573 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800574 this: syn!(Type) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500575 path: option!(tuple!(keyword!(as), syn!(Path))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 gt: punct!(>) >>
577 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700578 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700579 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400580 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700581 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700582 let pos = path.segments.len();
David Tolnaydc03aec2017-12-30 01:54:18 -0500583 if !path.segments.empty_or_trailing() {
Alex Crichton954046c2017-05-30 21:49:42 -0700584 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700585 }
Alex Crichton954046c2017-05-30 21:49:42 -0700586 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 path.segments.push(item);
588 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400589 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700590 }
591 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400592 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700593 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700594 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700595 })
David Tolnay9636c052016-10-02 17:11:17 -0700596 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597 };
598 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700599 lt_token: lt,
600 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700601 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400602 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700603 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700605 })
606 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700607 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800608 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700609 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700610
Nika Layzellc08227a2017-12-04 16:30:17 -0500611 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400612 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -0500613 data: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800614 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500615 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400616 paren_token: data.1,
617 inputs: data.0,
618 output: output,
619 })
620 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800621
622 fn description() -> Option<&'static str> {
623 Some("parenthesized generic arguments: `Foo(A, B, ..) -> T`")
624 }
Alex Crichton954046c2017-05-30 21:49:42 -0700625 }
626
David Tolnayf93b90d2017-11-11 19:21:26 -0800627 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400628 named!(parse -> Self, alt!(
629 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800630 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800631 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500632 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400633 )
634 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800635 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400636 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800637
638 fn description() -> Option<&'static str> {
639 Some("return type")
640 }
Alex Crichton954046c2017-05-30 21:49:42 -0700641 }
642
David Tolnay0a169d42017-12-29 17:57:29 -0500643 impl Synom for TypeTraitObject {
644 named!(parse -> Self, call!(Self::parse, true));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800645
646 fn description() -> Option<&'static str> {
647 Some("trait object type")
648 }
David Tolnay0a169d42017-12-29 17:57:29 -0500649 }
650
David Tolnay7d38c7e2017-12-25 22:31:50 -0500651 impl TypeTraitObject {
David Tolnay0a169d42017-12-29 17:57:29 -0500652 named!(pub without_plus -> Self, call!(Self::parse, false));
653
David Tolnay7d38c7e2017-12-25 22:31:50 -0500654 // Only allow multiple trait references if allow_plus is true.
655 named!(parse(allow_plus: bool) -> Self, do_parse!(
656 dyn_token: option!(keyword!(dyn)) >>
657 bounds: alt!(
David Tolnaye64213b2017-12-30 00:24:20 -0500658 cond_reduce!(allow_plus, Delimited::parse_terminated_nonempty)
David Tolnay7d38c7e2017-12-25 22:31:50 -0500659 |
660 syn!(TypeParamBound) => { |x| vec![x].into() }
661 ) >>
662 (TypeTraitObject {
663 dyn_token: dyn_token,
664 bounds: bounds,
665 })
666 ));
667 }
David Tolnay6414da72016-10-08 00:55:17 -0700668
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800669 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400670 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800671 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400672 // NOTE: rust-lang/rust#34511 includes discussion about whether or
673 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500674 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800675 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400676 impl_token: impl_,
677 bounds: elem,
678 })
679 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800680
681 fn description() -> Option<&'static str> {
682 Some("`impl Trait` type")
683 }
Alex Crichton954046c2017-05-30 21:49:42 -0700684 }
David Tolnayb79ee962016-09-04 09:39:20 -0700685
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800686 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400687 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800688 data: grouped!(syn!(Type)) >>
689 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400690 group_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500691 elem: Box::new(data.0),
Michael Layzell93c36282017-06-04 20:43:14 -0400692 })
693 ));
694 }
695
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800696 impl Synom for TypeParen {
David Tolnay0a169d42017-12-29 17:57:29 -0500697 named!(parse -> Self, call!(Self::parse, false));
698 }
699
700 impl TypeParen {
701 named!(parse(allow_plus: bool) -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800702 data: parens!(syn!(Type)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500703 cond!(allow_plus, not!(punct!(+))) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800704 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400705 paren_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500706 elem: Box::new(data.0),
Michael Layzell92639a52017-06-01 00:07:44 -0400707 })
708 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700709 }
David Tolnayb79ee962016-09-04 09:39:20 -0700710
Alex Crichton954046c2017-05-30 21:49:42 -0700711 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400712 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800713 colon: option!(punct!(::)) >>
David Tolnaye45b59f2017-12-25 18:44:49 -0500714 segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >>
715 cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400716 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700717 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400718 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400719 })
720 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700721
722 fn description() -> Option<&'static str> {
723 Some("path")
724 }
Alex Crichton954046c2017-05-30 21:49:42 -0700725 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700726
Nika Layzellc680e612017-12-04 19:07:20 -0500727 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500728 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500729 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500730 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500731 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500732 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500733 |
David Tolnay506e43a2017-12-29 11:34:36 -0500734 syn!(Binding) => { GenericArgument::Binding }
Nika Layzell357885a2017-12-04 15:47:07 -0500735 ));
736 }
737
Nika Layzellc680e612017-12-04 19:07:20 -0500738 #[cfg(feature = "full")]
739 impl Synom for GenericArgument {
740 named!(parse -> Self, alt!(
741 call!(ty_no_eq_after) => { GenericArgument::Type }
742 |
743 syn!(Lifetime) => { GenericArgument::Lifetime }
744 |
David Tolnay506e43a2017-12-29 11:34:36 -0500745 syn!(Binding) => { GenericArgument::Binding }
Nika Layzellc680e612017-12-04 19:07:20 -0500746 |
David Tolnay8c91b882017-12-28 23:04:32 -0500747 syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) }
Nika Layzellce37f332017-12-05 12:01:22 -0500748 |
David Tolnay8c91b882017-12-28 23:04:32 -0500749 syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500750 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800751
752 fn description() -> Option<&'static str> {
753 Some("generic argument")
754 }
Nika Layzellc680e612017-12-04 19:07:20 -0500755 }
756
Nika Layzellc08227a2017-12-04 16:30:17 -0500757 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500758 named!(parse -> Self, do_parse!(
David Tolnay2d4e08a2017-12-28 23:54:07 -0500759 colon2: option!(punct!(::)) >>
Nika Layzell357885a2017-12-04 15:47:07 -0500760 lt: punct!(<) >>
761 args: call!(Delimited::parse_terminated) >>
762 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500763 (AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500764 colon2_token: colon2,
Nika Layzell357885a2017-12-04 15:47:07 -0500765 lt_token: lt,
766 args: args,
767 gt_token: gt,
768 })
769 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800770
771 fn description() -> Option<&'static str> {
772 Some("angle bracketed generic arguments")
773 }
Nika Layzell357885a2017-12-04 15:47:07 -0500774 }
775
Alex Crichton954046c2017-05-30 21:49:42 -0700776 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400777 named!(parse -> Self, alt!(
778 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700779 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500780 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400781 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700782 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500783 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400784 })
785 )
786 |
787 mod_style_path_segment
788 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800789
790 fn description() -> Option<&'static str> {
791 Some("path segment")
792 }
Alex Crichton954046c2017-05-30 21:49:42 -0700793 }
David Tolnay570695e2017-06-03 16:15:13 -0700794
David Tolnaydc03aec2017-12-30 01:54:18 -0500795 named!(pub ty_no_eq_after -> Type, do_parse!(
796 ty: syn!(Type) >>
797 not!(punct!(=)) >>
798 (ty)
799 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700800
Alex Crichton954046c2017-05-30 21:49:42 -0700801 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400802 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800803 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400804 segments: call!(Delimited::parse_separated_nonempty_with,
805 mod_style_path_segment) >>
806 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700807 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400808 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400809 })
810 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700811 }
Arnavionf2dada12017-04-20 23:55:20 -0700812
813 named!(mod_style_path_segment -> PathSegment, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500814 syn!(Ident) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700815 |
David Tolnay5f332a92017-12-26 00:42:45 -0500816 keyword!(super) => { Into::into }
817 |
818 keyword!(self) => { Into::into }
819 |
820 keyword!(Self) => { Into::into }
821 |
822 keyword!(crate) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700823 ));
824
David Tolnay506e43a2017-12-29 11:34:36 -0500825 impl Synom for Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400826 named!(parse -> Self, do_parse!(
827 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800828 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800829 ty: syn!(Type) >>
David Tolnay506e43a2017-12-29 11:34:36 -0500830 (Binding {
Michael Layzell92639a52017-06-01 00:07:44 -0400831 ident: id,
832 eq_token: eq,
833 ty: ty,
834 })
835 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800836
837 fn description() -> Option<&'static str> {
838 Some("associated type binding")
839 }
Alex Crichton954046c2017-05-30 21:49:42 -0700840 }
841
842 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400843 named!(parse -> Self, do_parse!(
844 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
845 trait_ref: syn!(Path) >>
846 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500847 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
848 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400849 )) >>
850 ({
851 let mut trait_ref = trait_ref;
852 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500853 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400854 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500855 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400856 }
857 PolyTraitRef {
858 bound_lifetimes: bound_lifetimes,
859 trait_ref: trait_ref,
860 }
861 })
862 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800863
864 fn description() -> Option<&'static str> {
865 Some("poly trait reference")
866 }
Alex Crichton954046c2017-05-30 21:49:42 -0700867 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700868
Alex Crichton954046c2017-05-30 21:49:42 -0700869 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400870 named!(parse -> Self, do_parse!(
871 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700872 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800873 not!(punct!(::)) >>
874 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400875 (name, colon)
876 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800877 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400878 (BareFnArg {
879 name: name,
880 ty: ty,
881 })
882 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800883
884 fn description() -> Option<&'static str> {
885 Some("function type argument")
886 }
Alex Crichton954046c2017-05-30 21:49:42 -0700887 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700888
Alex Crichton23a15f62017-08-28 12:34:23 -0700889 impl Synom for BareFnArgName {
890 named!(parse -> Self, alt!(
891 map!(syn!(Ident), BareFnArgName::Named)
892 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800893 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700894 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800895
896 fn description() -> Option<&'static str> {
897 Some("function argument name")
898 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700899 }
900
Alex Crichton954046c2017-05-30 21:49:42 -0700901 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400902 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400904 // TODO: this parses all literals, not just strings
905 name: option!(syn!(Lit)) >>
906 (Abi {
907 extern_token: extern_,
David Tolnayd5125762017-12-29 02:42:17 -0500908 name: name,
Michael Layzell92639a52017-06-01 00:07:44 -0400909 })
910 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800911
912 fn description() -> Option<&'static str> {
913 Some("ABI qualifier")
914 }
Alex Crichton954046c2017-05-30 21:49:42 -0700915 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700916}
David Tolnay87d0b442016-09-04 11:52:12 -0700917
918#[cfg(feature = "printing")]
919mod printing {
920 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500921 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700922
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800923 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700924 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700925 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500926 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700927 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700928 }
929 }
930
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800931 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700932 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700933 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500934 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700935 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500936 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700937 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700938 }
939 }
940
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800941 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700943 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500944 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500945 Some(ref tok) => tok.to_tokens(tokens),
946 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700947 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400948 }
949 }
David Tolnay136aaa32017-12-29 02:37:36 -0500950 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 }
952 }
953
David Tolnay0a89b4d2017-11-13 00:55:45 -0800954 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700956 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700957 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500958 self.mutability.to_tokens(tokens);
959 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700960 }
961 }
962
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800963 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500965 self.lifetimes.to_tokens(tokens);
966 self.unsafety.to_tokens(tokens);
967 self.abi.to_tokens(tokens);
968 self.fn_token.to_tokens(tokens);
969 self.paren_token.surround(tokens, |tokens| {
970 self.inputs.to_tokens(tokens);
971 if let Some(ref variadic) = self.variadic {
972 if !self.inputs.empty_or_trailing() {
973 let span = variadic.0[0];
974 <Token![,]>::new(span).to_tokens(tokens);
975 }
976 variadic.to_tokens(tokens);
977 }
978 });
979 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700980 }
981 }
982
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800983 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700985 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 }
987 }
988
David Tolnay05362582017-12-26 01:33:57 -0500989 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700990 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500992 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700993 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700994 }
995 }
996
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800997 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700998 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700999 PathTokens(&self.qself, &self.path).to_tokens(tokens);
1000 }
1001 }
1002
1003 impl<'a> ToTokens for PathTokens<'a> {
1004 fn to_tokens(&self, tokens: &mut Tokens) {
1005 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -07001006 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001007 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -07001008 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001009 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001010 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001011
1012 // XXX: Gross.
1013 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
1014 self.1.segments.len() - 1
1015 } else {
1016 qself.position
1017 };
David Tolnay570695e2017-06-03 16:15:13 -07001018 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -04001019 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -07001020 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001022 for (i, segment) in (&mut segments).take(pos).enumerate() {
1023 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001024 segment.item().to_tokens(tokens);
1025 qself.gt_token.to_tokens(tokens);
1026 segment.delimiter().to_tokens(tokens);
1027 } else {
1028 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001029 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001030 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001031 } else {
1032 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001033 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001034 }
David Tolnay570695e2017-06-03 16:15:13 -07001035 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -07001036 segment.to_tokens(tokens);
1037 }
1038 }
1039 }
1040
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001041 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -07001042 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -05001043 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001044 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001045 }
1046 }
1047
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001048 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -07001049 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001050 self.impl_token.to_tokens(tokens);
1051 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001052 }
1053 }
1054
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001055 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -04001056 fn to_tokens(&self, tokens: &mut Tokens) {
1057 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -05001058 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04001059 });
1060 }
1061 }
1062
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001063 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -07001064 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001065 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -05001066 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001067 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001068 }
1069 }
1070
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001071 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -07001072 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001073 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001074 }
1075 }
1076
David Tolnay323279a2017-12-29 11:26:32 -05001077 impl ToTokens for TypeMacro {
1078 fn to_tokens(&self, tokens: &mut Tokens) {
1079 self.mac.to_tokens(tokens);
1080 }
1081 }
1082
David Tolnay2ae520a2017-12-29 11:19:50 -05001083 impl ToTokens for TypeVerbatim {
1084 fn to_tokens(&self, tokens: &mut Tokens) {
1085 self.tts.to_tokens(tokens);
1086 }
1087 }
1088
David Tolnay87d0b442016-09-04 11:52:12 -07001089 impl ToTokens for Path {
1090 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001091 self.leading_colon.to_tokens(tokens);
1092 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001093 }
1094 }
1095
1096 impl ToTokens for PathSegment {
1097 fn to_tokens(&self, tokens: &mut Tokens) {
1098 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001099 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001100 }
1101 }
1102
Nika Layzellc08227a2017-12-04 16:30:17 -05001103 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001104 fn to_tokens(&self, tokens: &mut Tokens) {
1105 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001106 PathArguments::None => {}
1107 PathArguments::AngleBracketed(ref arguments) => {
1108 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001109 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001110 PathArguments::Parenthesized(ref arguments) => {
1111 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001112 }
1113 }
1114 }
1115 }
1116
Nika Layzellc08227a2017-12-04 16:30:17 -05001117 impl ToTokens for GenericArgument {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001118 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Nika Layzell357885a2017-12-04 15:47:07 -05001119 fn to_tokens(&self, tokens: &mut Tokens) {
1120 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001121 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1122 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
David Tolnay506e43a2017-12-29 11:34:36 -05001123 GenericArgument::Binding(ref tb) => tb.to_tokens(tokens),
David Tolnay8c91b882017-12-28 23:04:32 -05001124 GenericArgument::Const(ref e) => match *e {
1125 Expr::Lit(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001126
1127 // NOTE: We should probably support parsing blocks with only
1128 // expressions in them without the full feature for const
1129 // generics.
1130 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001131 Expr::Block(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001132
1133 // ERROR CORRECTION: Add braces to make sure that the
1134 // generated code is valid.
David Tolnay32954ef2017-12-26 22:43:16 -05001135 _ => token::Brace::default().surround(tokens, |tokens| {
Nika Layzellce37f332017-12-05 12:01:22 -05001136 e.to_tokens(tokens);
1137 }),
David Tolnay51382052017-12-27 13:46:21 -05001138 },
Nika Layzell357885a2017-12-04 15:47:07 -05001139 }
1140 }
1141 }
1142
Nika Layzellc08227a2017-12-04 16:30:17 -05001143 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001144 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay2d4e08a2017-12-28 23:54:07 -05001145 self.colon2_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001146 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001147 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001148 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001149 }
1150 }
1151
David Tolnay506e43a2017-12-29 11:34:36 -05001152 impl ToTokens for Binding {
David Tolnay87d0b442016-09-04 11:52:12 -07001153 fn to_tokens(&self, tokens: &mut Tokens) {
1154 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001155 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001156 self.ty.to_tokens(tokens);
1157 }
1158 }
1159
Nika Layzellc08227a2017-12-04 16:30:17 -05001160 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001161 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001162 self.paren_token.surround(tokens, |tokens| {
1163 self.inputs.to_tokens(tokens);
1164 });
1165 self.output.to_tokens(tokens);
1166 }
1167 }
1168
David Tolnayf93b90d2017-11-11 19:21:26 -08001169 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001170 fn to_tokens(&self, tokens: &mut Tokens) {
1171 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001172 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -05001173 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001174 arrow.to_tokens(tokens);
1175 ty.to_tokens(tokens);
1176 }
David Tolnay87d0b442016-09-04 11:52:12 -07001177 }
1178 }
1179 }
1180
1181 impl ToTokens for PolyTraitRef {
1182 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001183 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001184 self.trait_ref.to_tokens(tokens);
1185 }
1186 }
1187
David Tolnay62f374c2016-10-02 13:37:00 -07001188 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001189 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001190 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001191 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001192 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001193 }
1194 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001195 }
1196 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001197
Alex Crichton23a15f62017-08-28 12:34:23 -07001198 impl ToTokens for BareFnArgName {
1199 fn to_tokens(&self, tokens: &mut Tokens) {
1200 match *self {
1201 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1202 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1203 }
1204 }
1205 }
1206
David Tolnayb8d8ef52016-10-29 14:30:08 -07001207 impl ToTokens for Abi {
1208 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001209 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -05001210 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001211 }
1212 }
David Tolnay87d0b442016-09-04 11:52:12 -07001213}