blob: 82712d39b8cdadb1e16b2d7dfb6ce8918c546dcd [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 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800848
849 fn description() -> Option<&'static str> {
850 Some("function argument")
851 }
Alex Crichton954046c2017-05-30 21:49:42 -0700852 }
David Tolnay62f374c2016-10-02 13:37:00 -0700853
David Tolnay4c614be2017-11-10 00:02:38 -0800854 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500855 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700856 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800857 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700858 ident: syn!(Ident) >>
859 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800860 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700861 Vec::new(),
862 None,
863 Some(semi),
864 )}
David Tolnay37d10332016-10-13 20:51:04 -0700865 |
Alex Crichton954046c2017-05-30 21:49:42 -0700866 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700867 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500868 many0!(Attribute::parse_inner),
869 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400870 )
David Tolnay570695e2017-06-03 16:15:13 -0700871 ) => {|((inner_attrs, items), brace)| (
872 inner_attrs,
873 Some((brace, items)),
874 None,
875 )}
David Tolnay37d10332016-10-13 20:51:04 -0700876 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800877 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700878 attrs: {
879 let mut attrs = outer_attrs;
880 attrs.extend(content_semi.0);
881 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700882 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800883 vis: vis,
884 mod_token: mod_,
885 ident: ident,
886 content: content_semi.1,
887 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800888 })
David Tolnay35902302016-10-06 01:11:08 -0700889 ));
890
David Tolnay4c614be2017-11-10 00:02:38 -0800891 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500892 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700893 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500894 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800895 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700896 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800897 abi: abi,
898 brace_token: items.1,
899 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800900 })
David Tolnay35902302016-10-06 01:11:08 -0700901 ));
902
David Tolnay8894f602017-11-11 12:11:04 -0800903 impl_synom!(ForeignItem "foreign item" alt!(
904 syn!(ForeignItemFn) => { ForeignItem::Fn }
905 |
906 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800907 |
908 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800909 ));
David Tolnay35902302016-10-06 01:11:08 -0700910
David Tolnay8894f602017-11-11 12:11:04 -0800911 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500912 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700913 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700915 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700916 generics: syn!(Generics) >>
917 inputs: parens!(do_parse!(
918 args: call!(Delimited::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500919 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700920 (args, variadic)
921 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800922 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500923 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800924 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700925 ({
926 let ((inputs, variadic), parens) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800927 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700928 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700929 attrs: attrs,
930 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800931 decl: Box::new(FnDecl {
932 fn_token: fn_,
933 paren_token: parens,
934 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500935 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800936 output: ret,
937 generics: Generics {
938 where_clause: where_clause,
939 .. generics
940 },
941 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700942 vis: vis,
943 }
David Tolnay35902302016-10-06 01:11:08 -0700944 })
945 ));
946
David Tolnay8894f602017-11-11 12:11:04 -0800947 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500948 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700949 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800950 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500951 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700952 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800953 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800954 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800955 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800956 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700957 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700958 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700959 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800960 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500961 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800962 static_token: static_,
963 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700964 vis: vis,
965 })
966 ));
967
David Tolnay199bcbb2017-11-12 10:33:52 -0800968 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500969 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800970 vis: syn!(Visibility) >>
971 type_: keyword!(type) >>
972 ident: syn!(Ident) >>
973 semi: punct!(;) >>
974 (ForeignItemType {
975 attrs: attrs,
976 vis: vis,
977 type_token: type_,
978 ident: ident,
979 semi_token: semi,
980 })
981 ));
982
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800983 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500984 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700985 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700987 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700988 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500989 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800991 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800992 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800993 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700994 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800995 vis: vis,
996 type_token: type_,
997 ident: ident,
998 generics: Generics {
999 where_clause: where_clause,
1000 ..generics
1001 },
1002 eq_token: eq,
1003 ty: Box::new(ty),
1004 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001005 })
David Tolnay3cf52982016-10-01 17:11:37 -07001006 ));
1007
David Tolnay4c614be2017-11-10 00:02:38 -08001008 impl_synom!(ItemStruct "struct item" switch!(
1009 map!(syn!(DeriveInput), Into::into),
1010 Item::Struct(item) => value!(item)
1011 |
1012 _ => reject!()
1013 ));
David Tolnay42602292016-10-01 22:25:45 -07001014
David Tolnay4c614be2017-11-10 00:02:38 -08001015 impl_synom!(ItemEnum "enum item" switch!(
1016 map!(syn!(DeriveInput), Into::into),
1017 Item::Enum(item) => value!(item)
1018 |
1019 _ => reject!()
1020 ));
1021
1022 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001023 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001024 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001026 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001027 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001028 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001029 fields: braces!(call!(Delimited::parse_terminated_with,
1030 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001031 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001032 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001033 vis: vis,
1034 union_token: union_,
1035 ident: ident,
1036 generics: Generics {
1037 where_clause: where_clause,
1038 .. generics
1039 },
1040 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -08001041 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001042 ));
1043
David Tolnay4c614be2017-11-10 00:02:38 -08001044 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001045 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001046 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001047 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001048 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001049 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001050 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001051 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001052 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001053 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001054 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001055 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001056 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001057 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001058 vis: vis,
1059 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001060 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001061 trait_token: trait_,
1062 ident: ident,
1063 generics: Generics {
1064 where_clause: where_clause,
1065 .. generics
1066 },
1067 colon_token: colon,
1068 supertraits: bounds.unwrap_or_default(),
1069 brace_token: body.1,
1070 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001071 })
David Tolnay0aecb732016-10-03 23:03:50 -07001072 ));
1073
David Tolnay03342952017-12-29 11:52:00 -05001074 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1075 Ok((cursor, cursor))
1076 }
1077
1078 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1079 begin: call!(grab_cursor) >>
1080 many0!(Attribute::parse_outer) >>
1081 option!(keyword!(unsafe)) >>
1082 keyword!(impl) >>
1083 syn!(Path) >>
1084 keyword!(for) >>
1085 punct!(..) >>
1086 braces!(epsilon!()) >>
1087 end: call!(grab_cursor) >>
1088 ({
1089 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1090 let len = tts.len() - end.token_stream().into_iter().count();
1091 ItemVerbatim {
1092 tts: tts.into_iter().take(len).collect(),
1093 }
David Tolnay4c614be2017-11-10 00:02:38 -08001094 })
David Tolnayf94e2362016-10-04 00:29:51 -07001095 ));
1096
David Tolnayda705bd2017-11-10 21:58:05 -08001097 impl_synom!(TraitItem "trait item" alt!(
1098 syn!(TraitItemConst) => { TraitItem::Const }
1099 |
1100 syn!(TraitItemMethod) => { TraitItem::Method }
1101 |
1102 syn!(TraitItemType) => { TraitItem::Type }
1103 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001104 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001105 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001106
David Tolnayda705bd2017-11-10 21:58:05 -08001107 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001108 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001110 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001111 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001112 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001113 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1114 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001115 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001116 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001117 const_token: const_,
1118 ident: ident,
1119 colon_token: colon,
1120 ty: ty,
1121 default: default,
1122 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001123 })
1124 ));
1125
David Tolnayda705bd2017-11-10 21:58:05 -08001126 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001127 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001128 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001129 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001130 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001131 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001132 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001133 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001134 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001135 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001136 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001137 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001138 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001139 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001140 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001141 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001142 ({
1143 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001144 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001145 None => (Vec::new(), None),
1146 };
David Tolnayda705bd2017-11-10 21:58:05 -08001147 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001148 attrs: {
1149 let mut attrs = outer_attrs;
1150 attrs.extend(inner_attrs);
1151 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001152 },
David Tolnayda705bd2017-11-10 21:58:05 -08001153 sig: MethodSig {
1154 constness: constness,
1155 unsafety: unsafety,
1156 abi: abi,
1157 ident: ident,
1158 decl: FnDecl {
1159 inputs: inputs.0,
1160 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001161 fn_token: fn_,
1162 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001163 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001164 generics: Generics {
1165 where_clause: where_clause,
1166 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001167 },
1168 },
David Tolnayda705bd2017-11-10 21:58:05 -08001169 },
1170 default: stmts.map(|stmts| {
1171 Block {
1172 stmts: stmts.0,
1173 brace_token: stmts.1,
1174 }
1175 }),
1176 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001177 }
David Tolnay0aecb732016-10-03 23:03:50 -07001178 })
1179 ));
1180
David Tolnayda705bd2017-11-10 21:58:05 -08001181 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001182 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001184 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001185 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001186 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001187 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001188 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001189 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001190 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001191 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001192 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001193 type_token: type_,
1194 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001195 generics: Generics {
1196 where_clause: where_clause,
1197 .. generics
1198 },
David Tolnayda705bd2017-11-10 21:58:05 -08001199 colon_token: colon,
1200 bounds: bounds.unwrap_or_default(),
1201 default: default,
1202 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001203 })
1204 ));
1205
David Tolnaydecf28d2017-11-11 11:56:45 -08001206 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001207 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001208 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001209 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001210 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001211 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001212 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001213 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001214 })
1215 ));
1216
David Tolnay4c614be2017-11-10 00:02:38 -08001217 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001218 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001219 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001220 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001221 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001222 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001223 polarity_path: alt!(
1224 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001225 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001226 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001227 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001228 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001229 )
1230 |
David Tolnay570695e2017-06-03 16:15:13 -07001231 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001232 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001233 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001234 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001235 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001236 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001237 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001238 defaultness: defaultness,
1239 unsafety: unsafety,
1240 impl_token: impl_,
1241 generics: Generics {
1242 where_clause: where_clause,
1243 .. generics
1244 },
1245 trait_: polarity_path,
1246 self_ty: Box::new(self_ty),
1247 brace_token: body.1,
1248 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001249 })
David Tolnay4c9be372016-10-06 00:47:37 -07001250 ));
1251
David Tolnay857628c2017-11-11 12:25:31 -08001252 impl_synom!(ImplItem "item in impl block" alt!(
1253 syn!(ImplItemConst) => { ImplItem::Const }
1254 |
1255 syn!(ImplItemMethod) => { ImplItem::Method }
1256 |
1257 syn!(ImplItemType) => { ImplItem::Type }
1258 |
1259 syn!(ImplItemMacro) => { ImplItem::Macro }
1260 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001261
David Tolnay857628c2017-11-11 12:25:31 -08001262 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001263 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001264 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001265 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001266 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001267 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001268 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001269 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001270 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001271 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001272 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001273 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001274 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001275 vis: vis,
1276 defaultness: defaultness,
1277 const_token: const_,
1278 ident: ident,
1279 colon_token: colon,
1280 ty: ty,
1281 eq_token: eq,
1282 expr: value,
1283 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001284 })
1285 ));
1286
David Tolnay857628c2017-11-11 12:25:31 -08001287 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001288 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001289 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001290 defaultness: option!(keyword!(default)) >>
1291 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001292 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001293 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001295 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001297 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001298 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001299 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001300 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001301 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001302 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001303 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001304 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001305 attrs: {
1306 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001307 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001308 attrs
1309 },
David Tolnay857628c2017-11-11 12:25:31 -08001310 vis: vis,
1311 defaultness: defaultness,
1312 sig: MethodSig {
1313 constness: constness,
1314 unsafety: unsafety,
1315 abi: abi,
1316 ident: ident,
1317 decl: FnDecl {
1318 fn_token: fn_,
1319 paren_token: inputs.1,
1320 inputs: inputs.0,
1321 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001322 generics: Generics {
1323 where_clause: where_clause,
1324 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001325 },
David Tolnayd2836e22017-12-27 23:13:00 -05001326 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001327 },
David Tolnay857628c2017-11-11 12:25:31 -08001328 },
1329 block: Block {
1330 brace_token: inner_attrs_stmts.1,
1331 stmts: (inner_attrs_stmts.0).1,
1332 },
David Tolnay4c9be372016-10-06 00:47:37 -07001333 })
1334 ));
1335
David Tolnay857628c2017-11-11 12:25:31 -08001336 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001337 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001338 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001339 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001340 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001341 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001342 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001343 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001344 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001345 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001346 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001347 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001348 vis: vis,
1349 defaultness: defaultness,
1350 type_token: type_,
1351 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001352 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001353 eq_token: eq,
1354 ty: ty,
1355 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001356 })
1357 ));
1358
David Tolnay857628c2017-11-11 12:25:31 -08001359 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001360 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001361 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001362 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001363 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001364 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001365 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001366 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001367 })
1368 ));
1369
David Tolnayab919512017-12-30 23:31:51 -05001370 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1371 match *delimiter {
1372 MacroDelimiter::Brace(_) => true,
1373 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001374 }
1375 }
David Tolnayedf2b992016-09-23 20:43:45 -07001376}
David Tolnay4a51dc72016-10-01 00:40:31 -07001377
1378#[cfg(feature = "printing")]
1379mod printing {
1380 use super::*;
1381 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001382 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001383 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001384
David Tolnay1bfa7332017-11-11 12:41:20 -08001385 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001386 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001387 tokens.append_all(self.attrs.outer());
1388 self.vis.to_tokens(tokens);
1389 self.extern_token.to_tokens(tokens);
1390 self.crate_token.to_tokens(tokens);
1391 self.ident.to_tokens(tokens);
1392 if let Some((ref as_token, ref rename)) = self.rename {
1393 as_token.to_tokens(tokens);
1394 rename.to_tokens(tokens);
1395 }
1396 self.semi_token.to_tokens(tokens);
1397 }
1398 }
1399
1400 impl ToTokens for ItemUse {
1401 fn to_tokens(&self, tokens: &mut Tokens) {
1402 tokens.append_all(self.attrs.outer());
1403 self.vis.to_tokens(tokens);
1404 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001405 self.leading_colon.to_tokens(tokens);
1406 self.prefix.to_tokens(tokens);
1407 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001408 self.semi_token.to_tokens(tokens);
1409 }
1410 }
1411
1412 impl ToTokens for ItemStatic {
1413 fn to_tokens(&self, tokens: &mut Tokens) {
1414 tokens.append_all(self.attrs.outer());
1415 self.vis.to_tokens(tokens);
1416 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001417 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001418 self.ident.to_tokens(tokens);
1419 self.colon_token.to_tokens(tokens);
1420 self.ty.to_tokens(tokens);
1421 self.eq_token.to_tokens(tokens);
1422 self.expr.to_tokens(tokens);
1423 self.semi_token.to_tokens(tokens);
1424 }
1425 }
1426
1427 impl ToTokens for ItemConst {
1428 fn to_tokens(&self, tokens: &mut Tokens) {
1429 tokens.append_all(self.attrs.outer());
1430 self.vis.to_tokens(tokens);
1431 self.const_token.to_tokens(tokens);
1432 self.ident.to_tokens(tokens);
1433 self.colon_token.to_tokens(tokens);
1434 self.ty.to_tokens(tokens);
1435 self.eq_token.to_tokens(tokens);
1436 self.expr.to_tokens(tokens);
1437 self.semi_token.to_tokens(tokens);
1438 }
1439 }
1440
1441 impl ToTokens for ItemFn {
1442 fn to_tokens(&self, tokens: &mut Tokens) {
1443 tokens.append_all(self.attrs.outer());
1444 self.vis.to_tokens(tokens);
1445 self.constness.to_tokens(tokens);
1446 self.unsafety.to_tokens(tokens);
1447 self.abi.to_tokens(tokens);
1448 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1449 self.block.brace_token.surround(tokens, |tokens| {
1450 tokens.append_all(self.attrs.inner());
1451 tokens.append_all(&self.block.stmts);
1452 });
1453 }
1454 }
1455
1456 impl ToTokens for ItemMod {
1457 fn to_tokens(&self, tokens: &mut Tokens) {
1458 tokens.append_all(self.attrs.outer());
1459 self.vis.to_tokens(tokens);
1460 self.mod_token.to_tokens(tokens);
1461 self.ident.to_tokens(tokens);
1462 if let Some((ref brace, ref items)) = self.content {
1463 brace.surround(tokens, |tokens| {
1464 tokens.append_all(self.attrs.inner());
1465 tokens.append_all(items);
1466 });
1467 } else {
1468 TokensOrDefault(&self.semi).to_tokens(tokens);
1469 }
1470 }
1471 }
1472
1473 impl ToTokens for ItemForeignMod {
1474 fn to_tokens(&self, tokens: &mut Tokens) {
1475 tokens.append_all(self.attrs.outer());
1476 self.abi.to_tokens(tokens);
1477 self.brace_token.surround(tokens, |tokens| {
1478 tokens.append_all(&self.items);
1479 });
1480 }
1481 }
1482
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001483 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001484 fn to_tokens(&self, tokens: &mut Tokens) {
1485 tokens.append_all(self.attrs.outer());
1486 self.vis.to_tokens(tokens);
1487 self.type_token.to_tokens(tokens);
1488 self.ident.to_tokens(tokens);
1489 self.generics.to_tokens(tokens);
1490 self.generics.where_clause.to_tokens(tokens);
1491 self.eq_token.to_tokens(tokens);
1492 self.ty.to_tokens(tokens);
1493 self.semi_token.to_tokens(tokens);
1494 }
1495 }
1496
1497 impl ToTokens for ItemEnum {
1498 fn to_tokens(&self, tokens: &mut Tokens) {
1499 tokens.append_all(self.attrs.outer());
1500 self.vis.to_tokens(tokens);
1501 self.enum_token.to_tokens(tokens);
1502 self.ident.to_tokens(tokens);
1503 self.generics.to_tokens(tokens);
1504 self.generics.where_clause.to_tokens(tokens);
1505 self.brace_token.surround(tokens, |tokens| {
1506 self.variants.to_tokens(tokens);
1507 });
1508 }
1509 }
1510
1511 impl ToTokens for ItemStruct {
1512 fn to_tokens(&self, tokens: &mut Tokens) {
1513 tokens.append_all(self.attrs.outer());
1514 self.vis.to_tokens(tokens);
1515 self.struct_token.to_tokens(tokens);
1516 self.ident.to_tokens(tokens);
1517 self.generics.to_tokens(tokens);
1518 match self.data {
1519 VariantData::Struct(..) => {
1520 self.generics.where_clause.to_tokens(tokens);
1521 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001522 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001523 VariantData::Tuple(..) => {
1524 self.data.to_tokens(tokens);
1525 self.generics.where_clause.to_tokens(tokens);
1526 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001527 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001528 VariantData::Unit => {
1529 self.generics.where_clause.to_tokens(tokens);
1530 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001531 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001532 }
1533 }
1534 }
1535
1536 impl ToTokens for ItemUnion {
1537 fn to_tokens(&self, tokens: &mut Tokens) {
1538 tokens.append_all(self.attrs.outer());
1539 self.vis.to_tokens(tokens);
1540 self.union_token.to_tokens(tokens);
1541 self.ident.to_tokens(tokens);
1542 self.generics.to_tokens(tokens);
1543 self.generics.where_clause.to_tokens(tokens);
1544 // XXX: Should we handle / complain when using a
1545 // non-VariantData::Struct Variant in Union?
1546 self.data.to_tokens(tokens);
1547 }
1548 }
1549
1550 impl ToTokens for ItemTrait {
1551 fn to_tokens(&self, tokens: &mut Tokens) {
1552 tokens.append_all(self.attrs.outer());
1553 self.vis.to_tokens(tokens);
1554 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001555 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001556 self.trait_token.to_tokens(tokens);
1557 self.ident.to_tokens(tokens);
1558 self.generics.to_tokens(tokens);
1559 if !self.supertraits.is_empty() {
1560 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1561 self.supertraits.to_tokens(tokens);
1562 }
1563 self.generics.where_clause.to_tokens(tokens);
1564 self.brace_token.surround(tokens, |tokens| {
1565 tokens.append_all(&self.items);
1566 });
1567 }
1568 }
1569
David Tolnay1bfa7332017-11-11 12:41:20 -08001570 impl ToTokens for ItemImpl {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 tokens.append_all(self.attrs.outer());
1573 self.defaultness.to_tokens(tokens);
1574 self.unsafety.to_tokens(tokens);
1575 self.impl_token.to_tokens(tokens);
1576 self.generics.to_tokens(tokens);
1577 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1578 polarity.to_tokens(tokens);
1579 path.to_tokens(tokens);
1580 for_token.to_tokens(tokens);
1581 }
1582 self.self_ty.to_tokens(tokens);
1583 self.generics.where_clause.to_tokens(tokens);
1584 self.brace_token.surround(tokens, |tokens| {
1585 tokens.append_all(&self.items);
1586 });
1587 }
1588 }
1589
1590 impl ToTokens for ItemMacro {
1591 fn to_tokens(&self, tokens: &mut Tokens) {
1592 tokens.append_all(self.attrs.outer());
1593 self.mac.path.to_tokens(tokens);
1594 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001595 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001596 match self.mac.delimiter {
1597 MacroDelimiter::Paren(ref paren) => {
1598 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1599 }
1600 MacroDelimiter::Brace(ref brace) => {
1601 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1602 }
1603 MacroDelimiter::Bracket(ref bracket) => {
1604 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1605 }
1606 }
David Tolnay57292da2017-12-27 21:03:33 -05001607 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001608 }
1609 }
David Tolnay42602292016-10-01 22:25:45 -07001610
David Tolnay500d8322017-12-18 00:32:51 -08001611 impl ToTokens for ItemMacro2 {
1612 fn to_tokens(&self, tokens: &mut Tokens) {
1613 tokens.append_all(self.attrs.outer());
1614 self.vis.to_tokens(tokens);
1615 self.macro_token.to_tokens(tokens);
1616 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001617 self.paren_token.surround(tokens, |tokens| {
1618 self.args.to_tokens(tokens);
1619 });
1620 self.brace_token.surround(tokens, |tokens| {
1621 self.body.to_tokens(tokens);
1622 });
David Tolnay500d8322017-12-18 00:32:51 -08001623 }
1624 }
1625
David Tolnay2ae520a2017-12-29 11:19:50 -05001626 impl ToTokens for ItemVerbatim {
1627 fn to_tokens(&self, tokens: &mut Tokens) {
1628 self.tts.to_tokens(tokens);
1629 }
1630 }
1631
David Tolnay5f332a92017-12-26 00:42:45 -05001632 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001634 self.ident.to_tokens(tokens);
1635 if let Some((ref as_token, ref rename)) = self.rename {
1636 as_token.to_tokens(tokens);
1637 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001638 }
David Tolnay4a057422016-10-08 00:02:31 -07001639 }
1640 }
1641
David Tolnay5f332a92017-12-26 00:42:45 -05001642 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001643 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001644 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 }
1646 }
1647
David Tolnay5f332a92017-12-26 00:42:45 -05001648 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001650 self.brace_token.surround(tokens, |tokens| {
1651 self.items.to_tokens(tokens);
1652 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001653 }
1654 }
1655
David Tolnay1bfa7332017-11-11 12:41:20 -08001656 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001657 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001658 tokens.append_all(self.attrs.outer());
1659 self.const_token.to_tokens(tokens);
1660 self.ident.to_tokens(tokens);
1661 self.colon_token.to_tokens(tokens);
1662 self.ty.to_tokens(tokens);
1663 if let Some((ref eq_token, ref default)) = self.default {
1664 eq_token.to_tokens(tokens);
1665 default.to_tokens(tokens);
1666 }
1667 self.semi_token.to_tokens(tokens);
1668 }
1669 }
1670
1671 impl ToTokens for TraitItemMethod {
1672 fn to_tokens(&self, tokens: &mut Tokens) {
1673 tokens.append_all(self.attrs.outer());
1674 self.sig.to_tokens(tokens);
1675 match self.default {
1676 Some(ref block) => {
1677 block.brace_token.surround(tokens, |tokens| {
1678 tokens.append_all(self.attrs.inner());
1679 tokens.append_all(&block.stmts);
1680 });
David Tolnayca085422016-10-04 00:12:38 -07001681 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001682 None => {
1683 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001684 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001685 }
1686 }
1687 }
1688
1689 impl ToTokens for TraitItemType {
1690 fn to_tokens(&self, tokens: &mut Tokens) {
1691 tokens.append_all(self.attrs.outer());
1692 self.type_token.to_tokens(tokens);
1693 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001694 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001695 if !self.bounds.is_empty() {
1696 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1697 self.bounds.to_tokens(tokens);
1698 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001699 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001700 if let Some((ref eq_token, ref default)) = self.default {
1701 eq_token.to_tokens(tokens);
1702 default.to_tokens(tokens);
1703 }
1704 self.semi_token.to_tokens(tokens);
1705 }
1706 }
1707
1708 impl ToTokens for TraitItemMacro {
1709 fn to_tokens(&self, tokens: &mut Tokens) {
1710 tokens.append_all(self.attrs.outer());
1711 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001712 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001713 }
1714 }
1715
David Tolnay2ae520a2017-12-29 11:19:50 -05001716 impl ToTokens for TraitItemVerbatim {
1717 fn to_tokens(&self, tokens: &mut Tokens) {
1718 self.tts.to_tokens(tokens);
1719 }
1720 }
1721
David Tolnay857628c2017-11-11 12:25:31 -08001722 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001723 fn to_tokens(&self, tokens: &mut Tokens) {
1724 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001725 self.vis.to_tokens(tokens);
1726 self.defaultness.to_tokens(tokens);
1727 self.const_token.to_tokens(tokens);
1728 self.ident.to_tokens(tokens);
1729 self.colon_token.to_tokens(tokens);
1730 self.ty.to_tokens(tokens);
1731 self.eq_token.to_tokens(tokens);
1732 self.expr.to_tokens(tokens);
1733 self.semi_token.to_tokens(tokens);
1734 }
1735 }
1736
1737 impl ToTokens for ImplItemMethod {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
1739 tokens.append_all(self.attrs.outer());
1740 self.vis.to_tokens(tokens);
1741 self.defaultness.to_tokens(tokens);
1742 self.sig.to_tokens(tokens);
1743 self.block.brace_token.surround(tokens, |tokens| {
1744 tokens.append_all(self.attrs.inner());
1745 tokens.append_all(&self.block.stmts);
1746 });
1747 }
1748 }
1749
1750 impl ToTokens for ImplItemType {
1751 fn to_tokens(&self, tokens: &mut Tokens) {
1752 tokens.append_all(self.attrs.outer());
1753 self.vis.to_tokens(tokens);
1754 self.defaultness.to_tokens(tokens);
1755 self.type_token.to_tokens(tokens);
1756 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001757 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001758 self.eq_token.to_tokens(tokens);
1759 self.ty.to_tokens(tokens);
1760 self.semi_token.to_tokens(tokens);
1761 }
1762 }
1763
1764 impl ToTokens for ImplItemMacro {
1765 fn to_tokens(&self, tokens: &mut Tokens) {
1766 tokens.append_all(self.attrs.outer());
1767 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001768 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001769 }
1770 }
1771
David Tolnay2ae520a2017-12-29 11:19:50 -05001772 impl ToTokens for ImplItemVerbatim {
1773 fn to_tokens(&self, tokens: &mut Tokens) {
1774 self.tts.to_tokens(tokens);
1775 }
1776 }
1777
David Tolnay8894f602017-11-11 12:11:04 -08001778 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001779 fn to_tokens(&self, tokens: &mut Tokens) {
1780 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001781 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001782 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1783 self.semi_token.to_tokens(tokens);
1784 }
1785 }
1786
1787 impl ToTokens for ForeignItemStatic {
1788 fn to_tokens(&self, tokens: &mut Tokens) {
1789 tokens.append_all(self.attrs.outer());
1790 self.vis.to_tokens(tokens);
1791 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001792 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001793 self.ident.to_tokens(tokens);
1794 self.colon_token.to_tokens(tokens);
1795 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001796 self.semi_token.to_tokens(tokens);
1797 }
1798 }
1799
David Tolnay199bcbb2017-11-12 10:33:52 -08001800 impl ToTokens for ForeignItemType {
1801 fn to_tokens(&self, tokens: &mut Tokens) {
1802 tokens.append_all(self.attrs.outer());
1803 self.vis.to_tokens(tokens);
1804 self.type_token.to_tokens(tokens);
1805 self.ident.to_tokens(tokens);
1806 self.semi_token.to_tokens(tokens);
1807 }
1808 }
1809
David Tolnay2ae520a2017-12-29 11:19:50 -05001810 impl ToTokens for ForeignItemVerbatim {
1811 fn to_tokens(&self, tokens: &mut Tokens) {
1812 self.tts.to_tokens(tokens);
1813 }
1814 }
1815
David Tolnay570695e2017-06-03 16:15:13 -07001816 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001818 self.constness.to_tokens(tokens);
1819 self.unsafety.to_tokens(tokens);
1820 self.abi.to_tokens(tokens);
1821 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001822 }
1823 }
1824
David Tolnay570695e2017-06-03 16:15:13 -07001825 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001826
1827 impl<'a> ToTokens for NamedDecl<'a> {
1828 fn to_tokens(&self, tokens: &mut Tokens) {
1829 self.0.fn_token.to_tokens(tokens);
1830 self.1.to_tokens(tokens);
1831 self.0.generics.to_tokens(tokens);
1832 self.0.paren_token.surround(tokens, |tokens| {
1833 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001834 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1835 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001836 }
David Tolnayd2836e22017-12-27 23:13:00 -05001837 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001838 });
1839 self.0.output.to_tokens(tokens);
1840 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001841 }
1842 }
1843
Alex Crichton62a0a592017-05-22 13:58:53 -07001844 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001845 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001846 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001847 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001848 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001849 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001850 }
1851 }
1852
1853 impl ToTokens for ArgSelf {
1854 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001855 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001856 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001857 }
1858 }
1859
1860 impl ToTokens for ArgCaptured {
1861 fn to_tokens(&self, tokens: &mut Tokens) {
1862 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001863 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001864 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001865 }
1866 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001867}