blob: 0dfc95182e626e50adf698257a15bc03b4f7e2c3 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -08002use derive::{Data, DeriveInput};
David Tolnayf2cfd722017-12-31 18:02:51 -05003use punctuated::Punctuated;
David Tolnayab919512017-12-30 23:31:51 -05004use proc_macro2::{TokenStream};
5use token::{Paren, Brace};
David Tolnay9c76bcb2017-12-26 23:14:59 -05006
7#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -05008use tt::TokenStreamHelper;
David Tolnay9c76bcb2017-12-26 23:14:59 -05009#[cfg(feature = "extra-traits")]
10use std::hash::{Hash, Hasher};
David Tolnayb79ee962016-09-04 09:39:20 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -080013 /// Things that can appear directly inside of a module.
14 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -070015 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070016 ///
17 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
18 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080019 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070020 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 pub extern_token: Token![extern],
22 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070023 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080024 pub rename: Option<(Token![as], Ident)>,
25 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 }),
27 /// A use declaration (`use` or `pub use`) item.
28 ///
29 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
30 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080031 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070032 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050034 pub leading_colon: Option<Token![::]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050035 pub prefix: Punctuated<Ident, Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050036 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070038 }),
39 /// A static item (`static` or `pub static`).
40 ///
41 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
42 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080043 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070044 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050046 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070047 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080049 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070051 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070053 }),
54 /// A constant item (`const` or `pub const`).
55 ///
56 /// E.g. `const FOO: i32 = 42;`
57 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080058 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070059 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070061 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080063 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070065 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080066 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070067 }),
68 /// A function declaration (`fn` or `pub fn`).
69 ///
70 /// E.g. `fn foo(bar: usize) -> usize { .. }`
71 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080072 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070073 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050074 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050075 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070076 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050078 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070079 pub block: Box<Block>,
80 }),
81 /// A module declaration (`mod` or `pub mod`).
82 ///
83 /// E.g. `mod foo;` or `mod foo { .. }`
84 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080085 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070086 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080087 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070088 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050089 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080090 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070091 }),
92 /// An external module (`extern` or `pub extern`).
93 ///
94 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080096 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050098 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070099 pub items: Vec<ForeignItem>,
100 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 /// A type alias (`type` or `pub type`).
102 ///
103 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800104 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800105 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700106 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800107 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800111 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500114 /// A struct definition (`struct` or `pub struct`).
115 ///
116 /// E.g. `struct Foo<A> { x: A }`
117 pub Struct(ItemStruct {
118 pub attrs: Vec<Attribute>,
119 pub vis: Visibility,
120 pub struct_token: Token![struct],
121 pub ident: Ident,
122 pub generics: Generics,
123 pub fields: Fields,
124 pub semi_token: Option<Token![;]>,
125 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 /// An enum definition (`enum` or `pub enum`).
127 ///
128 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
129 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800130 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800132 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub ident: Ident,
134 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500135 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500136 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 /// A union definition (`union` or `pub union`).
139 ///
140 /// E.g. `union Foo<A, B> { x: A, y: B }`
141 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800142 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700143 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800144 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700145 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700146 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500147 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700148 }),
149 /// A Trait declaration (`trait` or `pub trait`).
150 ///
151 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
152 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800153 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700154 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500155 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500156 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700158 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800160 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500161 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500162 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 pub items: Vec<TraitItem>,
164 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 /// An implementation.
166 ///
167 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
168 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800169 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500170 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500171 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800172 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700174 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500175 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700176 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800177 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500178 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 pub items: Vec<ImplItem>,
180 }),
181 /// A macro invocation (which includes macro definition).
182 ///
183 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800184 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800185 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800186 /// The `example` in `macro_rules! example { ... }`.
187 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800188 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500189 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800190 }),
David Tolnay9c76bcb2017-12-26 23:14:59 -0500191 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800192 pub attrs: Vec<Attribute>,
193 pub vis: Visibility,
194 pub macro_token: Token![macro],
195 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500196 pub paren_token: Paren,
197 pub args: TokenStream,
198 pub brace_token: Brace,
199 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800200 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500201 pub Verbatim(ItemVerbatim #manual_extra_traits {
202 pub tts: TokenStream,
203 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 }
David Tolnayb79ee962016-09-04 09:39:20 -0700205}
206
David Tolnay9c76bcb2017-12-26 23:14:59 -0500207#[cfg(feature = "extra-traits")]
208impl Eq for ItemMacro2 {}
209
210#[cfg(feature = "extra-traits")]
211impl PartialEq for ItemMacro2 {
212 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500213 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnayab919512017-12-30 23:31:51 -0500214 && self.ident == other.ident && self.paren_token == other.paren_token
215 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
216 && self.brace_token == other.brace_token
217 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500218 }
219}
220
221#[cfg(feature = "extra-traits")]
222impl Hash for ItemMacro2 {
223 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500224 where
225 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500226 {
227 self.attrs.hash(state);
228 self.vis.hash(state);
229 self.macro_token.hash(state);
230 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500231 self.paren_token.hash(state);
232 TokenStreamHelper(&self.args).hash(state);
233 self.brace_token.hash(state);
234 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500235 }
236}
237
David Tolnay2ae520a2017-12-29 11:19:50 -0500238#[cfg(feature = "extra-traits")]
239impl Eq for ItemVerbatim {}
240
241#[cfg(feature = "extra-traits")]
242impl PartialEq for ItemVerbatim {
243 fn eq(&self, other: &Self) -> bool {
244 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
245 }
246}
247
248#[cfg(feature = "extra-traits")]
249impl Hash for ItemVerbatim {
250 fn hash<H>(&self, state: &mut H)
251 where
252 H: Hasher,
253 {
254 TokenStreamHelper(&self.tts).hash(state);
255 }
256}
257
David Tolnay0e837402016-12-22 17:25:55 -0500258impl From<DeriveInput> for Item {
259 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500260 match input.data {
261 Data::Struct(data) => Item::Struct(ItemStruct {
262 attrs: input.attrs,
263 vis: input.vis,
264 struct_token: data.struct_token,
265 ident: input.ident,
266 generics: input.generics,
267 fields: data.fields,
268 semi_token: data.semi_token,
269 }),
270 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500271 attrs: input.attrs,
272 vis: input.vis,
273 enum_token: data.enum_token,
274 ident: input.ident,
275 generics: input.generics,
276 brace_token: data.brace_token,
277 variants: data.variants,
278 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500279 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500280 attrs: input.attrs,
281 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500282 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500283 ident: input.ident,
284 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500285 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500286 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700287 }
288 }
289}
290
Alex Crichton62a0a592017-05-22 13:58:53 -0700291ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500292 /// Things that can appear directly inside of a module.
293 pub enum UseTree {
294 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
295 pub Path(UsePath {
296 pub ident: Ident,
297 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500299 /// `use prefix::*`
300 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500303 /// `use prefix::{a, b, c}`
304 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500305 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500306 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 }),
308 }
309}
310
Alex Crichton62a0a592017-05-22 13:58:53 -0700311ast_enum_of_structs! {
312 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800313 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 /// A foreign function
315 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800316 pub attrs: Vec<Attribute>,
317 pub vis: Visibility,
318 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800320 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700321 }),
322 /// A foreign static item (`static ext: u8`)
323 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800324 pub attrs: Vec<Attribute>,
325 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800326 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500327 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800328 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800329 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800330 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800331 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800333 /// A foreign type
334 pub Type(ForeignItemType {
335 pub attrs: Vec<Attribute>,
336 pub vis: Visibility,
337 pub type_token: Token![type],
338 pub ident: Ident,
339 pub semi_token: Token![;],
340 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500341 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
342 pub tts: TokenStream,
343 }),
344 }
345}
346
347#[cfg(feature = "extra-traits")]
348impl Eq for ForeignItemVerbatim {}
349
350#[cfg(feature = "extra-traits")]
351impl PartialEq for ForeignItemVerbatim {
352 fn eq(&self, other: &Self) -> bool {
353 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
354 }
355}
356
357#[cfg(feature = "extra-traits")]
358impl Hash for ForeignItemVerbatim {
359 fn hash<H>(&self, state: &mut H)
360 where
361 H: Hasher,
362 {
363 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700364 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700365}
366
David Tolnayda705bd2017-11-10 21:58:05 -0800367ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 /// Represents an item declaration within a trait declaration,
369 /// possibly including a default implementation. A trait item is
370 /// either required (meaning it doesn't have an implementation, just a
371 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800372 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800374 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700376 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800378 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800379 pub default: Option<(Token![=], Expr)>,
380 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
382 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800383 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 pub sig: MethodSig,
385 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800386 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700387 }),
388 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800389 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800390 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700391 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500392 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800393 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500394 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800395 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800396 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800398 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800399 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800400 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500401 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800402 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500403 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
404 pub tts: TokenStream,
405 }),
406 }
407}
408
409#[cfg(feature = "extra-traits")]
410impl Eq for TraitItemVerbatim {}
411
412#[cfg(feature = "extra-traits")]
413impl PartialEq for TraitItemVerbatim {
414 fn eq(&self, other: &Self) -> bool {
415 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
416 }
417}
418
419#[cfg(feature = "extra-traits")]
420impl Hash for TraitItemVerbatim {
421 fn hash<H>(&self, state: &mut H)
422 where
423 H: Hasher,
424 {
425 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700427}
428
Alex Crichton62a0a592017-05-22 13:58:53 -0700429ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800430 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800432 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700433 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500434 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700436 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800438 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800439 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700440 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700442 }),
443 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800444 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700445 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500446 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700447 pub sig: MethodSig,
448 pub block: Block,
449 }),
450 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800451 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700452 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500453 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800454 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700455 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500456 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800457 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800458 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800459 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700460 }),
David Tolnay857628c2017-11-11 12:25:31 -0800461 pub Macro(ImplItemMacro {
462 pub attrs: Vec<Attribute>,
463 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500464 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800465 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500466 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
467 pub tts: TokenStream,
468 }),
469 }
470}
471
472#[cfg(feature = "extra-traits")]
473impl Eq for ImplItemVerbatim {}
474
475#[cfg(feature = "extra-traits")]
476impl PartialEq for ImplItemVerbatim {
477 fn eq(&self, other: &Self) -> bool {
478 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
479 }
480}
481
482#[cfg(feature = "extra-traits")]
483impl Hash for ImplItemVerbatim {
484 fn hash<H>(&self, state: &mut H)
485 where
486 H: Hasher,
487 {
488 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700490}
491
492ast_struct! {
493 /// Represents a method's signature in a trait declaration,
494 /// or in an implementation.
495 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500496 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500497 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700498 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700499 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700501 }
502}
503
504ast_struct! {
505 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700506 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 /// E.g. `fn foo(bar: baz)`
508 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500510 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500511 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500512 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500513 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500514 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700516}
517
Alex Crichton62a0a592017-05-22 13:58:53 -0700518ast_enum_of_structs! {
519 /// An argument in a function header.
520 ///
521 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
522 pub enum FnArg {
523 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800524 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500526 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500527 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 }),
529 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500530 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }),
533 pub Captured(ArgCaptured {
534 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800535 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800536 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500538 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800539 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700540 }
David Tolnay62f374c2016-10-02 13:37:00 -0700541}
542
David Tolnayedf2b992016-09-23 20:43:45 -0700543#[cfg(feature = "parsing")]
544pub mod parsing {
545 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700546
David Tolnay03342952017-12-29 11:52:00 -0500547 use synom::{Synom, Cursor, PResult};
David Tolnay84aa0752016-10-02 23:01:13 -0700548
David Tolnay4c614be2017-11-10 00:02:38 -0800549 impl_synom!(Item "item" alt!(
550 syn!(ItemExternCrate) => { Item::ExternCrate }
551 |
552 syn!(ItemUse) => { Item::Use }
553 |
554 syn!(ItemStatic) => { Item::Static }
555 |
556 syn!(ItemConst) => { Item::Const }
557 |
558 syn!(ItemFn) => { Item::Fn }
559 |
560 syn!(ItemMod) => { Item::Mod }
561 |
562 syn!(ItemForeignMod) => { Item::ForeignMod }
563 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800564 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800565 |
566 syn!(ItemStruct) => { Item::Struct }
567 |
568 syn!(ItemEnum) => { Item::Enum }
569 |
570 syn!(ItemUnion) => { Item::Union }
571 |
572 syn!(ItemTrait) => { Item::Trait }
573 |
David Tolnay03342952017-12-29 11:52:00 -0500574 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800575 |
576 syn!(ItemImpl) => { Item::Impl }
577 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800578 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800579 |
580 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800581 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700582
David Tolnaydecf28d2017-11-11 11:56:45 -0800583 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500584 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700585 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800586 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700587 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500588 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500589 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800590 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700591 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800592 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800593 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500594 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700595 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500596 delimiter: body.0,
597 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800598 },
David Tolnay57292da2017-12-27 21:03:33 -0500599 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800600 })
David Tolnayedf2b992016-09-23 20:43:45 -0700601 ));
602
David Tolnay500d8322017-12-18 00:32:51 -0800603 // TODO: figure out the actual grammar; is body required to be braced?
604 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500605 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800606 vis: syn!(Visibility) >>
607 macro_: keyword!(macro) >>
608 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500609 args: call!(tt::parenthesized) >>
610 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800611 (ItemMacro2 {
612 attrs: attrs,
613 vis: vis,
614 macro_token: macro_,
615 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500616 paren_token: args.0,
617 args: args.1,
618 brace_token: body.0,
619 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800620 })
621 ));
622
David Tolnay4c614be2017-11-10 00:02:38 -0800623 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500624 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700625 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800626 extern_: keyword!(extern) >>
627 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700628 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800629 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
630 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800631 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700632 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800633 vis: vis,
634 extern_token: extern_,
635 crate_token: crate_,
636 ident: ident,
637 rename: rename,
638 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800639 })
David Tolnayedf2b992016-09-23 20:43:45 -0700640 ));
641
David Tolnay4c614be2017-11-10 00:02:38 -0800642 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500643 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700644 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800645 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500646 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500647 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500648 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500649 true => syn!(UseTree)
650 |
David Tolnay8edcef12017-12-28 12:06:52 -0500651 false => alt!(
652 tuple!(keyword!(as), syn!(Ident)) => {
653 |rename| UseTree::Path(UsePath {
654 ident: prefix.pop().unwrap().into_item(),
655 rename: Some(rename),
656 })
657 }
David Tolnay5f332a92017-12-26 00:42:45 -0500658 |
David Tolnay8edcef12017-12-28 12:06:52 -0500659 epsilon!() => {
660 |_| UseTree::Path(UsePath {
661 ident: prefix.pop().unwrap().into_item(),
662 rename: None,
663 })
664 }
David Tolnay5f332a92017-12-26 00:42:45 -0500665 )
666 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800667 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800668 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700669 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800670 vis: vis,
671 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500672 leading_colon: leading_colon,
673 prefix: prefix,
674 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800675 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800676 })
David Tolnay4a057422016-10-08 00:02:31 -0700677 ));
678
David Tolnay5f332a92017-12-26 00:42:45 -0500679 named!(use_prefix -> Ident, alt!(
680 syn!(Ident)
681 |
682 keyword!(self) => { Into::into }
683 |
684 keyword!(super) => { Into::into }
685 |
686 keyword!(crate) => { Into::into }
687 ));
688
689 impl_synom!(UseTree "use tree" alt!(
690 syn!(UsePath) => { UseTree::Path }
691 |
692 syn!(UseGlob) => { UseTree::Glob }
693 |
694 syn!(UseList) => { UseTree::List }
695 ));
696
697 impl_synom!(UsePath "use path" do_parse!(
698 ident: alt!(
699 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400700 |
David Tolnay5f332a92017-12-26 00:42:45 -0500701 keyword!(self) => { Into::into }
702 ) >>
703 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
704 (UsePath {
705 ident: ident,
706 rename: rename,
707 })
708 ));
David Tolnay4a057422016-10-08 00:02:31 -0700709
David Tolnay5f332a92017-12-26 00:42:45 -0500710 impl_synom!(UseGlob "use glob" do_parse!(
711 star: punct!(*) >>
712 (UseGlob {
713 star_token: star,
714 })
715 ));
David Tolnay4a057422016-10-08 00:02:31 -0700716
David Tolnay5f332a92017-12-26 00:42:45 -0500717 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500718 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500719 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500720 brace_token: list.0,
721 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500722 })
723 ));
David Tolnay4a057422016-10-08 00:02:31 -0700724
David Tolnay4c614be2017-11-10 00:02:38 -0800725 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500726 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700727 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800728 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500729 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700730 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800731 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800732 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800733 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700734 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800735 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800736 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700737 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800738 vis: vis,
739 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500740 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800741 ident: ident,
742 colon_token: colon,
743 ty: Box::new(ty),
744 eq_token: eq,
745 expr: Box::new(value),
746 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800747 })
David Tolnay47a877c2016-10-01 16:50:55 -0700748 ));
749
David Tolnay4c614be2017-11-10 00:02:38 -0800750 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500751 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700752 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800753 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700754 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800755 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800756 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800757 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700758 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800759 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800760 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700761 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800762 vis: vis,
763 const_token: const_,
764 ident: ident,
765 colon_token: colon,
766 ty: Box::new(ty),
767 eq_token: eq,
768 expr: Box::new(value),
769 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800770 })
David Tolnay47a877c2016-10-01 16:50:55 -0700771 ));
772
David Tolnay4c614be2017-11-10 00:02:38 -0800773 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500774 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700775 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500776 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500777 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700778 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800779 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700780 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700781 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500782 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800783 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500784 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700785 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500786 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700787 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400788 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800789 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700790 attrs: {
791 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500792 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700793 attrs
794 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800795 vis: vis,
796 constness: constness,
797 unsafety: unsafety,
798 abi: abi,
799 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800800 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500801 paren_token: inputs.0,
802 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800803 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500804 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800805 generics: Generics {
806 where_clause: where_clause,
807 .. generics
808 },
809 }),
810 ident: ident,
811 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500812 brace_token: inner_attrs_stmts.0,
813 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800814 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800815 })
David Tolnay42602292016-10-01 22:25:45 -0700816 ));
817
Alex Crichton954046c2017-05-30 21:49:42 -0700818 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400819 named!(parse -> Self, alt!(
820 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800821 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400822 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500823 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800824 self_: keyword!(self) >>
825 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400826 (ArgSelfRef {
827 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500828 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400829 and_token: and,
830 self_token: self_,
831 }.into())
832 )
833 |
834 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500835 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800836 self_: keyword!(self) >>
837 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400838 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500839 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400840 self_token: self_,
841 }.into())
842 )
843 |
844 do_parse!(
845 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800846 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800847 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400848 (ArgCaptured {
849 pat: pat,
850 ty: ty,
851 colon_token: colon,
852 }.into())
853 )
854 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800855 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400856 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800857
858 fn description() -> Option<&'static str> {
859 Some("function argument")
860 }
Alex Crichton954046c2017-05-30 21:49:42 -0700861 }
David Tolnay62f374c2016-10-02 13:37:00 -0700862
David Tolnay4c614be2017-11-10 00:02:38 -0800863 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500864 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700865 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700867 ident: syn!(Ident) >>
868 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800869 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700870 Vec::new(),
871 None,
872 Some(semi),
873 )}
David Tolnay37d10332016-10-13 20:51:04 -0700874 |
Alex Crichton954046c2017-05-30 21:49:42 -0700875 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700876 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500877 many0!(Attribute::parse_inner),
878 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400879 )
David Tolnay8875fca2017-12-31 13:52:37 -0500880 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -0700881 inner_attrs,
882 Some((brace, items)),
883 None,
884 )}
David Tolnay37d10332016-10-13 20:51:04 -0700885 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800886 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700887 attrs: {
888 let mut attrs = outer_attrs;
889 attrs.extend(content_semi.0);
890 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700891 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800892 vis: vis,
893 mod_token: mod_,
894 ident: ident,
895 content: content_semi.1,
896 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800897 })
David Tolnay35902302016-10-06 01:11:08 -0700898 ));
899
David Tolnay4c614be2017-11-10 00:02:38 -0800900 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500901 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700902 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500903 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800904 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700905 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800906 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -0500907 brace_token: items.0,
908 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800909 })
David Tolnay35902302016-10-06 01:11:08 -0700910 ));
911
David Tolnay8894f602017-11-11 12:11:04 -0800912 impl_synom!(ForeignItem "foreign item" alt!(
913 syn!(ForeignItemFn) => { ForeignItem::Fn }
914 |
915 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800916 |
917 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800918 ));
David Tolnay35902302016-10-06 01:11:08 -0700919
David Tolnay8894f602017-11-11 12:11:04 -0800920 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500921 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700922 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800923 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700924 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700925 generics: syn!(Generics) >>
926 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500927 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500928 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700929 (args, variadic)
930 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800931 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500932 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700934 ({
David Tolnay8875fca2017-12-31 13:52:37 -0500935 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800936 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700937 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700938 attrs: attrs,
939 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800940 decl: Box::new(FnDecl {
941 fn_token: fn_,
942 paren_token: parens,
943 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500944 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800945 output: ret,
946 generics: Generics {
947 where_clause: where_clause,
948 .. generics
949 },
950 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700951 vis: vis,
952 }
David Tolnay35902302016-10-06 01:11:08 -0700953 })
954 ));
955
David Tolnay8894f602017-11-11 12:11:04 -0800956 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500957 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700958 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800959 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500960 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700961 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800963 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800964 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800965 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700966 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700967 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700968 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800969 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500970 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800971 static_token: static_,
972 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700973 vis: vis,
974 })
975 ));
976
David Tolnay199bcbb2017-11-12 10:33:52 -0800977 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500978 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800979 vis: syn!(Visibility) >>
980 type_: keyword!(type) >>
981 ident: syn!(Ident) >>
982 semi: punct!(;) >>
983 (ForeignItemType {
984 attrs: attrs,
985 vis: vis,
986 type_token: type_,
987 ident: ident,
988 semi_token: semi,
989 })
990 ));
991
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800992 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500993 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700994 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800995 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700996 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700997 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500998 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800999 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001000 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001001 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001002 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001003 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001004 vis: vis,
1005 type_token: type_,
1006 ident: ident,
1007 generics: Generics {
1008 where_clause: where_clause,
1009 ..generics
1010 },
1011 eq_token: eq,
1012 ty: Box::new(ty),
1013 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001014 })
David Tolnay3cf52982016-10-01 17:11:37 -07001015 ));
1016
David Tolnay4c614be2017-11-10 00:02:38 -08001017 impl_synom!(ItemStruct "struct item" switch!(
1018 map!(syn!(DeriveInput), Into::into),
1019 Item::Struct(item) => value!(item)
1020 |
1021 _ => reject!()
1022 ));
David Tolnay42602292016-10-01 22:25:45 -07001023
David Tolnay4c614be2017-11-10 00:02:38 -08001024 impl_synom!(ItemEnum "enum item" switch!(
1025 map!(syn!(DeriveInput), Into::into),
1026 Item::Enum(item) => value!(item)
1027 |
1028 _ => reject!()
1029 ));
1030
1031 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001032 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001033 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001034 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001035 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001036 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001037 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001038 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001039 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001040 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001041 vis: vis,
1042 union_token: union_,
1043 ident: ident,
1044 generics: Generics {
1045 where_clause: where_clause,
1046 .. generics
1047 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001048 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001049 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001050 ));
1051
David Tolnay4c614be2017-11-10 00:02:38 -08001052 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001053 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001054 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001055 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001056 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001057 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001058 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001059 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001060 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001061 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001062 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001063 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001064 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001065 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001066 vis: vis,
1067 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001068 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001069 trait_token: trait_,
1070 ident: ident,
1071 generics: Generics {
1072 where_clause: where_clause,
1073 .. generics
1074 },
1075 colon_token: colon,
1076 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001077 brace_token: body.0,
1078 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001079 })
David Tolnay0aecb732016-10-03 23:03:50 -07001080 ));
1081
David Tolnay03342952017-12-29 11:52:00 -05001082 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1083 Ok((cursor, cursor))
1084 }
1085
1086 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1087 begin: call!(grab_cursor) >>
1088 many0!(Attribute::parse_outer) >>
1089 option!(keyword!(unsafe)) >>
1090 keyword!(impl) >>
1091 syn!(Path) >>
1092 keyword!(for) >>
1093 punct!(..) >>
1094 braces!(epsilon!()) >>
1095 end: call!(grab_cursor) >>
1096 ({
1097 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1098 let len = tts.len() - end.token_stream().into_iter().count();
1099 ItemVerbatim {
1100 tts: tts.into_iter().take(len).collect(),
1101 }
David Tolnay4c614be2017-11-10 00:02:38 -08001102 })
David Tolnayf94e2362016-10-04 00:29:51 -07001103 ));
1104
David Tolnayda705bd2017-11-10 21:58:05 -08001105 impl_synom!(TraitItem "trait item" alt!(
1106 syn!(TraitItemConst) => { TraitItem::Const }
1107 |
1108 syn!(TraitItemMethod) => { TraitItem::Method }
1109 |
1110 syn!(TraitItemType) => { TraitItem::Type }
1111 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001112 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001113 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001114
David Tolnayda705bd2017-11-10 21:58:05 -08001115 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001116 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001117 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001118 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001119 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001120 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001121 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1122 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001123 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001124 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001125 const_token: const_,
1126 ident: ident,
1127 colon_token: colon,
1128 ty: ty,
1129 default: default,
1130 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001131 })
1132 ));
1133
David Tolnayda705bd2017-11-10 21:58:05 -08001134 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001135 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001136 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001137 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001138 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001139 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001140 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001141 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001142 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001143 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001144 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001145 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001146 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001147 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001148 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001149 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001150 ({
1151 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001152 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001153 None => (Vec::new(), None),
1154 };
David Tolnayda705bd2017-11-10 21:58:05 -08001155 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001156 attrs: {
1157 let mut attrs = outer_attrs;
1158 attrs.extend(inner_attrs);
1159 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001160 },
David Tolnayda705bd2017-11-10 21:58:05 -08001161 sig: MethodSig {
1162 constness: constness,
1163 unsafety: unsafety,
1164 abi: abi,
1165 ident: ident,
1166 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001167 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001168 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001169 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001170 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001171 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001172 generics: Generics {
1173 where_clause: where_clause,
1174 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001175 },
1176 },
David Tolnayda705bd2017-11-10 21:58:05 -08001177 },
1178 default: stmts.map(|stmts| {
1179 Block {
1180 stmts: stmts.0,
1181 brace_token: stmts.1,
1182 }
1183 }),
1184 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001185 }
David Tolnay0aecb732016-10-03 23:03:50 -07001186 })
1187 ));
1188
David Tolnayda705bd2017-11-10 21:58:05 -08001189 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001190 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001191 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001192 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001193 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001194 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001195 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001196 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001197 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001198 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001199 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001200 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001201 type_token: type_,
1202 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001203 generics: Generics {
1204 where_clause: where_clause,
1205 .. generics
1206 },
David Tolnayda705bd2017-11-10 21:58:05 -08001207 colon_token: colon,
1208 bounds: bounds.unwrap_or_default(),
1209 default: default,
1210 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001211 })
1212 ));
1213
David Tolnaydecf28d2017-11-11 11:56:45 -08001214 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001215 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001216 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001217 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001218 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001219 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001220 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001221 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001222 })
1223 ));
1224
David Tolnay4c614be2017-11-10 00:02:38 -08001225 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001226 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001227 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001228 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001229 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001230 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001231 polarity_path: alt!(
1232 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001233 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001234 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001235 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001236 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001237 )
1238 |
David Tolnay570695e2017-06-03 16:15:13 -07001239 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001240 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001241 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001242 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001243 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001244 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001245 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001246 defaultness: defaultness,
1247 unsafety: unsafety,
1248 impl_token: impl_,
1249 generics: Generics {
1250 where_clause: where_clause,
1251 .. generics
1252 },
1253 trait_: polarity_path,
1254 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001255 brace_token: body.0,
1256 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001257 })
David Tolnay4c9be372016-10-06 00:47:37 -07001258 ));
1259
David Tolnay857628c2017-11-11 12:25:31 -08001260 impl_synom!(ImplItem "item in impl block" alt!(
1261 syn!(ImplItemConst) => { ImplItem::Const }
1262 |
1263 syn!(ImplItemMethod) => { ImplItem::Method }
1264 |
1265 syn!(ImplItemType) => { ImplItem::Type }
1266 |
1267 syn!(ImplItemMacro) => { ImplItem::Macro }
1268 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001269
David Tolnay857628c2017-11-11 12:25:31 -08001270 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001271 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001272 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001273 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001274 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001275 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001276 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001277 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001278 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001279 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001280 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001281 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001282 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001283 vis: vis,
1284 defaultness: defaultness,
1285 const_token: const_,
1286 ident: ident,
1287 colon_token: colon,
1288 ty: ty,
1289 eq_token: eq,
1290 expr: value,
1291 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001292 })
1293 ));
1294
David Tolnay857628c2017-11-11 12:25:31 -08001295 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001296 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001297 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001298 defaultness: option!(keyword!(default)) >>
1299 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001300 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001301 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001302 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001303 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001304 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001305 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001306 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001307 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001308 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001309 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001310 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001311 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001312 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001313 attrs: {
1314 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001315 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001316 attrs
1317 },
David Tolnay857628c2017-11-11 12:25:31 -08001318 vis: vis,
1319 defaultness: defaultness,
1320 sig: MethodSig {
1321 constness: constness,
1322 unsafety: unsafety,
1323 abi: abi,
1324 ident: ident,
1325 decl: FnDecl {
1326 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001327 paren_token: inputs.0,
1328 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001329 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001330 generics: Generics {
1331 where_clause: where_clause,
1332 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001333 },
David Tolnayd2836e22017-12-27 23:13:00 -05001334 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001335 },
David Tolnay857628c2017-11-11 12:25:31 -08001336 },
1337 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001338 brace_token: inner_attrs_stmts.0,
1339 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001340 },
David Tolnay4c9be372016-10-06 00:47:37 -07001341 })
1342 ));
1343
David Tolnay857628c2017-11-11 12:25:31 -08001344 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001345 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001346 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001347 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001348 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001349 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001350 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001351 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001352 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001353 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001354 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001355 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001356 vis: vis,
1357 defaultness: defaultness,
1358 type_token: type_,
1359 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001360 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001361 eq_token: eq,
1362 ty: ty,
1363 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001364 })
1365 ));
1366
David Tolnay857628c2017-11-11 12:25:31 -08001367 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001368 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001369 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001370 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001371 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001372 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001373 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001374 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001375 })
1376 ));
1377
David Tolnayab919512017-12-30 23:31:51 -05001378 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1379 match *delimiter {
1380 MacroDelimiter::Brace(_) => true,
1381 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001382 }
1383 }
David Tolnayedf2b992016-09-23 20:43:45 -07001384}
David Tolnay4a51dc72016-10-01 00:40:31 -07001385
1386#[cfg(feature = "printing")]
1387mod printing {
1388 use super::*;
1389 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001390 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001391
David Tolnay1bfa7332017-11-11 12:41:20 -08001392 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001393 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001394 tokens.append_all(self.attrs.outer());
1395 self.vis.to_tokens(tokens);
1396 self.extern_token.to_tokens(tokens);
1397 self.crate_token.to_tokens(tokens);
1398 self.ident.to_tokens(tokens);
1399 if let Some((ref as_token, ref rename)) = self.rename {
1400 as_token.to_tokens(tokens);
1401 rename.to_tokens(tokens);
1402 }
1403 self.semi_token.to_tokens(tokens);
1404 }
1405 }
1406
1407 impl ToTokens for ItemUse {
1408 fn to_tokens(&self, tokens: &mut Tokens) {
1409 tokens.append_all(self.attrs.outer());
1410 self.vis.to_tokens(tokens);
1411 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001412 self.leading_colon.to_tokens(tokens);
1413 self.prefix.to_tokens(tokens);
1414 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001415 self.semi_token.to_tokens(tokens);
1416 }
1417 }
1418
1419 impl ToTokens for ItemStatic {
1420 fn to_tokens(&self, tokens: &mut Tokens) {
1421 tokens.append_all(self.attrs.outer());
1422 self.vis.to_tokens(tokens);
1423 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001424 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001425 self.ident.to_tokens(tokens);
1426 self.colon_token.to_tokens(tokens);
1427 self.ty.to_tokens(tokens);
1428 self.eq_token.to_tokens(tokens);
1429 self.expr.to_tokens(tokens);
1430 self.semi_token.to_tokens(tokens);
1431 }
1432 }
1433
1434 impl ToTokens for ItemConst {
1435 fn to_tokens(&self, tokens: &mut Tokens) {
1436 tokens.append_all(self.attrs.outer());
1437 self.vis.to_tokens(tokens);
1438 self.const_token.to_tokens(tokens);
1439 self.ident.to_tokens(tokens);
1440 self.colon_token.to_tokens(tokens);
1441 self.ty.to_tokens(tokens);
1442 self.eq_token.to_tokens(tokens);
1443 self.expr.to_tokens(tokens);
1444 self.semi_token.to_tokens(tokens);
1445 }
1446 }
1447
1448 impl ToTokens for ItemFn {
1449 fn to_tokens(&self, tokens: &mut Tokens) {
1450 tokens.append_all(self.attrs.outer());
1451 self.vis.to_tokens(tokens);
1452 self.constness.to_tokens(tokens);
1453 self.unsafety.to_tokens(tokens);
1454 self.abi.to_tokens(tokens);
1455 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1456 self.block.brace_token.surround(tokens, |tokens| {
1457 tokens.append_all(self.attrs.inner());
1458 tokens.append_all(&self.block.stmts);
1459 });
1460 }
1461 }
1462
1463 impl ToTokens for ItemMod {
1464 fn to_tokens(&self, tokens: &mut Tokens) {
1465 tokens.append_all(self.attrs.outer());
1466 self.vis.to_tokens(tokens);
1467 self.mod_token.to_tokens(tokens);
1468 self.ident.to_tokens(tokens);
1469 if let Some((ref brace, ref items)) = self.content {
1470 brace.surround(tokens, |tokens| {
1471 tokens.append_all(self.attrs.inner());
1472 tokens.append_all(items);
1473 });
1474 } else {
1475 TokensOrDefault(&self.semi).to_tokens(tokens);
1476 }
1477 }
1478 }
1479
1480 impl ToTokens for ItemForeignMod {
1481 fn to_tokens(&self, tokens: &mut Tokens) {
1482 tokens.append_all(self.attrs.outer());
1483 self.abi.to_tokens(tokens);
1484 self.brace_token.surround(tokens, |tokens| {
1485 tokens.append_all(&self.items);
1486 });
1487 }
1488 }
1489
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001490 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001491 fn to_tokens(&self, tokens: &mut Tokens) {
1492 tokens.append_all(self.attrs.outer());
1493 self.vis.to_tokens(tokens);
1494 self.type_token.to_tokens(tokens);
1495 self.ident.to_tokens(tokens);
1496 self.generics.to_tokens(tokens);
1497 self.generics.where_clause.to_tokens(tokens);
1498 self.eq_token.to_tokens(tokens);
1499 self.ty.to_tokens(tokens);
1500 self.semi_token.to_tokens(tokens);
1501 }
1502 }
1503
1504 impl ToTokens for ItemEnum {
1505 fn to_tokens(&self, tokens: &mut Tokens) {
1506 tokens.append_all(self.attrs.outer());
1507 self.vis.to_tokens(tokens);
1508 self.enum_token.to_tokens(tokens);
1509 self.ident.to_tokens(tokens);
1510 self.generics.to_tokens(tokens);
1511 self.generics.where_clause.to_tokens(tokens);
1512 self.brace_token.surround(tokens, |tokens| {
1513 self.variants.to_tokens(tokens);
1514 });
1515 }
1516 }
1517
1518 impl ToTokens for ItemStruct {
1519 fn to_tokens(&self, tokens: &mut Tokens) {
1520 tokens.append_all(self.attrs.outer());
1521 self.vis.to_tokens(tokens);
1522 self.struct_token.to_tokens(tokens);
1523 self.ident.to_tokens(tokens);
1524 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001525 match self.fields {
1526 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001527 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001528 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001529 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001530 Fields::Unnamed(ref fields) => {
1531 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001532 self.generics.where_clause.to_tokens(tokens);
1533 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001534 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001535 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001536 self.generics.where_clause.to_tokens(tokens);
1537 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001538 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001539 }
1540 }
1541 }
1542
1543 impl ToTokens for ItemUnion {
1544 fn to_tokens(&self, tokens: &mut Tokens) {
1545 tokens.append_all(self.attrs.outer());
1546 self.vis.to_tokens(tokens);
1547 self.union_token.to_tokens(tokens);
1548 self.ident.to_tokens(tokens);
1549 self.generics.to_tokens(tokens);
1550 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001551 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001552 }
1553 }
1554
1555 impl ToTokens for ItemTrait {
1556 fn to_tokens(&self, tokens: &mut Tokens) {
1557 tokens.append_all(self.attrs.outer());
1558 self.vis.to_tokens(tokens);
1559 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001560 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001561 self.trait_token.to_tokens(tokens);
1562 self.ident.to_tokens(tokens);
1563 self.generics.to_tokens(tokens);
1564 if !self.supertraits.is_empty() {
1565 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1566 self.supertraits.to_tokens(tokens);
1567 }
1568 self.generics.where_clause.to_tokens(tokens);
1569 self.brace_token.surround(tokens, |tokens| {
1570 tokens.append_all(&self.items);
1571 });
1572 }
1573 }
1574
David Tolnay1bfa7332017-11-11 12:41:20 -08001575 impl ToTokens for ItemImpl {
1576 fn to_tokens(&self, tokens: &mut Tokens) {
1577 tokens.append_all(self.attrs.outer());
1578 self.defaultness.to_tokens(tokens);
1579 self.unsafety.to_tokens(tokens);
1580 self.impl_token.to_tokens(tokens);
1581 self.generics.to_tokens(tokens);
1582 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1583 polarity.to_tokens(tokens);
1584 path.to_tokens(tokens);
1585 for_token.to_tokens(tokens);
1586 }
1587 self.self_ty.to_tokens(tokens);
1588 self.generics.where_clause.to_tokens(tokens);
1589 self.brace_token.surround(tokens, |tokens| {
1590 tokens.append_all(&self.items);
1591 });
1592 }
1593 }
1594
1595 impl ToTokens for ItemMacro {
1596 fn to_tokens(&self, tokens: &mut Tokens) {
1597 tokens.append_all(self.attrs.outer());
1598 self.mac.path.to_tokens(tokens);
1599 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001600 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001601 match self.mac.delimiter {
1602 MacroDelimiter::Paren(ref paren) => {
1603 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1604 }
1605 MacroDelimiter::Brace(ref brace) => {
1606 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1607 }
1608 MacroDelimiter::Bracket(ref bracket) => {
1609 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1610 }
1611 }
David Tolnay57292da2017-12-27 21:03:33 -05001612 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001613 }
1614 }
David Tolnay42602292016-10-01 22:25:45 -07001615
David Tolnay500d8322017-12-18 00:32:51 -08001616 impl ToTokens for ItemMacro2 {
1617 fn to_tokens(&self, tokens: &mut Tokens) {
1618 tokens.append_all(self.attrs.outer());
1619 self.vis.to_tokens(tokens);
1620 self.macro_token.to_tokens(tokens);
1621 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001622 self.paren_token.surround(tokens, |tokens| {
1623 self.args.to_tokens(tokens);
1624 });
1625 self.brace_token.surround(tokens, |tokens| {
1626 self.body.to_tokens(tokens);
1627 });
David Tolnay500d8322017-12-18 00:32:51 -08001628 }
1629 }
1630
David Tolnay2ae520a2017-12-29 11:19:50 -05001631 impl ToTokens for ItemVerbatim {
1632 fn to_tokens(&self, tokens: &mut Tokens) {
1633 self.tts.to_tokens(tokens);
1634 }
1635 }
1636
David Tolnay5f332a92017-12-26 00:42:45 -05001637 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001638 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001639 self.ident.to_tokens(tokens);
1640 if let Some((ref as_token, ref rename)) = self.rename {
1641 as_token.to_tokens(tokens);
1642 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001643 }
David Tolnay4a057422016-10-08 00:02:31 -07001644 }
1645 }
1646
David Tolnay5f332a92017-12-26 00:42:45 -05001647 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001648 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001649 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001650 }
1651 }
1652
David Tolnay5f332a92017-12-26 00:42:45 -05001653 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001655 self.brace_token.surround(tokens, |tokens| {
1656 self.items.to_tokens(tokens);
1657 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001658 }
1659 }
1660
David Tolnay1bfa7332017-11-11 12:41:20 -08001661 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001662 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001663 tokens.append_all(self.attrs.outer());
1664 self.const_token.to_tokens(tokens);
1665 self.ident.to_tokens(tokens);
1666 self.colon_token.to_tokens(tokens);
1667 self.ty.to_tokens(tokens);
1668 if let Some((ref eq_token, ref default)) = self.default {
1669 eq_token.to_tokens(tokens);
1670 default.to_tokens(tokens);
1671 }
1672 self.semi_token.to_tokens(tokens);
1673 }
1674 }
1675
1676 impl ToTokens for TraitItemMethod {
1677 fn to_tokens(&self, tokens: &mut Tokens) {
1678 tokens.append_all(self.attrs.outer());
1679 self.sig.to_tokens(tokens);
1680 match self.default {
1681 Some(ref block) => {
1682 block.brace_token.surround(tokens, |tokens| {
1683 tokens.append_all(self.attrs.inner());
1684 tokens.append_all(&block.stmts);
1685 });
David Tolnayca085422016-10-04 00:12:38 -07001686 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001687 None => {
1688 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001689 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001690 }
1691 }
1692 }
1693
1694 impl ToTokens for TraitItemType {
1695 fn to_tokens(&self, tokens: &mut Tokens) {
1696 tokens.append_all(self.attrs.outer());
1697 self.type_token.to_tokens(tokens);
1698 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001699 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001700 if !self.bounds.is_empty() {
1701 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1702 self.bounds.to_tokens(tokens);
1703 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001704 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001705 if let Some((ref eq_token, ref default)) = self.default {
1706 eq_token.to_tokens(tokens);
1707 default.to_tokens(tokens);
1708 }
1709 self.semi_token.to_tokens(tokens);
1710 }
1711 }
1712
1713 impl ToTokens for TraitItemMacro {
1714 fn to_tokens(&self, tokens: &mut Tokens) {
1715 tokens.append_all(self.attrs.outer());
1716 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001717 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001718 }
1719 }
1720
David Tolnay2ae520a2017-12-29 11:19:50 -05001721 impl ToTokens for TraitItemVerbatim {
1722 fn to_tokens(&self, tokens: &mut Tokens) {
1723 self.tts.to_tokens(tokens);
1724 }
1725 }
1726
David Tolnay857628c2017-11-11 12:25:31 -08001727 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001728 fn to_tokens(&self, tokens: &mut Tokens) {
1729 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001730 self.vis.to_tokens(tokens);
1731 self.defaultness.to_tokens(tokens);
1732 self.const_token.to_tokens(tokens);
1733 self.ident.to_tokens(tokens);
1734 self.colon_token.to_tokens(tokens);
1735 self.ty.to_tokens(tokens);
1736 self.eq_token.to_tokens(tokens);
1737 self.expr.to_tokens(tokens);
1738 self.semi_token.to_tokens(tokens);
1739 }
1740 }
1741
1742 impl ToTokens for ImplItemMethod {
1743 fn to_tokens(&self, tokens: &mut Tokens) {
1744 tokens.append_all(self.attrs.outer());
1745 self.vis.to_tokens(tokens);
1746 self.defaultness.to_tokens(tokens);
1747 self.sig.to_tokens(tokens);
1748 self.block.brace_token.surround(tokens, |tokens| {
1749 tokens.append_all(self.attrs.inner());
1750 tokens.append_all(&self.block.stmts);
1751 });
1752 }
1753 }
1754
1755 impl ToTokens for ImplItemType {
1756 fn to_tokens(&self, tokens: &mut Tokens) {
1757 tokens.append_all(self.attrs.outer());
1758 self.vis.to_tokens(tokens);
1759 self.defaultness.to_tokens(tokens);
1760 self.type_token.to_tokens(tokens);
1761 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001762 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001763 self.eq_token.to_tokens(tokens);
1764 self.ty.to_tokens(tokens);
1765 self.semi_token.to_tokens(tokens);
1766 }
1767 }
1768
1769 impl ToTokens for ImplItemMacro {
1770 fn to_tokens(&self, tokens: &mut Tokens) {
1771 tokens.append_all(self.attrs.outer());
1772 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001773 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001774 }
1775 }
1776
David Tolnay2ae520a2017-12-29 11:19:50 -05001777 impl ToTokens for ImplItemVerbatim {
1778 fn to_tokens(&self, tokens: &mut Tokens) {
1779 self.tts.to_tokens(tokens);
1780 }
1781 }
1782
David Tolnay8894f602017-11-11 12:11:04 -08001783 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001784 fn to_tokens(&self, tokens: &mut Tokens) {
1785 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001786 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001787 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1788 self.semi_token.to_tokens(tokens);
1789 }
1790 }
1791
1792 impl ToTokens for ForeignItemStatic {
1793 fn to_tokens(&self, tokens: &mut Tokens) {
1794 tokens.append_all(self.attrs.outer());
1795 self.vis.to_tokens(tokens);
1796 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001797 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001798 self.ident.to_tokens(tokens);
1799 self.colon_token.to_tokens(tokens);
1800 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001801 self.semi_token.to_tokens(tokens);
1802 }
1803 }
1804
David Tolnay199bcbb2017-11-12 10:33:52 -08001805 impl ToTokens for ForeignItemType {
1806 fn to_tokens(&self, tokens: &mut Tokens) {
1807 tokens.append_all(self.attrs.outer());
1808 self.vis.to_tokens(tokens);
1809 self.type_token.to_tokens(tokens);
1810 self.ident.to_tokens(tokens);
1811 self.semi_token.to_tokens(tokens);
1812 }
1813 }
1814
David Tolnay2ae520a2017-12-29 11:19:50 -05001815 impl ToTokens for ForeignItemVerbatim {
1816 fn to_tokens(&self, tokens: &mut Tokens) {
1817 self.tts.to_tokens(tokens);
1818 }
1819 }
1820
David Tolnay570695e2017-06-03 16:15:13 -07001821 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001822 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001823 self.constness.to_tokens(tokens);
1824 self.unsafety.to_tokens(tokens);
1825 self.abi.to_tokens(tokens);
1826 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001827 }
1828 }
1829
David Tolnay570695e2017-06-03 16:15:13 -07001830 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001831
1832 impl<'a> ToTokens for NamedDecl<'a> {
1833 fn to_tokens(&self, tokens: &mut Tokens) {
1834 self.0.fn_token.to_tokens(tokens);
1835 self.1.to_tokens(tokens);
1836 self.0.generics.to_tokens(tokens);
1837 self.0.paren_token.surround(tokens, |tokens| {
1838 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001839 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1840 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001841 }
David Tolnayd2836e22017-12-27 23:13:00 -05001842 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001843 });
1844 self.0.output.to_tokens(tokens);
1845 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001846 }
1847 }
1848
Alex Crichton62a0a592017-05-22 13:58:53 -07001849 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001850 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001851 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001852 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001853 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001854 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001855 }
1856 }
1857
1858 impl ToTokens for ArgSelf {
1859 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001860 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001861 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001862 }
1863 }
1864
1865 impl ToTokens for ArgCaptured {
1866 fn to_tokens(&self, tokens: &mut Tokens) {
1867 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001868 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001869 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001870 }
1871 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001872}