blob: c9b7c9bd8458c2c4abab183a9efeaaabeae345b1 [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!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001044 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001045 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001046 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001047 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001048 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001049 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001050 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001051 vis: vis,
1052 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001053 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001054 trait_token: trait_,
1055 ident: ident,
1056 generics: Generics {
1057 where_clause: where_clause,
1058 .. generics
1059 },
1060 colon_token: colon,
1061 supertraits: bounds.unwrap_or_default(),
1062 brace_token: body.1,
1063 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001064 })
David Tolnay0aecb732016-10-03 23:03:50 -07001065 ));
1066
David Tolnay03342952017-12-29 11:52:00 -05001067 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1068 Ok((cursor, cursor))
1069 }
1070
1071 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1072 begin: call!(grab_cursor) >>
1073 many0!(Attribute::parse_outer) >>
1074 option!(keyword!(unsafe)) >>
1075 keyword!(impl) >>
1076 syn!(Path) >>
1077 keyword!(for) >>
1078 punct!(..) >>
1079 braces!(epsilon!()) >>
1080 end: call!(grab_cursor) >>
1081 ({
1082 let mut tts = begin.token_stream().into_iter().collect::<Vec<_>>();
1083 let len = tts.len() - end.token_stream().into_iter().count();
1084 ItemVerbatim {
1085 tts: tts.into_iter().take(len).collect(),
1086 }
David Tolnay4c614be2017-11-10 00:02:38 -08001087 })
David Tolnayf94e2362016-10-04 00:29:51 -07001088 ));
1089
David Tolnayda705bd2017-11-10 21:58:05 -08001090 impl_synom!(TraitItem "trait item" alt!(
1091 syn!(TraitItemConst) => { TraitItem::Const }
1092 |
1093 syn!(TraitItemMethod) => { TraitItem::Method }
1094 |
1095 syn!(TraitItemType) => { TraitItem::Type }
1096 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001097 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001098 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001099
David Tolnayda705bd2017-11-10 21:58:05 -08001100 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001101 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001102 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001103 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001104 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001105 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001106 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1107 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001108 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001109 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001110 const_token: const_,
1111 ident: ident,
1112 colon_token: colon,
1113 ty: ty,
1114 default: default,
1115 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001116 })
1117 ));
1118
David Tolnayda705bd2017-11-10 21:58:05 -08001119 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001120 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001121 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001122 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001123 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001124 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001125 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001126 generics: syn!(Generics) >>
1127 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001128 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001129 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001130 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001131 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001132 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001133 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001134 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001135 ({
1136 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001137 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001138 None => (Vec::new(), None),
1139 };
David Tolnayda705bd2017-11-10 21:58:05 -08001140 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001141 attrs: {
1142 let mut attrs = outer_attrs;
1143 attrs.extend(inner_attrs);
1144 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001145 },
David Tolnayda705bd2017-11-10 21:58:05 -08001146 sig: MethodSig {
1147 constness: constness,
1148 unsafety: unsafety,
1149 abi: abi,
1150 ident: ident,
1151 decl: FnDecl {
1152 inputs: inputs.0,
1153 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001154 fn_token: fn_,
1155 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001156 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001157 generics: Generics {
1158 where_clause: where_clause,
1159 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001160 },
1161 },
David Tolnayda705bd2017-11-10 21:58:05 -08001162 },
1163 default: stmts.map(|stmts| {
1164 Block {
1165 stmts: stmts.0,
1166 brace_token: stmts.1,
1167 }
1168 }),
1169 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001170 }
David Tolnay0aecb732016-10-03 23:03:50 -07001171 })
1172 ));
1173
David Tolnayda705bd2017-11-10 21:58:05 -08001174 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001175 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001176 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001177 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001178 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001180 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001181 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001182 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001183 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001184 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001185 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001186 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001187 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001188 type_token: type_,
1189 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001190 generics: Generics {
1191 where_clause: where_clause,
1192 .. generics
1193 },
David Tolnayda705bd2017-11-10 21:58:05 -08001194 colon_token: colon,
1195 bounds: bounds.unwrap_or_default(),
1196 default: default,
1197 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001198 })
1199 ));
1200
David Tolnaydecf28d2017-11-11 11:56:45 -08001201 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001202 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001203 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001204 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001205 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001206 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001207 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001208 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001209 })
1210 ));
1211
David Tolnay4c614be2017-11-10 00:02:38 -08001212 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001213 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001214 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001215 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001216 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001217 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001218 polarity_path: alt!(
1219 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001220 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001221 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001222 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001223 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001224 )
1225 |
David Tolnay570695e2017-06-03 16:15:13 -07001226 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001227 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001228 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001229 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001230 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001231 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001232 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001233 defaultness: defaultness,
1234 unsafety: unsafety,
1235 impl_token: impl_,
1236 generics: Generics {
1237 where_clause: where_clause,
1238 .. generics
1239 },
1240 trait_: polarity_path,
1241 self_ty: Box::new(self_ty),
1242 brace_token: body.1,
1243 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001244 })
David Tolnay4c9be372016-10-06 00:47:37 -07001245 ));
1246
David Tolnay857628c2017-11-11 12:25:31 -08001247 impl_synom!(ImplItem "item in impl block" alt!(
1248 syn!(ImplItemConst) => { ImplItem::Const }
1249 |
1250 syn!(ImplItemMethod) => { ImplItem::Method }
1251 |
1252 syn!(ImplItemType) => { ImplItem::Type }
1253 |
1254 syn!(ImplItemMacro) => { ImplItem::Macro }
1255 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001256
David Tolnay857628c2017-11-11 12:25:31 -08001257 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001258 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001259 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001260 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001262 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001264 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001265 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001266 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001267 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001268 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001269 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001270 vis: vis,
1271 defaultness: defaultness,
1272 const_token: const_,
1273 ident: ident,
1274 colon_token: colon,
1275 ty: ty,
1276 eq_token: eq,
1277 expr: value,
1278 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001279 })
1280 ));
1281
David Tolnay857628c2017-11-11 12:25:31 -08001282 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001283 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001284 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001285 defaultness: option!(keyword!(default)) >>
1286 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001287 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001288 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001289 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001290 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001291 generics: syn!(Generics) >>
1292 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001293 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001294 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001295 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001296 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001297 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001298 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001299 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001300 attrs: {
1301 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001302 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001303 attrs
1304 },
David Tolnay857628c2017-11-11 12:25:31 -08001305 vis: vis,
1306 defaultness: defaultness,
1307 sig: MethodSig {
1308 constness: constness,
1309 unsafety: unsafety,
1310 abi: abi,
1311 ident: ident,
1312 decl: FnDecl {
1313 fn_token: fn_,
1314 paren_token: inputs.1,
1315 inputs: inputs.0,
1316 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001317 generics: Generics {
1318 where_clause: where_clause,
1319 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001320 },
David Tolnayd2836e22017-12-27 23:13:00 -05001321 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001322 },
David Tolnay857628c2017-11-11 12:25:31 -08001323 },
1324 block: Block {
1325 brace_token: inner_attrs_stmts.1,
1326 stmts: (inner_attrs_stmts.0).1,
1327 },
David Tolnay4c9be372016-10-06 00:47:37 -07001328 })
1329 ));
1330
David Tolnay857628c2017-11-11 12:25:31 -08001331 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001332 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001333 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001334 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001335 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001336 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001337 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001338 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001339 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001340 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001341 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001342 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001343 vis: vis,
1344 defaultness: defaultness,
1345 type_token: type_,
1346 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001347 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001348 eq_token: eq,
1349 ty: ty,
1350 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001351 })
1352 ));
1353
David Tolnay857628c2017-11-11 12:25:31 -08001354 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001355 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001356 mac: syn!(Macro) >>
David Tolnayfe9d2782017-12-29 11:32:42 -05001357 semi: cond!(!is_braced(&mac.tt), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001358 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001359 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001360 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001361 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001362 })
1363 ));
1364
David Tolnay57292da2017-12-27 21:03:33 -05001365 fn is_braced(tt: &TokenTree) -> bool {
1366 match tt.kind {
1367 TokenNode::Group(Delimiter::Brace, _) => true,
1368 _ => false,
1369 }
1370 }
David Tolnayedf2b992016-09-23 20:43:45 -07001371}
David Tolnay4a51dc72016-10-01 00:40:31 -07001372
1373#[cfg(feature = "printing")]
1374mod printing {
1375 use super::*;
1376 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001377 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001378 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001379
David Tolnay1bfa7332017-11-11 12:41:20 -08001380 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001381 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001382 tokens.append_all(self.attrs.outer());
1383 self.vis.to_tokens(tokens);
1384 self.extern_token.to_tokens(tokens);
1385 self.crate_token.to_tokens(tokens);
1386 self.ident.to_tokens(tokens);
1387 if let Some((ref as_token, ref rename)) = self.rename {
1388 as_token.to_tokens(tokens);
1389 rename.to_tokens(tokens);
1390 }
1391 self.semi_token.to_tokens(tokens);
1392 }
1393 }
1394
1395 impl ToTokens for ItemUse {
1396 fn to_tokens(&self, tokens: &mut Tokens) {
1397 tokens.append_all(self.attrs.outer());
1398 self.vis.to_tokens(tokens);
1399 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001400 self.leading_colon.to_tokens(tokens);
1401 self.prefix.to_tokens(tokens);
1402 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001403 self.semi_token.to_tokens(tokens);
1404 }
1405 }
1406
1407 impl ToTokens for ItemStatic {
1408 fn to_tokens(&self, tokens: &mut Tokens) {
1409 tokens.append_all(self.attrs.outer());
1410 self.vis.to_tokens(tokens);
1411 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001412 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001413 self.ident.to_tokens(tokens);
1414 self.colon_token.to_tokens(tokens);
1415 self.ty.to_tokens(tokens);
1416 self.eq_token.to_tokens(tokens);
1417 self.expr.to_tokens(tokens);
1418 self.semi_token.to_tokens(tokens);
1419 }
1420 }
1421
1422 impl ToTokens for ItemConst {
1423 fn to_tokens(&self, tokens: &mut Tokens) {
1424 tokens.append_all(self.attrs.outer());
1425 self.vis.to_tokens(tokens);
1426 self.const_token.to_tokens(tokens);
1427 self.ident.to_tokens(tokens);
1428 self.colon_token.to_tokens(tokens);
1429 self.ty.to_tokens(tokens);
1430 self.eq_token.to_tokens(tokens);
1431 self.expr.to_tokens(tokens);
1432 self.semi_token.to_tokens(tokens);
1433 }
1434 }
1435
1436 impl ToTokens for ItemFn {
1437 fn to_tokens(&self, tokens: &mut Tokens) {
1438 tokens.append_all(self.attrs.outer());
1439 self.vis.to_tokens(tokens);
1440 self.constness.to_tokens(tokens);
1441 self.unsafety.to_tokens(tokens);
1442 self.abi.to_tokens(tokens);
1443 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1444 self.block.brace_token.surround(tokens, |tokens| {
1445 tokens.append_all(self.attrs.inner());
1446 tokens.append_all(&self.block.stmts);
1447 });
1448 }
1449 }
1450
1451 impl ToTokens for ItemMod {
1452 fn to_tokens(&self, tokens: &mut Tokens) {
1453 tokens.append_all(self.attrs.outer());
1454 self.vis.to_tokens(tokens);
1455 self.mod_token.to_tokens(tokens);
1456 self.ident.to_tokens(tokens);
1457 if let Some((ref brace, ref items)) = self.content {
1458 brace.surround(tokens, |tokens| {
1459 tokens.append_all(self.attrs.inner());
1460 tokens.append_all(items);
1461 });
1462 } else {
1463 TokensOrDefault(&self.semi).to_tokens(tokens);
1464 }
1465 }
1466 }
1467
1468 impl ToTokens for ItemForeignMod {
1469 fn to_tokens(&self, tokens: &mut Tokens) {
1470 tokens.append_all(self.attrs.outer());
1471 self.abi.to_tokens(tokens);
1472 self.brace_token.surround(tokens, |tokens| {
1473 tokens.append_all(&self.items);
1474 });
1475 }
1476 }
1477
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001478 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001479 fn to_tokens(&self, tokens: &mut Tokens) {
1480 tokens.append_all(self.attrs.outer());
1481 self.vis.to_tokens(tokens);
1482 self.type_token.to_tokens(tokens);
1483 self.ident.to_tokens(tokens);
1484 self.generics.to_tokens(tokens);
1485 self.generics.where_clause.to_tokens(tokens);
1486 self.eq_token.to_tokens(tokens);
1487 self.ty.to_tokens(tokens);
1488 self.semi_token.to_tokens(tokens);
1489 }
1490 }
1491
1492 impl ToTokens for ItemEnum {
1493 fn to_tokens(&self, tokens: &mut Tokens) {
1494 tokens.append_all(self.attrs.outer());
1495 self.vis.to_tokens(tokens);
1496 self.enum_token.to_tokens(tokens);
1497 self.ident.to_tokens(tokens);
1498 self.generics.to_tokens(tokens);
1499 self.generics.where_clause.to_tokens(tokens);
1500 self.brace_token.surround(tokens, |tokens| {
1501 self.variants.to_tokens(tokens);
1502 });
1503 }
1504 }
1505
1506 impl ToTokens for ItemStruct {
1507 fn to_tokens(&self, tokens: &mut Tokens) {
1508 tokens.append_all(self.attrs.outer());
1509 self.vis.to_tokens(tokens);
1510 self.struct_token.to_tokens(tokens);
1511 self.ident.to_tokens(tokens);
1512 self.generics.to_tokens(tokens);
1513 match self.data {
1514 VariantData::Struct(..) => {
1515 self.generics.where_clause.to_tokens(tokens);
1516 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001517 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001518 VariantData::Tuple(..) => {
1519 self.data.to_tokens(tokens);
1520 self.generics.where_clause.to_tokens(tokens);
1521 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001522 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001523 VariantData::Unit => {
1524 self.generics.where_clause.to_tokens(tokens);
1525 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001526 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001527 }
1528 }
1529 }
1530
1531 impl ToTokens for ItemUnion {
1532 fn to_tokens(&self, tokens: &mut Tokens) {
1533 tokens.append_all(self.attrs.outer());
1534 self.vis.to_tokens(tokens);
1535 self.union_token.to_tokens(tokens);
1536 self.ident.to_tokens(tokens);
1537 self.generics.to_tokens(tokens);
1538 self.generics.where_clause.to_tokens(tokens);
1539 // XXX: Should we handle / complain when using a
1540 // non-VariantData::Struct Variant in Union?
1541 self.data.to_tokens(tokens);
1542 }
1543 }
1544
1545 impl ToTokens for ItemTrait {
1546 fn to_tokens(&self, tokens: &mut Tokens) {
1547 tokens.append_all(self.attrs.outer());
1548 self.vis.to_tokens(tokens);
1549 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001550 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001551 self.trait_token.to_tokens(tokens);
1552 self.ident.to_tokens(tokens);
1553 self.generics.to_tokens(tokens);
1554 if !self.supertraits.is_empty() {
1555 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1556 self.supertraits.to_tokens(tokens);
1557 }
1558 self.generics.where_clause.to_tokens(tokens);
1559 self.brace_token.surround(tokens, |tokens| {
1560 tokens.append_all(&self.items);
1561 });
1562 }
1563 }
1564
David Tolnay1bfa7332017-11-11 12:41:20 -08001565 impl ToTokens for ItemImpl {
1566 fn to_tokens(&self, tokens: &mut Tokens) {
1567 tokens.append_all(self.attrs.outer());
1568 self.defaultness.to_tokens(tokens);
1569 self.unsafety.to_tokens(tokens);
1570 self.impl_token.to_tokens(tokens);
1571 self.generics.to_tokens(tokens);
1572 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1573 polarity.to_tokens(tokens);
1574 path.to_tokens(tokens);
1575 for_token.to_tokens(tokens);
1576 }
1577 self.self_ty.to_tokens(tokens);
1578 self.generics.where_clause.to_tokens(tokens);
1579 self.brace_token.surround(tokens, |tokens| {
1580 tokens.append_all(&self.items);
1581 });
1582 }
1583 }
1584
1585 impl ToTokens for ItemMacro {
1586 fn to_tokens(&self, tokens: &mut Tokens) {
1587 tokens.append_all(self.attrs.outer());
1588 self.mac.path.to_tokens(tokens);
1589 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001590 self.ident.to_tokens(tokens);
David Tolnayfe9d2782017-12-29 11:32:42 -05001591 self.mac.tt.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001592 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001593 }
1594 }
David Tolnay42602292016-10-01 22:25:45 -07001595
David Tolnay500d8322017-12-18 00:32:51 -08001596 impl ToTokens for ItemMacro2 {
1597 fn to_tokens(&self, tokens: &mut Tokens) {
1598 tokens.append_all(self.attrs.outer());
1599 self.vis.to_tokens(tokens);
1600 self.macro_token.to_tokens(tokens);
1601 self.ident.to_tokens(tokens);
1602 self.args.to_tokens(tokens);
1603 self.body.to_tokens(tokens);
1604 }
1605 }
1606
David Tolnay2ae520a2017-12-29 11:19:50 -05001607 impl ToTokens for ItemVerbatim {
1608 fn to_tokens(&self, tokens: &mut Tokens) {
1609 self.tts.to_tokens(tokens);
1610 }
1611 }
1612
David Tolnay5f332a92017-12-26 00:42:45 -05001613 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001614 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001615 self.ident.to_tokens(tokens);
1616 if let Some((ref as_token, ref rename)) = self.rename {
1617 as_token.to_tokens(tokens);
1618 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001619 }
David Tolnay4a057422016-10-08 00:02:31 -07001620 }
1621 }
1622
David Tolnay5f332a92017-12-26 00:42:45 -05001623 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001624 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001625 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001626 }
1627 }
1628
David Tolnay5f332a92017-12-26 00:42:45 -05001629 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001630 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001631 self.brace_token.surround(tokens, |tokens| {
1632 self.items.to_tokens(tokens);
1633 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001634 }
1635 }
1636
David Tolnay1bfa7332017-11-11 12:41:20 -08001637 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001638 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001639 tokens.append_all(self.attrs.outer());
1640 self.const_token.to_tokens(tokens);
1641 self.ident.to_tokens(tokens);
1642 self.colon_token.to_tokens(tokens);
1643 self.ty.to_tokens(tokens);
1644 if let Some((ref eq_token, ref default)) = self.default {
1645 eq_token.to_tokens(tokens);
1646 default.to_tokens(tokens);
1647 }
1648 self.semi_token.to_tokens(tokens);
1649 }
1650 }
1651
1652 impl ToTokens for TraitItemMethod {
1653 fn to_tokens(&self, tokens: &mut Tokens) {
1654 tokens.append_all(self.attrs.outer());
1655 self.sig.to_tokens(tokens);
1656 match self.default {
1657 Some(ref block) => {
1658 block.brace_token.surround(tokens, |tokens| {
1659 tokens.append_all(self.attrs.inner());
1660 tokens.append_all(&block.stmts);
1661 });
David Tolnayca085422016-10-04 00:12:38 -07001662 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001663 None => {
1664 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001665 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001666 }
1667 }
1668 }
1669
1670 impl ToTokens for TraitItemType {
1671 fn to_tokens(&self, tokens: &mut Tokens) {
1672 tokens.append_all(self.attrs.outer());
1673 self.type_token.to_tokens(tokens);
1674 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001675 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001676 if !self.bounds.is_empty() {
1677 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1678 self.bounds.to_tokens(tokens);
1679 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001680 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001681 if let Some((ref eq_token, ref default)) = self.default {
1682 eq_token.to_tokens(tokens);
1683 default.to_tokens(tokens);
1684 }
1685 self.semi_token.to_tokens(tokens);
1686 }
1687 }
1688
1689 impl ToTokens for TraitItemMacro {
1690 fn to_tokens(&self, tokens: &mut Tokens) {
1691 tokens.append_all(self.attrs.outer());
1692 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001693 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001694 }
1695 }
1696
David Tolnay2ae520a2017-12-29 11:19:50 -05001697 impl ToTokens for TraitItemVerbatim {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 self.tts.to_tokens(tokens);
1700 }
1701 }
1702
David Tolnay857628c2017-11-11 12:25:31 -08001703 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001704 fn to_tokens(&self, tokens: &mut Tokens) {
1705 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001706 self.vis.to_tokens(tokens);
1707 self.defaultness.to_tokens(tokens);
1708 self.const_token.to_tokens(tokens);
1709 self.ident.to_tokens(tokens);
1710 self.colon_token.to_tokens(tokens);
1711 self.ty.to_tokens(tokens);
1712 self.eq_token.to_tokens(tokens);
1713 self.expr.to_tokens(tokens);
1714 self.semi_token.to_tokens(tokens);
1715 }
1716 }
1717
1718 impl ToTokens for ImplItemMethod {
1719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 tokens.append_all(self.attrs.outer());
1721 self.vis.to_tokens(tokens);
1722 self.defaultness.to_tokens(tokens);
1723 self.sig.to_tokens(tokens);
1724 self.block.brace_token.surround(tokens, |tokens| {
1725 tokens.append_all(self.attrs.inner());
1726 tokens.append_all(&self.block.stmts);
1727 });
1728 }
1729 }
1730
1731 impl ToTokens for ImplItemType {
1732 fn to_tokens(&self, tokens: &mut Tokens) {
1733 tokens.append_all(self.attrs.outer());
1734 self.vis.to_tokens(tokens);
1735 self.defaultness.to_tokens(tokens);
1736 self.type_token.to_tokens(tokens);
1737 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001738 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001739 self.eq_token.to_tokens(tokens);
1740 self.ty.to_tokens(tokens);
1741 self.semi_token.to_tokens(tokens);
1742 }
1743 }
1744
1745 impl ToTokens for ImplItemMacro {
1746 fn to_tokens(&self, tokens: &mut Tokens) {
1747 tokens.append_all(self.attrs.outer());
1748 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001749 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001750 }
1751 }
1752
David Tolnay2ae520a2017-12-29 11:19:50 -05001753 impl ToTokens for ImplItemVerbatim {
1754 fn to_tokens(&self, tokens: &mut Tokens) {
1755 self.tts.to_tokens(tokens);
1756 }
1757 }
1758
David Tolnay8894f602017-11-11 12:11:04 -08001759 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001760 fn to_tokens(&self, tokens: &mut Tokens) {
1761 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001762 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001763 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1764 self.semi_token.to_tokens(tokens);
1765 }
1766 }
1767
1768 impl ToTokens for ForeignItemStatic {
1769 fn to_tokens(&self, tokens: &mut Tokens) {
1770 tokens.append_all(self.attrs.outer());
1771 self.vis.to_tokens(tokens);
1772 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001773 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001774 self.ident.to_tokens(tokens);
1775 self.colon_token.to_tokens(tokens);
1776 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001777 self.semi_token.to_tokens(tokens);
1778 }
1779 }
1780
David Tolnay199bcbb2017-11-12 10:33:52 -08001781 impl ToTokens for ForeignItemType {
1782 fn to_tokens(&self, tokens: &mut Tokens) {
1783 tokens.append_all(self.attrs.outer());
1784 self.vis.to_tokens(tokens);
1785 self.type_token.to_tokens(tokens);
1786 self.ident.to_tokens(tokens);
1787 self.semi_token.to_tokens(tokens);
1788 }
1789 }
1790
David Tolnay2ae520a2017-12-29 11:19:50 -05001791 impl ToTokens for ForeignItemVerbatim {
1792 fn to_tokens(&self, tokens: &mut Tokens) {
1793 self.tts.to_tokens(tokens);
1794 }
1795 }
1796
David Tolnay570695e2017-06-03 16:15:13 -07001797 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001798 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001799 self.constness.to_tokens(tokens);
1800 self.unsafety.to_tokens(tokens);
1801 self.abi.to_tokens(tokens);
1802 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001803 }
1804 }
1805
David Tolnay570695e2017-06-03 16:15:13 -07001806 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001807
1808 impl<'a> ToTokens for NamedDecl<'a> {
1809 fn to_tokens(&self, tokens: &mut Tokens) {
1810 self.0.fn_token.to_tokens(tokens);
1811 self.1.to_tokens(tokens);
1812 self.0.generics.to_tokens(tokens);
1813 self.0.paren_token.surround(tokens, |tokens| {
1814 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001815 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1816 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001817 }
David Tolnayd2836e22017-12-27 23:13:00 -05001818 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001819 });
1820 self.0.output.to_tokens(tokens);
1821 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001822 }
1823 }
1824
Alex Crichton62a0a592017-05-22 13:58:53 -07001825 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001826 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001827 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001828 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001829 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001830 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001831 }
1832 }
1833
1834 impl ToTokens for ArgSelf {
1835 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001836 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001837 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001838 }
1839 }
1840
1841 impl ToTokens for ArgCaptured {
1842 fn to_tokens(&self, tokens: &mut Tokens) {
1843 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001844 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001845 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001846 }
1847 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001848}