blob: 0416b9554d21e8661c29331054963f142b4013be [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 Tolnaydecf28d2017-11-11 11:56:45 -0800185 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800186 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800187 pub mac: Macro,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800188 }),
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
Alex Crichton62a0a592017-05-22 13:58:53 -0700278ast_enum_of_structs! {
279 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800280 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 /// A foreign function
282 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800283 pub attrs: Vec<Attribute>,
284 pub vis: Visibility,
285 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub decl: Box<FnDecl>,
David Tolnay8894f602017-11-11 12:11:04 -0800287 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
289 /// A foreign static item (`static ext: u8`)
290 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800291 pub attrs: Vec<Attribute>,
292 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700293 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800295 pub ident: Ident,
296 pub colon_token: tokens::Colon,
297 pub ty: Box<Ty>,
298 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 }),
300 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700301}
302
David Tolnayda705bd2017-11-10 21:58:05 -0800303ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 /// Represents an item declaration within a trait declaration,
305 /// possibly including a default implementation. A trait item is
306 /// either required (meaning it doesn't have an implementation, just a
307 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800308 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800310 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700311 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700312 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700313 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700315 pub default: Option<(tokens::Eq, Expr)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700316 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 }),
318 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800319 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 pub sig: MethodSig,
321 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700322 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
324 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800325 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700326 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700327 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700328 pub colon_token: Option<tokens::Colon>,
329 pub bounds: Delimited<TyParamBound, tokens::Add>,
David Tolnay570695e2017-06-03 16:15:13 -0700330 pub default: Option<(tokens::Eq, Ty)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700331 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800333 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800334 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800335 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800336 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700337 }
338
339 do_not_generate_to_tokens
340}
341
342ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700343 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700344 pub enum ImplPolarity {
345 /// `impl Trait for Type`
346 Positive,
347 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700348 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 }
350}
351
352ast_struct! {
353 pub struct ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 pub attrs: Vec<Attribute>,
355 pub node: ImplItemKind,
356 }
357}
358
359ast_enum_of_structs! {
360 pub enum ImplItemKind {
361 pub Const(ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700362 pub vis: Visibility,
363 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700364 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700365 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700366 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700367 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700368 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700370 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }),
372 pub Method(ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -0700373 pub vis: Visibility,
374 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700375 pub sig: MethodSig,
376 pub block: Block,
377 }),
378 pub Type(ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -0700379 pub vis: Visibility,
380 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700381 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700382 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700383 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700385 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800387 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 }
389
390 do_not_generate_to_tokens
391}
392
393ast_struct! {
394 /// Represents a method's signature in a trait declaration,
395 /// or in an implementation.
396 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700398 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700400 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 }
403}
404
405ast_struct! {
406 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700407 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700408 /// E.g. `fn foo(bar: baz)`
409 pub struct FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -0700410 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700411 pub paren_token: tokens::Paren,
412 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700414 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700416 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700418}
419
Alex Crichton62a0a592017-05-22 13:58:53 -0700420ast_enum_of_structs! {
421 /// An argument in a function header.
422 ///
423 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
424 pub enum FnArg {
425 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700426 pub and_token: tokens::And,
427 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700428 pub lifetime: Option<Lifetime>,
429 pub mutbl: Mutability,
430 }),
431 pub SelfValue(ArgSelf {
432 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700433 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }),
435 pub Captured(ArgCaptured {
436 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700437 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 pub ty: Ty,
439 }),
440 pub Ignored(Ty),
441 }
David Tolnay62f374c2016-10-02 13:37:00 -0700442}
443
David Tolnayedf2b992016-09-23 20:43:45 -0700444#[cfg(feature = "parsing")]
445pub mod parsing {
446 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700447
Michael Layzell92639a52017-06-01 00:07:44 -0400448 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700449 use synom::tokens::*;
450 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700451
David Tolnay4c614be2017-11-10 00:02:38 -0800452 impl_synom!(Item "item" alt!(
453 syn!(ItemExternCrate) => { Item::ExternCrate }
454 |
455 syn!(ItemUse) => { Item::Use }
456 |
457 syn!(ItemStatic) => { Item::Static }
458 |
459 syn!(ItemConst) => { Item::Const }
460 |
461 syn!(ItemFn) => { Item::Fn }
462 |
463 syn!(ItemMod) => { Item::Mod }
464 |
465 syn!(ItemForeignMod) => { Item::ForeignMod }
466 |
467 syn!(ItemTy) => { Item::Ty }
468 |
469 syn!(ItemStruct) => { Item::Struct }
470 |
471 syn!(ItemEnum) => { Item::Enum }
472 |
473 syn!(ItemUnion) => { Item::Union }
474 |
475 syn!(ItemTrait) => { Item::Trait }
476 |
477 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
478 |
479 syn!(ItemImpl) => { Item::Impl }
480 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800481 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800482 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700483
David Tolnaydecf28d2017-11-11 11:56:45 -0800484 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700485 attrs: many0!(call!(Attribute::parse_outer)) >>
486 what: syn!(Path) >>
487 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700488 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700489 body: call!(::TokenTree::parse_delimited) >>
490 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800491 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700492 attrs: attrs,
David Tolnaydecf28d2017-11-11 11:56:45 -0800493 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500494 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700495 bang_token: bang,
496 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700497 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800498 },
David Tolnay4c614be2017-11-10 00:02:38 -0800499 })
David Tolnayedf2b992016-09-23 20:43:45 -0700500 ));
501
David Tolnay4c614be2017-11-10 00:02:38 -0800502 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700503 attrs: many0!(call!(Attribute::parse_outer)) >>
504 vis: syn!(Visibility) >>
505 extern_: syn!(Extern) >>
506 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700507 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700508 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
509 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800510 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700511 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800512 vis: vis,
513 extern_token: extern_,
514 crate_token: crate_,
515 ident: ident,
516 rename: rename,
517 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800518 })
David Tolnayedf2b992016-09-23 20:43:45 -0700519 ));
520
David Tolnay4c614be2017-11-10 00:02:38 -0800521 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700522 attrs: many0!(call!(Attribute::parse_outer)) >>
523 vis: syn!(Visibility) >>
524 use_: syn!(Use) >>
525 what: syn!(ViewPath) >>
526 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800527 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700528 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800529 vis: vis,
530 use_token: use_,
531 path: Box::new(what),
532 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800533 })
David Tolnay4a057422016-10-08 00:02:31 -0700534 ));
535
Alex Crichton954046c2017-05-30 21:49:42 -0700536 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400537 named!(parse -> Self, alt!(
538 syn!(PathGlob) => { ViewPath::Glob }
539 |
540 syn!(PathList) => { ViewPath::List }
541 |
542 syn!(PathSimple) => { ViewPath::Simple } // must be last
543 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700544 }
David Tolnay4a057422016-10-08 00:02:31 -0700545
Alex Crichton954046c2017-05-30 21:49:42 -0700546 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400547 named!(parse -> Self, do_parse!(
548 path: syn!(Path) >>
549 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
550 (PathSimple {
551 path: path,
552 as_token: rename.as_ref().map(|p| As((p.0).0)),
553 rename: rename.map(|p| p.1),
554 })
555 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700556 }
David Tolnay4a057422016-10-08 00:02:31 -0700557
Alex Crichton954046c2017-05-30 21:49:42 -0700558 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400559 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700560 path: option!(do_parse!(
561 path: syn!(Path) >>
562 colon2: syn!(Colon2) >>
563 (path, colon2)
564 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400565 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700566 ({
567 match path {
568 Some((path, colon2)) => {
569 PathGlob {
570 path: path,
571 colon2_token: Some(colon2),
572 star_token: star,
573 }
574 }
575 None => {
576 PathGlob {
577 path: Path {
578 leading_colon: None,
579 segments: Default::default(),
580 },
581 colon2_token: None,
582 star_token: star,
583 }
584 }
585 }
Michael Layzell92639a52017-06-01 00:07:44 -0400586 })
587 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700588 }
David Tolnay4a057422016-10-08 00:02:31 -0700589
Alex Crichton954046c2017-05-30 21:49:42 -0700590 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400591 named!(parse -> Self, alt!(
592 do_parse!(
593 path: syn!(Path) >>
594 colon2: syn!(Colon2) >>
595 items: braces!(call!(Delimited::parse_terminated)) >>
596 (PathList {
597 path: path,
598 items: items.0,
599 brace_token: items.1,
600 colon2_token: colon2,
601 })
602 )
603 |
604 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700605 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400606 items: braces!(call!(Delimited::parse_terminated)) >>
607 (PathList {
608 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400609 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700610 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400611 },
David Tolnay570695e2017-06-03 16:15:13 -0700612 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400613 brace_token: items.1,
614 items: items.0,
615 })
616 )
617 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700618 }
David Tolnay4a057422016-10-08 00:02:31 -0700619
Alex Crichton954046c2017-05-30 21:49:42 -0700620 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400621 named!(parse -> Self, do_parse!(
622 name: alt!(
623 syn!(Ident)
624 |
625 map!(syn!(Self_), Into::into)
626 ) >>
627 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
628 (PathListItem {
629 name: name,
630 as_token: rename.as_ref().map(|p| As((p.0).0)),
631 rename: rename.map(|p| p.1),
632 })
633 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700634 }
David Tolnay4a057422016-10-08 00:02:31 -0700635
David Tolnay4c614be2017-11-10 00:02:38 -0800636 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700637 attrs: many0!(call!(Attribute::parse_outer)) >>
638 vis: syn!(Visibility) >>
639 static_: syn!(Static) >>
640 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700641 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700642 colon: syn!(Colon) >>
643 ty: syn!(Ty) >>
644 eq: syn!(Eq) >>
645 value: syn!(Expr) >>
646 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800647 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700648 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800649 vis: vis,
650 static_token: static_,
651 mutbl: mutability,
652 ident: ident,
653 colon_token: colon,
654 ty: Box::new(ty),
655 eq_token: eq,
656 expr: Box::new(value),
657 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800658 })
David Tolnay47a877c2016-10-01 16:50:55 -0700659 ));
660
David Tolnay4c614be2017-11-10 00:02:38 -0800661 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700662 attrs: many0!(call!(Attribute::parse_outer)) >>
663 vis: syn!(Visibility) >>
664 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700665 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700666 colon: syn!(Colon) >>
667 ty: syn!(Ty) >>
668 eq: syn!(Eq) >>
669 value: syn!(Expr) >>
670 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800671 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700672 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800673 vis: vis,
674 const_token: const_,
675 ident: ident,
676 colon_token: colon,
677 ty: Box::new(ty),
678 eq_token: eq,
679 expr: Box::new(value),
680 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800681 })
David Tolnay47a877c2016-10-01 16:50:55 -0700682 ));
683
David Tolnay4c614be2017-11-10 00:02:38 -0800684 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700685 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
686 vis: syn!(Visibility) >>
687 constness: syn!(Constness) >>
688 unsafety: syn!(Unsafety) >>
689 abi: option!(syn!(Abi)) >>
690 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700691 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700692 generics: syn!(Generics) >>
693 inputs: parens!(Delimited::parse_terminated) >>
694 ret: syn!(FunctionRetTy) >>
695 where_clause: syn!(WhereClause) >>
696 inner_attrs_stmts: braces!(tuple!(
697 many0!(call!(Attribute::parse_inner)),
698 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400699 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800700 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700701 attrs: {
702 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700703 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700704 attrs
705 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800706 vis: vis,
707 constness: constness,
708 unsafety: unsafety,
709 abi: abi,
710 decl: Box::new(FnDecl {
711 dot_tokens: None,
712 fn_token: fn_,
713 paren_token: inputs.1,
714 inputs: inputs.0,
715 output: ret,
716 variadic: false,
717 generics: Generics {
718 where_clause: where_clause,
719 .. generics
720 },
721 }),
722 ident: ident,
723 block: Box::new(Block {
724 brace_token: inner_attrs_stmts.1,
725 stmts: (inner_attrs_stmts.0).1,
726 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800727 })
David Tolnay42602292016-10-01 22:25:45 -0700728 ));
729
Alex Crichton954046c2017-05-30 21:49:42 -0700730 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400731 named!(parse -> Self, alt!(
732 do_parse!(
733 and: syn!(And) >>
734 lt: option!(syn!(Lifetime)) >>
735 mutability: syn!(Mutability) >>
736 self_: syn!(Self_) >>
737 not!(syn!(Colon)) >>
738 (ArgSelfRef {
739 lifetime: lt,
740 mutbl: mutability,
741 and_token: and,
742 self_token: self_,
743 }.into())
744 )
745 |
746 do_parse!(
747 mutability: syn!(Mutability) >>
748 self_: syn!(Self_) >>
749 not!(syn!(Colon)) >>
750 (ArgSelf {
751 mutbl: mutability,
752 self_token: self_,
753 }.into())
754 )
755 |
756 do_parse!(
757 pat: syn!(Pat) >>
758 colon: syn!(Colon) >>
759 ty: syn!(Ty) >>
760 (ArgCaptured {
761 pat: pat,
762 ty: ty,
763 colon_token: colon,
764 }.into())
765 )
766 |
767 syn!(Ty) => { FnArg::Ignored }
768 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700769 }
David Tolnay62f374c2016-10-02 13:37:00 -0700770
David Tolnay4c614be2017-11-10 00:02:38 -0800771 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700772 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
773 vis: syn!(Visibility) >>
774 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700775 ident: syn!(Ident) >>
776 content_semi: alt!(
777 syn!(Semi) => {|semi| (
778 Vec::new(),
779 None,
780 Some(semi),
781 )}
David Tolnay37d10332016-10-13 20:51:04 -0700782 |
Alex Crichton954046c2017-05-30 21:49:42 -0700783 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700784 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700785 many0!(call!(Attribute::parse_inner)),
786 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400787 )
David Tolnay570695e2017-06-03 16:15:13 -0700788 ) => {|((inner_attrs, items), brace)| (
789 inner_attrs,
790 Some((brace, items)),
791 None,
792 )}
David Tolnay37d10332016-10-13 20:51:04 -0700793 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800794 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700795 attrs: {
796 let mut attrs = outer_attrs;
797 attrs.extend(content_semi.0);
798 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700799 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800800 vis: vis,
801 mod_token: mod_,
802 ident: ident,
803 content: content_semi.1,
804 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800805 })
David Tolnay35902302016-10-06 01:11:08 -0700806 ));
807
David Tolnay4c614be2017-11-10 00:02:38 -0800808 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700809 attrs: many0!(call!(Attribute::parse_outer)) >>
810 abi: syn!(Abi) >>
811 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800812 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700813 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800814 abi: abi,
815 brace_token: items.1,
816 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800817 })
David Tolnay35902302016-10-06 01:11:08 -0700818 ));
819
David Tolnay8894f602017-11-11 12:11:04 -0800820 impl_synom!(ForeignItem "foreign item" alt!(
821 syn!(ForeignItemFn) => { ForeignItem::Fn }
822 |
823 syn!(ForeignItemStatic) => { ForeignItem::Static }
824 ));
David Tolnay35902302016-10-06 01:11:08 -0700825
David Tolnay8894f602017-11-11 12:11:04 -0800826 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700827 attrs: many0!(call!(Attribute::parse_outer)) >>
828 vis: syn!(Visibility) >>
829 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700830 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700831 generics: syn!(Generics) >>
832 inputs: parens!(do_parse!(
833 args: call!(Delimited::parse_terminated) >>
834 variadic: cond!(args.is_empty() || args.trailing_delim(),
835 option!(syn!(Dot3))) >>
836 (args, variadic)
837 )) >>
838 ret: syn!(FunctionRetTy) >>
839 where_clause: syn!(WhereClause) >>
840 semi: syn!(Semi) >>
841 ({
842 let ((inputs, variadic), parens) = inputs;
843 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800844 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700845 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700846 attrs: attrs,
847 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800848 decl: Box::new(FnDecl {
849 fn_token: fn_,
850 paren_token: parens,
851 inputs: inputs,
852 variadic: variadic.is_some(),
853 dot_tokens: variadic,
854 output: ret,
855 generics: Generics {
856 where_clause: where_clause,
857 .. generics
858 },
859 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700860 vis: vis,
861 }
David Tolnay35902302016-10-06 01:11:08 -0700862 })
863 ));
864
David Tolnay8894f602017-11-11 12:11:04 -0800865 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700866 attrs: many0!(call!(Attribute::parse_outer)) >>
867 vis: syn!(Visibility) >>
868 static_: syn!(Static) >>
869 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700870 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700871 colon: syn!(Colon) >>
872 ty: syn!(Ty) >>
873 semi: syn!(Semi) >>
David Tolnay8894f602017-11-11 12:11:04 -0800874 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700875 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700876 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700877 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800878 ty: Box::new(ty),
879 mutbl: mutability,
880 static_token: static_,
881 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700882 vis: vis,
883 })
884 ));
885
David Tolnay4c614be2017-11-10 00:02:38 -0800886 impl_synom!(ItemTy "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700887 attrs: many0!(call!(Attribute::parse_outer)) >>
888 vis: syn!(Visibility) >>
889 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700890 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700891 generics: syn!(Generics) >>
892 where_clause: syn!(WhereClause) >>
893 eq: syn!(Eq) >>
894 ty: syn!(Ty) >>
895 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800896 (ItemTy {
David Tolnay3cf52982016-10-01 17:11:37 -0700897 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800898 vis: vis,
899 type_token: type_,
900 ident: ident,
901 generics: Generics {
902 where_clause: where_clause,
903 ..generics
904 },
905 eq_token: eq,
906 ty: Box::new(ty),
907 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800908 })
David Tolnay3cf52982016-10-01 17:11:37 -0700909 ));
910
David Tolnay4c614be2017-11-10 00:02:38 -0800911 impl_synom!(ItemStruct "struct item" switch!(
912 map!(syn!(DeriveInput), Into::into),
913 Item::Struct(item) => value!(item)
914 |
915 _ => reject!()
916 ));
David Tolnay42602292016-10-01 22:25:45 -0700917
David Tolnay4c614be2017-11-10 00:02:38 -0800918 impl_synom!(ItemEnum "enum item" switch!(
919 map!(syn!(DeriveInput), Into::into),
920 Item::Enum(item) => value!(item)
921 |
922 _ => reject!()
923 ));
924
925 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700926 attrs: many0!(call!(Attribute::parse_outer)) >>
927 vis: syn!(Visibility) >>
928 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700929 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700930 generics: syn!(Generics) >>
931 where_clause: syn!(WhereClause) >>
932 fields: braces!(call!(Delimited::parse_terminated_with,
933 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800934 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700935 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800936 vis: vis,
937 union_token: union_,
938 ident: ident,
939 generics: Generics {
940 where_clause: where_clause,
941 .. generics
942 },
943 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800944 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700945 ));
946
David Tolnay4c614be2017-11-10 00:02:38 -0800947 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700948 attrs: many0!(call!(Attribute::parse_outer)) >>
949 vis: syn!(Visibility) >>
950 unsafety: syn!(Unsafety) >>
951 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700952 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700953 generics: syn!(Generics) >>
954 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700955 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700956 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700957 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700958 where_clause: syn!(WhereClause) >>
959 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800960 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700961 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800962 vis: vis,
963 unsafety: unsafety,
964 trait_token: trait_,
965 ident: ident,
966 generics: Generics {
967 where_clause: where_clause,
968 .. generics
969 },
970 colon_token: colon,
971 supertraits: bounds.unwrap_or_default(),
972 brace_token: body.1,
973 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800974 })
David Tolnay0aecb732016-10-03 23:03:50 -0700975 ));
976
David Tolnay4c614be2017-11-10 00:02:38 -0800977 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700978 attrs: many0!(call!(Attribute::parse_outer)) >>
979 unsafety: syn!(Unsafety) >>
980 impl_: syn!(Impl) >>
981 path: syn!(Path) >>
982 for_: syn!(For) >>
983 dot2: syn!(Dot2) >>
984 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800985 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700986 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800987 unsafety: unsafety,
988 impl_token: impl_,
989 path: path,
990 for_token: for_,
991 dot2_token: dot2,
992 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800993 })
David Tolnayf94e2362016-10-04 00:29:51 -0700994 ));
995
David Tolnayda705bd2017-11-10 21:58:05 -0800996 impl_synom!(TraitItem "trait item" alt!(
997 syn!(TraitItemConst) => { TraitItem::Const }
998 |
999 syn!(TraitItemMethod) => { TraitItem::Method }
1000 |
1001 syn!(TraitItemType) => { TraitItem::Type }
1002 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001003 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001004 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001005
David Tolnayda705bd2017-11-10 21:58:05 -08001006 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001007 attrs: many0!(call!(Attribute::parse_outer)) >>
1008 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001009 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001010 colon: syn!(Colon) >>
1011 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001012 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001013 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001014 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001015 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001016 const_token: const_,
1017 ident: ident,
1018 colon_token: colon,
1019 ty: ty,
1020 default: default,
1021 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001022 })
1023 ));
1024
David Tolnayda705bd2017-11-10 21:58:05 -08001025 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001026 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1027 constness: syn!(Constness) >>
1028 unsafety: syn!(Unsafety) >>
1029 abi: option!(syn!(Abi)) >>
1030 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001031 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001032 generics: syn!(Generics) >>
1033 inputs: parens!(call!(Delimited::parse_terminated)) >>
1034 ret: syn!(FunctionRetTy) >>
1035 where_clause: syn!(WhereClause) >>
1036 body: option!(braces!(
1037 tuple!(many0!(call!(Attribute::parse_inner)),
1038 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001039 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001040 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001041 ({
1042 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001043 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001044 None => (Vec::new(), None),
1045 };
David Tolnayda705bd2017-11-10 21:58:05 -08001046 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001047 attrs: {
1048 let mut attrs = outer_attrs;
1049 attrs.extend(inner_attrs);
1050 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001051 },
David Tolnayda705bd2017-11-10 21:58:05 -08001052 sig: MethodSig {
1053 constness: constness,
1054 unsafety: unsafety,
1055 abi: abi,
1056 ident: ident,
1057 decl: FnDecl {
1058 inputs: inputs.0,
1059 output: ret,
1060 variadic: false,
1061 fn_token: fn_,
1062 paren_token: inputs.1,
1063 dot_tokens: None,
1064 generics: Generics {
1065 where_clause: where_clause,
1066 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001067 },
1068 },
David Tolnayda705bd2017-11-10 21:58:05 -08001069 },
1070 default: stmts.map(|stmts| {
1071 Block {
1072 stmts: stmts.0,
1073 brace_token: stmts.1,
1074 }
1075 }),
1076 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001077 }
David Tolnay0aecb732016-10-03 23:03:50 -07001078 })
1079 ));
1080
David Tolnayda705bd2017-11-10 21:58:05 -08001081 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001082 attrs: many0!(call!(Attribute::parse_outer)) >>
1083 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001084 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001085 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001086 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001087 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001088 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001089 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1090 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001091 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001092 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001093 type_token: type_,
1094 ident: ident,
1095 colon_token: colon,
1096 bounds: bounds.unwrap_or_default(),
1097 default: default,
1098 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001099 })
1100 ));
1101
David Tolnaydecf28d2017-11-11 11:56:45 -08001102 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001103 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001104 mac: syn!(Macro) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001105 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001106 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001107 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001108 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001109 })
1110 ));
1111
David Tolnay4c614be2017-11-10 00:02:38 -08001112 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001113 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001114 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001115 unsafety: syn!(Unsafety) >>
1116 impl_: syn!(Impl) >>
1117 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001118 polarity_path: alt!(
1119 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001120 polarity: syn!(ImplPolarity) >>
1121 path: syn!(Path) >>
1122 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001123 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001124 )
1125 |
David Tolnay570695e2017-06-03 16:15:13 -07001126 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001127 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001128 self_ty: syn!(Ty) >>
1129 where_clause: syn!(WhereClause) >>
1130 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001131 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001132 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001133 defaultness: defaultness,
1134 unsafety: unsafety,
1135 impl_token: impl_,
1136 generics: Generics {
1137 where_clause: where_clause,
1138 .. generics
1139 },
1140 trait_: polarity_path,
1141 self_ty: Box::new(self_ty),
1142 brace_token: body.1,
1143 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001144 })
David Tolnay4c9be372016-10-06 00:47:37 -07001145 ));
1146
Alex Crichton954046c2017-05-30 21:49:42 -07001147 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001148 named!(parse -> Self, alt!(
1149 impl_item_const
1150 |
1151 impl_item_method
1152 |
1153 impl_item_type
1154 |
David Tolnay570695e2017-06-03 16:15:13 -07001155 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001156 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001157 }
David Tolnay4c9be372016-10-06 00:47:37 -07001158
1159 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001160 attrs: many0!(call!(Attribute::parse_outer)) >>
1161 vis: syn!(Visibility) >>
1162 defaultness: syn!(Defaultness) >>
1163 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001164 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001165 colon: syn!(Colon) >>
1166 ty: syn!(Ty) >>
1167 eq: syn!(Eq) >>
1168 value: syn!(Expr) >>
1169 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001170 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001171 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001172 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001173 vis: vis,
1174 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001175 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001176 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001177 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001178 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001179 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001180 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001181 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001182 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001183 })
1184 ));
1185
1186 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001187 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1188 vis: syn!(Visibility) >>
1189 defaultness: syn!(Defaultness) >>
1190 constness: syn!(Constness) >>
1191 unsafety: syn!(Unsafety) >>
1192 abi: option!(syn!(Abi)) >>
1193 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001194 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001195 generics: syn!(Generics) >>
1196 inputs: parens!(call!(Delimited::parse_terminated)) >>
1197 ret: syn!(FunctionRetTy) >>
1198 where_clause: syn!(WhereClause) >>
1199 inner_attrs_stmts: braces!(tuple!(
1200 many0!(call!(Attribute::parse_inner)),
1201 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001202 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001203 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001204 attrs: {
1205 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001206 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001207 attrs
1208 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001209 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001210 vis: vis,
1211 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001212 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001213 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001214 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001215 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001216 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001217 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001218 fn_token: fn_,
1219 paren_token: inputs.1,
1220 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001221 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001222 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001223 generics: Generics {
1224 where_clause: where_clause,
1225 .. generics
1226 },
1227 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001228 },
1229 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001230 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001231 brace_token: inner_attrs_stmts.1,
1232 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001233 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001234 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001235 })
1236 ));
1237
1238 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001239 attrs: many0!(call!(Attribute::parse_outer)) >>
1240 vis: syn!(Visibility) >>
1241 defaultness: syn!(Defaultness) >>
1242 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001243 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001244 eq: syn!(Eq) >>
1245 ty: syn!(Ty) >>
1246 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001247 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001248 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001249 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001250 vis: vis,
1251 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001252 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001253 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001254 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001255 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001256 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001257 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001258 })
1259 ));
1260
David Tolnay570695e2017-06-03 16:15:13 -07001261 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001262 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001263 mac: syn!(Macro) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001264 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001265 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001266 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001267 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001268 })
1269 ));
1270
Alex Crichton954046c2017-05-30 21:49:42 -07001271 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001272 named!(parse -> Self, alt!(
1273 syn!(Bang) => { ImplPolarity::Negative }
1274 |
1275 epsilon!() => { |_| ImplPolarity::Positive }
1276 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001277 }
David Tolnay4c9be372016-10-06 00:47:37 -07001278
Alex Crichton954046c2017-05-30 21:49:42 -07001279 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001280 named!(parse -> Self, alt!(
1281 syn!(Const) => { Constness::Const }
1282 |
1283 epsilon!() => { |_| Constness::NotConst }
1284 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001285 }
David Tolnay42602292016-10-01 22:25:45 -07001286
Alex Crichton954046c2017-05-30 21:49:42 -07001287 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001288 named!(parse -> Self, alt!(
1289 syn!(Default_) => { Defaultness::Default }
1290 |
1291 epsilon!() => { |_| Defaultness::Final }
1292 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001293 }
David Tolnayedf2b992016-09-23 20:43:45 -07001294}
David Tolnay4a51dc72016-10-01 00:40:31 -07001295
1296#[cfg(feature = "printing")]
1297mod printing {
1298 use super::*;
1299 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001300 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001301 use quote::{Tokens, ToTokens};
1302
1303 impl ToTokens for Item {
1304 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001305 match *self {
1306 Item::ExternCrate(ref item) => {
1307 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001308 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001309 item.extern_token.to_tokens(tokens);
1310 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001311 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001312 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001313 as_token.to_tokens(tokens);
1314 rename.to_tokens(tokens);
1315 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001316 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001317 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001318 Item::Use(ref item) => {
1319 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001320 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001321 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001322 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001323 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001324 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001325 Item::Static(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.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001329 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001330 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001331 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001332 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001333 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001334 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001335 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001336 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001337 Item::Const(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.const_token.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::Fn(ref item) => {
1349 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001350 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001351 item.constness.to_tokens(tokens);
1352 item.unsafety.to_tokens(tokens);
1353 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001354 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001355 item.block.brace_token.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001356 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001357 tokens.append_all(&item.block.stmts);
1358 });
David Tolnay42602292016-10-01 22:25:45 -07001359 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001360 Item::Mod(ref item) => {
1361 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001362 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001363 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001364 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001365 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001366 brace.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001367 tokens.append_all(item.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001368 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001369 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001370 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001371 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001372 }
David Tolnay35902302016-10-06 01:11:08 -07001373 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001374 Item::ForeignMod(ref item) => {
1375 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001376 item.abi.to_tokens(tokens);
1377 item.brace_token.surround(tokens, |tokens| {
1378 tokens.append_all(&item.items);
1379 });
David Tolnay35902302016-10-06 01:11:08 -07001380 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001381 Item::Ty(ref item) => {
1382 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001383 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001384 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001385 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001386 item.generics.to_tokens(tokens);
1387 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001388 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001389 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001390 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001391 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001392 Item::Enum(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.enum_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.brace_token.surround(tokens, |tokens| {
1400 item.variants.to_tokens(tokens);
1401 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001402 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001403 Item::Struct(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.struct_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 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001410 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001411 item.generics.where_clause.to_tokens(tokens);
1412 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001413 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001415 item.data.to_tokens(tokens);
1416 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001417 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001418 }
1419 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001420 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001421 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001422 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001423 }
1424 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001425 Item::Union(ref item) => {
1426 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001427 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001428 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001429 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001430 item.generics.to_tokens(tokens);
1431 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001432 // XXX: Should we handle / complain when using a
1433 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001434 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001435 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001436 Item::Trait(ref item) => {
1437 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001438 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001439 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001440 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001441 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001442 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001443 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001444 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001445 item.supertraits.to_tokens(tokens);
1446 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001447 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001448 item.brace_token.surround(tokens, |tokens| {
1449 tokens.append_all(&item.items);
1450 });
David Tolnayca085422016-10-04 00:12:38 -07001451 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001452 Item::DefaultImpl(ref item) => {
1453 tokens.append_all(item.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001454 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001455 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001456 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001457 item.for_token.to_tokens(tokens);
1458 item.dot2_token.to_tokens(tokens);
1459 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001460 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001461 Item::Impl(ref item) => {
1462 tokens.append_all(item.attrs.outer());
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001463 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001464 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001465 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001466 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001467 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001468 polarity.to_tokens(tokens);
1469 path.to_tokens(tokens);
1470 for_token.to_tokens(tokens);
1471 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001472 item.self_ty.to_tokens(tokens);
1473 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001474 item.brace_token.surround(tokens, |tokens| {
1475 tokens.append_all(&item.items);
1476 });
David Tolnay4c9be372016-10-06 00:47:37 -07001477 }
David Tolnaydecf28d2017-11-11 11:56:45 -08001478 Item::Macro(ref item) => {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001479 tokens.append_all(item.attrs.outer());
1480 item.mac.path.to_tokens(tokens);
1481 item.mac.bang_token.to_tokens(tokens);
1482 item.mac.ident.to_tokens(tokens);
1483 tokens.append_all(&item.mac.tokens);
1484 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001485 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001486 }
1487 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001488 }
1489 }
1490 }
David Tolnay42602292016-10-01 22:25:45 -07001491
Alex Crichton62a0a592017-05-22 13:58:53 -07001492 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001493 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001494 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001495 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001496 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001497 self.rename.to_tokens(tokens);
1498 }
David Tolnay4a057422016-10-08 00:02:31 -07001499 }
1500 }
1501
Alex Crichton62a0a592017-05-22 13:58:53 -07001502 impl ToTokens for PathGlob {
1503 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001504 if self.path.segments.len() > 0 {
1505 self.path.to_tokens(tokens);
1506 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1507 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001508 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001509 }
1510 }
1511
1512 impl ToTokens for PathList {
1513 fn to_tokens(&self, tokens: &mut Tokens) {
1514 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001515 self.colon2_token.to_tokens(tokens);
1516 self.brace_token.surround(tokens, |tokens| {
1517 self.items.to_tokens(tokens);
1518 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001519 }
1520 }
1521
David Tolnay4a057422016-10-08 00:02:31 -07001522 impl ToTokens for PathListItem {
1523 fn to_tokens(&self, tokens: &mut Tokens) {
1524 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001525 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001526 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001527 self.rename.to_tokens(tokens);
1528 }
David Tolnay4a057422016-10-08 00:02:31 -07001529 }
1530 }
1531
David Tolnayca085422016-10-04 00:12:38 -07001532 impl ToTokens for TraitItem {
1533 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayda705bd2017-11-10 21:58:05 -08001534 match *self {
1535 TraitItem::Const(ref item) => {
1536 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001537 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001538 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001539 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001540 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001541 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001542 eq_token.to_tokens(tokens);
1543 default.to_tokens(tokens);
1544 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001545 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001546 }
David Tolnayda705bd2017-11-10 21:58:05 -08001547 TraitItem::Method(ref item) => {
1548 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001549 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001550 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001551 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001552 block.brace_token.surround(tokens, |tokens| {
David Tolnayda705bd2017-11-10 21:58:05 -08001553 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001554 tokens.append_all(&block.stmts);
1555 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001556 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001557 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001558 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 }
David Tolnayca085422016-10-04 00:12:38 -07001560 }
1561 }
David Tolnayda705bd2017-11-10 21:58:05 -08001562 TraitItem::Type(ref item) => {
1563 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001564 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001565 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001566 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001567 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001568 item.bounds.to_tokens(tokens);
1569 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001570 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001571 eq_token.to_tokens(tokens);
1572 default.to_tokens(tokens);
1573 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001574 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001575 }
David Tolnayda705bd2017-11-10 21:58:05 -08001576 TraitItem::Macro(ref item) => {
1577 tokens.append_all(item.attrs.outer());
1578 item.mac.to_tokens(tokens);
1579 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001580 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001581 }
David Tolnayca085422016-10-04 00:12:38 -07001582 }
1583 }
1584 }
1585 }
1586
David Tolnay4c9be372016-10-06 00:47:37 -07001587 impl ToTokens for ImplItem {
1588 fn to_tokens(&self, tokens: &mut Tokens) {
1589 tokens.append_all(self.attrs.outer());
1590 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001591 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001592 item.vis.to_tokens(tokens);
1593 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001594 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001595 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001596 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001597 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001598 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001599 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001600 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001601 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001602 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001603 item.vis.to_tokens(tokens);
1604 item.defaultness.to_tokens(tokens);
1605 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001606 item.block.brace_token.surround(tokens, |tokens| {
1607 tokens.append_all(self.attrs.inner());
1608 tokens.append_all(&item.block.stmts);
1609 });
David Tolnay4c9be372016-10-06 00:47:37 -07001610 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001611 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001612 item.vis.to_tokens(tokens);
1613 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001614 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001615 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001616 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001617 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001618 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001619 }
1620 ImplItemKind::Macro(ref mac) => {
1621 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001622 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001623 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001624 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001625 }
1626 }
1627 }
1628 }
1629 }
1630
David Tolnay8894f602017-11-11 12:11:04 -08001631 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001632 fn to_tokens(&self, tokens: &mut Tokens) {
1633 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001634 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001635 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1636 self.semi_token.to_tokens(tokens);
1637 }
1638 }
1639
1640 impl ToTokens for ForeignItemStatic {
1641 fn to_tokens(&self, tokens: &mut Tokens) {
1642 tokens.append_all(self.attrs.outer());
1643 self.vis.to_tokens(tokens);
1644 self.static_token.to_tokens(tokens);
1645 self.mutbl.to_tokens(tokens);
1646 self.ident.to_tokens(tokens);
1647 self.colon_token.to_tokens(tokens);
1648 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001649 self.semi_token.to_tokens(tokens);
1650 }
1651 }
1652
David Tolnay570695e2017-06-03 16:15:13 -07001653 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001654 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001655 self.constness.to_tokens(tokens);
1656 self.unsafety.to_tokens(tokens);
1657 self.abi.to_tokens(tokens);
1658 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001659 }
1660 }
1661
David Tolnay570695e2017-06-03 16:15:13 -07001662 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001663
1664 impl<'a> ToTokens for NamedDecl<'a> {
1665 fn to_tokens(&self, tokens: &mut Tokens) {
1666 self.0.fn_token.to_tokens(tokens);
1667 self.1.to_tokens(tokens);
1668 self.0.generics.to_tokens(tokens);
1669 self.0.paren_token.surround(tokens, |tokens| {
1670 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001671
1672 if self.0.variadic {
1673 if !self.0.inputs.empty_or_trailing() {
1674 tokens::Comma::default().to_tokens(tokens);
1675 }
Alex Crichton259ee532017-07-14 06:51:02 -07001676 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001677 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001678 });
1679 self.0.output.to_tokens(tokens);
1680 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001681 }
1682 }
1683
Alex Crichton62a0a592017-05-22 13:58:53 -07001684 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001685 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001686 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001687 self.lifetime.to_tokens(tokens);
1688 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001689 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001690 }
1691 }
1692
1693 impl ToTokens for ArgSelf {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001696 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001697 }
1698 }
1699
1700 impl ToTokens for ArgCaptured {
1701 fn to_tokens(&self, tokens: &mut Tokens) {
1702 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001703 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001704 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001705 }
1706 }
1707
David Tolnay42602292016-10-01 22:25:45 -07001708 impl ToTokens for Constness {
1709 fn to_tokens(&self, tokens: &mut Tokens) {
1710 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001712 Constness::NotConst => {
1713 // nothing
1714 }
David Tolnay42602292016-10-01 22:25:45 -07001715 }
1716 }
1717 }
1718
David Tolnay4c9be372016-10-06 00:47:37 -07001719 impl ToTokens for Defaultness {
1720 fn to_tokens(&self, tokens: &mut Tokens) {
1721 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001722 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001723 Defaultness::Final => {
1724 // nothing
1725 }
1726 }
1727 }
1728 }
1729
1730 impl ToTokens for ImplPolarity {
1731 fn to_tokens(&self, tokens: &mut Tokens) {
1732 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001733 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001734 ImplPolarity::Positive => {
1735 // nothing
1736 }
1737 }
1738 }
1739 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001740}