blob: 82c79249f2dd01045cc3d37c6876594ec6fc11cd [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay2ae520a2017-12-29 11:19:50 -05003use proc_macro2::{TokenTree, TokenStream};
David Tolnay9c76bcb2017-12-26 23:14:59 -05004
5#[cfg(feature = "extra-traits")]
David Tolnay2ae520a2017-12-29 11:19:50 -05006use mac::{TokenTreeHelper, TokenStreamHelper};
David Tolnay9c76bcb2017-12-26 23:14:59 -05007#[cfg(feature = "extra-traits")]
8use std::hash::{Hash, Hasher};
David Tolnayb79ee962016-09-04 09:39:20 -07009
Alex Crichton62a0a592017-05-22 13:58:53 -070010ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -080011 /// Things that can appear directly inside of a module.
12 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -070013 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070014 ///
15 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
16 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080017 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070018 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080019 pub extern_token: Token![extern],
20 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070021 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 pub rename: Option<(Token![as], Ident)>,
23 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }),
25 /// A use declaration (`use` or `pub use`) item.
26 ///
27 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
28 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080029 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070030 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080031 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050032 pub leading_colon: Option<Token![::]>,
33 pub prefix: Delimited<Ident, Token![::]>,
34 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
37 /// A static item (`static` or `pub static`).
38 ///
39 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
40 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080041 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070042 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050044 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070045 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080046 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070049 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070051 }),
52 /// A constant item (`const` or `pub const`).
53 ///
54 /// E.g. `const FOO: i32 = 42;`
55 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080056 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070057 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080058 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070059 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080061 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070063 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }),
66 /// A function declaration (`fn` or `pub fn`).
67 ///
68 /// E.g. `fn foo(bar: usize) -> usize { .. }`
69 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080070 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070071 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050072 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050073 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070075 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050076 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub block: Box<Block>,
78 }),
79 /// A module declaration (`mod` or `pub mod`).
80 ///
81 /// E.g. `mod foo;` or `mod foo { .. }`
82 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080083 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070084 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080085 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070086 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050087 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080088 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070089 }),
90 /// An external module (`extern` or `pub extern`).
91 ///
92 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070093 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080094 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050096 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub items: Vec<ForeignItem>,
98 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070099 /// A type alias (`type` or `pub type`).
100 ///
101 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800102 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800103 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700104 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800105 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700106 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800109 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 }),
112 /// An enum definition (`enum` or `pub enum`).
113 ///
114 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
115 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800116 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700117 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800118 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700119 pub ident: Ident,
120 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 }),
124 /// A struct definition (`struct` or `pub struct`).
125 ///
126 /// E.g. `struct Foo<A> { x: A }`
127 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800128 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700129 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 pub struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700135 }),
136 /// A union definition (`union` or `pub union`).
137 ///
138 /// E.g. `union Foo<A, B> { x: A, y: B }`
139 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800140 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700141 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800142 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700143 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700145 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700146 }),
147 /// A Trait declaration (`trait` or `pub trait`).
148 ///
149 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
150 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800151 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700152 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500153 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500154 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700156 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800159 pub supertraits: Delimited<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500160 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 pub items: Vec<TraitItem>,
162 }),
163 /// Default trait implementation.
164 ///
165 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
166 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800167 pub attrs: Vec<Attribute>,
David Tolnay9b258702017-12-29 02:24:41 -0500168 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800169 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700170 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub for_token: Token![for],
172 pub dot2_token: Token![..],
David Tolnay32954ef2017-12-26 22:43:16 -0500173 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
175 /// An implementation.
176 ///
177 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
178 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800179 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500180 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500181 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800182 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700184 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500185 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700186 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800187 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500188 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 pub items: Vec<ImplItem>,
190 }),
191 /// A macro invocation (which includes macro definition).
192 ///
193 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800194 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800195 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800196 /// The `example` in `macro_rules! example { ... }`.
197 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800198 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500199 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800200 }),
David Tolnay500d8322017-12-18 00:32:51 -0800201 /// FIXME will need to revisit what this looks like in the AST.
David Tolnay9c76bcb2017-12-26 23:14:59 -0500202 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800203 pub attrs: Vec<Attribute>,
204 pub vis: Visibility,
205 pub macro_token: Token![macro],
206 pub ident: Ident,
207 pub args: TokenTree,
208 pub body: TokenTree,
209 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500210 pub Verbatim(ItemVerbatim #manual_extra_traits {
211 pub tts: TokenStream,
212 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 }
David Tolnayb79ee962016-09-04 09:39:20 -0700214}
215
David Tolnay9c76bcb2017-12-26 23:14:59 -0500216#[cfg(feature = "extra-traits")]
217impl Eq for ItemMacro2 {}
218
219#[cfg(feature = "extra-traits")]
220impl PartialEq for ItemMacro2 {
221 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500222 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnay9c76bcb2017-12-26 23:14:59 -0500223 && self.ident == other.ident
224 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
225 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
226 }
227}
228
229#[cfg(feature = "extra-traits")]
230impl Hash for ItemMacro2 {
231 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500232 where
233 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500234 {
235 self.attrs.hash(state);
236 self.vis.hash(state);
237 self.macro_token.hash(state);
238 self.ident.hash(state);
239 TokenTreeHelper(&self.args).hash(state);
240 TokenTreeHelper(&self.body).hash(state);
241 }
242}
243
David Tolnay2ae520a2017-12-29 11:19:50 -0500244#[cfg(feature = "extra-traits")]
245impl Eq for ItemVerbatim {}
246
247#[cfg(feature = "extra-traits")]
248impl PartialEq for ItemVerbatim {
249 fn eq(&self, other: &Self) -> bool {
250 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
251 }
252}
253
254#[cfg(feature = "extra-traits")]
255impl Hash for ItemVerbatim {
256 fn hash<H>(&self, state: &mut H)
257 where
258 H: Hasher,
259 {
260 TokenStreamHelper(&self.tts).hash(state);
261 }
262}
263
David Tolnay0e837402016-12-22 17:25:55 -0500264impl From<DeriveInput> for Item {
265 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800266 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500267 Body::Enum(data) => Item::Enum(ItemEnum {
268 attrs: input.attrs,
269 vis: input.vis,
270 enum_token: data.enum_token,
271 ident: input.ident,
272 generics: input.generics,
273 brace_token: data.brace_token,
274 variants: data.variants,
275 }),
276 Body::Struct(data) => Item::Struct(ItemStruct {
277 attrs: input.attrs,
278 vis: input.vis,
279 struct_token: data.struct_token,
280 ident: input.ident,
281 generics: input.generics,
282 data: data.data,
283 semi_token: data.semi_token,
284 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700285 }
286 }
287}
288
Alex Crichton62a0a592017-05-22 13:58:53 -0700289ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500290 /// Things that can appear directly inside of a module.
291 pub enum UseTree {
292 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
293 pub Path(UsePath {
294 pub ident: Ident,
295 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500297 /// `use prefix::*`
298 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800299 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500301 /// `use prefix::{a, b, c}`
302 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500303 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500304 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 }),
306 }
307}
308
Alex Crichton62a0a592017-05-22 13:58:53 -0700309ast_enum_of_structs! {
310 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800311 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700312 /// A foreign function
313 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800314 pub attrs: Vec<Attribute>,
315 pub vis: Visibility,
316 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800318 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 }),
320 /// A foreign static item (`static ext: u8`)
321 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800322 pub attrs: Vec<Attribute>,
323 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800324 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500325 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800326 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800327 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800328 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800329 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800331 /// A foreign type
332 pub Type(ForeignItemType {
333 pub attrs: Vec<Attribute>,
334 pub vis: Visibility,
335 pub type_token: Token![type],
336 pub ident: Ident,
337 pub semi_token: Token![;],
338 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500339 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
340 pub tts: TokenStream,
341 }),
342 }
343}
344
345#[cfg(feature = "extra-traits")]
346impl Eq for ForeignItemVerbatim {}
347
348#[cfg(feature = "extra-traits")]
349impl PartialEq for ForeignItemVerbatim {
350 fn eq(&self, other: &Self) -> bool {
351 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
352 }
353}
354
355#[cfg(feature = "extra-traits")]
356impl Hash for ForeignItemVerbatim {
357 fn hash<H>(&self, state: &mut H)
358 where
359 H: Hasher,
360 {
361 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700363}
364
David Tolnayda705bd2017-11-10 21:58:05 -0800365ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 /// Represents an item declaration within a trait declaration,
367 /// possibly including a default implementation. A trait item is
368 /// either required (meaning it doesn't have an implementation, just a
369 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800370 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800372 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800373 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700374 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800376 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub default: Option<(Token![=], Expr)>,
378 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 }),
380 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800381 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 pub sig: MethodSig,
383 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800384 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700385 }),
386 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800387 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700389 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500390 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800391 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800392 pub bounds: Delimited<TypeParamBound, Token![+]>,
393 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800396 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800397 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800398 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500399 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800400 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500401 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
402 pub tts: TokenStream,
403 }),
404 }
405}
406
407#[cfg(feature = "extra-traits")]
408impl Eq for TraitItemVerbatim {}
409
410#[cfg(feature = "extra-traits")]
411impl PartialEq for TraitItemVerbatim {
412 fn eq(&self, other: &Self) -> bool {
413 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
414 }
415}
416
417#[cfg(feature = "extra-traits")]
418impl Hash for TraitItemVerbatim {
419 fn hash<H>(&self, state: &mut H)
420 where
421 H: Hasher,
422 {
423 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700425}
426
Alex Crichton62a0a592017-05-22 13:58:53 -0700427ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800428 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800430 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700431 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500432 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800433 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700434 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800436 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800439 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700440 }),
441 pub Method(ImplItemMethod {
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]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 pub sig: MethodSig,
446 pub block: Block,
447 }),
448 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800449 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700450 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500451 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800452 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700453 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500454 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800455 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800456 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800457 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700458 }),
David Tolnay857628c2017-11-11 12:25:31 -0800459 pub Macro(ImplItemMacro {
460 pub attrs: Vec<Attribute>,
461 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500462 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800463 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500464 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
465 pub tts: TokenStream,
466 }),
467 }
468}
469
470#[cfg(feature = "extra-traits")]
471impl Eq for ImplItemVerbatim {}
472
473#[cfg(feature = "extra-traits")]
474impl PartialEq for ImplItemVerbatim {
475 fn eq(&self, other: &Self) -> bool {
476 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
477 }
478}
479
480#[cfg(feature = "extra-traits")]
481impl Hash for ImplItemVerbatim {
482 fn hash<H>(&self, state: &mut H)
483 where
484 H: Hasher,
485 {
486 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700488}
489
490ast_struct! {
491 /// Represents a method's signature in a trait declaration,
492 /// or in an implementation.
493 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500494 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500495 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700496 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700497 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700498 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700499 }
500}
501
502ast_struct! {
503 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700504 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700505 /// E.g. `fn foo(bar: baz)`
506 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800507 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500508 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500509 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500511 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500512 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700514}
515
Alex Crichton62a0a592017-05-22 13:58:53 -0700516ast_enum_of_structs! {
517 /// An argument in a function header.
518 ///
519 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
520 pub enum FnArg {
521 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800522 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500524 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500525 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700526 }),
527 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500528 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800529 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700530 }),
531 pub Captured(ArgCaptured {
532 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800533 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800534 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700535 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500536 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800537 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 }
David Tolnay62f374c2016-10-02 13:37:00 -0700539}
540
David Tolnayedf2b992016-09-23 20:43:45 -0700541#[cfg(feature = "parsing")]
542pub mod parsing {
543 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700544
Michael Layzell92639a52017-06-01 00:07:44 -0400545 use synom::Synom;
David Tolnay57292da2017-12-27 21:03:33 -0500546 use proc_macro2::{TokenNode, Delimiter};
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 |
573 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
574 |
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 Tolnay57292da2017-12-27 21:03:33 -0500588 semi: cond!(!is_braced(&body), 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 Tolnayfe9d2782017-12-29 11:32:42 -0500595 tt: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800596 },
David Tolnay57292da2017-12-27 21:03:33 -0500597 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800598 })
David Tolnayedf2b992016-09-23 20:43:45 -0700599 ));
600
David Tolnay500d8322017-12-18 00:32:51 -0800601 // TODO: figure out the actual grammar; is body required to be braced?
602 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500603 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800604 vis: syn!(Visibility) >>
605 macro_: keyword!(macro) >>
606 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500607 args: call!(tt::parenthesized) >>
608 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800609 (ItemMacro2 {
610 attrs: attrs,
611 vis: vis,
612 macro_token: macro_,
613 ident: ident,
614 args: args,
615 body: body,
616 })
617 ));
618
David Tolnay4c614be2017-11-10 00:02:38 -0800619 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500620 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700621 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 extern_: keyword!(extern) >>
623 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700624 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800625 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
626 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800627 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700628 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800629 vis: vis,
630 extern_token: extern_,
631 crate_token: crate_,
632 ident: ident,
633 rename: rename,
634 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800635 })
David Tolnayedf2b992016-09-23 20:43:45 -0700636 ));
637
David Tolnay4c614be2017-11-10 00:02:38 -0800638 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500639 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700640 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800641 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500642 leading_colon: option!(punct!(::)) >>
643 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500644 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500645 true => syn!(UseTree)
646 |
David Tolnay8edcef12017-12-28 12:06:52 -0500647 false => alt!(
648 tuple!(keyword!(as), syn!(Ident)) => {
649 |rename| UseTree::Path(UsePath {
650 ident: prefix.pop().unwrap().into_item(),
651 rename: Some(rename),
652 })
653 }
David Tolnay5f332a92017-12-26 00:42:45 -0500654 |
David Tolnay8edcef12017-12-28 12:06:52 -0500655 epsilon!() => {
656 |_| UseTree::Path(UsePath {
657 ident: prefix.pop().unwrap().into_item(),
658 rename: None,
659 })
660 }
David Tolnay5f332a92017-12-26 00:42:45 -0500661 )
662 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800664 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700665 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800666 vis: vis,
667 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500668 leading_colon: leading_colon,
669 prefix: prefix,
670 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800671 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800672 })
David Tolnay4a057422016-10-08 00:02:31 -0700673 ));
674
David Tolnay5f332a92017-12-26 00:42:45 -0500675 named!(use_prefix -> Ident, alt!(
676 syn!(Ident)
677 |
678 keyword!(self) => { Into::into }
679 |
680 keyword!(super) => { Into::into }
681 |
682 keyword!(crate) => { Into::into }
683 ));
684
685 impl_synom!(UseTree "use tree" alt!(
686 syn!(UsePath) => { UseTree::Path }
687 |
688 syn!(UseGlob) => { UseTree::Glob }
689 |
690 syn!(UseList) => { UseTree::List }
691 ));
692
693 impl_synom!(UsePath "use path" do_parse!(
694 ident: alt!(
695 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400696 |
David Tolnay5f332a92017-12-26 00:42:45 -0500697 keyword!(self) => { Into::into }
698 ) >>
699 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
700 (UsePath {
701 ident: ident,
702 rename: rename,
703 })
704 ));
David Tolnay4a057422016-10-08 00:02:31 -0700705
David Tolnay5f332a92017-12-26 00:42:45 -0500706 impl_synom!(UseGlob "use glob" do_parse!(
707 star: punct!(*) >>
708 (UseGlob {
709 star_token: star,
710 })
711 ));
David Tolnay4a057422016-10-08 00:02:31 -0700712
David Tolnay5f332a92017-12-26 00:42:45 -0500713 impl_synom!(UseList "use list" do_parse!(
714 list: braces!(Delimited::parse_terminated) >>
715 (UseList {
716 brace_token: list.1,
717 items: list.0,
718 })
719 ));
David Tolnay4a057422016-10-08 00:02:31 -0700720
David Tolnay4c614be2017-11-10 00:02:38 -0800721 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500722 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700723 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800724 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500725 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700726 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800727 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800728 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800729 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700730 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800731 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800732 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700733 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800734 vis: vis,
735 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500736 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800737 ident: ident,
738 colon_token: colon,
739 ty: Box::new(ty),
740 eq_token: eq,
741 expr: Box::new(value),
742 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800743 })
David Tolnay47a877c2016-10-01 16:50:55 -0700744 ));
745
David Tolnay4c614be2017-11-10 00:02:38 -0800746 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500747 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700748 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800749 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700750 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800751 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800752 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800753 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700754 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800755 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800756 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700757 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800758 vis: vis,
759 const_token: const_,
760 ident: ident,
761 colon_token: colon,
762 ty: Box::new(ty),
763 eq_token: eq,
764 expr: Box::new(value),
765 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800766 })
David Tolnay47a877c2016-10-01 16:50:55 -0700767 ));
768
David Tolnay4c614be2017-11-10 00:02:38 -0800769 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500770 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700771 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500772 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500773 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700774 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800775 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700776 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700777 generics: syn!(Generics) >>
778 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800779 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500780 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700781 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500782 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700783 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400784 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800785 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700786 attrs: {
787 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700788 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700789 attrs
790 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800791 vis: vis,
792 constness: constness,
793 unsafety: unsafety,
794 abi: abi,
795 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800796 fn_token: fn_,
797 paren_token: inputs.1,
798 inputs: inputs.0,
799 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500800 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800801 generics: Generics {
802 where_clause: where_clause,
803 .. generics
804 },
805 }),
806 ident: ident,
807 block: Box::new(Block {
808 brace_token: inner_attrs_stmts.1,
809 stmts: (inner_attrs_stmts.0).1,
810 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800811 })
David Tolnay42602292016-10-01 22:25:45 -0700812 ));
813
Alex Crichton954046c2017-05-30 21:49:42 -0700814 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400815 named!(parse -> Self, alt!(
816 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800817 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400818 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500819 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800820 self_: keyword!(self) >>
821 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400822 (ArgSelfRef {
823 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500824 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400825 and_token: and,
826 self_token: self_,
827 }.into())
828 )
829 |
830 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500831 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800832 self_: keyword!(self) >>
833 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400834 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500835 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400836 self_token: self_,
837 }.into())
838 )
839 |
840 do_parse!(
841 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800842 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800843 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400844 (ArgCaptured {
845 pat: pat,
846 ty: ty,
847 colon_token: colon,
848 }.into())
849 )
850 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800851 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400852 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700853 }
David Tolnay62f374c2016-10-02 13:37:00 -0700854
David Tolnay4c614be2017-11-10 00:02:38 -0800855 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500856 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700857 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800858 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700859 ident: syn!(Ident) >>
860 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700862 Vec::new(),
863 None,
864 Some(semi),
865 )}
David Tolnay37d10332016-10-13 20:51:04 -0700866 |
Alex Crichton954046c2017-05-30 21:49:42 -0700867 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700868 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500869 many0!(Attribute::parse_inner),
870 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400871 )
David Tolnay570695e2017-06-03 16:15:13 -0700872 ) => {|((inner_attrs, items), brace)| (
873 inner_attrs,
874 Some((brace, items)),
875 None,
876 )}
David Tolnay37d10332016-10-13 20:51:04 -0700877 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800878 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700879 attrs: {
880 let mut attrs = outer_attrs;
881 attrs.extend(content_semi.0);
882 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700883 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800884 vis: vis,
885 mod_token: mod_,
886 ident: ident,
887 content: content_semi.1,
888 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800889 })
David Tolnay35902302016-10-06 01:11:08 -0700890 ));
891
David Tolnay4c614be2017-11-10 00:02:38 -0800892 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500893 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700894 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500895 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800896 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700897 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800898 abi: abi,
899 brace_token: items.1,
900 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800901 })
David Tolnay35902302016-10-06 01:11:08 -0700902 ));
903
David Tolnay8894f602017-11-11 12:11:04 -0800904 impl_synom!(ForeignItem "foreign item" alt!(
905 syn!(ForeignItemFn) => { ForeignItem::Fn }
906 |
907 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800908 |
909 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800910 ));
David Tolnay35902302016-10-06 01:11:08 -0700911
David Tolnay8894f602017-11-11 12:11:04 -0800912 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500913 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700914 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800915 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700916 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700917 generics: syn!(Generics) >>
918 inputs: parens!(do_parse!(
919 args: call!(Delimited::parse_terminated) >>
920 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800921 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700922 (args, variadic)
923 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800924 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500925 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800926 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700927 ({
928 let ((inputs, variadic), parens) = inputs;
929 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800930 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700931 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700932 attrs: attrs,
933 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800934 decl: Box::new(FnDecl {
935 fn_token: fn_,
936 paren_token: parens,
937 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500938 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800939 output: ret,
940 generics: Generics {
941 where_clause: where_clause,
942 .. generics
943 },
944 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700945 vis: vis,
946 }
David Tolnay35902302016-10-06 01:11:08 -0700947 })
948 ));
949
David Tolnay8894f602017-11-11 12:11:04 -0800950 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500951 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700952 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800953 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500954 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700955 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800956 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800957 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800958 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800959 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700960 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700961 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700962 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800963 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500964 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800965 static_token: static_,
966 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700967 vis: vis,
968 })
969 ));
970
David Tolnay199bcbb2017-11-12 10:33:52 -0800971 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500972 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800973 vis: syn!(Visibility) >>
974 type_: keyword!(type) >>
975 ident: syn!(Ident) >>
976 semi: punct!(;) >>
977 (ForeignItemType {
978 attrs: attrs,
979 vis: vis,
980 type_token: type_,
981 ident: ident,
982 semi_token: semi,
983 })
984 ));
985
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800986 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500987 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700988 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800989 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700990 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700991 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500992 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800993 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800994 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800995 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800996 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700997 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800998 vis: vis,
999 type_token: type_,
1000 ident: ident,
1001 generics: Generics {
1002 where_clause: where_clause,
1003 ..generics
1004 },
1005 eq_token: eq,
1006 ty: Box::new(ty),
1007 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001008 })
David Tolnay3cf52982016-10-01 17:11:37 -07001009 ));
1010
David Tolnay4c614be2017-11-10 00:02:38 -08001011 impl_synom!(ItemStruct "struct item" switch!(
1012 map!(syn!(DeriveInput), Into::into),
1013 Item::Struct(item) => value!(item)
1014 |
1015 _ => reject!()
1016 ));
David Tolnay42602292016-10-01 22:25:45 -07001017
David Tolnay4c614be2017-11-10 00:02:38 -08001018 impl_synom!(ItemEnum "enum item" switch!(
1019 map!(syn!(DeriveInput), Into::into),
1020 Item::Enum(item) => value!(item)
1021 |
1022 _ => reject!()
1023 ));
1024
1025 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001026 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001027 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001028 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001029 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001030 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001031 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001032 fields: braces!(call!(Delimited::parse_terminated_with,
1033 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001034 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001035 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001036 vis: vis,
1037 union_token: union_,
1038 ident: ident,
1039 generics: Generics {
1040 where_clause: where_clause,
1041 .. generics
1042 },
1043 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -08001044 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001045 ));
1046
David Tolnay4c614be2017-11-10 00:02:38 -08001047 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001048 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001049 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001050 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001051 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001052 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001053 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001054 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001055 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001056 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001057 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001058 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001059 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001060 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001061 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001062 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001063 vis: vis,
1064 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001065 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001066 trait_token: trait_,
1067 ident: ident,
1068 generics: Generics {
1069 where_clause: where_clause,
1070 .. generics
1071 },
1072 colon_token: colon,
1073 supertraits: bounds.unwrap_or_default(),
1074 brace_token: body.1,
1075 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001076 })
David Tolnay0aecb732016-10-03 23:03:50 -07001077 ));
1078
David Tolnay4c614be2017-11-10 00:02:38 -08001079 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001080 attrs: many0!(Attribute::parse_outer) >>
David Tolnay9b258702017-12-29 02:24:41 -05001081 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001082 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001083 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001084 for_: keyword!(for) >>
1085 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001086 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001087 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001088 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001089 unsafety: unsafety,
1090 impl_token: impl_,
1091 path: path,
1092 for_token: for_,
1093 dot2_token: dot2,
1094 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001095 })
David Tolnayf94e2362016-10-04 00:29:51 -07001096 ));
1097
David Tolnayda705bd2017-11-10 21:58:05 -08001098 impl_synom!(TraitItem "trait item" alt!(
1099 syn!(TraitItemConst) => { TraitItem::Const }
1100 |
1101 syn!(TraitItemMethod) => { TraitItem::Method }
1102 |
1103 syn!(TraitItemType) => { TraitItem::Type }
1104 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001105 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001106 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001107
David Tolnayda705bd2017-11-10 21:58:05 -08001108 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001109 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001110 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001111 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001112 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001113 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001114 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1115 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001116 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001117 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001118 const_token: const_,
1119 ident: ident,
1120 colon_token: colon,
1121 ty: ty,
1122 default: default,
1123 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001124 })
1125 ));
1126
David Tolnayda705bd2017-11-10 21:58:05 -08001127 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001128 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001129 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001130 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001131 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001132 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001133 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001134 generics: syn!(Generics) >>
1135 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001136 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001137 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001138 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001139 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001140 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001141 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001142 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001143 ({
1144 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001145 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001146 None => (Vec::new(), None),
1147 };
David Tolnayda705bd2017-11-10 21:58:05 -08001148 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001149 attrs: {
1150 let mut attrs = outer_attrs;
1151 attrs.extend(inner_attrs);
1152 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001153 },
David Tolnayda705bd2017-11-10 21:58:05 -08001154 sig: MethodSig {
1155 constness: constness,
1156 unsafety: unsafety,
1157 abi: abi,
1158 ident: ident,
1159 decl: FnDecl {
1160 inputs: inputs.0,
1161 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001162 fn_token: fn_,
1163 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001164 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001165 generics: Generics {
1166 where_clause: where_clause,
1167 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001168 },
1169 },
David Tolnayda705bd2017-11-10 21:58:05 -08001170 },
1171 default: stmts.map(|stmts| {
1172 Block {
1173 stmts: stmts.0,
1174 brace_token: stmts.1,
1175 }
1176 }),
1177 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001178 }
David Tolnay0aecb732016-10-03 23:03:50 -07001179 })
1180 ));
1181
David Tolnayda705bd2017-11-10 21:58:05 -08001182 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001183 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001184 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001185 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001186 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001187 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001188 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001189 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001190 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001191 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001192 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001193 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001194 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001195 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001196 type_token: type_,
1197 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001198 generics: Generics {
1199 where_clause: where_clause,
1200 .. generics
1201 },
David Tolnayda705bd2017-11-10 21:58:05 -08001202 colon_token: colon,
1203 bounds: bounds.unwrap_or_default(),
1204 default: default,
1205 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001206 })
1207 ));
1208
David Tolnaydecf28d2017-11-11 11:56:45 -08001209 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001210 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001211 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001212 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001213 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001214 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001215 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001216 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001217 })
1218 ));
1219
David Tolnay4c614be2017-11-10 00:02:38 -08001220 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001221 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001222 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001223 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001224 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001225 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001226 polarity_path: alt!(
1227 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001228 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001229 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001230 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001231 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001232 )
1233 |
David Tolnay570695e2017-06-03 16:15:13 -07001234 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001235 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001236 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001237 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001238 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001239 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001240 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001241 defaultness: defaultness,
1242 unsafety: unsafety,
1243 impl_token: impl_,
1244 generics: Generics {
1245 where_clause: where_clause,
1246 .. generics
1247 },
1248 trait_: polarity_path,
1249 self_ty: Box::new(self_ty),
1250 brace_token: body.1,
1251 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001252 })
David Tolnay4c9be372016-10-06 00:47:37 -07001253 ));
1254
David Tolnay857628c2017-11-11 12:25:31 -08001255 impl_synom!(ImplItem "item in impl block" alt!(
1256 syn!(ImplItemConst) => { ImplItem::Const }
1257 |
1258 syn!(ImplItemMethod) => { ImplItem::Method }
1259 |
1260 syn!(ImplItemType) => { ImplItem::Type }
1261 |
1262 syn!(ImplItemMacro) => { ImplItem::Macro }
1263 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001264
David Tolnay857628c2017-11-11 12:25:31 -08001265 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001266 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001267 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001268 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001270 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001271 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001272 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001273 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001274 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001275 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001276 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001277 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001278 vis: vis,
1279 defaultness: defaultness,
1280 const_token: const_,
1281 ident: ident,
1282 colon_token: colon,
1283 ty: ty,
1284 eq_token: eq,
1285 expr: value,
1286 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001287 })
1288 ));
1289
David Tolnay857628c2017-11-11 12:25:31 -08001290 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001291 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001292 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001293 defaultness: option!(keyword!(default)) >>
1294 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001295 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001297 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001298 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001299 generics: syn!(Generics) >>
1300 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001301 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001302 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001303 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001304 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001305 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001306 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001307 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001308 attrs: {
1309 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001310 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001311 attrs
1312 },
David Tolnay857628c2017-11-11 12:25:31 -08001313 vis: vis,
1314 defaultness: defaultness,
1315 sig: MethodSig {
1316 constness: constness,
1317 unsafety: unsafety,
1318 abi: abi,
1319 ident: ident,
1320 decl: FnDecl {
1321 fn_token: fn_,
1322 paren_token: inputs.1,
1323 inputs: inputs.0,
1324 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001325 generics: Generics {
1326 where_clause: where_clause,
1327 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001328 },
David Tolnayd2836e22017-12-27 23:13:00 -05001329 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001330 },
David Tolnay857628c2017-11-11 12:25:31 -08001331 },
1332 block: Block {
1333 brace_token: inner_attrs_stmts.1,
1334 stmts: (inner_attrs_stmts.0).1,
1335 },
David Tolnay4c9be372016-10-06 00:47:37 -07001336 })
1337 ));
1338
David Tolnay857628c2017-11-11 12:25:31 -08001339 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001340 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001341 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001342 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001343 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001344 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001345 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001346 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001347 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001348 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001349 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001350 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001351 vis: vis,
1352 defaultness: defaultness,
1353 type_token: type_,
1354 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001355 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001356 eq_token: eq,
1357 ty: ty,
1358 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001359 })
1360 ));
1361
David Tolnay857628c2017-11-11 12:25:31 -08001362 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001363 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001364 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001365 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001366 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001367 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001368 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001369 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001370 })
1371 ));
1372
David Tolnay57292da2017-12-27 21:03:33 -05001373 fn is_braced(tt: &TokenTree) -> bool {
1374 match tt.kind {
1375 TokenNode::Group(Delimiter::Brace, _) => true,
1376 _ => false,
1377 }
1378 }
David Tolnayedf2b992016-09-23 20:43:45 -07001379}
David Tolnay4a51dc72016-10-01 00:40:31 -07001380
1381#[cfg(feature = "printing")]
1382mod printing {
1383 use super::*;
1384 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001385 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001386 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001387
David Tolnay1bfa7332017-11-11 12:41:20 -08001388 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001389 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001390 tokens.append_all(self.attrs.outer());
1391 self.vis.to_tokens(tokens);
1392 self.extern_token.to_tokens(tokens);
1393 self.crate_token.to_tokens(tokens);
1394 self.ident.to_tokens(tokens);
1395 if let Some((ref as_token, ref rename)) = self.rename {
1396 as_token.to_tokens(tokens);
1397 rename.to_tokens(tokens);
1398 }
1399 self.semi_token.to_tokens(tokens);
1400 }
1401 }
1402
1403 impl ToTokens for ItemUse {
1404 fn to_tokens(&self, tokens: &mut Tokens) {
1405 tokens.append_all(self.attrs.outer());
1406 self.vis.to_tokens(tokens);
1407 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001408 self.leading_colon.to_tokens(tokens);
1409 self.prefix.to_tokens(tokens);
1410 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001411 self.semi_token.to_tokens(tokens);
1412 }
1413 }
1414
1415 impl ToTokens for ItemStatic {
1416 fn to_tokens(&self, tokens: &mut Tokens) {
1417 tokens.append_all(self.attrs.outer());
1418 self.vis.to_tokens(tokens);
1419 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001420 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001421 self.ident.to_tokens(tokens);
1422 self.colon_token.to_tokens(tokens);
1423 self.ty.to_tokens(tokens);
1424 self.eq_token.to_tokens(tokens);
1425 self.expr.to_tokens(tokens);
1426 self.semi_token.to_tokens(tokens);
1427 }
1428 }
1429
1430 impl ToTokens for ItemConst {
1431 fn to_tokens(&self, tokens: &mut Tokens) {
1432 tokens.append_all(self.attrs.outer());
1433 self.vis.to_tokens(tokens);
1434 self.const_token.to_tokens(tokens);
1435 self.ident.to_tokens(tokens);
1436 self.colon_token.to_tokens(tokens);
1437 self.ty.to_tokens(tokens);
1438 self.eq_token.to_tokens(tokens);
1439 self.expr.to_tokens(tokens);
1440 self.semi_token.to_tokens(tokens);
1441 }
1442 }
1443
1444 impl ToTokens for ItemFn {
1445 fn to_tokens(&self, tokens: &mut Tokens) {
1446 tokens.append_all(self.attrs.outer());
1447 self.vis.to_tokens(tokens);
1448 self.constness.to_tokens(tokens);
1449 self.unsafety.to_tokens(tokens);
1450 self.abi.to_tokens(tokens);
1451 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1452 self.block.brace_token.surround(tokens, |tokens| {
1453 tokens.append_all(self.attrs.inner());
1454 tokens.append_all(&self.block.stmts);
1455 });
1456 }
1457 }
1458
1459 impl ToTokens for ItemMod {
1460 fn to_tokens(&self, tokens: &mut Tokens) {
1461 tokens.append_all(self.attrs.outer());
1462 self.vis.to_tokens(tokens);
1463 self.mod_token.to_tokens(tokens);
1464 self.ident.to_tokens(tokens);
1465 if let Some((ref brace, ref items)) = self.content {
1466 brace.surround(tokens, |tokens| {
1467 tokens.append_all(self.attrs.inner());
1468 tokens.append_all(items);
1469 });
1470 } else {
1471 TokensOrDefault(&self.semi).to_tokens(tokens);
1472 }
1473 }
1474 }
1475
1476 impl ToTokens for ItemForeignMod {
1477 fn to_tokens(&self, tokens: &mut Tokens) {
1478 tokens.append_all(self.attrs.outer());
1479 self.abi.to_tokens(tokens);
1480 self.brace_token.surround(tokens, |tokens| {
1481 tokens.append_all(&self.items);
1482 });
1483 }
1484 }
1485
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001486 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001487 fn to_tokens(&self, tokens: &mut Tokens) {
1488 tokens.append_all(self.attrs.outer());
1489 self.vis.to_tokens(tokens);
1490 self.type_token.to_tokens(tokens);
1491 self.ident.to_tokens(tokens);
1492 self.generics.to_tokens(tokens);
1493 self.generics.where_clause.to_tokens(tokens);
1494 self.eq_token.to_tokens(tokens);
1495 self.ty.to_tokens(tokens);
1496 self.semi_token.to_tokens(tokens);
1497 }
1498 }
1499
1500 impl ToTokens for ItemEnum {
1501 fn to_tokens(&self, tokens: &mut Tokens) {
1502 tokens.append_all(self.attrs.outer());
1503 self.vis.to_tokens(tokens);
1504 self.enum_token.to_tokens(tokens);
1505 self.ident.to_tokens(tokens);
1506 self.generics.to_tokens(tokens);
1507 self.generics.where_clause.to_tokens(tokens);
1508 self.brace_token.surround(tokens, |tokens| {
1509 self.variants.to_tokens(tokens);
1510 });
1511 }
1512 }
1513
1514 impl ToTokens for ItemStruct {
1515 fn to_tokens(&self, tokens: &mut Tokens) {
1516 tokens.append_all(self.attrs.outer());
1517 self.vis.to_tokens(tokens);
1518 self.struct_token.to_tokens(tokens);
1519 self.ident.to_tokens(tokens);
1520 self.generics.to_tokens(tokens);
1521 match self.data {
1522 VariantData::Struct(..) => {
1523 self.generics.where_clause.to_tokens(tokens);
1524 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001525 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001526 VariantData::Tuple(..) => {
1527 self.data.to_tokens(tokens);
1528 self.generics.where_clause.to_tokens(tokens);
1529 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001530 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001531 VariantData::Unit => {
1532 self.generics.where_clause.to_tokens(tokens);
1533 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001534 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001535 }
1536 }
1537 }
1538
1539 impl ToTokens for ItemUnion {
1540 fn to_tokens(&self, tokens: &mut Tokens) {
1541 tokens.append_all(self.attrs.outer());
1542 self.vis.to_tokens(tokens);
1543 self.union_token.to_tokens(tokens);
1544 self.ident.to_tokens(tokens);
1545 self.generics.to_tokens(tokens);
1546 self.generics.where_clause.to_tokens(tokens);
1547 // XXX: Should we handle / complain when using a
1548 // non-VariantData::Struct Variant in Union?
1549 self.data.to_tokens(tokens);
1550 }
1551 }
1552
1553 impl ToTokens for ItemTrait {
1554 fn to_tokens(&self, tokens: &mut Tokens) {
1555 tokens.append_all(self.attrs.outer());
1556 self.vis.to_tokens(tokens);
1557 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001558 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001559 self.trait_token.to_tokens(tokens);
1560 self.ident.to_tokens(tokens);
1561 self.generics.to_tokens(tokens);
1562 if !self.supertraits.is_empty() {
1563 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1564 self.supertraits.to_tokens(tokens);
1565 }
1566 self.generics.where_clause.to_tokens(tokens);
1567 self.brace_token.surround(tokens, |tokens| {
1568 tokens.append_all(&self.items);
1569 });
1570 }
1571 }
1572
1573 impl ToTokens for ItemDefaultImpl {
1574 fn to_tokens(&self, tokens: &mut Tokens) {
1575 tokens.append_all(self.attrs.outer());
1576 self.unsafety.to_tokens(tokens);
1577 self.impl_token.to_tokens(tokens);
1578 self.path.to_tokens(tokens);
1579 self.for_token.to_tokens(tokens);
1580 self.dot2_token.to_tokens(tokens);
1581 self.brace_token.surround(tokens, |_tokens| {});
1582 }
1583 }
1584
1585 impl ToTokens for ItemImpl {
1586 fn to_tokens(&self, tokens: &mut Tokens) {
1587 tokens.append_all(self.attrs.outer());
1588 self.defaultness.to_tokens(tokens);
1589 self.unsafety.to_tokens(tokens);
1590 self.impl_token.to_tokens(tokens);
1591 self.generics.to_tokens(tokens);
1592 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1593 polarity.to_tokens(tokens);
1594 path.to_tokens(tokens);
1595 for_token.to_tokens(tokens);
1596 }
1597 self.self_ty.to_tokens(tokens);
1598 self.generics.where_clause.to_tokens(tokens);
1599 self.brace_token.surround(tokens, |tokens| {
1600 tokens.append_all(&self.items);
1601 });
1602 }
1603 }
1604
1605 impl ToTokens for ItemMacro {
1606 fn to_tokens(&self, tokens: &mut Tokens) {
1607 tokens.append_all(self.attrs.outer());
1608 self.mac.path.to_tokens(tokens);
1609 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001610 self.ident.to_tokens(tokens);
David Tolnayfe9d2782017-12-29 11:32:42 -05001611 self.mac.tt.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001612 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001613 }
1614 }
David Tolnay42602292016-10-01 22:25:45 -07001615
David Tolnay500d8322017-12-18 00:32:51 -08001616 impl ToTokens for ItemMacro2 {
1617 fn to_tokens(&self, tokens: &mut Tokens) {
1618 tokens.append_all(self.attrs.outer());
1619 self.vis.to_tokens(tokens);
1620 self.macro_token.to_tokens(tokens);
1621 self.ident.to_tokens(tokens);
1622 self.args.to_tokens(tokens);
1623 self.body.to_tokens(tokens);
1624 }
1625 }
1626
David Tolnay2ae520a2017-12-29 11:19:50 -05001627 impl ToTokens for ItemVerbatim {
1628 fn to_tokens(&self, tokens: &mut Tokens) {
1629 self.tts.to_tokens(tokens);
1630 }
1631 }
1632
David Tolnay5f332a92017-12-26 00:42:45 -05001633 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001634 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001635 self.ident.to_tokens(tokens);
1636 if let Some((ref as_token, ref rename)) = self.rename {
1637 as_token.to_tokens(tokens);
1638 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001639 }
David Tolnay4a057422016-10-08 00:02:31 -07001640 }
1641 }
1642
David Tolnay5f332a92017-12-26 00:42:45 -05001643 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001644 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001645 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001646 }
1647 }
1648
David Tolnay5f332a92017-12-26 00:42:45 -05001649 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001650 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001651 self.brace_token.surround(tokens, |tokens| {
1652 self.items.to_tokens(tokens);
1653 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 }
1655 }
1656
David Tolnay1bfa7332017-11-11 12:41:20 -08001657 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001658 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001659 tokens.append_all(self.attrs.outer());
1660 self.const_token.to_tokens(tokens);
1661 self.ident.to_tokens(tokens);
1662 self.colon_token.to_tokens(tokens);
1663 self.ty.to_tokens(tokens);
1664 if let Some((ref eq_token, ref default)) = self.default {
1665 eq_token.to_tokens(tokens);
1666 default.to_tokens(tokens);
1667 }
1668 self.semi_token.to_tokens(tokens);
1669 }
1670 }
1671
1672 impl ToTokens for TraitItemMethod {
1673 fn to_tokens(&self, tokens: &mut Tokens) {
1674 tokens.append_all(self.attrs.outer());
1675 self.sig.to_tokens(tokens);
1676 match self.default {
1677 Some(ref block) => {
1678 block.brace_token.surround(tokens, |tokens| {
1679 tokens.append_all(self.attrs.inner());
1680 tokens.append_all(&block.stmts);
1681 });
David Tolnayca085422016-10-04 00:12:38 -07001682 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001683 None => {
1684 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001685 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001686 }
1687 }
1688 }
1689
1690 impl ToTokens for TraitItemType {
1691 fn to_tokens(&self, tokens: &mut Tokens) {
1692 tokens.append_all(self.attrs.outer());
1693 self.type_token.to_tokens(tokens);
1694 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001695 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001696 if !self.bounds.is_empty() {
1697 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1698 self.bounds.to_tokens(tokens);
1699 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001700 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001701 if let Some((ref eq_token, ref default)) = self.default {
1702 eq_token.to_tokens(tokens);
1703 default.to_tokens(tokens);
1704 }
1705 self.semi_token.to_tokens(tokens);
1706 }
1707 }
1708
1709 impl ToTokens for TraitItemMacro {
1710 fn to_tokens(&self, tokens: &mut Tokens) {
1711 tokens.append_all(self.attrs.outer());
1712 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001713 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001714 }
1715 }
1716
David Tolnay2ae520a2017-12-29 11:19:50 -05001717 impl ToTokens for TraitItemVerbatim {
1718 fn to_tokens(&self, tokens: &mut Tokens) {
1719 self.tts.to_tokens(tokens);
1720 }
1721 }
1722
David Tolnay857628c2017-11-11 12:25:31 -08001723 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001724 fn to_tokens(&self, tokens: &mut Tokens) {
1725 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001726 self.vis.to_tokens(tokens);
1727 self.defaultness.to_tokens(tokens);
1728 self.const_token.to_tokens(tokens);
1729 self.ident.to_tokens(tokens);
1730 self.colon_token.to_tokens(tokens);
1731 self.ty.to_tokens(tokens);
1732 self.eq_token.to_tokens(tokens);
1733 self.expr.to_tokens(tokens);
1734 self.semi_token.to_tokens(tokens);
1735 }
1736 }
1737
1738 impl ToTokens for ImplItemMethod {
1739 fn to_tokens(&self, tokens: &mut Tokens) {
1740 tokens.append_all(self.attrs.outer());
1741 self.vis.to_tokens(tokens);
1742 self.defaultness.to_tokens(tokens);
1743 self.sig.to_tokens(tokens);
1744 self.block.brace_token.surround(tokens, |tokens| {
1745 tokens.append_all(self.attrs.inner());
1746 tokens.append_all(&self.block.stmts);
1747 });
1748 }
1749 }
1750
1751 impl ToTokens for ImplItemType {
1752 fn to_tokens(&self, tokens: &mut Tokens) {
1753 tokens.append_all(self.attrs.outer());
1754 self.vis.to_tokens(tokens);
1755 self.defaultness.to_tokens(tokens);
1756 self.type_token.to_tokens(tokens);
1757 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001758 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001759 self.eq_token.to_tokens(tokens);
1760 self.ty.to_tokens(tokens);
1761 self.semi_token.to_tokens(tokens);
1762 }
1763 }
1764
1765 impl ToTokens for ImplItemMacro {
1766 fn to_tokens(&self, tokens: &mut Tokens) {
1767 tokens.append_all(self.attrs.outer());
1768 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001769 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001770 }
1771 }
1772
David Tolnay2ae520a2017-12-29 11:19:50 -05001773 impl ToTokens for ImplItemVerbatim {
1774 fn to_tokens(&self, tokens: &mut Tokens) {
1775 self.tts.to_tokens(tokens);
1776 }
1777 }
1778
David Tolnay8894f602017-11-11 12:11:04 -08001779 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001780 fn to_tokens(&self, tokens: &mut Tokens) {
1781 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001782 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001783 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1784 self.semi_token.to_tokens(tokens);
1785 }
1786 }
1787
1788 impl ToTokens for ForeignItemStatic {
1789 fn to_tokens(&self, tokens: &mut Tokens) {
1790 tokens.append_all(self.attrs.outer());
1791 self.vis.to_tokens(tokens);
1792 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001793 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001794 self.ident.to_tokens(tokens);
1795 self.colon_token.to_tokens(tokens);
1796 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001797 self.semi_token.to_tokens(tokens);
1798 }
1799 }
1800
David Tolnay199bcbb2017-11-12 10:33:52 -08001801 impl ToTokens for ForeignItemType {
1802 fn to_tokens(&self, tokens: &mut Tokens) {
1803 tokens.append_all(self.attrs.outer());
1804 self.vis.to_tokens(tokens);
1805 self.type_token.to_tokens(tokens);
1806 self.ident.to_tokens(tokens);
1807 self.semi_token.to_tokens(tokens);
1808 }
1809 }
1810
David Tolnay2ae520a2017-12-29 11:19:50 -05001811 impl ToTokens for ForeignItemVerbatim {
1812 fn to_tokens(&self, tokens: &mut Tokens) {
1813 self.tts.to_tokens(tokens);
1814 }
1815 }
1816
David Tolnay570695e2017-06-03 16:15:13 -07001817 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001818 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001819 self.constness.to_tokens(tokens);
1820 self.unsafety.to_tokens(tokens);
1821 self.abi.to_tokens(tokens);
1822 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001823 }
1824 }
1825
David Tolnay570695e2017-06-03 16:15:13 -07001826 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001827
1828 impl<'a> ToTokens for NamedDecl<'a> {
1829 fn to_tokens(&self, tokens: &mut Tokens) {
1830 self.0.fn_token.to_tokens(tokens);
1831 self.1.to_tokens(tokens);
1832 self.0.generics.to_tokens(tokens);
1833 self.0.paren_token.surround(tokens, |tokens| {
1834 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001835 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1836 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001837 }
David Tolnayd2836e22017-12-27 23:13:00 -05001838 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001839 });
1840 self.0.output.to_tokens(tokens);
1841 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001842 }
1843 }
1844
Alex Crichton62a0a592017-05-22 13:58:53 -07001845 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001846 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001847 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001848 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001849 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001850 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001851 }
1852 }
1853
1854 impl ToTokens for ArgSelf {
1855 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001856 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001857 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001858 }
1859 }
1860
1861 impl ToTokens for ArgCaptured {
1862 fn to_tokens(&self, tokens: &mut Tokens) {
1863 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001864 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001865 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001866 }
1867 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001868}