blob: dd6c206fd4d8d93f660f5635219d4e31e0ff5fae [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.
Nika Layzellce37f332017-12-05 12:01:22 -0500208 Const(Expr),
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 |
Nika Layzellce37f332017-12-05 12:01:22 -0500719 syn!(Lit) => { |l| GenericArgument::Const(ExprKind::Lit(l).into()) }
720 |
721 syn!(ExprBlock) => { |b| GenericArgument::Const(ExprKind::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500722 ));
723 }
724
Nika Layzellc08227a2017-12-04 16:30:17 -0500725 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500726 named!(parse -> Self, do_parse!(
727 turbofish: option!(punct!(::)) >>
728 lt: punct!(<) >>
729 args: call!(Delimited::parse_terminated) >>
730 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500731 (AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500732 turbofish: turbofish,
733 lt_token: lt,
734 args: args,
735 gt_token: gt,
736 })
737 ));
738 }
739
Alex Crichton954046c2017-05-30 21:49:42 -0700740 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400741 named!(parse -> Self, alt!(
742 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700743 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500744 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400745 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700746 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500747 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400748 })
749 )
750 |
751 mod_style_path_segment
752 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700753 }
David Tolnay570695e2017-06-03 16:15:13 -0700754
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800755 named!(ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700756
Alex Crichton954046c2017-05-30 21:49:42 -0700757 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400758 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800759 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400760 segments: call!(Delimited::parse_separated_nonempty_with,
761 mod_style_path_segment) >>
762 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700763 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400764 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400765 })
766 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700767 }
Arnavionf2dada12017-04-20 23:55:20 -0700768
769 named!(mod_style_path_segment -> PathSegment, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700770 map!(syn!(Ident), Into::into)
Arnavionf2dada12017-04-20 23:55:20 -0700771 |
Alex Crichton954046c2017-05-30 21:49:42 -0700772 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800773 keyword!(super) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700774 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800775 keyword!(self) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700776 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800777 keyword!(Self) => { Into::into }
Alex Crichton954046c2017-05-30 21:49:42 -0700778 )
Arnavionf2dada12017-04-20 23:55:20 -0700779 ));
780
Alex Crichton954046c2017-05-30 21:49:42 -0700781 impl Synom for TypeBinding {
Michael Layzell92639a52017-06-01 00:07:44 -0400782 named!(parse -> Self, do_parse!(
783 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800784 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800785 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400786 (TypeBinding {
787 ident: id,
788 eq_token: eq,
789 ty: ty,
790 })
791 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700792 }
793
794 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400795 named!(parse -> Self, do_parse!(
796 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
797 trait_ref: syn!(Path) >>
798 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500799 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
800 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400801 )) >>
802 ({
803 let mut trait_ref = trait_ref;
804 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500805 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400806 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500807 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400808 }
809 PolyTraitRef {
810 bound_lifetimes: bound_lifetimes,
811 trait_ref: trait_ref,
812 }
813 })
814 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700815 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700816
Alex Crichton954046c2017-05-30 21:49:42 -0700817 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400818 named!(parse -> Self, do_parse!(
819 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700820 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800821 not!(punct!(::)) >>
822 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400823 (name, colon)
824 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800825 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400826 (BareFnArg {
827 name: name,
828 ty: ty,
829 })
830 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700831 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700832
Alex Crichton23a15f62017-08-28 12:34:23 -0700833 impl Synom for BareFnArgName {
834 named!(parse -> Self, alt!(
835 map!(syn!(Ident), BareFnArgName::Named)
836 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800837 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700838 ));
839 }
840
Alex Crichton954046c2017-05-30 21:49:42 -0700841 impl Synom for Unsafety {
Michael Layzell92639a52017-06-01 00:07:44 -0400842 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800843 keyword!(unsafe) => { Unsafety::Unsafe }
Michael Layzell92639a52017-06-01 00:07:44 -0400844 |
845 epsilon!() => { |_| Unsafety::Normal }
846 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700847 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700848
Alex Crichton954046c2017-05-30 21:49:42 -0700849 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400850 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800851 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400852 // TODO: this parses all literals, not just strings
853 name: option!(syn!(Lit)) >>
854 (Abi {
855 extern_token: extern_,
856 kind: match name {
857 Some(name) => AbiKind::Named(name),
858 None => AbiKind::Default,
859 },
860 })
861 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700862 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700863}
David Tolnay87d0b442016-09-04 11:52:12 -0700864
865#[cfg(feature = "printing")]
866mod printing {
867 use super::*;
868 use quote::{Tokens, ToTokens};
869
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800870 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700871 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700872 self.bracket_token.surround(tokens, |tokens| {
873 self.ty.to_tokens(tokens);
874 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700875 }
876 }
877
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800878 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700879 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 self.bracket_token.surround(tokens, |tokens| {
881 self.ty.to_tokens(tokens);
882 self.semi_token.to_tokens(tokens);
883 self.amt.to_tokens(tokens);
884 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700885 }
886 }
887
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800888 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700889 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 self.star_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400891 match self.ty.mutability {
892 Mutability::Mutable(ref tok) => tok.to_tokens(tokens),
893 Mutability::Immutable => {
Alex Crichton259ee532017-07-14 06:51:02 -0700894 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400895 }
896 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700897 self.ty.ty.to_tokens(tokens);
898 }
899 }
900
David Tolnay0a89b4d2017-11-13 00:55:45 -0800901 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700902 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700903 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700904 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700905 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700906 self.ty.ty.to_tokens(tokens);
907 }
908 }
909
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800910 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700911 fn to_tokens(&self, tokens: &mut Tokens) {
912 self.ty.to_tokens(tokens)
913 }
914 }
915
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800916 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700917 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700918 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700919 }
920 }
921
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800922 impl ToTokens for TypeTup {
Alex Crichton62a0a592017-05-22 13:58:53 -0700923 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700924 self.paren_token.surround(tokens, |tokens| {
925 self.tys.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400926 // XXX: I don't think (,) is a thing.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700927 self.lone_comma.to_tokens(tokens);
928 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700929 }
930 }
931
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800932 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700933 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700934 PathTokens(&self.qself, &self.path).to_tokens(tokens);
935 }
936 }
937
938 impl<'a> ToTokens for PathTokens<'a> {
939 fn to_tokens(&self, tokens: &mut Tokens) {
940 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700941 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700942 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700944 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700945 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400946
947 // XXX: Gross.
948 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
949 self.1.segments.len() - 1
950 } else {
951 qself.position
952 };
David Tolnay570695e2017-06-03 16:15:13 -0700953 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400954 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700955 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700956 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700957 for (i, segment) in (&mut segments).take(pos).enumerate() {
958 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700959 segment.item().to_tokens(tokens);
960 qself.gt_token.to_tokens(tokens);
961 segment.delimiter().to_tokens(tokens);
962 } else {
963 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700965 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 } else {
967 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700968 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700969 }
David Tolnay570695e2017-06-03 16:15:13 -0700970 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700971 segment.to_tokens(tokens);
972 }
973 }
974 }
975
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800976 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700977 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700978 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700979 }
980 }
981
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800982 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700983 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700984 self.impl_token.to_tokens(tokens);
985 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 }
987 }
988
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800989 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400990 fn to_tokens(&self, tokens: &mut Tokens) {
991 self.group_token.surround(tokens, |tokens| {
992 self.ty.to_tokens(tokens);
993 });
994 }
995 }
996
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800997 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700998 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700999 self.paren_token.surround(tokens, |tokens| {
1000 self.ty.to_tokens(tokens);
1001 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001002 }
1003 }
1004
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001005 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -07001006 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001007 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001008 }
1009 }
1010
David Tolnay47a877c2016-10-01 16:50:55 -07001011 impl ToTokens for Mutability {
1012 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001013 if let Mutability::Mutable(ref t) = *self {
1014 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001015 }
1016 }
1017 }
1018
David Tolnay87d0b442016-09-04 11:52:12 -07001019 impl ToTokens for Path {
1020 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 self.leading_colon.to_tokens(tokens);
1022 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001023 }
1024 }
1025
1026 impl ToTokens for PathSegment {
1027 fn to_tokens(&self, tokens: &mut Tokens) {
1028 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001029 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001030 }
1031 }
1032
Nika Layzellc08227a2017-12-04 16:30:17 -05001033 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001034 fn to_tokens(&self, tokens: &mut Tokens) {
1035 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001036 PathArguments::None => {}
1037 PathArguments::AngleBracketed(ref arguments) => {
1038 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001039 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001040 PathArguments::Parenthesized(ref arguments) => {
1041 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001042 }
1043 }
1044 }
1045 }
1046
Nika Layzellc08227a2017-12-04 16:30:17 -05001047 impl ToTokens for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -05001048 fn to_tokens(&self, tokens: &mut Tokens) {
1049 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001050 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1051 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
1052 GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001053 GenericArgument::Const(ref e) => match e.node {
1054 ExprKind::Lit(_) => e.to_tokens(tokens),
1055
1056 // NOTE: We should probably support parsing blocks with only
1057 // expressions in them without the full feature for const
1058 // generics.
1059 #[cfg(feature = "full")]
1060 ExprKind::Block(_) => e.to_tokens(tokens),
1061
1062 // ERROR CORRECTION: Add braces to make sure that the
1063 // generated code is valid.
1064 _ => tokens::Brace::default().surround(tokens, |tokens| {
1065 e.to_tokens(tokens);
1066 }),
1067 }
Nika Layzell357885a2017-12-04 15:47:07 -05001068 }
1069 }
1070 }
1071
Nika Layzellc08227a2017-12-04 16:30:17 -05001072 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001073 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001074 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001075 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001076 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001077 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001078 }
1079 }
1080
1081 impl ToTokens for TypeBinding {
1082 fn to_tokens(&self, tokens: &mut Tokens) {
1083 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001084 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001085 self.ty.to_tokens(tokens);
1086 }
1087 }
1088
Nika Layzellc08227a2017-12-04 16:30:17 -05001089 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001090 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001091 self.paren_token.surround(tokens, |tokens| {
1092 self.inputs.to_tokens(tokens);
1093 });
1094 self.output.to_tokens(tokens);
1095 }
1096 }
1097
David Tolnayf93b90d2017-11-11 19:21:26 -08001098 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001099 fn to_tokens(&self, tokens: &mut Tokens) {
1100 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001101 ReturnType::Default => {}
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001102 ReturnType::Type(ref ty, ref arrow) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001103 arrow.to_tokens(tokens);
1104 ty.to_tokens(tokens);
1105 }
David Tolnay87d0b442016-09-04 11:52:12 -07001106 }
1107 }
1108 }
1109
1110 impl ToTokens for PolyTraitRef {
1111 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001112 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001113 self.trait_ref.to_tokens(tokens);
1114 }
1115 }
1116
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001117 impl ToTokens for BareFnType {
David Tolnay87d0b442016-09-04 11:52:12 -07001118 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001119 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001120 self.unsafety.to_tokens(tokens);
1121 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001122 self.fn_token.to_tokens(tokens);
1123 self.paren_token.surround(tokens, |tokens| {
1124 self.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001125 if self.variadic.is_some() && !self.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001126 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001127 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001128 self.variadic.to_tokens(tokens);
1129 });
1130 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001131 }
1132 }
1133
David Tolnay62f374c2016-10-02 13:37:00 -07001134 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001135 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001136 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001137 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001138 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001139 }
1140 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001141 }
1142 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001143
Alex Crichton23a15f62017-08-28 12:34:23 -07001144 impl ToTokens for BareFnArgName {
1145 fn to_tokens(&self, tokens: &mut Tokens) {
1146 match *self {
1147 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1148 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1149 }
1150 }
1151 }
1152
David Tolnayb8d8ef52016-10-29 14:30:08 -07001153 impl ToTokens for Unsafety {
1154 fn to_tokens(&self, tokens: &mut Tokens) {
1155 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001156 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001157 Unsafety::Normal => {
1158 // nothing
1159 }
1160 }
1161 }
1162 }
1163
1164 impl ToTokens for Abi {
1165 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001166 self.extern_token.to_tokens(tokens);
1167 match self.kind {
1168 AbiKind::Named(ref named) => named.to_tokens(tokens),
1169 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001170 }
1171 }
1172 }
David Tolnay87d0b442016-09-04 11:52:12 -07001173}