blob: 085bf9cd4c620c8bff2bc3e4e3370e00510fcd2f [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 {
9 pub ty: Box<Type>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070010 pub bracket_token: tokens::Bracket,
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 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070014 pub bracket_token: tokens::Bracket,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080015 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub semi_token: Token![;],
Michael Layzelld7ee9102017-06-07 10:02:19 -040017 pub amt: 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 Tolnayfd6bf5c2017-11-12 09:41:14 -080023 pub ty: Box<MutType>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }),
25 /// A reference (`&'a T` or `&'a mut T`)
David Tolnay0a89b4d2017-11-13 00:55:45 -080026 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070028 pub lifetime: Option<Lifetime>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080029 pub ty: Box<MutType>,
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
31 /// A bare function (e.g. `fn(usize) -> bool`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080032 pub BareFn(TypeBareFn {
33 pub ty: Box<BareFnType>,
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
35 /// The never type (`!`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080036 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070038 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070039 /// A tuple (`(A, B, C, D, ...)`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -080040 pub Tup(TypeTup {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070041 pub paren_token: tokens::Paren,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080042 pub tys: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 pub lone_comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070044 }),
45 /// A path (`module::module::...::Type`), optionally
46 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
47 ///
Nika Layzellc08227a2017-12-04 16:30:17 -050048 /// Type arguments are stored in the Path itself
David Tolnayfd6bf5c2017-11-12 09:41:14 -080049 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -070050 pub qself: Option<QSelf>,
51 pub path: Path,
52 }),
53 /// A trait object type `Bound1 + Bound2 + Bound3`
54 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080055 pub TraitObject(TypeTraitObject {
56 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// An `impl Bound1 + Bound2 + Bound3` type
59 /// where `Bound` is a trait or a lifetime.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080060 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -080061 pub impl_token: Token![impl],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070063 }),
64 /// No-op; kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080065 pub Paren(TypeParen {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 pub paren_token: tokens::Paren,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080067 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070068 }),
Michael Layzell93c36282017-06-04 20:43:14 -040069 /// No-op: kept solely so that we can pretty-print faithfully
David Tolnayfd6bf5c2017-11-12 09:41:14 -080070 pub Group(TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -040071 pub group_token: tokens::Group,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080072 pub ty: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -040073 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -080074 /// TypeKind::Infer means the type should be inferred instead of it having been
Alex Crichton62a0a592017-05-22 13:58:53 -070075 /// specified. This can appear anywhere in a type.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080076 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070078 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070079 /// A macro in the type position.
David Tolnaydecf28d2017-11-11 11:56:45 -080080 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -070081 }
82}
83
84ast_struct! {
David Tolnayfd6bf5c2017-11-12 09:41:14 -080085 pub struct MutType {
86 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -070087 pub mutability: Mutability,
88 }
89}
90
91ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -070092 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070093 pub enum Mutability {
David Tolnayf8db7ba2017-11-11 22:52:16 -080094 Mutable(Token![mut]),
Alex Crichton62a0a592017-05-22 13:58:53 -070095 Immutable,
96 }
97}
98
99ast_struct! {
100 /// A "Path" is essentially Rust's notion of a name.
David Tolnayb79ee962016-09-04 09:39:20 -0700101 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 /// It's represented as a sequence of identifiers,
103 /// along with a bunch of supporting information.
104 ///
105 /// E.g. `std::cmp::PartialEq`
106 pub struct Path {
107 /// A `::foo` path, is relative to the crate root rather than current
108 /// module (like paths in an import).
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub leading_colon: Option<Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700110 /// The segments in the path: the things separated by `::`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub segments: Delimited<PathSegment, Token![::]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }
David Tolnayb79ee962016-09-04 09:39:20 -0700113}
114
David Tolnay570695e2017-06-03 16:15:13 -0700115impl Path {
116 pub fn global(&self) -> bool {
117 self.leading_colon.is_some()
118 }
119}
120
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400122#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
123#[cfg_attr(feature = "clone-impls", derive(Clone))]
124pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700125
David Tolnaydaaf7742016-10-03 11:11:43 -0700126impl<T> From<T> for Path
127 where T: Into<PathSegment>
128{
David Tolnay84aa0752016-10-02 23:01:13 -0700129 fn from(segment: T) -> Self {
130 Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700131 leading_colon: None,
132 segments: vec![(segment.into(), None)].into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700133 }
134 }
135}
136
Alex Crichton62a0a592017-05-22 13:58:53 -0700137ast_struct! {
138 /// A segment of a path: an identifier, an optional lifetime, and a set of types.
139 ///
140 /// E.g. `std`, `String` or `Box<T>`
141 pub struct PathSegment {
142 /// The identifier portion of this path segment.
143 pub ident: Ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500144 /// Type/lifetime arguments attached to this path. They come in
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
146 /// this is more than just simple syntactic sugar; the use of
147 /// parens affects the region binding rules, so we preserve the
148 /// distinction.
Nika Layzellc08227a2017-12-04 16:30:17 -0500149 pub arguments: PathArguments,
Alex Crichton62a0a592017-05-22 13:58:53 -0700150 }
David Tolnayb79ee962016-09-04 09:39:20 -0700151}
152
David Tolnaydaaf7742016-10-03 11:11:43 -0700153impl<T> From<T> for PathSegment
154 where T: Into<Ident>
155{
David Tolnay84aa0752016-10-02 23:01:13 -0700156 fn from(ident: T) -> Self {
David Tolnayb79ee962016-09-04 09:39:20 -0700157 PathSegment {
David Tolnay84aa0752016-10-02 23:01:13 -0700158 ident: ident.into(),
Nika Layzellc08227a2017-12-04 16:30:17 -0500159 arguments: PathArguments::None,
David Tolnayb79ee962016-09-04 09:39:20 -0700160 }
161 }
162}
163
Alex Crichton62a0a592017-05-22 13:58:53 -0700164ast_enum! {
Nika Layzellc08227a2017-12-04 16:30:17 -0500165 /// Arguments of a path segment.
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 ///
167 /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
Nika Layzellc08227a2017-12-04 16:30:17 -0500168 pub enum PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700169 None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500171 AngleBracketed(AngleBracketedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500173 Parenthesized(ParenthesizedGenericArguments),
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }
David Tolnayb79ee962016-09-04 09:39:20 -0700175}
176
Nika Layzellc08227a2017-12-04 16:30:17 -0500177impl Default for PathArguments {
David Tolnay570695e2017-06-03 16:15:13 -0700178 fn default() -> Self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500179 PathArguments::None
David Tolnayb79ee962016-09-04 09:39:20 -0700180 }
David Tolnay570695e2017-06-03 16:15:13 -0700181}
David Tolnay5332d4b2016-10-30 14:25:22 -0700182
Nika Layzellc08227a2017-12-04 16:30:17 -0500183impl PathArguments {
David Tolnay5332d4b2016-10-30 14:25:22 -0700184 pub fn is_empty(&self) -> bool {
185 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -0500186 PathArguments::None => true,
187 PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(),
188 PathArguments::Parenthesized(_) => false,
David Tolnay5332d4b2016-10-30 14:25:22 -0700189 }
190 }
David Tolnayb79ee962016-09-04 09:39:20 -0700191}
192
Nika Layzell357885a2017-12-04 15:47:07 -0500193ast_enum! {
194 /// A individual generic argument, like `'a`, `T`, or `Item=T`.
Nika Layzellc08227a2017-12-04 16:30:17 -0500195 pub enum GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500196 /// The lifetime parameters for this path segment.
197 Lifetime(Lifetime),
198 /// The type parameters for this path segment, if present.
199 Type(Type),
200 /// Bindings (equality constraints) on associated types, if present.
201 ///
202 /// E.g., `Foo<A=Bar>`.
203 TypeBinding(TypeBinding),
Nika Layzellc680e612017-12-04 19:07:20 -0500204 /// Const expression. Must be inside of a block.
205 ///
206 /// NOTE: Identity expressions are represented as Type arguments, as
207 /// they are indistinguishable syntactically.
208 Const(ExprBlock),
Nika Layzell357885a2017-12-04 15:47:07 -0500209 }
210}
211
Alex Crichton62a0a592017-05-22 13:58:53 -0700212ast_struct! {
213 /// A path like `Foo<'a, T>`
Nika Layzellc08227a2017-12-04 16:30:17 -0500214 pub struct AngleBracketedGenericArguments {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub turbofish: Option<Token![::]>,
216 pub lt_token: Token![<],
Nika Layzellc08227a2017-12-04 16:30:17 -0500217 pub args: Delimited<GenericArgument, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800218 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700219 }
220}
221
222ast_struct! {
223 /// Bind a type to an associated type: `A=Foo`.
224 pub struct TypeBinding {
225 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800226 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800227 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700228 }
229}
230
231
232ast_struct! {
233 /// A path like `Foo(A,B) -> C`
Nika Layzellc08227a2017-12-04 16:30:17 -0500234 pub struct ParenthesizedGenericArguments {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700235 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 /// `(A, B)`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800237 pub inputs: Delimited<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700238 /// `C`
David Tolnayf93b90d2017-11-11 19:21:26 -0800239 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }
241}
242
243ast_struct! {
244 pub struct PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700245 /// The `for<'a>` in `for<'a> Foo<&'a T>`
246 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700247 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
248 pub trait_ref: Path,
249 }
250}
251
252ast_struct! {
253 /// The explicit Self type in a "qualified path". The actual
254 /// path, including the trait and the associated item, is stored
255 /// separately. `position` represents the index of the associated
256 /// item qualified with this Self type.
David Tolnayb79ee962016-09-04 09:39:20 -0700257 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// ```rust,ignore
259 /// <Vec<T> as a::b::Trait>::AssociatedItem
260 /// ^~~~~ ~~~~~~~~~~~~~~^
261 /// ty position = 3
David Tolnayb79ee962016-09-04 09:39:20 -0700262 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700263 /// <Vec<T>>::AssociatedItem
264 /// ^~~~~ ^
265 /// ty position = 0
266 /// ```
267 pub struct QSelf {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800268 pub lt_token: Token![<],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800269 pub ty: Box<Type>,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400270 pub position: usize,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800271 pub as_token: Option<Token![as]>,
272 pub gt_token: Token![>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 }
274}
275
276ast_struct! {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800277 pub struct BareFnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700278 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub unsafety: Unsafety,
280 pub abi: Option<Abi>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800281 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700282 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800283 pub inputs: Delimited<BareFnArg, Token![,]>,
284 pub variadic: Option<Token![...]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800285 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 }
287}
288
289ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700290 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 pub enum Unsafety {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800292 Unsafe(Token![unsafe]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 Normal,
294 }
295}
296
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700297ast_struct! {
298 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800299 pub extern_token: Token![extern],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700300 pub kind: AbiKind,
301 }
302}
303
Alex Crichton62a0a592017-05-22 13:58:53 -0700304ast_enum! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700305 pub enum AbiKind {
306 Named(Lit),
307 Default,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 }
309}
310
311ast_struct! {
312 /// An argument in a function type.
313 ///
314 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
315 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800316 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800317 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 }
319}
320
Alex Crichton23a15f62017-08-28 12:34:23 -0700321ast_enum! {
322 /// Names of arguments in the `BareFnArg` structure
323 pub enum BareFnArgName {
324 /// Argument with the provided name
325 Named(Ident),
326 /// Argument matched with `_`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800327 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700328 }
329}
Alex Crichton62a0a592017-05-22 13:58:53 -0700330
331ast_enum! {
David Tolnayf93b90d2017-11-11 19:21:26 -0800332 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 /// Return type is not specified.
334 ///
335 /// Functions default to `()` and
336 /// closures default to inference. Span points to where return
337 /// type would be inserted.
338 Default,
339 /// Everything else
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800340 Type(Type, Token![->]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }
David Tolnayb79ee962016-09-04 09:39:20 -0700342}
343
David Tolnay86eca752016-09-04 11:26:41 -0700344#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700345pub mod parsing {
346 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400347 use synom::Synom;
David Tolnayda4049b2016-09-04 10:59:23 -0700348
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800349 impl Synom for Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400350 named!(parse -> Self, call!(ambig_ty, true));
David Tolnayb79ee962016-09-04 09:39:20 -0700351
Alex Crichton954046c2017-05-30 21:49:42 -0700352 fn description() -> Option<&'static str> {
353 Some("type")
354 }
355 }
David Tolnay0047c712016-12-21 21:59:25 -0500356
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800357 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400358 /// In some positions, types may not contain the `+` character, to
359 /// disambiguate them. For example in the expression `1 as T`, T may not
360 /// contain a `+` character.
361 ///
362 /// This parser does not allow a `+`, while the default parser does.
Michael Layzell6a5a1642017-06-04 19:35:15 -0400363 named!(pub without_plus -> Self, call!(ambig_ty, false));
Michael Layzell9bf2b002017-06-04 18:49:53 -0400364 }
365
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800366 named!(ambig_ty(allow_plus: bool) -> Type, alt!(
367 syn!(TypeGroup) => { Type::Group }
Michael Layzell93c36282017-06-04 20:43:14 -0400368 |
Michael Layzell9bf2b002017-06-04 18:49:53 -0400369 // must be before mac
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800370 syn!(TypeParen) => { Type::Paren }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400371 |
372 // must be before path
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800373 syn!(Macro) => { Type::Macro }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400374 |
375 // must be before ty_poly_trait_ref
376 call!(ty_path, allow_plus)
377 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800378 syn!(TypeSlice) => { Type::Slice }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400379 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800380 syn!(TypeArray) => { Type::Array }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400381 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800382 syn!(TypePtr) => { Type::Ptr }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400383 |
David Tolnay0a89b4d2017-11-13 00:55:45 -0800384 syn!(TypeReference) => { Type::Reference }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400385 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800386 syn!(TypeBareFn) => { Type::BareFn }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400387 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800388 syn!(TypeNever) => { Type::Never }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400389 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800390 syn!(TypeTup) => { Type::Tup }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400391 |
392 // Don't try parsing poly_trait_ref if we aren't allowing it
393 call!(ty_poly_trait_ref, allow_plus)
394 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800395 syn!(TypeImplTrait) => { Type::ImplTrait }
Alex Crichton23a15f62017-08-28 12:34:23 -0700396 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800397 syn!(TypeInfer) => { Type::Infer }
Michael Layzell9bf2b002017-06-04 18:49:53 -0400398 ));
399
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800400 impl Synom for TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400401 named!(parse -> Self, map!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800402 brackets!(syn!(Type)),
403 |(ty, b)| TypeSlice {
Michael Layzell92639a52017-06-01 00:07:44 -0400404 ty: Box::new(ty),
405 bracket_token: b,
Alex Crichton954046c2017-05-30 21:49:42 -0700406 }
Michael Layzell92639a52017-06-01 00:07:44 -0400407 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700408 }
David Tolnayb79ee962016-09-04 09:39:20 -0700409
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800410 impl Synom for TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400411 named!(parse -> Self, map!(
412 brackets!(do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800413 elem: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 semi: punct!(;) >>
Michael Layzelld7ee9102017-06-07 10:02:19 -0400415 len: syn!(Expr) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700416 (elem, semi, len)
Michael Layzell92639a52017-06-01 00:07:44 -0400417 )),
418 |((elem, semi, len), brackets)| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800419 TypeArray {
Michael Layzell92639a52017-06-01 00:07:44 -0400420 ty: Box::new(elem),
421 amt: len,
422 bracket_token: brackets,
423 semi_token: semi,
Alex Crichton954046c2017-05-30 21:49:42 -0700424 }
425 }
Michael Layzell92639a52017-06-01 00:07:44 -0400426 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700427 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700428
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800429 impl Synom for TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400430 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800431 star: punct!(*) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400432 mutability: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800433 keyword!(const) => { |c| (Mutability::Immutable, Some(c)) }
Michael Layzell92639a52017-06-01 00:07:44 -0400434 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 keyword!(mut) => { |m| (Mutability::Mutable(m), None) }
Michael Layzell92639a52017-06-01 00:07:44 -0400436 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800437 target: call!(Type::without_plus) >>
438 (TypePtr {
Michael Layzell92639a52017-06-01 00:07:44 -0400439 const_token: mutability.1,
440 star_token: star,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800441 ty: Box::new(MutType {
Michael Layzell92639a52017-06-01 00:07:44 -0400442 ty: target,
443 mutability: mutability.0,
444 }),
445 })
446 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700447 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700448
David Tolnay0a89b4d2017-11-13 00:55:45 -0800449 impl Synom for TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400450 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800451 amp: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400452 life: option!(syn!(Lifetime)) >>
453 mutability: syn!(Mutability) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400454 // & binds tighter than +, so we don't allow + here.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800455 target: call!(Type::without_plus) >>
David Tolnay0a89b4d2017-11-13 00:55:45 -0800456 (TypeReference {
Michael Layzell92639a52017-06-01 00:07:44 -0400457 lifetime: life,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800458 ty: Box::new(MutType {
Michael Layzell92639a52017-06-01 00:07:44 -0400459 ty: target,
460 mutability: mutability,
461 }),
462 and_token: amp,
463 })
464 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700465 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700466
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800467 impl Synom for TypeBareFn {
Michael Layzell92639a52017-06-01 00:07:44 -0400468 named!(parse -> Self, do_parse!(
469 lifetimes: option!(syn!(BoundLifetimes)) >>
470 unsafety: syn!(Unsafety) >>
471 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800472 fn_: keyword!(fn) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400473 parens: parens!(do_parse!(
474 inputs: call!(Delimited::parse_terminated) >>
475 variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800476 punct!(...))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400477 (inputs, variadic)
478 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800479 output: syn!(ReturnType) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800480 (TypeBareFn {
481 ty: Box::new(BareFnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400482 unsafety: unsafety,
483 abi: abi,
484 lifetimes: lifetimes,
485 output: output,
486 variadic: (parens.0).1,
487 fn_token: fn_,
488 paren_token: parens.1,
489 inputs: (parens.0).0,
490 }),
491 })
492 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700493 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700494
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800495 impl Synom for TypeNever {
Michael Layzell92639a52017-06-01 00:07:44 -0400496 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800497 punct!(!),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800498 |b| TypeNever { bang_token: b }
Michael Layzell92639a52017-06-01 00:07:44 -0400499 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700500 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700501
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800502 impl Synom for TypeInfer {
Alex Crichton23a15f62017-08-28 12:34:23 -0700503 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800504 punct!(_),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 |u| TypeInfer { underscore_token: u }
Alex Crichton23a15f62017-08-28 12:34:23 -0700506 ));
507 }
508
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800509 impl Synom for TypeTup {
Michael Layzell92639a52017-06-01 00:07:44 -0400510 named!(parse -> Self, do_parse!(
511 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800512 (TypeTup {
Michael Layzell92639a52017-06-01 00:07:44 -0400513 tys: data.0,
514 paren_token: data.1,
515 lone_comma: None, // TODO: does this just not parse?
516 })
517 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700518 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700519
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800520 named!(ty_path(allow_plus: bool) -> Type, do_parse!(
David Tolnay6414da72016-10-08 00:55:17 -0700521 qpath: qpath >>
David Tolnayf6c74402016-10-08 02:31:26 -0700522 parenthesized: cond!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500523 qpath.1.segments.get(qpath.1.segments.len() - 1).item().arguments.is_empty(),
524 option!(syn!(ParenthesizedGenericArguments))
David Tolnayf6c74402016-10-08 02:31:26 -0700525 ) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400526 // Only allow parsing additional bounds if allow_plus is true.
527 bounds: alt!(
528 cond_reduce!(
529 allow_plus,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 many0!(tuple!(punct!(+), syn!(TypeParamBound)))
Michael Layzell9bf2b002017-06-04 18:49:53 -0400531 )
532 |
533 value!(vec![])
534 ) >>
David Tolnay6414da72016-10-08 00:55:17 -0700535 ({
David Tolnayf6c74402016-10-08 02:31:26 -0700536 let (qself, mut path) = qpath;
537 if let Some(Some(parenthesized)) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500538 let parenthesized = PathArguments::Parenthesized(parenthesized);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700539 let len = path.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500540 path.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
David Tolnayf6c74402016-10-08 02:31:26 -0700541 }
David Tolnay6414da72016-10-08 00:55:17 -0700542 if bounds.is_empty() {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800543 TypePath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700544 } else {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800545 let path = TypeParamBound::Trait(
David Tolnay02c907f2017-01-23 00:06:37 -0800546 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700547 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800548 trait_ref: path,
549 },
550 TraitBoundModifier::None,
551 );
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700552 let mut new_bounds = Delimited::new();
553 new_bounds.push_first(path);
Alex Crichton954046c2017-05-30 21:49:42 -0700554 for (_tok, bound) in bounds {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 new_bounds.push_default(bound);
556 }
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800557 TypeTraitObject { bounds: new_bounds }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700558 }
559 })
560 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700561
David Tolnay9636c052016-10-02 17:11:17 -0700562 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700563 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700564 |
565 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800566 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800567 this: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700568 path: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800569 as_: keyword!(as) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700570 path: syn!(Path) >>
571 (as_, path)
David Tolnay9636c052016-10-02 17:11:17 -0700572 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800573 gt: punct!(>) >>
574 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700575 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700576 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400577 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700578 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700579 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700580 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700581 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700582 }
Alex Crichton954046c2017-05-30 21:49:42 -0700583 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700584 path.segments.push(item);
585 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400586 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700587 }
588 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400589 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700590 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700591 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700592 })
David Tolnay9636c052016-10-02 17:11:17 -0700593 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700594 };
595 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700596 lt_token: lt,
597 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700598 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400599 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700600 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700601 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700602 })
603 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700604 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800605 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700606 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700607
Nika Layzellc08227a2017-12-04 16:30:17 -0500608 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400609 named!(parse -> Self, do_parse!(
610 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800611 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500612 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400613 paren_token: data.1,
614 inputs: data.0,
615 output: output,
616 })
617 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700618 }
619
David Tolnayf93b90d2017-11-11 19:21:26 -0800620 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400621 named!(parse -> Self, alt!(
622 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800623 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800624 ty: syn!(Type) >>
625 (ReturnType::Type(ty, arrow))
Michael Layzell92639a52017-06-01 00:07:44 -0400626 )
627 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800628 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400629 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700630 }
631
Michael Layzell9bf2b002017-06-04 18:49:53 -0400632 // Only allow multiple trait references if allow_plus is true.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800633 named!(ty_poly_trait_ref(allow_plus: bool) -> Type, alt!(
Michael Layzell9bf2b002017-06-04 18:49:53 -0400634 cond_reduce!(allow_plus, call!(Delimited::parse_separated_nonempty)) => {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800635 |x| TypeTraitObject { bounds: x }.into()
Michael Layzell9bf2b002017-06-04 18:49:53 -0400636 }
637 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800638 syn!(TypeParamBound) => {
639 |x| TypeTraitObject { bounds: vec![x].into() }.into()
Michael Layzell9bf2b002017-06-04 18:49:53 -0400640 }
David Tolnay6414da72016-10-08 00:55:17 -0700641 ));
642
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800643 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400644 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800645 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400646 // NOTE: rust-lang/rust#34511 includes discussion about whether or
647 // not + should be allowed in ImplTrait directly without ().
Michael Layzell92639a52017-06-01 00:07:44 -0400648 elem: call!(Delimited::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800649 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400650 impl_token: impl_,
651 bounds: elem,
652 })
653 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700654 }
David Tolnayb79ee962016-09-04 09:39:20 -0700655
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800656 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400657 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800658 data: grouped!(syn!(Type)) >>
659 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400660 group_token: data.1,
661 ty: Box::new(data.0),
662 })
663 ));
664 }
665
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800666 impl Synom for TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400667 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800668 data: parens!(syn!(Type)) >>
669 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400670 paren_token: data.1,
671 ty: Box::new(data.0),
672 })
673 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700674 }
David Tolnayb79ee962016-09-04 09:39:20 -0700675
Alex Crichton954046c2017-05-30 21:49:42 -0700676 impl Synom for Mutability {
Michael Layzell92639a52017-06-01 00:07:44 -0400677 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800678 keyword!(mut) => { Mutability::Mutable }
Michael Layzell92639a52017-06-01 00:07:44 -0400679 |
680 epsilon!() => { |_| Mutability::Immutable }
681 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700682 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700683
Alex Crichton954046c2017-05-30 21:49:42 -0700684 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400685 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800686 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400687 segments: call!(Delimited::parse_separated_nonempty) >>
688 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700689 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400690 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400691 })
692 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700693
694 fn description() -> Option<&'static str> {
695 Some("path")
696 }
Alex Crichton954046c2017-05-30 21:49:42 -0700697 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700698
Nika Layzellc680e612017-12-04 19:07:20 -0500699 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500700 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500701 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500702 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500703 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500704 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500705 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500706 syn!(TypeBinding) => { GenericArgument::TypeBinding }
Nika Layzell357885a2017-12-04 15:47:07 -0500707 ));
708 }
709
Nika Layzellc680e612017-12-04 19:07:20 -0500710 #[cfg(feature = "full")]
711 impl Synom for GenericArgument {
712 named!(parse -> Self, alt!(
713 call!(ty_no_eq_after) => { GenericArgument::Type }
714 |
715 syn!(Lifetime) => { GenericArgument::Lifetime }
716 |
717 syn!(TypeBinding) => { GenericArgument::TypeBinding }
718 |
719 syn!(ExprBlock) => { GenericArgument::Const }
720 ));
721 }
722
Nika Layzellc08227a2017-12-04 16:30:17 -0500723 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500724 named!(parse -> Self, do_parse!(
725 turbofish: option!(punct!(::)) >>
726 lt: punct!(<) >>
727 args: call!(Delimited::parse_terminated) >>
728 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500729 (AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500730 turbofish: turbofish,
731 lt_token: lt,
732 args: args,
733 gt_token: gt,
734 })
735 ));
736 }
737
Alex Crichton954046c2017-05-30 21:49:42 -0700738 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400739 named!(parse -> Self, alt!(
740 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700741 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500742 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400743 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700744 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500745 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400746 })
747 )
748 |
749 mod_style_path_segment
750 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700751 }
David Tolnay570695e2017-06-03 16:15:13 -0700752
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800753 named!(ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700754
Alex Crichton954046c2017-05-30 21:49:42 -0700755 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400756 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800757 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400758 segments: call!(Delimited::parse_separated_nonempty_with,
759 mod_style_path_segment) >>
760 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700761 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400762 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400763 })
764 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700765 }
Arnavionf2dada12017-04-20 23:55:20 -0700766
767 named!(mod_style_path_segment -> PathSegment, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700768 map!(syn!(Ident), Into::into)
Arnavionf2dada12017-04-20 23:55:20 -0700769 |
Alex Crichton954046c2017-05-30 21:49:42 -0700770 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800771 keyword!(super) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700772 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800773 keyword!(self) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700774 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800775 keyword!(Self) => { Into::into }
Alex Crichton954046c2017-05-30 21:49:42 -0700776 )
Arnavionf2dada12017-04-20 23:55:20 -0700777 ));
778
Alex Crichton954046c2017-05-30 21:49:42 -0700779 impl Synom for TypeBinding {
Michael Layzell92639a52017-06-01 00:07:44 -0400780 named!(parse -> Self, do_parse!(
781 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800782 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800783 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400784 (TypeBinding {
785 ident: id,
786 eq_token: eq,
787 ty: ty,
788 })
789 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700790 }
791
792 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400793 named!(parse -> Self, do_parse!(
794 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
795 trait_ref: syn!(Path) >>
796 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500797 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
798 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400799 )) >>
800 ({
801 let mut trait_ref = trait_ref;
802 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500803 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400804 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500805 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400806 }
807 PolyTraitRef {
808 bound_lifetimes: bound_lifetimes,
809 trait_ref: trait_ref,
810 }
811 })
812 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700813 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700814
Alex Crichton954046c2017-05-30 21:49:42 -0700815 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400816 named!(parse -> Self, do_parse!(
817 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700818 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800819 not!(punct!(::)) >>
820 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400821 (name, colon)
822 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800823 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400824 (BareFnArg {
825 name: name,
826 ty: ty,
827 })
828 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700829 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700830
Alex Crichton23a15f62017-08-28 12:34:23 -0700831 impl Synom for BareFnArgName {
832 named!(parse -> Self, alt!(
833 map!(syn!(Ident), BareFnArgName::Named)
834 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800835 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700836 ));
837 }
838
Alex Crichton954046c2017-05-30 21:49:42 -0700839 impl Synom for Unsafety {
Michael Layzell92639a52017-06-01 00:07:44 -0400840 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800841 keyword!(unsafe) => { Unsafety::Unsafe }
Michael Layzell92639a52017-06-01 00:07:44 -0400842 |
843 epsilon!() => { |_| Unsafety::Normal }
844 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700845 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700846
Alex Crichton954046c2017-05-30 21:49:42 -0700847 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400848 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800849 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400850 // TODO: this parses all literals, not just strings
851 name: option!(syn!(Lit)) >>
852 (Abi {
853 extern_token: extern_,
854 kind: match name {
855 Some(name) => AbiKind::Named(name),
856 None => AbiKind::Default,
857 },
858 })
859 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700860 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700861}
David Tolnay87d0b442016-09-04 11:52:12 -0700862
863#[cfg(feature = "printing")]
864mod printing {
865 use super::*;
866 use quote::{Tokens, ToTokens};
867
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800868 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700869 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700870 self.bracket_token.surround(tokens, |tokens| {
871 self.ty.to_tokens(tokens);
872 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700873 }
874 }
875
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800876 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700877 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700878 self.bracket_token.surround(tokens, |tokens| {
879 self.ty.to_tokens(tokens);
880 self.semi_token.to_tokens(tokens);
881 self.amt.to_tokens(tokens);
882 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700883 }
884 }
885
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800886 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700887 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700888 self.star_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400889 match self.ty.mutability {
890 Mutability::Mutable(ref tok) => tok.to_tokens(tokens),
891 Mutability::Immutable => {
Alex Crichton259ee532017-07-14 06:51:02 -0700892 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400893 }
894 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700895 self.ty.ty.to_tokens(tokens);
896 }
897 }
898
David Tolnay0a89b4d2017-11-13 00:55:45 -0800899 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700900 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700901 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700902 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700903 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700904 self.ty.ty.to_tokens(tokens);
905 }
906 }
907
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800908 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700909 fn to_tokens(&self, tokens: &mut Tokens) {
910 self.ty.to_tokens(tokens)
911 }
912 }
913
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800914 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700915 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700917 }
918 }
919
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800920 impl ToTokens for TypeTup {
Alex Crichton62a0a592017-05-22 13:58:53 -0700921 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700922 self.paren_token.surround(tokens, |tokens| {
923 self.tys.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400924 // XXX: I don't think (,) is a thing.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700925 self.lone_comma.to_tokens(tokens);
926 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700927 }
928 }
929
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800930 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700931 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700932 PathTokens(&self.qself, &self.path).to_tokens(tokens);
933 }
934 }
935
936 impl<'a> ToTokens for PathTokens<'a> {
937 fn to_tokens(&self, tokens: &mut Tokens) {
938 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700939 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700940 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700941 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700942 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400944
945 // XXX: Gross.
946 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
947 self.1.segments.len() - 1
948 } else {
949 qself.position
950 };
David Tolnay570695e2017-06-03 16:15:13 -0700951 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400952 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700953 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700954 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700955 for (i, segment) in (&mut segments).take(pos).enumerate() {
956 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700957 segment.item().to_tokens(tokens);
958 qself.gt_token.to_tokens(tokens);
959 segment.delimiter().to_tokens(tokens);
960 } else {
961 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700963 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 } else {
965 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700966 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 }
David Tolnay570695e2017-06-03 16:15:13 -0700968 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700969 segment.to_tokens(tokens);
970 }
971 }
972 }
973
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800974 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700976 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700977 }
978 }
979
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800980 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700982 self.impl_token.to_tokens(tokens);
983 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 }
985 }
986
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800987 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400988 fn to_tokens(&self, tokens: &mut Tokens) {
989 self.group_token.surround(tokens, |tokens| {
990 self.ty.to_tokens(tokens);
991 });
992 }
993 }
994
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800995 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700996 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700997 self.paren_token.surround(tokens, |tokens| {
998 self.ty.to_tokens(tokens);
999 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 }
1001 }
1002
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001003 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001005 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001006 }
1007 }
1008
David Tolnay47a877c2016-10-01 16:50:55 -07001009 impl ToTokens for Mutability {
1010 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001011 if let Mutability::Mutable(ref t) = *self {
1012 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001013 }
1014 }
1015 }
1016
David Tolnay87d0b442016-09-04 11:52:12 -07001017 impl ToTokens for Path {
1018 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001019 self.leading_colon.to_tokens(tokens);
1020 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001021 }
1022 }
1023
1024 impl ToTokens for PathSegment {
1025 fn to_tokens(&self, tokens: &mut Tokens) {
1026 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001027 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001028 }
1029 }
1030
Nika Layzellc08227a2017-12-04 16:30:17 -05001031 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001032 fn to_tokens(&self, tokens: &mut Tokens) {
1033 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001034 PathArguments::None => {}
1035 PathArguments::AngleBracketed(ref arguments) => {
1036 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001037 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001038 PathArguments::Parenthesized(ref arguments) => {
1039 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001040 }
1041 }
1042 }
1043 }
1044
Nika Layzellc08227a2017-12-04 16:30:17 -05001045 impl ToTokens for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -05001046 fn to_tokens(&self, tokens: &mut Tokens) {
1047 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001048 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1049 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
1050 GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens),
Nika Layzellc680e612017-12-04 19:07:20 -05001051 #[cfg(not(feature = "full"))]
1052 GenericArgument::Const(_) => unreachable!(),
1053 #[cfg(feature = "full")]
1054 GenericArgument::Const(ref eb) => eb.to_tokens(tokens),
Nika Layzell357885a2017-12-04 15:47:07 -05001055 }
1056 }
1057 }
1058
Nika Layzellc08227a2017-12-04 16:30:17 -05001059 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001060 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001061 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001062 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001063 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001064 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001065 }
1066 }
1067
1068 impl ToTokens for TypeBinding {
1069 fn to_tokens(&self, tokens: &mut Tokens) {
1070 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001072 self.ty.to_tokens(tokens);
1073 }
1074 }
1075
Nika Layzellc08227a2017-12-04 16:30:17 -05001076 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001077 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001078 self.paren_token.surround(tokens, |tokens| {
1079 self.inputs.to_tokens(tokens);
1080 });
1081 self.output.to_tokens(tokens);
1082 }
1083 }
1084
David Tolnayf93b90d2017-11-11 19:21:26 -08001085 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001086 fn to_tokens(&self, tokens: &mut Tokens) {
1087 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001088 ReturnType::Default => {}
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001089 ReturnType::Type(ref ty, ref arrow) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 arrow.to_tokens(tokens);
1091 ty.to_tokens(tokens);
1092 }
David Tolnay87d0b442016-09-04 11:52:12 -07001093 }
1094 }
1095 }
1096
1097 impl ToTokens for PolyTraitRef {
1098 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001099 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001100 self.trait_ref.to_tokens(tokens);
1101 }
1102 }
1103
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001104 impl ToTokens for BareFnType {
David Tolnay87d0b442016-09-04 11:52:12 -07001105 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001106 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001107 self.unsafety.to_tokens(tokens);
1108 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 self.fn_token.to_tokens(tokens);
1110 self.paren_token.surround(tokens, |tokens| {
1111 self.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001112 if self.variadic.is_some() && !self.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001113 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001114 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001115 self.variadic.to_tokens(tokens);
1116 });
1117 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001118 }
1119 }
1120
David Tolnay62f374c2016-10-02 13:37:00 -07001121 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001122 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001123 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001124 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001125 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001126 }
1127 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001128 }
1129 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001130
Alex Crichton23a15f62017-08-28 12:34:23 -07001131 impl ToTokens for BareFnArgName {
1132 fn to_tokens(&self, tokens: &mut Tokens) {
1133 match *self {
1134 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1135 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1136 }
1137 }
1138 }
1139
David Tolnayb8d8ef52016-10-29 14:30:08 -07001140 impl ToTokens for Unsafety {
1141 fn to_tokens(&self, tokens: &mut Tokens) {
1142 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001143 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001144 Unsafety::Normal => {
1145 // nothing
1146 }
1147 }
1148 }
1149 }
1150
1151 impl ToTokens for Abi {
1152 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001153 self.extern_token.to_tokens(tokens);
1154 match self.kind {
1155 AbiKind::Named(ref named) => named.to_tokens(tokens),
1156 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001157 }
1158 }
1159 }
David Tolnay87d0b442016-09-04 11:52:12 -07001160}