blob: ff3638027b24e0873b7b1900bc7c99653e7ee004 [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 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800300 /// A foreign type
301 pub Type(ForeignItemType {
302 pub attrs: Vec<Attribute>,
303 pub vis: Visibility,
304 pub type_token: Token![type],
305 pub ident: Ident,
306 pub semi_token: Token![;],
307 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700309}
310
David Tolnayda705bd2017-11-10 21:58:05 -0800311ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700312 /// Represents an item declaration within a trait declaration,
313 /// possibly including a default implementation. A trait item is
314 /// either required (meaning it doesn't have an implementation, just a
315 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800316 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800318 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700320 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800322 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800323 pub default: Option<(Token![=], Expr)>,
324 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 }),
326 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800327 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700328 pub sig: MethodSig,
329 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800330 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 }),
332 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800333 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800334 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700335 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800336 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800337 pub bounds: Delimited<TypeParamBound, Token![+]>,
338 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800339 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800341 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800342 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800343 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800344 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700345 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700346}
347
348ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700349 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 pub enum ImplPolarity {
351 /// `impl Trait for Type`
352 Positive,
353 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800354 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 }
356}
357
Alex Crichton62a0a592017-05-22 13:58:53 -0700358ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800359 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700360 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800361 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700362 pub vis: Visibility,
363 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700365 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800367 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }),
372 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800373 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700374 pub vis: Visibility,
375 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 pub sig: MethodSig,
377 pub block: Block,
378 }),
379 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800380 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700381 pub vis: Visibility,
382 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800383 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700384 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800386 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800387 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 }),
David Tolnay857628c2017-11-11 12:25:31 -0800389 pub Macro(ImplItemMacro {
390 pub attrs: Vec<Attribute>,
391 pub mac: Macro,
392 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700393 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700394}
395
396ast_struct! {
397 /// Represents a method's signature in a trait declaration,
398 /// or in an implementation.
399 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700401 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700403 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }
406}
407
408ast_struct! {
409 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700410 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 /// E.g. `fn foo(bar: baz)`
412 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800413 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700414 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800415 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800416 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700417 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700418 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800419 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700420 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700421}
422
Alex Crichton62a0a592017-05-22 13:58:53 -0700423ast_enum_of_structs! {
424 /// An argument in a function header.
425 ///
426 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
427 pub enum FnArg {
428 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub and_token: Token![&],
430 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub lifetime: Option<Lifetime>,
432 pub mutbl: Mutability,
433 }),
434 pub SelfValue(ArgSelf {
435 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800436 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700437 }),
438 pub Captured(ArgCaptured {
439 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800441 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700442 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800443 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700444 }
David Tolnay62f374c2016-10-02 13:37:00 -0700445}
446
David Tolnayedf2b992016-09-23 20:43:45 -0700447#[cfg(feature = "parsing")]
448pub mod parsing {
449 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700450
Michael Layzell92639a52017-06-01 00:07:44 -0400451 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700452
David Tolnay4c614be2017-11-10 00:02:38 -0800453 impl_synom!(Item "item" alt!(
454 syn!(ItemExternCrate) => { Item::ExternCrate }
455 |
456 syn!(ItemUse) => { Item::Use }
457 |
458 syn!(ItemStatic) => { Item::Static }
459 |
460 syn!(ItemConst) => { Item::Const }
461 |
462 syn!(ItemFn) => { Item::Fn }
463 |
464 syn!(ItemMod) => { Item::Mod }
465 |
466 syn!(ItemForeignMod) => { Item::ForeignMod }
467 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800468 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800469 |
470 syn!(ItemStruct) => { Item::Struct }
471 |
472 syn!(ItemEnum) => { Item::Enum }
473 |
474 syn!(ItemUnion) => { Item::Union }
475 |
476 syn!(ItemTrait) => { Item::Trait }
477 |
478 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
479 |
480 syn!(ItemImpl) => { Item::Impl }
481 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800482 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800483 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700484
David Tolnaydecf28d2017-11-11 11:56:45 -0800485 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700486 attrs: many0!(call!(Attribute::parse_outer)) >>
487 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800488 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700489 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700490 body: call!(::TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800491 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800492 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700493 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800494 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800495 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500496 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700497 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700498 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800499 },
David Tolnay4c614be2017-11-10 00:02:38 -0800500 })
David Tolnayedf2b992016-09-23 20:43:45 -0700501 ));
502
David Tolnay4c614be2017-11-10 00:02:38 -0800503 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700504 attrs: many0!(call!(Attribute::parse_outer)) >>
505 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 extern_: keyword!(extern) >>
507 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700508 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
510 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800511 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700512 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800513 vis: vis,
514 extern_token: extern_,
515 crate_token: crate_,
516 ident: ident,
517 rename: rename,
518 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800519 })
David Tolnayedf2b992016-09-23 20:43:45 -0700520 ));
521
David Tolnay4c614be2017-11-10 00:02:38 -0800522 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700523 attrs: many0!(call!(Attribute::parse_outer)) >>
524 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700526 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800527 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800528 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700529 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800530 vis: vis,
531 use_token: use_,
532 path: Box::new(what),
533 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800534 })
David Tolnay4a057422016-10-08 00:02:31 -0700535 ));
536
Alex Crichton954046c2017-05-30 21:49:42 -0700537 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400538 named!(parse -> Self, alt!(
539 syn!(PathGlob) => { ViewPath::Glob }
540 |
541 syn!(PathList) => { ViewPath::List }
542 |
543 syn!(PathSimple) => { ViewPath::Simple } // must be last
544 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700545 }
David Tolnay4a057422016-10-08 00:02:31 -0700546
Alex Crichton954046c2017-05-30 21:49:42 -0700547 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400548 named!(parse -> Self, do_parse!(
549 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800550 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400551 (PathSimple {
552 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800553 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400554 rename: rename.map(|p| p.1),
555 })
556 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700557 }
David Tolnay4a057422016-10-08 00:02:31 -0700558
Alex Crichton954046c2017-05-30 21:49:42 -0700559 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400560 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700561 path: option!(do_parse!(
562 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800563 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700564 (path, colon2)
565 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800566 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700567 ({
568 match path {
569 Some((path, colon2)) => {
570 PathGlob {
571 path: path,
572 colon2_token: Some(colon2),
573 star_token: star,
574 }
575 }
576 None => {
577 PathGlob {
578 path: Path {
579 leading_colon: None,
580 segments: Default::default(),
581 },
582 colon2_token: None,
583 star_token: star,
584 }
585 }
586 }
Michael Layzell92639a52017-06-01 00:07:44 -0400587 })
588 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700589 }
David Tolnay4a057422016-10-08 00:02:31 -0700590
Alex Crichton954046c2017-05-30 21:49:42 -0700591 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400592 named!(parse -> Self, alt!(
593 do_parse!(
594 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400596 items: braces!(call!(Delimited::parse_terminated)) >>
597 (PathList {
598 path: path,
599 items: items.0,
600 brace_token: items.1,
601 colon2_token: colon2,
602 })
603 )
604 |
605 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400607 items: braces!(call!(Delimited::parse_terminated)) >>
608 (PathList {
609 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400610 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700611 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400612 },
David Tolnay570695e2017-06-03 16:15:13 -0700613 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400614 brace_token: items.1,
615 items: items.0,
616 })
617 )
618 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700619 }
David Tolnay4a057422016-10-08 00:02:31 -0700620
Alex Crichton954046c2017-05-30 21:49:42 -0700621 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400622 named!(parse -> Self, do_parse!(
623 name: alt!(
624 syn!(Ident)
625 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800626 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400627 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400629 (PathListItem {
630 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800631 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400632 rename: rename.map(|p| p.1),
633 })
634 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700635 }
David Tolnay4a057422016-10-08 00:02:31 -0700636
David Tolnay4c614be2017-11-10 00:02:38 -0800637 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700638 attrs: many0!(call!(Attribute::parse_outer)) >>
639 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800640 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700641 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700642 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800643 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800644 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800645 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700646 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800647 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800648 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700649 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800650 vis: vis,
651 static_token: static_,
652 mutbl: mutability,
653 ident: ident,
654 colon_token: colon,
655 ty: Box::new(ty),
656 eq_token: eq,
657 expr: Box::new(value),
658 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800659 })
David Tolnay47a877c2016-10-01 16:50:55 -0700660 ));
661
David Tolnay4c614be2017-11-10 00:02:38 -0800662 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700663 attrs: many0!(call!(Attribute::parse_outer)) >>
664 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700666 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800667 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800668 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800669 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700670 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800671 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800672 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700673 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800674 vis: vis,
675 const_token: const_,
676 ident: ident,
677 colon_token: colon,
678 ty: Box::new(ty),
679 eq_token: eq,
680 expr: Box::new(value),
681 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800682 })
David Tolnay47a877c2016-10-01 16:50:55 -0700683 ));
684
David Tolnay4c614be2017-11-10 00:02:38 -0800685 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700686 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
687 vis: syn!(Visibility) >>
688 constness: syn!(Constness) >>
689 unsafety: syn!(Unsafety) >>
690 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800691 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700692 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700693 generics: syn!(Generics) >>
694 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800695 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700696 where_clause: syn!(WhereClause) >>
697 inner_attrs_stmts: braces!(tuple!(
698 many0!(call!(Attribute::parse_inner)),
699 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400700 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800701 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700702 attrs: {
703 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700704 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700705 attrs
706 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800707 vis: vis,
708 constness: constness,
709 unsafety: unsafety,
710 abi: abi,
711 decl: Box::new(FnDecl {
712 dot_tokens: None,
713 fn_token: fn_,
714 paren_token: inputs.1,
715 inputs: inputs.0,
716 output: ret,
717 variadic: false,
718 generics: Generics {
719 where_clause: where_clause,
720 .. generics
721 },
722 }),
723 ident: ident,
724 block: Box::new(Block {
725 brace_token: inner_attrs_stmts.1,
726 stmts: (inner_attrs_stmts.0).1,
727 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800728 })
David Tolnay42602292016-10-01 22:25:45 -0700729 ));
730
Alex Crichton954046c2017-05-30 21:49:42 -0700731 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400732 named!(parse -> Self, alt!(
733 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400735 lt: option!(syn!(Lifetime)) >>
736 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800737 self_: keyword!(self) >>
738 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400739 (ArgSelfRef {
740 lifetime: lt,
741 mutbl: mutability,
742 and_token: and,
743 self_token: self_,
744 }.into())
745 )
746 |
747 do_parse!(
748 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800749 self_: keyword!(self) >>
750 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400751 (ArgSelf {
752 mutbl: mutability,
753 self_token: self_,
754 }.into())
755 )
756 |
757 do_parse!(
758 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800759 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800760 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400761 (ArgCaptured {
762 pat: pat,
763 ty: ty,
764 colon_token: colon,
765 }.into())
766 )
767 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800768 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400769 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700770 }
David Tolnay62f374c2016-10-02 13:37:00 -0700771
David Tolnay4c614be2017-11-10 00:02:38 -0800772 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700773 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
774 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800775 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700776 ident: syn!(Ident) >>
777 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700779 Vec::new(),
780 None,
781 Some(semi),
782 )}
David Tolnay37d10332016-10-13 20:51:04 -0700783 |
Alex Crichton954046c2017-05-30 21:49:42 -0700784 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700785 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700786 many0!(call!(Attribute::parse_inner)),
787 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400788 )
David Tolnay570695e2017-06-03 16:15:13 -0700789 ) => {|((inner_attrs, items), brace)| (
790 inner_attrs,
791 Some((brace, items)),
792 None,
793 )}
David Tolnay37d10332016-10-13 20:51:04 -0700794 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800795 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700796 attrs: {
797 let mut attrs = outer_attrs;
798 attrs.extend(content_semi.0);
799 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700800 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800801 vis: vis,
802 mod_token: mod_,
803 ident: ident,
804 content: content_semi.1,
805 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800806 })
David Tolnay35902302016-10-06 01:11:08 -0700807 ));
808
David Tolnay4c614be2017-11-10 00:02:38 -0800809 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700810 attrs: many0!(call!(Attribute::parse_outer)) >>
811 abi: syn!(Abi) >>
812 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800813 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700814 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800815 abi: abi,
816 brace_token: items.1,
817 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800818 })
David Tolnay35902302016-10-06 01:11:08 -0700819 ));
820
David Tolnay8894f602017-11-11 12:11:04 -0800821 impl_synom!(ForeignItem "foreign item" alt!(
822 syn!(ForeignItemFn) => { ForeignItem::Fn }
823 |
824 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800825 |
826 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800827 ));
David Tolnay35902302016-10-06 01:11:08 -0700828
David Tolnay8894f602017-11-11 12:11:04 -0800829 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700830 attrs: many0!(call!(Attribute::parse_outer)) >>
831 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800832 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700833 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700834 generics: syn!(Generics) >>
835 inputs: parens!(do_parse!(
836 args: call!(Delimited::parse_terminated) >>
837 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800838 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700839 (args, variadic)
840 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800841 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700842 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800843 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700844 ({
845 let ((inputs, variadic), parens) = inputs;
846 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800847 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700848 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700849 attrs: attrs,
850 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800851 decl: Box::new(FnDecl {
852 fn_token: fn_,
853 paren_token: parens,
854 inputs: inputs,
855 variadic: variadic.is_some(),
856 dot_tokens: variadic,
857 output: ret,
858 generics: Generics {
859 where_clause: where_clause,
860 .. generics
861 },
862 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700863 vis: vis,
864 }
David Tolnay35902302016-10-06 01:11:08 -0700865 })
866 ));
867
David Tolnay8894f602017-11-11 12:11:04 -0800868 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700869 attrs: many0!(call!(Attribute::parse_outer)) >>
870 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800871 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700872 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700873 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800875 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800876 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800877 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700878 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700879 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700880 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800881 ty: Box::new(ty),
882 mutbl: mutability,
883 static_token: static_,
884 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700885 vis: vis,
886 })
887 ));
888
David Tolnay199bcbb2017-11-12 10:33:52 -0800889 impl_synom!(ForeignItemType "foreign type" do_parse!(
890 attrs: many0!(call!(Attribute::parse_outer)) >>
891 vis: syn!(Visibility) >>
892 type_: keyword!(type) >>
893 ident: syn!(Ident) >>
894 semi: punct!(;) >>
895 (ForeignItemType {
896 attrs: attrs,
897 vis: vis,
898 type_token: type_,
899 ident: ident,
900 semi_token: semi,
901 })
902 ));
903
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800904 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700905 attrs: many0!(call!(Attribute::parse_outer)) >>
906 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800907 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700908 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700909 generics: syn!(Generics) >>
910 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800911 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800912 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800913 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800914 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700915 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800916 vis: vis,
917 type_token: type_,
918 ident: ident,
919 generics: Generics {
920 where_clause: where_clause,
921 ..generics
922 },
923 eq_token: eq,
924 ty: Box::new(ty),
925 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800926 })
David Tolnay3cf52982016-10-01 17:11:37 -0700927 ));
928
David Tolnay4c614be2017-11-10 00:02:38 -0800929 impl_synom!(ItemStruct "struct item" switch!(
930 map!(syn!(DeriveInput), Into::into),
931 Item::Struct(item) => value!(item)
932 |
933 _ => reject!()
934 ));
David Tolnay42602292016-10-01 22:25:45 -0700935
David Tolnay4c614be2017-11-10 00:02:38 -0800936 impl_synom!(ItemEnum "enum item" switch!(
937 map!(syn!(DeriveInput), Into::into),
938 Item::Enum(item) => value!(item)
939 |
940 _ => reject!()
941 ));
942
943 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700944 attrs: many0!(call!(Attribute::parse_outer)) >>
945 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800946 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700947 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700948 generics: syn!(Generics) >>
949 where_clause: syn!(WhereClause) >>
950 fields: braces!(call!(Delimited::parse_terminated_with,
951 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800952 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700953 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800954 vis: vis,
955 union_token: union_,
956 ident: ident,
957 generics: Generics {
958 where_clause: where_clause,
959 .. generics
960 },
961 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800962 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700963 ));
964
David Tolnay4c614be2017-11-10 00:02:38 -0800965 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700966 attrs: many0!(call!(Attribute::parse_outer)) >>
967 vis: syn!(Visibility) >>
968 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700970 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700971 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800972 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700973 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700974 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700975 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700976 where_clause: syn!(WhereClause) >>
977 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800978 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700979 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800980 vis: vis,
981 unsafety: unsafety,
982 trait_token: trait_,
983 ident: ident,
984 generics: Generics {
985 where_clause: where_clause,
986 .. generics
987 },
988 colon_token: colon,
989 supertraits: bounds.unwrap_or_default(),
990 brace_token: body.1,
991 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800992 })
David Tolnay0aecb732016-10-03 23:03:50 -0700993 ));
994
David Tolnay4c614be2017-11-10 00:02:38 -0800995 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700996 attrs: many0!(call!(Attribute::parse_outer)) >>
997 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800998 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700999 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001000 for_: keyword!(for) >>
1001 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001002 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001003 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001004 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001005 unsafety: unsafety,
1006 impl_token: impl_,
1007 path: path,
1008 for_token: for_,
1009 dot2_token: dot2,
1010 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001011 })
David Tolnayf94e2362016-10-04 00:29:51 -07001012 ));
1013
David Tolnayda705bd2017-11-10 21:58:05 -08001014 impl_synom!(TraitItem "trait item" alt!(
1015 syn!(TraitItemConst) => { TraitItem::Const }
1016 |
1017 syn!(TraitItemMethod) => { TraitItem::Method }
1018 |
1019 syn!(TraitItemType) => { TraitItem::Type }
1020 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001021 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001022 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001023
David Tolnayda705bd2017-11-10 21:58:05 -08001024 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001025 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001026 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001027 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001028 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001029 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001030 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1031 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001032 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001033 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001034 const_token: const_,
1035 ident: ident,
1036 colon_token: colon,
1037 ty: ty,
1038 default: default,
1039 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001040 })
1041 ));
1042
David Tolnayda705bd2017-11-10 21:58:05 -08001043 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001044 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1045 constness: syn!(Constness) >>
1046 unsafety: syn!(Unsafety) >>
1047 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001048 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001049 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001050 generics: syn!(Generics) >>
1051 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001052 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001053 where_clause: syn!(WhereClause) >>
1054 body: option!(braces!(
1055 tuple!(many0!(call!(Attribute::parse_inner)),
1056 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001057 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001058 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001059 ({
1060 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001061 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001062 None => (Vec::new(), None),
1063 };
David Tolnayda705bd2017-11-10 21:58:05 -08001064 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001065 attrs: {
1066 let mut attrs = outer_attrs;
1067 attrs.extend(inner_attrs);
1068 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001069 },
David Tolnayda705bd2017-11-10 21:58:05 -08001070 sig: MethodSig {
1071 constness: constness,
1072 unsafety: unsafety,
1073 abi: abi,
1074 ident: ident,
1075 decl: FnDecl {
1076 inputs: inputs.0,
1077 output: ret,
1078 variadic: false,
1079 fn_token: fn_,
1080 paren_token: inputs.1,
1081 dot_tokens: None,
1082 generics: Generics {
1083 where_clause: where_clause,
1084 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001085 },
1086 },
David Tolnayda705bd2017-11-10 21:58:05 -08001087 },
1088 default: stmts.map(|stmts| {
1089 Block {
1090 stmts: stmts.0,
1091 brace_token: stmts.1,
1092 }
1093 }),
1094 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001095 }
David Tolnay0aecb732016-10-03 23:03:50 -07001096 })
1097 ));
1098
David Tolnayda705bd2017-11-10 21:58:05 -08001099 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001100 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001101 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001102 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001103 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001104 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001105 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001106 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001107 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001108 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001109 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001110 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001111 type_token: type_,
1112 ident: ident,
1113 colon_token: colon,
1114 bounds: bounds.unwrap_or_default(),
1115 default: default,
1116 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001117 })
1118 ));
1119
David Tolnaydecf28d2017-11-11 11:56:45 -08001120 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001121 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001122 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001123 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001124 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001125 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001126 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001127 })
1128 ));
1129
David Tolnay4c614be2017-11-10 00:02:38 -08001130 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001131 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001132 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001133 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001134 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001135 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001136 polarity_path: alt!(
1137 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001138 polarity: syn!(ImplPolarity) >>
1139 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001140 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001141 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001142 )
1143 |
David Tolnay570695e2017-06-03 16:15:13 -07001144 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001145 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001146 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001147 where_clause: syn!(WhereClause) >>
1148 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001149 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001150 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001151 defaultness: defaultness,
1152 unsafety: unsafety,
1153 impl_token: impl_,
1154 generics: Generics {
1155 where_clause: where_clause,
1156 .. generics
1157 },
1158 trait_: polarity_path,
1159 self_ty: Box::new(self_ty),
1160 brace_token: body.1,
1161 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001162 })
David Tolnay4c9be372016-10-06 00:47:37 -07001163 ));
1164
David Tolnay857628c2017-11-11 12:25:31 -08001165 impl_synom!(ImplItem "item in impl block" alt!(
1166 syn!(ImplItemConst) => { ImplItem::Const }
1167 |
1168 syn!(ImplItemMethod) => { ImplItem::Method }
1169 |
1170 syn!(ImplItemType) => { ImplItem::Type }
1171 |
1172 syn!(ImplItemMacro) => { ImplItem::Macro }
1173 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001174
David Tolnay857628c2017-11-11 12:25:31 -08001175 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001176 attrs: many0!(call!(Attribute::parse_outer)) >>
1177 vis: syn!(Visibility) >>
1178 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001180 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001181 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001182 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001184 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001185 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001186 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001187 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001188 vis: vis,
1189 defaultness: defaultness,
1190 const_token: const_,
1191 ident: ident,
1192 colon_token: colon,
1193 ty: ty,
1194 eq_token: eq,
1195 expr: value,
1196 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001197 })
1198 ));
1199
David Tolnay857628c2017-11-11 12:25:31 -08001200 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001201 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1202 vis: syn!(Visibility) >>
1203 defaultness: syn!(Defaultness) >>
1204 constness: syn!(Constness) >>
1205 unsafety: syn!(Unsafety) >>
1206 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001207 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001208 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001209 generics: syn!(Generics) >>
1210 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001211 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001212 where_clause: syn!(WhereClause) >>
1213 inner_attrs_stmts: braces!(tuple!(
1214 many0!(call!(Attribute::parse_inner)),
1215 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001216 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001217 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001218 attrs: {
1219 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001220 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001221 attrs
1222 },
David Tolnay857628c2017-11-11 12:25:31 -08001223 vis: vis,
1224 defaultness: defaultness,
1225 sig: MethodSig {
1226 constness: constness,
1227 unsafety: unsafety,
1228 abi: abi,
1229 ident: ident,
1230 decl: FnDecl {
1231 fn_token: fn_,
1232 paren_token: inputs.1,
1233 inputs: inputs.0,
1234 output: ret,
1235 variadic: false,
1236 generics: Generics {
1237 where_clause: where_clause,
1238 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001239 },
David Tolnay857628c2017-11-11 12:25:31 -08001240 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001241 },
David Tolnay857628c2017-11-11 12:25:31 -08001242 },
1243 block: Block {
1244 brace_token: inner_attrs_stmts.1,
1245 stmts: (inner_attrs_stmts.0).1,
1246 },
David Tolnay4c9be372016-10-06 00:47:37 -07001247 })
1248 ));
1249
David Tolnay857628c2017-11-11 12:25:31 -08001250 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001251 attrs: many0!(call!(Attribute::parse_outer)) >>
1252 vis: syn!(Visibility) >>
1253 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001254 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001255 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001256 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001257 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001258 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001259 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001260 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001261 vis: vis,
1262 defaultness: defaultness,
1263 type_token: type_,
1264 ident: ident,
1265 eq_token: eq,
1266 ty: ty,
1267 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001268 })
1269 ));
1270
David Tolnay857628c2017-11-11 12:25:31 -08001271 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001272 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001273 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001274 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001275 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001276 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001277 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001278 })
1279 ));
1280
Alex Crichton954046c2017-05-30 21:49:42 -07001281 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001282 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001283 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001284 |
1285 epsilon!() => { |_| ImplPolarity::Positive }
1286 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001287 }
David Tolnay4c9be372016-10-06 00:47:37 -07001288
Alex Crichton954046c2017-05-30 21:49:42 -07001289 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001290 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001291 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001292 |
1293 epsilon!() => { |_| Constness::NotConst }
1294 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001295 }
David Tolnay42602292016-10-01 22:25:45 -07001296
Alex Crichton954046c2017-05-30 21:49:42 -07001297 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001298 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001299 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001300 |
1301 epsilon!() => { |_| Defaultness::Final }
1302 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001303 }
David Tolnayedf2b992016-09-23 20:43:45 -07001304}
David Tolnay4a51dc72016-10-01 00:40:31 -07001305
1306#[cfg(feature = "printing")]
1307mod printing {
1308 use super::*;
1309 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001310 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001311 use quote::{Tokens, ToTokens};
1312
David Tolnay1bfa7332017-11-11 12:41:20 -08001313 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001314 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001315 tokens.append_all(self.attrs.outer());
1316 self.vis.to_tokens(tokens);
1317 self.extern_token.to_tokens(tokens);
1318 self.crate_token.to_tokens(tokens);
1319 self.ident.to_tokens(tokens);
1320 if let Some((ref as_token, ref rename)) = self.rename {
1321 as_token.to_tokens(tokens);
1322 rename.to_tokens(tokens);
1323 }
1324 self.semi_token.to_tokens(tokens);
1325 }
1326 }
1327
1328 impl ToTokens for ItemUse {
1329 fn to_tokens(&self, tokens: &mut Tokens) {
1330 tokens.append_all(self.attrs.outer());
1331 self.vis.to_tokens(tokens);
1332 self.use_token.to_tokens(tokens);
1333 self.path.to_tokens(tokens);
1334 self.semi_token.to_tokens(tokens);
1335 }
1336 }
1337
1338 impl ToTokens for ItemStatic {
1339 fn to_tokens(&self, tokens: &mut Tokens) {
1340 tokens.append_all(self.attrs.outer());
1341 self.vis.to_tokens(tokens);
1342 self.static_token.to_tokens(tokens);
1343 self.mutbl.to_tokens(tokens);
1344 self.ident.to_tokens(tokens);
1345 self.colon_token.to_tokens(tokens);
1346 self.ty.to_tokens(tokens);
1347 self.eq_token.to_tokens(tokens);
1348 self.expr.to_tokens(tokens);
1349 self.semi_token.to_tokens(tokens);
1350 }
1351 }
1352
1353 impl ToTokens for ItemConst {
1354 fn to_tokens(&self, tokens: &mut Tokens) {
1355 tokens.append_all(self.attrs.outer());
1356 self.vis.to_tokens(tokens);
1357 self.const_token.to_tokens(tokens);
1358 self.ident.to_tokens(tokens);
1359 self.colon_token.to_tokens(tokens);
1360 self.ty.to_tokens(tokens);
1361 self.eq_token.to_tokens(tokens);
1362 self.expr.to_tokens(tokens);
1363 self.semi_token.to_tokens(tokens);
1364 }
1365 }
1366
1367 impl ToTokens for ItemFn {
1368 fn to_tokens(&self, tokens: &mut Tokens) {
1369 tokens.append_all(self.attrs.outer());
1370 self.vis.to_tokens(tokens);
1371 self.constness.to_tokens(tokens);
1372 self.unsafety.to_tokens(tokens);
1373 self.abi.to_tokens(tokens);
1374 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1375 self.block.brace_token.surround(tokens, |tokens| {
1376 tokens.append_all(self.attrs.inner());
1377 tokens.append_all(&self.block.stmts);
1378 });
1379 }
1380 }
1381
1382 impl ToTokens for ItemMod {
1383 fn to_tokens(&self, tokens: &mut Tokens) {
1384 tokens.append_all(self.attrs.outer());
1385 self.vis.to_tokens(tokens);
1386 self.mod_token.to_tokens(tokens);
1387 self.ident.to_tokens(tokens);
1388 if let Some((ref brace, ref items)) = self.content {
1389 brace.surround(tokens, |tokens| {
1390 tokens.append_all(self.attrs.inner());
1391 tokens.append_all(items);
1392 });
1393 } else {
1394 TokensOrDefault(&self.semi).to_tokens(tokens);
1395 }
1396 }
1397 }
1398
1399 impl ToTokens for ItemForeignMod {
1400 fn to_tokens(&self, tokens: &mut Tokens) {
1401 tokens.append_all(self.attrs.outer());
1402 self.abi.to_tokens(tokens);
1403 self.brace_token.surround(tokens, |tokens| {
1404 tokens.append_all(&self.items);
1405 });
1406 }
1407 }
1408
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001409 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001410 fn to_tokens(&self, tokens: &mut Tokens) {
1411 tokens.append_all(self.attrs.outer());
1412 self.vis.to_tokens(tokens);
1413 self.type_token.to_tokens(tokens);
1414 self.ident.to_tokens(tokens);
1415 self.generics.to_tokens(tokens);
1416 self.generics.where_clause.to_tokens(tokens);
1417 self.eq_token.to_tokens(tokens);
1418 self.ty.to_tokens(tokens);
1419 self.semi_token.to_tokens(tokens);
1420 }
1421 }
1422
1423 impl ToTokens for ItemEnum {
1424 fn to_tokens(&self, tokens: &mut Tokens) {
1425 tokens.append_all(self.attrs.outer());
1426 self.vis.to_tokens(tokens);
1427 self.enum_token.to_tokens(tokens);
1428 self.ident.to_tokens(tokens);
1429 self.generics.to_tokens(tokens);
1430 self.generics.where_clause.to_tokens(tokens);
1431 self.brace_token.surround(tokens, |tokens| {
1432 self.variants.to_tokens(tokens);
1433 });
1434 }
1435 }
1436
1437 impl ToTokens for ItemStruct {
1438 fn to_tokens(&self, tokens: &mut Tokens) {
1439 tokens.append_all(self.attrs.outer());
1440 self.vis.to_tokens(tokens);
1441 self.struct_token.to_tokens(tokens);
1442 self.ident.to_tokens(tokens);
1443 self.generics.to_tokens(tokens);
1444 match self.data {
1445 VariantData::Struct(..) => {
1446 self.generics.where_clause.to_tokens(tokens);
1447 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001448 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001449 VariantData::Tuple(..) => {
1450 self.data.to_tokens(tokens);
1451 self.generics.where_clause.to_tokens(tokens);
1452 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001453 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001454 VariantData::Unit => {
1455 self.generics.where_clause.to_tokens(tokens);
1456 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001457 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001458 }
1459 }
1460 }
1461
1462 impl ToTokens for ItemUnion {
1463 fn to_tokens(&self, tokens: &mut Tokens) {
1464 tokens.append_all(self.attrs.outer());
1465 self.vis.to_tokens(tokens);
1466 self.union_token.to_tokens(tokens);
1467 self.ident.to_tokens(tokens);
1468 self.generics.to_tokens(tokens);
1469 self.generics.where_clause.to_tokens(tokens);
1470 // XXX: Should we handle / complain when using a
1471 // non-VariantData::Struct Variant in Union?
1472 self.data.to_tokens(tokens);
1473 }
1474 }
1475
1476 impl ToTokens for ItemTrait {
1477 fn to_tokens(&self, tokens: &mut Tokens) {
1478 tokens.append_all(self.attrs.outer());
1479 self.vis.to_tokens(tokens);
1480 self.unsafety.to_tokens(tokens);
1481 self.trait_token.to_tokens(tokens);
1482 self.ident.to_tokens(tokens);
1483 self.generics.to_tokens(tokens);
1484 if !self.supertraits.is_empty() {
1485 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1486 self.supertraits.to_tokens(tokens);
1487 }
1488 self.generics.where_clause.to_tokens(tokens);
1489 self.brace_token.surround(tokens, |tokens| {
1490 tokens.append_all(&self.items);
1491 });
1492 }
1493 }
1494
1495 impl ToTokens for ItemDefaultImpl {
1496 fn to_tokens(&self, tokens: &mut Tokens) {
1497 tokens.append_all(self.attrs.outer());
1498 self.unsafety.to_tokens(tokens);
1499 self.impl_token.to_tokens(tokens);
1500 self.path.to_tokens(tokens);
1501 self.for_token.to_tokens(tokens);
1502 self.dot2_token.to_tokens(tokens);
1503 self.brace_token.surround(tokens, |_tokens| {});
1504 }
1505 }
1506
1507 impl ToTokens for ItemImpl {
1508 fn to_tokens(&self, tokens: &mut Tokens) {
1509 tokens.append_all(self.attrs.outer());
1510 self.defaultness.to_tokens(tokens);
1511 self.unsafety.to_tokens(tokens);
1512 self.impl_token.to_tokens(tokens);
1513 self.generics.to_tokens(tokens);
1514 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1515 polarity.to_tokens(tokens);
1516 path.to_tokens(tokens);
1517 for_token.to_tokens(tokens);
1518 }
1519 self.self_ty.to_tokens(tokens);
1520 self.generics.where_clause.to_tokens(tokens);
1521 self.brace_token.surround(tokens, |tokens| {
1522 tokens.append_all(&self.items);
1523 });
1524 }
1525 }
1526
1527 impl ToTokens for ItemMacro {
1528 fn to_tokens(&self, tokens: &mut Tokens) {
1529 tokens.append_all(self.attrs.outer());
1530 self.mac.path.to_tokens(tokens);
1531 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001532 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001533 tokens.append_all(&self.mac.tokens);
1534 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001535 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001536 }
1537 }
1538 }
David Tolnay42602292016-10-01 22:25:45 -07001539
Alex Crichton62a0a592017-05-22 13:58:53 -07001540 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001541 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001542 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001543 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001544 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001545 self.rename.to_tokens(tokens);
1546 }
David Tolnay4a057422016-10-08 00:02:31 -07001547 }
1548 }
1549
Alex Crichton62a0a592017-05-22 13:58:53 -07001550 impl ToTokens for PathGlob {
1551 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001552 if self.path.segments.len() > 0 {
1553 self.path.to_tokens(tokens);
1554 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1555 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001556 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001557 }
1558 }
1559
1560 impl ToTokens for PathList {
1561 fn to_tokens(&self, tokens: &mut Tokens) {
1562 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 self.colon2_token.to_tokens(tokens);
1564 self.brace_token.surround(tokens, |tokens| {
1565 self.items.to_tokens(tokens);
1566 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001567 }
1568 }
1569
David Tolnay4a057422016-10-08 00:02:31 -07001570 impl ToTokens for PathListItem {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001573 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001574 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001575 self.rename.to_tokens(tokens);
1576 }
David Tolnay4a057422016-10-08 00:02:31 -07001577 }
1578 }
1579
David Tolnay1bfa7332017-11-11 12:41:20 -08001580 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001581 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001582 tokens.append_all(self.attrs.outer());
1583 self.const_token.to_tokens(tokens);
1584 self.ident.to_tokens(tokens);
1585 self.colon_token.to_tokens(tokens);
1586 self.ty.to_tokens(tokens);
1587 if let Some((ref eq_token, ref default)) = self.default {
1588 eq_token.to_tokens(tokens);
1589 default.to_tokens(tokens);
1590 }
1591 self.semi_token.to_tokens(tokens);
1592 }
1593 }
1594
1595 impl ToTokens for TraitItemMethod {
1596 fn to_tokens(&self, tokens: &mut Tokens) {
1597 tokens.append_all(self.attrs.outer());
1598 self.sig.to_tokens(tokens);
1599 match self.default {
1600 Some(ref block) => {
1601 block.brace_token.surround(tokens, |tokens| {
1602 tokens.append_all(self.attrs.inner());
1603 tokens.append_all(&block.stmts);
1604 });
David Tolnayca085422016-10-04 00:12:38 -07001605 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001606 None => {
1607 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001608 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001609 }
1610 }
1611 }
1612
1613 impl ToTokens for TraitItemType {
1614 fn to_tokens(&self, tokens: &mut Tokens) {
1615 tokens.append_all(self.attrs.outer());
1616 self.type_token.to_tokens(tokens);
1617 self.ident.to_tokens(tokens);
1618 if !self.bounds.is_empty() {
1619 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1620 self.bounds.to_tokens(tokens);
1621 }
1622 if let Some((ref eq_token, ref default)) = self.default {
1623 eq_token.to_tokens(tokens);
1624 default.to_tokens(tokens);
1625 }
1626 self.semi_token.to_tokens(tokens);
1627 }
1628 }
1629
1630 impl ToTokens for TraitItemMacro {
1631 fn to_tokens(&self, tokens: &mut Tokens) {
1632 tokens.append_all(self.attrs.outer());
1633 self.mac.to_tokens(tokens);
1634 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001635 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001636 }
1637 }
1638 }
1639
David Tolnay857628c2017-11-11 12:25:31 -08001640 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001641 fn to_tokens(&self, tokens: &mut Tokens) {
1642 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001643 self.vis.to_tokens(tokens);
1644 self.defaultness.to_tokens(tokens);
1645 self.const_token.to_tokens(tokens);
1646 self.ident.to_tokens(tokens);
1647 self.colon_token.to_tokens(tokens);
1648 self.ty.to_tokens(tokens);
1649 self.eq_token.to_tokens(tokens);
1650 self.expr.to_tokens(tokens);
1651 self.semi_token.to_tokens(tokens);
1652 }
1653 }
1654
1655 impl ToTokens for ImplItemMethod {
1656 fn to_tokens(&self, tokens: &mut Tokens) {
1657 tokens.append_all(self.attrs.outer());
1658 self.vis.to_tokens(tokens);
1659 self.defaultness.to_tokens(tokens);
1660 self.sig.to_tokens(tokens);
1661 self.block.brace_token.surround(tokens, |tokens| {
1662 tokens.append_all(self.attrs.inner());
1663 tokens.append_all(&self.block.stmts);
1664 });
1665 }
1666 }
1667
1668 impl ToTokens for ImplItemType {
1669 fn to_tokens(&self, tokens: &mut Tokens) {
1670 tokens.append_all(self.attrs.outer());
1671 self.vis.to_tokens(tokens);
1672 self.defaultness.to_tokens(tokens);
1673 self.type_token.to_tokens(tokens);
1674 self.ident.to_tokens(tokens);
1675 self.eq_token.to_tokens(tokens);
1676 self.ty.to_tokens(tokens);
1677 self.semi_token.to_tokens(tokens);
1678 }
1679 }
1680
1681 impl ToTokens for ImplItemMacro {
1682 fn to_tokens(&self, tokens: &mut Tokens) {
1683 tokens.append_all(self.attrs.outer());
1684 self.mac.to_tokens(tokens);
1685 if !self.mac.is_braced() {
1686 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001687 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001688 }
1689 }
1690 }
1691
David Tolnay8894f602017-11-11 12:11:04 -08001692 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001693 fn to_tokens(&self, tokens: &mut Tokens) {
1694 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001695 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001696 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1697 self.semi_token.to_tokens(tokens);
1698 }
1699 }
1700
1701 impl ToTokens for ForeignItemStatic {
1702 fn to_tokens(&self, tokens: &mut Tokens) {
1703 tokens.append_all(self.attrs.outer());
1704 self.vis.to_tokens(tokens);
1705 self.static_token.to_tokens(tokens);
1706 self.mutbl.to_tokens(tokens);
1707 self.ident.to_tokens(tokens);
1708 self.colon_token.to_tokens(tokens);
1709 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001710 self.semi_token.to_tokens(tokens);
1711 }
1712 }
1713
David Tolnay199bcbb2017-11-12 10:33:52 -08001714 impl ToTokens for ForeignItemType {
1715 fn to_tokens(&self, tokens: &mut Tokens) {
1716 tokens.append_all(self.attrs.outer());
1717 self.vis.to_tokens(tokens);
1718 self.type_token.to_tokens(tokens);
1719 self.ident.to_tokens(tokens);
1720 self.semi_token.to_tokens(tokens);
1721 }
1722 }
1723
David Tolnay570695e2017-06-03 16:15:13 -07001724 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001726 self.constness.to_tokens(tokens);
1727 self.unsafety.to_tokens(tokens);
1728 self.abi.to_tokens(tokens);
1729 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001730 }
1731 }
1732
David Tolnay570695e2017-06-03 16:15:13 -07001733 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001734
1735 impl<'a> ToTokens for NamedDecl<'a> {
1736 fn to_tokens(&self, tokens: &mut Tokens) {
1737 self.0.fn_token.to_tokens(tokens);
1738 self.1.to_tokens(tokens);
1739 self.0.generics.to_tokens(tokens);
1740 self.0.paren_token.surround(tokens, |tokens| {
1741 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001742
1743 if self.0.variadic {
1744 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001745 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001746 }
Alex Crichton259ee532017-07-14 06:51:02 -07001747 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001748 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001749 });
1750 self.0.output.to_tokens(tokens);
1751 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001752 }
1753 }
1754
Alex Crichton62a0a592017-05-22 13:58:53 -07001755 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001756 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001757 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001758 self.lifetime.to_tokens(tokens);
1759 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001760 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001761 }
1762 }
1763
1764 impl ToTokens for ArgSelf {
1765 fn to_tokens(&self, tokens: &mut Tokens) {
1766 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001767 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001768 }
1769 }
1770
1771 impl ToTokens for ArgCaptured {
1772 fn to_tokens(&self, tokens: &mut Tokens) {
1773 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001774 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001775 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001776 }
1777 }
1778
David Tolnay42602292016-10-01 22:25:45 -07001779 impl ToTokens for Constness {
1780 fn to_tokens(&self, tokens: &mut Tokens) {
1781 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001782 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001783 Constness::NotConst => {
1784 // nothing
1785 }
David Tolnay42602292016-10-01 22:25:45 -07001786 }
1787 }
1788 }
1789
David Tolnay4c9be372016-10-06 00:47:37 -07001790 impl ToTokens for Defaultness {
1791 fn to_tokens(&self, tokens: &mut Tokens) {
1792 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001793 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001794 Defaultness::Final => {
1795 // nothing
1796 }
1797 }
1798 }
1799 }
1800
1801 impl ToTokens for ImplPolarity {
1802 fn to_tokens(&self, tokens: &mut Tokens) {
1803 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001804 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001805 ImplPolarity::Positive => {
1806 // nothing
1807 }
1808 }
1809 }
1810 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001811}