blob: 59694bbccd7aed69e6ce98bf875c1840a4cdd9ee [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 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 /// An implementation.
164 ///
165 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
166 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800167 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500168 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500169 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800170 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700172 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500173 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700174 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800175 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500176 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 pub items: Vec<ImplItem>,
178 }),
179 /// A macro invocation (which includes macro definition).
180 ///
181 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800182 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800183 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800184 /// The `example` in `macro_rules! example { ... }`.
185 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800186 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500187 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800188 }),
David Tolnay500d8322017-12-18 00:32:51 -0800189 /// FIXME will need to revisit what this looks like in the AST.
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,
195 pub args: TokenTree,
196 pub body: TokenTree,
197 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500198 pub Verbatim(ItemVerbatim #manual_extra_traits {
199 pub tts: TokenStream,
200 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700201 }
David Tolnayb79ee962016-09-04 09:39:20 -0700202}
203
David Tolnay9c76bcb2017-12-26 23:14:59 -0500204#[cfg(feature = "extra-traits")]
205impl Eq for ItemMacro2 {}
206
207#[cfg(feature = "extra-traits")]
208impl PartialEq for ItemMacro2 {
209 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500210 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnay9c76bcb2017-12-26 23:14:59 -0500211 && self.ident == other.ident
212 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
213 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
214 }
215}
216
217#[cfg(feature = "extra-traits")]
218impl Hash for ItemMacro2 {
219 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500220 where
221 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500222 {
223 self.attrs.hash(state);
224 self.vis.hash(state);
225 self.macro_token.hash(state);
226 self.ident.hash(state);
227 TokenTreeHelper(&self.args).hash(state);
228 TokenTreeHelper(&self.body).hash(state);
229 }
230}
231
David Tolnay2ae520a2017-12-29 11:19:50 -0500232#[cfg(feature = "extra-traits")]
233impl Eq for ItemVerbatim {}
234
235#[cfg(feature = "extra-traits")]
236impl PartialEq for ItemVerbatim {
237 fn eq(&self, other: &Self) -> bool {
238 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
239 }
240}
241
242#[cfg(feature = "extra-traits")]
243impl Hash for ItemVerbatim {
244 fn hash<H>(&self, state: &mut H)
245 where
246 H: Hasher,
247 {
248 TokenStreamHelper(&self.tts).hash(state);
249 }
250}
251
David Tolnay0e837402016-12-22 17:25:55 -0500252impl From<DeriveInput> for Item {
253 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800254 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500255 Body::Enum(data) => Item::Enum(ItemEnum {
256 attrs: input.attrs,
257 vis: input.vis,
258 enum_token: data.enum_token,
259 ident: input.ident,
260 generics: input.generics,
261 brace_token: data.brace_token,
262 variants: data.variants,
263 }),
264 Body::Struct(data) => Item::Struct(ItemStruct {
265 attrs: input.attrs,
266 vis: input.vis,
267 struct_token: data.struct_token,
268 ident: input.ident,
269 generics: input.generics,
270 data: data.data,
271 semi_token: data.semi_token,
272 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700273 }
274 }
275}
276
Alex Crichton62a0a592017-05-22 13:58:53 -0700277ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500278 /// Things that can appear directly inside of a module.
279 pub enum UseTree {
280 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
281 pub Path(UsePath {
282 pub ident: Ident,
283 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500285 /// `use prefix::*`
286 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800287 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500289 /// `use prefix::{a, b, c}`
290 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500291 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500292 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 }),
294 }
295}
296
Alex Crichton62a0a592017-05-22 13:58:53 -0700297ast_enum_of_structs! {
298 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800299 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 /// A foreign function
301 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800302 pub attrs: Vec<Attribute>,
303 pub vis: Visibility,
304 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800306 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 }),
308 /// A foreign static item (`static ext: u8`)
309 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800310 pub attrs: Vec<Attribute>,
311 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800312 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500313 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800314 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800315 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800316 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800317 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800319 /// A foreign type
320 pub Type(ForeignItemType {
321 pub attrs: Vec<Attribute>,
322 pub vis: Visibility,
323 pub type_token: Token![type],
324 pub ident: Ident,
325 pub semi_token: Token![;],
326 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500327 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
328 pub tts: TokenStream,
329 }),
330 }
331}
332
333#[cfg(feature = "extra-traits")]
334impl Eq for ForeignItemVerbatim {}
335
336#[cfg(feature = "extra-traits")]
337impl PartialEq for ForeignItemVerbatim {
338 fn eq(&self, other: &Self) -> bool {
339 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
340 }
341}
342
343#[cfg(feature = "extra-traits")]
344impl Hash for ForeignItemVerbatim {
345 fn hash<H>(&self, state: &mut H)
346 where
347 H: Hasher,
348 {
349 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700351}
352
David Tolnayda705bd2017-11-10 21:58:05 -0800353ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 /// Represents an item declaration within a trait declaration,
355 /// possibly including a default implementation. A trait item is
356 /// either required (meaning it doesn't have an implementation, just a
357 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800358 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800360 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800361 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700362 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800363 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800364 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800365 pub default: Option<(Token![=], Expr)>,
366 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700367 }),
368 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800369 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 pub sig: MethodSig,
371 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800372 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
374 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800375 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800376 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700377 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500378 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800379 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800380 pub bounds: Delimited<TypeParamBound, Token![+]>,
381 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800382 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800384 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800385 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800386 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500387 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800388 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500389 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
390 pub tts: TokenStream,
391 }),
392 }
393}
394
395#[cfg(feature = "extra-traits")]
396impl Eq for TraitItemVerbatim {}
397
398#[cfg(feature = "extra-traits")]
399impl PartialEq for TraitItemVerbatim {
400 fn eq(&self, other: &Self) -> bool {
401 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
402 }
403}
404
405#[cfg(feature = "extra-traits")]
406impl Hash for TraitItemVerbatim {
407 fn hash<H>(&self, state: &mut H)
408 where
409 H: Hasher,
410 {
411 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700413}
414
Alex Crichton62a0a592017-05-22 13:58:53 -0700415ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800416 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800418 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700419 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500420 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800421 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700422 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800423 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800424 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800425 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800427 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700428 }),
429 pub Method(ImplItemMethod {
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]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 pub sig: MethodSig,
434 pub block: Block,
435 }),
436 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800437 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700438 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500439 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700441 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500442 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800444 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800445 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }),
David Tolnay857628c2017-11-11 12:25:31 -0800447 pub Macro(ImplItemMacro {
448 pub attrs: Vec<Attribute>,
449 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500450 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800451 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500452 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
453 pub tts: TokenStream,
454 }),
455 }
456}
457
458#[cfg(feature = "extra-traits")]
459impl Eq for ImplItemVerbatim {}
460
461#[cfg(feature = "extra-traits")]
462impl PartialEq for ImplItemVerbatim {
463 fn eq(&self, other: &Self) -> bool {
464 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
465 }
466}
467
468#[cfg(feature = "extra-traits")]
469impl Hash for ImplItemVerbatim {
470 fn hash<H>(&self, state: &mut H)
471 where
472 H: Hasher,
473 {
474 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700475 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700476}
477
478ast_struct! {
479 /// Represents a method's signature in a trait declaration,
480 /// or in an implementation.
481 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500482 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500483 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700485 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700486 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 }
488}
489
490ast_struct! {
491 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700492 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700493 /// E.g. `fn foo(bar: baz)`
494 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800495 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500496 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500497 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800498 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500499 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500500 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700501 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700502}
503
Alex Crichton62a0a592017-05-22 13:58:53 -0700504ast_enum_of_structs! {
505 /// An argument in a function header.
506 ///
507 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
508 pub enum FnArg {
509 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500512 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500513 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 }),
515 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500516 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 }),
519 pub Captured(ArgCaptured {
520 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800521 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800522 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500524 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800525 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700526 }
David Tolnay62f374c2016-10-02 13:37:00 -0700527}
528
David Tolnayedf2b992016-09-23 20:43:45 -0700529#[cfg(feature = "parsing")]
530pub mod parsing {
531 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700532
David Tolnay03342952017-12-29 11:52:00 -0500533 use synom::{Synom, Cursor, PResult};
David Tolnay57292da2017-12-27 21:03:33 -0500534 use proc_macro2::{TokenNode, Delimiter};
David Tolnay84aa0752016-10-02 23:01:13 -0700535
David Tolnay4c614be2017-11-10 00:02:38 -0800536 impl_synom!(Item "item" alt!(
537 syn!(ItemExternCrate) => { Item::ExternCrate }
538 |
539 syn!(ItemUse) => { Item::Use }
540 |
541 syn!(ItemStatic) => { Item::Static }
542 |
543 syn!(ItemConst) => { Item::Const }
544 |
545 syn!(ItemFn) => { Item::Fn }
546 |
547 syn!(ItemMod) => { Item::Mod }
548 |
549 syn!(ItemForeignMod) => { Item::ForeignMod }
550 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800551 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800552 |
553 syn!(ItemStruct) => { Item::Struct }
554 |
555 syn!(ItemEnum) => { Item::Enum }
556 |
557 syn!(ItemUnion) => { Item::Union }
558 |
559 syn!(ItemTrait) => { Item::Trait }
560 |
David Tolnay03342952017-12-29 11:52:00 -0500561 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800562 |
563 syn!(ItemImpl) => { Item::Impl }
564 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800565 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800566 |
567 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800568 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700569
David Tolnaydecf28d2017-11-11 11:56:45 -0800570 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500571 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700572 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800573 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700574 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500575 body: call!(tt::delimited) >>
David Tolnay57292da2017-12-27 21:03:33 -0500576 semi: cond!(!is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800577 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700578 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800579 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800580 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500581 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700582 bang_token: bang,
David Tolnayfe9d2782017-12-29 11:32:42 -0500583 tt: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800584 },
David Tolnay57292da2017-12-27 21:03:33 -0500585 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800586 })
David Tolnayedf2b992016-09-23 20:43:45 -0700587 ));
588
David Tolnay500d8322017-12-18 00:32:51 -0800589 // TODO: figure out the actual grammar; is body required to be braced?
590 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500591 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800592 vis: syn!(Visibility) >>
593 macro_: keyword!(macro) >>
594 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500595 args: call!(tt::parenthesized) >>
596 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800597 (ItemMacro2 {
598 attrs: attrs,
599 vis: vis,
600 macro_token: macro_,
601 ident: ident,
602 args: args,
603 body: body,
604 })
605 ));
606
David Tolnay4c614be2017-11-10 00:02:38 -0800607 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500608 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700609 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800610 extern_: keyword!(extern) >>
611 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700612 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800613 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
614 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800615 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700616 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800617 vis: vis,
618 extern_token: extern_,
619 crate_token: crate_,
620 ident: ident,
621 rename: rename,
622 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800623 })
David Tolnayedf2b992016-09-23 20:43:45 -0700624 ));
625
David Tolnay4c614be2017-11-10 00:02:38 -0800626 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500627 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700628 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800629 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500630 leading_colon: option!(punct!(::)) >>
631 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500632 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500633 true => syn!(UseTree)
634 |
David Tolnay8edcef12017-12-28 12:06:52 -0500635 false => alt!(
636 tuple!(keyword!(as), syn!(Ident)) => {
637 |rename| UseTree::Path(UsePath {
638 ident: prefix.pop().unwrap().into_item(),
639 rename: Some(rename),
640 })
641 }
David Tolnay5f332a92017-12-26 00:42:45 -0500642 |
David Tolnay8edcef12017-12-28 12:06:52 -0500643 epsilon!() => {
644 |_| UseTree::Path(UsePath {
645 ident: prefix.pop().unwrap().into_item(),
646 rename: None,
647 })
648 }
David Tolnay5f332a92017-12-26 00:42:45 -0500649 )
650 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800651 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800652 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700653 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800654 vis: vis,
655 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500656 leading_colon: leading_colon,
657 prefix: prefix,
658 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800659 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800660 })
David Tolnay4a057422016-10-08 00:02:31 -0700661 ));
662
David Tolnay5f332a92017-12-26 00:42:45 -0500663 named!(use_prefix -> Ident, alt!(
664 syn!(Ident)
665 |
666 keyword!(self) => { Into::into }
667 |
668 keyword!(super) => { Into::into }
669 |
670 keyword!(crate) => { Into::into }
671 ));
672
673 impl_synom!(UseTree "use tree" alt!(
674 syn!(UsePath) => { UseTree::Path }
675 |
676 syn!(UseGlob) => { UseTree::Glob }
677 |
678 syn!(UseList) => { UseTree::List }
679 ));
680
681 impl_synom!(UsePath "use path" do_parse!(
682 ident: alt!(
683 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400684 |
David Tolnay5f332a92017-12-26 00:42:45 -0500685 keyword!(self) => { Into::into }
686 ) >>
687 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
688 (UsePath {
689 ident: ident,
690 rename: rename,
691 })
692 ));
David Tolnay4a057422016-10-08 00:02:31 -0700693
David Tolnay5f332a92017-12-26 00:42:45 -0500694 impl_synom!(UseGlob "use glob" do_parse!(
695 star: punct!(*) >>
696 (UseGlob {
697 star_token: star,
698 })
699 ));
David Tolnay4a057422016-10-08 00:02:31 -0700700
David Tolnay5f332a92017-12-26 00:42:45 -0500701 impl_synom!(UseList "use list" do_parse!(
702 list: braces!(Delimited::parse_terminated) >>
703 (UseList {
704 brace_token: list.1,
705 items: list.0,
706 })
707 ));
David Tolnay4a057422016-10-08 00:02:31 -0700708
David Tolnay4c614be2017-11-10 00:02:38 -0800709 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500710 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700711 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800712 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500713 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700714 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800715 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800716 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800717 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700718 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800719 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800720 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700721 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800722 vis: vis,
723 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500724 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800725 ident: ident,
726 colon_token: colon,
727 ty: Box::new(ty),
728 eq_token: eq,
729 expr: Box::new(value),
730 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800731 })
David Tolnay47a877c2016-10-01 16:50:55 -0700732 ));
733
David Tolnay4c614be2017-11-10 00:02:38 -0800734 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500735 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700736 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800737 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700738 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800739 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800740 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800741 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700742 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800743 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800744 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700745 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800746 vis: vis,
747 const_token: const_,
748 ident: ident,
749 colon_token: colon,
750 ty: Box::new(ty),
751 eq_token: eq,
752 expr: Box::new(value),
753 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800754 })
David Tolnay47a877c2016-10-01 16:50:55 -0700755 ));
756
David Tolnay4c614be2017-11-10 00:02:38 -0800757 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500758 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700759 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500760 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500761 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700762 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800763 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700764 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700765 generics: syn!(Generics) >>
766 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800767 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500768 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700769 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500770 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700771 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400772 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800773 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700774 attrs: {
775 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700776 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700777 attrs
778 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800779 vis: vis,
780 constness: constness,
781 unsafety: unsafety,
782 abi: abi,
783 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800784 fn_token: fn_,
785 paren_token: inputs.1,
786 inputs: inputs.0,
787 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500788 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800789 generics: Generics {
790 where_clause: where_clause,
791 .. generics
792 },
793 }),
794 ident: ident,
795 block: Box::new(Block {
796 brace_token: inner_attrs_stmts.1,
797 stmts: (inner_attrs_stmts.0).1,
798 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800799 })
David Tolnay42602292016-10-01 22:25:45 -0700800 ));
801
Alex Crichton954046c2017-05-30 21:49:42 -0700802 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400803 named!(parse -> Self, alt!(
804 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800805 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400806 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500807 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800808 self_: keyword!(self) >>
809 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400810 (ArgSelfRef {
811 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500812 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400813 and_token: and,
814 self_token: self_,
815 }.into())
816 )
817 |
818 do_parse!(
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 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500823 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400824 self_token: self_,
825 }.into())
826 )
827 |
828 do_parse!(
829 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800830 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800831 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400832 (ArgCaptured {
833 pat: pat,
834 ty: ty,
835 colon_token: colon,
836 }.into())
837 )
838 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800839 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400840 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700841 }
David Tolnay62f374c2016-10-02 13:37:00 -0700842
David Tolnay4c614be2017-11-10 00:02:38 -0800843 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500844 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700845 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800846 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700847 ident: syn!(Ident) >>
848 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800849 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700850 Vec::new(),
851 None,
852 Some(semi),
853 )}
David Tolnay37d10332016-10-13 20:51:04 -0700854 |
Alex Crichton954046c2017-05-30 21:49:42 -0700855 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700856 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500857 many0!(Attribute::parse_inner),
858 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400859 )
David Tolnay570695e2017-06-03 16:15:13 -0700860 ) => {|((inner_attrs, items), brace)| (
861 inner_attrs,
862 Some((brace, items)),
863 None,
864 )}
David Tolnay37d10332016-10-13 20:51:04 -0700865 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800866 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700867 attrs: {
868 let mut attrs = outer_attrs;
869 attrs.extend(content_semi.0);
870 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700871 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800872 vis: vis,
873 mod_token: mod_,
874 ident: ident,
875 content: content_semi.1,
876 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800877 })
David Tolnay35902302016-10-06 01:11:08 -0700878 ));
879
David Tolnay4c614be2017-11-10 00:02:38 -0800880 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500881 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700882 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500883 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800884 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700885 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800886 abi: abi,
887 brace_token: items.1,
888 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800889 })
David Tolnay35902302016-10-06 01:11:08 -0700890 ));
891
David Tolnay8894f602017-11-11 12:11:04 -0800892 impl_synom!(ForeignItem "foreign item" alt!(
893 syn!(ForeignItemFn) => { ForeignItem::Fn }
894 |
895 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800896 |
897 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800898 ));
David Tolnay35902302016-10-06 01:11:08 -0700899
David Tolnay8894f602017-11-11 12:11:04 -0800900 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500901 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700902 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700904 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700905 generics: syn!(Generics) >>
906 inputs: parens!(do_parse!(
907 args: call!(Delimited::parse_terminated) >>
908 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800909 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700910 (args, variadic)
911 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800912 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500913 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700915 ({
916 let ((inputs, variadic), parens) = inputs;
917 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800918 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700919 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700920 attrs: attrs,
921 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800922 decl: Box::new(FnDecl {
923 fn_token: fn_,
924 paren_token: parens,
925 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500926 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800927 output: ret,
928 generics: Generics {
929 where_clause: where_clause,
930 .. generics
931 },
932 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700933 vis: vis,
934 }
David Tolnay35902302016-10-06 01:11:08 -0700935 })
936 ));
937
David Tolnay8894f602017-11-11 12:11:04 -0800938 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500939 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700940 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800941 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500942 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700943 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800944 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800945 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800946 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800947 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700948 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700949 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700950 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800951 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500952 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800953 static_token: static_,
954 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700955 vis: vis,
956 })
957 ));
958
David Tolnay199bcbb2017-11-12 10:33:52 -0800959 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500960 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800961 vis: syn!(Visibility) >>
962 type_: keyword!(type) >>
963 ident: syn!(Ident) >>
964 semi: punct!(;) >>
965 (ForeignItemType {
966 attrs: attrs,
967 vis: vis,
968 type_token: type_,
969 ident: ident,
970 semi_token: semi,
971 })
972 ));
973
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800974 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500975 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700976 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800977 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700978 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700979 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500980 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800981 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800982 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800983 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800984 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700985 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800986 vis: vis,
987 type_token: type_,
988 ident: ident,
989 generics: Generics {
990 where_clause: where_clause,
991 ..generics
992 },
993 eq_token: eq,
994 ty: Box::new(ty),
995 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800996 })
David Tolnay3cf52982016-10-01 17:11:37 -0700997 ));
998
David Tolnay4c614be2017-11-10 00:02:38 -0800999 impl_synom!(ItemStruct "struct item" switch!(
1000 map!(syn!(DeriveInput), Into::into),
1001 Item::Struct(item) => value!(item)
1002 |
1003 _ => reject!()
1004 ));
David Tolnay42602292016-10-01 22:25:45 -07001005
David Tolnay4c614be2017-11-10 00:02:38 -08001006 impl_synom!(ItemEnum "enum item" switch!(
1007 map!(syn!(DeriveInput), Into::into),
1008 Item::Enum(item) => value!(item)
1009 |
1010 _ => reject!()
1011 ));
1012
1013 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001014 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001015 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001016 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001017 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001018 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001019 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001020 fields: braces!(call!(Delimited::parse_terminated_with,
1021 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001022 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001023 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001024 vis: vis,
1025 union_token: union_,
1026 ident: ident,
1027 generics: Generics {
1028 where_clause: where_clause,
1029 .. generics
1030 },
1031 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -08001032 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001033 ));
1034
David Tolnay4c614be2017-11-10 00:02:38 -08001035 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001036 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001037 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001038 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001039 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001040 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001041 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001042 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001043 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001044 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001045 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001046 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001047 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001048 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001049 vis: vis,
1050 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001051 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001052 trait_token: trait_,
1053 ident: ident,
1054 generics: Generics {
1055 where_clause: where_clause,
1056 .. generics
1057 },
1058 colon_token: colon,
1059 supertraits: bounds.unwrap_or_default(),
1060 brace_token: body.1,
1061 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001062 })
David Tolnay0aecb732016-10-03 23:03:50 -07001063 ));
1064
David Tolnay03342952017-12-29 11:52:00 -05001065 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1066 Ok((cursor, cursor))
1067 }
1068
1069 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1070 begin: call!(grab_cursor) >>
1071 many0!(Attribute::parse_outer) >>
1072 option!(keyword!(unsafe)) >>
1073 keyword!(impl) >>
1074 syn!(Path) >>
1075 keyword!(for) >>
1076 punct!(..) >>
1077 braces!(epsilon!()) >>
1078 end: call!(grab_cursor) >>
1079 ({
1080 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1081 let len = tts.len() - end.token_stream().into_iter().count();
1082 ItemVerbatim {
1083 tts: tts.into_iter().take(len).collect(),
1084 }
David Tolnay4c614be2017-11-10 00:02:38 -08001085 })
David Tolnayf94e2362016-10-04 00:29:51 -07001086 ));
1087
David Tolnayda705bd2017-11-10 21:58:05 -08001088 impl_synom!(TraitItem "trait item" alt!(
1089 syn!(TraitItemConst) => { TraitItem::Const }
1090 |
1091 syn!(TraitItemMethod) => { TraitItem::Method }
1092 |
1093 syn!(TraitItemType) => { TraitItem::Type }
1094 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001095 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001096 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001097
David Tolnayda705bd2017-11-10 21:58:05 -08001098 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001099 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001100 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001101 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001102 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001103 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001104 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1105 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001106 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001107 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001108 const_token: const_,
1109 ident: ident,
1110 colon_token: colon,
1111 ty: ty,
1112 default: default,
1113 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001114 })
1115 ));
1116
David Tolnayda705bd2017-11-10 21:58:05 -08001117 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001118 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001119 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001120 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001121 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001122 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001123 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001124 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001125 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001126 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001127 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001128 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001129 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001130 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001131 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001132 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001133 ({
1134 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001135 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001136 None => (Vec::new(), None),
1137 };
David Tolnayda705bd2017-11-10 21:58:05 -08001138 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001139 attrs: {
1140 let mut attrs = outer_attrs;
1141 attrs.extend(inner_attrs);
1142 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001143 },
David Tolnayda705bd2017-11-10 21:58:05 -08001144 sig: MethodSig {
1145 constness: constness,
1146 unsafety: unsafety,
1147 abi: abi,
1148 ident: ident,
1149 decl: FnDecl {
1150 inputs: inputs.0,
1151 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001152 fn_token: fn_,
1153 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001154 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001155 generics: Generics {
1156 where_clause: where_clause,
1157 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001158 },
1159 },
David Tolnayda705bd2017-11-10 21:58:05 -08001160 },
1161 default: stmts.map(|stmts| {
1162 Block {
1163 stmts: stmts.0,
1164 brace_token: stmts.1,
1165 }
1166 }),
1167 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001168 }
David Tolnay0aecb732016-10-03 23:03:50 -07001169 })
1170 ));
1171
David Tolnayda705bd2017-11-10 21:58:05 -08001172 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001173 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001174 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001175 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001176 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001177 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001178 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001179 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001180 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001181 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001182 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001183 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001184 type_token: type_,
1185 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001186 generics: Generics {
1187 where_clause: where_clause,
1188 .. generics
1189 },
David Tolnayda705bd2017-11-10 21:58:05 -08001190 colon_token: colon,
1191 bounds: bounds.unwrap_or_default(),
1192 default: default,
1193 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001194 })
1195 ));
1196
David Tolnaydecf28d2017-11-11 11:56:45 -08001197 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001198 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001199 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001200 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001201 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001202 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001203 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001204 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001205 })
1206 ));
1207
David Tolnay4c614be2017-11-10 00:02:38 -08001208 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001209 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001210 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001211 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001212 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001213 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001214 polarity_path: alt!(
1215 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001216 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001217 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001218 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001219 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001220 )
1221 |
David Tolnay570695e2017-06-03 16:15:13 -07001222 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001223 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001224 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001225 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001226 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001227 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001228 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001229 defaultness: defaultness,
1230 unsafety: unsafety,
1231 impl_token: impl_,
1232 generics: Generics {
1233 where_clause: where_clause,
1234 .. generics
1235 },
1236 trait_: polarity_path,
1237 self_ty: Box::new(self_ty),
1238 brace_token: body.1,
1239 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001240 })
David Tolnay4c9be372016-10-06 00:47:37 -07001241 ));
1242
David Tolnay857628c2017-11-11 12:25:31 -08001243 impl_synom!(ImplItem "item in impl block" alt!(
1244 syn!(ImplItemConst) => { ImplItem::Const }
1245 |
1246 syn!(ImplItemMethod) => { ImplItem::Method }
1247 |
1248 syn!(ImplItemType) => { ImplItem::Type }
1249 |
1250 syn!(ImplItemMacro) => { ImplItem::Macro }
1251 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001252
David Tolnay857628c2017-11-11 12:25:31 -08001253 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001254 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001255 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001256 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001258 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001259 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001260 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001262 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001264 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001265 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001266 vis: vis,
1267 defaultness: defaultness,
1268 const_token: const_,
1269 ident: ident,
1270 colon_token: colon,
1271 ty: ty,
1272 eq_token: eq,
1273 expr: value,
1274 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001275 })
1276 ));
1277
David Tolnay857628c2017-11-11 12:25:31 -08001278 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001279 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001280 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001281 defaultness: option!(keyword!(default)) >>
1282 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001283 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001284 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001285 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001286 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001287 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001288 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001289 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001290 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001291 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001292 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001293 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001294 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001295 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001296 attrs: {
1297 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001298 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001299 attrs
1300 },
David Tolnay857628c2017-11-11 12:25:31 -08001301 vis: vis,
1302 defaultness: defaultness,
1303 sig: MethodSig {
1304 constness: constness,
1305 unsafety: unsafety,
1306 abi: abi,
1307 ident: ident,
1308 decl: FnDecl {
1309 fn_token: fn_,
1310 paren_token: inputs.1,
1311 inputs: inputs.0,
1312 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001313 generics: Generics {
1314 where_clause: where_clause,
1315 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001316 },
David Tolnayd2836e22017-12-27 23:13:00 -05001317 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001318 },
David Tolnay857628c2017-11-11 12:25:31 -08001319 },
1320 block: Block {
1321 brace_token: inner_attrs_stmts.1,
1322 stmts: (inner_attrs_stmts.0).1,
1323 },
David Tolnay4c9be372016-10-06 00:47:37 -07001324 })
1325 ));
1326
David Tolnay857628c2017-11-11 12:25:31 -08001327 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001328 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001329 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001330 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001331 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001332 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001333 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001335 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001336 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001337 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001338 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001339 vis: vis,
1340 defaultness: defaultness,
1341 type_token: type_,
1342 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001343 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001344 eq_token: eq,
1345 ty: ty,
1346 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001347 })
1348 ));
1349
David Tolnay857628c2017-11-11 12:25:31 -08001350 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001351 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001352 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001353 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001354 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001355 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001356 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001357 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001358 })
1359 ));
1360
David Tolnay57292da2017-12-27 21:03:33 -05001361 fn is_braced(tt: &TokenTree) -> bool {
1362 match tt.kind {
1363 TokenNode::Group(Delimiter::Brace, _) => true,
1364 _ => false,
1365 }
1366 }
David Tolnayedf2b992016-09-23 20:43:45 -07001367}
David Tolnay4a51dc72016-10-01 00:40:31 -07001368
1369#[cfg(feature = "printing")]
1370mod printing {
1371 use super::*;
1372 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001373 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001374 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001375
David Tolnay1bfa7332017-11-11 12:41:20 -08001376 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001377 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001378 tokens.append_all(self.attrs.outer());
1379 self.vis.to_tokens(tokens);
1380 self.extern_token.to_tokens(tokens);
1381 self.crate_token.to_tokens(tokens);
1382 self.ident.to_tokens(tokens);
1383 if let Some((ref as_token, ref rename)) = self.rename {
1384 as_token.to_tokens(tokens);
1385 rename.to_tokens(tokens);
1386 }
1387 self.semi_token.to_tokens(tokens);
1388 }
1389 }
1390
1391 impl ToTokens for ItemUse {
1392 fn to_tokens(&self, tokens: &mut Tokens) {
1393 tokens.append_all(self.attrs.outer());
1394 self.vis.to_tokens(tokens);
1395 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001396 self.leading_colon.to_tokens(tokens);
1397 self.prefix.to_tokens(tokens);
1398 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001399 self.semi_token.to_tokens(tokens);
1400 }
1401 }
1402
1403 impl ToTokens for ItemStatic {
1404 fn to_tokens(&self, tokens: &mut Tokens) {
1405 tokens.append_all(self.attrs.outer());
1406 self.vis.to_tokens(tokens);
1407 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001408 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001409 self.ident.to_tokens(tokens);
1410 self.colon_token.to_tokens(tokens);
1411 self.ty.to_tokens(tokens);
1412 self.eq_token.to_tokens(tokens);
1413 self.expr.to_tokens(tokens);
1414 self.semi_token.to_tokens(tokens);
1415 }
1416 }
1417
1418 impl ToTokens for ItemConst {
1419 fn to_tokens(&self, tokens: &mut Tokens) {
1420 tokens.append_all(self.attrs.outer());
1421 self.vis.to_tokens(tokens);
1422 self.const_token.to_tokens(tokens);
1423 self.ident.to_tokens(tokens);
1424 self.colon_token.to_tokens(tokens);
1425 self.ty.to_tokens(tokens);
1426 self.eq_token.to_tokens(tokens);
1427 self.expr.to_tokens(tokens);
1428 self.semi_token.to_tokens(tokens);
1429 }
1430 }
1431
1432 impl ToTokens for ItemFn {
1433 fn to_tokens(&self, tokens: &mut Tokens) {
1434 tokens.append_all(self.attrs.outer());
1435 self.vis.to_tokens(tokens);
1436 self.constness.to_tokens(tokens);
1437 self.unsafety.to_tokens(tokens);
1438 self.abi.to_tokens(tokens);
1439 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1440 self.block.brace_token.surround(tokens, |tokens| {
1441 tokens.append_all(self.attrs.inner());
1442 tokens.append_all(&self.block.stmts);
1443 });
1444 }
1445 }
1446
1447 impl ToTokens for ItemMod {
1448 fn to_tokens(&self, tokens: &mut Tokens) {
1449 tokens.append_all(self.attrs.outer());
1450 self.vis.to_tokens(tokens);
1451 self.mod_token.to_tokens(tokens);
1452 self.ident.to_tokens(tokens);
1453 if let Some((ref brace, ref items)) = self.content {
1454 brace.surround(tokens, |tokens| {
1455 tokens.append_all(self.attrs.inner());
1456 tokens.append_all(items);
1457 });
1458 } else {
1459 TokensOrDefault(&self.semi).to_tokens(tokens);
1460 }
1461 }
1462 }
1463
1464 impl ToTokens for ItemForeignMod {
1465 fn to_tokens(&self, tokens: &mut Tokens) {
1466 tokens.append_all(self.attrs.outer());
1467 self.abi.to_tokens(tokens);
1468 self.brace_token.surround(tokens, |tokens| {
1469 tokens.append_all(&self.items);
1470 });
1471 }
1472 }
1473
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001474 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001475 fn to_tokens(&self, tokens: &mut Tokens) {
1476 tokens.append_all(self.attrs.outer());
1477 self.vis.to_tokens(tokens);
1478 self.type_token.to_tokens(tokens);
1479 self.ident.to_tokens(tokens);
1480 self.generics.to_tokens(tokens);
1481 self.generics.where_clause.to_tokens(tokens);
1482 self.eq_token.to_tokens(tokens);
1483 self.ty.to_tokens(tokens);
1484 self.semi_token.to_tokens(tokens);
1485 }
1486 }
1487
1488 impl ToTokens for ItemEnum {
1489 fn to_tokens(&self, tokens: &mut Tokens) {
1490 tokens.append_all(self.attrs.outer());
1491 self.vis.to_tokens(tokens);
1492 self.enum_token.to_tokens(tokens);
1493 self.ident.to_tokens(tokens);
1494 self.generics.to_tokens(tokens);
1495 self.generics.where_clause.to_tokens(tokens);
1496 self.brace_token.surround(tokens, |tokens| {
1497 self.variants.to_tokens(tokens);
1498 });
1499 }
1500 }
1501
1502 impl ToTokens for ItemStruct {
1503 fn to_tokens(&self, tokens: &mut Tokens) {
1504 tokens.append_all(self.attrs.outer());
1505 self.vis.to_tokens(tokens);
1506 self.struct_token.to_tokens(tokens);
1507 self.ident.to_tokens(tokens);
1508 self.generics.to_tokens(tokens);
1509 match self.data {
1510 VariantData::Struct(..) => {
1511 self.generics.where_clause.to_tokens(tokens);
1512 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001513 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001514 VariantData::Tuple(..) => {
1515 self.data.to_tokens(tokens);
1516 self.generics.where_clause.to_tokens(tokens);
1517 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001518 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001519 VariantData::Unit => {
1520 self.generics.where_clause.to_tokens(tokens);
1521 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001522 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001523 }
1524 }
1525 }
1526
1527 impl ToTokens for ItemUnion {
1528 fn to_tokens(&self, tokens: &mut Tokens) {
1529 tokens.append_all(self.attrs.outer());
1530 self.vis.to_tokens(tokens);
1531 self.union_token.to_tokens(tokens);
1532 self.ident.to_tokens(tokens);
1533 self.generics.to_tokens(tokens);
1534 self.generics.where_clause.to_tokens(tokens);
1535 // XXX: Should we handle / complain when using a
1536 // non-VariantData::Struct Variant in Union?
1537 self.data.to_tokens(tokens);
1538 }
1539 }
1540
1541 impl ToTokens for ItemTrait {
1542 fn to_tokens(&self, tokens: &mut Tokens) {
1543 tokens.append_all(self.attrs.outer());
1544 self.vis.to_tokens(tokens);
1545 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001546 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001547 self.trait_token.to_tokens(tokens);
1548 self.ident.to_tokens(tokens);
1549 self.generics.to_tokens(tokens);
1550 if !self.supertraits.is_empty() {
1551 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1552 self.supertraits.to_tokens(tokens);
1553 }
1554 self.generics.where_clause.to_tokens(tokens);
1555 self.brace_token.surround(tokens, |tokens| {
1556 tokens.append_all(&self.items);
1557 });
1558 }
1559 }
1560
David Tolnay1bfa7332017-11-11 12:41:20 -08001561 impl ToTokens for ItemImpl {
1562 fn to_tokens(&self, tokens: &mut Tokens) {
1563 tokens.append_all(self.attrs.outer());
1564 self.defaultness.to_tokens(tokens);
1565 self.unsafety.to_tokens(tokens);
1566 self.impl_token.to_tokens(tokens);
1567 self.generics.to_tokens(tokens);
1568 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1569 polarity.to_tokens(tokens);
1570 path.to_tokens(tokens);
1571 for_token.to_tokens(tokens);
1572 }
1573 self.self_ty.to_tokens(tokens);
1574 self.generics.where_clause.to_tokens(tokens);
1575 self.brace_token.surround(tokens, |tokens| {
1576 tokens.append_all(&self.items);
1577 });
1578 }
1579 }
1580
1581 impl ToTokens for ItemMacro {
1582 fn to_tokens(&self, tokens: &mut Tokens) {
1583 tokens.append_all(self.attrs.outer());
1584 self.mac.path.to_tokens(tokens);
1585 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001586 self.ident.to_tokens(tokens);
David Tolnayfe9d2782017-12-29 11:32:42 -05001587 self.mac.tt.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001588 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001589 }
1590 }
David Tolnay42602292016-10-01 22:25:45 -07001591
David Tolnay500d8322017-12-18 00:32:51 -08001592 impl ToTokens for ItemMacro2 {
1593 fn to_tokens(&self, tokens: &mut Tokens) {
1594 tokens.append_all(self.attrs.outer());
1595 self.vis.to_tokens(tokens);
1596 self.macro_token.to_tokens(tokens);
1597 self.ident.to_tokens(tokens);
1598 self.args.to_tokens(tokens);
1599 self.body.to_tokens(tokens);
1600 }
1601 }
1602
David Tolnay2ae520a2017-12-29 11:19:50 -05001603 impl ToTokens for ItemVerbatim {
1604 fn to_tokens(&self, tokens: &mut Tokens) {
1605 self.tts.to_tokens(tokens);
1606 }
1607 }
1608
David Tolnay5f332a92017-12-26 00:42:45 -05001609 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001610 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001611 self.ident.to_tokens(tokens);
1612 if let Some((ref as_token, ref rename)) = self.rename {
1613 as_token.to_tokens(tokens);
1614 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001615 }
David Tolnay4a057422016-10-08 00:02:31 -07001616 }
1617 }
1618
David Tolnay5f332a92017-12-26 00:42:45 -05001619 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001620 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001621 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001622 }
1623 }
1624
David Tolnay5f332a92017-12-26 00:42:45 -05001625 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001626 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001627 self.brace_token.surround(tokens, |tokens| {
1628 self.items.to_tokens(tokens);
1629 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001630 }
1631 }
1632
David Tolnay1bfa7332017-11-11 12:41:20 -08001633 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001634 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001635 tokens.append_all(self.attrs.outer());
1636 self.const_token.to_tokens(tokens);
1637 self.ident.to_tokens(tokens);
1638 self.colon_token.to_tokens(tokens);
1639 self.ty.to_tokens(tokens);
1640 if let Some((ref eq_token, ref default)) = self.default {
1641 eq_token.to_tokens(tokens);
1642 default.to_tokens(tokens);
1643 }
1644 self.semi_token.to_tokens(tokens);
1645 }
1646 }
1647
1648 impl ToTokens for TraitItemMethod {
1649 fn to_tokens(&self, tokens: &mut Tokens) {
1650 tokens.append_all(self.attrs.outer());
1651 self.sig.to_tokens(tokens);
1652 match self.default {
1653 Some(ref block) => {
1654 block.brace_token.surround(tokens, |tokens| {
1655 tokens.append_all(self.attrs.inner());
1656 tokens.append_all(&block.stmts);
1657 });
David Tolnayca085422016-10-04 00:12:38 -07001658 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001659 None => {
1660 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001661 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001662 }
1663 }
1664 }
1665
1666 impl ToTokens for TraitItemType {
1667 fn to_tokens(&self, tokens: &mut Tokens) {
1668 tokens.append_all(self.attrs.outer());
1669 self.type_token.to_tokens(tokens);
1670 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001671 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001672 if !self.bounds.is_empty() {
1673 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1674 self.bounds.to_tokens(tokens);
1675 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001676 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001677 if let Some((ref eq_token, ref default)) = self.default {
1678 eq_token.to_tokens(tokens);
1679 default.to_tokens(tokens);
1680 }
1681 self.semi_token.to_tokens(tokens);
1682 }
1683 }
1684
1685 impl ToTokens for TraitItemMacro {
1686 fn to_tokens(&self, tokens: &mut Tokens) {
1687 tokens.append_all(self.attrs.outer());
1688 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001689 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001690 }
1691 }
1692
David Tolnay2ae520a2017-12-29 11:19:50 -05001693 impl ToTokens for TraitItemVerbatim {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 self.tts.to_tokens(tokens);
1696 }
1697 }
1698
David Tolnay857628c2017-11-11 12:25:31 -08001699 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001700 fn to_tokens(&self, tokens: &mut Tokens) {
1701 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001702 self.vis.to_tokens(tokens);
1703 self.defaultness.to_tokens(tokens);
1704 self.const_token.to_tokens(tokens);
1705 self.ident.to_tokens(tokens);
1706 self.colon_token.to_tokens(tokens);
1707 self.ty.to_tokens(tokens);
1708 self.eq_token.to_tokens(tokens);
1709 self.expr.to_tokens(tokens);
1710 self.semi_token.to_tokens(tokens);
1711 }
1712 }
1713
1714 impl ToTokens for ImplItemMethod {
1715 fn to_tokens(&self, tokens: &mut Tokens) {
1716 tokens.append_all(self.attrs.outer());
1717 self.vis.to_tokens(tokens);
1718 self.defaultness.to_tokens(tokens);
1719 self.sig.to_tokens(tokens);
1720 self.block.brace_token.surround(tokens, |tokens| {
1721 tokens.append_all(self.attrs.inner());
1722 tokens.append_all(&self.block.stmts);
1723 });
1724 }
1725 }
1726
1727 impl ToTokens for ImplItemType {
1728 fn to_tokens(&self, tokens: &mut Tokens) {
1729 tokens.append_all(self.attrs.outer());
1730 self.vis.to_tokens(tokens);
1731 self.defaultness.to_tokens(tokens);
1732 self.type_token.to_tokens(tokens);
1733 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001734 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001735 self.eq_token.to_tokens(tokens);
1736 self.ty.to_tokens(tokens);
1737 self.semi_token.to_tokens(tokens);
1738 }
1739 }
1740
1741 impl ToTokens for ImplItemMacro {
1742 fn to_tokens(&self, tokens: &mut Tokens) {
1743 tokens.append_all(self.attrs.outer());
1744 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001745 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001746 }
1747 }
1748
David Tolnay2ae520a2017-12-29 11:19:50 -05001749 impl ToTokens for ImplItemVerbatim {
1750 fn to_tokens(&self, tokens: &mut Tokens) {
1751 self.tts.to_tokens(tokens);
1752 }
1753 }
1754
David Tolnay8894f602017-11-11 12:11:04 -08001755 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001756 fn to_tokens(&self, tokens: &mut Tokens) {
1757 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001759 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1760 self.semi_token.to_tokens(tokens);
1761 }
1762 }
1763
1764 impl ToTokens for ForeignItemStatic {
1765 fn to_tokens(&self, tokens: &mut Tokens) {
1766 tokens.append_all(self.attrs.outer());
1767 self.vis.to_tokens(tokens);
1768 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001769 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001770 self.ident.to_tokens(tokens);
1771 self.colon_token.to_tokens(tokens);
1772 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001773 self.semi_token.to_tokens(tokens);
1774 }
1775 }
1776
David Tolnay199bcbb2017-11-12 10:33:52 -08001777 impl ToTokens for ForeignItemType {
1778 fn to_tokens(&self, tokens: &mut Tokens) {
1779 tokens.append_all(self.attrs.outer());
1780 self.vis.to_tokens(tokens);
1781 self.type_token.to_tokens(tokens);
1782 self.ident.to_tokens(tokens);
1783 self.semi_token.to_tokens(tokens);
1784 }
1785 }
1786
David Tolnay2ae520a2017-12-29 11:19:50 -05001787 impl ToTokens for ForeignItemVerbatim {
1788 fn to_tokens(&self, tokens: &mut Tokens) {
1789 self.tts.to_tokens(tokens);
1790 }
1791 }
1792
David Tolnay570695e2017-06-03 16:15:13 -07001793 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001794 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001795 self.constness.to_tokens(tokens);
1796 self.unsafety.to_tokens(tokens);
1797 self.abi.to_tokens(tokens);
1798 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001799 }
1800 }
1801
David Tolnay570695e2017-06-03 16:15:13 -07001802 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001803
1804 impl<'a> ToTokens for NamedDecl<'a> {
1805 fn to_tokens(&self, tokens: &mut Tokens) {
1806 self.0.fn_token.to_tokens(tokens);
1807 self.1.to_tokens(tokens);
1808 self.0.generics.to_tokens(tokens);
1809 self.0.paren_token.surround(tokens, |tokens| {
1810 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001811 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1812 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001813 }
David Tolnayd2836e22017-12-27 23:13:00 -05001814 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001815 });
1816 self.0.output.to_tokens(tokens);
1817 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001818 }
1819 }
1820
Alex Crichton62a0a592017-05-22 13:58:53 -07001821 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001822 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001823 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001824 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001825 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001826 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001827 }
1828 }
1829
1830 impl ToTokens for ArgSelf {
1831 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001832 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001833 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001834 }
1835 }
1836
1837 impl ToTokens for ArgCaptured {
1838 fn to_tokens(&self, tokens: &mut Tokens) {
1839 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001840 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001841 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001842 }
1843 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001844}