blob: cf71fcca63f21aad7624d160314bf510bb75f65c [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -08005 /// Things that can appear directly inside of a module.
6 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -07007 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -07008 ///
9 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
10 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080011 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070012 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070013 pub extern_token: tokens::Extern,
14 pub crate_token: tokens::Crate,
David Tolnay570695e2017-06-03 16:15:13 -070015 pub ident: Ident,
16 pub rename: Option<(tokens::As, Ident)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070017 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070018 }),
19 /// A use declaration (`use` or `pub use`) item.
20 ///
21 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
22 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080023 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070024 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025 pub use_token: tokens::Use,
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub path: Box<ViewPath>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070027 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070028 }),
29 /// A static item (`static` or `pub static`).
30 ///
31 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
32 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080033 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070034 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub ident: Ident,
38 pub colon_token: tokens::Colon,
39 pub ty: Box<Ty>,
40 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070041 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070042 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
44 /// A constant item (`const` or `pub const`).
45 ///
46 /// E.g. `const FOO: i32 = 42;`
47 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070050 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -070053 pub ty: Box<Ty>,
David Tolnay570695e2017-06-03 16:15:13 -070054 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070056 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// A function declaration (`fn` or `pub fn`).
59 ///
60 /// E.g. `fn foo(bar: usize) -> usize { .. }`
61 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080062 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070063 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -070065 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070067 pub decl: Box<FnDecl>,
68 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub block: Box<Block>,
70 }),
71 /// A module declaration (`mod` or `pub mod`).
72 ///
73 /// E.g. `mod foo;` or `mod foo { .. }`
74 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080075 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070076 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 pub mod_token: tokens::Mod,
David Tolnay570695e2017-06-03 16:15:13 -070078 pub ident: Ident,
79 pub content: Option<(tokens::Brace, Vec<Item>)>,
80 pub semi: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -070081 }),
82 /// An external module (`extern` or `pub extern`).
83 ///
84 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080086 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070087 pub abi: Abi,
David Tolnay570695e2017-06-03 16:15:13 -070088 pub brace_token: tokens::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070089 pub items: Vec<ForeignItem>,
90 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070091 /// A type alias (`type` or `pub type`).
92 ///
93 /// E.g. `type Foo = Bar<u8>;`
94 pub Ty(ItemTy {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070096 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700100 pub eq_token: tokens::Eq,
101 pub ty: Box<Ty>,
102 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 }),
104 /// An enum definition (`enum` or `pub enum`).
105 ///
106 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
107 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800108 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700110 pub enum_token: tokens::Enum,
David Tolnay570695e2017-06-03 16:15:13 -0700111 pub ident: Ident,
112 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700113 pub brace_token: tokens::Brace,
114 pub variants: Delimited<Variant, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 }),
116 /// A struct definition (`struct` or `pub struct`).
117 ///
118 /// E.g. `struct Foo<A> { x: A }`
119 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800120 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700121 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub struct_token: tokens::Struct,
David Tolnay570695e2017-06-03 16:15:13 -0700123 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700125 pub data: VariantData,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 }),
128 /// A union definition (`union` or `pub union`).
129 ///
130 /// E.g. `union Foo<A, B> { x: A, y: B }`
131 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800132 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700134 pub union_token: tokens::Union,
David Tolnay570695e2017-06-03 16:15:13 -0700135 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700137 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 }),
139 /// A Trait declaration (`trait` or `pub trait`).
140 ///
141 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
142 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800143 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700144 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700146 pub trait_token: tokens::Trait,
147 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700148 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700149 pub colon_token: Option<tokens::Colon>,
150 pub supertraits: Delimited<TyParamBound, tokens::Add>,
151 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 pub items: Vec<TraitItem>,
153 }),
154 /// Default trait implementation.
155 ///
156 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
157 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800158 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 pub unsafety: Unsafety,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700160 pub impl_token: tokens::Impl,
David Tolnay570695e2017-06-03 16:15:13 -0700161 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700162 pub for_token: tokens::For,
163 pub dot2_token: tokens::Dot2,
164 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 }),
166 /// An implementation.
167 ///
168 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
169 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800170 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700171 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700173 pub impl_token: tokens::Impl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700175 /// Trait this impl implements.
176 pub trait_: Option<(ImplPolarity, Path, tokens::For)>,
177 /// The Self type of the impl.
178 pub self_ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700179 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub items: Vec<ImplItem>,
181 }),
182 /// A macro invocation (which includes macro definition).
183 ///
184 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnayc6b55bc2017-11-09 22:48:38 -0800185 pub Mac(ItemMac {
186 pub attrs: Vec<Attribute>,
187 pub mac: Mac,
188 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }
190
191 do_not_generate_to_tokens
David Tolnayb79ee962016-09-04 09:39:20 -0700192}
193
David Tolnay0e837402016-12-22 17:25:55 -0500194impl From<DeriveInput> for Item {
195 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 match input.body {
197 Body::Enum(data) => {
198 Item::Enum(ItemEnum {
199 attrs: input.attrs,
200 vis: input.vis,
201 enum_token: data.enum_token,
202 ident: input.ident,
203 generics: input.generics,
204 brace_token: data.brace_token,
205 variants: data.variants,
206 })
207 }
208 Body::Struct(data) => {
209 Item::Struct(ItemStruct {
210 attrs: input.attrs,
211 vis: input.vis,
212 struct_token: data.struct_token,
213 ident: input.ident,
214 generics: input.generics,
215 data: data.data,
216 semi_token: data.semi_token,
217 })
218 }
David Tolnay453cfd12016-10-23 11:00:14 -0700219 }
220 }
221}
222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223ast_enum_of_structs! {
224 pub enum ViewPath {
225 /// `foo::bar::baz as quux`
226 ///
227 /// or just
228 ///
229 /// `foo::bar::baz` (with `as baz` implicitly on the right)
230 pub Simple(PathSimple {
231 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700232 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700233 pub rename: Option<Ident>,
234 }),
235
236 /// `foo::bar::*`
237 pub Glob(PathGlob {
238 pub path: Path,
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700239 pub colon2_token: Option<tokens::Colon2>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700240 pub star_token: tokens::Star,
Alex Crichton62a0a592017-05-22 13:58:53 -0700241 }),
242
243 /// `foo::bar::{a, b, c}`
244 pub List(PathList {
245 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700246 pub colon2_token: tokens::Colon2,
247 pub brace_token: tokens::Brace,
248 pub items: Delimited<PathListItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 }),
250 }
251}
252
253ast_struct! {
254 pub struct PathListItem {
255 pub name: Ident,
256 /// renamed in list, e.g. `use foo::{bar as baz};`
257 pub rename: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700258 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 }
260}
261
262ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700263 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 pub enum Constness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700265 Const(tokens::Const),
Alex Crichton62a0a592017-05-22 13:58:53 -0700266 NotConst,
267 }
268}
269
270ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700271 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 pub enum Defaultness {
Alex Crichton954046c2017-05-30 21:49:42 -0700273 Default(tokens::Default_),
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 Final,
275 }
276}
277
278ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub struct ForeignItem {
280 pub ident: Ident,
281 pub attrs: Vec<Attribute>,
282 pub node: ForeignItemKind,
283 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 }
286}
287
288ast_enum_of_structs! {
289 /// An item within an `extern` block
290 pub enum ForeignItemKind {
291 /// A foreign function
292 pub Fn(ForeignItemFn {
293 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 }),
295 /// A foreign static item (`static ext: u8`)
296 pub Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700297 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700299 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 pub mutbl: Mutability,
301 }),
302 }
303
304 do_not_generate_to_tokens
305}
306
307ast_struct! {
308 /// Represents an item declaration within a trait declaration,
309 /// possibly including a default implementation. A trait item is
310 /// either required (meaning it doesn't have an implementation, just a
311 /// signature) or provided (meaning it has a default implementation).
312 pub struct TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 pub attrs: Vec<Attribute>,
314 pub node: TraitItemKind,
315 }
316}
317
318ast_enum_of_structs! {
319 pub enum TraitItemKind {
320 pub Const(TraitItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700321 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700322 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700323 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700325 pub default: Option<(tokens::Eq, Expr)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700326 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 }),
328 pub Method(TraitItemMethod {
329 pub sig: MethodSig,
330 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700331 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 }),
333 pub Type(TraitItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700334 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700335 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700336 pub colon_token: Option<tokens::Colon>,
337 pub bounds: Delimited<TyParamBound, tokens::Add>,
David Tolnay570695e2017-06-03 16:15:13 -0700338 pub default: Option<(tokens::Eq, Ty)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700339 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 }),
341 pub Macro(Mac),
342 }
343
344 do_not_generate_to_tokens
345}
346
347ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700348 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub enum ImplPolarity {
350 /// `impl Trait for Type`
351 Positive,
352 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700353 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 }
355}
356
357ast_struct! {
358 pub struct ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub attrs: Vec<Attribute>,
360 pub node: ImplItemKind,
361 }
362}
363
364ast_enum_of_structs! {
365 pub enum ImplItemKind {
366 pub Const(ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700367 pub vis: Visibility,
368 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700369 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700370 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700371 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700373 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700374 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700375 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 }),
377 pub Method(ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -0700378 pub vis: Visibility,
379 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub sig: MethodSig,
381 pub block: Block,
382 }),
383 pub Type(ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -0700384 pub vis: Visibility,
385 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700386 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700387 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700388 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700390 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 }),
392 pub Macro(Mac),
393 }
394
395 do_not_generate_to_tokens
396}
397
398ast_struct! {
399 /// Represents a method's signature in a trait declaration,
400 /// or in an implementation.
401 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700403 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700405 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700407 }
408}
409
410ast_struct! {
411 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700412 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 /// E.g. `fn foo(bar: baz)`
414 pub struct FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -0700415 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700416 pub paren_token: tokens::Paren,
417 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700418 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700419 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700420 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700421 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700423}
424
Alex Crichton62a0a592017-05-22 13:58:53 -0700425ast_enum_of_structs! {
426 /// An argument in a function header.
427 ///
428 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
429 pub enum FnArg {
430 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700431 pub and_token: tokens::And,
432 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 pub lifetime: Option<Lifetime>,
434 pub mutbl: Mutability,
435 }),
436 pub SelfValue(ArgSelf {
437 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700438 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 }),
440 pub Captured(ArgCaptured {
441 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700442 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 pub ty: Ty,
444 }),
445 pub Ignored(Ty),
446 }
David Tolnay62f374c2016-10-02 13:37:00 -0700447}
448
David Tolnayedf2b992016-09-23 20:43:45 -0700449#[cfg(feature = "parsing")]
450pub mod parsing {
451 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700452
Michael Layzell92639a52017-06-01 00:07:44 -0400453 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700454 use synom::tokens::*;
455 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700456
Alex Crichton954046c2017-05-30 21:49:42 -0700457 impl Synom for Item {
Michael Layzell92639a52017-06-01 00:07:44 -0400458 named!(parse -> Self, alt!(
459 item_extern_crate
460 |
461 item_use
462 |
463 item_static
464 |
465 item_const
466 |
467 item_fn
468 |
469 item_mod
470 |
471 item_foreign_mod
472 |
473 item_ty
474 |
475 item_struct_or_enum
476 |
477 item_union
478 |
479 item_trait
480 |
481 item_default_impl
482 |
483 item_impl
484 |
485 item_mac
486 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700487
488 fn description() -> Option<&'static str> {
489 Some("item")
490 }
491 }
David Tolnay453cfd12016-10-23 11:00:14 -0700492
David Tolnay84aa0752016-10-02 23:01:13 -0700493 named!(item_mac -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700494 attrs: many0!(call!(Attribute::parse_outer)) >>
495 what: syn!(Path) >>
496 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700497 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700498 body: call!(::TokenTree::parse_delimited) >>
499 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800500 (Item::Mac(ItemMac {
David Tolnay84aa0752016-10-02 23:01:13 -0700501 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800502 mac: Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -0500503 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700504 bang_token: bang,
505 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700506 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800507 },
508 }))
David Tolnayedf2b992016-09-23 20:43:45 -0700509 ));
510
David Tolnaya96a3fa2016-09-24 07:17:42 -0700511 named!(item_extern_crate -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700512 attrs: many0!(call!(Attribute::parse_outer)) >>
513 vis: syn!(Visibility) >>
514 extern_: syn!(Extern) >>
515 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700516 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700517 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
518 semi: syn!(Semi) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800519 (Item::ExternCrate(ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700520 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800521 vis: vis,
522 extern_token: extern_,
523 crate_token: crate_,
524 ident: ident,
525 rename: rename,
526 semi_token: semi,
527 }))
David Tolnayedf2b992016-09-23 20:43:45 -0700528 ));
529
David Tolnay4a057422016-10-08 00:02:31 -0700530 named!(item_use -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700531 attrs: many0!(call!(Attribute::parse_outer)) >>
532 vis: syn!(Visibility) >>
533 use_: syn!(Use) >>
534 what: syn!(ViewPath) >>
535 semi: syn!(Semi) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800536 (Item::Use(ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700537 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800538 vis: vis,
539 use_token: use_,
540 path: Box::new(what),
541 semi_token: semi,
542 }))
David Tolnay4a057422016-10-08 00:02:31 -0700543 ));
544
Alex Crichton954046c2017-05-30 21:49:42 -0700545 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400546 named!(parse -> Self, alt!(
547 syn!(PathGlob) => { ViewPath::Glob }
548 |
549 syn!(PathList) => { ViewPath::List }
550 |
551 syn!(PathSimple) => { ViewPath::Simple } // must be last
552 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700553 }
David Tolnay4a057422016-10-08 00:02:31 -0700554
Alex Crichton954046c2017-05-30 21:49:42 -0700555 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400556 named!(parse -> Self, do_parse!(
557 path: syn!(Path) >>
558 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
559 (PathSimple {
560 path: path,
561 as_token: rename.as_ref().map(|p| As((p.0).0)),
562 rename: rename.map(|p| p.1),
563 })
564 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700565 }
David Tolnay4a057422016-10-08 00:02:31 -0700566
Alex Crichton954046c2017-05-30 21:49:42 -0700567 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400568 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700569 path: option!(do_parse!(
570 path: syn!(Path) >>
571 colon2: syn!(Colon2) >>
572 (path, colon2)
573 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400574 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700575 ({
576 match path {
577 Some((path, colon2)) => {
578 PathGlob {
579 path: path,
580 colon2_token: Some(colon2),
581 star_token: star,
582 }
583 }
584 None => {
585 PathGlob {
586 path: Path {
587 leading_colon: None,
588 segments: Default::default(),
589 },
590 colon2_token: None,
591 star_token: star,
592 }
593 }
594 }
Michael Layzell92639a52017-06-01 00:07:44 -0400595 })
596 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700597 }
David Tolnay4a057422016-10-08 00:02:31 -0700598
Alex Crichton954046c2017-05-30 21:49:42 -0700599 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400600 named!(parse -> Self, alt!(
601 do_parse!(
602 path: syn!(Path) >>
603 colon2: syn!(Colon2) >>
604 items: braces!(call!(Delimited::parse_terminated)) >>
605 (PathList {
606 path: path,
607 items: items.0,
608 brace_token: items.1,
609 colon2_token: colon2,
610 })
611 )
612 |
613 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700614 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400615 items: braces!(call!(Delimited::parse_terminated)) >>
616 (PathList {
617 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400618 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700619 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400620 },
David Tolnay570695e2017-06-03 16:15:13 -0700621 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400622 brace_token: items.1,
623 items: items.0,
624 })
625 )
626 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700627 }
David Tolnay4a057422016-10-08 00:02:31 -0700628
Alex Crichton954046c2017-05-30 21:49:42 -0700629 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400630 named!(parse -> Self, do_parse!(
631 name: alt!(
632 syn!(Ident)
633 |
634 map!(syn!(Self_), Into::into)
635 ) >>
636 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
637 (PathListItem {
638 name: name,
639 as_token: rename.as_ref().map(|p| As((p.0).0)),
640 rename: rename.map(|p| p.1),
641 })
642 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700643 }
David Tolnay4a057422016-10-08 00:02:31 -0700644
David Tolnay47a877c2016-10-01 16:50:55 -0700645 named!(item_static -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700646 attrs: many0!(call!(Attribute::parse_outer)) >>
647 vis: syn!(Visibility) >>
648 static_: syn!(Static) >>
649 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700650 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700651 colon: syn!(Colon) >>
652 ty: syn!(Ty) >>
653 eq: syn!(Eq) >>
654 value: syn!(Expr) >>
655 semi: syn!(Semi) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800656 (Item::Static(ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700657 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800658 vis: vis,
659 static_token: static_,
660 mutbl: mutability,
661 ident: ident,
662 colon_token: colon,
663 ty: Box::new(ty),
664 eq_token: eq,
665 expr: Box::new(value),
666 semi_token: semi,
667 }))
David Tolnay47a877c2016-10-01 16:50:55 -0700668 ));
669
670 named!(item_const -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700671 attrs: many0!(call!(Attribute::parse_outer)) >>
672 vis: syn!(Visibility) >>
673 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700674 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700675 colon: syn!(Colon) >>
676 ty: syn!(Ty) >>
677 eq: syn!(Eq) >>
678 value: syn!(Expr) >>
679 semi: syn!(Semi) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800680 (Item::Const(ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700681 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800682 vis: vis,
683 const_token: const_,
684 ident: ident,
685 colon_token: colon,
686 ty: Box::new(ty),
687 eq_token: eq,
688 expr: Box::new(value),
689 semi_token: semi,
690 }))
David Tolnay47a877c2016-10-01 16:50:55 -0700691 ));
692
David Tolnay42602292016-10-01 22:25:45 -0700693 named!(item_fn -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700694 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
695 vis: syn!(Visibility) >>
696 constness: syn!(Constness) >>
697 unsafety: syn!(Unsafety) >>
698 abi: option!(syn!(Abi)) >>
699 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700700 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700701 generics: syn!(Generics) >>
702 inputs: parens!(Delimited::parse_terminated) >>
703 ret: syn!(FunctionRetTy) >>
704 where_clause: syn!(WhereClause) >>
705 inner_attrs_stmts: braces!(tuple!(
706 many0!(call!(Attribute::parse_inner)),
707 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400708 )) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800709 (Item::Fn(ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700710 attrs: {
711 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700712 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700713 attrs
714 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800715 vis: vis,
716 constness: constness,
717 unsafety: unsafety,
718 abi: abi,
719 decl: Box::new(FnDecl {
720 dot_tokens: None,
721 fn_token: fn_,
722 paren_token: inputs.1,
723 inputs: inputs.0,
724 output: ret,
725 variadic: false,
726 generics: Generics {
727 where_clause: where_clause,
728 .. generics
729 },
730 }),
731 ident: ident,
732 block: Box::new(Block {
733 brace_token: inner_attrs_stmts.1,
734 stmts: (inner_attrs_stmts.0).1,
735 }),
736 }))
David Tolnay42602292016-10-01 22:25:45 -0700737 ));
738
Alex Crichton954046c2017-05-30 21:49:42 -0700739 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400740 named!(parse -> Self, alt!(
741 do_parse!(
742 and: syn!(And) >>
743 lt: option!(syn!(Lifetime)) >>
744 mutability: syn!(Mutability) >>
745 self_: syn!(Self_) >>
746 not!(syn!(Colon)) >>
747 (ArgSelfRef {
748 lifetime: lt,
749 mutbl: mutability,
750 and_token: and,
751 self_token: self_,
752 }.into())
753 )
754 |
755 do_parse!(
756 mutability: syn!(Mutability) >>
757 self_: syn!(Self_) >>
758 not!(syn!(Colon)) >>
759 (ArgSelf {
760 mutbl: mutability,
761 self_token: self_,
762 }.into())
763 )
764 |
765 do_parse!(
766 pat: syn!(Pat) >>
767 colon: syn!(Colon) >>
768 ty: syn!(Ty) >>
769 (ArgCaptured {
770 pat: pat,
771 ty: ty,
772 colon_token: colon,
773 }.into())
774 )
775 |
776 syn!(Ty) => { FnArg::Ignored }
777 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700778 }
David Tolnay62f374c2016-10-02 13:37:00 -0700779
David Tolnay35902302016-10-06 01:11:08 -0700780 named!(item_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700781 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
782 vis: syn!(Visibility) >>
783 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700784 ident: syn!(Ident) >>
785 content_semi: alt!(
786 syn!(Semi) => {|semi| (
787 Vec::new(),
788 None,
789 Some(semi),
790 )}
David Tolnay37d10332016-10-13 20:51:04 -0700791 |
Alex Crichton954046c2017-05-30 21:49:42 -0700792 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700793 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700794 many0!(call!(Attribute::parse_inner)),
795 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400796 )
David Tolnay570695e2017-06-03 16:15:13 -0700797 ) => {|((inner_attrs, items), brace)| (
798 inner_attrs,
799 Some((brace, items)),
800 None,
801 )}
David Tolnay37d10332016-10-13 20:51:04 -0700802 ) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800803 (Item::Mod(ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700804 attrs: {
805 let mut attrs = outer_attrs;
806 attrs.extend(content_semi.0);
807 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700808 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800809 vis: vis,
810 mod_token: mod_,
811 ident: ident,
812 content: content_semi.1,
813 semi: content_semi.2,
814 }))
David Tolnay35902302016-10-06 01:11:08 -0700815 ));
816
817 named!(item_foreign_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700818 attrs: many0!(call!(Attribute::parse_outer)) >>
819 abi: syn!(Abi) >>
820 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800821 (Item::ForeignMod(ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700822 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800823 abi: abi,
824 brace_token: items.1,
825 items: items.0,
826 }))
David Tolnay35902302016-10-06 01:11:08 -0700827 ));
828
Alex Crichton954046c2017-05-30 21:49:42 -0700829 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400830 named!(parse -> Self, alt!(
831 foreign_fn
832 |
833 foreign_static
834 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700835 }
David Tolnay35902302016-10-06 01:11:08 -0700836
837 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700838 attrs: many0!(call!(Attribute::parse_outer)) >>
839 vis: syn!(Visibility) >>
840 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700841 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700842 generics: syn!(Generics) >>
843 inputs: parens!(do_parse!(
844 args: call!(Delimited::parse_terminated) >>
845 variadic: cond!(args.is_empty() || args.trailing_delim(),
846 option!(syn!(Dot3))) >>
847 (args, variadic)
848 )) >>
849 ret: syn!(FunctionRetTy) >>
850 where_clause: syn!(WhereClause) >>
851 semi: syn!(Semi) >>
852 ({
853 let ((inputs, variadic), parens) = inputs;
854 let variadic = variadic.and_then(|v| v);
855 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700856 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700857 attrs: attrs,
858 semi_token: semi,
859 node: ForeignItemFn {
860 decl: Box::new(FnDecl {
861 fn_token: fn_,
862 paren_token: parens,
863 inputs: inputs,
864 variadic: variadic.is_some(),
865 dot_tokens: variadic,
866 output: ret,
867 generics: Generics {
868 where_clause: where_clause,
869 .. generics
870 },
871 }),
872 }.into(),
873 vis: vis,
874 }
David Tolnay35902302016-10-06 01:11:08 -0700875 })
876 ));
877
878 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700879 attrs: many0!(call!(Attribute::parse_outer)) >>
880 vis: syn!(Visibility) >>
881 static_: syn!(Static) >>
882 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700883 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700884 colon: syn!(Colon) >>
885 ty: syn!(Ty) >>
886 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700887 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700888 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700889 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700890 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 node: ForeignItemStatic {
892 ty: Box::new(ty),
893 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700894 static_token: static_,
895 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700897 vis: vis,
898 })
899 ));
900
David Tolnay3cf52982016-10-01 17:11:37 -0700901 named!(item_ty -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700902 attrs: many0!(call!(Attribute::parse_outer)) >>
903 vis: syn!(Visibility) >>
904 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700905 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700906 generics: syn!(Generics) >>
907 where_clause: syn!(WhereClause) >>
908 eq: syn!(Eq) >>
909 ty: syn!(Ty) >>
910 semi: syn!(Semi) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800911 (Item::Ty(ItemTy {
David Tolnay3cf52982016-10-01 17:11:37 -0700912 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800913 vis: vis,
914 type_token: type_,
915 ident: ident,
916 generics: Generics {
917 where_clause: where_clause,
918 ..generics
919 },
920 eq_token: eq,
921 ty: Box::new(ty),
922 semi_token: semi,
923 }))
David Tolnay3cf52982016-10-01 17:11:37 -0700924 ));
925
David Tolnay570695e2017-06-03 16:15:13 -0700926 named!(item_struct_or_enum -> Item, map!(syn!(DeriveInput), Into::into));
David Tolnay42602292016-10-01 22:25:45 -0700927
David Tolnay2f9fa632016-10-03 22:08:48 -0700928 named!(item_union -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700929 attrs: many0!(call!(Attribute::parse_outer)) >>
930 vis: syn!(Visibility) >>
931 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700932 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700933 generics: syn!(Generics) >>
934 where_clause: syn!(WhereClause) >>
935 fields: braces!(call!(Delimited::parse_terminated_with,
936 Field::parse_struct)) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800937 (Item::Union(ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700938 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800939 vis: vis,
940 union_token: union_,
941 ident: ident,
942 generics: Generics {
943 where_clause: where_clause,
944 .. generics
945 },
946 data: VariantData::Struct(fields.0, fields.1),
947 }))
David Tolnay2f9fa632016-10-03 22:08:48 -0700948 ));
949
David Tolnay0aecb732016-10-03 23:03:50 -0700950 named!(item_trait -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700951 attrs: many0!(call!(Attribute::parse_outer)) >>
952 vis: syn!(Visibility) >>
953 unsafety: syn!(Unsafety) >>
954 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700955 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700956 generics: syn!(Generics) >>
957 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700958 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700959 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700960 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700961 where_clause: syn!(WhereClause) >>
962 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800963 (Item::Trait(ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700964 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800965 vis: vis,
966 unsafety: unsafety,
967 trait_token: trait_,
968 ident: ident,
969 generics: Generics {
970 where_clause: where_clause,
971 .. generics
972 },
973 colon_token: colon,
974 supertraits: bounds.unwrap_or_default(),
975 brace_token: body.1,
976 items: body.0,
977 }))
David Tolnay0aecb732016-10-03 23:03:50 -0700978 ));
979
David Tolnayf94e2362016-10-04 00:29:51 -0700980 named!(item_default_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700981 attrs: many0!(call!(Attribute::parse_outer)) >>
982 unsafety: syn!(Unsafety) >>
983 impl_: syn!(Impl) >>
984 path: syn!(Path) >>
985 for_: syn!(For) >>
986 dot2: syn!(Dot2) >>
987 braces: braces!(epsilon!()) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -0800988 (Item::DefaultImpl(ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700989 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800990 unsafety: unsafety,
991 impl_token: impl_,
992 path: path,
993 for_token: for_,
994 dot2_token: dot2,
995 brace_token: braces.1,
996 }))
David Tolnayf94e2362016-10-04 00:29:51 -0700997 ));
998
Alex Crichton954046c2017-05-30 21:49:42 -0700999 impl Synom for TraitItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001000 named!(parse -> Self, alt!(
1001 trait_item_const
1002 |
1003 trait_item_method
1004 |
1005 trait_item_type
1006 |
1007 trait_item_mac
1008 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001009 }
David Tolnay0aecb732016-10-03 23:03:50 -07001010
1011 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001012 attrs: many0!(call!(Attribute::parse_outer)) >>
1013 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001014 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001015 colon: syn!(Colon) >>
1016 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001017 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001018 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001019 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001020 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 node: TraitItemConst {
Alex Crichton954046c2017-05-30 21:49:42 -07001022 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001023 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001024 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001025 ty: ty,
1026 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001027 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001028 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001029 })
1030 ));
1031
1032 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001033 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1034 constness: syn!(Constness) >>
1035 unsafety: syn!(Unsafety) >>
1036 abi: option!(syn!(Abi)) >>
1037 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001038 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001039 generics: syn!(Generics) >>
1040 inputs: parens!(call!(Delimited::parse_terminated)) >>
1041 ret: syn!(FunctionRetTy) >>
1042 where_clause: syn!(WhereClause) >>
1043 body: option!(braces!(
1044 tuple!(many0!(call!(Attribute::parse_inner)),
1045 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001046 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001047 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001048 ({
1049 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001050 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001051 None => (Vec::new(), None),
1052 };
1053 TraitItem {
David Tolnay5859df12016-10-29 22:49:54 -07001054 attrs: {
1055 let mut attrs = outer_attrs;
1056 attrs.extend(inner_attrs);
1057 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001058 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001059 node: TraitItemMethod {
1060 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001061 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001062 unsafety: unsafety,
David Tolnay5859df12016-10-29 22:49:54 -07001063 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001064 ident: ident,
David Tolnay5859df12016-10-29 22:49:54 -07001065 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001066 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001067 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001068 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001069 fn_token: fn_,
1070 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 dot_tokens: None,
1072 generics: Generics {
1073 where_clause: where_clause,
1074 .. generics
1075 },
David Tolnay5859df12016-10-29 22:49:54 -07001076 },
1077 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001078 default: stmts.map(|stmts| {
1079 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001080 stmts: stmts.0,
1081 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001082 }
1083 }),
David Tolnay570695e2017-06-03 16:15:13 -07001084 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001085 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001086 }
David Tolnay0aecb732016-10-03 23:03:50 -07001087 })
1088 ));
1089
1090 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001091 attrs: many0!(call!(Attribute::parse_outer)) >>
1092 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001093 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001094 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001095 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001096 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001097 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001098 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1099 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001100 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001101 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001102 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001103 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001104 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001105 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001106 bounds: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -07001107 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001108 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001110 })
1111 ));
1112
1113 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001114 attrs: many0!(call!(Attribute::parse_outer)) >>
1115 mac: syn!(Mac) >>
1116 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001117 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001118 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001119 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001120 })
1121 ));
1122
David Tolnay4c9be372016-10-06 00:47:37 -07001123 named!(item_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001124 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001125 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001126 unsafety: syn!(Unsafety) >>
1127 impl_: syn!(Impl) >>
1128 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001129 polarity_path: alt!(
1130 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001131 polarity: syn!(ImplPolarity) >>
1132 path: syn!(Path) >>
1133 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001134 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001135 )
1136 |
David Tolnay570695e2017-06-03 16:15:13 -07001137 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001138 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001139 self_ty: syn!(Ty) >>
1140 where_clause: syn!(WhereClause) >>
1141 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnayc6b55bc2017-11-09 22:48:38 -08001142 (Item::Impl(ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001143 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001144 defaultness: defaultness,
1145 unsafety: unsafety,
1146 impl_token: impl_,
1147 generics: Generics {
1148 where_clause: where_clause,
1149 .. generics
1150 },
1151 trait_: polarity_path,
1152 self_ty: Box::new(self_ty),
1153 brace_token: body.1,
1154 items: body.0,
1155 }))
David Tolnay4c9be372016-10-06 00:47:37 -07001156 ));
1157
Alex Crichton954046c2017-05-30 21:49:42 -07001158 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001159 named!(parse -> Self, alt!(
1160 impl_item_const
1161 |
1162 impl_item_method
1163 |
1164 impl_item_type
1165 |
David Tolnay570695e2017-06-03 16:15:13 -07001166 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001167 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001168 }
David Tolnay4c9be372016-10-06 00:47:37 -07001169
1170 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001171 attrs: many0!(call!(Attribute::parse_outer)) >>
1172 vis: syn!(Visibility) >>
1173 defaultness: syn!(Defaultness) >>
1174 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001175 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001176 colon: syn!(Colon) >>
1177 ty: syn!(Ty) >>
1178 eq: syn!(Eq) >>
1179 value: syn!(Expr) >>
1180 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001181 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001182 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001183 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001184 vis: vis,
1185 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001186 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001187 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001188 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001189 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001190 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001191 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001192 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001193 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001194 })
1195 ));
1196
1197 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001198 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1199 vis: syn!(Visibility) >>
1200 defaultness: syn!(Defaultness) >>
1201 constness: syn!(Constness) >>
1202 unsafety: syn!(Unsafety) >>
1203 abi: option!(syn!(Abi)) >>
1204 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001205 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001206 generics: syn!(Generics) >>
1207 inputs: parens!(call!(Delimited::parse_terminated)) >>
1208 ret: syn!(FunctionRetTy) >>
1209 where_clause: syn!(WhereClause) >>
1210 inner_attrs_stmts: braces!(tuple!(
1211 many0!(call!(Attribute::parse_inner)),
1212 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001213 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001214 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001215 attrs: {
1216 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001217 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001218 attrs
1219 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001220 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001221 vis: vis,
1222 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001223 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001224 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001225 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001226 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001227 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001228 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001229 fn_token: fn_,
1230 paren_token: inputs.1,
1231 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001232 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001233 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001234 generics: Generics {
1235 where_clause: where_clause,
1236 .. generics
1237 },
1238 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001239 },
1240 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001241 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001242 brace_token: inner_attrs_stmts.1,
1243 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001244 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001245 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001246 })
1247 ));
1248
1249 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001250 attrs: many0!(call!(Attribute::parse_outer)) >>
1251 vis: syn!(Visibility) >>
1252 defaultness: syn!(Defaultness) >>
1253 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001254 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001255 eq: syn!(Eq) >>
1256 ty: syn!(Ty) >>
1257 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001258 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001259 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001260 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001261 vis: vis,
1262 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001263 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001264 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001265 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001266 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001267 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001268 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001269 })
1270 ));
1271
David Tolnay570695e2017-06-03 16:15:13 -07001272 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001273 attrs: many0!(call!(Attribute::parse_outer)) >>
1274 mac: syn!(Mac) >>
1275 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001276 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001277 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001278 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001279 })
1280 ));
1281
Alex Crichton954046c2017-05-30 21:49:42 -07001282 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001283 named!(parse -> Self, alt!(
1284 syn!(Bang) => { ImplPolarity::Negative }
1285 |
1286 epsilon!() => { |_| ImplPolarity::Positive }
1287 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001288 }
David Tolnay4c9be372016-10-06 00:47:37 -07001289
Alex Crichton954046c2017-05-30 21:49:42 -07001290 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001291 named!(parse -> Self, alt!(
1292 syn!(Const) => { Constness::Const }
1293 |
1294 epsilon!() => { |_| Constness::NotConst }
1295 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001296 }
David Tolnay42602292016-10-01 22:25:45 -07001297
Alex Crichton954046c2017-05-30 21:49:42 -07001298 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001299 named!(parse -> Self, alt!(
1300 syn!(Default_) => { Defaultness::Default }
1301 |
1302 epsilon!() => { |_| Defaultness::Final }
1303 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001304 }
David Tolnayedf2b992016-09-23 20:43:45 -07001305}
David Tolnay4a51dc72016-10-01 00:40:31 -07001306
1307#[cfg(feature = "printing")]
1308mod printing {
1309 use super::*;
1310 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001311 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001312 use quote::{Tokens, ToTokens};
1313
1314 impl ToTokens for Item {
1315 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001316 match *self {
1317 Item::ExternCrate(ref item) => {
1318 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001319 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001320 item.extern_token.to_tokens(tokens);
1321 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001322 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001323 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001324 as_token.to_tokens(tokens);
1325 rename.to_tokens(tokens);
1326 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001327 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001328 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001329 Item::Use(ref item) => {
1330 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001331 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001332 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001333 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001334 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001335 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001336 Item::Static(ref item) => {
1337 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001338 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001339 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001340 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001341 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001342 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001343 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001344 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001345 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001346 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001347 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001348 Item::Const(ref item) => {
1349 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001350 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001351 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001352 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001353 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001354 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001355 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001356 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001357 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001358 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001359 Item::Fn(ref item) => {
1360 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001361 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001362 item.constness.to_tokens(tokens);
1363 item.unsafety.to_tokens(tokens);
1364 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001365 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001366 item.block.brace_token.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001367 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001368 tokens.append_all(&item.block.stmts);
1369 });
David Tolnay42602292016-10-01 22:25:45 -07001370 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001371 Item::Mod(ref item) => {
1372 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001373 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001375 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001376 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001377 brace.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001378 tokens.append_all(item.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001379 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001380 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001381 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001382 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001383 }
David Tolnay35902302016-10-06 01:11:08 -07001384 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001385 Item::ForeignMod(ref item) => {
1386 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001387 item.abi.to_tokens(tokens);
1388 item.brace_token.surround(tokens, |tokens| {
1389 tokens.append_all(&item.items);
1390 });
David Tolnay35902302016-10-06 01:11:08 -07001391 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001392 Item::Ty(ref item) => {
1393 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001394 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001396 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001397 item.generics.to_tokens(tokens);
1398 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001399 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001400 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001401 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001402 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001403 Item::Enum(ref item) => {
1404 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001405 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001406 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001407 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 item.generics.to_tokens(tokens);
1409 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001410 item.brace_token.surround(tokens, |tokens| {
1411 item.variants.to_tokens(tokens);
1412 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001413 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001414 Item::Struct(ref item) => {
1415 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001416 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001417 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001418 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001419 item.generics.to_tokens(tokens);
1420 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001421 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001422 item.generics.where_clause.to_tokens(tokens);
1423 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001424 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001425 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001426 item.data.to_tokens(tokens);
1427 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001428 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001429 }
1430 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001431 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001432 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001433 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001434 }
1435 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001436 Item::Union(ref item) => {
1437 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001438 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001439 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001440 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001441 item.generics.to_tokens(tokens);
1442 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001443 // XXX: Should we handle / complain when using a
1444 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001445 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001446 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001447 Item::Trait(ref item) => {
1448 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001449 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001450 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001451 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001452 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001453 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001454 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001455 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001456 item.supertraits.to_tokens(tokens);
1457 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001458 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001459 item.brace_token.surround(tokens, |tokens| {
1460 tokens.append_all(&item.items);
1461 });
David Tolnayca085422016-10-04 00:12:38 -07001462 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001463 Item::DefaultImpl(ref item) => {
1464 tokens.append_all(item.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001465 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001466 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001467 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001468 item.for_token.to_tokens(tokens);
1469 item.dot2_token.to_tokens(tokens);
1470 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001471 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001472 Item::Impl(ref item) => {
1473 tokens.append_all(item.attrs.outer());
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001474 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001476 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001477 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001478 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001479 polarity.to_tokens(tokens);
1480 path.to_tokens(tokens);
1481 for_token.to_tokens(tokens);
1482 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 item.self_ty.to_tokens(tokens);
1484 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001485 item.brace_token.surround(tokens, |tokens| {
1486 tokens.append_all(&item.items);
1487 });
David Tolnay4c9be372016-10-06 00:47:37 -07001488 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001489 Item::Mac(ref item) => {
1490 tokens.append_all(item.attrs.outer());
1491 item.mac.path.to_tokens(tokens);
1492 item.mac.bang_token.to_tokens(tokens);
1493 item.mac.ident.to_tokens(tokens);
1494 tokens.append_all(&item.mac.tokens);
1495 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001496 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001497 }
1498 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001499 }
1500 }
1501 }
David Tolnay42602292016-10-01 22:25:45 -07001502
Alex Crichton62a0a592017-05-22 13:58:53 -07001503 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001504 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001505 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001506 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001507 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001508 self.rename.to_tokens(tokens);
1509 }
David Tolnay4a057422016-10-08 00:02:31 -07001510 }
1511 }
1512
Alex Crichton62a0a592017-05-22 13:58:53 -07001513 impl ToTokens for PathGlob {
1514 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001515 if self.path.segments.len() > 0 {
1516 self.path.to_tokens(tokens);
1517 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1518 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001519 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001520 }
1521 }
1522
1523 impl ToTokens for PathList {
1524 fn to_tokens(&self, tokens: &mut Tokens) {
1525 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001526 self.colon2_token.to_tokens(tokens);
1527 self.brace_token.surround(tokens, |tokens| {
1528 self.items.to_tokens(tokens);
1529 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001530 }
1531 }
1532
David Tolnay4a057422016-10-08 00:02:31 -07001533 impl ToTokens for PathListItem {
1534 fn to_tokens(&self, tokens: &mut Tokens) {
1535 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001536 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001537 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001538 self.rename.to_tokens(tokens);
1539 }
David Tolnay4a057422016-10-08 00:02:31 -07001540 }
1541 }
1542
David Tolnayca085422016-10-04 00:12:38 -07001543 impl ToTokens for TraitItem {
1544 fn to_tokens(&self, tokens: &mut Tokens) {
1545 tokens.append_all(self.attrs.outer());
1546 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001547 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001548 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001549 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001550 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001551 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001552 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001553 eq_token.to_tokens(tokens);
1554 default.to_tokens(tokens);
1555 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001556 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001557 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001558 TraitItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001559 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001561 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001562 block.brace_token.surround(tokens, |tokens| {
1563 tokens.append_all(self.attrs.inner());
1564 tokens.append_all(&block.stmts);
1565 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001566 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001567 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001568 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 }
David Tolnayca085422016-10-04 00:12:38 -07001570 }
1571 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001572 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001573 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001574 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001575 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001576 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001577 item.bounds.to_tokens(tokens);
1578 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001579 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001580 eq_token.to_tokens(tokens);
1581 default.to_tokens(tokens);
1582 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001583 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001584 }
1585 TraitItemKind::Macro(ref mac) => {
1586 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001587 if !mac.is_braced() {
1588 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001589 }
David Tolnayca085422016-10-04 00:12:38 -07001590 }
1591 }
1592 }
1593 }
1594
David Tolnay4c9be372016-10-06 00:47:37 -07001595 impl ToTokens for ImplItem {
1596 fn to_tokens(&self, tokens: &mut Tokens) {
1597 tokens.append_all(self.attrs.outer());
1598 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001599 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001600 item.vis.to_tokens(tokens);
1601 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001602 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001603 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001604 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001605 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001606 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001608 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001609 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001610 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001611 item.vis.to_tokens(tokens);
1612 item.defaultness.to_tokens(tokens);
1613 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001614 item.block.brace_token.surround(tokens, |tokens| {
1615 tokens.append_all(self.attrs.inner());
1616 tokens.append_all(&item.block.stmts);
1617 });
David Tolnay4c9be372016-10-06 00:47:37 -07001618 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001619 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001620 item.vis.to_tokens(tokens);
1621 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001622 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001623 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001624 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001625 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001626 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001627 }
1628 ImplItemKind::Macro(ref mac) => {
1629 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001630 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001631 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001632 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001633 }
1634 }
1635 }
1636 }
1637 }
1638
David Tolnay35902302016-10-06 01:11:08 -07001639 impl ToTokens for ForeignItem {
1640 fn to_tokens(&self, tokens: &mut Tokens) {
1641 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001642 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001643 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001644 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001645 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001646 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001647 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001648 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001650 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001651 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001652 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001653 }
1654 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001655 self.semi_token.to_tokens(tokens);
1656 }
1657 }
1658
David Tolnay570695e2017-06-03 16:15:13 -07001659 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001660 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001661 self.constness.to_tokens(tokens);
1662 self.unsafety.to_tokens(tokens);
1663 self.abi.to_tokens(tokens);
1664 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001665 }
1666 }
1667
David Tolnay570695e2017-06-03 16:15:13 -07001668 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001669
1670 impl<'a> ToTokens for NamedDecl<'a> {
1671 fn to_tokens(&self, tokens: &mut Tokens) {
1672 self.0.fn_token.to_tokens(tokens);
1673 self.1.to_tokens(tokens);
1674 self.0.generics.to_tokens(tokens);
1675 self.0.paren_token.surround(tokens, |tokens| {
1676 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001677
1678 if self.0.variadic {
1679 if !self.0.inputs.empty_or_trailing() {
1680 tokens::Comma::default().to_tokens(tokens);
1681 }
Alex Crichton259ee532017-07-14 06:51:02 -07001682 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001683 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001684 });
1685 self.0.output.to_tokens(tokens);
1686 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001687 }
1688 }
1689
Alex Crichton62a0a592017-05-22 13:58:53 -07001690 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001691 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001692 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001693 self.lifetime.to_tokens(tokens);
1694 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001695 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001696 }
1697 }
1698
1699 impl ToTokens for ArgSelf {
1700 fn to_tokens(&self, tokens: &mut Tokens) {
1701 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001702 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001703 }
1704 }
1705
1706 impl ToTokens for ArgCaptured {
1707 fn to_tokens(&self, tokens: &mut Tokens) {
1708 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001709 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001710 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001711 }
1712 }
1713
David Tolnay42602292016-10-01 22:25:45 -07001714 impl ToTokens for Constness {
1715 fn to_tokens(&self, tokens: &mut Tokens) {
1716 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001717 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001718 Constness::NotConst => {
1719 // nothing
1720 }
David Tolnay42602292016-10-01 22:25:45 -07001721 }
1722 }
1723 }
1724
David Tolnay4c9be372016-10-06 00:47:37 -07001725 impl ToTokens for Defaultness {
1726 fn to_tokens(&self, tokens: &mut Tokens) {
1727 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001728 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001729 Defaultness::Final => {
1730 // nothing
1731 }
1732 }
1733 }
1734 }
1735
1736 impl ToTokens for ImplPolarity {
1737 fn to_tokens(&self, tokens: &mut Tokens) {
1738 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001739 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001740 ImplPolarity::Positive => {
1741 // nothing
1742 }
1743 }
1744 }
1745 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001746}