blob: 30a4a58bcc5d38003189971f69c1bba29ddfd080 [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,
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 pub extern_token: Token![extern],
14 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070015 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub rename: Option<(Token![as], Ident)>,
17 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 pub use_token: Token![use],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub path: Box<ViewPath>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080038 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080039 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070041 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070078 pub ident: Ident,
79 pub content: Option<(tokens::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub semi: Option<Token![;]>,
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>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -080094 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070096 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080097 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800101 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub enum_token: Token![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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 pub variants: Delimited<Variant, Token![,]>,
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub struct_token: Token![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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 pub semi_token: Option<Token![;]>,
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub union_token: Token![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 Tolnayf8db7ba2017-11-11 22:52:16 -0800146 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700147 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700148 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800150 pub supertraits: Delimited<TypeParamBound, Token![+]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700151 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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800160 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700161 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800162 pub for_token: Token![for],
163 pub dot2_token: Token![..],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700164 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 Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700175 /// Trait this impl implements.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800176 pub trait_: Option<(ImplPolarity, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700177 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800178 pub self_ty: Box<Type>,
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 Tolnay99a953d2017-11-11 12:51:43 -0800187 /// The `example` in `macro_rules! example { ... }`.
188 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800189 pub mac: Macro,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800190 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 }
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800232 pub as_token: Option<Token![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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800239 pub colon2_token: Option<Token![::]>,
240 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700241 }),
242
243 /// `foo::bar::{a, b, c}`
244 pub List(PathList {
245 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub colon2_token: Token![::],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700247 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800248 pub items: Delimited<PathListItem, Token![,]>,
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>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub as_token: Option<Token![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 {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800265 Const(Token![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 {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800273 Default(Token![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 Tolnayf8db7ba2017-11-11 22:52:16 -0800287 pub semi_token: Token![;],
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800293 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800295 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800296 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800297 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800298 pub semi_token: Token![;],
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>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800311 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700312 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800313 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800314 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800315 pub default: Option<(Token![=], Expr)>,
316 pub semi_token: Token![;],
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>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800322 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
324 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800325 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800326 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700327 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800328 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800329 pub bounds: Delimited<TypeParamBound, Token![+]>,
330 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800331 pub semi_token: Token![;],
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 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700338}
339
340ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700341 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 pub enum ImplPolarity {
343 /// `impl Trait for Type`
344 Positive,
345 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800346 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 }
348}
349
Alex Crichton62a0a592017-05-22 13:58:53 -0700350ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800351 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800353 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700354 pub vis: Visibility,
355 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800356 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700357 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800359 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800362 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 }),
364 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800365 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700366 pub vis: Visibility,
367 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 pub sig: MethodSig,
369 pub block: Block,
370 }),
371 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800372 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700373 pub vis: Visibility,
374 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700376 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800378 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800379 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 }),
David Tolnay857628c2017-11-11 12:25:31 -0800381 pub Macro(ImplItemMacro {
382 pub attrs: Vec<Attribute>,
383 pub mac: Macro,
384 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700385 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700386}
387
388ast_struct! {
389 /// Represents a method's signature in a trait declaration,
390 /// or in an implementation.
391 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700393 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700395 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }
398}
399
400ast_struct! {
401 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700402 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 /// E.g. `fn foo(bar: baz)`
404 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800405 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700406 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800407 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800408 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700409 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700410 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800411 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700413}
414
Alex Crichton62a0a592017-05-22 13:58:53 -0700415ast_enum_of_structs! {
416 /// An argument in a function header.
417 ///
418 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
419 pub enum FnArg {
420 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800421 pub and_token: Token![&],
422 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 pub lifetime: Option<Lifetime>,
424 pub mutbl: Mutability,
425 }),
426 pub SelfValue(ArgSelf {
427 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800428 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 }),
430 pub Captured(ArgCaptured {
431 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800433 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800435 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700436 }
David Tolnay62f374c2016-10-02 13:37:00 -0700437}
438
David Tolnayedf2b992016-09-23 20:43:45 -0700439#[cfg(feature = "parsing")]
440pub mod parsing {
441 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700442
Michael Layzell92639a52017-06-01 00:07:44 -0400443 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700444
David Tolnay4c614be2017-11-10 00:02:38 -0800445 impl_synom!(Item "item" alt!(
446 syn!(ItemExternCrate) => { Item::ExternCrate }
447 |
448 syn!(ItemUse) => { Item::Use }
449 |
450 syn!(ItemStatic) => { Item::Static }
451 |
452 syn!(ItemConst) => { Item::Const }
453 |
454 syn!(ItemFn) => { Item::Fn }
455 |
456 syn!(ItemMod) => { Item::Mod }
457 |
458 syn!(ItemForeignMod) => { Item::ForeignMod }
459 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800460 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800461 |
462 syn!(ItemStruct) => { Item::Struct }
463 |
464 syn!(ItemEnum) => { Item::Enum }
465 |
466 syn!(ItemUnion) => { Item::Union }
467 |
468 syn!(ItemTrait) => { Item::Trait }
469 |
470 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
471 |
472 syn!(ItemImpl) => { Item::Impl }
473 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800474 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800475 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700476
David Tolnaydecf28d2017-11-11 11:56:45 -0800477 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700478 attrs: many0!(call!(Attribute::parse_outer)) >>
479 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800480 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700481 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700482 body: call!(::TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800483 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800484 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700485 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800486 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800487 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500488 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700489 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700490 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800491 },
David Tolnay4c614be2017-11-10 00:02:38 -0800492 })
David Tolnayedf2b992016-09-23 20:43:45 -0700493 ));
494
David Tolnay4c614be2017-11-10 00:02:38 -0800495 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700496 attrs: many0!(call!(Attribute::parse_outer)) >>
497 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800498 extern_: keyword!(extern) >>
499 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700500 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800501 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
502 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800503 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700504 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800505 vis: vis,
506 extern_token: extern_,
507 crate_token: crate_,
508 ident: ident,
509 rename: rename,
510 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800511 })
David Tolnayedf2b992016-09-23 20:43:45 -0700512 ));
513
David Tolnay4c614be2017-11-10 00:02:38 -0800514 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700515 attrs: many0!(call!(Attribute::parse_outer)) >>
516 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700518 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800519 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800520 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700521 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800522 vis: vis,
523 use_token: use_,
524 path: Box::new(what),
525 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800526 })
David Tolnay4a057422016-10-08 00:02:31 -0700527 ));
528
Alex Crichton954046c2017-05-30 21:49:42 -0700529 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400530 named!(parse -> Self, alt!(
531 syn!(PathGlob) => { ViewPath::Glob }
532 |
533 syn!(PathList) => { ViewPath::List }
534 |
535 syn!(PathSimple) => { ViewPath::Simple } // must be last
536 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700537 }
David Tolnay4a057422016-10-08 00:02:31 -0700538
Alex Crichton954046c2017-05-30 21:49:42 -0700539 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400540 named!(parse -> Self, do_parse!(
541 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400543 (PathSimple {
544 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800545 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400546 rename: rename.map(|p| p.1),
547 })
548 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700549 }
David Tolnay4a057422016-10-08 00:02:31 -0700550
Alex Crichton954046c2017-05-30 21:49:42 -0700551 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400552 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700553 path: option!(do_parse!(
554 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800555 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700556 (path, colon2)
557 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800558 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700559 ({
560 match path {
561 Some((path, colon2)) => {
562 PathGlob {
563 path: path,
564 colon2_token: Some(colon2),
565 star_token: star,
566 }
567 }
568 None => {
569 PathGlob {
570 path: Path {
571 leading_colon: None,
572 segments: Default::default(),
573 },
574 colon2_token: None,
575 star_token: star,
576 }
577 }
578 }
Michael Layzell92639a52017-06-01 00:07:44 -0400579 })
580 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700581 }
David Tolnay4a057422016-10-08 00:02:31 -0700582
Alex Crichton954046c2017-05-30 21:49:42 -0700583 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400584 named!(parse -> Self, alt!(
585 do_parse!(
586 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800587 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400588 items: braces!(call!(Delimited::parse_terminated)) >>
589 (PathList {
590 path: path,
591 items: items.0,
592 brace_token: items.1,
593 colon2_token: colon2,
594 })
595 )
596 |
597 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800598 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400599 items: braces!(call!(Delimited::parse_terminated)) >>
600 (PathList {
601 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400602 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700603 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400604 },
David Tolnay570695e2017-06-03 16:15:13 -0700605 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400606 brace_token: items.1,
607 items: items.0,
608 })
609 )
610 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700611 }
David Tolnay4a057422016-10-08 00:02:31 -0700612
Alex Crichton954046c2017-05-30 21:49:42 -0700613 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400614 named!(parse -> Self, do_parse!(
615 name: alt!(
616 syn!(Ident)
617 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800618 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400619 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400621 (PathListItem {
622 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800623 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400624 rename: rename.map(|p| p.1),
625 })
626 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700627 }
David Tolnay4a057422016-10-08 00:02:31 -0700628
David Tolnay4c614be2017-11-10 00:02:38 -0800629 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700630 attrs: many0!(call!(Attribute::parse_outer)) >>
631 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800632 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700633 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700634 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800635 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800636 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800637 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700638 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800639 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800640 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700641 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800642 vis: vis,
643 static_token: static_,
644 mutbl: mutability,
645 ident: ident,
646 colon_token: colon,
647 ty: Box::new(ty),
648 eq_token: eq,
649 expr: Box::new(value),
650 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800651 })
David Tolnay47a877c2016-10-01 16:50:55 -0700652 ));
653
David Tolnay4c614be2017-11-10 00:02:38 -0800654 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700655 attrs: many0!(call!(Attribute::parse_outer)) >>
656 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800657 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700658 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800659 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800660 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800661 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700662 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800664 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700665 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800666 vis: vis,
667 const_token: const_,
668 ident: ident,
669 colon_token: colon,
670 ty: Box::new(ty),
671 eq_token: eq,
672 expr: Box::new(value),
673 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800674 })
David Tolnay47a877c2016-10-01 16:50:55 -0700675 ));
676
David Tolnay4c614be2017-11-10 00:02:38 -0800677 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700678 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
679 vis: syn!(Visibility) >>
680 constness: syn!(Constness) >>
681 unsafety: syn!(Unsafety) >>
682 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800683 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700684 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700685 generics: syn!(Generics) >>
686 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800687 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700688 where_clause: syn!(WhereClause) >>
689 inner_attrs_stmts: braces!(tuple!(
690 many0!(call!(Attribute::parse_inner)),
691 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400692 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800693 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700694 attrs: {
695 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700696 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700697 attrs
698 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800699 vis: vis,
700 constness: constness,
701 unsafety: unsafety,
702 abi: abi,
703 decl: Box::new(FnDecl {
704 dot_tokens: None,
705 fn_token: fn_,
706 paren_token: inputs.1,
707 inputs: inputs.0,
708 output: ret,
709 variadic: false,
710 generics: Generics {
711 where_clause: where_clause,
712 .. generics
713 },
714 }),
715 ident: ident,
716 block: Box::new(Block {
717 brace_token: inner_attrs_stmts.1,
718 stmts: (inner_attrs_stmts.0).1,
719 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800720 })
David Tolnay42602292016-10-01 22:25:45 -0700721 ));
722
Alex Crichton954046c2017-05-30 21:49:42 -0700723 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400724 named!(parse -> Self, alt!(
725 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800726 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400727 lt: option!(syn!(Lifetime)) >>
728 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800729 self_: keyword!(self) >>
730 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400731 (ArgSelfRef {
732 lifetime: lt,
733 mutbl: mutability,
734 and_token: and,
735 self_token: self_,
736 }.into())
737 )
738 |
739 do_parse!(
740 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800741 self_: keyword!(self) >>
742 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400743 (ArgSelf {
744 mutbl: mutability,
745 self_token: self_,
746 }.into())
747 )
748 |
749 do_parse!(
750 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800751 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800752 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400753 (ArgCaptured {
754 pat: pat,
755 ty: ty,
756 colon_token: colon,
757 }.into())
758 )
759 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800760 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400761 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700762 }
David Tolnay62f374c2016-10-02 13:37:00 -0700763
David Tolnay4c614be2017-11-10 00:02:38 -0800764 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700765 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
766 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700768 ident: syn!(Ident) >>
769 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800770 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700771 Vec::new(),
772 None,
773 Some(semi),
774 )}
David Tolnay37d10332016-10-13 20:51:04 -0700775 |
Alex Crichton954046c2017-05-30 21:49:42 -0700776 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700777 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700778 many0!(call!(Attribute::parse_inner)),
779 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400780 )
David Tolnay570695e2017-06-03 16:15:13 -0700781 ) => {|((inner_attrs, items), brace)| (
782 inner_attrs,
783 Some((brace, items)),
784 None,
785 )}
David Tolnay37d10332016-10-13 20:51:04 -0700786 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800787 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700788 attrs: {
789 let mut attrs = outer_attrs;
790 attrs.extend(content_semi.0);
791 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700792 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800793 vis: vis,
794 mod_token: mod_,
795 ident: ident,
796 content: content_semi.1,
797 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800798 })
David Tolnay35902302016-10-06 01:11:08 -0700799 ));
800
David Tolnay4c614be2017-11-10 00:02:38 -0800801 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700802 attrs: many0!(call!(Attribute::parse_outer)) >>
803 abi: syn!(Abi) >>
804 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800805 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700806 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800807 abi: abi,
808 brace_token: items.1,
809 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800810 })
David Tolnay35902302016-10-06 01:11:08 -0700811 ));
812
David Tolnay8894f602017-11-11 12:11:04 -0800813 impl_synom!(ForeignItem "foreign item" alt!(
814 syn!(ForeignItemFn) => { ForeignItem::Fn }
815 |
816 syn!(ForeignItemStatic) => { ForeignItem::Static }
817 ));
David Tolnay35902302016-10-06 01:11:08 -0700818
David Tolnay8894f602017-11-11 12:11:04 -0800819 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700820 attrs: many0!(call!(Attribute::parse_outer)) >>
821 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800822 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700823 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700824 generics: syn!(Generics) >>
825 inputs: parens!(do_parse!(
826 args: call!(Delimited::parse_terminated) >>
827 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800828 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700829 (args, variadic)
830 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800831 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700832 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800833 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700834 ({
835 let ((inputs, variadic), parens) = inputs;
836 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800837 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700838 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700839 attrs: attrs,
840 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800841 decl: Box::new(FnDecl {
842 fn_token: fn_,
843 paren_token: parens,
844 inputs: inputs,
845 variadic: variadic.is_some(),
846 dot_tokens: variadic,
847 output: ret,
848 generics: Generics {
849 where_clause: where_clause,
850 .. generics
851 },
852 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700853 vis: vis,
854 }
David Tolnay35902302016-10-06 01:11:08 -0700855 })
856 ));
857
David Tolnay8894f602017-11-11 12:11:04 -0800858 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700859 attrs: many0!(call!(Attribute::parse_outer)) >>
860 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700862 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700863 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800864 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800865 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800867 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700868 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700869 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700870 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800871 ty: Box::new(ty),
872 mutbl: mutability,
873 static_token: static_,
874 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700875 vis: vis,
876 })
877 ));
878
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800879 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700880 attrs: many0!(call!(Attribute::parse_outer)) >>
881 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800882 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700883 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700884 generics: syn!(Generics) >>
885 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800886 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800887 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800888 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800889 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700890 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800891 vis: vis,
892 type_token: type_,
893 ident: ident,
894 generics: Generics {
895 where_clause: where_clause,
896 ..generics
897 },
898 eq_token: eq,
899 ty: Box::new(ty),
900 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800901 })
David Tolnay3cf52982016-10-01 17:11:37 -0700902 ));
903
David Tolnay4c614be2017-11-10 00:02:38 -0800904 impl_synom!(ItemStruct "struct item" switch!(
905 map!(syn!(DeriveInput), Into::into),
906 Item::Struct(item) => value!(item)
907 |
908 _ => reject!()
909 ));
David Tolnay42602292016-10-01 22:25:45 -0700910
David Tolnay4c614be2017-11-10 00:02:38 -0800911 impl_synom!(ItemEnum "enum item" switch!(
912 map!(syn!(DeriveInput), Into::into),
913 Item::Enum(item) => value!(item)
914 |
915 _ => reject!()
916 ));
917
918 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700919 attrs: many0!(call!(Attribute::parse_outer)) >>
920 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800921 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700922 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700923 generics: syn!(Generics) >>
924 where_clause: syn!(WhereClause) >>
925 fields: braces!(call!(Delimited::parse_terminated_with,
926 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800927 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700928 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800929 vis: vis,
930 union_token: union_,
931 ident: ident,
932 generics: Generics {
933 where_clause: where_clause,
934 .. generics
935 },
936 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800937 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700938 ));
939
David Tolnay4c614be2017-11-10 00:02:38 -0800940 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700941 attrs: many0!(call!(Attribute::parse_outer)) >>
942 vis: syn!(Visibility) >>
943 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800944 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700945 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700946 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800947 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700949 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700950 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700951 where_clause: syn!(WhereClause) >>
952 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800953 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700954 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800955 vis: vis,
956 unsafety: unsafety,
957 trait_token: trait_,
958 ident: ident,
959 generics: Generics {
960 where_clause: where_clause,
961 .. generics
962 },
963 colon_token: colon,
964 supertraits: bounds.unwrap_or_default(),
965 brace_token: body.1,
966 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800967 })
David Tolnay0aecb732016-10-03 23:03:50 -0700968 ));
969
David Tolnay4c614be2017-11-10 00:02:38 -0800970 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700971 attrs: many0!(call!(Attribute::parse_outer)) >>
972 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800973 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700974 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 for_: keyword!(for) >>
976 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700977 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800978 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700979 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800980 unsafety: unsafety,
981 impl_token: impl_,
982 path: path,
983 for_token: for_,
984 dot2_token: dot2,
985 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800986 })
David Tolnayf94e2362016-10-04 00:29:51 -0700987 ));
988
David Tolnayda705bd2017-11-10 21:58:05 -0800989 impl_synom!(TraitItem "trait item" alt!(
990 syn!(TraitItemConst) => { TraitItem::Const }
991 |
992 syn!(TraitItemMethod) => { TraitItem::Method }
993 |
994 syn!(TraitItemType) => { TraitItem::Type }
995 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800996 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -0800997 ));
David Tolnay0aecb732016-10-03 23:03:50 -0700998
David Tolnayda705bd2017-11-10 21:58:05 -0800999 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001000 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001001 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001002 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001004 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1006 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001007 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001008 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001009 const_token: const_,
1010 ident: ident,
1011 colon_token: colon,
1012 ty: ty,
1013 default: default,
1014 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001015 })
1016 ));
1017
David Tolnayda705bd2017-11-10 21:58:05 -08001018 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001019 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1020 constness: syn!(Constness) >>
1021 unsafety: syn!(Unsafety) >>
1022 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001024 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001025 generics: syn!(Generics) >>
1026 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001027 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001028 where_clause: syn!(WhereClause) >>
1029 body: option!(braces!(
1030 tuple!(many0!(call!(Attribute::parse_inner)),
1031 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001032 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001034 ({
1035 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001036 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001037 None => (Vec::new(), None),
1038 };
David Tolnayda705bd2017-11-10 21:58:05 -08001039 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001040 attrs: {
1041 let mut attrs = outer_attrs;
1042 attrs.extend(inner_attrs);
1043 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001044 },
David Tolnayda705bd2017-11-10 21:58:05 -08001045 sig: MethodSig {
1046 constness: constness,
1047 unsafety: unsafety,
1048 abi: abi,
1049 ident: ident,
1050 decl: FnDecl {
1051 inputs: inputs.0,
1052 output: ret,
1053 variadic: false,
1054 fn_token: fn_,
1055 paren_token: inputs.1,
1056 dot_tokens: None,
1057 generics: Generics {
1058 where_clause: where_clause,
1059 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001060 },
1061 },
David Tolnayda705bd2017-11-10 21:58:05 -08001062 },
1063 default: stmts.map(|stmts| {
1064 Block {
1065 stmts: stmts.0,
1066 brace_token: stmts.1,
1067 }
1068 }),
1069 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001070 }
David Tolnay0aecb732016-10-03 23:03:50 -07001071 })
1072 ));
1073
David Tolnayda705bd2017-11-10 21:58:05 -08001074 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001075 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001076 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001077 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001078 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001079 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001080 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001081 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001082 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001083 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001084 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001085 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001086 type_token: type_,
1087 ident: ident,
1088 colon_token: colon,
1089 bounds: bounds.unwrap_or_default(),
1090 default: default,
1091 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001092 })
1093 ));
1094
David Tolnaydecf28d2017-11-11 11:56:45 -08001095 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001096 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001097 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001099 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001100 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001101 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001102 })
1103 ));
1104
David Tolnay4c614be2017-11-10 00:02:38 -08001105 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001106 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001107 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001108 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001110 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001111 polarity_path: alt!(
1112 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001113 polarity: syn!(ImplPolarity) >>
1114 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001115 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001116 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001117 )
1118 |
David Tolnay570695e2017-06-03 16:15:13 -07001119 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001120 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001121 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001122 where_clause: syn!(WhereClause) >>
1123 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001124 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001125 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001126 defaultness: defaultness,
1127 unsafety: unsafety,
1128 impl_token: impl_,
1129 generics: Generics {
1130 where_clause: where_clause,
1131 .. generics
1132 },
1133 trait_: polarity_path,
1134 self_ty: Box::new(self_ty),
1135 brace_token: body.1,
1136 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001137 })
David Tolnay4c9be372016-10-06 00:47:37 -07001138 ));
1139
David Tolnay857628c2017-11-11 12:25:31 -08001140 impl_synom!(ImplItem "item in impl block" alt!(
1141 syn!(ImplItemConst) => { ImplItem::Const }
1142 |
1143 syn!(ImplItemMethod) => { ImplItem::Method }
1144 |
1145 syn!(ImplItemType) => { ImplItem::Type }
1146 |
1147 syn!(ImplItemMacro) => { ImplItem::Macro }
1148 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001149
David Tolnay857628c2017-11-11 12:25:31 -08001150 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001151 attrs: many0!(call!(Attribute::parse_outer)) >>
1152 vis: syn!(Visibility) >>
1153 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001154 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001155 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001156 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001157 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001158 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001159 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001160 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001161 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001162 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001163 vis: vis,
1164 defaultness: defaultness,
1165 const_token: const_,
1166 ident: ident,
1167 colon_token: colon,
1168 ty: ty,
1169 eq_token: eq,
1170 expr: value,
1171 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001172 })
1173 ));
1174
David Tolnay857628c2017-11-11 12:25:31 -08001175 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001176 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1177 vis: syn!(Visibility) >>
1178 defaultness: syn!(Defaultness) >>
1179 constness: syn!(Constness) >>
1180 unsafety: syn!(Unsafety) >>
1181 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001182 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001183 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001184 generics: syn!(Generics) >>
1185 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001186 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001187 where_clause: syn!(WhereClause) >>
1188 inner_attrs_stmts: braces!(tuple!(
1189 many0!(call!(Attribute::parse_inner)),
1190 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001191 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001192 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001193 attrs: {
1194 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001195 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001196 attrs
1197 },
David Tolnay857628c2017-11-11 12:25:31 -08001198 vis: vis,
1199 defaultness: defaultness,
1200 sig: MethodSig {
1201 constness: constness,
1202 unsafety: unsafety,
1203 abi: abi,
1204 ident: ident,
1205 decl: FnDecl {
1206 fn_token: fn_,
1207 paren_token: inputs.1,
1208 inputs: inputs.0,
1209 output: ret,
1210 variadic: false,
1211 generics: Generics {
1212 where_clause: where_clause,
1213 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001214 },
David Tolnay857628c2017-11-11 12:25:31 -08001215 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001216 },
David Tolnay857628c2017-11-11 12:25:31 -08001217 },
1218 block: Block {
1219 brace_token: inner_attrs_stmts.1,
1220 stmts: (inner_attrs_stmts.0).1,
1221 },
David Tolnay4c9be372016-10-06 00:47:37 -07001222 })
1223 ));
1224
David Tolnay857628c2017-11-11 12:25:31 -08001225 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001226 attrs: many0!(call!(Attribute::parse_outer)) >>
1227 vis: syn!(Visibility) >>
1228 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001229 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001230 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001231 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001232 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001233 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001234 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001235 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001236 vis: vis,
1237 defaultness: defaultness,
1238 type_token: type_,
1239 ident: ident,
1240 eq_token: eq,
1241 ty: ty,
1242 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001243 })
1244 ));
1245
David Tolnay857628c2017-11-11 12:25:31 -08001246 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001247 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001248 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001249 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001250 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001251 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001252 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001253 })
1254 ));
1255
Alex Crichton954046c2017-05-30 21:49:42 -07001256 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001257 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001258 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001259 |
1260 epsilon!() => { |_| ImplPolarity::Positive }
1261 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001262 }
David Tolnay4c9be372016-10-06 00:47:37 -07001263
Alex Crichton954046c2017-05-30 21:49:42 -07001264 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001265 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001266 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001267 |
1268 epsilon!() => { |_| Constness::NotConst }
1269 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001270 }
David Tolnay42602292016-10-01 22:25:45 -07001271
Alex Crichton954046c2017-05-30 21:49:42 -07001272 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001273 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001274 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001275 |
1276 epsilon!() => { |_| Defaultness::Final }
1277 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001278 }
David Tolnayedf2b992016-09-23 20:43:45 -07001279}
David Tolnay4a51dc72016-10-01 00:40:31 -07001280
1281#[cfg(feature = "printing")]
1282mod printing {
1283 use super::*;
1284 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001285 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001286 use quote::{Tokens, ToTokens};
1287
David Tolnay1bfa7332017-11-11 12:41:20 -08001288 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001289 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001290 tokens.append_all(self.attrs.outer());
1291 self.vis.to_tokens(tokens);
1292 self.extern_token.to_tokens(tokens);
1293 self.crate_token.to_tokens(tokens);
1294 self.ident.to_tokens(tokens);
1295 if let Some((ref as_token, ref rename)) = self.rename {
1296 as_token.to_tokens(tokens);
1297 rename.to_tokens(tokens);
1298 }
1299 self.semi_token.to_tokens(tokens);
1300 }
1301 }
1302
1303 impl ToTokens for ItemUse {
1304 fn to_tokens(&self, tokens: &mut Tokens) {
1305 tokens.append_all(self.attrs.outer());
1306 self.vis.to_tokens(tokens);
1307 self.use_token.to_tokens(tokens);
1308 self.path.to_tokens(tokens);
1309 self.semi_token.to_tokens(tokens);
1310 }
1311 }
1312
1313 impl ToTokens for ItemStatic {
1314 fn to_tokens(&self, tokens: &mut Tokens) {
1315 tokens.append_all(self.attrs.outer());
1316 self.vis.to_tokens(tokens);
1317 self.static_token.to_tokens(tokens);
1318 self.mutbl.to_tokens(tokens);
1319 self.ident.to_tokens(tokens);
1320 self.colon_token.to_tokens(tokens);
1321 self.ty.to_tokens(tokens);
1322 self.eq_token.to_tokens(tokens);
1323 self.expr.to_tokens(tokens);
1324 self.semi_token.to_tokens(tokens);
1325 }
1326 }
1327
1328 impl ToTokens for ItemConst {
1329 fn to_tokens(&self, tokens: &mut Tokens) {
1330 tokens.append_all(self.attrs.outer());
1331 self.vis.to_tokens(tokens);
1332 self.const_token.to_tokens(tokens);
1333 self.ident.to_tokens(tokens);
1334 self.colon_token.to_tokens(tokens);
1335 self.ty.to_tokens(tokens);
1336 self.eq_token.to_tokens(tokens);
1337 self.expr.to_tokens(tokens);
1338 self.semi_token.to_tokens(tokens);
1339 }
1340 }
1341
1342 impl ToTokens for ItemFn {
1343 fn to_tokens(&self, tokens: &mut Tokens) {
1344 tokens.append_all(self.attrs.outer());
1345 self.vis.to_tokens(tokens);
1346 self.constness.to_tokens(tokens);
1347 self.unsafety.to_tokens(tokens);
1348 self.abi.to_tokens(tokens);
1349 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1350 self.block.brace_token.surround(tokens, |tokens| {
1351 tokens.append_all(self.attrs.inner());
1352 tokens.append_all(&self.block.stmts);
1353 });
1354 }
1355 }
1356
1357 impl ToTokens for ItemMod {
1358 fn to_tokens(&self, tokens: &mut Tokens) {
1359 tokens.append_all(self.attrs.outer());
1360 self.vis.to_tokens(tokens);
1361 self.mod_token.to_tokens(tokens);
1362 self.ident.to_tokens(tokens);
1363 if let Some((ref brace, ref items)) = self.content {
1364 brace.surround(tokens, |tokens| {
1365 tokens.append_all(self.attrs.inner());
1366 tokens.append_all(items);
1367 });
1368 } else {
1369 TokensOrDefault(&self.semi).to_tokens(tokens);
1370 }
1371 }
1372 }
1373
1374 impl ToTokens for ItemForeignMod {
1375 fn to_tokens(&self, tokens: &mut Tokens) {
1376 tokens.append_all(self.attrs.outer());
1377 self.abi.to_tokens(tokens);
1378 self.brace_token.surround(tokens, |tokens| {
1379 tokens.append_all(&self.items);
1380 });
1381 }
1382 }
1383
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001384 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001385 fn to_tokens(&self, tokens: &mut Tokens) {
1386 tokens.append_all(self.attrs.outer());
1387 self.vis.to_tokens(tokens);
1388 self.type_token.to_tokens(tokens);
1389 self.ident.to_tokens(tokens);
1390 self.generics.to_tokens(tokens);
1391 self.generics.where_clause.to_tokens(tokens);
1392 self.eq_token.to_tokens(tokens);
1393 self.ty.to_tokens(tokens);
1394 self.semi_token.to_tokens(tokens);
1395 }
1396 }
1397
1398 impl ToTokens for ItemEnum {
1399 fn to_tokens(&self, tokens: &mut Tokens) {
1400 tokens.append_all(self.attrs.outer());
1401 self.vis.to_tokens(tokens);
1402 self.enum_token.to_tokens(tokens);
1403 self.ident.to_tokens(tokens);
1404 self.generics.to_tokens(tokens);
1405 self.generics.where_clause.to_tokens(tokens);
1406 self.brace_token.surround(tokens, |tokens| {
1407 self.variants.to_tokens(tokens);
1408 });
1409 }
1410 }
1411
1412 impl ToTokens for ItemStruct {
1413 fn to_tokens(&self, tokens: &mut Tokens) {
1414 tokens.append_all(self.attrs.outer());
1415 self.vis.to_tokens(tokens);
1416 self.struct_token.to_tokens(tokens);
1417 self.ident.to_tokens(tokens);
1418 self.generics.to_tokens(tokens);
1419 match self.data {
1420 VariantData::Struct(..) => {
1421 self.generics.where_clause.to_tokens(tokens);
1422 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001423 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001424 VariantData::Tuple(..) => {
1425 self.data.to_tokens(tokens);
1426 self.generics.where_clause.to_tokens(tokens);
1427 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001428 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001429 VariantData::Unit => {
1430 self.generics.where_clause.to_tokens(tokens);
1431 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001432 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001433 }
1434 }
1435 }
1436
1437 impl ToTokens for ItemUnion {
1438 fn to_tokens(&self, tokens: &mut Tokens) {
1439 tokens.append_all(self.attrs.outer());
1440 self.vis.to_tokens(tokens);
1441 self.union_token.to_tokens(tokens);
1442 self.ident.to_tokens(tokens);
1443 self.generics.to_tokens(tokens);
1444 self.generics.where_clause.to_tokens(tokens);
1445 // XXX: Should we handle / complain when using a
1446 // non-VariantData::Struct Variant in Union?
1447 self.data.to_tokens(tokens);
1448 }
1449 }
1450
1451 impl ToTokens for ItemTrait {
1452 fn to_tokens(&self, tokens: &mut Tokens) {
1453 tokens.append_all(self.attrs.outer());
1454 self.vis.to_tokens(tokens);
1455 self.unsafety.to_tokens(tokens);
1456 self.trait_token.to_tokens(tokens);
1457 self.ident.to_tokens(tokens);
1458 self.generics.to_tokens(tokens);
1459 if !self.supertraits.is_empty() {
1460 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1461 self.supertraits.to_tokens(tokens);
1462 }
1463 self.generics.where_clause.to_tokens(tokens);
1464 self.brace_token.surround(tokens, |tokens| {
1465 tokens.append_all(&self.items);
1466 });
1467 }
1468 }
1469
1470 impl ToTokens for ItemDefaultImpl {
1471 fn to_tokens(&self, tokens: &mut Tokens) {
1472 tokens.append_all(self.attrs.outer());
1473 self.unsafety.to_tokens(tokens);
1474 self.impl_token.to_tokens(tokens);
1475 self.path.to_tokens(tokens);
1476 self.for_token.to_tokens(tokens);
1477 self.dot2_token.to_tokens(tokens);
1478 self.brace_token.surround(tokens, |_tokens| {});
1479 }
1480 }
1481
1482 impl ToTokens for ItemImpl {
1483 fn to_tokens(&self, tokens: &mut Tokens) {
1484 tokens.append_all(self.attrs.outer());
1485 self.defaultness.to_tokens(tokens);
1486 self.unsafety.to_tokens(tokens);
1487 self.impl_token.to_tokens(tokens);
1488 self.generics.to_tokens(tokens);
1489 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1490 polarity.to_tokens(tokens);
1491 path.to_tokens(tokens);
1492 for_token.to_tokens(tokens);
1493 }
1494 self.self_ty.to_tokens(tokens);
1495 self.generics.where_clause.to_tokens(tokens);
1496 self.brace_token.surround(tokens, |tokens| {
1497 tokens.append_all(&self.items);
1498 });
1499 }
1500 }
1501
1502 impl ToTokens for ItemMacro {
1503 fn to_tokens(&self, tokens: &mut Tokens) {
1504 tokens.append_all(self.attrs.outer());
1505 self.mac.path.to_tokens(tokens);
1506 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001507 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001508 tokens.append_all(&self.mac.tokens);
1509 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001510 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001511 }
1512 }
1513 }
David Tolnay42602292016-10-01 22:25:45 -07001514
Alex Crichton62a0a592017-05-22 13:58:53 -07001515 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001516 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001517 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001518 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001519 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001520 self.rename.to_tokens(tokens);
1521 }
David Tolnay4a057422016-10-08 00:02:31 -07001522 }
1523 }
1524
Alex Crichton62a0a592017-05-22 13:58:53 -07001525 impl ToTokens for PathGlob {
1526 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001527 if self.path.segments.len() > 0 {
1528 self.path.to_tokens(tokens);
1529 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1530 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001531 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001532 }
1533 }
1534
1535 impl ToTokens for PathList {
1536 fn to_tokens(&self, tokens: &mut Tokens) {
1537 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001538 self.colon2_token.to_tokens(tokens);
1539 self.brace_token.surround(tokens, |tokens| {
1540 self.items.to_tokens(tokens);
1541 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001542 }
1543 }
1544
David Tolnay4a057422016-10-08 00:02:31 -07001545 impl ToTokens for PathListItem {
1546 fn to_tokens(&self, tokens: &mut Tokens) {
1547 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001548 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001549 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001550 self.rename.to_tokens(tokens);
1551 }
David Tolnay4a057422016-10-08 00:02:31 -07001552 }
1553 }
1554
David Tolnay1bfa7332017-11-11 12:41:20 -08001555 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001556 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001557 tokens.append_all(self.attrs.outer());
1558 self.const_token.to_tokens(tokens);
1559 self.ident.to_tokens(tokens);
1560 self.colon_token.to_tokens(tokens);
1561 self.ty.to_tokens(tokens);
1562 if let Some((ref eq_token, ref default)) = self.default {
1563 eq_token.to_tokens(tokens);
1564 default.to_tokens(tokens);
1565 }
1566 self.semi_token.to_tokens(tokens);
1567 }
1568 }
1569
1570 impl ToTokens for TraitItemMethod {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 tokens.append_all(self.attrs.outer());
1573 self.sig.to_tokens(tokens);
1574 match self.default {
1575 Some(ref block) => {
1576 block.brace_token.surround(tokens, |tokens| {
1577 tokens.append_all(self.attrs.inner());
1578 tokens.append_all(&block.stmts);
1579 });
David Tolnayca085422016-10-04 00:12:38 -07001580 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001581 None => {
1582 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001583 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001584 }
1585 }
1586 }
1587
1588 impl ToTokens for TraitItemType {
1589 fn to_tokens(&self, tokens: &mut Tokens) {
1590 tokens.append_all(self.attrs.outer());
1591 self.type_token.to_tokens(tokens);
1592 self.ident.to_tokens(tokens);
1593 if !self.bounds.is_empty() {
1594 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1595 self.bounds.to_tokens(tokens);
1596 }
1597 if let Some((ref eq_token, ref default)) = self.default {
1598 eq_token.to_tokens(tokens);
1599 default.to_tokens(tokens);
1600 }
1601 self.semi_token.to_tokens(tokens);
1602 }
1603 }
1604
1605 impl ToTokens for TraitItemMacro {
1606 fn to_tokens(&self, tokens: &mut Tokens) {
1607 tokens.append_all(self.attrs.outer());
1608 self.mac.to_tokens(tokens);
1609 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001610 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001611 }
1612 }
1613 }
1614
David Tolnay857628c2017-11-11 12:25:31 -08001615 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001616 fn to_tokens(&self, tokens: &mut Tokens) {
1617 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001618 self.vis.to_tokens(tokens);
1619 self.defaultness.to_tokens(tokens);
1620 self.const_token.to_tokens(tokens);
1621 self.ident.to_tokens(tokens);
1622 self.colon_token.to_tokens(tokens);
1623 self.ty.to_tokens(tokens);
1624 self.eq_token.to_tokens(tokens);
1625 self.expr.to_tokens(tokens);
1626 self.semi_token.to_tokens(tokens);
1627 }
1628 }
1629
1630 impl ToTokens for ImplItemMethod {
1631 fn to_tokens(&self, tokens: &mut Tokens) {
1632 tokens.append_all(self.attrs.outer());
1633 self.vis.to_tokens(tokens);
1634 self.defaultness.to_tokens(tokens);
1635 self.sig.to_tokens(tokens);
1636 self.block.brace_token.surround(tokens, |tokens| {
1637 tokens.append_all(self.attrs.inner());
1638 tokens.append_all(&self.block.stmts);
1639 });
1640 }
1641 }
1642
1643 impl ToTokens for ImplItemType {
1644 fn to_tokens(&self, tokens: &mut Tokens) {
1645 tokens.append_all(self.attrs.outer());
1646 self.vis.to_tokens(tokens);
1647 self.defaultness.to_tokens(tokens);
1648 self.type_token.to_tokens(tokens);
1649 self.ident.to_tokens(tokens);
1650 self.eq_token.to_tokens(tokens);
1651 self.ty.to_tokens(tokens);
1652 self.semi_token.to_tokens(tokens);
1653 }
1654 }
1655
1656 impl ToTokens for ImplItemMacro {
1657 fn to_tokens(&self, tokens: &mut Tokens) {
1658 tokens.append_all(self.attrs.outer());
1659 self.mac.to_tokens(tokens);
1660 if !self.mac.is_braced() {
1661 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001662 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001663 }
1664 }
1665 }
1666
David Tolnay8894f602017-11-11 12:11:04 -08001667 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001670 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001671 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1672 self.semi_token.to_tokens(tokens);
1673 }
1674 }
1675
1676 impl ToTokens for ForeignItemStatic {
1677 fn to_tokens(&self, tokens: &mut Tokens) {
1678 tokens.append_all(self.attrs.outer());
1679 self.vis.to_tokens(tokens);
1680 self.static_token.to_tokens(tokens);
1681 self.mutbl.to_tokens(tokens);
1682 self.ident.to_tokens(tokens);
1683 self.colon_token.to_tokens(tokens);
1684 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001685 self.semi_token.to_tokens(tokens);
1686 }
1687 }
1688
David Tolnay570695e2017-06-03 16:15:13 -07001689 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001690 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001691 self.constness.to_tokens(tokens);
1692 self.unsafety.to_tokens(tokens);
1693 self.abi.to_tokens(tokens);
1694 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001695 }
1696 }
1697
David Tolnay570695e2017-06-03 16:15:13 -07001698 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001699
1700 impl<'a> ToTokens for NamedDecl<'a> {
1701 fn to_tokens(&self, tokens: &mut Tokens) {
1702 self.0.fn_token.to_tokens(tokens);
1703 self.1.to_tokens(tokens);
1704 self.0.generics.to_tokens(tokens);
1705 self.0.paren_token.surround(tokens, |tokens| {
1706 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001707
1708 if self.0.variadic {
1709 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001710 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001711 }
Alex Crichton259ee532017-07-14 06:51:02 -07001712 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001713 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001714 });
1715 self.0.output.to_tokens(tokens);
1716 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001717 }
1718 }
1719
Alex Crichton62a0a592017-05-22 13:58:53 -07001720 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001721 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001722 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001723 self.lifetime.to_tokens(tokens);
1724 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001726 }
1727 }
1728
1729 impl ToTokens for ArgSelf {
1730 fn to_tokens(&self, tokens: &mut Tokens) {
1731 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001732 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001733 }
1734 }
1735
1736 impl ToTokens for ArgCaptured {
1737 fn to_tokens(&self, tokens: &mut Tokens) {
1738 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001739 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001740 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001741 }
1742 }
1743
David Tolnay42602292016-10-01 22:25:45 -07001744 impl ToTokens for Constness {
1745 fn to_tokens(&self, tokens: &mut Tokens) {
1746 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001748 Constness::NotConst => {
1749 // nothing
1750 }
David Tolnay42602292016-10-01 22:25:45 -07001751 }
1752 }
1753 }
1754
David Tolnay4c9be372016-10-06 00:47:37 -07001755 impl ToTokens for Defaultness {
1756 fn to_tokens(&self, tokens: &mut Tokens) {
1757 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001759 Defaultness::Final => {
1760 // nothing
1761 }
1762 }
1763 }
1764 }
1765
1766 impl ToTokens for ImplPolarity {
1767 fn to_tokens(&self, tokens: &mut Tokens) {
1768 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001769 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001770 ImplPolarity::Positive => {
1771 // nothing
1772 }
1773 }
1774 }
1775 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001776}