blob: 44d13027d7a5c0e8659fd7411bec20d81aa27149 [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
David Tolnay4c614be2017-11-10 00:02:38 -0800457 impl_synom!(Item "item" alt!(
458 syn!(ItemExternCrate) => { Item::ExternCrate }
459 |
460 syn!(ItemUse) => { Item::Use }
461 |
462 syn!(ItemStatic) => { Item::Static }
463 |
464 syn!(ItemConst) => { Item::Const }
465 |
466 syn!(ItemFn) => { Item::Fn }
467 |
468 syn!(ItemMod) => { Item::Mod }
469 |
470 syn!(ItemForeignMod) => { Item::ForeignMod }
471 |
472 syn!(ItemTy) => { Item::Ty }
473 |
474 syn!(ItemStruct) => { Item::Struct }
475 |
476 syn!(ItemEnum) => { Item::Enum }
477 |
478 syn!(ItemUnion) => { Item::Union }
479 |
480 syn!(ItemTrait) => { Item::Trait }
481 |
482 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
483 |
484 syn!(ItemImpl) => { Item::Impl }
485 |
486 syn!(ItemMac) => { Item::Mac }
487 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700488
David Tolnay4c614be2017-11-10 00:02:38 -0800489 impl_synom!(ItemMac "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700490 attrs: many0!(call!(Attribute::parse_outer)) >>
491 what: syn!(Path) >>
492 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700493 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700494 body: call!(::TokenTree::parse_delimited) >>
495 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800496 (ItemMac {
David Tolnay84aa0752016-10-02 23:01:13 -0700497 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800498 mac: Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -0500499 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700500 bang_token: bang,
501 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700502 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800503 },
David Tolnay4c614be2017-11-10 00:02:38 -0800504 })
David Tolnayedf2b992016-09-23 20:43:45 -0700505 ));
506
David Tolnay4c614be2017-11-10 00:02:38 -0800507 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700508 attrs: many0!(call!(Attribute::parse_outer)) >>
509 vis: syn!(Visibility) >>
510 extern_: syn!(Extern) >>
511 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700512 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700513 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
514 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800515 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700516 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800517 vis: vis,
518 extern_token: extern_,
519 crate_token: crate_,
520 ident: ident,
521 rename: rename,
522 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800523 })
David Tolnayedf2b992016-09-23 20:43:45 -0700524 ));
525
David Tolnay4c614be2017-11-10 00:02:38 -0800526 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700527 attrs: many0!(call!(Attribute::parse_outer)) >>
528 vis: syn!(Visibility) >>
529 use_: syn!(Use) >>
530 what: syn!(ViewPath) >>
531 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800532 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700533 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800534 vis: vis,
535 use_token: use_,
536 path: Box::new(what),
537 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800538 })
David Tolnay4a057422016-10-08 00:02:31 -0700539 ));
540
Alex Crichton954046c2017-05-30 21:49:42 -0700541 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400542 named!(parse -> Self, alt!(
543 syn!(PathGlob) => { ViewPath::Glob }
544 |
545 syn!(PathList) => { ViewPath::List }
546 |
547 syn!(PathSimple) => { ViewPath::Simple } // must be last
548 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700549 }
David Tolnay4a057422016-10-08 00:02:31 -0700550
Alex Crichton954046c2017-05-30 21:49:42 -0700551 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400552 named!(parse -> Self, do_parse!(
553 path: syn!(Path) >>
554 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
555 (PathSimple {
556 path: path,
557 as_token: rename.as_ref().map(|p| As((p.0).0)),
558 rename: rename.map(|p| p.1),
559 })
560 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700561 }
David Tolnay4a057422016-10-08 00:02:31 -0700562
Alex Crichton954046c2017-05-30 21:49:42 -0700563 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400564 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700565 path: option!(do_parse!(
566 path: syn!(Path) >>
567 colon2: syn!(Colon2) >>
568 (path, colon2)
569 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400570 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700571 ({
572 match path {
573 Some((path, colon2)) => {
574 PathGlob {
575 path: path,
576 colon2_token: Some(colon2),
577 star_token: star,
578 }
579 }
580 None => {
581 PathGlob {
582 path: Path {
583 leading_colon: None,
584 segments: Default::default(),
585 },
586 colon2_token: None,
587 star_token: star,
588 }
589 }
590 }
Michael Layzell92639a52017-06-01 00:07:44 -0400591 })
592 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700593 }
David Tolnay4a057422016-10-08 00:02:31 -0700594
Alex Crichton954046c2017-05-30 21:49:42 -0700595 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400596 named!(parse -> Self, alt!(
597 do_parse!(
598 path: syn!(Path) >>
599 colon2: syn!(Colon2) >>
600 items: braces!(call!(Delimited::parse_terminated)) >>
601 (PathList {
602 path: path,
603 items: items.0,
604 brace_token: items.1,
605 colon2_token: colon2,
606 })
607 )
608 |
609 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700610 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400611 items: braces!(call!(Delimited::parse_terminated)) >>
612 (PathList {
613 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400614 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700615 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400616 },
David Tolnay570695e2017-06-03 16:15:13 -0700617 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400618 brace_token: items.1,
619 items: items.0,
620 })
621 )
622 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700623 }
David Tolnay4a057422016-10-08 00:02:31 -0700624
Alex Crichton954046c2017-05-30 21:49:42 -0700625 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400626 named!(parse -> Self, do_parse!(
627 name: alt!(
628 syn!(Ident)
629 |
630 map!(syn!(Self_), Into::into)
631 ) >>
632 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
633 (PathListItem {
634 name: name,
635 as_token: rename.as_ref().map(|p| As((p.0).0)),
636 rename: rename.map(|p| p.1),
637 })
638 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700639 }
David Tolnay4a057422016-10-08 00:02:31 -0700640
David Tolnay4c614be2017-11-10 00:02:38 -0800641 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700642 attrs: many0!(call!(Attribute::parse_outer)) >>
643 vis: syn!(Visibility) >>
644 static_: syn!(Static) >>
645 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700646 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700647 colon: syn!(Colon) >>
648 ty: syn!(Ty) >>
649 eq: syn!(Eq) >>
650 value: syn!(Expr) >>
651 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800652 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700653 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800654 vis: vis,
655 static_token: static_,
656 mutbl: mutability,
657 ident: ident,
658 colon_token: colon,
659 ty: Box::new(ty),
660 eq_token: eq,
661 expr: Box::new(value),
662 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800663 })
David Tolnay47a877c2016-10-01 16:50:55 -0700664 ));
665
David Tolnay4c614be2017-11-10 00:02:38 -0800666 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700667 attrs: many0!(call!(Attribute::parse_outer)) >>
668 vis: syn!(Visibility) >>
669 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700670 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700671 colon: syn!(Colon) >>
672 ty: syn!(Ty) >>
673 eq: syn!(Eq) >>
674 value: syn!(Expr) >>
675 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800676 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700677 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800678 vis: vis,
679 const_token: const_,
680 ident: ident,
681 colon_token: colon,
682 ty: Box::new(ty),
683 eq_token: eq,
684 expr: Box::new(value),
685 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800686 })
David Tolnay47a877c2016-10-01 16:50:55 -0700687 ));
688
David Tolnay4c614be2017-11-10 00:02:38 -0800689 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700690 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
691 vis: syn!(Visibility) >>
692 constness: syn!(Constness) >>
693 unsafety: syn!(Unsafety) >>
694 abi: option!(syn!(Abi)) >>
695 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700696 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700697 generics: syn!(Generics) >>
698 inputs: parens!(Delimited::parse_terminated) >>
699 ret: syn!(FunctionRetTy) >>
700 where_clause: syn!(WhereClause) >>
701 inner_attrs_stmts: braces!(tuple!(
702 many0!(call!(Attribute::parse_inner)),
703 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400704 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800705 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700706 attrs: {
707 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700708 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700709 attrs
710 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800711 vis: vis,
712 constness: constness,
713 unsafety: unsafety,
714 abi: abi,
715 decl: Box::new(FnDecl {
716 dot_tokens: None,
717 fn_token: fn_,
718 paren_token: inputs.1,
719 inputs: inputs.0,
720 output: ret,
721 variadic: false,
722 generics: Generics {
723 where_clause: where_clause,
724 .. generics
725 },
726 }),
727 ident: ident,
728 block: Box::new(Block {
729 brace_token: inner_attrs_stmts.1,
730 stmts: (inner_attrs_stmts.0).1,
731 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800732 })
David Tolnay42602292016-10-01 22:25:45 -0700733 ));
734
Alex Crichton954046c2017-05-30 21:49:42 -0700735 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400736 named!(parse -> Self, alt!(
737 do_parse!(
738 and: syn!(And) >>
739 lt: option!(syn!(Lifetime)) >>
740 mutability: syn!(Mutability) >>
741 self_: syn!(Self_) >>
742 not!(syn!(Colon)) >>
743 (ArgSelfRef {
744 lifetime: lt,
745 mutbl: mutability,
746 and_token: and,
747 self_token: self_,
748 }.into())
749 )
750 |
751 do_parse!(
752 mutability: syn!(Mutability) >>
753 self_: syn!(Self_) >>
754 not!(syn!(Colon)) >>
755 (ArgSelf {
756 mutbl: mutability,
757 self_token: self_,
758 }.into())
759 )
760 |
761 do_parse!(
762 pat: syn!(Pat) >>
763 colon: syn!(Colon) >>
764 ty: syn!(Ty) >>
765 (ArgCaptured {
766 pat: pat,
767 ty: ty,
768 colon_token: colon,
769 }.into())
770 )
771 |
772 syn!(Ty) => { FnArg::Ignored }
773 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700774 }
David Tolnay62f374c2016-10-02 13:37:00 -0700775
David Tolnay4c614be2017-11-10 00:02:38 -0800776 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700777 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
778 vis: syn!(Visibility) >>
779 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700780 ident: syn!(Ident) >>
781 content_semi: alt!(
782 syn!(Semi) => {|semi| (
783 Vec::new(),
784 None,
785 Some(semi),
786 )}
David Tolnay37d10332016-10-13 20:51:04 -0700787 |
Alex Crichton954046c2017-05-30 21:49:42 -0700788 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700789 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700790 many0!(call!(Attribute::parse_inner)),
791 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400792 )
David Tolnay570695e2017-06-03 16:15:13 -0700793 ) => {|((inner_attrs, items), brace)| (
794 inner_attrs,
795 Some((brace, items)),
796 None,
797 )}
David Tolnay37d10332016-10-13 20:51:04 -0700798 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800799 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700800 attrs: {
801 let mut attrs = outer_attrs;
802 attrs.extend(content_semi.0);
803 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700804 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800805 vis: vis,
806 mod_token: mod_,
807 ident: ident,
808 content: content_semi.1,
809 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800810 })
David Tolnay35902302016-10-06 01:11:08 -0700811 ));
812
David Tolnay4c614be2017-11-10 00:02:38 -0800813 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700814 attrs: many0!(call!(Attribute::parse_outer)) >>
815 abi: syn!(Abi) >>
816 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800817 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700818 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800819 abi: abi,
820 brace_token: items.1,
821 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800822 })
David Tolnay35902302016-10-06 01:11:08 -0700823 ));
824
Alex Crichton954046c2017-05-30 21:49:42 -0700825 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400826 named!(parse -> Self, alt!(
827 foreign_fn
828 |
829 foreign_static
830 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700831 }
David Tolnay35902302016-10-06 01:11:08 -0700832
833 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700834 attrs: many0!(call!(Attribute::parse_outer)) >>
835 vis: syn!(Visibility) >>
836 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700837 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700838 generics: syn!(Generics) >>
839 inputs: parens!(do_parse!(
840 args: call!(Delimited::parse_terminated) >>
841 variadic: cond!(args.is_empty() || args.trailing_delim(),
842 option!(syn!(Dot3))) >>
843 (args, variadic)
844 )) >>
845 ret: syn!(FunctionRetTy) >>
846 where_clause: syn!(WhereClause) >>
847 semi: syn!(Semi) >>
848 ({
849 let ((inputs, variadic), parens) = inputs;
850 let variadic = variadic.and_then(|v| v);
851 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700852 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700853 attrs: attrs,
854 semi_token: semi,
855 node: ForeignItemFn {
856 decl: Box::new(FnDecl {
857 fn_token: fn_,
858 paren_token: parens,
859 inputs: inputs,
860 variadic: variadic.is_some(),
861 dot_tokens: variadic,
862 output: ret,
863 generics: Generics {
864 where_clause: where_clause,
865 .. generics
866 },
867 }),
868 }.into(),
869 vis: vis,
870 }
David Tolnay35902302016-10-06 01:11:08 -0700871 })
872 ));
873
874 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700875 attrs: many0!(call!(Attribute::parse_outer)) >>
876 vis: syn!(Visibility) >>
877 static_: syn!(Static) >>
878 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700879 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700880 colon: syn!(Colon) >>
881 ty: syn!(Ty) >>
882 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700883 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700884 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700885 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700886 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700887 node: ForeignItemStatic {
888 ty: Box::new(ty),
889 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700890 static_token: static_,
891 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700893 vis: vis,
894 })
895 ));
896
David Tolnay4c614be2017-11-10 00:02:38 -0800897 impl_synom!(ItemTy "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700898 attrs: many0!(call!(Attribute::parse_outer)) >>
899 vis: syn!(Visibility) >>
900 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700901 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700902 generics: syn!(Generics) >>
903 where_clause: syn!(WhereClause) >>
904 eq: syn!(Eq) >>
905 ty: syn!(Ty) >>
906 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800907 (ItemTy {
David Tolnay3cf52982016-10-01 17:11:37 -0700908 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800909 vis: vis,
910 type_token: type_,
911 ident: ident,
912 generics: Generics {
913 where_clause: where_clause,
914 ..generics
915 },
916 eq_token: eq,
917 ty: Box::new(ty),
918 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800919 })
David Tolnay3cf52982016-10-01 17:11:37 -0700920 ));
921
David Tolnay4c614be2017-11-10 00:02:38 -0800922 impl_synom!(ItemStruct "struct item" switch!(
923 map!(syn!(DeriveInput), Into::into),
924 Item::Struct(item) => value!(item)
925 |
926 _ => reject!()
927 ));
David Tolnay42602292016-10-01 22:25:45 -0700928
David Tolnay4c614be2017-11-10 00:02:38 -0800929 impl_synom!(ItemEnum "enum item" switch!(
930 map!(syn!(DeriveInput), Into::into),
931 Item::Enum(item) => value!(item)
932 |
933 _ => reject!()
934 ));
935
936 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700937 attrs: many0!(call!(Attribute::parse_outer)) >>
938 vis: syn!(Visibility) >>
939 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700940 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700941 generics: syn!(Generics) >>
942 where_clause: syn!(WhereClause) >>
943 fields: braces!(call!(Delimited::parse_terminated_with,
944 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800945 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700946 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800947 vis: vis,
948 union_token: union_,
949 ident: ident,
950 generics: Generics {
951 where_clause: where_clause,
952 .. generics
953 },
954 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800955 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700956 ));
957
David Tolnay4c614be2017-11-10 00:02:38 -0800958 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700959 attrs: many0!(call!(Attribute::parse_outer)) >>
960 vis: syn!(Visibility) >>
961 unsafety: syn!(Unsafety) >>
962 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700963 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700964 generics: syn!(Generics) >>
965 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700967 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700968 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700969 where_clause: syn!(WhereClause) >>
970 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800971 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700972 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800973 vis: vis,
974 unsafety: unsafety,
975 trait_token: trait_,
976 ident: ident,
977 generics: Generics {
978 where_clause: where_clause,
979 .. generics
980 },
981 colon_token: colon,
982 supertraits: bounds.unwrap_or_default(),
983 brace_token: body.1,
984 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800985 })
David Tolnay0aecb732016-10-03 23:03:50 -0700986 ));
987
David Tolnay4c614be2017-11-10 00:02:38 -0800988 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700989 attrs: many0!(call!(Attribute::parse_outer)) >>
990 unsafety: syn!(Unsafety) >>
991 impl_: syn!(Impl) >>
992 path: syn!(Path) >>
993 for_: syn!(For) >>
994 dot2: syn!(Dot2) >>
995 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800996 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700997 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800998 unsafety: unsafety,
999 impl_token: impl_,
1000 path: path,
1001 for_token: for_,
1002 dot2_token: dot2,
1003 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001004 })
David Tolnayf94e2362016-10-04 00:29:51 -07001005 ));
1006
Alex Crichton954046c2017-05-30 21:49:42 -07001007 impl Synom for TraitItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001008 named!(parse -> Self, alt!(
1009 trait_item_const
1010 |
1011 trait_item_method
1012 |
1013 trait_item_type
1014 |
1015 trait_item_mac
1016 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001017 }
David Tolnay0aecb732016-10-03 23:03:50 -07001018
1019 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001020 attrs: many0!(call!(Attribute::parse_outer)) >>
1021 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001022 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001023 colon: syn!(Colon) >>
1024 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001025 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001026 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001027 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001028 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001029 node: TraitItemConst {
Alex Crichton954046c2017-05-30 21:49:42 -07001030 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001031 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001032 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001033 ty: ty,
1034 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001035 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001036 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001037 })
1038 ));
1039
1040 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001041 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1042 constness: syn!(Constness) >>
1043 unsafety: syn!(Unsafety) >>
1044 abi: option!(syn!(Abi)) >>
1045 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001046 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001047 generics: syn!(Generics) >>
1048 inputs: parens!(call!(Delimited::parse_terminated)) >>
1049 ret: syn!(FunctionRetTy) >>
1050 where_clause: syn!(WhereClause) >>
1051 body: option!(braces!(
1052 tuple!(many0!(call!(Attribute::parse_inner)),
1053 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001054 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001055 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001056 ({
1057 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001058 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001059 None => (Vec::new(), None),
1060 };
1061 TraitItem {
David Tolnay5859df12016-10-29 22:49:54 -07001062 attrs: {
1063 let mut attrs = outer_attrs;
1064 attrs.extend(inner_attrs);
1065 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001066 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001067 node: TraitItemMethod {
1068 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001069 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001070 unsafety: unsafety,
David Tolnay5859df12016-10-29 22:49:54 -07001071 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001072 ident: ident,
David Tolnay5859df12016-10-29 22:49:54 -07001073 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001074 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001075 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001076 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001077 fn_token: fn_,
1078 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001079 dot_tokens: None,
1080 generics: Generics {
1081 where_clause: where_clause,
1082 .. generics
1083 },
David Tolnay5859df12016-10-29 22:49:54 -07001084 },
1085 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001086 default: stmts.map(|stmts| {
1087 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001088 stmts: stmts.0,
1089 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 }
1091 }),
David Tolnay570695e2017-06-03 16:15:13 -07001092 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001093 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001094 }
David Tolnay0aecb732016-10-03 23:03:50 -07001095 })
1096 ));
1097
1098 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001099 attrs: many0!(call!(Attribute::parse_outer)) >>
1100 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001101 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001102 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001103 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001104 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001105 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001106 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1107 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001108 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001109 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001110 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001111 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001112 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001113 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001114 bounds: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -07001115 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001116 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001117 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001118 })
1119 ));
1120
1121 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001122 attrs: many0!(call!(Attribute::parse_outer)) >>
1123 mac: syn!(Mac) >>
1124 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001125 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001126 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001127 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001128 })
1129 ));
1130
David Tolnay4c614be2017-11-10 00:02:38 -08001131 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001132 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001133 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001134 unsafety: syn!(Unsafety) >>
1135 impl_: syn!(Impl) >>
1136 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001137 polarity_path: alt!(
1138 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001139 polarity: syn!(ImplPolarity) >>
1140 path: syn!(Path) >>
1141 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001142 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001143 )
1144 |
David Tolnay570695e2017-06-03 16:15:13 -07001145 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001146 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001147 self_ty: syn!(Ty) >>
1148 where_clause: syn!(WhereClause) >>
1149 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001150 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001151 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001152 defaultness: defaultness,
1153 unsafety: unsafety,
1154 impl_token: impl_,
1155 generics: Generics {
1156 where_clause: where_clause,
1157 .. generics
1158 },
1159 trait_: polarity_path,
1160 self_ty: Box::new(self_ty),
1161 brace_token: body.1,
1162 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001163 })
David Tolnay4c9be372016-10-06 00:47:37 -07001164 ));
1165
Alex Crichton954046c2017-05-30 21:49:42 -07001166 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001167 named!(parse -> Self, alt!(
1168 impl_item_const
1169 |
1170 impl_item_method
1171 |
1172 impl_item_type
1173 |
David Tolnay570695e2017-06-03 16:15:13 -07001174 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001175 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001176 }
David Tolnay4c9be372016-10-06 00:47:37 -07001177
1178 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001179 attrs: many0!(call!(Attribute::parse_outer)) >>
1180 vis: syn!(Visibility) >>
1181 defaultness: syn!(Defaultness) >>
1182 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001183 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001184 colon: syn!(Colon) >>
1185 ty: syn!(Ty) >>
1186 eq: syn!(Eq) >>
1187 value: syn!(Expr) >>
1188 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001189 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001190 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001191 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001192 vis: vis,
1193 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001194 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001195 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001196 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001197 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001198 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001199 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001200 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001201 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001202 })
1203 ));
1204
1205 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001206 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1207 vis: syn!(Visibility) >>
1208 defaultness: syn!(Defaultness) >>
1209 constness: syn!(Constness) >>
1210 unsafety: syn!(Unsafety) >>
1211 abi: option!(syn!(Abi)) >>
1212 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001213 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001214 generics: syn!(Generics) >>
1215 inputs: parens!(call!(Delimited::parse_terminated)) >>
1216 ret: syn!(FunctionRetTy) >>
1217 where_clause: syn!(WhereClause) >>
1218 inner_attrs_stmts: braces!(tuple!(
1219 many0!(call!(Attribute::parse_inner)),
1220 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001221 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001222 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001223 attrs: {
1224 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001225 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001226 attrs
1227 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001228 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001229 vis: vis,
1230 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001231 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001232 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001233 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001234 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001235 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001236 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001237 fn_token: fn_,
1238 paren_token: inputs.1,
1239 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001240 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001241 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001242 generics: Generics {
1243 where_clause: where_clause,
1244 .. generics
1245 },
1246 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001247 },
1248 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001249 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001250 brace_token: inner_attrs_stmts.1,
1251 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001252 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001253 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001254 })
1255 ));
1256
1257 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001258 attrs: many0!(call!(Attribute::parse_outer)) >>
1259 vis: syn!(Visibility) >>
1260 defaultness: syn!(Defaultness) >>
1261 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001262 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001263 eq: syn!(Eq) >>
1264 ty: syn!(Ty) >>
1265 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001266 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001267 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001268 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001269 vis: vis,
1270 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001271 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001272 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001273 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001274 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001275 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001276 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001277 })
1278 ));
1279
David Tolnay570695e2017-06-03 16:15:13 -07001280 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001281 attrs: many0!(call!(Attribute::parse_outer)) >>
1282 mac: syn!(Mac) >>
1283 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001284 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001285 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001286 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001287 })
1288 ));
1289
Alex Crichton954046c2017-05-30 21:49:42 -07001290 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001291 named!(parse -> Self, alt!(
1292 syn!(Bang) => { ImplPolarity::Negative }
1293 |
1294 epsilon!() => { |_| ImplPolarity::Positive }
1295 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001296 }
David Tolnay4c9be372016-10-06 00:47:37 -07001297
Alex Crichton954046c2017-05-30 21:49:42 -07001298 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001299 named!(parse -> Self, alt!(
1300 syn!(Const) => { Constness::Const }
1301 |
1302 epsilon!() => { |_| Constness::NotConst }
1303 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001304 }
David Tolnay42602292016-10-01 22:25:45 -07001305
Alex Crichton954046c2017-05-30 21:49:42 -07001306 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001307 named!(parse -> Self, alt!(
1308 syn!(Default_) => { Defaultness::Default }
1309 |
1310 epsilon!() => { |_| Defaultness::Final }
1311 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001312 }
David Tolnayedf2b992016-09-23 20:43:45 -07001313}
David Tolnay4a51dc72016-10-01 00:40:31 -07001314
1315#[cfg(feature = "printing")]
1316mod printing {
1317 use super::*;
1318 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001319 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001320 use quote::{Tokens, ToTokens};
1321
1322 impl ToTokens for Item {
1323 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001324 match *self {
1325 Item::ExternCrate(ref item) => {
1326 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001327 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001328 item.extern_token.to_tokens(tokens);
1329 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001330 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001331 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001332 as_token.to_tokens(tokens);
1333 rename.to_tokens(tokens);
1334 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001335 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001336 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001337 Item::Use(ref item) => {
1338 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001339 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001340 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001341 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001342 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001343 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001344 Item::Static(ref item) => {
1345 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001346 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001347 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001348 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001349 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001350 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001351 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001352 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001353 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001354 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001355 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001356 Item::Const(ref item) => {
1357 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001358 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001359 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001360 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001361 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001362 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001363 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001364 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001366 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001367 Item::Fn(ref item) => {
1368 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001369 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001370 item.constness.to_tokens(tokens);
1371 item.unsafety.to_tokens(tokens);
1372 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001373 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.block.brace_token.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001375 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001376 tokens.append_all(&item.block.stmts);
1377 });
David Tolnay42602292016-10-01 22:25:45 -07001378 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001379 Item::Mod(ref item) => {
1380 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001381 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001382 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001383 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001384 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001385 brace.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001386 tokens.append_all(item.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001387 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001388 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001389 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001390 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001391 }
David Tolnay35902302016-10-06 01:11:08 -07001392 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001393 Item::ForeignMod(ref item) => {
1394 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 item.abi.to_tokens(tokens);
1396 item.brace_token.surround(tokens, |tokens| {
1397 tokens.append_all(&item.items);
1398 });
David Tolnay35902302016-10-06 01:11:08 -07001399 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001400 Item::Ty(ref item) => {
1401 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001402 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001403 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001404 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001405 item.generics.to_tokens(tokens);
1406 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001407 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001409 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001410 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001411 Item::Enum(ref item) => {
1412 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001413 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001415 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001416 item.generics.to_tokens(tokens);
1417 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001418 item.brace_token.surround(tokens, |tokens| {
1419 item.variants.to_tokens(tokens);
1420 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001421 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001422 Item::Struct(ref item) => {
1423 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001424 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001425 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001426 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001427 item.generics.to_tokens(tokens);
1428 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001429 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001430 item.generics.where_clause.to_tokens(tokens);
1431 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001432 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001433 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001434 item.data.to_tokens(tokens);
1435 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001436 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001437 }
1438 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001439 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001440 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001441 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001442 }
1443 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001444 Item::Union(ref item) => {
1445 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001446 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001447 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001448 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001449 item.generics.to_tokens(tokens);
1450 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001451 // XXX: Should we handle / complain when using a
1452 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001453 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001454 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001455 Item::Trait(ref item) => {
1456 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001457 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001458 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001459 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001460 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001461 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001462 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001463 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001464 item.supertraits.to_tokens(tokens);
1465 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001466 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001467 item.brace_token.surround(tokens, |tokens| {
1468 tokens.append_all(&item.items);
1469 });
David Tolnayca085422016-10-04 00:12:38 -07001470 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001471 Item::DefaultImpl(ref item) => {
1472 tokens.append_all(item.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001473 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001474 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001476 item.for_token.to_tokens(tokens);
1477 item.dot2_token.to_tokens(tokens);
1478 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001479 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001480 Item::Impl(ref item) => {
1481 tokens.append_all(item.attrs.outer());
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001482 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001484 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001485 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001486 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001487 polarity.to_tokens(tokens);
1488 path.to_tokens(tokens);
1489 for_token.to_tokens(tokens);
1490 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001491 item.self_ty.to_tokens(tokens);
1492 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001493 item.brace_token.surround(tokens, |tokens| {
1494 tokens.append_all(&item.items);
1495 });
David Tolnay4c9be372016-10-06 00:47:37 -07001496 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001497 Item::Mac(ref item) => {
1498 tokens.append_all(item.attrs.outer());
1499 item.mac.path.to_tokens(tokens);
1500 item.mac.bang_token.to_tokens(tokens);
1501 item.mac.ident.to_tokens(tokens);
1502 tokens.append_all(&item.mac.tokens);
1503 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001504 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001505 }
1506 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001507 }
1508 }
1509 }
David Tolnay42602292016-10-01 22:25:45 -07001510
Alex Crichton62a0a592017-05-22 13:58:53 -07001511 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001512 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001513 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001514 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001515 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001516 self.rename.to_tokens(tokens);
1517 }
David Tolnay4a057422016-10-08 00:02:31 -07001518 }
1519 }
1520
Alex Crichton62a0a592017-05-22 13:58:53 -07001521 impl ToTokens for PathGlob {
1522 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001523 if self.path.segments.len() > 0 {
1524 self.path.to_tokens(tokens);
1525 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1526 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001527 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001528 }
1529 }
1530
1531 impl ToTokens for PathList {
1532 fn to_tokens(&self, tokens: &mut Tokens) {
1533 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001534 self.colon2_token.to_tokens(tokens);
1535 self.brace_token.surround(tokens, |tokens| {
1536 self.items.to_tokens(tokens);
1537 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001538 }
1539 }
1540
David Tolnay4a057422016-10-08 00:02:31 -07001541 impl ToTokens for PathListItem {
1542 fn to_tokens(&self, tokens: &mut Tokens) {
1543 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001544 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001545 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001546 self.rename.to_tokens(tokens);
1547 }
David Tolnay4a057422016-10-08 00:02:31 -07001548 }
1549 }
1550
David Tolnayca085422016-10-04 00:12:38 -07001551 impl ToTokens for TraitItem {
1552 fn to_tokens(&self, tokens: &mut Tokens) {
1553 tokens.append_all(self.attrs.outer());
1554 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001555 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001556 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001557 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001558 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001559 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001560 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001561 eq_token.to_tokens(tokens);
1562 default.to_tokens(tokens);
1563 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001564 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001565 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001566 TraitItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001567 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001568 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001569 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001570 block.brace_token.surround(tokens, |tokens| {
1571 tokens.append_all(self.attrs.inner());
1572 tokens.append_all(&block.stmts);
1573 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001574 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001575 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001576 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001577 }
David Tolnayca085422016-10-04 00:12:38 -07001578 }
1579 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001580 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001581 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001582 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001583 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001584 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001585 item.bounds.to_tokens(tokens);
1586 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001587 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001588 eq_token.to_tokens(tokens);
1589 default.to_tokens(tokens);
1590 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001591 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001592 }
1593 TraitItemKind::Macro(ref mac) => {
1594 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001595 if !mac.is_braced() {
1596 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001597 }
David Tolnayca085422016-10-04 00:12:38 -07001598 }
1599 }
1600 }
1601 }
1602
David Tolnay4c9be372016-10-06 00:47:37 -07001603 impl ToTokens for ImplItem {
1604 fn to_tokens(&self, tokens: &mut Tokens) {
1605 tokens.append_all(self.attrs.outer());
1606 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001608 item.vis.to_tokens(tokens);
1609 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001610 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001611 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001612 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001613 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001614 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001615 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001616 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001617 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001618 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001619 item.vis.to_tokens(tokens);
1620 item.defaultness.to_tokens(tokens);
1621 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001622 item.block.brace_token.surround(tokens, |tokens| {
1623 tokens.append_all(self.attrs.inner());
1624 tokens.append_all(&item.block.stmts);
1625 });
David Tolnay4c9be372016-10-06 00:47:37 -07001626 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001627 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001628 item.vis.to_tokens(tokens);
1629 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001630 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001631 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001632 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001633 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001634 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001635 }
1636 ImplItemKind::Macro(ref mac) => {
1637 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001638 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001639 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001640 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001641 }
1642 }
1643 }
1644 }
1645 }
1646
David Tolnay35902302016-10-06 01:11:08 -07001647 impl ToTokens for ForeignItem {
1648 fn to_tokens(&self, tokens: &mut Tokens) {
1649 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001650 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001651 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001652 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001653 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001654 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001655 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001656 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001657 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001658 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001659 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001660 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001661 }
1662 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001663 self.semi_token.to_tokens(tokens);
1664 }
1665 }
1666
David Tolnay570695e2017-06-03 16:15:13 -07001667 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001668 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001669 self.constness.to_tokens(tokens);
1670 self.unsafety.to_tokens(tokens);
1671 self.abi.to_tokens(tokens);
1672 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001673 }
1674 }
1675
David Tolnay570695e2017-06-03 16:15:13 -07001676 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001677
1678 impl<'a> ToTokens for NamedDecl<'a> {
1679 fn to_tokens(&self, tokens: &mut Tokens) {
1680 self.0.fn_token.to_tokens(tokens);
1681 self.1.to_tokens(tokens);
1682 self.0.generics.to_tokens(tokens);
1683 self.0.paren_token.surround(tokens, |tokens| {
1684 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001685
1686 if self.0.variadic {
1687 if !self.0.inputs.empty_or_trailing() {
1688 tokens::Comma::default().to_tokens(tokens);
1689 }
Alex Crichton259ee532017-07-14 06:51:02 -07001690 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001691 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001692 });
1693 self.0.output.to_tokens(tokens);
1694 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001695 }
1696 }
1697
Alex Crichton62a0a592017-05-22 13:58:53 -07001698 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001699 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001700 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001701 self.lifetime.to_tokens(tokens);
1702 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001703 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001704 }
1705 }
1706
1707 impl ToTokens for ArgSelf {
1708 fn to_tokens(&self, tokens: &mut Tokens) {
1709 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001710 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001711 }
1712 }
1713
1714 impl ToTokens for ArgCaptured {
1715 fn to_tokens(&self, tokens: &mut Tokens) {
1716 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001717 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001718 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001719 }
1720 }
1721
David Tolnay42602292016-10-01 22:25:45 -07001722 impl ToTokens for Constness {
1723 fn to_tokens(&self, tokens: &mut Tokens) {
1724 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001726 Constness::NotConst => {
1727 // nothing
1728 }
David Tolnay42602292016-10-01 22:25:45 -07001729 }
1730 }
1731 }
1732
David Tolnay4c9be372016-10-06 00:47:37 -07001733 impl ToTokens for Defaultness {
1734 fn to_tokens(&self, tokens: &mut Tokens) {
1735 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001736 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001737 Defaultness::Final => {
1738 // nothing
1739 }
1740 }
1741 }
1742 }
1743
1744 impl ToTokens for ImplPolarity {
1745 fn to_tokens(&self, tokens: &mut Tokens) {
1746 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001748 ImplPolarity::Positive => {
1749 // nothing
1750 }
1751 }
1752 }
1753 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001754}