blob: 42267205034506a9235f6ba19f5b615d50bc831e [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
Alex Crichton62a0a592017-05-22 13:58:53 -0700352ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800353 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800355 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700356 pub vis: Visibility,
357 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700358 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700359 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700360 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700362 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700364 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700365 }),
366 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800367 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700368 pub vis: Visibility,
369 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 pub sig: MethodSig,
371 pub block: Block,
372 }),
373 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800374 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700375 pub vis: Visibility,
376 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700377 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700378 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700379 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700381 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
David Tolnay857628c2017-11-11 12:25:31 -0800383 pub Macro(ImplItemMacro {
384 pub attrs: Vec<Attribute>,
385 pub mac: Macro,
386 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700387 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700388}
389
390ast_struct! {
391 /// Represents a method's signature in a trait declaration,
392 /// or in an implementation.
393 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700395 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700397 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 }
400}
401
402ast_struct! {
403 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700404 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 /// E.g. `fn foo(bar: baz)`
406 pub struct FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -0700407 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700408 pub paren_token: tokens::Paren,
409 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700410 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700411 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700413 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700415}
416
Alex Crichton62a0a592017-05-22 13:58:53 -0700417ast_enum_of_structs! {
418 /// An argument in a function header.
419 ///
420 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
421 pub enum FnArg {
422 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700423 pub and_token: tokens::And,
424 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700425 pub lifetime: Option<Lifetime>,
426 pub mutbl: Mutability,
427 }),
428 pub SelfValue(ArgSelf {
429 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700430 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 }),
432 pub Captured(ArgCaptured {
433 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700434 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 pub ty: Ty,
436 }),
437 pub Ignored(Ty),
438 }
David Tolnay62f374c2016-10-02 13:37:00 -0700439}
440
David Tolnayedf2b992016-09-23 20:43:45 -0700441#[cfg(feature = "parsing")]
442pub mod parsing {
443 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700444
Michael Layzell92639a52017-06-01 00:07:44 -0400445 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700446 use synom::tokens::*;
447 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700448
David Tolnay4c614be2017-11-10 00:02:38 -0800449 impl_synom!(Item "item" alt!(
450 syn!(ItemExternCrate) => { Item::ExternCrate }
451 |
452 syn!(ItemUse) => { Item::Use }
453 |
454 syn!(ItemStatic) => { Item::Static }
455 |
456 syn!(ItemConst) => { Item::Const }
457 |
458 syn!(ItemFn) => { Item::Fn }
459 |
460 syn!(ItemMod) => { Item::Mod }
461 |
462 syn!(ItemForeignMod) => { Item::ForeignMod }
463 |
464 syn!(ItemTy) => { Item::Ty }
465 |
466 syn!(ItemStruct) => { Item::Struct }
467 |
468 syn!(ItemEnum) => { Item::Enum }
469 |
470 syn!(ItemUnion) => { Item::Union }
471 |
472 syn!(ItemTrait) => { Item::Trait }
473 |
474 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
475 |
476 syn!(ItemImpl) => { Item::Impl }
477 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800478 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800479 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700480
David Tolnaydecf28d2017-11-11 11:56:45 -0800481 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700482 attrs: many0!(call!(Attribute::parse_outer)) >>
483 what: syn!(Path) >>
484 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700485 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700486 body: call!(::TokenTree::parse_delimited) >>
487 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800488 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700489 attrs: attrs,
David Tolnaydecf28d2017-11-11 11:56:45 -0800490 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500491 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700492 bang_token: bang,
493 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700494 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800495 },
David Tolnay4c614be2017-11-10 00:02:38 -0800496 })
David Tolnayedf2b992016-09-23 20:43:45 -0700497 ));
498
David Tolnay4c614be2017-11-10 00:02:38 -0800499 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700500 attrs: many0!(call!(Attribute::parse_outer)) >>
501 vis: syn!(Visibility) >>
502 extern_: syn!(Extern) >>
503 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700504 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700505 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
506 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800507 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700508 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800509 vis: vis,
510 extern_token: extern_,
511 crate_token: crate_,
512 ident: ident,
513 rename: rename,
514 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800515 })
David Tolnayedf2b992016-09-23 20:43:45 -0700516 ));
517
David Tolnay4c614be2017-11-10 00:02:38 -0800518 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700519 attrs: many0!(call!(Attribute::parse_outer)) >>
520 vis: syn!(Visibility) >>
521 use_: syn!(Use) >>
522 what: syn!(ViewPath) >>
523 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800524 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700525 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800526 vis: vis,
527 use_token: use_,
528 path: Box::new(what),
529 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800530 })
David Tolnay4a057422016-10-08 00:02:31 -0700531 ));
532
Alex Crichton954046c2017-05-30 21:49:42 -0700533 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400534 named!(parse -> Self, alt!(
535 syn!(PathGlob) => { ViewPath::Glob }
536 |
537 syn!(PathList) => { ViewPath::List }
538 |
539 syn!(PathSimple) => { ViewPath::Simple } // must be last
540 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700541 }
David Tolnay4a057422016-10-08 00:02:31 -0700542
Alex Crichton954046c2017-05-30 21:49:42 -0700543 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400544 named!(parse -> Self, do_parse!(
545 path: syn!(Path) >>
546 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
547 (PathSimple {
548 path: path,
549 as_token: rename.as_ref().map(|p| As((p.0).0)),
550 rename: rename.map(|p| p.1),
551 })
552 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700553 }
David Tolnay4a057422016-10-08 00:02:31 -0700554
Alex Crichton954046c2017-05-30 21:49:42 -0700555 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400556 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700557 path: option!(do_parse!(
558 path: syn!(Path) >>
559 colon2: syn!(Colon2) >>
560 (path, colon2)
561 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400562 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700563 ({
564 match path {
565 Some((path, colon2)) => {
566 PathGlob {
567 path: path,
568 colon2_token: Some(colon2),
569 star_token: star,
570 }
571 }
572 None => {
573 PathGlob {
574 path: Path {
575 leading_colon: None,
576 segments: Default::default(),
577 },
578 colon2_token: None,
579 star_token: star,
580 }
581 }
582 }
Michael Layzell92639a52017-06-01 00:07:44 -0400583 })
584 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700585 }
David Tolnay4a057422016-10-08 00:02:31 -0700586
Alex Crichton954046c2017-05-30 21:49:42 -0700587 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400588 named!(parse -> Self, alt!(
589 do_parse!(
590 path: syn!(Path) >>
591 colon2: syn!(Colon2) >>
592 items: braces!(call!(Delimited::parse_terminated)) >>
593 (PathList {
594 path: path,
595 items: items.0,
596 brace_token: items.1,
597 colon2_token: colon2,
598 })
599 )
600 |
601 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700602 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400603 items: braces!(call!(Delimited::parse_terminated)) >>
604 (PathList {
605 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400606 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700607 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400608 },
David Tolnay570695e2017-06-03 16:15:13 -0700609 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400610 brace_token: items.1,
611 items: items.0,
612 })
613 )
614 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700615 }
David Tolnay4a057422016-10-08 00:02:31 -0700616
Alex Crichton954046c2017-05-30 21:49:42 -0700617 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400618 named!(parse -> Self, do_parse!(
619 name: alt!(
620 syn!(Ident)
621 |
622 map!(syn!(Self_), Into::into)
623 ) >>
624 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
625 (PathListItem {
626 name: name,
627 as_token: rename.as_ref().map(|p| As((p.0).0)),
628 rename: rename.map(|p| p.1),
629 })
630 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700631 }
David Tolnay4a057422016-10-08 00:02:31 -0700632
David Tolnay4c614be2017-11-10 00:02:38 -0800633 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700634 attrs: many0!(call!(Attribute::parse_outer)) >>
635 vis: syn!(Visibility) >>
636 static_: syn!(Static) >>
637 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700638 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700639 colon: syn!(Colon) >>
640 ty: syn!(Ty) >>
641 eq: syn!(Eq) >>
642 value: syn!(Expr) >>
643 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800644 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700645 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800646 vis: vis,
647 static_token: static_,
648 mutbl: mutability,
649 ident: ident,
650 colon_token: colon,
651 ty: Box::new(ty),
652 eq_token: eq,
653 expr: Box::new(value),
654 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800655 })
David Tolnay47a877c2016-10-01 16:50:55 -0700656 ));
657
David Tolnay4c614be2017-11-10 00:02:38 -0800658 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700659 attrs: many0!(call!(Attribute::parse_outer)) >>
660 vis: syn!(Visibility) >>
661 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700662 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700663 colon: syn!(Colon) >>
664 ty: syn!(Ty) >>
665 eq: syn!(Eq) >>
666 value: syn!(Expr) >>
667 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800668 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700669 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800670 vis: vis,
671 const_token: const_,
672 ident: ident,
673 colon_token: colon,
674 ty: Box::new(ty),
675 eq_token: eq,
676 expr: Box::new(value),
677 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800678 })
David Tolnay47a877c2016-10-01 16:50:55 -0700679 ));
680
David Tolnay4c614be2017-11-10 00:02:38 -0800681 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700682 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
683 vis: syn!(Visibility) >>
684 constness: syn!(Constness) >>
685 unsafety: syn!(Unsafety) >>
686 abi: option!(syn!(Abi)) >>
687 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700688 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700689 generics: syn!(Generics) >>
690 inputs: parens!(Delimited::parse_terminated) >>
691 ret: syn!(FunctionRetTy) >>
692 where_clause: syn!(WhereClause) >>
693 inner_attrs_stmts: braces!(tuple!(
694 many0!(call!(Attribute::parse_inner)),
695 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400696 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800697 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700698 attrs: {
699 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700700 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700701 attrs
702 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800703 vis: vis,
704 constness: constness,
705 unsafety: unsafety,
706 abi: abi,
707 decl: Box::new(FnDecl {
708 dot_tokens: None,
709 fn_token: fn_,
710 paren_token: inputs.1,
711 inputs: inputs.0,
712 output: ret,
713 variadic: false,
714 generics: Generics {
715 where_clause: where_clause,
716 .. generics
717 },
718 }),
719 ident: ident,
720 block: Box::new(Block {
721 brace_token: inner_attrs_stmts.1,
722 stmts: (inner_attrs_stmts.0).1,
723 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800724 })
David Tolnay42602292016-10-01 22:25:45 -0700725 ));
726
Alex Crichton954046c2017-05-30 21:49:42 -0700727 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400728 named!(parse -> Self, alt!(
729 do_parse!(
730 and: syn!(And) >>
731 lt: option!(syn!(Lifetime)) >>
732 mutability: syn!(Mutability) >>
733 self_: syn!(Self_) >>
734 not!(syn!(Colon)) >>
735 (ArgSelfRef {
736 lifetime: lt,
737 mutbl: mutability,
738 and_token: and,
739 self_token: self_,
740 }.into())
741 )
742 |
743 do_parse!(
744 mutability: syn!(Mutability) >>
745 self_: syn!(Self_) >>
746 not!(syn!(Colon)) >>
747 (ArgSelf {
748 mutbl: mutability,
749 self_token: self_,
750 }.into())
751 )
752 |
753 do_parse!(
754 pat: syn!(Pat) >>
755 colon: syn!(Colon) >>
756 ty: syn!(Ty) >>
757 (ArgCaptured {
758 pat: pat,
759 ty: ty,
760 colon_token: colon,
761 }.into())
762 )
763 |
764 syn!(Ty) => { FnArg::Ignored }
765 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700766 }
David Tolnay62f374c2016-10-02 13:37:00 -0700767
David Tolnay4c614be2017-11-10 00:02:38 -0800768 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700769 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
770 vis: syn!(Visibility) >>
771 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700772 ident: syn!(Ident) >>
773 content_semi: alt!(
774 syn!(Semi) => {|semi| (
775 Vec::new(),
776 None,
777 Some(semi),
778 )}
David Tolnay37d10332016-10-13 20:51:04 -0700779 |
Alex Crichton954046c2017-05-30 21:49:42 -0700780 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700781 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700782 many0!(call!(Attribute::parse_inner)),
783 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400784 )
David Tolnay570695e2017-06-03 16:15:13 -0700785 ) => {|((inner_attrs, items), brace)| (
786 inner_attrs,
787 Some((brace, items)),
788 None,
789 )}
David Tolnay37d10332016-10-13 20:51:04 -0700790 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800791 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700792 attrs: {
793 let mut attrs = outer_attrs;
794 attrs.extend(content_semi.0);
795 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700796 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800797 vis: vis,
798 mod_token: mod_,
799 ident: ident,
800 content: content_semi.1,
801 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800802 })
David Tolnay35902302016-10-06 01:11:08 -0700803 ));
804
David Tolnay4c614be2017-11-10 00:02:38 -0800805 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700806 attrs: many0!(call!(Attribute::parse_outer)) >>
807 abi: syn!(Abi) >>
808 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800809 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700810 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800811 abi: abi,
812 brace_token: items.1,
813 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800814 })
David Tolnay35902302016-10-06 01:11:08 -0700815 ));
816
David Tolnay8894f602017-11-11 12:11:04 -0800817 impl_synom!(ForeignItem "foreign item" alt!(
818 syn!(ForeignItemFn) => { ForeignItem::Fn }
819 |
820 syn!(ForeignItemStatic) => { ForeignItem::Static }
821 ));
David Tolnay35902302016-10-06 01:11:08 -0700822
David Tolnay8894f602017-11-11 12:11:04 -0800823 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700824 attrs: many0!(call!(Attribute::parse_outer)) >>
825 vis: syn!(Visibility) >>
826 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700827 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700828 generics: syn!(Generics) >>
829 inputs: parens!(do_parse!(
830 args: call!(Delimited::parse_terminated) >>
831 variadic: cond!(args.is_empty() || args.trailing_delim(),
832 option!(syn!(Dot3))) >>
833 (args, variadic)
834 )) >>
835 ret: syn!(FunctionRetTy) >>
836 where_clause: syn!(WhereClause) >>
837 semi: syn!(Semi) >>
838 ({
839 let ((inputs, variadic), parens) = inputs;
840 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800841 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700842 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700843 attrs: attrs,
844 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800845 decl: Box::new(FnDecl {
846 fn_token: fn_,
847 paren_token: parens,
848 inputs: inputs,
849 variadic: variadic.is_some(),
850 dot_tokens: variadic,
851 output: ret,
852 generics: Generics {
853 where_clause: where_clause,
854 .. generics
855 },
856 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700857 vis: vis,
858 }
David Tolnay35902302016-10-06 01:11:08 -0700859 })
860 ));
861
David Tolnay8894f602017-11-11 12:11:04 -0800862 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700863 attrs: many0!(call!(Attribute::parse_outer)) >>
864 vis: syn!(Visibility) >>
865 static_: syn!(Static) >>
866 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700867 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700868 colon: syn!(Colon) >>
869 ty: syn!(Ty) >>
870 semi: syn!(Semi) >>
David Tolnay8894f602017-11-11 12:11:04 -0800871 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700872 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700873 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700874 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800875 ty: Box::new(ty),
876 mutbl: mutability,
877 static_token: static_,
878 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700879 vis: vis,
880 })
881 ));
882
David Tolnay4c614be2017-11-10 00:02:38 -0800883 impl_synom!(ItemTy "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700884 attrs: many0!(call!(Attribute::parse_outer)) >>
885 vis: syn!(Visibility) >>
886 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700887 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700888 generics: syn!(Generics) >>
889 where_clause: syn!(WhereClause) >>
890 eq: syn!(Eq) >>
891 ty: syn!(Ty) >>
892 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800893 (ItemTy {
David Tolnay3cf52982016-10-01 17:11:37 -0700894 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800895 vis: vis,
896 type_token: type_,
897 ident: ident,
898 generics: Generics {
899 where_clause: where_clause,
900 ..generics
901 },
902 eq_token: eq,
903 ty: Box::new(ty),
904 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800905 })
David Tolnay3cf52982016-10-01 17:11:37 -0700906 ));
907
David Tolnay4c614be2017-11-10 00:02:38 -0800908 impl_synom!(ItemStruct "struct item" switch!(
909 map!(syn!(DeriveInput), Into::into),
910 Item::Struct(item) => value!(item)
911 |
912 _ => reject!()
913 ));
David Tolnay42602292016-10-01 22:25:45 -0700914
David Tolnay4c614be2017-11-10 00:02:38 -0800915 impl_synom!(ItemEnum "enum item" switch!(
916 map!(syn!(DeriveInput), Into::into),
917 Item::Enum(item) => value!(item)
918 |
919 _ => reject!()
920 ));
921
922 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700923 attrs: many0!(call!(Attribute::parse_outer)) >>
924 vis: syn!(Visibility) >>
925 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700926 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700927 generics: syn!(Generics) >>
928 where_clause: syn!(WhereClause) >>
929 fields: braces!(call!(Delimited::parse_terminated_with,
930 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800931 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700932 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800933 vis: vis,
934 union_token: union_,
935 ident: ident,
936 generics: Generics {
937 where_clause: where_clause,
938 .. generics
939 },
940 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800941 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700942 ));
943
David Tolnay4c614be2017-11-10 00:02:38 -0800944 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700945 attrs: many0!(call!(Attribute::parse_outer)) >>
946 vis: syn!(Visibility) >>
947 unsafety: syn!(Unsafety) >>
948 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700949 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700950 generics: syn!(Generics) >>
951 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700952 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700953 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700954 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700955 where_clause: syn!(WhereClause) >>
956 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800957 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700958 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800959 vis: vis,
960 unsafety: unsafety,
961 trait_token: trait_,
962 ident: ident,
963 generics: Generics {
964 where_clause: where_clause,
965 .. generics
966 },
967 colon_token: colon,
968 supertraits: bounds.unwrap_or_default(),
969 brace_token: body.1,
970 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800971 })
David Tolnay0aecb732016-10-03 23:03:50 -0700972 ));
973
David Tolnay4c614be2017-11-10 00:02:38 -0800974 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700975 attrs: many0!(call!(Attribute::parse_outer)) >>
976 unsafety: syn!(Unsafety) >>
977 impl_: syn!(Impl) >>
978 path: syn!(Path) >>
979 for_: syn!(For) >>
980 dot2: syn!(Dot2) >>
981 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800982 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700983 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800984 unsafety: unsafety,
985 impl_token: impl_,
986 path: path,
987 for_token: for_,
988 dot2_token: dot2,
989 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800990 })
David Tolnayf94e2362016-10-04 00:29:51 -0700991 ));
992
David Tolnayda705bd2017-11-10 21:58:05 -0800993 impl_synom!(TraitItem "trait item" alt!(
994 syn!(TraitItemConst) => { TraitItem::Const }
995 |
996 syn!(TraitItemMethod) => { TraitItem::Method }
997 |
998 syn!(TraitItemType) => { TraitItem::Type }
999 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001000 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001001 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001002
David Tolnayda705bd2017-11-10 21:58:05 -08001003 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001004 attrs: many0!(call!(Attribute::parse_outer)) >>
1005 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001006 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001007 colon: syn!(Colon) >>
1008 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001009 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001010 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001011 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001012 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001013 const_token: const_,
1014 ident: ident,
1015 colon_token: colon,
1016 ty: ty,
1017 default: default,
1018 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001019 })
1020 ));
1021
David Tolnayda705bd2017-11-10 21:58:05 -08001022 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001023 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1024 constness: syn!(Constness) >>
1025 unsafety: syn!(Unsafety) >>
1026 abi: option!(syn!(Abi)) >>
1027 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001028 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001029 generics: syn!(Generics) >>
1030 inputs: parens!(call!(Delimited::parse_terminated)) >>
1031 ret: syn!(FunctionRetTy) >>
1032 where_clause: syn!(WhereClause) >>
1033 body: option!(braces!(
1034 tuple!(many0!(call!(Attribute::parse_inner)),
1035 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001036 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001037 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001038 ({
1039 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001040 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001041 None => (Vec::new(), None),
1042 };
David Tolnayda705bd2017-11-10 21:58:05 -08001043 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001044 attrs: {
1045 let mut attrs = outer_attrs;
1046 attrs.extend(inner_attrs);
1047 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001048 },
David Tolnayda705bd2017-11-10 21:58:05 -08001049 sig: MethodSig {
1050 constness: constness,
1051 unsafety: unsafety,
1052 abi: abi,
1053 ident: ident,
1054 decl: FnDecl {
1055 inputs: inputs.0,
1056 output: ret,
1057 variadic: false,
1058 fn_token: fn_,
1059 paren_token: inputs.1,
1060 dot_tokens: None,
1061 generics: Generics {
1062 where_clause: where_clause,
1063 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001064 },
1065 },
David Tolnayda705bd2017-11-10 21:58:05 -08001066 },
1067 default: stmts.map(|stmts| {
1068 Block {
1069 stmts: stmts.0,
1070 brace_token: stmts.1,
1071 }
1072 }),
1073 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001074 }
David Tolnay0aecb732016-10-03 23:03:50 -07001075 })
1076 ));
1077
David Tolnayda705bd2017-11-10 21:58:05 -08001078 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001079 attrs: many0!(call!(Attribute::parse_outer)) >>
1080 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001081 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001082 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001083 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001084 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001085 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001086 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1087 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001088 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001089 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001090 type_token: type_,
1091 ident: ident,
1092 colon_token: colon,
1093 bounds: bounds.unwrap_or_default(),
1094 default: default,
1095 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001096 })
1097 ));
1098
David Tolnaydecf28d2017-11-11 11:56:45 -08001099 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001100 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001101 mac: syn!(Macro) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001102 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001103 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001104 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001105 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001106 })
1107 ));
1108
David Tolnay4c614be2017-11-10 00:02:38 -08001109 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001110 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001111 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001112 unsafety: syn!(Unsafety) >>
1113 impl_: syn!(Impl) >>
1114 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001115 polarity_path: alt!(
1116 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001117 polarity: syn!(ImplPolarity) >>
1118 path: syn!(Path) >>
1119 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001120 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001121 )
1122 |
David Tolnay570695e2017-06-03 16:15:13 -07001123 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001124 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001125 self_ty: syn!(Ty) >>
1126 where_clause: syn!(WhereClause) >>
1127 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001128 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001129 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001130 defaultness: defaultness,
1131 unsafety: unsafety,
1132 impl_token: impl_,
1133 generics: Generics {
1134 where_clause: where_clause,
1135 .. generics
1136 },
1137 trait_: polarity_path,
1138 self_ty: Box::new(self_ty),
1139 brace_token: body.1,
1140 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001141 })
David Tolnay4c9be372016-10-06 00:47:37 -07001142 ));
1143
David Tolnay857628c2017-11-11 12:25:31 -08001144 impl_synom!(ImplItem "item in impl block" alt!(
1145 syn!(ImplItemConst) => { ImplItem::Const }
1146 |
1147 syn!(ImplItemMethod) => { ImplItem::Method }
1148 |
1149 syn!(ImplItemType) => { ImplItem::Type }
1150 |
1151 syn!(ImplItemMacro) => { ImplItem::Macro }
1152 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001153
David Tolnay857628c2017-11-11 12:25:31 -08001154 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001155 attrs: many0!(call!(Attribute::parse_outer)) >>
1156 vis: syn!(Visibility) >>
1157 defaultness: syn!(Defaultness) >>
1158 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001159 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001160 colon: syn!(Colon) >>
1161 ty: syn!(Ty) >>
1162 eq: syn!(Eq) >>
1163 value: syn!(Expr) >>
1164 semi: syn!(Semi) >>
David Tolnay857628c2017-11-11 12:25:31 -08001165 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001166 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001167 vis: vis,
1168 defaultness: defaultness,
1169 const_token: const_,
1170 ident: ident,
1171 colon_token: colon,
1172 ty: ty,
1173 eq_token: eq,
1174 expr: value,
1175 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001176 })
1177 ));
1178
David Tolnay857628c2017-11-11 12:25:31 -08001179 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001180 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1181 vis: syn!(Visibility) >>
1182 defaultness: syn!(Defaultness) >>
1183 constness: syn!(Constness) >>
1184 unsafety: syn!(Unsafety) >>
1185 abi: option!(syn!(Abi)) >>
1186 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001187 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001188 generics: syn!(Generics) >>
1189 inputs: parens!(call!(Delimited::parse_terminated)) >>
1190 ret: syn!(FunctionRetTy) >>
1191 where_clause: syn!(WhereClause) >>
1192 inner_attrs_stmts: braces!(tuple!(
1193 many0!(call!(Attribute::parse_inner)),
1194 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001195 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001196 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001197 attrs: {
1198 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001199 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001200 attrs
1201 },
David Tolnay857628c2017-11-11 12:25:31 -08001202 vis: vis,
1203 defaultness: defaultness,
1204 sig: MethodSig {
1205 constness: constness,
1206 unsafety: unsafety,
1207 abi: abi,
1208 ident: ident,
1209 decl: FnDecl {
1210 fn_token: fn_,
1211 paren_token: inputs.1,
1212 inputs: inputs.0,
1213 output: ret,
1214 variadic: false,
1215 generics: Generics {
1216 where_clause: where_clause,
1217 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001218 },
David Tolnay857628c2017-11-11 12:25:31 -08001219 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001220 },
David Tolnay857628c2017-11-11 12:25:31 -08001221 },
1222 block: Block {
1223 brace_token: inner_attrs_stmts.1,
1224 stmts: (inner_attrs_stmts.0).1,
1225 },
David Tolnay4c9be372016-10-06 00:47:37 -07001226 })
1227 ));
1228
David Tolnay857628c2017-11-11 12:25:31 -08001229 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001230 attrs: many0!(call!(Attribute::parse_outer)) >>
1231 vis: syn!(Visibility) >>
1232 defaultness: syn!(Defaultness) >>
1233 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001234 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001235 eq: syn!(Eq) >>
1236 ty: syn!(Ty) >>
1237 semi: syn!(Semi) >>
David Tolnay857628c2017-11-11 12:25:31 -08001238 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001239 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001240 vis: vis,
1241 defaultness: defaultness,
1242 type_token: type_,
1243 ident: ident,
1244 eq_token: eq,
1245 ty: ty,
1246 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001247 })
1248 ));
1249
David Tolnay857628c2017-11-11 12:25:31 -08001250 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001251 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001252 mac: syn!(Macro) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001253 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001254 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001255 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001256 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001257 })
1258 ));
1259
Alex Crichton954046c2017-05-30 21:49:42 -07001260 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001261 named!(parse -> Self, alt!(
1262 syn!(Bang) => { ImplPolarity::Negative }
1263 |
1264 epsilon!() => { |_| ImplPolarity::Positive }
1265 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001266 }
David Tolnay4c9be372016-10-06 00:47:37 -07001267
Alex Crichton954046c2017-05-30 21:49:42 -07001268 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001269 named!(parse -> Self, alt!(
1270 syn!(Const) => { Constness::Const }
1271 |
1272 epsilon!() => { |_| Constness::NotConst }
1273 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001274 }
David Tolnay42602292016-10-01 22:25:45 -07001275
Alex Crichton954046c2017-05-30 21:49:42 -07001276 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001277 named!(parse -> Self, alt!(
1278 syn!(Default_) => { Defaultness::Default }
1279 |
1280 epsilon!() => { |_| Defaultness::Final }
1281 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001282 }
David Tolnayedf2b992016-09-23 20:43:45 -07001283}
David Tolnay4a51dc72016-10-01 00:40:31 -07001284
1285#[cfg(feature = "printing")]
1286mod printing {
1287 use super::*;
1288 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001289 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001290 use quote::{Tokens, ToTokens};
1291
1292 impl ToTokens for Item {
1293 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001294 match *self {
1295 Item::ExternCrate(ref item) => {
1296 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001297 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001298 item.extern_token.to_tokens(tokens);
1299 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001300 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001301 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001302 as_token.to_tokens(tokens);
1303 rename.to_tokens(tokens);
1304 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001305 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001306 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001307 Item::Use(ref item) => {
1308 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001309 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001310 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001311 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001312 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001313 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001314 Item::Static(ref item) => {
1315 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001316 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001317 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001318 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001319 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001320 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001321 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001322 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001323 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001324 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001325 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001326 Item::Const(ref item) => {
1327 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001328 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001329 item.const_token.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::Fn(ref item) => {
1338 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001339 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001340 item.constness.to_tokens(tokens);
1341 item.unsafety.to_tokens(tokens);
1342 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001343 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001344 item.block.brace_token.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001345 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001346 tokens.append_all(&item.block.stmts);
1347 });
David Tolnay42602292016-10-01 22:25:45 -07001348 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001349 Item::Mod(ref item) => {
1350 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001351 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001352 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001353 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001354 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001355 brace.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001356 tokens.append_all(item.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001357 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001358 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001359 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001360 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001361 }
David Tolnay35902302016-10-06 01:11:08 -07001362 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001363 Item::ForeignMod(ref item) => {
1364 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 item.abi.to_tokens(tokens);
1366 item.brace_token.surround(tokens, |tokens| {
1367 tokens.append_all(&item.items);
1368 });
David Tolnay35902302016-10-06 01:11:08 -07001369 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001370 Item::Ty(ref item) => {
1371 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001372 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001373 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001374 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001375 item.generics.to_tokens(tokens);
1376 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001377 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001378 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001379 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001380 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001381 Item::Enum(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.enum_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.brace_token.surround(tokens, |tokens| {
1389 item.variants.to_tokens(tokens);
1390 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001391 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001392 Item::Struct(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.struct_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 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001399 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001400 item.generics.where_clause.to_tokens(tokens);
1401 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001402 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001403 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001404 item.data.to_tokens(tokens);
1405 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001406 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001407 }
1408 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001409 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001410 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001411 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001412 }
1413 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001414 Item::Union(ref item) => {
1415 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001416 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001417 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001418 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001419 item.generics.to_tokens(tokens);
1420 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001421 // XXX: Should we handle / complain when using a
1422 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001423 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001424 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001425 Item::Trait(ref item) => {
1426 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001427 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001428 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001429 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001430 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001431 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001432 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001433 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001434 item.supertraits.to_tokens(tokens);
1435 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001436 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001437 item.brace_token.surround(tokens, |tokens| {
1438 tokens.append_all(&item.items);
1439 });
David Tolnayca085422016-10-04 00:12:38 -07001440 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001441 Item::DefaultImpl(ref item) => {
1442 tokens.append_all(item.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001443 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001444 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001445 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001446 item.for_token.to_tokens(tokens);
1447 item.dot2_token.to_tokens(tokens);
1448 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001449 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001450 Item::Impl(ref item) => {
1451 tokens.append_all(item.attrs.outer());
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001452 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001453 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001454 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001455 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001456 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001457 polarity.to_tokens(tokens);
1458 path.to_tokens(tokens);
1459 for_token.to_tokens(tokens);
1460 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001461 item.self_ty.to_tokens(tokens);
1462 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001463 item.brace_token.surround(tokens, |tokens| {
1464 tokens.append_all(&item.items);
1465 });
David Tolnay4c9be372016-10-06 00:47:37 -07001466 }
David Tolnaydecf28d2017-11-11 11:56:45 -08001467 Item::Macro(ref item) => {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001468 tokens.append_all(item.attrs.outer());
1469 item.mac.path.to_tokens(tokens);
1470 item.mac.bang_token.to_tokens(tokens);
1471 item.mac.ident.to_tokens(tokens);
1472 tokens.append_all(&item.mac.tokens);
1473 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001474 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001475 }
1476 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001477 }
1478 }
1479 }
David Tolnay42602292016-10-01 22:25:45 -07001480
Alex Crichton62a0a592017-05-22 13:58:53 -07001481 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001482 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001484 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001485 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001486 self.rename.to_tokens(tokens);
1487 }
David Tolnay4a057422016-10-08 00:02:31 -07001488 }
1489 }
1490
Alex Crichton62a0a592017-05-22 13:58:53 -07001491 impl ToTokens for PathGlob {
1492 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001493 if self.path.segments.len() > 0 {
1494 self.path.to_tokens(tokens);
1495 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1496 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001497 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001498 }
1499 }
1500
1501 impl ToTokens for PathList {
1502 fn to_tokens(&self, tokens: &mut Tokens) {
1503 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001504 self.colon2_token.to_tokens(tokens);
1505 self.brace_token.surround(tokens, |tokens| {
1506 self.items.to_tokens(tokens);
1507 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001508 }
1509 }
1510
David Tolnay4a057422016-10-08 00:02:31 -07001511 impl ToTokens for PathListItem {
1512 fn to_tokens(&self, tokens: &mut Tokens) {
1513 self.name.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
David Tolnayca085422016-10-04 00:12:38 -07001521 impl ToTokens for TraitItem {
1522 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayda705bd2017-11-10 21:58:05 -08001523 match *self {
1524 TraitItem::Const(ref item) => {
1525 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001526 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001527 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001528 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001529 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001530 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001531 eq_token.to_tokens(tokens);
1532 default.to_tokens(tokens);
1533 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001534 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001535 }
David Tolnayda705bd2017-11-10 21:58:05 -08001536 TraitItem::Method(ref item) => {
1537 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001538 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001539 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001540 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001541 block.brace_token.surround(tokens, |tokens| {
David Tolnayda705bd2017-11-10 21:58:05 -08001542 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001543 tokens.append_all(&block.stmts);
1544 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001545 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001546 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001547 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001548 }
David Tolnayca085422016-10-04 00:12:38 -07001549 }
1550 }
David Tolnayda705bd2017-11-10 21:58:05 -08001551 TraitItem::Type(ref item) => {
1552 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001553 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001554 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001555 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001556 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001557 item.bounds.to_tokens(tokens);
1558 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001559 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001560 eq_token.to_tokens(tokens);
1561 default.to_tokens(tokens);
1562 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001564 }
David Tolnayda705bd2017-11-10 21:58:05 -08001565 TraitItem::Macro(ref item) => {
1566 tokens.append_all(item.attrs.outer());
1567 item.mac.to_tokens(tokens);
1568 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001570 }
David Tolnayca085422016-10-04 00:12:38 -07001571 }
1572 }
1573 }
1574 }
1575
David Tolnay857628c2017-11-11 12:25:31 -08001576 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001577 fn to_tokens(&self, tokens: &mut Tokens) {
1578 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001579 self.vis.to_tokens(tokens);
1580 self.defaultness.to_tokens(tokens);
1581 self.const_token.to_tokens(tokens);
1582 self.ident.to_tokens(tokens);
1583 self.colon_token.to_tokens(tokens);
1584 self.ty.to_tokens(tokens);
1585 self.eq_token.to_tokens(tokens);
1586 self.expr.to_tokens(tokens);
1587 self.semi_token.to_tokens(tokens);
1588 }
1589 }
1590
1591 impl ToTokens for ImplItemMethod {
1592 fn to_tokens(&self, tokens: &mut Tokens) {
1593 tokens.append_all(self.attrs.outer());
1594 self.vis.to_tokens(tokens);
1595 self.defaultness.to_tokens(tokens);
1596 self.sig.to_tokens(tokens);
1597 self.block.brace_token.surround(tokens, |tokens| {
1598 tokens.append_all(self.attrs.inner());
1599 tokens.append_all(&self.block.stmts);
1600 });
1601 }
1602 }
1603
1604 impl ToTokens for ImplItemType {
1605 fn to_tokens(&self, tokens: &mut Tokens) {
1606 tokens.append_all(self.attrs.outer());
1607 self.vis.to_tokens(tokens);
1608 self.defaultness.to_tokens(tokens);
1609 self.type_token.to_tokens(tokens);
1610 self.ident.to_tokens(tokens);
1611 self.eq_token.to_tokens(tokens);
1612 self.ty.to_tokens(tokens);
1613 self.semi_token.to_tokens(tokens);
1614 }
1615 }
1616
1617 impl ToTokens for ImplItemMacro {
1618 fn to_tokens(&self, tokens: &mut Tokens) {
1619 tokens.append_all(self.attrs.outer());
1620 self.mac.to_tokens(tokens);
1621 if !self.mac.is_braced() {
1622 // FIXME needs a span
1623 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001624 }
1625 }
1626 }
1627
David Tolnay8894f602017-11-11 12:11:04 -08001628 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001629 fn to_tokens(&self, tokens: &mut Tokens) {
1630 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001631 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001632 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1633 self.semi_token.to_tokens(tokens);
1634 }
1635 }
1636
1637 impl ToTokens for ForeignItemStatic {
1638 fn to_tokens(&self, tokens: &mut Tokens) {
1639 tokens.append_all(self.attrs.outer());
1640 self.vis.to_tokens(tokens);
1641 self.static_token.to_tokens(tokens);
1642 self.mutbl.to_tokens(tokens);
1643 self.ident.to_tokens(tokens);
1644 self.colon_token.to_tokens(tokens);
1645 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001646 self.semi_token.to_tokens(tokens);
1647 }
1648 }
1649
David Tolnay570695e2017-06-03 16:15:13 -07001650 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001651 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001652 self.constness.to_tokens(tokens);
1653 self.unsafety.to_tokens(tokens);
1654 self.abi.to_tokens(tokens);
1655 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001656 }
1657 }
1658
David Tolnay570695e2017-06-03 16:15:13 -07001659 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001660
1661 impl<'a> ToTokens for NamedDecl<'a> {
1662 fn to_tokens(&self, tokens: &mut Tokens) {
1663 self.0.fn_token.to_tokens(tokens);
1664 self.1.to_tokens(tokens);
1665 self.0.generics.to_tokens(tokens);
1666 self.0.paren_token.surround(tokens, |tokens| {
1667 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001668
1669 if self.0.variadic {
1670 if !self.0.inputs.empty_or_trailing() {
1671 tokens::Comma::default().to_tokens(tokens);
1672 }
Alex Crichton259ee532017-07-14 06:51:02 -07001673 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001674 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001675 });
1676 self.0.output.to_tokens(tokens);
1677 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001678 }
1679 }
1680
Alex Crichton62a0a592017-05-22 13:58:53 -07001681 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001682 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001683 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001684 self.lifetime.to_tokens(tokens);
1685 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001686 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001687 }
1688 }
1689
1690 impl ToTokens for ArgSelf {
1691 fn to_tokens(&self, tokens: &mut Tokens) {
1692 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001693 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001694 }
1695 }
1696
1697 impl ToTokens for ArgCaptured {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001700 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001701 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001702 }
1703 }
1704
David Tolnay42602292016-10-01 22:25:45 -07001705 impl ToTokens for Constness {
1706 fn to_tokens(&self, tokens: &mut Tokens) {
1707 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001708 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001709 Constness::NotConst => {
1710 // nothing
1711 }
David Tolnay42602292016-10-01 22:25:45 -07001712 }
1713 }
1714 }
1715
David Tolnay4c9be372016-10-06 00:47:37 -07001716 impl ToTokens for Defaultness {
1717 fn to_tokens(&self, tokens: &mut Tokens) {
1718 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001719 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001720 Defaultness::Final => {
1721 // nothing
1722 }
1723 }
1724 }
1725 }
1726
1727 impl ToTokens for ImplPolarity {
1728 fn to_tokens(&self, tokens: &mut Tokens) {
1729 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001730 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001731 ImplPolarity::Positive => {
1732 // nothing
1733 }
1734 }
1735 }
1736 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001737}