blob: 722f8357e206a97898d2953262bd28fa404961d1 [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,
Nika Layzellb49a9e52017-12-05 13:31:52 -0500530 option!(tuple!(punct!(+), call!(Delimited::<TypeParamBound, Token![+]>::parse_terminated)))
Michael Layzell9bf2b002017-06-04 18:49:53 -0400531 )
532 |
Nika Layzellb49a9e52017-12-05 13:31:52 -0500533 value!(None)
Michael Layzell9bf2b002017-06-04 18:49:53 -0400534 ) >>
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 }
Nika Layzellb49a9e52017-12-05 13:31:52 -0500542 if let Some((plus, rest)) = bounds {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800543 let path = TypeParamBound::Trait(
David Tolnay02c907f2017-01-23 00:06:37 -0800544 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700545 bound_lifetimes: None,
David Tolnay02c907f2017-01-23 00:06:37 -0800546 trait_ref: path,
547 },
548 TraitBoundModifier::None,
549 );
Nika Layzellb49a9e52017-12-05 13:31:52 -0500550
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700551 let mut new_bounds = Delimited::new();
Nika Layzellb49a9e52017-12-05 13:31:52 -0500552 new_bounds.push(delimited::Element::Delimited(path, plus));
553 new_bounds.extend(rest);
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800554 TypeTraitObject { bounds: new_bounds }.into()
Nika Layzellb49a9e52017-12-05 13:31:52 -0500555 } else {
556 TypePath { qself: qself, path: path }.into()
David Tolnay6414da72016-10-08 00:55:17 -0700557 }
558 })
559 ));
David Tolnay9d8f1972016-09-04 11:58:48 -0700560
David Tolnay9636c052016-10-02 17:11:17 -0700561 named!(pub qpath -> (Option<QSelf>, Path), alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700562 map!(syn!(Path), |p| (None, p))
David Tolnay9636c052016-10-02 17:11:17 -0700563 |
564 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800565 lt: punct!(<) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800566 this: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700567 path: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800568 as_: keyword!(as) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700569 path: syn!(Path) >>
570 (as_, path)
David Tolnay9636c052016-10-02 17:11:17 -0700571 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800572 gt: punct!(>) >>
573 colon2: punct!(::) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700574 rest: call!(Delimited::parse_separated_nonempty) >>
David Tolnay9636c052016-10-02 17:11:17 -0700575 ({
Michael Layzell3936ceb2017-07-08 00:28:36 -0400576 let (pos, as_, path) = match path {
Alex Crichton954046c2017-05-30 21:49:42 -0700577 Some((as_, mut path)) => {
David Tolnay9636c052016-10-02 17:11:17 -0700578 let pos = path.segments.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700579 if !path.segments.is_empty() && !path.segments.trailing_delim() {
Alex Crichton954046c2017-05-30 21:49:42 -0700580 path.segments.push_trailing(colon2);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581 }
Alex Crichton954046c2017-05-30 21:49:42 -0700582 for item in rest {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700583 path.segments.push(item);
584 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400585 (pos, Some(as_), path)
David Tolnay9636c052016-10-02 17:11:17 -0700586 }
587 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400588 (0, None, Path {
David Tolnay570695e2017-06-03 16:15:13 -0700589 leading_colon: Some(colon2),
David Tolnay9636c052016-10-02 17:11:17 -0700590 segments: rest,
David Tolnay570695e2017-06-03 16:15:13 -0700591 })
David Tolnay9636c052016-10-02 17:11:17 -0700592 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700593 };
594 (Some(QSelf {
David Tolnay570695e2017-06-03 16:15:13 -0700595 lt_token: lt,
596 ty: Box::new(this),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597 position: pos,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400598 as_token: as_,
Alex Crichton954046c2017-05-30 21:49:42 -0700599 gt_token: gt,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700600 }), path)
David Tolnay9636c052016-10-02 17:11:17 -0700601 })
602 )
David Tolnay6cd2a232016-10-24 22:41:08 -0700603 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800604 map!(keyword!(self), |s| (None, s.into()))
David Tolnay9d8f1972016-09-04 11:58:48 -0700605 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700606
Nika Layzellc08227a2017-12-04 16:30:17 -0500607 impl Synom for ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400608 named!(parse -> Self, do_parse!(
609 data: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800610 output: syn!(ReturnType) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500611 (ParenthesizedGenericArguments {
Michael Layzell92639a52017-06-01 00:07:44 -0400612 paren_token: data.1,
613 inputs: data.0,
614 output: output,
615 })
616 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700617 }
618
David Tolnayf93b90d2017-11-11 19:21:26 -0800619 impl Synom for ReturnType {
Michael Layzell92639a52017-06-01 00:07:44 -0400620 named!(parse -> Self, alt!(
621 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800623 ty: syn!(Type) >>
624 (ReturnType::Type(ty, arrow))
Michael Layzell92639a52017-06-01 00:07:44 -0400625 )
626 |
David Tolnayf93b90d2017-11-11 19:21:26 -0800627 epsilon!() => { |_| ReturnType::Default }
Michael Layzell92639a52017-06-01 00:07:44 -0400628 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700629 }
630
Michael Layzell9bf2b002017-06-04 18:49:53 -0400631 // Only allow multiple trait references if allow_plus is true.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800632 named!(ty_poly_trait_ref(allow_plus: bool) -> Type, alt!(
Nika Layzellb49a9e52017-12-05 13:31:52 -0500633 cond_reduce!(allow_plus, call!(Delimited::parse_terminated_nonempty)) => {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800634 |x| TypeTraitObject { bounds: x }.into()
Michael Layzell9bf2b002017-06-04 18:49:53 -0400635 }
636 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800637 syn!(TypeParamBound) => {
638 |x| TypeTraitObject { bounds: vec![x].into() }.into()
Michael Layzell9bf2b002017-06-04 18:49:53 -0400639 }
David Tolnay6414da72016-10-08 00:55:17 -0700640 ));
641
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800642 impl Synom for TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400643 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 impl_: keyword!(impl) >>
Michael Layzell9bf2b002017-06-04 18:49:53 -0400645 // NOTE: rust-lang/rust#34511 includes discussion about whether or
646 // not + should be allowed in ImplTrait directly without ().
Nika Layzellb49a9e52017-12-05 13:31:52 -0500647 elem: call!(Delimited::parse_terminated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800648 (TypeImplTrait {
Michael Layzell92639a52017-06-01 00:07:44 -0400649 impl_token: impl_,
650 bounds: elem,
651 })
652 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700653 }
David Tolnayb79ee962016-09-04 09:39:20 -0700654
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800655 impl Synom for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400656 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800657 data: grouped!(syn!(Type)) >>
658 (TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400659 group_token: data.1,
660 ty: Box::new(data.0),
661 })
662 ));
663 }
664
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800665 impl Synom for TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400666 named!(parse -> Self, do_parse!(
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800667 data: parens!(syn!(Type)) >>
668 (TypeParen {
Michael Layzell92639a52017-06-01 00:07:44 -0400669 paren_token: data.1,
670 ty: Box::new(data.0),
671 })
672 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700673 }
David Tolnayb79ee962016-09-04 09:39:20 -0700674
Alex Crichton954046c2017-05-30 21:49:42 -0700675 impl Synom for Mutability {
Michael Layzell92639a52017-06-01 00:07:44 -0400676 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800677 keyword!(mut) => { Mutability::Mutable }
Michael Layzell92639a52017-06-01 00:07:44 -0400678 |
679 epsilon!() => { |_| Mutability::Immutable }
680 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700681 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700682
Alex Crichton954046c2017-05-30 21:49:42 -0700683 impl Synom for Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400684 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800685 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400686 segments: call!(Delimited::parse_separated_nonempty) >>
687 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700688 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400689 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400690 })
691 ));
Alex Crichton36e91bf2017-07-06 14:59:56 -0700692
693 fn description() -> Option<&'static str> {
694 Some("path")
695 }
Alex Crichton954046c2017-05-30 21:49:42 -0700696 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700697
Nika Layzellc680e612017-12-04 19:07:20 -0500698 #[cfg(not(feature = "full"))]
Nika Layzellc08227a2017-12-04 16:30:17 -0500699 impl Synom for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -0500700 named!(parse -> Self, alt!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500701 call!(ty_no_eq_after) => { GenericArgument::Type }
Nika Layzell357885a2017-12-04 15:47:07 -0500702 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500703 syn!(Lifetime) => { GenericArgument::Lifetime }
Nika Layzell357885a2017-12-04 15:47:07 -0500704 |
Nika Layzellc08227a2017-12-04 16:30:17 -0500705 syn!(TypeBinding) => { GenericArgument::TypeBinding }
Nika Layzell357885a2017-12-04 15:47:07 -0500706 ));
707 }
708
Nika Layzellc680e612017-12-04 19:07:20 -0500709 #[cfg(feature = "full")]
710 impl Synom for GenericArgument {
711 named!(parse -> Self, alt!(
712 call!(ty_no_eq_after) => { GenericArgument::Type }
713 |
714 syn!(Lifetime) => { GenericArgument::Lifetime }
715 |
716 syn!(TypeBinding) => { GenericArgument::TypeBinding }
717 |
Nika Layzellce37f332017-12-05 12:01:22 -0500718 syn!(Lit) => { |l| GenericArgument::Const(ExprKind::Lit(l).into()) }
719 |
720 syn!(ExprBlock) => { |b| GenericArgument::Const(ExprKind::Block(b).into()) }
Nika Layzellc680e612017-12-04 19:07:20 -0500721 ));
722 }
723
Nika Layzellc08227a2017-12-04 16:30:17 -0500724 impl Synom for AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500725 named!(parse -> Self, do_parse!(
726 turbofish: option!(punct!(::)) >>
727 lt: punct!(<) >>
728 args: call!(Delimited::parse_terminated) >>
729 gt: punct!(>) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500730 (AngleBracketedGenericArguments {
Nika Layzell357885a2017-12-04 15:47:07 -0500731 turbofish: turbofish,
732 lt_token: lt,
733 args: args,
734 gt_token: gt,
735 })
736 ));
737 }
738
Alex Crichton954046c2017-05-30 21:49:42 -0700739 impl Synom for PathSegment {
Michael Layzell92639a52017-06-01 00:07:44 -0400740 named!(parse -> Self, alt!(
741 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700742 ident: syn!(Ident) >>
Nika Layzellc08227a2017-12-04 16:30:17 -0500743 arguments: syn!(AngleBracketedGenericArguments) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400744 (PathSegment {
David Tolnay570695e2017-06-03 16:15:13 -0700745 ident: ident,
Nika Layzellc08227a2017-12-04 16:30:17 -0500746 arguments: PathArguments::AngleBracketed(arguments),
Michael Layzell92639a52017-06-01 00:07:44 -0400747 })
748 )
749 |
750 mod_style_path_segment
751 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700752 }
David Tolnay570695e2017-06-03 16:15:13 -0700753
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800754 named!(ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=))));
David Tolnay9d8f1972016-09-04 11:58:48 -0700755
Alex Crichton954046c2017-05-30 21:49:42 -0700756 impl Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400757 named!(pub parse_mod_style -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800758 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400759 segments: call!(Delimited::parse_separated_nonempty_with,
760 mod_style_path_segment) >>
761 (Path {
David Tolnay570695e2017-06-03 16:15:13 -0700762 leading_colon: colon,
Michael Layzell92639a52017-06-01 00:07:44 -0400763 segments: segments,
Michael Layzell92639a52017-06-01 00:07:44 -0400764 })
765 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700766 }
Arnavionf2dada12017-04-20 23:55:20 -0700767
768 named!(mod_style_path_segment -> PathSegment, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700769 map!(syn!(Ident), Into::into)
Arnavionf2dada12017-04-20 23:55:20 -0700770 |
Alex Crichton954046c2017-05-30 21:49:42 -0700771 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800772 keyword!(super) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700773 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800774 keyword!(self) => { Into::into }
Arnavionf2dada12017-04-20 23:55:20 -0700775 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800776 keyword!(Self) => { Into::into }
Alex Crichton954046c2017-05-30 21:49:42 -0700777 )
Arnavionf2dada12017-04-20 23:55:20 -0700778 ));
779
Alex Crichton954046c2017-05-30 21:49:42 -0700780 impl Synom for TypeBinding {
Michael Layzell92639a52017-06-01 00:07:44 -0400781 named!(parse -> Self, do_parse!(
782 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800783 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800784 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400785 (TypeBinding {
786 ident: id,
787 eq_token: eq,
788 ty: ty,
789 })
790 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700791 }
792
793 impl Synom for PolyTraitRef {
Michael Layzell92639a52017-06-01 00:07:44 -0400794 named!(parse -> Self, do_parse!(
795 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
796 trait_ref: syn!(Path) >>
797 parenthesized: option!(cond_reduce!(
Nika Layzellc08227a2017-12-04 16:30:17 -0500798 trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(),
799 syn!(ParenthesizedGenericArguments)
Michael Layzell92639a52017-06-01 00:07:44 -0400800 )) >>
801 ({
802 let mut trait_ref = trait_ref;
803 if let Some(parenthesized) = parenthesized {
Nika Layzellc08227a2017-12-04 16:30:17 -0500804 let parenthesized = PathArguments::Parenthesized(parenthesized);
Michael Layzell92639a52017-06-01 00:07:44 -0400805 let len = trait_ref.segments.len();
Nika Layzellc08227a2017-12-04 16:30:17 -0500806 trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized;
Michael Layzell92639a52017-06-01 00:07:44 -0400807 }
808 PolyTraitRef {
809 bound_lifetimes: bound_lifetimes,
810 trait_ref: trait_ref,
811 }
812 })
813 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700814 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700815
Alex Crichton954046c2017-05-30 21:49:42 -0700816 impl Synom for BareFnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400817 named!(parse -> Self, do_parse!(
818 name: option!(do_parse!(
Alex Crichton23a15f62017-08-28 12:34:23 -0700819 name: syn!(BareFnArgName) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800820 not!(punct!(::)) >>
821 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400822 (name, colon)
823 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800824 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400825 (BareFnArg {
826 name: name,
827 ty: ty,
828 })
829 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700830 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700831
Alex Crichton23a15f62017-08-28 12:34:23 -0700832 impl Synom for BareFnArgName {
833 named!(parse -> Self, alt!(
834 map!(syn!(Ident), BareFnArgName::Named)
835 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800836 map!(punct!(_), BareFnArgName::Wild)
Alex Crichton23a15f62017-08-28 12:34:23 -0700837 ));
838 }
839
Alex Crichton954046c2017-05-30 21:49:42 -0700840 impl Synom for Unsafety {
Michael Layzell92639a52017-06-01 00:07:44 -0400841 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800842 keyword!(unsafe) => { Unsafety::Unsafe }
Michael Layzell92639a52017-06-01 00:07:44 -0400843 |
844 epsilon!() => { |_| Unsafety::Normal }
845 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700846 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700847
Alex Crichton954046c2017-05-30 21:49:42 -0700848 impl Synom for Abi {
Michael Layzell92639a52017-06-01 00:07:44 -0400849 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800850 extern_: keyword!(extern) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400851 // TODO: this parses all literals, not just strings
852 name: option!(syn!(Lit)) >>
853 (Abi {
854 extern_token: extern_,
855 kind: match name {
856 Some(name) => AbiKind::Named(name),
857 None => AbiKind::Default,
858 },
859 })
860 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700861 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700862}
David Tolnay87d0b442016-09-04 11:52:12 -0700863
864#[cfg(feature = "printing")]
865mod printing {
866 use super::*;
867 use quote::{Tokens, ToTokens};
868
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800869 impl ToTokens for TypeSlice {
David Tolnay87d0b442016-09-04 11:52:12 -0700870 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700871 self.bracket_token.surround(tokens, |tokens| {
872 self.ty.to_tokens(tokens);
873 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700874 }
875 }
876
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800877 impl ToTokens for TypeArray {
Alex Crichton62a0a592017-05-22 13:58:53 -0700878 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700879 self.bracket_token.surround(tokens, |tokens| {
880 self.ty.to_tokens(tokens);
881 self.semi_token.to_tokens(tokens);
882 self.amt.to_tokens(tokens);
883 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700884 }
885 }
886
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800887 impl ToTokens for TypePtr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700888 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700889 self.star_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400890 match self.ty.mutability {
891 Mutability::Mutable(ref tok) => tok.to_tokens(tokens),
892 Mutability::Immutable => {
Alex Crichton259ee532017-07-14 06:51:02 -0700893 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400894 }
895 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700896 self.ty.ty.to_tokens(tokens);
897 }
898 }
899
David Tolnay0a89b4d2017-11-13 00:55:45 -0800900 impl ToTokens for TypeReference {
Alex Crichton62a0a592017-05-22 13:58:53 -0700901 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700902 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700903 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700904 self.ty.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700905 self.ty.ty.to_tokens(tokens);
906 }
907 }
908
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800909 impl ToTokens for TypeBareFn {
Alex Crichton62a0a592017-05-22 13:58:53 -0700910 fn to_tokens(&self, tokens: &mut Tokens) {
911 self.ty.to_tokens(tokens)
912 }
913 }
914
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800915 impl ToTokens for TypeNever {
Alex Crichton62a0a592017-05-22 13:58:53 -0700916 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700917 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700918 }
919 }
920
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800921 impl ToTokens for TypeTup {
Alex Crichton62a0a592017-05-22 13:58:53 -0700922 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700923 self.paren_token.surround(tokens, |tokens| {
924 self.tys.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400925 // XXX: I don't think (,) is a thing.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700926 self.lone_comma.to_tokens(tokens);
927 })
Alex Crichton62a0a592017-05-22 13:58:53 -0700928 }
929 }
930
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800931 impl ToTokens for TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700932 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700933 PathTokens(&self.qself, &self.path).to_tokens(tokens);
934 }
935 }
936
937 impl<'a> ToTokens for PathTokens<'a> {
938 fn to_tokens(&self, tokens: &mut Tokens) {
939 let qself = match *self.0 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700940 Some(ref qself) => qself,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700941 None => return self.1.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700943 qself.lt_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700944 qself.ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400945
946 // XXX: Gross.
947 let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
948 self.1.segments.len() - 1
949 } else {
950 qself.position
951 };
David Tolnay570695e2017-06-03 16:15:13 -0700952 let mut segments = self.1.segments.iter();
Michael Layzell3936ceb2017-07-08 00:28:36 -0400953 if pos > 0 {
Alex Crichton259ee532017-07-14 06:51:02 -0700954 TokensOrDefault(&qself.as_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700955 self.1.leading_colon.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700956 for (i, segment) in (&mut segments).take(pos).enumerate() {
957 if i + 1 == pos {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700958 segment.item().to_tokens(tokens);
959 qself.gt_token.to_tokens(tokens);
960 segment.delimiter().to_tokens(tokens);
961 } else {
962 segment.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700963 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700965 } else {
966 qself.gt_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -0700967 self.1.leading_colon.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700968 }
David Tolnay570695e2017-06-03 16:15:13 -0700969 for segment in segments {
Alex Crichton62a0a592017-05-22 13:58:53 -0700970 segment.to_tokens(tokens);
971 }
972 }
973 }
974
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800975 impl ToTokens for TypeTraitObject {
Alex Crichton62a0a592017-05-22 13:58:53 -0700976 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700977 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700978 }
979 }
980
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800981 impl ToTokens for TypeImplTrait {
Alex Crichton62a0a592017-05-22 13:58:53 -0700982 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700983 self.impl_token.to_tokens(tokens);
984 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700985 }
986 }
987
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800988 impl ToTokens for TypeGroup {
Michael Layzell93c36282017-06-04 20:43:14 -0400989 fn to_tokens(&self, tokens: &mut Tokens) {
990 self.group_token.surround(tokens, |tokens| {
991 self.ty.to_tokens(tokens);
992 });
993 }
994 }
995
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800996 impl ToTokens for TypeParen {
Alex Crichton62a0a592017-05-22 13:58:53 -0700997 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700998 self.paren_token.surround(tokens, |tokens| {
999 self.ty.to_tokens(tokens);
1000 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001001 }
1002 }
1003
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001004 impl ToTokens for TypeInfer {
Alex Crichton62a0a592017-05-22 13:58:53 -07001005 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001006 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001007 }
1008 }
1009
David Tolnay47a877c2016-10-01 16:50:55 -07001010 impl ToTokens for Mutability {
1011 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001012 if let Mutability::Mutable(ref t) = *self {
1013 t.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001014 }
1015 }
1016 }
1017
David Tolnay87d0b442016-09-04 11:52:12 -07001018 impl ToTokens for Path {
1019 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001020 self.leading_colon.to_tokens(tokens);
1021 self.segments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001022 }
1023 }
1024
1025 impl ToTokens for PathSegment {
1026 fn to_tokens(&self, tokens: &mut Tokens) {
1027 self.ident.to_tokens(tokens);
Nika Layzellc08227a2017-12-04 16:30:17 -05001028 self.arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001029 }
1030 }
1031
Nika Layzellc08227a2017-12-04 16:30:17 -05001032 impl ToTokens for PathArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001033 fn to_tokens(&self, tokens: &mut Tokens) {
1034 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001035 PathArguments::None => {}
1036 PathArguments::AngleBracketed(ref arguments) => {
1037 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001038 }
Nika Layzellc08227a2017-12-04 16:30:17 -05001039 PathArguments::Parenthesized(ref arguments) => {
1040 arguments.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001041 }
1042 }
1043 }
1044 }
1045
Nika Layzellc08227a2017-12-04 16:30:17 -05001046 impl ToTokens for GenericArgument {
Nika Layzell357885a2017-12-04 15:47:07 -05001047 fn to_tokens(&self, tokens: &mut Tokens) {
1048 match *self {
Nika Layzellc08227a2017-12-04 16:30:17 -05001049 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
1050 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
1051 GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens),
Nika Layzellce37f332017-12-05 12:01:22 -05001052 GenericArgument::Const(ref e) => match e.node {
1053 ExprKind::Lit(_) => e.to_tokens(tokens),
1054
1055 // NOTE: We should probably support parsing blocks with only
1056 // expressions in them without the full feature for const
1057 // generics.
1058 #[cfg(feature = "full")]
1059 ExprKind::Block(_) => e.to_tokens(tokens),
1060
1061 // ERROR CORRECTION: Add braces to make sure that the
1062 // generated code is valid.
1063 _ => tokens::Brace::default().surround(tokens, |tokens| {
1064 e.to_tokens(tokens);
1065 }),
1066 }
Nika Layzell357885a2017-12-04 15:47:07 -05001067 }
1068 }
1069 }
1070
Nika Layzellc08227a2017-12-04 16:30:17 -05001071 impl ToTokens for AngleBracketedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001072 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001073 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001074 self.lt_token.to_tokens(tokens);
Nika Layzell357885a2017-12-04 15:47:07 -05001075 self.args.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001076 self.gt_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001077 }
1078 }
1079
1080 impl ToTokens for TypeBinding {
1081 fn to_tokens(&self, tokens: &mut Tokens) {
1082 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001083 self.eq_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001084 self.ty.to_tokens(tokens);
1085 }
1086 }
1087
Nika Layzellc08227a2017-12-04 16:30:17 -05001088 impl ToTokens for ParenthesizedGenericArguments {
David Tolnay87d0b442016-09-04 11:52:12 -07001089 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 self.paren_token.surround(tokens, |tokens| {
1091 self.inputs.to_tokens(tokens);
1092 });
1093 self.output.to_tokens(tokens);
1094 }
1095 }
1096
David Tolnayf93b90d2017-11-11 19:21:26 -08001097 impl ToTokens for ReturnType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001098 fn to_tokens(&self, tokens: &mut Tokens) {
1099 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -08001100 ReturnType::Default => {}
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001101 ReturnType::Type(ref ty, ref arrow) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001102 arrow.to_tokens(tokens);
1103 ty.to_tokens(tokens);
1104 }
David Tolnay87d0b442016-09-04 11:52:12 -07001105 }
1106 }
1107 }
1108
1109 impl ToTokens for PolyTraitRef {
1110 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001111 self.bound_lifetimes.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001112 self.trait_ref.to_tokens(tokens);
1113 }
1114 }
1115
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001116 impl ToTokens for BareFnType {
David Tolnay87d0b442016-09-04 11:52:12 -07001117 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001118 self.lifetimes.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001119 self.unsafety.to_tokens(tokens);
1120 self.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001121 self.fn_token.to_tokens(tokens);
1122 self.paren_token.surround(tokens, |tokens| {
1123 self.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001124 if self.variadic.is_some() && !self.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001125 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001126 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001127 self.variadic.to_tokens(tokens);
1128 });
1129 self.output.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001130 }
1131 }
1132
David Tolnay62f374c2016-10-02 13:37:00 -07001133 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -07001134 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001135 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -07001136 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001137 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07001138 }
1139 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -07001140 }
1141 }
David Tolnayb8d8ef52016-10-29 14:30:08 -07001142
Alex Crichton23a15f62017-08-28 12:34:23 -07001143 impl ToTokens for BareFnArgName {
1144 fn to_tokens(&self, tokens: &mut Tokens) {
1145 match *self {
1146 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
1147 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
1148 }
1149 }
1150 }
1151
David Tolnayb8d8ef52016-10-29 14:30:08 -07001152 impl ToTokens for Unsafety {
1153 fn to_tokens(&self, tokens: &mut Tokens) {
1154 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001155 Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
David Tolnayb8d8ef52016-10-29 14:30:08 -07001156 Unsafety::Normal => {
1157 // nothing
1158 }
1159 }
1160 }
1161 }
1162
1163 impl ToTokens for Abi {
1164 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001165 self.extern_token.to_tokens(tokens);
1166 match self.kind {
1167 AbiKind::Named(ref named) => named.to_tokens(tokens),
1168 AbiKind::Default => {}
David Tolnayb8d8ef52016-10-29 14:30:08 -07001169 }
1170 }
1171 }
David Tolnay87d0b442016-09-04 11:52:12 -07001172}