blob: 970ddfdfefa6d605b5c75dcf4511438f412a8f71 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07002use super::*;
3
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_enum_of_structs! {
5 /// The different kinds of types recognized by the compiler
David Tolnayfd6bf5c2017-11-12 09:41:14 -08006 pub enum Type {
Alex Crichton62a0a592017-05-22 13:58:53 -07007 /// A variable-length array (`[T]`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -08008 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -05009 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050010 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070011 }),
12 /// A fixed length array (`[T; n]`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080013 pub Array(TypeArray {
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>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050017 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070018 }),
19 /// A raw pointer (`*const T` or `*mut T`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080020 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 pub star_token: Token![*],
22 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050023 pub mutability: Option<Token![mut]>,
24 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070025 }),
26 /// A reference (`&'a T` or `&'a mut T`)
David Tolnay0a89b4d2017-11-13 00:55:45 -080027 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080028 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070029 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050030 pub mutability: Option<Token![mut]>,
31 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070032 }),
33 /// A bare function (e.g. `fn(usize) -> bool`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080034 pub BareFn(TypeBareFn {
35 pub ty: Box<BareFnType>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
37 /// The never type (`!`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080038 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080039 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070040 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// A tuple (`(A, B, C, D, ...)`)
David Tolnay05362582017-12-26 01:33:57 -050042 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050043 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050044 pub elems: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070045 }),
46 /// A path (`module::module::...::Type`), optionally
47 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
48 ///
Nika Layzellc08227a2017-12-04 16:30:17 -050049 /// Type arguments are stored in the Path itself
David Tolnayfd6bf5c2017-11-12 09:41:14 -080050 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -070051 pub qself: Option<QSelf>,
52 pub path: Path,
53 }),
54 /// A trait object type `Bound1 + Bound2 + Bound3`
55 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080056 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -050057 pub dyn_token: Option<Token![dyn]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080058 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070059 }),
60 /// An `impl Bound1 + Bound2 + Bound3` type
61 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -080063 pub impl_token: Token![impl],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080064 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }),
66 /// No-op; kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080067 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -050068 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -050069 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
Michael Layzell93c36282017-06-04 20:43:14 -040071 /// No-op: kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080072 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -050073 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -050074 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -040075 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -080076 /// TypeKind::Infer means the type should be inferred instead of it having been
Alex Crichton62a0a592017-05-22 13:58:53 -070077 /// specified. This can appear anywhere in a type.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080078 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -080079 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A macro in the type position.
David Tolnaydecf28d2017-11-11 11:56:45 -080082 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -070083 }
84}
85
86ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A "Path" is essentially Rust's notion of a name.
David Tolnayb79ee962016-09-04 09:39:20 -070088 ///
Alex Crichton62a0a592017-05-22 13:58:53 -070089 /// It's represented as a sequence of identifiers,
90 /// along with a bunch of supporting information.
91 ///
92 /// E.g. `std::cmp::PartialEq`
93 pub struct Path {
94 /// A `::foo` path, is relative to the crate root rather than current
95 /// module (like paths in an import).
David Tolnayf8db7ba2017-11-11 22:52:16 -080096 pub leading_colon: Option<Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 /// The segments in the path: the things separated by `::`.
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 pub segments: Delimited<PathSegment, Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 }
David Tolnayb79ee962016-09-04 09:39:20 -0700100}
101
David Tolnay570695e2017-06-03 16:15:13 -0700102impl Path {
103 pub fn global(&self) -> bool {
104 self.leading_colon.is_some()
105 }
106}
107
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400109#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
110#[cfg_attr(feature = "clone-impls", derive(Clone))]
111pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112
David Tolnaydaaf7742016-10-03 11:11:43 -0700113impl<T> From<T> for Path
David Tolnay51382052017-12-27 13:46:21 -0500114where
115 T: Into<PathSegment>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700116{
David Tolnay84aa0752016-10-02 23:01:13 -0700117 fn from(segment: T) -> Self {
118 Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700119 leading_colon: None,
120 segments: vec![(segment.into(), None)].into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700121 }
122 }
123}
124
Alex Crichton62a0a592017-05-22 13:58:53 -0700125ast_struct! {
126 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
127 ///
128 /// E.g. `std`, `String` or `Box<T>`
129 pub struct PathSegment {
130 /// The identifier portion of this path segment.
131 pub ident: Ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500132 /// Type/lifetime arguments attached to this path. They come in
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
134 /// this is more than just simple syntactic sugar; the use of
135 /// parens affects the region binding rules, so we preserve the
136 /// distinction.
Nika Layzellc08227a2017-12-04 16:30:17 -0500137 pub arguments: PathArguments,
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 }
David Tolnayb79ee962016-09-04 09:39:20 -0700139}
140
David Tolnaydaaf7742016-10-03 11:11:43 -0700141impl<T> From<T> for PathSegment
David Tolnay51382052017-12-27 13:46:21 -0500142where
143 T: Into<Ident>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700144{
David Tolnay84aa0752016-10-02 23:01:13 -0700145 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700146 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700147 ident: ident.into(),
Nika Layzellc08227a2017-12-04 16:30:17 -0500148 arguments: PathArguments::None,
David Tolnayb79ee962016-09-04 09:39:20 -0700149 }
150 }
151}
152
Alex Crichton62a0a592017-05-22 13:58:53 -0700153ast_enum! {
Nika Layzellc08227a2017-12-04 16:30:17 -0500154 /// Arguments of a path segment.
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 ///
156 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
Nika Layzellc08227a2017-12-04 16:30:17 -0500157 pub enum PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700158 None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500160 AngleBracketed(AngleBracketedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500162 Parenthesized(ParenthesizedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 }
David Tolnayb79ee962016-09-04 09:39:20 -0700164}
165
Nika Layzellc08227a2017-12-04 16:30:17 -0500166impl Default for PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700167 fn default() -> Self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500168 PathArguments::None
David Tolnayb79ee962016-09-04 09:39:20 -0700169 }
David Tolnay570695e2017-06-03 16:15:13 -0700170}
David Tolnay5332d4b2016-10-30 14:25:22 -0700171
Nika Layzellc08227a2017-12-04 16:30:17 -0500172impl PathArguments {
David Tolnay5332d4b2016-10-30 14:25:22 -0700173 pub fn is_empty(&self) -> bool {
174 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500175 PathArguments::None => true,
176 PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(),
177 PathArguments::Parenthesized(_) => false,
David Tolnay5332d4b2016-10-30 14:25:22 -0700178 }
179 }
David Tolnayb79ee962016-09-04 09:39:20 -0700180}
181
Nika Layzell357885a2017-12-04 15:47:07 -0500182ast_enum! {
183 /// A individual generic argument, like `'a`, `T`, or `Item=T`.
Nika Layzellc08227a2017-12-04 16:30:17 -0500184 pub enum GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500185 /// The lifetime parameters for this path segment.
186 Lifetime(Lifetime),
187 /// The type parameters for this path segment, if present.
188 Type(Type),
189 /// Bindings (equality constraints) on associated types, if present.
190 ///
191 /// E.g., `Foo<A=Bar>`.
192 TypeBinding(TypeBinding),
Nika Layzellc680e612017-12-04 19:07:20 -0500193 /// Const expression. Must be inside of a block.
194 ///
195 /// NOTE: Identity expressions are represented as Type arguments, as
196 /// they are indistinguishable syntactically.
Nika Layzellce37f332017-12-05 12:01:22 -0500197 Const(Expr),
Nika Layzell357885a2017-12-04 15:47:07 -0500198 }
199}
200
Alex Crichton62a0a592017-05-22 13:58:53 -0700201ast_struct! {
202 /// A path like `Foo<'a, T>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500203 pub struct AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500204 pub colon2_token: Option<Token![::]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub lt_token: Token![<],
Nika Layzellc08227a2017-12-04 16:30:17 -0500206 pub args: Delimited<GenericArgument, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800207 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700208 }
209}
210
211ast_struct! {
212 /// Bind a type to an associated type: `A=Foo`.
213 pub struct TypeBinding {
214 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800216 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 }
218}
219
Alex Crichton62a0a592017-05-22 13:58:53 -0700220ast_struct! {
221 /// A path like `Foo(A,B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500222 pub struct ParenthesizedGenericArguments {
David Tolnay32954ef2017-12-26 22:43:16 -0500223 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 /// `(A, B)`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800225 pub inputs: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700226 /// `C`
David Tolnayf93b90d2017-11-11 19:21:26 -0800227 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700228 }
229}
230
231ast_struct! {
232 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700233 /// The `for<'a>` in `for<'a> Foo<&'a T>`
234 pub bound_lifetimes: Option<BoundLifetimes>,
David Tolnay7d38c7e2017-12-25 22:31:50 -0500235 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 pub trait_ref: Path,
237 }
238}
239
240ast_struct! {
241 /// The explicit Self type in a "qualified path". The actual
242 /// path, including the trait and the associated item, is stored
243 /// separately. `position` represents the index of the associated
244 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700245 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500246 /// ```text
Alex Crichton62a0a592017-05-22 13:58:53 -0700247 /// <Vec<T> as a::b::Trait>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500248 /// ^~~~~~ ~~~~~~~~~~~~~~^
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700250 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 /// <Vec<T>>::AssociatedItem
David Tolnaybcf26022017-12-25 22:10:52 -0500252 /// ^~~~~~ ^
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 /// ty position = 0
254 /// ```
255 pub struct QSelf {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800256 pub lt_token: Token![<],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800257 pub ty: Box<Type>,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400258 pub position: usize,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800259 pub as_token: Option<Token![as]>,
260 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 }
262}
263
264ast_struct! {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800265 pub struct BareFnType {
David Tolnay9b258702017-12-29 02:24:41 -0500266 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 pub abi: Option<Abi>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800268 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500269 pub lifetimes: Option<BoundLifetimes>,
David Tolnay32954ef2017-12-26 22:43:16 -0500270 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800271 pub inputs: Delimited<BareFnArg, Token![,]>,
272 pub variadic: Option<Token![...]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800273 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 }
275}
276
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700277ast_struct! {
278 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800279 pub extern_token: Token![extern],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700280 pub kind: AbiKind,
281 }
282}
283
Alex Crichton62a0a592017-05-22 13:58:53 -0700284ast_enum! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700285 pub enum AbiKind {
286 Named(Lit),
287 Default,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }
289}
290
291ast_struct! {
292 /// An argument in a function type.
293 ///
294 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
295 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800296 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800297 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }
299}
300
Alex Crichton23a15f62017-08-28 12:34:23 -0700301ast_enum! {
302 /// Names of arguments in the `BareFnArg` structure
303 pub enum BareFnArgName {
304 /// Argument with the provided name
305 Named(Ident),
306 /// Argument matched with `_`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800307 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700308 }
309}
Alex Crichton62a0a592017-05-22 13:58:53 -0700310
311ast_enum! {
David Tolnayf93b90d2017-11-11 19:21:26 -0800312 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 /// Return type is not specified.
314 ///
315 /// Functions default to `()` and
316 /// closures default to inference. Span points to where return
317 /// type would be inserted.
318 Default,
319 /// Everything else
David Tolnay4a3f59a2017-12-28 21:21:12 -0500320 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700321 }
David Tolnayb79ee962016-09-04 09:39:20 -0700322}
323
David Tolnay86eca752016-09-04 11:26:41 -0700324#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700325pub mod parsing {
326 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400327 use synom::Synom;
David Tolnayda4049b2016-09-04 10:59:23 -0700328
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800329 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400330 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700331
Alex Crichton954046c2017-05-30 21:49:42 -0700332 fn description() -> Option<&'static str> {
333 Some("type")
334 }
335 }
David Tolnay0047c712016-12-21 21:59:25 -0500336
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800337 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400338 /// In some positions, types may not contain the `+` character, to
339 /// disambiguate them. For example in the expression `1 as T`, T may not
340 /// contain a `+` character.
341 ///
342 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400343 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400344 }
345
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800346 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
347 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400348 |
David Tolnay05362582017-12-26 01:33:57 -0500349 // must be before TypeTuple
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800350 syn!(TypeParen) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400351 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500352 // must be before TypePath
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800353 syn!(Macro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400354 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500355 // must be before TypeTraitObject
356 call!(TypePath::parse, allow_plus) => { Type::Path }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400357 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800358 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400359 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800360 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400361 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800362 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400363 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800364 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400365 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800366 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400367 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800368 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400369 |
David Tolnay05362582017-12-26 01:33:57 -0500370 syn!(TypeTuple) => { Type::Tuple }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400371 |
David Tolnay7d38c7e2017-12-25 22:31:50 -0500372 // Don't try parsing more than one trait bound if we aren't allowing it
373 call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400374 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800375 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700376 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800377 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400378 ));
379
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800380 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400381 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800382 brackets!(syn!(Type)),
383 |(ty, b)| TypeSlice {
David Tolnayeadbda32017-12-29 02:33:47 -0500384 elem: Box::new(ty),
Michael Layzell92639a52017-06-01 00:07:44 -0400385 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700386 }
Michael Layzell92639a52017-06-01 00:07:44 -0400387 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700388 }
David Tolnayb79ee962016-09-04 09:39:20 -0700389
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800390 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400391 named!(parse -> Self, map!(
392 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800393 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400395 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700396 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400397 )),
398 |((elem, semi, len), brackets)| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800399 TypeArray {
David Tolnayeadbda32017-12-29 02:33:47 -0500400 elem: Box::new(elem),
401 len: len,
Michael Layzell92639a52017-06-01 00:07:44 -0400402 bracket_token: brackets,
403 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700404 }
405 }
Michael Layzell92639a52017-06-01 00:07:44 -0400406 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700407 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700408
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800409 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400410 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800411 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400412 mutability: alt!(
David Tolnay24237fb2017-12-29 02:15:26 -0500413 keyword!(const) => { |c| (None, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400414 |
David Tolnay24237fb2017-12-29 02:15:26 -0500415 keyword!(mut) => { |m| (Some(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400416 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800417 target: call!(Type::without_plus) >>
418 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400419 const_token: mutability.1,
420 star_token: star,
David Tolnay136aaa32017-12-29 02:37:36 -0500421 mutability: mutability.0,
422 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400423 })
424 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700425 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700426
David Tolnay0a89b4d2017-11-13 00:55:45 -0800427 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400428 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400430 life: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500431 mutability: option!(keyword!(mut)) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400432 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800434 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400435 lifetime: life,
David Tolnay136aaa32017-12-29 02:37:36 -0500436 mutability: mutability,
437 elem: Box::new(target),
Michael Layzell92639a52017-06-01 00:07:44 -0400438 and_token: amp,
439 })
440 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700441 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700442
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800443 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400444 named!(parse -> Self, do_parse!(
445 lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500446 unsafety: option!(keyword!(unsafe)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400447 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800448 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400449 parens: parens!(do_parse!(
450 inputs: call!(Delimited::parse_terminated) >>
451 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800452 punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400453 (inputs, variadic)
454 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800455 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800456 (TypeBareFn {
457 ty: Box::new(BareFnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400458 unsafety: unsafety,
459 abi: abi,
460 lifetimes: lifetimes,
461 output: output,
462 variadic: (parens.0).1,
463 fn_token: fn_,
464 paren_token: parens.1,
465 inputs: (parens.0).0,
466 }),
467 })
468 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700469 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700470
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400472 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800473 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800474 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400475 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700476 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700477
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800478 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700479 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800480 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800481 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700482 ));
483 }
484
David Tolnay05362582017-12-26 01:33:57 -0500485 impl Synom for TypeTuple {
Michael Layzell92639a52017-06-01 00:07:44 -0400486 named!(parse -> Self, do_parse!(
487 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -0500488 (TypeTuple {
David Tolnayeadbda32017-12-29 02:33:47 -0500489 elems: data.0,
Michael Layzell92639a52017-06-01 00:07:44 -0400490 paren_token: data.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400491 })
492 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700493 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700494
David Tolnay7d38c7e2017-12-25 22:31:50 -0500495 impl TypePath {
496 named!(parse(allow_plus: bool) -> Self, do_parse!(
497 qpath: qpath >>
498 parenthesized: cond!(
499 qpath.1.segments.last().unwrap().item().arguments.is_empty(),
500 option!(syn!(ParenthesizedGenericArguments))
501 ) >>
502 cond!(allow_plus, not!(peek!(punct!(+)))) >>
503 ({
504 let (qself, mut path) = qpath;
505 if let Some(Some(parenthesized)) = parenthesized {
506 let parenthesized = PathArguments::Parenthesized(parenthesized);
507 path.segments.last_mut().unwrap().item_mut().arguments = parenthesized;
508 }
509 TypePath { qself: qself, path: path }
510 })
511 ));
512 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700513
David Tolnay9636c052016-10-02 17:11:17 -0700514 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700515 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700516 |
517 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800518 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800519 this: syn!(Type) >>
David Tolnay7d38c7e2017-12-25 22:31:50 -0500520 path: option!(tuple!(keyword!(as), syn!(Path))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800521 gt: punct!(>) >>
522 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700523 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700524 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400525 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700526 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700527 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700528 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700529 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700530 }
Alex Crichton954046c2017-05-30 21:49:42 -0700531 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700532 path.segments.push(item);
533 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400534 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700535 }
536 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400537 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700538 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700539 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700540 })
David Tolnay9636c052016-10-02 17:11:17 -0700541 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 };
543 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700544 lt_token: lt,
545 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400547 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700548 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700549 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700550 })
551 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700552 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800553 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700554 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700555
Nika Layzellc08227a2017-12-04 16:30:17 -0500556 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400557 named!(parse -> Self, do_parse!(
558 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800559 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500560 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400561 paren_token: data.1,
562 inputs: data.0,
563 output: output,
564 })
565 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700566 }
567
David Tolnayf93b90d2017-11-11 19:21:26 -0800568 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400569 named!(parse -> Self, alt!(
570 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800571 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800572 ty: syn!(Type) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -0500573 (ReturnType::Type(arrow, Box::new(ty)))
Michael Layzell92639a52017-06-01 00:07:44 -0400574 )
575 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800576 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400577 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700578 }
579
David Tolnay7d38c7e2017-12-25 22:31:50 -0500580 impl TypeTraitObject {
581 // Only allow multiple trait references if allow_plus is true.
582 named!(parse(allow_plus: bool) -> Self, do_parse!(
583 dyn_token: option!(keyword!(dyn)) >>
584 bounds: alt!(
585 cond_reduce!(allow_plus, call!(Delimited::parse_terminated_nonempty))
586 |
587 syn!(TypeParamBound) => { |x| vec![x].into() }
588 ) >>
589 (TypeTraitObject {
590 dyn_token: dyn_token,
591 bounds: bounds,
592 })
593 ));
594 }
David Tolnay6414da72016-10-08 00:55:17 -0700595
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800596 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400597 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800598 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400599 // NOTE: rust-lang/rust#34511 includes discussion about whether or
600 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500601 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800602 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400603 impl_token: impl_,
604 bounds: elem,
605 })
606 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700607 }
David Tolnayb79ee962016-09-04 09:39:20 -0700608
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800609 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400610 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800611 data: grouped!(syn!(Type)) >>
612 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400613 group_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500614 elem: Box::new(data.0),
Michael Layzell93c36282017-06-04 20:43:14 -0400615 })
616 ));
617 }
618
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800619 impl Synom for TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400620 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800621 data: parens!(syn!(Type)) >>
622 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400623 paren_token: data.1,
David Tolnayeadbda32017-12-29 02:33:47 -0500624 elem: Box::new(data.0),
Michael Layzell92639a52017-06-01 00:07:44 -0400625 })
626 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700627 }
David Tolnayb79ee962016-09-04 09:39:20 -0700628
Alex Crichton954046c2017-05-30 21:49:42 -0700629 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400630 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800631 colon: option!(punct!(::)) >>
David Tolnaye45b59f2017-12-25 18:44:49 -0500632 segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >>
633 cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400634 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700635 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400636 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400637 })
638 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700639
640 fn description() -> Option<&'static str> {
641 Some("path")
642 }
Alex Crichton954046c2017-05-30 21:49:42 -0700643 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700644
Nika Layzellc680e612017-12-04 19:07:20 -0500645 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500646 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500647 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500648 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500649 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500650 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500651 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500652 syn!(TypeBinding) => { GenericArgument::TypeBinding }
Nika Layzell357885a2017-12-04 15:47:07 -0500653 ));
654 }
655
Nika Layzellc680e612017-12-04 19:07:20 -0500656 #[cfg(feature = "full")]
657 impl Synom for GenericArgument {
658 named!(parse -> Self, alt!(
659 call!(ty_no_eq_after) => { GenericArgument::Type }
660 |
661 syn!(Lifetime) => { GenericArgument::Lifetime }
662 |
663 syn!(TypeBinding) => { GenericArgument::TypeBinding }
664 |
David Tolnay8c91b882017-12-28 23:04:32 -0500665 syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) }
Nika Layzellce37f332017-12-05 12:01:22 -0500666 |
David Tolnay8c91b882017-12-28 23:04:32 -0500667 syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500668 ));
669 }
670
Nika Layzellc08227a2017-12-04 16:30:17 -0500671 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500672 named!(parse -> Self, do_parse!(
David Tolnay2d4e08a2017-12-28 23:54:07 -0500673 colon2: option!(punct!(::)) >>
Nika Layzell357885a2017-12-04 15:47:07 -0500674 lt: punct!(<) >>
675 args: call!(Delimited::parse_terminated) >>
676 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500677 (AngleBracketedGenericArguments {
David Tolnay2d4e08a2017-12-28 23:54:07 -0500678 colon2_token: colon2,
Nika Layzell357885a2017-12-04 15:47:07 -0500679 lt_token: lt,
680 args: args,
681 gt_token: gt,
682 })
683 ));
684 }
685
Alex Crichton954046c2017-05-30 21:49:42 -0700686 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400687 named!(parse -> Self, alt!(
688 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700689 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500690 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400691 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700692 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500693 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400694 })
695 )
696 |
697 mod_style_path_segment
698 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700699 }
David Tolnay570695e2017-06-03 16:15:13 -0700700
David Tolnayd60cfec2017-12-29 00:21:38 -0500701 named!(pub ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700702
Alex Crichton954046c2017-05-30 21:49:42 -0700703 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400704 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800705 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400706 segments: call!(Delimited::parse_separated_nonempty_with,
707 mod_style_path_segment) >>
708 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700709 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400710 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400711 })
712 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700713 }
Arnavionf2dada12017-04-20 23:55:20 -0700714
715 named!(mod_style_path_segment -> PathSegment, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500716 syn!(Ident) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700717 |
David Tolnay5f332a92017-12-26 00:42:45 -0500718 keyword!(super) => { Into::into }
719 |
720 keyword!(self) => { Into::into }
721 |
722 keyword!(Self) => { Into::into }
723 |
724 keyword!(crate) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700725 ));
726
Alex Crichton954046c2017-05-30 21:49:42 -0700727 impl Synom for TypeBinding {
Michael Layzell92639a52017-06-01 00:07:44 -0400728 named!(parse -> Self, do_parse!(
729 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800730 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800731 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400732 (TypeBinding {
733 ident: id,
734 eq_token: eq,
735 ty: ty,
736 })
737 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700738 }
739
740 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400741 named!(parse -> Self, do_parse!(
742 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
743 trait_ref: syn!(Path) >>
744 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500745 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
746 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400747 )) >>
748 ({
749 let mut trait_ref = trait_ref;
750 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500751 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400752 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500753 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400754 }
755 PolyTraitRef {
756 bound_lifetimes: bound_lifetimes,
757 trait_ref: trait_ref,
758 }
759 })
760 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700761 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700762
Alex Crichton954046c2017-05-30 21:49:42 -0700763 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400764 named!(parse -> Self, do_parse!(
765 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700766 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 not!(punct!(::)) >>
768 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400769 (name, colon)
770 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800771 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400772 (BareFnArg {
773 name: name,
774 ty: ty,
775 })
776 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700777 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700778
Alex Crichton23a15f62017-08-28 12:34:23 -0700779 impl Synom for BareFnArgName {
780 named!(parse -> Self, alt!(
781 map!(syn!(Ident), BareFnArgName::Named)
782 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800783 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700784 ));
785 }
786
Alex Crichton954046c2017-05-30 21:49:42 -0700787 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400788 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800789 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400790 // TODO: this parses all literals, not just strings
791 name: option!(syn!(Lit)) >>
792 (Abi {
793 extern_token: extern_,
794 kind: match name {
795 Some(name) => AbiKind::Named(name),
796 None => AbiKind::Default,
797 },
798 })
799 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700800 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700801}
David Tolnay87d0b442016-09-04 11:52:12 -0700802
803#[cfg(feature = "printing")]
804mod printing {
805 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500806 use quote::{ToTokens, Tokens};
David Tolnay87d0b442016-09-04 11:52:12 -0700807
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800808 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700809 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700810 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500811 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700812 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700813 }
814 }
815
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800816 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700817 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700818 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500819 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700820 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500821 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700822 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700823 }
824 }
825
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800826 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700827 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700828 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500829 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500830 Some(ref tok) => tok.to_tokens(tokens),
831 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700832 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400833 }
834 }
David Tolnay136aaa32017-12-29 02:37:36 -0500835 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700836 }
837 }
838
David Tolnay0a89b4d2017-11-13 00:55:45 -0800839 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700840 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700841 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700842 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500843 self.mutability.to_tokens(tokens);
844 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700845 }
846 }
847
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800848 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700849 fn to_tokens(&self, tokens: &mut Tokens) {
850 self.ty.to_tokens(tokens)
851 }
852 }
853
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800854 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700855 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700856 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700857 }
858 }
859
David Tolnay05362582017-12-26 01:33:57 -0500860 impl ToTokens for TypeTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -0700861 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700862 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500863 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700864 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700865 }
866 }
867
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800868 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700869 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700870 PathTokens(&self.qself, &self.path).to_tokens(tokens);
871 }
872 }
873
874 impl<'a> ToTokens for PathTokens<'a> {
875 fn to_tokens(&self, tokens: &mut Tokens) {
876 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700877 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700878 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700879 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700881 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400882
883 // XXX: Gross.
884 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
885 self.1.segments.len() - 1
886 } else {
887 qself.position
888 };
David Tolnay570695e2017-06-03 16:15:13 -0700889 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400890 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700891 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700893 for (i, segment) in (&mut segments).take(pos).enumerate() {
894 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700895 segment.item().to_tokens(tokens);
896 qself.gt_token.to_tokens(tokens);
897 segment.delimiter().to_tokens(tokens);
898 } else {
899 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700900 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700901 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700902 } else {
903 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700904 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700905 }
David Tolnay570695e2017-06-03 16:15:13 -0700906 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700907 segment.to_tokens(tokens);
908 }
909 }
910 }
911
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800912 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700913 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500914 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700915 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700916 }
917 }
918
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800919 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700920 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700921 self.impl_token.to_tokens(tokens);
922 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700923 }
924 }
925
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800926 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400927 fn to_tokens(&self, tokens: &mut Tokens) {
928 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500929 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400930 });
931 }
932 }
933
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800934 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700935 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700936 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500937 self.elem.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 TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700944 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700945 }
946 }
947
948 impl ToTokens for Path {
949 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700950 self.leading_colon.to_tokens(tokens);
951 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700952 }
953 }
954
955 impl ToTokens for PathSegment {
956 fn to_tokens(&self, tokens: &mut Tokens) {
957 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -0500958 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700959 }
960 }
961
Nika Layzellc08227a2017-12-04 16:30:17 -0500962 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -0700963 fn to_tokens(&self, tokens: &mut Tokens) {
964 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500965 PathArguments::None => {}
966 PathArguments::AngleBracketed(ref arguments) => {
967 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700968 }
Nika Layzellc08227a2017-12-04 16:30:17 -0500969 PathArguments::Parenthesized(ref arguments) => {
970 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700971 }
972 }
973 }
974 }
975
Nika Layzellc08227a2017-12-04 16:30:17 -0500976 impl ToTokens for GenericArgument {
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500977 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Nika Layzell357885a2017-12-04 15:47:07 -0500978 fn to_tokens(&self, tokens: &mut Tokens) {
979 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500980 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
981 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
982 GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens),
David Tolnay8c91b882017-12-28 23:04:32 -0500983 GenericArgument::Const(ref e) => match *e {
984 Expr::Lit(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -0500985
986 // NOTE: We should probably support parsing blocks with only
987 // expressions in them without the full feature for const
988 // generics.
989 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500990 Expr::Block(_) => e.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -0500991
992 // ERROR CORRECTION: Add braces to make sure that the
993 // generated code is valid.
David Tolnay32954ef2017-12-26 22:43:16 -0500994 _ => token::Brace::default().surround(tokens, |tokens| {
Nika Layzellce37f332017-12-05 12:01:22 -0500995 e.to_tokens(tokens);
996 }),
David Tolnay51382052017-12-27 13:46:21 -0500997 },
Nika Layzell357885a2017-12-04 15:47:07 -0500998 }
999 }
1000 }
1001
Nika Layzellc08227a2017-12-04 16:30:17 -05001002 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001003 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay2d4e08a2017-12-28 23:54:07 -05001004 self.colon2_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001005 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001006 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001007 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001008 }
1009 }
1010
1011 impl ToTokens for TypeBinding {
1012 fn to_tokens(&self, tokens: &mut Tokens) {
1013 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001014 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001015 self.ty.to_tokens(tokens);
1016 }
1017 }
1018
Nika Layzellc08227a2017-12-04 16:30:17 -05001019 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001020 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 self.paren_token.surround(tokens, |tokens| {
1022 self.inputs.to_tokens(tokens);
1023 });
1024 self.output.to_tokens(tokens);
1025 }
1026 }
1027
David Tolnayf93b90d2017-11-11 19:21:26 -08001028 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001029 fn to_tokens(&self, tokens: &mut Tokens) {
1030 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001031 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -05001032 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001033 arrow.to_tokens(tokens);
1034 ty.to_tokens(tokens);
1035 }
David Tolnay87d0b442016-09-04 11:52:12 -07001036 }
1037 }
1038 }
1039
1040 impl ToTokens for PolyTraitRef {
1041 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001042 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001043 self.trait_ref.to_tokens(tokens);
1044 }
1045 }
1046
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001047 impl ToTokens for BareFnType {
David Tolnay87d0b442016-09-04 11:52:12 -07001048 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001049 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001050 self.unsafety.to_tokens(tokens);
1051 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001052 self.fn_token.to_tokens(tokens);
1053 self.paren_token.surround(tokens, |tokens| {
1054 self.inputs.to_tokens(tokens);
David Tolnay73cc6442017-12-27 22:17:57 -05001055 if let Some(ref variadic) = self.variadic {
David Tolnay0bdb0552017-12-27 21:31:51 -05001056 if !self.inputs.empty_or_trailing() {
1057 let span = variadic.0[0];
1058 <Token![,]>::new(span).to_tokens(tokens);
1059 }
1060 variadic.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001061 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001062 });
1063 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001064 }
1065 }
1066
David Tolnay62f374c2016-10-02 13:37:00 -07001067 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001068 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001069 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001070 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001072 }
1073 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001074 }
1075 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001076
Alex Crichton23a15f62017-08-28 12:34:23 -07001077 impl ToTokens for BareFnArgName {
1078 fn to_tokens(&self, tokens: &mut Tokens) {
1079 match *self {
1080 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1081 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1082 }
1083 }
1084 }
1085
David Tolnayb8d8ef52016-10-29 14:30:08 -07001086 impl ToTokens for Abi {
1087 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001088 self.extern_token.to_tokens(tokens);
1089 match self.kind {
1090 AbiKind::Named(ref named) => named.to_tokens(tokens),
1091 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001092 }
1093 }
1094 }
David Tolnay87d0b442016-09-04 11:52:12 -07001095}