blob: 6cab34ddde4cb5b8df6ae63c963ec20a8c9190e9 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -05002use punctuated::Punctuated;
David Tolnayab919512017-12-30 23:31:51 -05003use proc_macro2::{TokenStream};
4use token::{Paren, Brace};
David Tolnay9c76bcb2017-12-26 23:14:59 -05005
6#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -05007use tt::TokenStreamHelper;
David Tolnay9c76bcb2017-12-26 23:14:59 -05008#[cfg(feature = "extra-traits")]
9use std::hash::{Hash, Hasher};
David Tolnayb79ee962016-09-04 09:39:20 -070010
Alex Crichton62a0a592017-05-22 13:58:53 -070011ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -080012 /// Things that can appear directly inside of a module.
13 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -070014 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070015 ///
16 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
17 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080018 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070019 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080020 pub extern_token: Token![extern],
21 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070022 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080023 pub rename: Option<(Token![as], Ident)>,
24 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070025 }),
26 /// A use declaration (`use` or `pub use`) item.
27 ///
28 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
29 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070031 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080032 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050033 pub leading_colon: Option<Token![::]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050034 pub prefix: Punctuated<Ident, Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050035 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080036 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070037 }),
38 /// A static item (`static` or `pub static`).
39 ///
40 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
41 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080042 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070043 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080044 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050045 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070046 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080047 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080048 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070050 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070052 }),
53 /// A constant item (`const` or `pub const`).
54 ///
55 /// E.g. `const FOO: i32 = 42;`
56 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080057 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070058 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070060 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080061 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080063 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070064 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070066 }),
67 /// A function declaration (`fn` or `pub fn`).
68 ///
69 /// E.g. `fn foo(bar: usize) -> usize { .. }`
70 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080071 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070072 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050073 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050074 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070076 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050077 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070078 pub block: Box<Block>,
79 }),
80 /// A module declaration (`mod` or `pub mod`).
81 ///
82 /// E.g. `mod foo;` or `mod foo { .. }`
83 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080084 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070085 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080086 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070087 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050088 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080089 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 }),
91 /// An external module (`extern` or `pub extern`).
92 ///
93 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070094 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070096 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050097 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098 pub items: Vec<ForeignItem>,
99 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 /// A type alias (`type` or `pub type`).
101 ///
102 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800103 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800104 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700105 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800106 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700107 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700108 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800110 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500113 /// A struct definition (`struct` or `pub struct`).
114 ///
115 /// E.g. `struct Foo<A> { x: A }`
116 pub Struct(ItemStruct {
117 pub attrs: Vec<Attribute>,
118 pub vis: Visibility,
119 pub struct_token: Token![struct],
120 pub ident: Ident,
121 pub generics: Generics,
122 pub fields: Fields,
123 pub semi_token: Option<Token![;]>,
124 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700125 /// An enum definition (`enum` or `pub enum`).
126 ///
127 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
128 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
133 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500134 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500135 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 /// A union definition (`union` or `pub union`).
138 ///
139 /// E.g. `union Foo<A, B> { x: A, y: B }`
140 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800141 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700142 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800143 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700144 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500146 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 }),
148 /// A Trait declaration (`trait` or `pub trait`).
149 ///
150 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
151 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800152 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700153 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500154 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500155 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800156 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700157 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700158 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800159 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500160 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500161 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 pub items: Vec<TraitItem>,
163 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700164 /// An implementation.
165 ///
166 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
167 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800168 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500169 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500170 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700173 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500174 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700175 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800176 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500177 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 pub items: Vec<ImplItem>,
179 }),
180 /// A macro invocation (which includes macro definition).
181 ///
182 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800183 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800185 /// The `example` in `macro_rules! example { ... }`.
186 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800187 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500188 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800189 }),
David Tolnay9c76bcb2017-12-26 23:14:59 -0500190 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800191 pub attrs: Vec<Attribute>,
192 pub vis: Visibility,
193 pub macro_token: Token![macro],
194 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500195 pub paren_token: Paren,
196 pub args: TokenStream,
197 pub brace_token: Brace,
198 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800199 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500200 pub Verbatim(ItemVerbatim #manual_extra_traits {
201 pub tts: TokenStream,
202 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 }
David Tolnayb79ee962016-09-04 09:39:20 -0700204}
205
David Tolnay9c76bcb2017-12-26 23:14:59 -0500206#[cfg(feature = "extra-traits")]
207impl Eq for ItemMacro2 {}
208
209#[cfg(feature = "extra-traits")]
210impl PartialEq for ItemMacro2 {
211 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500212 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnayab919512017-12-30 23:31:51 -0500213 && self.ident == other.ident && self.paren_token == other.paren_token
214 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
215 && self.brace_token == other.brace_token
216 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500217 }
218}
219
220#[cfg(feature = "extra-traits")]
221impl Hash for ItemMacro2 {
222 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500223 where
224 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500225 {
226 self.attrs.hash(state);
227 self.vis.hash(state);
228 self.macro_token.hash(state);
229 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500230 self.paren_token.hash(state);
231 TokenStreamHelper(&self.args).hash(state);
232 self.brace_token.hash(state);
233 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500234 }
235}
236
David Tolnay2ae520a2017-12-29 11:19:50 -0500237#[cfg(feature = "extra-traits")]
238impl Eq for ItemVerbatim {}
239
240#[cfg(feature = "extra-traits")]
241impl PartialEq for ItemVerbatim {
242 fn eq(&self, other: &Self) -> bool {
243 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
244 }
245}
246
247#[cfg(feature = "extra-traits")]
248impl Hash for ItemVerbatim {
249 fn hash<H>(&self, state: &mut H)
250 where
251 H: Hasher,
252 {
253 TokenStreamHelper(&self.tts).hash(state);
254 }
255}
256
David Tolnay0e837402016-12-22 17:25:55 -0500257impl From<DeriveInput> for Item {
258 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500259 match input.data {
260 Data::Struct(data) => Item::Struct(ItemStruct {
261 attrs: input.attrs,
262 vis: input.vis,
263 struct_token: data.struct_token,
264 ident: input.ident,
265 generics: input.generics,
266 fields: data.fields,
267 semi_token: data.semi_token,
268 }),
269 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500270 attrs: input.attrs,
271 vis: input.vis,
272 enum_token: data.enum_token,
273 ident: input.ident,
274 generics: input.generics,
275 brace_token: data.brace_token,
276 variants: data.variants,
277 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500278 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500279 attrs: input.attrs,
280 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500281 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500282 ident: input.ident,
283 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500284 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500285 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700286 }
287 }
288}
289
Alex Crichton62a0a592017-05-22 13:58:53 -0700290ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500291 /// Things that can appear directly inside of a module.
292 pub enum UseTree {
293 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
294 pub Path(UsePath {
295 pub ident: Ident,
296 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500298 /// `use prefix::*`
299 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800300 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700301 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500302 /// `use prefix::{a, b, c}`
303 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500304 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500305 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 }),
307 }
308}
309
Alex Crichton62a0a592017-05-22 13:58:53 -0700310ast_enum_of_structs! {
311 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800312 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 /// A foreign function
314 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800315 pub attrs: Vec<Attribute>,
316 pub vis: Visibility,
317 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 }),
321 /// A foreign static item (`static ext: u8`)
322 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800323 pub attrs: Vec<Attribute>,
324 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800325 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500326 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800327 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800328 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800329 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800330 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800332 /// A foreign type
333 pub Type(ForeignItemType {
334 pub attrs: Vec<Attribute>,
335 pub vis: Visibility,
336 pub type_token: Token![type],
337 pub ident: Ident,
338 pub semi_token: Token![;],
339 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500340 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
341 pub tts: TokenStream,
342 }),
343 }
344}
345
346#[cfg(feature = "extra-traits")]
347impl Eq for ForeignItemVerbatim {}
348
349#[cfg(feature = "extra-traits")]
350impl PartialEq for ForeignItemVerbatim {
351 fn eq(&self, other: &Self) -> bool {
352 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
353 }
354}
355
356#[cfg(feature = "extra-traits")]
357impl Hash for ForeignItemVerbatim {
358 fn hash<H>(&self, state: &mut H)
359 where
360 H: Hasher,
361 {
362 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700364}
365
David Tolnayda705bd2017-11-10 21:58:05 -0800366ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700367 /// Represents an item declaration within a trait declaration,
368 /// possibly including a default implementation. A trait item is
369 /// either required (meaning it doesn't have an implementation, just a
370 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800371 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800373 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800374 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700375 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800376 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800377 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800378 pub default: Option<(Token![=], Expr)>,
379 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 }),
381 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800382 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 pub sig: MethodSig,
384 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 }),
387 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800388 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800389 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700390 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500391 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800392 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500393 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800394 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800395 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800397 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800398 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800399 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500400 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800401 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500402 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
403 pub tts: TokenStream,
404 }),
405 }
406}
407
408#[cfg(feature = "extra-traits")]
409impl Eq for TraitItemVerbatim {}
410
411#[cfg(feature = "extra-traits")]
412impl PartialEq for TraitItemVerbatim {
413 fn eq(&self, other: &Self) -> bool {
414 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
415 }
416}
417
418#[cfg(feature = "extra-traits")]
419impl Hash for TraitItemVerbatim {
420 fn hash<H>(&self, state: &mut H)
421 where
422 H: Hasher,
423 {
424 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700425 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700426}
427
Alex Crichton62a0a592017-05-22 13:58:53 -0700428ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800429 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800431 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700432 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500433 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700435 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800436 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800437 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800438 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700441 }),
442 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800443 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700444 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500445 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 pub sig: MethodSig,
447 pub block: Block,
448 }),
449 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800450 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700451 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500452 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800453 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700454 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500455 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800456 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800457 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800458 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700459 }),
David Tolnay857628c2017-11-11 12:25:31 -0800460 pub Macro(ImplItemMacro {
461 pub attrs: Vec<Attribute>,
462 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500463 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800464 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500465 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
466 pub tts: TokenStream,
467 }),
468 }
469}
470
471#[cfg(feature = "extra-traits")]
472impl Eq for ImplItemVerbatim {}
473
474#[cfg(feature = "extra-traits")]
475impl PartialEq for ImplItemVerbatim {
476 fn eq(&self, other: &Self) -> bool {
477 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
478 }
479}
480
481#[cfg(feature = "extra-traits")]
482impl Hash for ImplItemVerbatim {
483 fn hash<H>(&self, state: &mut H)
484 where
485 H: Hasher,
486 {
487 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700488 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700489}
490
491ast_struct! {
492 /// Represents a method's signature in a trait declaration,
493 /// or in an implementation.
494 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500495 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500496 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700498 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700499 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 }
501}
502
503ast_struct! {
504 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700505 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 /// E.g. `fn foo(bar: baz)`
507 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800508 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500509 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500510 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500511 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500512 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500513 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700515}
516
Alex Crichton62a0a592017-05-22 13:58:53 -0700517ast_enum_of_structs! {
518 /// An argument in a function header.
519 ///
520 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
521 pub enum FnArg {
522 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800523 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700524 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500525 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500526 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700527 }),
528 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500529 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800530 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700531 }),
532 pub Captured(ArgCaptured {
533 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800534 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800535 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700536 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500537 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800538 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700539 }
David Tolnay62f374c2016-10-02 13:37:00 -0700540}
541
David Tolnayedf2b992016-09-23 20:43:45 -0700542#[cfg(feature = "parsing")]
543pub mod parsing {
544 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700545
David Tolnay03342952017-12-29 11:52:00 -0500546 use synom::{Synom, Cursor, PResult};
David Tolnay84aa0752016-10-02 23:01:13 -0700547
David Tolnay4c614be2017-11-10 00:02:38 -0800548 impl_synom!(Item "item" alt!(
549 syn!(ItemExternCrate) => { Item::ExternCrate }
550 |
551 syn!(ItemUse) => { Item::Use }
552 |
553 syn!(ItemStatic) => { Item::Static }
554 |
555 syn!(ItemConst) => { Item::Const }
556 |
557 syn!(ItemFn) => { Item::Fn }
558 |
559 syn!(ItemMod) => { Item::Mod }
560 |
561 syn!(ItemForeignMod) => { Item::ForeignMod }
562 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800563 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800564 |
565 syn!(ItemStruct) => { Item::Struct }
566 |
567 syn!(ItemEnum) => { Item::Enum }
568 |
569 syn!(ItemUnion) => { Item::Union }
570 |
571 syn!(ItemTrait) => { Item::Trait }
572 |
David Tolnay03342952017-12-29 11:52:00 -0500573 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800574 |
575 syn!(ItemImpl) => { Item::Impl }
576 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800577 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800578 |
579 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800580 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700581
David Tolnaydecf28d2017-11-11 11:56:45 -0800582 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500583 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700584 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800585 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700586 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500587 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500588 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800589 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700590 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800591 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800592 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500593 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700594 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500595 delimiter: body.0,
596 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800597 },
David Tolnay57292da2017-12-27 21:03:33 -0500598 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800599 })
David Tolnayedf2b992016-09-23 20:43:45 -0700600 ));
601
David Tolnay500d8322017-12-18 00:32:51 -0800602 // TODO: figure out the actual grammar; is body required to be braced?
603 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500604 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800605 vis: syn!(Visibility) >>
606 macro_: keyword!(macro) >>
607 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500608 args: call!(tt::parenthesized) >>
609 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800610 (ItemMacro2 {
611 attrs: attrs,
612 vis: vis,
613 macro_token: macro_,
614 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500615 paren_token: args.0,
616 args: args.1,
617 brace_token: body.0,
618 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800619 })
620 ));
621
David Tolnay4c614be2017-11-10 00:02:38 -0800622 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500623 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700624 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800625 extern_: keyword!(extern) >>
626 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700627 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
629 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800630 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700631 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800632 vis: vis,
633 extern_token: extern_,
634 crate_token: crate_,
635 ident: ident,
636 rename: rename,
637 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800638 })
David Tolnayedf2b992016-09-23 20:43:45 -0700639 ));
640
David Tolnay4c614be2017-11-10 00:02:38 -0800641 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500642 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700643 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500645 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500646 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500647 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500648 true => syn!(UseTree)
649 |
David Tolnay8edcef12017-12-28 12:06:52 -0500650 false => alt!(
651 tuple!(keyword!(as), syn!(Ident)) => {
652 |rename| UseTree::Path(UsePath {
653 ident: prefix.pop().unwrap().into_item(),
654 rename: Some(rename),
655 })
656 }
David Tolnay5f332a92017-12-26 00:42:45 -0500657 |
David Tolnay8edcef12017-12-28 12:06:52 -0500658 epsilon!() => {
659 |_| UseTree::Path(UsePath {
660 ident: prefix.pop().unwrap().into_item(),
661 rename: None,
662 })
663 }
David Tolnay5f332a92017-12-26 00:42:45 -0500664 )
665 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800667 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700668 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800669 vis: vis,
670 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500671 leading_colon: leading_colon,
672 prefix: prefix,
673 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800674 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800675 })
David Tolnay4a057422016-10-08 00:02:31 -0700676 ));
677
David Tolnay5f332a92017-12-26 00:42:45 -0500678 named!(use_prefix -> Ident, alt!(
679 syn!(Ident)
680 |
681 keyword!(self) => { Into::into }
682 |
683 keyword!(super) => { Into::into }
684 |
685 keyword!(crate) => { Into::into }
686 ));
687
688 impl_synom!(UseTree "use tree" alt!(
689 syn!(UsePath) => { UseTree::Path }
690 |
691 syn!(UseGlob) => { UseTree::Glob }
692 |
693 syn!(UseList) => { UseTree::List }
694 ));
695
696 impl_synom!(UsePath "use path" do_parse!(
697 ident: alt!(
698 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400699 |
David Tolnay5f332a92017-12-26 00:42:45 -0500700 keyword!(self) => { Into::into }
701 ) >>
702 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
703 (UsePath {
704 ident: ident,
705 rename: rename,
706 })
707 ));
David Tolnay4a057422016-10-08 00:02:31 -0700708
David Tolnay5f332a92017-12-26 00:42:45 -0500709 impl_synom!(UseGlob "use glob" do_parse!(
710 star: punct!(*) >>
711 (UseGlob {
712 star_token: star,
713 })
714 ));
David Tolnay4a057422016-10-08 00:02:31 -0700715
David Tolnay5f332a92017-12-26 00:42:45 -0500716 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500717 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500718 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500719 brace_token: list.0,
720 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500721 })
722 ));
David Tolnay4a057422016-10-08 00:02:31 -0700723
David Tolnay4c614be2017-11-10 00:02:38 -0800724 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500725 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700726 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800727 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500728 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700729 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800730 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800731 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800732 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700733 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800735 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700736 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800737 vis: vis,
738 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500739 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800740 ident: ident,
741 colon_token: colon,
742 ty: Box::new(ty),
743 eq_token: eq,
744 expr: Box::new(value),
745 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800746 })
David Tolnay47a877c2016-10-01 16:50:55 -0700747 ));
748
David Tolnay4c614be2017-11-10 00:02:38 -0800749 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500750 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700751 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800752 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700753 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800754 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800755 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800756 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700757 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800758 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800759 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700760 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800761 vis: vis,
762 const_token: const_,
763 ident: ident,
764 colon_token: colon,
765 ty: Box::new(ty),
766 eq_token: eq,
767 expr: Box::new(value),
768 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800769 })
David Tolnay47a877c2016-10-01 16:50:55 -0700770 ));
771
David Tolnay4c614be2017-11-10 00:02:38 -0800772 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500773 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700774 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500775 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500776 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700777 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700779 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700780 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500781 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800782 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500783 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700784 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500785 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700786 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400787 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800788 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700789 attrs: {
790 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500791 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700792 attrs
793 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800794 vis: vis,
795 constness: constness,
796 unsafety: unsafety,
797 abi: abi,
798 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800799 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500800 paren_token: inputs.0,
801 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800802 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500803 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800804 generics: Generics {
805 where_clause: where_clause,
806 .. generics
807 },
808 }),
809 ident: ident,
810 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500811 brace_token: inner_attrs_stmts.0,
812 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800813 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800814 })
David Tolnay42602292016-10-01 22:25:45 -0700815 ));
816
Alex Crichton954046c2017-05-30 21:49:42 -0700817 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400818 named!(parse -> Self, alt!(
819 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800820 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400821 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500822 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800823 self_: keyword!(self) >>
824 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400825 (ArgSelfRef {
826 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500827 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400828 and_token: and,
829 self_token: self_,
830 }.into())
831 )
832 |
833 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500834 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800835 self_: keyword!(self) >>
836 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400837 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500838 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400839 self_token: self_,
840 }.into())
841 )
842 |
843 do_parse!(
844 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800845 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800846 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400847 (ArgCaptured {
848 pat: pat,
849 ty: ty,
850 colon_token: colon,
851 }.into())
852 )
853 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800854 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400855 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800856
857 fn description() -> Option<&'static str> {
858 Some("function argument")
859 }
Alex Crichton954046c2017-05-30 21:49:42 -0700860 }
David Tolnay62f374c2016-10-02 13:37:00 -0700861
David Tolnay4c614be2017-11-10 00:02:38 -0800862 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500863 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700864 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800865 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700866 ident: syn!(Ident) >>
867 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800868 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700869 Vec::new(),
870 None,
871 Some(semi),
872 )}
David Tolnay37d10332016-10-13 20:51:04 -0700873 |
Alex Crichton954046c2017-05-30 21:49:42 -0700874 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700875 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500876 many0!(Attribute::parse_inner),
877 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400878 )
David Tolnay8875fca2017-12-31 13:52:37 -0500879 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -0700880 inner_attrs,
881 Some((brace, items)),
882 None,
883 )}
David Tolnay37d10332016-10-13 20:51:04 -0700884 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800885 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700886 attrs: {
887 let mut attrs = outer_attrs;
888 attrs.extend(content_semi.0);
889 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700890 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800891 vis: vis,
892 mod_token: mod_,
893 ident: ident,
894 content: content_semi.1,
895 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800896 })
David Tolnay35902302016-10-06 01:11:08 -0700897 ));
898
David Tolnay4c614be2017-11-10 00:02:38 -0800899 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500900 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700901 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500902 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800903 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700904 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800905 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -0500906 brace_token: items.0,
907 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800908 })
David Tolnay35902302016-10-06 01:11:08 -0700909 ));
910
David Tolnay8894f602017-11-11 12:11:04 -0800911 impl_synom!(ForeignItem "foreign item" alt!(
912 syn!(ForeignItemFn) => { ForeignItem::Fn }
913 |
914 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800915 |
916 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800917 ));
David Tolnay35902302016-10-06 01:11:08 -0700918
David Tolnay8894f602017-11-11 12:11:04 -0800919 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500920 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700921 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800922 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700923 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700924 generics: syn!(Generics) >>
925 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500926 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500927 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700928 (args, variadic)
929 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800930 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500931 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800932 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700933 ({
David Tolnay8875fca2017-12-31 13:52:37 -0500934 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800935 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700936 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700937 attrs: attrs,
938 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800939 decl: Box::new(FnDecl {
940 fn_token: fn_,
941 paren_token: parens,
942 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500943 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800944 output: ret,
945 generics: Generics {
946 where_clause: where_clause,
947 .. generics
948 },
949 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700950 vis: vis,
951 }
David Tolnay35902302016-10-06 01:11:08 -0700952 })
953 ));
954
David Tolnay8894f602017-11-11 12:11:04 -0800955 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500956 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700957 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800958 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500959 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700960 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800961 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800962 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800964 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700965 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700966 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700967 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800968 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500969 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800970 static_token: static_,
971 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700972 vis: vis,
973 })
974 ));
975
David Tolnay199bcbb2017-11-12 10:33:52 -0800976 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500977 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800978 vis: syn!(Visibility) >>
979 type_: keyword!(type) >>
980 ident: syn!(Ident) >>
981 semi: punct!(;) >>
982 (ForeignItemType {
983 attrs: attrs,
984 vis: vis,
985 type_token: type_,
986 ident: ident,
987 semi_token: semi,
988 })
989 ));
990
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800991 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500992 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700993 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800994 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700995 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700996 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500997 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800998 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800999 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001000 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001001 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001002 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001003 vis: vis,
1004 type_token: type_,
1005 ident: ident,
1006 generics: Generics {
1007 where_clause: where_clause,
1008 ..generics
1009 },
1010 eq_token: eq,
1011 ty: Box::new(ty),
1012 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001013 })
David Tolnay3cf52982016-10-01 17:11:37 -07001014 ));
1015
David Tolnay4c614be2017-11-10 00:02:38 -08001016 impl_synom!(ItemStruct "struct item" switch!(
1017 map!(syn!(DeriveInput), Into::into),
1018 Item::Struct(item) => value!(item)
1019 |
1020 _ => reject!()
1021 ));
David Tolnay42602292016-10-01 22:25:45 -07001022
David Tolnay4c614be2017-11-10 00:02:38 -08001023 impl_synom!(ItemEnum "enum item" switch!(
1024 map!(syn!(DeriveInput), Into::into),
1025 Item::Enum(item) => value!(item)
1026 |
1027 _ => reject!()
1028 ));
1029
1030 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001031 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001032 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001034 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001035 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001036 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001037 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001038 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001039 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001040 vis: vis,
1041 union_token: union_,
1042 ident: ident,
1043 generics: Generics {
1044 where_clause: where_clause,
1045 .. generics
1046 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001047 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001048 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001049 ));
1050
David Tolnay4c614be2017-11-10 00:02:38 -08001051 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001052 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001053 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001054 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001055 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001056 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001057 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001058 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001059 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001060 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001061 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001062 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001063 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001064 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001065 vis: vis,
1066 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001067 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001068 trait_token: trait_,
1069 ident: ident,
1070 generics: Generics {
1071 where_clause: where_clause,
1072 .. generics
1073 },
1074 colon_token: colon,
1075 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001076 brace_token: body.0,
1077 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001078 })
David Tolnay0aecb732016-10-03 23:03:50 -07001079 ));
1080
David Tolnay03342952017-12-29 11:52:00 -05001081 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1082 Ok((cursor, cursor))
1083 }
1084
1085 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1086 begin: call!(grab_cursor) >>
1087 many0!(Attribute::parse_outer) >>
1088 option!(keyword!(unsafe)) >>
1089 keyword!(impl) >>
1090 syn!(Path) >>
1091 keyword!(for) >>
1092 punct!(..) >>
1093 braces!(epsilon!()) >>
1094 end: call!(grab_cursor) >>
1095 ({
1096 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1097 let len = tts.len() - end.token_stream().into_iter().count();
1098 ItemVerbatim {
1099 tts: tts.into_iter().take(len).collect(),
1100 }
David Tolnay4c614be2017-11-10 00:02:38 -08001101 })
David Tolnayf94e2362016-10-04 00:29:51 -07001102 ));
1103
David Tolnayda705bd2017-11-10 21:58:05 -08001104 impl_synom!(TraitItem "trait item" alt!(
1105 syn!(TraitItemConst) => { TraitItem::Const }
1106 |
1107 syn!(TraitItemMethod) => { TraitItem::Method }
1108 |
1109 syn!(TraitItemType) => { TraitItem::Type }
1110 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001111 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001112 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001113
David Tolnayda705bd2017-11-10 21:58:05 -08001114 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001115 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001116 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001117 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001118 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001119 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001120 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1121 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001122 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001123 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001124 const_token: const_,
1125 ident: ident,
1126 colon_token: colon,
1127 ty: ty,
1128 default: default,
1129 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001130 })
1131 ));
1132
David Tolnayda705bd2017-11-10 21:58:05 -08001133 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001134 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001135 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001136 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001137 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001138 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001139 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001140 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001141 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001142 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001143 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001144 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001145 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001146 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001147 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001148 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001149 ({
1150 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001151 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001152 None => (Vec::new(), None),
1153 };
David Tolnayda705bd2017-11-10 21:58:05 -08001154 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001155 attrs: {
1156 let mut attrs = outer_attrs;
1157 attrs.extend(inner_attrs);
1158 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001159 },
David Tolnayda705bd2017-11-10 21:58:05 -08001160 sig: MethodSig {
1161 constness: constness,
1162 unsafety: unsafety,
1163 abi: abi,
1164 ident: ident,
1165 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001166 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001167 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001168 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001169 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001170 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001171 generics: Generics {
1172 where_clause: where_clause,
1173 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001174 },
1175 },
David Tolnayda705bd2017-11-10 21:58:05 -08001176 },
1177 default: stmts.map(|stmts| {
1178 Block {
1179 stmts: stmts.0,
1180 brace_token: stmts.1,
1181 }
1182 }),
1183 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001184 }
David Tolnay0aecb732016-10-03 23:03:50 -07001185 })
1186 ));
1187
David Tolnayda705bd2017-11-10 21:58:05 -08001188 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001189 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001190 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001191 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001192 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001193 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001194 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001195 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001196 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001197 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001198 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001199 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001200 type_token: type_,
1201 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001202 generics: Generics {
1203 where_clause: where_clause,
1204 .. generics
1205 },
David Tolnayda705bd2017-11-10 21:58:05 -08001206 colon_token: colon,
1207 bounds: bounds.unwrap_or_default(),
1208 default: default,
1209 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001210 })
1211 ));
1212
David Tolnaydecf28d2017-11-11 11:56:45 -08001213 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001214 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001215 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001216 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001217 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001218 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001219 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001220 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001221 })
1222 ));
1223
David Tolnay4c614be2017-11-10 00:02:38 -08001224 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001225 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001226 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001227 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001228 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001229 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001230 polarity_path: alt!(
1231 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001232 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001233 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001234 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001235 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001236 )
1237 |
David Tolnay570695e2017-06-03 16:15:13 -07001238 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001239 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001240 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001241 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001242 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001243 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001244 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001245 defaultness: defaultness,
1246 unsafety: unsafety,
1247 impl_token: impl_,
1248 generics: Generics {
1249 where_clause: where_clause,
1250 .. generics
1251 },
1252 trait_: polarity_path,
1253 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001254 brace_token: body.0,
1255 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001256 })
David Tolnay4c9be372016-10-06 00:47:37 -07001257 ));
1258
David Tolnay857628c2017-11-11 12:25:31 -08001259 impl_synom!(ImplItem "item in impl block" alt!(
1260 syn!(ImplItemConst) => { ImplItem::Const }
1261 |
1262 syn!(ImplItemMethod) => { ImplItem::Method }
1263 |
1264 syn!(ImplItemType) => { ImplItem::Type }
1265 |
1266 syn!(ImplItemMacro) => { ImplItem::Macro }
1267 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001268
David Tolnay857628c2017-11-11 12:25:31 -08001269 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001270 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001271 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001272 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001273 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001274 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001275 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001276 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001277 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001278 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001279 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001280 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001281 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001282 vis: vis,
1283 defaultness: defaultness,
1284 const_token: const_,
1285 ident: ident,
1286 colon_token: colon,
1287 ty: ty,
1288 eq_token: eq,
1289 expr: value,
1290 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001291 })
1292 ));
1293
David Tolnay857628c2017-11-11 12:25:31 -08001294 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001295 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001297 defaultness: option!(keyword!(default)) >>
1298 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001299 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001300 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001301 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001302 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001303 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001304 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001305 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001306 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001307 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001308 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001309 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001310 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001311 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001312 attrs: {
1313 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001314 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001315 attrs
1316 },
David Tolnay857628c2017-11-11 12:25:31 -08001317 vis: vis,
1318 defaultness: defaultness,
1319 sig: MethodSig {
1320 constness: constness,
1321 unsafety: unsafety,
1322 abi: abi,
1323 ident: ident,
1324 decl: FnDecl {
1325 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001326 paren_token: inputs.0,
1327 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001328 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001329 generics: Generics {
1330 where_clause: where_clause,
1331 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001332 },
David Tolnayd2836e22017-12-27 23:13:00 -05001333 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001334 },
David Tolnay857628c2017-11-11 12:25:31 -08001335 },
1336 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001337 brace_token: inner_attrs_stmts.0,
1338 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001339 },
David Tolnay4c9be372016-10-06 00:47:37 -07001340 })
1341 ));
1342
David Tolnay857628c2017-11-11 12:25:31 -08001343 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001344 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001345 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001346 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001348 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001349 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001350 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001351 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001352 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001353 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001354 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001355 vis: vis,
1356 defaultness: defaultness,
1357 type_token: type_,
1358 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001359 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001360 eq_token: eq,
1361 ty: ty,
1362 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001363 })
1364 ));
1365
David Tolnay857628c2017-11-11 12:25:31 -08001366 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001367 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001368 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001369 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001370 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001371 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001372 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001373 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001374 })
1375 ));
1376
David Tolnayab919512017-12-30 23:31:51 -05001377 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1378 match *delimiter {
1379 MacroDelimiter::Brace(_) => true,
1380 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001381 }
1382 }
David Tolnayedf2b992016-09-23 20:43:45 -07001383}
David Tolnay4a51dc72016-10-01 00:40:31 -07001384
1385#[cfg(feature = "printing")]
1386mod printing {
1387 use super::*;
1388 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001389 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001390
David Tolnay1bfa7332017-11-11 12:41:20 -08001391 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001392 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001393 tokens.append_all(self.attrs.outer());
1394 self.vis.to_tokens(tokens);
1395 self.extern_token.to_tokens(tokens);
1396 self.crate_token.to_tokens(tokens);
1397 self.ident.to_tokens(tokens);
1398 if let Some((ref as_token, ref rename)) = self.rename {
1399 as_token.to_tokens(tokens);
1400 rename.to_tokens(tokens);
1401 }
1402 self.semi_token.to_tokens(tokens);
1403 }
1404 }
1405
1406 impl ToTokens for ItemUse {
1407 fn to_tokens(&self, tokens: &mut Tokens) {
1408 tokens.append_all(self.attrs.outer());
1409 self.vis.to_tokens(tokens);
1410 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001411 self.leading_colon.to_tokens(tokens);
1412 self.prefix.to_tokens(tokens);
1413 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001414 self.semi_token.to_tokens(tokens);
1415 }
1416 }
1417
1418 impl ToTokens for ItemStatic {
1419 fn to_tokens(&self, tokens: &mut Tokens) {
1420 tokens.append_all(self.attrs.outer());
1421 self.vis.to_tokens(tokens);
1422 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001423 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001424 self.ident.to_tokens(tokens);
1425 self.colon_token.to_tokens(tokens);
1426 self.ty.to_tokens(tokens);
1427 self.eq_token.to_tokens(tokens);
1428 self.expr.to_tokens(tokens);
1429 self.semi_token.to_tokens(tokens);
1430 }
1431 }
1432
1433 impl ToTokens for ItemConst {
1434 fn to_tokens(&self, tokens: &mut Tokens) {
1435 tokens.append_all(self.attrs.outer());
1436 self.vis.to_tokens(tokens);
1437 self.const_token.to_tokens(tokens);
1438 self.ident.to_tokens(tokens);
1439 self.colon_token.to_tokens(tokens);
1440 self.ty.to_tokens(tokens);
1441 self.eq_token.to_tokens(tokens);
1442 self.expr.to_tokens(tokens);
1443 self.semi_token.to_tokens(tokens);
1444 }
1445 }
1446
1447 impl ToTokens for ItemFn {
1448 fn to_tokens(&self, tokens: &mut Tokens) {
1449 tokens.append_all(self.attrs.outer());
1450 self.vis.to_tokens(tokens);
1451 self.constness.to_tokens(tokens);
1452 self.unsafety.to_tokens(tokens);
1453 self.abi.to_tokens(tokens);
1454 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1455 self.block.brace_token.surround(tokens, |tokens| {
1456 tokens.append_all(self.attrs.inner());
1457 tokens.append_all(&self.block.stmts);
1458 });
1459 }
1460 }
1461
1462 impl ToTokens for ItemMod {
1463 fn to_tokens(&self, tokens: &mut Tokens) {
1464 tokens.append_all(self.attrs.outer());
1465 self.vis.to_tokens(tokens);
1466 self.mod_token.to_tokens(tokens);
1467 self.ident.to_tokens(tokens);
1468 if let Some((ref brace, ref items)) = self.content {
1469 brace.surround(tokens, |tokens| {
1470 tokens.append_all(self.attrs.inner());
1471 tokens.append_all(items);
1472 });
1473 } else {
1474 TokensOrDefault(&self.semi).to_tokens(tokens);
1475 }
1476 }
1477 }
1478
1479 impl ToTokens for ItemForeignMod {
1480 fn to_tokens(&self, tokens: &mut Tokens) {
1481 tokens.append_all(self.attrs.outer());
1482 self.abi.to_tokens(tokens);
1483 self.brace_token.surround(tokens, |tokens| {
1484 tokens.append_all(&self.items);
1485 });
1486 }
1487 }
1488
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001489 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001490 fn to_tokens(&self, tokens: &mut Tokens) {
1491 tokens.append_all(self.attrs.outer());
1492 self.vis.to_tokens(tokens);
1493 self.type_token.to_tokens(tokens);
1494 self.ident.to_tokens(tokens);
1495 self.generics.to_tokens(tokens);
1496 self.generics.where_clause.to_tokens(tokens);
1497 self.eq_token.to_tokens(tokens);
1498 self.ty.to_tokens(tokens);
1499 self.semi_token.to_tokens(tokens);
1500 }
1501 }
1502
1503 impl ToTokens for ItemEnum {
1504 fn to_tokens(&self, tokens: &mut Tokens) {
1505 tokens.append_all(self.attrs.outer());
1506 self.vis.to_tokens(tokens);
1507 self.enum_token.to_tokens(tokens);
1508 self.ident.to_tokens(tokens);
1509 self.generics.to_tokens(tokens);
1510 self.generics.where_clause.to_tokens(tokens);
1511 self.brace_token.surround(tokens, |tokens| {
1512 self.variants.to_tokens(tokens);
1513 });
1514 }
1515 }
1516
1517 impl ToTokens for ItemStruct {
1518 fn to_tokens(&self, tokens: &mut Tokens) {
1519 tokens.append_all(self.attrs.outer());
1520 self.vis.to_tokens(tokens);
1521 self.struct_token.to_tokens(tokens);
1522 self.ident.to_tokens(tokens);
1523 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001524 match self.fields {
1525 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001526 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001527 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001528 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001529 Fields::Unnamed(ref fields) => {
1530 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001531 self.generics.where_clause.to_tokens(tokens);
1532 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001533 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001534 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001535 self.generics.where_clause.to_tokens(tokens);
1536 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001537 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001538 }
1539 }
1540 }
1541
1542 impl ToTokens for ItemUnion {
1543 fn to_tokens(&self, tokens: &mut Tokens) {
1544 tokens.append_all(self.attrs.outer());
1545 self.vis.to_tokens(tokens);
1546 self.union_token.to_tokens(tokens);
1547 self.ident.to_tokens(tokens);
1548 self.generics.to_tokens(tokens);
1549 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001550 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001551 }
1552 }
1553
1554 impl ToTokens for ItemTrait {
1555 fn to_tokens(&self, tokens: &mut Tokens) {
1556 tokens.append_all(self.attrs.outer());
1557 self.vis.to_tokens(tokens);
1558 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001559 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001560 self.trait_token.to_tokens(tokens);
1561 self.ident.to_tokens(tokens);
1562 self.generics.to_tokens(tokens);
1563 if !self.supertraits.is_empty() {
1564 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1565 self.supertraits.to_tokens(tokens);
1566 }
1567 self.generics.where_clause.to_tokens(tokens);
1568 self.brace_token.surround(tokens, |tokens| {
1569 tokens.append_all(&self.items);
1570 });
1571 }
1572 }
1573
David Tolnay1bfa7332017-11-11 12:41:20 -08001574 impl ToTokens for ItemImpl {
1575 fn to_tokens(&self, tokens: &mut Tokens) {
1576 tokens.append_all(self.attrs.outer());
1577 self.defaultness.to_tokens(tokens);
1578 self.unsafety.to_tokens(tokens);
1579 self.impl_token.to_tokens(tokens);
1580 self.generics.to_tokens(tokens);
1581 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1582 polarity.to_tokens(tokens);
1583 path.to_tokens(tokens);
1584 for_token.to_tokens(tokens);
1585 }
1586 self.self_ty.to_tokens(tokens);
1587 self.generics.where_clause.to_tokens(tokens);
1588 self.brace_token.surround(tokens, |tokens| {
1589 tokens.append_all(&self.items);
1590 });
1591 }
1592 }
1593
1594 impl ToTokens for ItemMacro {
1595 fn to_tokens(&self, tokens: &mut Tokens) {
1596 tokens.append_all(self.attrs.outer());
1597 self.mac.path.to_tokens(tokens);
1598 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001599 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001600 match self.mac.delimiter {
1601 MacroDelimiter::Paren(ref paren) => {
1602 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1603 }
1604 MacroDelimiter::Brace(ref brace) => {
1605 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1606 }
1607 MacroDelimiter::Bracket(ref bracket) => {
1608 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1609 }
1610 }
David Tolnay57292da2017-12-27 21:03:33 -05001611 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001612 }
1613 }
David Tolnay42602292016-10-01 22:25:45 -07001614
David Tolnay500d8322017-12-18 00:32:51 -08001615 impl ToTokens for ItemMacro2 {
1616 fn to_tokens(&self, tokens: &mut Tokens) {
1617 tokens.append_all(self.attrs.outer());
1618 self.vis.to_tokens(tokens);
1619 self.macro_token.to_tokens(tokens);
1620 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001621 self.paren_token.surround(tokens, |tokens| {
1622 self.args.to_tokens(tokens);
1623 });
1624 self.brace_token.surround(tokens, |tokens| {
1625 self.body.to_tokens(tokens);
1626 });
David Tolnay500d8322017-12-18 00:32:51 -08001627 }
1628 }
1629
David Tolnay2ae520a2017-12-29 11:19:50 -05001630 impl ToTokens for ItemVerbatim {
1631 fn to_tokens(&self, tokens: &mut Tokens) {
1632 self.tts.to_tokens(tokens);
1633 }
1634 }
1635
David Tolnay5f332a92017-12-26 00:42:45 -05001636 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001637 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001638 self.ident.to_tokens(tokens);
1639 if let Some((ref as_token, ref rename)) = self.rename {
1640 as_token.to_tokens(tokens);
1641 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001642 }
David Tolnay4a057422016-10-08 00:02:31 -07001643 }
1644 }
1645
David Tolnay5f332a92017-12-26 00:42:45 -05001646 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001647 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001648 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 }
1650 }
1651
David Tolnay5f332a92017-12-26 00:42:45 -05001652 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001653 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001654 self.brace_token.surround(tokens, |tokens| {
1655 self.items.to_tokens(tokens);
1656 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001657 }
1658 }
1659
David Tolnay1bfa7332017-11-11 12:41:20 -08001660 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001661 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001662 tokens.append_all(self.attrs.outer());
1663 self.const_token.to_tokens(tokens);
1664 self.ident.to_tokens(tokens);
1665 self.colon_token.to_tokens(tokens);
1666 self.ty.to_tokens(tokens);
1667 if let Some((ref eq_token, ref default)) = self.default {
1668 eq_token.to_tokens(tokens);
1669 default.to_tokens(tokens);
1670 }
1671 self.semi_token.to_tokens(tokens);
1672 }
1673 }
1674
1675 impl ToTokens for TraitItemMethod {
1676 fn to_tokens(&self, tokens: &mut Tokens) {
1677 tokens.append_all(self.attrs.outer());
1678 self.sig.to_tokens(tokens);
1679 match self.default {
1680 Some(ref block) => {
1681 block.brace_token.surround(tokens, |tokens| {
1682 tokens.append_all(self.attrs.inner());
1683 tokens.append_all(&block.stmts);
1684 });
David Tolnayca085422016-10-04 00:12:38 -07001685 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001686 None => {
1687 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001688 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001689 }
1690 }
1691 }
1692
1693 impl ToTokens for TraitItemType {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 tokens.append_all(self.attrs.outer());
1696 self.type_token.to_tokens(tokens);
1697 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001698 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001699 if !self.bounds.is_empty() {
1700 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1701 self.bounds.to_tokens(tokens);
1702 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001703 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001704 if let Some((ref eq_token, ref default)) = self.default {
1705 eq_token.to_tokens(tokens);
1706 default.to_tokens(tokens);
1707 }
1708 self.semi_token.to_tokens(tokens);
1709 }
1710 }
1711
1712 impl ToTokens for TraitItemMacro {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 tokens.append_all(self.attrs.outer());
1715 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001716 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001717 }
1718 }
1719
David Tolnay2ae520a2017-12-29 11:19:50 -05001720 impl ToTokens for TraitItemVerbatim {
1721 fn to_tokens(&self, tokens: &mut Tokens) {
1722 self.tts.to_tokens(tokens);
1723 }
1724 }
1725
David Tolnay857628c2017-11-11 12:25:31 -08001726 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001727 fn to_tokens(&self, tokens: &mut Tokens) {
1728 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001729 self.vis.to_tokens(tokens);
1730 self.defaultness.to_tokens(tokens);
1731 self.const_token.to_tokens(tokens);
1732 self.ident.to_tokens(tokens);
1733 self.colon_token.to_tokens(tokens);
1734 self.ty.to_tokens(tokens);
1735 self.eq_token.to_tokens(tokens);
1736 self.expr.to_tokens(tokens);
1737 self.semi_token.to_tokens(tokens);
1738 }
1739 }
1740
1741 impl ToTokens for ImplItemMethod {
1742 fn to_tokens(&self, tokens: &mut Tokens) {
1743 tokens.append_all(self.attrs.outer());
1744 self.vis.to_tokens(tokens);
1745 self.defaultness.to_tokens(tokens);
1746 self.sig.to_tokens(tokens);
1747 self.block.brace_token.surround(tokens, |tokens| {
1748 tokens.append_all(self.attrs.inner());
1749 tokens.append_all(&self.block.stmts);
1750 });
1751 }
1752 }
1753
1754 impl ToTokens for ImplItemType {
1755 fn to_tokens(&self, tokens: &mut Tokens) {
1756 tokens.append_all(self.attrs.outer());
1757 self.vis.to_tokens(tokens);
1758 self.defaultness.to_tokens(tokens);
1759 self.type_token.to_tokens(tokens);
1760 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001761 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001762 self.eq_token.to_tokens(tokens);
1763 self.ty.to_tokens(tokens);
1764 self.semi_token.to_tokens(tokens);
1765 }
1766 }
1767
1768 impl ToTokens for ImplItemMacro {
1769 fn to_tokens(&self, tokens: &mut Tokens) {
1770 tokens.append_all(self.attrs.outer());
1771 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001772 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001773 }
1774 }
1775
David Tolnay2ae520a2017-12-29 11:19:50 -05001776 impl ToTokens for ImplItemVerbatim {
1777 fn to_tokens(&self, tokens: &mut Tokens) {
1778 self.tts.to_tokens(tokens);
1779 }
1780 }
1781
David Tolnay8894f602017-11-11 12:11:04 -08001782 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001783 fn to_tokens(&self, tokens: &mut Tokens) {
1784 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001785 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001786 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1787 self.semi_token.to_tokens(tokens);
1788 }
1789 }
1790
1791 impl ToTokens for ForeignItemStatic {
1792 fn to_tokens(&self, tokens: &mut Tokens) {
1793 tokens.append_all(self.attrs.outer());
1794 self.vis.to_tokens(tokens);
1795 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001796 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001797 self.ident.to_tokens(tokens);
1798 self.colon_token.to_tokens(tokens);
1799 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001800 self.semi_token.to_tokens(tokens);
1801 }
1802 }
1803
David Tolnay199bcbb2017-11-12 10:33:52 -08001804 impl ToTokens for ForeignItemType {
1805 fn to_tokens(&self, tokens: &mut Tokens) {
1806 tokens.append_all(self.attrs.outer());
1807 self.vis.to_tokens(tokens);
1808 self.type_token.to_tokens(tokens);
1809 self.ident.to_tokens(tokens);
1810 self.semi_token.to_tokens(tokens);
1811 }
1812 }
1813
David Tolnay2ae520a2017-12-29 11:19:50 -05001814 impl ToTokens for ForeignItemVerbatim {
1815 fn to_tokens(&self, tokens: &mut Tokens) {
1816 self.tts.to_tokens(tokens);
1817 }
1818 }
1819
David Tolnay570695e2017-06-03 16:15:13 -07001820 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001822 self.constness.to_tokens(tokens);
1823 self.unsafety.to_tokens(tokens);
1824 self.abi.to_tokens(tokens);
1825 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001826 }
1827 }
1828
David Tolnay570695e2017-06-03 16:15:13 -07001829 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001830
1831 impl<'a> ToTokens for NamedDecl<'a> {
1832 fn to_tokens(&self, tokens: &mut Tokens) {
1833 self.0.fn_token.to_tokens(tokens);
1834 self.1.to_tokens(tokens);
1835 self.0.generics.to_tokens(tokens);
1836 self.0.paren_token.surround(tokens, |tokens| {
1837 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001838 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1839 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001840 }
David Tolnayd2836e22017-12-27 23:13:00 -05001841 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001842 });
1843 self.0.output.to_tokens(tokens);
1844 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001845 }
1846 }
1847
Alex Crichton62a0a592017-05-22 13:58:53 -07001848 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001849 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001850 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001851 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001852 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001853 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001854 }
1855 }
1856
1857 impl ToTokens for ArgSelf {
1858 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001859 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001860 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001861 }
1862 }
1863
1864 impl ToTokens for ArgCaptured {
1865 fn to_tokens(&self, tokens: &mut Tokens) {
1866 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001867 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001868 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001869 }
1870 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001871}