blob: 19537563e3f3a91f6e7321fb0055dded7cb210eb [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
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 Tolnayab919512017-12-30 23:31:51 -05007use mac::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![::]>,
34 pub prefix: Delimited<Ident, Token![::]>,
35 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 }),
113 /// An enum definition (`enum` or `pub enum`).
114 ///
115 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
116 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800117 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700118 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800119 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700120 pub ident: Ident,
121 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500122 pub brace_token: token::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800123 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 }),
125 /// A struct definition (`struct` or `pub struct`).
126 ///
127 /// E.g. `struct Foo<A> { x: A }`
128 pub Struct(ItemStruct {
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 struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700134 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
137 /// 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 Tolnay570695e2017-06-03 16:15:13 -0700146 pub data: VariantData,
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 Tolnayfd6bf5c2017-11-12 09:41:14 -0800160 pub supertraits: Delimited<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 Tolnayc6b55bc2017-11-09 22:48:38 -0800259 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500260 Body::Enum(data) => Item::Enum(ItemEnum {
261 attrs: input.attrs,
262 vis: input.vis,
263 enum_token: data.enum_token,
264 ident: input.ident,
265 generics: input.generics,
266 brace_token: data.brace_token,
267 variants: data.variants,
268 }),
269 Body::Struct(data) => Item::Struct(ItemStruct {
270 attrs: input.attrs,
271 vis: input.vis,
272 struct_token: data.struct_token,
273 ident: input.ident,
274 generics: input.generics,
275 data: data.data,
276 semi_token: data.semi_token,
277 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700278 }
279 }
280}
281
Alex Crichton62a0a592017-05-22 13:58:53 -0700282ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500283 /// Things that can appear directly inside of a module.
284 pub enum UseTree {
285 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
286 pub Path(UsePath {
287 pub ident: Ident,
288 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500290 /// `use prefix::*`
291 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800292 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500294 /// `use prefix::{a, b, c}`
295 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500296 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500297 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
299 }
300}
301
Alex Crichton62a0a592017-05-22 13:58:53 -0700302ast_enum_of_structs! {
303 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800304 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A foreign function
306 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800307 pub attrs: Vec<Attribute>,
308 pub vis: Visibility,
309 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700310 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800311 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700312 }),
313 /// A foreign static item (`static ext: u8`)
314 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800315 pub attrs: Vec<Attribute>,
316 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800317 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500318 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800319 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800320 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800321 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800322 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800324 /// A foreign type
325 pub Type(ForeignItemType {
326 pub attrs: Vec<Attribute>,
327 pub vis: Visibility,
328 pub type_token: Token![type],
329 pub ident: Ident,
330 pub semi_token: Token![;],
331 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500332 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
333 pub tts: TokenStream,
334 }),
335 }
336}
337
338#[cfg(feature = "extra-traits")]
339impl Eq for ForeignItemVerbatim {}
340
341#[cfg(feature = "extra-traits")]
342impl PartialEq for ForeignItemVerbatim {
343 fn eq(&self, other: &Self) -> bool {
344 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
345 }
346}
347
348#[cfg(feature = "extra-traits")]
349impl Hash for ForeignItemVerbatim {
350 fn hash<H>(&self, state: &mut H)
351 where
352 H: Hasher,
353 {
354 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700356}
357
David Tolnayda705bd2017-11-10 21:58:05 -0800358ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 /// Represents an item declaration within a trait declaration,
360 /// possibly including a default implementation. A trait item is
361 /// either required (meaning it doesn't have an implementation, just a
362 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800363 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700364 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800365 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700367 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800369 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub default: Option<(Token![=], Expr)>,
371 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 }),
373 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800374 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700375 pub sig: MethodSig,
376 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700378 }),
379 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800380 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800381 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700382 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500383 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800384 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800385 pub bounds: Delimited<TypeParamBound, Token![+]>,
386 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800387 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800389 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800390 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800391 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500392 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800393 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500394 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
395 pub tts: TokenStream,
396 }),
397 }
398}
399
400#[cfg(feature = "extra-traits")]
401impl Eq for TraitItemVerbatim {}
402
403#[cfg(feature = "extra-traits")]
404impl PartialEq for TraitItemVerbatim {
405 fn eq(&self, other: &Self) -> bool {
406 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
407 }
408}
409
410#[cfg(feature = "extra-traits")]
411impl Hash for TraitItemVerbatim {
412 fn hash<H>(&self, state: &mut H)
413 where
414 H: Hasher,
415 {
416 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700418}
419
Alex Crichton62a0a592017-05-22 13:58:53 -0700420ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800421 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800423 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700424 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500425 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800426 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700427 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800428 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800429 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800430 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 }),
434 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800435 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700436 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500437 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 pub sig: MethodSig,
439 pub block: Block,
440 }),
441 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800442 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700443 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500444 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800445 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700446 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500447 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800448 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800449 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800450 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700451 }),
David Tolnay857628c2017-11-11 12:25:31 -0800452 pub Macro(ImplItemMacro {
453 pub attrs: Vec<Attribute>,
454 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500455 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800456 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500457 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
458 pub tts: TokenStream,
459 }),
460 }
461}
462
463#[cfg(feature = "extra-traits")]
464impl Eq for ImplItemVerbatim {}
465
466#[cfg(feature = "extra-traits")]
467impl PartialEq for ImplItemVerbatim {
468 fn eq(&self, other: &Self) -> bool {
469 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
470 }
471}
472
473#[cfg(feature = "extra-traits")]
474impl Hash for ImplItemVerbatim {
475 fn hash<H>(&self, state: &mut H)
476 where
477 H: Hasher,
478 {
479 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700481}
482
483ast_struct! {
484 /// Represents a method's signature in a trait declaration,
485 /// or in an implementation.
486 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500487 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500488 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700490 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700491 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700492 }
493}
494
495ast_struct! {
496 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700497 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700498 /// E.g. `fn foo(bar: baz)`
499 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800500 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500501 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500502 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800503 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500504 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500505 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700507}
508
Alex Crichton62a0a592017-05-22 13:58:53 -0700509ast_enum_of_structs! {
510 /// An argument in a function header.
511 ///
512 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
513 pub enum FnArg {
514 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500517 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500518 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700519 }),
520 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500521 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800522 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 }),
524 pub Captured(ArgCaptured {
525 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800526 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800527 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500529 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700531 }
David Tolnay62f374c2016-10-02 13:37:00 -0700532}
533
David Tolnayedf2b992016-09-23 20:43:45 -0700534#[cfg(feature = "parsing")]
535pub mod parsing {
536 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700537
David Tolnay03342952017-12-29 11:52:00 -0500538 use synom::{Synom, Cursor, PResult};
David Tolnay84aa0752016-10-02 23:01:13 -0700539
David Tolnay4c614be2017-11-10 00:02:38 -0800540 impl_synom!(Item "item" alt!(
541 syn!(ItemExternCrate) => { Item::ExternCrate }
542 |
543 syn!(ItemUse) => { Item::Use }
544 |
545 syn!(ItemStatic) => { Item::Static }
546 |
547 syn!(ItemConst) => { Item::Const }
548 |
549 syn!(ItemFn) => { Item::Fn }
550 |
551 syn!(ItemMod) => { Item::Mod }
552 |
553 syn!(ItemForeignMod) => { Item::ForeignMod }
554 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800555 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800556 |
557 syn!(ItemStruct) => { Item::Struct }
558 |
559 syn!(ItemEnum) => { Item::Enum }
560 |
561 syn!(ItemUnion) => { Item::Union }
562 |
563 syn!(ItemTrait) => { Item::Trait }
564 |
David Tolnay03342952017-12-29 11:52:00 -0500565 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800566 |
567 syn!(ItemImpl) => { Item::Impl }
568 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800569 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800570 |
571 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800572 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700573
David Tolnaydecf28d2017-11-11 11:56:45 -0800574 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500575 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700576 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800577 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700578 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500579 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500580 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800581 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700582 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800583 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800584 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500585 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700586 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500587 delimiter: body.0,
588 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800589 },
David Tolnay57292da2017-12-27 21:03:33 -0500590 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800591 })
David Tolnayedf2b992016-09-23 20:43:45 -0700592 ));
593
David Tolnay500d8322017-12-18 00:32:51 -0800594 // TODO: figure out the actual grammar; is body required to be braced?
595 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500596 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800597 vis: syn!(Visibility) >>
598 macro_: keyword!(macro) >>
599 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500600 args: call!(tt::parenthesized) >>
601 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800602 (ItemMacro2 {
603 attrs: attrs,
604 vis: vis,
605 macro_token: macro_,
606 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500607 paren_token: args.0,
608 args: args.1,
609 brace_token: body.0,
610 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800611 })
612 ));
613
David Tolnay4c614be2017-11-10 00:02:38 -0800614 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500615 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700616 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 extern_: keyword!(extern) >>
618 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700619 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
621 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800622 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700623 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800624 vis: vis,
625 extern_token: extern_,
626 crate_token: crate_,
627 ident: ident,
628 rename: rename,
629 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800630 })
David Tolnayedf2b992016-09-23 20:43:45 -0700631 ));
632
David Tolnay4c614be2017-11-10 00:02:38 -0800633 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500634 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700635 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800636 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500637 leading_colon: option!(punct!(::)) >>
638 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500639 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500640 true => syn!(UseTree)
641 |
David Tolnay8edcef12017-12-28 12:06:52 -0500642 false => alt!(
643 tuple!(keyword!(as), syn!(Ident)) => {
644 |rename| UseTree::Path(UsePath {
645 ident: prefix.pop().unwrap().into_item(),
646 rename: Some(rename),
647 })
648 }
David Tolnay5f332a92017-12-26 00:42:45 -0500649 |
David Tolnay8edcef12017-12-28 12:06:52 -0500650 epsilon!() => {
651 |_| UseTree::Path(UsePath {
652 ident: prefix.pop().unwrap().into_item(),
653 rename: None,
654 })
655 }
David Tolnay5f332a92017-12-26 00:42:45 -0500656 )
657 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800658 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800659 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700660 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800661 vis: vis,
662 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500663 leading_colon: leading_colon,
664 prefix: prefix,
665 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800666 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800667 })
David Tolnay4a057422016-10-08 00:02:31 -0700668 ));
669
David Tolnay5f332a92017-12-26 00:42:45 -0500670 named!(use_prefix -> Ident, alt!(
671 syn!(Ident)
672 |
673 keyword!(self) => { Into::into }
674 |
675 keyword!(super) => { Into::into }
676 |
677 keyword!(crate) => { Into::into }
678 ));
679
680 impl_synom!(UseTree "use tree" alt!(
681 syn!(UsePath) => { UseTree::Path }
682 |
683 syn!(UseGlob) => { UseTree::Glob }
684 |
685 syn!(UseList) => { UseTree::List }
686 ));
687
688 impl_synom!(UsePath "use path" do_parse!(
689 ident: alt!(
690 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400691 |
David Tolnay5f332a92017-12-26 00:42:45 -0500692 keyword!(self) => { Into::into }
693 ) >>
694 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
695 (UsePath {
696 ident: ident,
697 rename: rename,
698 })
699 ));
David Tolnay4a057422016-10-08 00:02:31 -0700700
David Tolnay5f332a92017-12-26 00:42:45 -0500701 impl_synom!(UseGlob "use glob" do_parse!(
702 star: punct!(*) >>
703 (UseGlob {
704 star_token: star,
705 })
706 ));
David Tolnay4a057422016-10-08 00:02:31 -0700707
David Tolnay5f332a92017-12-26 00:42:45 -0500708 impl_synom!(UseList "use list" do_parse!(
709 list: braces!(Delimited::parse_terminated) >>
710 (UseList {
711 brace_token: list.1,
712 items: list.0,
713 })
714 ));
David Tolnay4a057422016-10-08 00:02:31 -0700715
David Tolnay4c614be2017-11-10 00:02:38 -0800716 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500717 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700718 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800719 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500720 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700721 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800722 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800723 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800724 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700725 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800726 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800727 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700728 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800729 vis: vis,
730 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500731 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800732 ident: ident,
733 colon_token: colon,
734 ty: Box::new(ty),
735 eq_token: eq,
736 expr: Box::new(value),
737 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800738 })
David Tolnay47a877c2016-10-01 16:50:55 -0700739 ));
740
David Tolnay4c614be2017-11-10 00:02:38 -0800741 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500742 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700743 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800744 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700745 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800746 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800747 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800748 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700749 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800750 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800751 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700752 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800753 vis: vis,
754 const_token: const_,
755 ident: ident,
756 colon_token: colon,
757 ty: Box::new(ty),
758 eq_token: eq,
759 expr: Box::new(value),
760 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800761 })
David Tolnay47a877c2016-10-01 16:50:55 -0700762 ));
763
David Tolnay4c614be2017-11-10 00:02:38 -0800764 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500765 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700766 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500767 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500768 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700769 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800770 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700771 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700772 generics: syn!(Generics) >>
773 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800774 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500775 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700776 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500777 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700778 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400779 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800780 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700781 attrs: {
782 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700783 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700784 attrs
785 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800786 vis: vis,
787 constness: constness,
788 unsafety: unsafety,
789 abi: abi,
790 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800791 fn_token: fn_,
792 paren_token: inputs.1,
793 inputs: inputs.0,
794 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500795 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800796 generics: Generics {
797 where_clause: where_clause,
798 .. generics
799 },
800 }),
801 ident: ident,
802 block: Box::new(Block {
803 brace_token: inner_attrs_stmts.1,
804 stmts: (inner_attrs_stmts.0).1,
805 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800806 })
David Tolnay42602292016-10-01 22:25:45 -0700807 ));
808
Alex Crichton954046c2017-05-30 21:49:42 -0700809 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400810 named!(parse -> Self, alt!(
811 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800812 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400813 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500814 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800815 self_: keyword!(self) >>
816 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400817 (ArgSelfRef {
818 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500819 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400820 and_token: and,
821 self_token: self_,
822 }.into())
823 )
824 |
825 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500826 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800827 self_: keyword!(self) >>
828 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400829 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500830 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400831 self_token: self_,
832 }.into())
833 )
834 |
835 do_parse!(
836 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800837 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800838 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400839 (ArgCaptured {
840 pat: pat,
841 ty: ty,
842 colon_token: colon,
843 }.into())
844 )
845 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800846 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400847 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700848 }
David Tolnay62f374c2016-10-02 13:37:00 -0700849
David Tolnay4c614be2017-11-10 00:02:38 -0800850 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500851 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700852 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800853 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700854 ident: syn!(Ident) >>
855 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800856 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700857 Vec::new(),
858 None,
859 Some(semi),
860 )}
David Tolnay37d10332016-10-13 20:51:04 -0700861 |
Alex Crichton954046c2017-05-30 21:49:42 -0700862 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700863 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500864 many0!(Attribute::parse_inner),
865 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400866 )
David Tolnay570695e2017-06-03 16:15:13 -0700867 ) => {|((inner_attrs, items), brace)| (
868 inner_attrs,
869 Some((brace, items)),
870 None,
871 )}
David Tolnay37d10332016-10-13 20:51:04 -0700872 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800873 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700874 attrs: {
875 let mut attrs = outer_attrs;
876 attrs.extend(content_semi.0);
877 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700878 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800879 vis: vis,
880 mod_token: mod_,
881 ident: ident,
882 content: content_semi.1,
883 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800884 })
David Tolnay35902302016-10-06 01:11:08 -0700885 ));
886
David Tolnay4c614be2017-11-10 00:02:38 -0800887 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500888 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700889 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500890 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800891 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700892 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800893 abi: abi,
894 brace_token: items.1,
895 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800896 })
David Tolnay35902302016-10-06 01:11:08 -0700897 ));
898
David Tolnay8894f602017-11-11 12:11:04 -0800899 impl_synom!(ForeignItem "foreign item" alt!(
900 syn!(ForeignItemFn) => { ForeignItem::Fn }
901 |
902 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800903 |
904 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800905 ));
David Tolnay35902302016-10-06 01:11:08 -0700906
David Tolnay8894f602017-11-11 12:11:04 -0800907 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500908 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700909 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800910 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700911 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700912 generics: syn!(Generics) >>
913 inputs: parens!(do_parse!(
914 args: call!(Delimited::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500915 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700916 (args, variadic)
917 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800918 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500919 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800920 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700921 ({
922 let ((inputs, variadic), parens) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800923 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700924 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700925 attrs: attrs,
926 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800927 decl: Box::new(FnDecl {
928 fn_token: fn_,
929 paren_token: parens,
930 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500931 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800932 output: ret,
933 generics: Generics {
934 where_clause: where_clause,
935 .. generics
936 },
937 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700938 vis: vis,
939 }
David Tolnay35902302016-10-06 01:11:08 -0700940 })
941 ));
942
David Tolnay8894f602017-11-11 12:11:04 -0800943 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500944 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700945 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800946 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500947 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700948 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800950 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800951 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800952 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700953 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700954 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700955 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800956 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500957 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800958 static_token: static_,
959 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700960 vis: vis,
961 })
962 ));
963
David Tolnay199bcbb2017-11-12 10:33:52 -0800964 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500965 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800966 vis: syn!(Visibility) >>
967 type_: keyword!(type) >>
968 ident: syn!(Ident) >>
969 semi: punct!(;) >>
970 (ForeignItemType {
971 attrs: attrs,
972 vis: vis,
973 type_token: type_,
974 ident: ident,
975 semi_token: semi,
976 })
977 ));
978
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800979 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500980 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700981 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800982 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700983 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700984 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500985 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800987 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800988 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800989 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700990 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800991 vis: vis,
992 type_token: type_,
993 ident: ident,
994 generics: Generics {
995 where_clause: where_clause,
996 ..generics
997 },
998 eq_token: eq,
999 ty: Box::new(ty),
1000 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001001 })
David Tolnay3cf52982016-10-01 17:11:37 -07001002 ));
1003
David Tolnay4c614be2017-11-10 00:02:38 -08001004 impl_synom!(ItemStruct "struct item" switch!(
1005 map!(syn!(DeriveInput), Into::into),
1006 Item::Struct(item) => value!(item)
1007 |
1008 _ => reject!()
1009 ));
David Tolnay42602292016-10-01 22:25:45 -07001010
David Tolnay4c614be2017-11-10 00:02:38 -08001011 impl_synom!(ItemEnum "enum item" switch!(
1012 map!(syn!(DeriveInput), Into::into),
1013 Item::Enum(item) => value!(item)
1014 |
1015 _ => reject!()
1016 ));
1017
1018 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001019 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001020 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001022 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001023 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001024 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001025 fields: braces!(call!(Delimited::parse_terminated_with,
1026 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001027 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001028 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001029 vis: vis,
1030 union_token: union_,
1031 ident: ident,
1032 generics: Generics {
1033 where_clause: where_clause,
1034 .. generics
1035 },
1036 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -08001037 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001038 ));
1039
David Tolnay4c614be2017-11-10 00:02:38 -08001040 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001041 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001042 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001043 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001044 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001045 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001046 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001047 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001048 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001049 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001050 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001051 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001052 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001053 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001054 vis: vis,
1055 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001056 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001057 trait_token: trait_,
1058 ident: ident,
1059 generics: Generics {
1060 where_clause: where_clause,
1061 .. generics
1062 },
1063 colon_token: colon,
1064 supertraits: bounds.unwrap_or_default(),
1065 brace_token: body.1,
1066 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001067 })
David Tolnay0aecb732016-10-03 23:03:50 -07001068 ));
1069
David Tolnay03342952017-12-29 11:52:00 -05001070 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1071 Ok((cursor, cursor))
1072 }
1073
1074 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1075 begin: call!(grab_cursor) >>
1076 many0!(Attribute::parse_outer) >>
1077 option!(keyword!(unsafe)) >>
1078 keyword!(impl) >>
1079 syn!(Path) >>
1080 keyword!(for) >>
1081 punct!(..) >>
1082 braces!(epsilon!()) >>
1083 end: call!(grab_cursor) >>
1084 ({
1085 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1086 let len = tts.len() - end.token_stream().into_iter().count();
1087 ItemVerbatim {
1088 tts: tts.into_iter().take(len).collect(),
1089 }
David Tolnay4c614be2017-11-10 00:02:38 -08001090 })
David Tolnayf94e2362016-10-04 00:29:51 -07001091 ));
1092
David Tolnayda705bd2017-11-10 21:58:05 -08001093 impl_synom!(TraitItem "trait item" alt!(
1094 syn!(TraitItemConst) => { TraitItem::Const }
1095 |
1096 syn!(TraitItemMethod) => { TraitItem::Method }
1097 |
1098 syn!(TraitItemType) => { TraitItem::Type }
1099 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001100 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001101 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001102
David Tolnayda705bd2017-11-10 21:58:05 -08001103 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001104 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001105 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001106 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001107 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001108 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1110 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001111 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001112 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001113 const_token: const_,
1114 ident: ident,
1115 colon_token: colon,
1116 ty: ty,
1117 default: default,
1118 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001119 })
1120 ));
1121
David Tolnayda705bd2017-11-10 21:58:05 -08001122 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001123 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001124 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001125 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001126 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001127 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001128 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001129 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001130 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001131 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001132 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001133 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001134 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001135 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001136 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001137 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001138 ({
1139 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001140 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001141 None => (Vec::new(), None),
1142 };
David Tolnayda705bd2017-11-10 21:58:05 -08001143 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001144 attrs: {
1145 let mut attrs = outer_attrs;
1146 attrs.extend(inner_attrs);
1147 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001148 },
David Tolnayda705bd2017-11-10 21:58:05 -08001149 sig: MethodSig {
1150 constness: constness,
1151 unsafety: unsafety,
1152 abi: abi,
1153 ident: ident,
1154 decl: FnDecl {
1155 inputs: inputs.0,
1156 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001157 fn_token: fn_,
1158 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001159 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001160 generics: Generics {
1161 where_clause: where_clause,
1162 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001163 },
1164 },
David Tolnayda705bd2017-11-10 21:58:05 -08001165 },
1166 default: stmts.map(|stmts| {
1167 Block {
1168 stmts: stmts.0,
1169 brace_token: stmts.1,
1170 }
1171 }),
1172 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001173 }
David Tolnay0aecb732016-10-03 23:03:50 -07001174 })
1175 ));
1176
David Tolnayda705bd2017-11-10 21:58:05 -08001177 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001178 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001180 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001181 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001182 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001183 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001184 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001185 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001186 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001187 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001188 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001189 type_token: type_,
1190 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001191 generics: Generics {
1192 where_clause: where_clause,
1193 .. generics
1194 },
David Tolnayda705bd2017-11-10 21:58:05 -08001195 colon_token: colon,
1196 bounds: bounds.unwrap_or_default(),
1197 default: default,
1198 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001199 })
1200 ));
1201
David Tolnaydecf28d2017-11-11 11:56:45 -08001202 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001203 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001204 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001205 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001206 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001207 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001208 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001209 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001210 })
1211 ));
1212
David Tolnay4c614be2017-11-10 00:02:38 -08001213 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001214 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001215 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001216 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001217 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001218 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001219 polarity_path: alt!(
1220 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001221 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001222 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001223 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001224 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001225 )
1226 |
David Tolnay570695e2017-06-03 16:15:13 -07001227 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001228 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001229 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001230 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001231 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001232 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001233 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001234 defaultness: defaultness,
1235 unsafety: unsafety,
1236 impl_token: impl_,
1237 generics: Generics {
1238 where_clause: where_clause,
1239 .. generics
1240 },
1241 trait_: polarity_path,
1242 self_ty: Box::new(self_ty),
1243 brace_token: body.1,
1244 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001245 })
David Tolnay4c9be372016-10-06 00:47:37 -07001246 ));
1247
David Tolnay857628c2017-11-11 12:25:31 -08001248 impl_synom!(ImplItem "item in impl block" alt!(
1249 syn!(ImplItemConst) => { ImplItem::Const }
1250 |
1251 syn!(ImplItemMethod) => { ImplItem::Method }
1252 |
1253 syn!(ImplItemType) => { ImplItem::Type }
1254 |
1255 syn!(ImplItemMacro) => { ImplItem::Macro }
1256 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001257
David Tolnay857628c2017-11-11 12:25:31 -08001258 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001259 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001260 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001261 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001262 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001263 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001264 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001265 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001266 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001267 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001268 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001269 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001270 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001271 vis: vis,
1272 defaultness: defaultness,
1273 const_token: const_,
1274 ident: ident,
1275 colon_token: colon,
1276 ty: ty,
1277 eq_token: eq,
1278 expr: value,
1279 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001280 })
1281 ));
1282
David Tolnay857628c2017-11-11 12:25:31 -08001283 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001284 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001285 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001286 defaultness: option!(keyword!(default)) >>
1287 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001288 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001289 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001290 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001291 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001292 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001293 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001294 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001295 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001297 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001298 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001299 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001300 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001301 attrs: {
1302 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001303 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001304 attrs
1305 },
David Tolnay857628c2017-11-11 12:25:31 -08001306 vis: vis,
1307 defaultness: defaultness,
1308 sig: MethodSig {
1309 constness: constness,
1310 unsafety: unsafety,
1311 abi: abi,
1312 ident: ident,
1313 decl: FnDecl {
1314 fn_token: fn_,
1315 paren_token: inputs.1,
1316 inputs: inputs.0,
1317 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001318 generics: Generics {
1319 where_clause: where_clause,
1320 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001321 },
David Tolnayd2836e22017-12-27 23:13:00 -05001322 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001323 },
David Tolnay857628c2017-11-11 12:25:31 -08001324 },
1325 block: Block {
1326 brace_token: inner_attrs_stmts.1,
1327 stmts: (inner_attrs_stmts.0).1,
1328 },
David Tolnay4c9be372016-10-06 00:47:37 -07001329 })
1330 ));
1331
David Tolnay857628c2017-11-11 12:25:31 -08001332 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001333 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001334 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001335 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001336 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001337 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001338 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001339 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001340 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001341 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001342 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001343 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001344 vis: vis,
1345 defaultness: defaultness,
1346 type_token: type_,
1347 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001348 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001349 eq_token: eq,
1350 ty: ty,
1351 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001352 })
1353 ));
1354
David Tolnay857628c2017-11-11 12:25:31 -08001355 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001356 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001357 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001358 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001359 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001360 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001361 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001362 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001363 })
1364 ));
1365
David Tolnayab919512017-12-30 23:31:51 -05001366 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1367 match *delimiter {
1368 MacroDelimiter::Brace(_) => true,
1369 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001370 }
1371 }
David Tolnayedf2b992016-09-23 20:43:45 -07001372}
David Tolnay4a51dc72016-10-01 00:40:31 -07001373
1374#[cfg(feature = "printing")]
1375mod printing {
1376 use super::*;
1377 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001378 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001379 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001380
David Tolnay1bfa7332017-11-11 12:41:20 -08001381 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001382 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001383 tokens.append_all(self.attrs.outer());
1384 self.vis.to_tokens(tokens);
1385 self.extern_token.to_tokens(tokens);
1386 self.crate_token.to_tokens(tokens);
1387 self.ident.to_tokens(tokens);
1388 if let Some((ref as_token, ref rename)) = self.rename {
1389 as_token.to_tokens(tokens);
1390 rename.to_tokens(tokens);
1391 }
1392 self.semi_token.to_tokens(tokens);
1393 }
1394 }
1395
1396 impl ToTokens for ItemUse {
1397 fn to_tokens(&self, tokens: &mut Tokens) {
1398 tokens.append_all(self.attrs.outer());
1399 self.vis.to_tokens(tokens);
1400 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001401 self.leading_colon.to_tokens(tokens);
1402 self.prefix.to_tokens(tokens);
1403 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001404 self.semi_token.to_tokens(tokens);
1405 }
1406 }
1407
1408 impl ToTokens for ItemStatic {
1409 fn to_tokens(&self, tokens: &mut Tokens) {
1410 tokens.append_all(self.attrs.outer());
1411 self.vis.to_tokens(tokens);
1412 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001413 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001414 self.ident.to_tokens(tokens);
1415 self.colon_token.to_tokens(tokens);
1416 self.ty.to_tokens(tokens);
1417 self.eq_token.to_tokens(tokens);
1418 self.expr.to_tokens(tokens);
1419 self.semi_token.to_tokens(tokens);
1420 }
1421 }
1422
1423 impl ToTokens for ItemConst {
1424 fn to_tokens(&self, tokens: &mut Tokens) {
1425 tokens.append_all(self.attrs.outer());
1426 self.vis.to_tokens(tokens);
1427 self.const_token.to_tokens(tokens);
1428 self.ident.to_tokens(tokens);
1429 self.colon_token.to_tokens(tokens);
1430 self.ty.to_tokens(tokens);
1431 self.eq_token.to_tokens(tokens);
1432 self.expr.to_tokens(tokens);
1433 self.semi_token.to_tokens(tokens);
1434 }
1435 }
1436
1437 impl ToTokens for ItemFn {
1438 fn to_tokens(&self, tokens: &mut Tokens) {
1439 tokens.append_all(self.attrs.outer());
1440 self.vis.to_tokens(tokens);
1441 self.constness.to_tokens(tokens);
1442 self.unsafety.to_tokens(tokens);
1443 self.abi.to_tokens(tokens);
1444 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1445 self.block.brace_token.surround(tokens, |tokens| {
1446 tokens.append_all(self.attrs.inner());
1447 tokens.append_all(&self.block.stmts);
1448 });
1449 }
1450 }
1451
1452 impl ToTokens for ItemMod {
1453 fn to_tokens(&self, tokens: &mut Tokens) {
1454 tokens.append_all(self.attrs.outer());
1455 self.vis.to_tokens(tokens);
1456 self.mod_token.to_tokens(tokens);
1457 self.ident.to_tokens(tokens);
1458 if let Some((ref brace, ref items)) = self.content {
1459 brace.surround(tokens, |tokens| {
1460 tokens.append_all(self.attrs.inner());
1461 tokens.append_all(items);
1462 });
1463 } else {
1464 TokensOrDefault(&self.semi).to_tokens(tokens);
1465 }
1466 }
1467 }
1468
1469 impl ToTokens for ItemForeignMod {
1470 fn to_tokens(&self, tokens: &mut Tokens) {
1471 tokens.append_all(self.attrs.outer());
1472 self.abi.to_tokens(tokens);
1473 self.brace_token.surround(tokens, |tokens| {
1474 tokens.append_all(&self.items);
1475 });
1476 }
1477 }
1478
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001479 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001480 fn to_tokens(&self, tokens: &mut Tokens) {
1481 tokens.append_all(self.attrs.outer());
1482 self.vis.to_tokens(tokens);
1483 self.type_token.to_tokens(tokens);
1484 self.ident.to_tokens(tokens);
1485 self.generics.to_tokens(tokens);
1486 self.generics.where_clause.to_tokens(tokens);
1487 self.eq_token.to_tokens(tokens);
1488 self.ty.to_tokens(tokens);
1489 self.semi_token.to_tokens(tokens);
1490 }
1491 }
1492
1493 impl ToTokens for ItemEnum {
1494 fn to_tokens(&self, tokens: &mut Tokens) {
1495 tokens.append_all(self.attrs.outer());
1496 self.vis.to_tokens(tokens);
1497 self.enum_token.to_tokens(tokens);
1498 self.ident.to_tokens(tokens);
1499 self.generics.to_tokens(tokens);
1500 self.generics.where_clause.to_tokens(tokens);
1501 self.brace_token.surround(tokens, |tokens| {
1502 self.variants.to_tokens(tokens);
1503 });
1504 }
1505 }
1506
1507 impl ToTokens for ItemStruct {
1508 fn to_tokens(&self, tokens: &mut Tokens) {
1509 tokens.append_all(self.attrs.outer());
1510 self.vis.to_tokens(tokens);
1511 self.struct_token.to_tokens(tokens);
1512 self.ident.to_tokens(tokens);
1513 self.generics.to_tokens(tokens);
1514 match self.data {
1515 VariantData::Struct(..) => {
1516 self.generics.where_clause.to_tokens(tokens);
1517 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001518 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001519 VariantData::Tuple(..) => {
1520 self.data.to_tokens(tokens);
1521 self.generics.where_clause.to_tokens(tokens);
1522 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001523 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001524 VariantData::Unit => {
1525 self.generics.where_clause.to_tokens(tokens);
1526 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001527 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001528 }
1529 }
1530 }
1531
1532 impl ToTokens for ItemUnion {
1533 fn to_tokens(&self, tokens: &mut Tokens) {
1534 tokens.append_all(self.attrs.outer());
1535 self.vis.to_tokens(tokens);
1536 self.union_token.to_tokens(tokens);
1537 self.ident.to_tokens(tokens);
1538 self.generics.to_tokens(tokens);
1539 self.generics.where_clause.to_tokens(tokens);
1540 // XXX: Should we handle / complain when using a
1541 // non-VariantData::Struct Variant in Union?
1542 self.data.to_tokens(tokens);
1543 }
1544 }
1545
1546 impl ToTokens for ItemTrait {
1547 fn to_tokens(&self, tokens: &mut Tokens) {
1548 tokens.append_all(self.attrs.outer());
1549 self.vis.to_tokens(tokens);
1550 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001551 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001552 self.trait_token.to_tokens(tokens);
1553 self.ident.to_tokens(tokens);
1554 self.generics.to_tokens(tokens);
1555 if !self.supertraits.is_empty() {
1556 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1557 self.supertraits.to_tokens(tokens);
1558 }
1559 self.generics.where_clause.to_tokens(tokens);
1560 self.brace_token.surround(tokens, |tokens| {
1561 tokens.append_all(&self.items);
1562 });
1563 }
1564 }
1565
David Tolnay1bfa7332017-11-11 12:41:20 -08001566 impl ToTokens for ItemImpl {
1567 fn to_tokens(&self, tokens: &mut Tokens) {
1568 tokens.append_all(self.attrs.outer());
1569 self.defaultness.to_tokens(tokens);
1570 self.unsafety.to_tokens(tokens);
1571 self.impl_token.to_tokens(tokens);
1572 self.generics.to_tokens(tokens);
1573 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1574 polarity.to_tokens(tokens);
1575 path.to_tokens(tokens);
1576 for_token.to_tokens(tokens);
1577 }
1578 self.self_ty.to_tokens(tokens);
1579 self.generics.where_clause.to_tokens(tokens);
1580 self.brace_token.surround(tokens, |tokens| {
1581 tokens.append_all(&self.items);
1582 });
1583 }
1584 }
1585
1586 impl ToTokens for ItemMacro {
1587 fn to_tokens(&self, tokens: &mut Tokens) {
1588 tokens.append_all(self.attrs.outer());
1589 self.mac.path.to_tokens(tokens);
1590 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001591 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001592 match self.mac.delimiter {
1593 MacroDelimiter::Paren(ref paren) => {
1594 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1595 }
1596 MacroDelimiter::Brace(ref brace) => {
1597 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1598 }
1599 MacroDelimiter::Bracket(ref bracket) => {
1600 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1601 }
1602 }
David Tolnay57292da2017-12-27 21:03:33 -05001603 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001604 }
1605 }
David Tolnay42602292016-10-01 22:25:45 -07001606
David Tolnay500d8322017-12-18 00:32:51 -08001607 impl ToTokens for ItemMacro2 {
1608 fn to_tokens(&self, tokens: &mut Tokens) {
1609 tokens.append_all(self.attrs.outer());
1610 self.vis.to_tokens(tokens);
1611 self.macro_token.to_tokens(tokens);
1612 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001613 self.paren_token.surround(tokens, |tokens| {
1614 self.args.to_tokens(tokens);
1615 });
1616 self.brace_token.surround(tokens, |tokens| {
1617 self.body.to_tokens(tokens);
1618 });
David Tolnay500d8322017-12-18 00:32:51 -08001619 }
1620 }
1621
David Tolnay2ae520a2017-12-29 11:19:50 -05001622 impl ToTokens for ItemVerbatim {
1623 fn to_tokens(&self, tokens: &mut Tokens) {
1624 self.tts.to_tokens(tokens);
1625 }
1626 }
1627
David Tolnay5f332a92017-12-26 00:42:45 -05001628 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001629 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001630 self.ident.to_tokens(tokens);
1631 if let Some((ref as_token, ref rename)) = self.rename {
1632 as_token.to_tokens(tokens);
1633 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001634 }
David Tolnay4a057422016-10-08 00:02:31 -07001635 }
1636 }
1637
David Tolnay5f332a92017-12-26 00:42:45 -05001638 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001639 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001640 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001641 }
1642 }
1643
David Tolnay5f332a92017-12-26 00:42:45 -05001644 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001646 self.brace_token.surround(tokens, |tokens| {
1647 self.items.to_tokens(tokens);
1648 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 }
1650 }
1651
David Tolnay1bfa7332017-11-11 12:41:20 -08001652 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001653 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001654 tokens.append_all(self.attrs.outer());
1655 self.const_token.to_tokens(tokens);
1656 self.ident.to_tokens(tokens);
1657 self.colon_token.to_tokens(tokens);
1658 self.ty.to_tokens(tokens);
1659 if let Some((ref eq_token, ref default)) = self.default {
1660 eq_token.to_tokens(tokens);
1661 default.to_tokens(tokens);
1662 }
1663 self.semi_token.to_tokens(tokens);
1664 }
1665 }
1666
1667 impl ToTokens for TraitItemMethod {
1668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 tokens.append_all(self.attrs.outer());
1670 self.sig.to_tokens(tokens);
1671 match self.default {
1672 Some(ref block) => {
1673 block.brace_token.surround(tokens, |tokens| {
1674 tokens.append_all(self.attrs.inner());
1675 tokens.append_all(&block.stmts);
1676 });
David Tolnayca085422016-10-04 00:12:38 -07001677 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001678 None => {
1679 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001680 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001681 }
1682 }
1683 }
1684
1685 impl ToTokens for TraitItemType {
1686 fn to_tokens(&self, tokens: &mut Tokens) {
1687 tokens.append_all(self.attrs.outer());
1688 self.type_token.to_tokens(tokens);
1689 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001690 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001691 if !self.bounds.is_empty() {
1692 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1693 self.bounds.to_tokens(tokens);
1694 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001695 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001696 if let Some((ref eq_token, ref default)) = self.default {
1697 eq_token.to_tokens(tokens);
1698 default.to_tokens(tokens);
1699 }
1700 self.semi_token.to_tokens(tokens);
1701 }
1702 }
1703
1704 impl ToTokens for TraitItemMacro {
1705 fn to_tokens(&self, tokens: &mut Tokens) {
1706 tokens.append_all(self.attrs.outer());
1707 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001708 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001709 }
1710 }
1711
David Tolnay2ae520a2017-12-29 11:19:50 -05001712 impl ToTokens for TraitItemVerbatim {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 self.tts.to_tokens(tokens);
1715 }
1716 }
1717
David Tolnay857628c2017-11-11 12:25:31 -08001718 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001721 self.vis.to_tokens(tokens);
1722 self.defaultness.to_tokens(tokens);
1723 self.const_token.to_tokens(tokens);
1724 self.ident.to_tokens(tokens);
1725 self.colon_token.to_tokens(tokens);
1726 self.ty.to_tokens(tokens);
1727 self.eq_token.to_tokens(tokens);
1728 self.expr.to_tokens(tokens);
1729 self.semi_token.to_tokens(tokens);
1730 }
1731 }
1732
1733 impl ToTokens for ImplItemMethod {
1734 fn to_tokens(&self, tokens: &mut Tokens) {
1735 tokens.append_all(self.attrs.outer());
1736 self.vis.to_tokens(tokens);
1737 self.defaultness.to_tokens(tokens);
1738 self.sig.to_tokens(tokens);
1739 self.block.brace_token.surround(tokens, |tokens| {
1740 tokens.append_all(self.attrs.inner());
1741 tokens.append_all(&self.block.stmts);
1742 });
1743 }
1744 }
1745
1746 impl ToTokens for ImplItemType {
1747 fn to_tokens(&self, tokens: &mut Tokens) {
1748 tokens.append_all(self.attrs.outer());
1749 self.vis.to_tokens(tokens);
1750 self.defaultness.to_tokens(tokens);
1751 self.type_token.to_tokens(tokens);
1752 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001753 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001754 self.eq_token.to_tokens(tokens);
1755 self.ty.to_tokens(tokens);
1756 self.semi_token.to_tokens(tokens);
1757 }
1758 }
1759
1760 impl ToTokens for ImplItemMacro {
1761 fn to_tokens(&self, tokens: &mut Tokens) {
1762 tokens.append_all(self.attrs.outer());
1763 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001764 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001765 }
1766 }
1767
David Tolnay2ae520a2017-12-29 11:19:50 -05001768 impl ToTokens for ImplItemVerbatim {
1769 fn to_tokens(&self, tokens: &mut Tokens) {
1770 self.tts.to_tokens(tokens);
1771 }
1772 }
1773
David Tolnay8894f602017-11-11 12:11:04 -08001774 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001775 fn to_tokens(&self, tokens: &mut Tokens) {
1776 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001777 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001778 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1779 self.semi_token.to_tokens(tokens);
1780 }
1781 }
1782
1783 impl ToTokens for ForeignItemStatic {
1784 fn to_tokens(&self, tokens: &mut Tokens) {
1785 tokens.append_all(self.attrs.outer());
1786 self.vis.to_tokens(tokens);
1787 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001788 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001789 self.ident.to_tokens(tokens);
1790 self.colon_token.to_tokens(tokens);
1791 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001792 self.semi_token.to_tokens(tokens);
1793 }
1794 }
1795
David Tolnay199bcbb2017-11-12 10:33:52 -08001796 impl ToTokens for ForeignItemType {
1797 fn to_tokens(&self, tokens: &mut Tokens) {
1798 tokens.append_all(self.attrs.outer());
1799 self.vis.to_tokens(tokens);
1800 self.type_token.to_tokens(tokens);
1801 self.ident.to_tokens(tokens);
1802 self.semi_token.to_tokens(tokens);
1803 }
1804 }
1805
David Tolnay2ae520a2017-12-29 11:19:50 -05001806 impl ToTokens for ForeignItemVerbatim {
1807 fn to_tokens(&self, tokens: &mut Tokens) {
1808 self.tts.to_tokens(tokens);
1809 }
1810 }
1811
David Tolnay570695e2017-06-03 16:15:13 -07001812 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001813 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001814 self.constness.to_tokens(tokens);
1815 self.unsafety.to_tokens(tokens);
1816 self.abi.to_tokens(tokens);
1817 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001818 }
1819 }
1820
David Tolnay570695e2017-06-03 16:15:13 -07001821 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001822
1823 impl<'a> ToTokens for NamedDecl<'a> {
1824 fn to_tokens(&self, tokens: &mut Tokens) {
1825 self.0.fn_token.to_tokens(tokens);
1826 self.1.to_tokens(tokens);
1827 self.0.generics.to_tokens(tokens);
1828 self.0.paren_token.surround(tokens, |tokens| {
1829 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001830 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1831 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001832 }
David Tolnayd2836e22017-12-27 23:13:00 -05001833 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001834 });
1835 self.0.output.to_tokens(tokens);
1836 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001837 }
1838 }
1839
Alex Crichton62a0a592017-05-22 13:58:53 -07001840 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001841 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001842 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001843 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001844 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001845 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001846 }
1847 }
1848
1849 impl ToTokens for ArgSelf {
1850 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001851 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001852 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001853 }
1854 }
1855
1856 impl ToTokens for ArgCaptured {
1857 fn to_tokens(&self, tokens: &mut Tokens) {
1858 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001859 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001860 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001861 }
1862 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001863}