blob: 90b70f05df729d05b672287ccf0e08f30bd482aa [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) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500908 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700909 (args, variadic)
910 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800911 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500912 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800913 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700914 ({
915 let ((inputs, variadic), parens) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800916 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700917 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700918 attrs: attrs,
919 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800920 decl: Box::new(FnDecl {
921 fn_token: fn_,
922 paren_token: parens,
923 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500924 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800925 output: ret,
926 generics: Generics {
927 where_clause: where_clause,
928 .. generics
929 },
930 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700931 vis: vis,
932 }
David Tolnay35902302016-10-06 01:11:08 -0700933 })
934 ));
935
David Tolnay8894f602017-11-11 12:11:04 -0800936 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500937 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700938 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800939 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500940 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700941 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800942 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800943 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800944 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800945 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700946 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700947 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700948 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800949 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500950 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800951 static_token: static_,
952 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700953 vis: vis,
954 })
955 ));
956
David Tolnay199bcbb2017-11-12 10:33:52 -0800957 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500958 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800959 vis: syn!(Visibility) >>
960 type_: keyword!(type) >>
961 ident: syn!(Ident) >>
962 semi: punct!(;) >>
963 (ForeignItemType {
964 attrs: attrs,
965 vis: vis,
966 type_token: type_,
967 ident: ident,
968 semi_token: semi,
969 })
970 ));
971
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800972 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500973 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700974 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700976 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700977 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500978 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800979 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800980 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800981 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800982 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700983 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800984 vis: vis,
985 type_token: type_,
986 ident: ident,
987 generics: Generics {
988 where_clause: where_clause,
989 ..generics
990 },
991 eq_token: eq,
992 ty: Box::new(ty),
993 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800994 })
David Tolnay3cf52982016-10-01 17:11:37 -0700995 ));
996
David Tolnay4c614be2017-11-10 00:02:38 -0800997 impl_synom!(ItemStruct "struct item" switch!(
998 map!(syn!(DeriveInput), Into::into),
999 Item::Struct(item) => value!(item)
1000 |
1001 _ => reject!()
1002 ));
David Tolnay42602292016-10-01 22:25:45 -07001003
David Tolnay4c614be2017-11-10 00:02:38 -08001004 impl_synom!(ItemEnum "enum item" switch!(
1005 map!(syn!(DeriveInput), Into::into),
1006 Item::Enum(item) => value!(item)
1007 |
1008 _ => reject!()
1009 ));
1010
1011 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001012 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001013 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001014 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001015 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001016 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001017 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001018 fields: braces!(call!(Delimited::parse_terminated_with,
1019 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001020 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001021 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001022 vis: vis,
1023 union_token: union_,
1024 ident: ident,
1025 generics: Generics {
1026 where_clause: where_clause,
1027 .. generics
1028 },
1029 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -08001030 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001031 ));
1032
David Tolnay4c614be2017-11-10 00:02:38 -08001033 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001034 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001035 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001036 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001037 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001038 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001039 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001040 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001041 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001042 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001043 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001044 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001045 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001046 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001047 vis: vis,
1048 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001049 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001050 trait_token: trait_,
1051 ident: ident,
1052 generics: Generics {
1053 where_clause: where_clause,
1054 .. generics
1055 },
1056 colon_token: colon,
1057 supertraits: bounds.unwrap_or_default(),
1058 brace_token: body.1,
1059 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001060 })
David Tolnay0aecb732016-10-03 23:03:50 -07001061 ));
1062
David Tolnay03342952017-12-29 11:52:00 -05001063 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1064 Ok((cursor, cursor))
1065 }
1066
1067 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1068 begin: call!(grab_cursor) >>
1069 many0!(Attribute::parse_outer) >>
1070 option!(keyword!(unsafe)) >>
1071 keyword!(impl) >>
1072 syn!(Path) >>
1073 keyword!(for) >>
1074 punct!(..) >>
1075 braces!(epsilon!()) >>
1076 end: call!(grab_cursor) >>
1077 ({
1078 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1079 let len = tts.len() - end.token_stream().into_iter().count();
1080 ItemVerbatim {
1081 tts: tts.into_iter().take(len).collect(),
1082 }
David Tolnay4c614be2017-11-10 00:02:38 -08001083 })
David Tolnayf94e2362016-10-04 00:29:51 -07001084 ));
1085
David Tolnayda705bd2017-11-10 21:58:05 -08001086 impl_synom!(TraitItem "trait item" alt!(
1087 syn!(TraitItemConst) => { TraitItem::Const }
1088 |
1089 syn!(TraitItemMethod) => { TraitItem::Method }
1090 |
1091 syn!(TraitItemType) => { TraitItem::Type }
1092 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001093 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001094 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001095
David Tolnayda705bd2017-11-10 21:58:05 -08001096 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001097 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001099 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001100 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001101 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001102 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1103 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001104 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001105 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001106 const_token: const_,
1107 ident: ident,
1108 colon_token: colon,
1109 ty: ty,
1110 default: default,
1111 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001112 })
1113 ));
1114
David Tolnayda705bd2017-11-10 21:58:05 -08001115 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001116 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001117 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001118 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001119 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001120 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001121 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001122 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001123 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001124 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001125 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001126 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001127 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001128 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001129 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001130 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001131 ({
1132 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001133 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001134 None => (Vec::new(), None),
1135 };
David Tolnayda705bd2017-11-10 21:58:05 -08001136 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001137 attrs: {
1138 let mut attrs = outer_attrs;
1139 attrs.extend(inner_attrs);
1140 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001141 },
David Tolnayda705bd2017-11-10 21:58:05 -08001142 sig: MethodSig {
1143 constness: constness,
1144 unsafety: unsafety,
1145 abi: abi,
1146 ident: ident,
1147 decl: FnDecl {
1148 inputs: inputs.0,
1149 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001150 fn_token: fn_,
1151 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001152 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001153 generics: Generics {
1154 where_clause: where_clause,
1155 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001156 },
1157 },
David Tolnayda705bd2017-11-10 21:58:05 -08001158 },
1159 default: stmts.map(|stmts| {
1160 Block {
1161 stmts: stmts.0,
1162 brace_token: stmts.1,
1163 }
1164 }),
1165 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001166 }
David Tolnay0aecb732016-10-03 23:03:50 -07001167 })
1168 ));
1169
David Tolnayda705bd2017-11-10 21:58:05 -08001170 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001171 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001172 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001173 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001174 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001175 colon: option!(punct!(:)) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001176 bounds: cond!(colon.is_some(), Delimited::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001177 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001178 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001180 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001181 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001182 type_token: type_,
1183 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001184 generics: Generics {
1185 where_clause: where_clause,
1186 .. generics
1187 },
David Tolnayda705bd2017-11-10 21:58:05 -08001188 colon_token: colon,
1189 bounds: bounds.unwrap_or_default(),
1190 default: default,
1191 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001192 })
1193 ));
1194
David Tolnaydecf28d2017-11-11 11:56:45 -08001195 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001196 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001197 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001198 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001199 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001200 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001201 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001202 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001203 })
1204 ));
1205
David Tolnay4c614be2017-11-10 00:02:38 -08001206 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001207 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001208 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001209 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001211 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001212 polarity_path: alt!(
1213 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001214 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001215 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001216 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001217 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001218 )
1219 |
David Tolnay570695e2017-06-03 16:15:13 -07001220 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001221 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001222 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001223 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001224 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001225 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001226 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001227 defaultness: defaultness,
1228 unsafety: unsafety,
1229 impl_token: impl_,
1230 generics: Generics {
1231 where_clause: where_clause,
1232 .. generics
1233 },
1234 trait_: polarity_path,
1235 self_ty: Box::new(self_ty),
1236 brace_token: body.1,
1237 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001238 })
David Tolnay4c9be372016-10-06 00:47:37 -07001239 ));
1240
David Tolnay857628c2017-11-11 12:25:31 -08001241 impl_synom!(ImplItem "item in impl block" alt!(
1242 syn!(ImplItemConst) => { ImplItem::Const }
1243 |
1244 syn!(ImplItemMethod) => { ImplItem::Method }
1245 |
1246 syn!(ImplItemType) => { ImplItem::Type }
1247 |
1248 syn!(ImplItemMacro) => { ImplItem::Macro }
1249 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001250
David Tolnay857628c2017-11-11 12:25:31 -08001251 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001252 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001253 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001254 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001255 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001256 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001258 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001259 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001260 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001262 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001263 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001264 vis: vis,
1265 defaultness: defaultness,
1266 const_token: const_,
1267 ident: ident,
1268 colon_token: colon,
1269 ty: ty,
1270 eq_token: eq,
1271 expr: value,
1272 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001273 })
1274 ));
1275
David Tolnay857628c2017-11-11 12:25:31 -08001276 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001277 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001278 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001279 defaultness: option!(keyword!(default)) >>
1280 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001281 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001282 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001283 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001284 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001285 generics: syn!(Generics) >>
David Tolnaye64213b2017-12-30 00:24:20 -05001286 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001287 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001288 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001289 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001290 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001291 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001292 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001293 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001294 attrs: {
1295 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001296 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001297 attrs
1298 },
David Tolnay857628c2017-11-11 12:25:31 -08001299 vis: vis,
1300 defaultness: defaultness,
1301 sig: MethodSig {
1302 constness: constness,
1303 unsafety: unsafety,
1304 abi: abi,
1305 ident: ident,
1306 decl: FnDecl {
1307 fn_token: fn_,
1308 paren_token: inputs.1,
1309 inputs: inputs.0,
1310 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001311 generics: Generics {
1312 where_clause: where_clause,
1313 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001314 },
David Tolnayd2836e22017-12-27 23:13:00 -05001315 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001316 },
David Tolnay857628c2017-11-11 12:25:31 -08001317 },
1318 block: Block {
1319 brace_token: inner_attrs_stmts.1,
1320 stmts: (inner_attrs_stmts.0).1,
1321 },
David Tolnay4c9be372016-10-06 00:47:37 -07001322 })
1323 ));
1324
David Tolnay857628c2017-11-11 12:25:31 -08001325 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001326 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001327 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001328 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001329 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001330 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001331 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001332 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001333 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001335 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001336 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001337 vis: vis,
1338 defaultness: defaultness,
1339 type_token: type_,
1340 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001341 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001342 eq_token: eq,
1343 ty: ty,
1344 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001345 })
1346 ));
1347
David Tolnay857628c2017-11-11 12:25:31 -08001348 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001349 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001350 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001351 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001352 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001353 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001354 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001355 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001356 })
1357 ));
1358
David Tolnay57292da2017-12-27 21:03:33 -05001359 fn is_braced(tt: &TokenTree) -> bool {
1360 match tt.kind {
1361 TokenNode::Group(Delimiter::Brace, _) => true,
1362 _ => false,
1363 }
1364 }
David Tolnayedf2b992016-09-23 20:43:45 -07001365}
David Tolnay4a51dc72016-10-01 00:40:31 -07001366
1367#[cfg(feature = "printing")]
1368mod printing {
1369 use super::*;
1370 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001371 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001372 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001373
David Tolnay1bfa7332017-11-11 12:41:20 -08001374 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001375 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001376 tokens.append_all(self.attrs.outer());
1377 self.vis.to_tokens(tokens);
1378 self.extern_token.to_tokens(tokens);
1379 self.crate_token.to_tokens(tokens);
1380 self.ident.to_tokens(tokens);
1381 if let Some((ref as_token, ref rename)) = self.rename {
1382 as_token.to_tokens(tokens);
1383 rename.to_tokens(tokens);
1384 }
1385 self.semi_token.to_tokens(tokens);
1386 }
1387 }
1388
1389 impl ToTokens for ItemUse {
1390 fn to_tokens(&self, tokens: &mut Tokens) {
1391 tokens.append_all(self.attrs.outer());
1392 self.vis.to_tokens(tokens);
1393 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001394 self.leading_colon.to_tokens(tokens);
1395 self.prefix.to_tokens(tokens);
1396 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001397 self.semi_token.to_tokens(tokens);
1398 }
1399 }
1400
1401 impl ToTokens for ItemStatic {
1402 fn to_tokens(&self, tokens: &mut Tokens) {
1403 tokens.append_all(self.attrs.outer());
1404 self.vis.to_tokens(tokens);
1405 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001406 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001407 self.ident.to_tokens(tokens);
1408 self.colon_token.to_tokens(tokens);
1409 self.ty.to_tokens(tokens);
1410 self.eq_token.to_tokens(tokens);
1411 self.expr.to_tokens(tokens);
1412 self.semi_token.to_tokens(tokens);
1413 }
1414 }
1415
1416 impl ToTokens for ItemConst {
1417 fn to_tokens(&self, tokens: &mut Tokens) {
1418 tokens.append_all(self.attrs.outer());
1419 self.vis.to_tokens(tokens);
1420 self.const_token.to_tokens(tokens);
1421 self.ident.to_tokens(tokens);
1422 self.colon_token.to_tokens(tokens);
1423 self.ty.to_tokens(tokens);
1424 self.eq_token.to_tokens(tokens);
1425 self.expr.to_tokens(tokens);
1426 self.semi_token.to_tokens(tokens);
1427 }
1428 }
1429
1430 impl ToTokens for ItemFn {
1431 fn to_tokens(&self, tokens: &mut Tokens) {
1432 tokens.append_all(self.attrs.outer());
1433 self.vis.to_tokens(tokens);
1434 self.constness.to_tokens(tokens);
1435 self.unsafety.to_tokens(tokens);
1436 self.abi.to_tokens(tokens);
1437 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1438 self.block.brace_token.surround(tokens, |tokens| {
1439 tokens.append_all(self.attrs.inner());
1440 tokens.append_all(&self.block.stmts);
1441 });
1442 }
1443 }
1444
1445 impl ToTokens for ItemMod {
1446 fn to_tokens(&self, tokens: &mut Tokens) {
1447 tokens.append_all(self.attrs.outer());
1448 self.vis.to_tokens(tokens);
1449 self.mod_token.to_tokens(tokens);
1450 self.ident.to_tokens(tokens);
1451 if let Some((ref brace, ref items)) = self.content {
1452 brace.surround(tokens, |tokens| {
1453 tokens.append_all(self.attrs.inner());
1454 tokens.append_all(items);
1455 });
1456 } else {
1457 TokensOrDefault(&self.semi).to_tokens(tokens);
1458 }
1459 }
1460 }
1461
1462 impl ToTokens for ItemForeignMod {
1463 fn to_tokens(&self, tokens: &mut Tokens) {
1464 tokens.append_all(self.attrs.outer());
1465 self.abi.to_tokens(tokens);
1466 self.brace_token.surround(tokens, |tokens| {
1467 tokens.append_all(&self.items);
1468 });
1469 }
1470 }
1471
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001472 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001473 fn to_tokens(&self, tokens: &mut Tokens) {
1474 tokens.append_all(self.attrs.outer());
1475 self.vis.to_tokens(tokens);
1476 self.type_token.to_tokens(tokens);
1477 self.ident.to_tokens(tokens);
1478 self.generics.to_tokens(tokens);
1479 self.generics.where_clause.to_tokens(tokens);
1480 self.eq_token.to_tokens(tokens);
1481 self.ty.to_tokens(tokens);
1482 self.semi_token.to_tokens(tokens);
1483 }
1484 }
1485
1486 impl ToTokens for ItemEnum {
1487 fn to_tokens(&self, tokens: &mut Tokens) {
1488 tokens.append_all(self.attrs.outer());
1489 self.vis.to_tokens(tokens);
1490 self.enum_token.to_tokens(tokens);
1491 self.ident.to_tokens(tokens);
1492 self.generics.to_tokens(tokens);
1493 self.generics.where_clause.to_tokens(tokens);
1494 self.brace_token.surround(tokens, |tokens| {
1495 self.variants.to_tokens(tokens);
1496 });
1497 }
1498 }
1499
1500 impl ToTokens for ItemStruct {
1501 fn to_tokens(&self, tokens: &mut Tokens) {
1502 tokens.append_all(self.attrs.outer());
1503 self.vis.to_tokens(tokens);
1504 self.struct_token.to_tokens(tokens);
1505 self.ident.to_tokens(tokens);
1506 self.generics.to_tokens(tokens);
1507 match self.data {
1508 VariantData::Struct(..) => {
1509 self.generics.where_clause.to_tokens(tokens);
1510 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001511 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001512 VariantData::Tuple(..) => {
1513 self.data.to_tokens(tokens);
1514 self.generics.where_clause.to_tokens(tokens);
1515 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001516 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001517 VariantData::Unit => {
1518 self.generics.where_clause.to_tokens(tokens);
1519 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001520 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001521 }
1522 }
1523 }
1524
1525 impl ToTokens for ItemUnion {
1526 fn to_tokens(&self, tokens: &mut Tokens) {
1527 tokens.append_all(self.attrs.outer());
1528 self.vis.to_tokens(tokens);
1529 self.union_token.to_tokens(tokens);
1530 self.ident.to_tokens(tokens);
1531 self.generics.to_tokens(tokens);
1532 self.generics.where_clause.to_tokens(tokens);
1533 // XXX: Should we handle / complain when using a
1534 // non-VariantData::Struct Variant in Union?
1535 self.data.to_tokens(tokens);
1536 }
1537 }
1538
1539 impl ToTokens for ItemTrait {
1540 fn to_tokens(&self, tokens: &mut Tokens) {
1541 tokens.append_all(self.attrs.outer());
1542 self.vis.to_tokens(tokens);
1543 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001544 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001545 self.trait_token.to_tokens(tokens);
1546 self.ident.to_tokens(tokens);
1547 self.generics.to_tokens(tokens);
1548 if !self.supertraits.is_empty() {
1549 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1550 self.supertraits.to_tokens(tokens);
1551 }
1552 self.generics.where_clause.to_tokens(tokens);
1553 self.brace_token.surround(tokens, |tokens| {
1554 tokens.append_all(&self.items);
1555 });
1556 }
1557 }
1558
David Tolnay1bfa7332017-11-11 12:41:20 -08001559 impl ToTokens for ItemImpl {
1560 fn to_tokens(&self, tokens: &mut Tokens) {
1561 tokens.append_all(self.attrs.outer());
1562 self.defaultness.to_tokens(tokens);
1563 self.unsafety.to_tokens(tokens);
1564 self.impl_token.to_tokens(tokens);
1565 self.generics.to_tokens(tokens);
1566 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1567 polarity.to_tokens(tokens);
1568 path.to_tokens(tokens);
1569 for_token.to_tokens(tokens);
1570 }
1571 self.self_ty.to_tokens(tokens);
1572 self.generics.where_clause.to_tokens(tokens);
1573 self.brace_token.surround(tokens, |tokens| {
1574 tokens.append_all(&self.items);
1575 });
1576 }
1577 }
1578
1579 impl ToTokens for ItemMacro {
1580 fn to_tokens(&self, tokens: &mut Tokens) {
1581 tokens.append_all(self.attrs.outer());
1582 self.mac.path.to_tokens(tokens);
1583 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001584 self.ident.to_tokens(tokens);
David Tolnayfe9d2782017-12-29 11:32:42 -05001585 self.mac.tt.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001586 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001587 }
1588 }
David Tolnay42602292016-10-01 22:25:45 -07001589
David Tolnay500d8322017-12-18 00:32:51 -08001590 impl ToTokens for ItemMacro2 {
1591 fn to_tokens(&self, tokens: &mut Tokens) {
1592 tokens.append_all(self.attrs.outer());
1593 self.vis.to_tokens(tokens);
1594 self.macro_token.to_tokens(tokens);
1595 self.ident.to_tokens(tokens);
1596 self.args.to_tokens(tokens);
1597 self.body.to_tokens(tokens);
1598 }
1599 }
1600
David Tolnay2ae520a2017-12-29 11:19:50 -05001601 impl ToTokens for ItemVerbatim {
1602 fn to_tokens(&self, tokens: &mut Tokens) {
1603 self.tts.to_tokens(tokens);
1604 }
1605 }
1606
David Tolnay5f332a92017-12-26 00:42:45 -05001607 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001608 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001609 self.ident.to_tokens(tokens);
1610 if let Some((ref as_token, ref rename)) = self.rename {
1611 as_token.to_tokens(tokens);
1612 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001613 }
David Tolnay4a057422016-10-08 00:02:31 -07001614 }
1615 }
1616
David Tolnay5f332a92017-12-26 00:42:45 -05001617 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001618 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001619 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001620 }
1621 }
1622
David Tolnay5f332a92017-12-26 00:42:45 -05001623 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001624 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001625 self.brace_token.surround(tokens, |tokens| {
1626 self.items.to_tokens(tokens);
1627 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001628 }
1629 }
1630
David Tolnay1bfa7332017-11-11 12:41:20 -08001631 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001632 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001633 tokens.append_all(self.attrs.outer());
1634 self.const_token.to_tokens(tokens);
1635 self.ident.to_tokens(tokens);
1636 self.colon_token.to_tokens(tokens);
1637 self.ty.to_tokens(tokens);
1638 if let Some((ref eq_token, ref default)) = self.default {
1639 eq_token.to_tokens(tokens);
1640 default.to_tokens(tokens);
1641 }
1642 self.semi_token.to_tokens(tokens);
1643 }
1644 }
1645
1646 impl ToTokens for TraitItemMethod {
1647 fn to_tokens(&self, tokens: &mut Tokens) {
1648 tokens.append_all(self.attrs.outer());
1649 self.sig.to_tokens(tokens);
1650 match self.default {
1651 Some(ref block) => {
1652 block.brace_token.surround(tokens, |tokens| {
1653 tokens.append_all(self.attrs.inner());
1654 tokens.append_all(&block.stmts);
1655 });
David Tolnayca085422016-10-04 00:12:38 -07001656 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001657 None => {
1658 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001659 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001660 }
1661 }
1662 }
1663
1664 impl ToTokens for TraitItemType {
1665 fn to_tokens(&self, tokens: &mut Tokens) {
1666 tokens.append_all(self.attrs.outer());
1667 self.type_token.to_tokens(tokens);
1668 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001669 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001670 if !self.bounds.is_empty() {
1671 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1672 self.bounds.to_tokens(tokens);
1673 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001674 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001675 if let Some((ref eq_token, ref default)) = self.default {
1676 eq_token.to_tokens(tokens);
1677 default.to_tokens(tokens);
1678 }
1679 self.semi_token.to_tokens(tokens);
1680 }
1681 }
1682
1683 impl ToTokens for TraitItemMacro {
1684 fn to_tokens(&self, tokens: &mut Tokens) {
1685 tokens.append_all(self.attrs.outer());
1686 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001687 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001688 }
1689 }
1690
David Tolnay2ae520a2017-12-29 11:19:50 -05001691 impl ToTokens for TraitItemVerbatim {
1692 fn to_tokens(&self, tokens: &mut Tokens) {
1693 self.tts.to_tokens(tokens);
1694 }
1695 }
1696
David Tolnay857628c2017-11-11 12:25:31 -08001697 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001700 self.vis.to_tokens(tokens);
1701 self.defaultness.to_tokens(tokens);
1702 self.const_token.to_tokens(tokens);
1703 self.ident.to_tokens(tokens);
1704 self.colon_token.to_tokens(tokens);
1705 self.ty.to_tokens(tokens);
1706 self.eq_token.to_tokens(tokens);
1707 self.expr.to_tokens(tokens);
1708 self.semi_token.to_tokens(tokens);
1709 }
1710 }
1711
1712 impl ToTokens for ImplItemMethod {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 tokens.append_all(self.attrs.outer());
1715 self.vis.to_tokens(tokens);
1716 self.defaultness.to_tokens(tokens);
1717 self.sig.to_tokens(tokens);
1718 self.block.brace_token.surround(tokens, |tokens| {
1719 tokens.append_all(self.attrs.inner());
1720 tokens.append_all(&self.block.stmts);
1721 });
1722 }
1723 }
1724
1725 impl ToTokens for ImplItemType {
1726 fn to_tokens(&self, tokens: &mut Tokens) {
1727 tokens.append_all(self.attrs.outer());
1728 self.vis.to_tokens(tokens);
1729 self.defaultness.to_tokens(tokens);
1730 self.type_token.to_tokens(tokens);
1731 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001732 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001733 self.eq_token.to_tokens(tokens);
1734 self.ty.to_tokens(tokens);
1735 self.semi_token.to_tokens(tokens);
1736 }
1737 }
1738
1739 impl ToTokens for ImplItemMacro {
1740 fn to_tokens(&self, tokens: &mut Tokens) {
1741 tokens.append_all(self.attrs.outer());
1742 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001743 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001744 }
1745 }
1746
David Tolnay2ae520a2017-12-29 11:19:50 -05001747 impl ToTokens for ImplItemVerbatim {
1748 fn to_tokens(&self, tokens: &mut Tokens) {
1749 self.tts.to_tokens(tokens);
1750 }
1751 }
1752
David Tolnay8894f602017-11-11 12:11:04 -08001753 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001754 fn to_tokens(&self, tokens: &mut Tokens) {
1755 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001756 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001757 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1758 self.semi_token.to_tokens(tokens);
1759 }
1760 }
1761
1762 impl ToTokens for ForeignItemStatic {
1763 fn to_tokens(&self, tokens: &mut Tokens) {
1764 tokens.append_all(self.attrs.outer());
1765 self.vis.to_tokens(tokens);
1766 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001767 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001768 self.ident.to_tokens(tokens);
1769 self.colon_token.to_tokens(tokens);
1770 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001771 self.semi_token.to_tokens(tokens);
1772 }
1773 }
1774
David Tolnay199bcbb2017-11-12 10:33:52 -08001775 impl ToTokens for ForeignItemType {
1776 fn to_tokens(&self, tokens: &mut Tokens) {
1777 tokens.append_all(self.attrs.outer());
1778 self.vis.to_tokens(tokens);
1779 self.type_token.to_tokens(tokens);
1780 self.ident.to_tokens(tokens);
1781 self.semi_token.to_tokens(tokens);
1782 }
1783 }
1784
David Tolnay2ae520a2017-12-29 11:19:50 -05001785 impl ToTokens for ForeignItemVerbatim {
1786 fn to_tokens(&self, tokens: &mut Tokens) {
1787 self.tts.to_tokens(tokens);
1788 }
1789 }
1790
David Tolnay570695e2017-06-03 16:15:13 -07001791 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001792 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001793 self.constness.to_tokens(tokens);
1794 self.unsafety.to_tokens(tokens);
1795 self.abi.to_tokens(tokens);
1796 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001797 }
1798 }
1799
David Tolnay570695e2017-06-03 16:15:13 -07001800 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001801
1802 impl<'a> ToTokens for NamedDecl<'a> {
1803 fn to_tokens(&self, tokens: &mut Tokens) {
1804 self.0.fn_token.to_tokens(tokens);
1805 self.1.to_tokens(tokens);
1806 self.0.generics.to_tokens(tokens);
1807 self.0.paren_token.surround(tokens, |tokens| {
1808 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001809 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1810 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001811 }
David Tolnayd2836e22017-12-27 23:13:00 -05001812 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001813 });
1814 self.0.output.to_tokens(tokens);
1815 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001816 }
1817 }
1818
Alex Crichton62a0a592017-05-22 13:58:53 -07001819 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001820 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001822 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001823 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001824 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001825 }
1826 }
1827
1828 impl ToTokens for ArgSelf {
1829 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001830 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001831 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001832 }
1833 }
1834
1835 impl ToTokens for ArgCaptured {
1836 fn to_tokens(&self, tokens: &mut Tokens) {
1837 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001838 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001839 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001840 }
1841 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001842}