blob: 57c9400cd144d7e72e2c5225191e22944eda566e [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay9c76bcb2017-12-26 23:14:59 -05003use proc_macro2::TokenTree;
4
5#[cfg(feature = "extra-traits")]
6use mac::TokenTreeHelper;
7#[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],
Alex Crichton62a0a592017-05-22 13:58:53 -070044 pub mutbl: Mutability,
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,
Alex Crichton62a0a592017-05-22 13:58:53 -070072 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -070073 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070075 pub decl: Box<FnDecl>,
76 pub ident: Ident,
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,
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 pub unsafety: Unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500154 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700156 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800159 pub supertraits: Delimited<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500160 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 pub items: Vec<TraitItem>,
162 }),
163 /// Default trait implementation.
164 ///
165 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
166 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800167 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800169 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700170 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub for_token: Token![for],
172 pub dot2_token: Token![..],
David Tolnay32954ef2017-12-26 22:43:16 -0500173 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
175 /// An implementation.
176 ///
177 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
178 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800179 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700180 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800182 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700184 /// Trait this impl implements.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub trait_: Option<(ImplPolarity, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700186 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800187 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500188 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 pub items: Vec<ImplItem>,
190 }),
191 /// A macro invocation (which includes macro definition).
192 ///
193 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800194 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800195 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800196 /// The `example` in `macro_rules! example { ... }`.
197 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800198 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500199 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800200 }),
David Tolnay500d8322017-12-18 00:32:51 -0800201 /// FIXME will need to revisit what this looks like in the AST.
David Tolnay9c76bcb2017-12-26 23:14:59 -0500202 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800203 pub attrs: Vec<Attribute>,
204 pub vis: Visibility,
205 pub macro_token: Token![macro],
206 pub ident: Ident,
207 pub args: TokenTree,
208 pub body: TokenTree,
209 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700210 }
David Tolnayb79ee962016-09-04 09:39:20 -0700211}
212
David Tolnay9c76bcb2017-12-26 23:14:59 -0500213#[cfg(feature = "extra-traits")]
214impl Eq for ItemMacro2 {}
215
216#[cfg(feature = "extra-traits")]
217impl PartialEq for ItemMacro2 {
218 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500219 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnay9c76bcb2017-12-26 23:14:59 -0500220 && self.ident == other.ident
221 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
222 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
223 }
224}
225
226#[cfg(feature = "extra-traits")]
227impl Hash for ItemMacro2 {
228 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500229 where
230 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500231 {
232 self.attrs.hash(state);
233 self.vis.hash(state);
234 self.macro_token.hash(state);
235 self.ident.hash(state);
236 TokenTreeHelper(&self.args).hash(state);
237 TokenTreeHelper(&self.body).hash(state);
238 }
239}
240
David Tolnay0e837402016-12-22 17:25:55 -0500241impl From<DeriveInput> for Item {
242 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800243 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500244 Body::Enum(data) => Item::Enum(ItemEnum {
245 attrs: input.attrs,
246 vis: input.vis,
247 enum_token: data.enum_token,
248 ident: input.ident,
249 generics: input.generics,
250 brace_token: data.brace_token,
251 variants: data.variants,
252 }),
253 Body::Struct(data) => Item::Struct(ItemStruct {
254 attrs: input.attrs,
255 vis: input.vis,
256 struct_token: data.struct_token,
257 ident: input.ident,
258 generics: input.generics,
259 data: data.data,
260 semi_token: data.semi_token,
261 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700262 }
263 }
264}
265
Alex Crichton62a0a592017-05-22 13:58:53 -0700266ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500267 /// Things that can appear directly inside of a module.
268 pub enum UseTree {
269 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
270 pub Path(UsePath {
271 pub ident: Ident,
272 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500274 /// `use prefix::*`
275 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800276 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500278 /// `use prefix::{a, b, c}`
279 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500280 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500281 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 }),
283 }
284}
285
Alex Crichton62a0a592017-05-22 13:58:53 -0700286ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700287 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800289 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 NotConst,
291 }
292}
293
294ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700295 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 pub enum Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800297 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 Final,
299 }
300}
301
Alex Crichton62a0a592017-05-22 13:58:53 -0700302ast_enum_of_structs! {
303 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800304 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A foreign function
306 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800307 pub attrs: Vec<Attribute>,
308 pub vis: Visibility,
309 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700310 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800311 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700312 }),
313 /// A foreign static item (`static ext: u8`)
314 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800315 pub attrs: Vec<Attribute>,
316 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800317 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800319 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800320 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800321 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800322 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800324 /// A foreign type
325 pub Type(ForeignItemType {
326 pub attrs: Vec<Attribute>,
327 pub vis: Visibility,
328 pub type_token: Token![type],
329 pub ident: Ident,
330 pub semi_token: Token![;],
331 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700333}
334
David Tolnayda705bd2017-11-10 21:58:05 -0800335ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 /// Represents an item declaration within a trait declaration,
337 /// possibly including a default implementation. A trait item is
338 /// either required (meaning it doesn't have an implementation, just a
339 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800340 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800342 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800343 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700344 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800345 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800346 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 pub default: Option<(Token![=], Expr)>,
348 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 }),
350 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800351 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 pub sig: MethodSig,
353 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800354 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 }),
356 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800357 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700359 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500360 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800361 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800362 pub bounds: Delimited<TypeParamBound, Token![+]>,
363 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700365 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800366 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800367 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800368 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500369 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800370 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700372}
373
374ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700375 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 pub enum ImplPolarity {
377 /// `impl Trait for Type`
378 Positive,
379 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800380 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }
382}
383
Alex Crichton62a0a592017-05-22 13:58:53 -0700384ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800385 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800387 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700388 pub vis: Visibility,
389 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800390 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700391 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800392 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800393 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800396 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }),
398 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800399 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700400 pub vis: Visibility,
401 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub sig: MethodSig,
403 pub block: Block,
404 }),
405 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800406 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700407 pub vis: Visibility,
408 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800409 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700410 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500411 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800413 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 }),
David Tolnay857628c2017-11-11 12:25:31 -0800416 pub Macro(ImplItemMacro {
417 pub attrs: Vec<Attribute>,
418 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500419 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800420 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700422}
423
424ast_struct! {
425 /// Represents a method's signature in a trait declaration,
426 /// or in an implementation.
427 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700428 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700429 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700431 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 }
434}
435
436ast_struct! {
437 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700438 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 /// E.g. `fn foo(bar: baz)`
440 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub fn_token: Token![fn],
David Tolnay32954ef2017-12-26 22:43:16 -0500442 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800444 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700445 pub generics: Generics,
David Tolnayd2836e22017-12-27 23:13:00 -0500446 pub variadic: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700447 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700448}
449
Alex Crichton62a0a592017-05-22 13:58:53 -0700450ast_enum_of_structs! {
451 /// An argument in a function header.
452 ///
453 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
454 pub enum FnArg {
455 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800456 pub and_token: Token![&],
457 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700458 pub lifetime: Option<Lifetime>,
459 pub mutbl: Mutability,
460 }),
461 pub SelfValue(ArgSelf {
462 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800463 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 }),
465 pub Captured(ArgCaptured {
466 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800467 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800468 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500470 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700472 }
David Tolnay62f374c2016-10-02 13:37:00 -0700473}
474
David Tolnayedf2b992016-09-23 20:43:45 -0700475#[cfg(feature = "parsing")]
476pub mod parsing {
477 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700478
Michael Layzell92639a52017-06-01 00:07:44 -0400479 use synom::Synom;
David Tolnay57292da2017-12-27 21:03:33 -0500480 use proc_macro2::{TokenNode, Delimiter};
David Tolnay84aa0752016-10-02 23:01:13 -0700481
David Tolnay4c614be2017-11-10 00:02:38 -0800482 impl_synom!(Item "item" alt!(
483 syn!(ItemExternCrate) => { Item::ExternCrate }
484 |
485 syn!(ItemUse) => { Item::Use }
486 |
487 syn!(ItemStatic) => { Item::Static }
488 |
489 syn!(ItemConst) => { Item::Const }
490 |
491 syn!(ItemFn) => { Item::Fn }
492 |
493 syn!(ItemMod) => { Item::Mod }
494 |
495 syn!(ItemForeignMod) => { Item::ForeignMod }
496 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800497 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800498 |
499 syn!(ItemStruct) => { Item::Struct }
500 |
501 syn!(ItemEnum) => { Item::Enum }
502 |
503 syn!(ItemUnion) => { Item::Union }
504 |
505 syn!(ItemTrait) => { Item::Trait }
506 |
507 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
508 |
509 syn!(ItemImpl) => { Item::Impl }
510 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800511 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800512 |
513 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800514 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700515
David Tolnaydecf28d2017-11-11 11:56:45 -0800516 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500517 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700518 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800519 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700520 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500521 body: call!(tt::delimited) >>
David Tolnay57292da2017-12-27 21:03:33 -0500522 semi: cond!(!is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800523 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700524 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800525 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800526 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500527 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700528 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500529 tokens: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800530 },
David Tolnay57292da2017-12-27 21:03:33 -0500531 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800532 })
David Tolnayedf2b992016-09-23 20:43:45 -0700533 ));
534
David Tolnay500d8322017-12-18 00:32:51 -0800535 // TODO: figure out the actual grammar; is body required to be braced?
536 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500537 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800538 vis: syn!(Visibility) >>
539 macro_: keyword!(macro) >>
540 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500541 args: call!(tt::parenthesized) >>
542 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800543 (ItemMacro2 {
544 attrs: attrs,
545 vis: vis,
546 macro_token: macro_,
547 ident: ident,
548 args: args,
549 body: body,
550 })
551 ));
552
David Tolnay4c614be2017-11-10 00:02:38 -0800553 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500554 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700555 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800556 extern_: keyword!(extern) >>
557 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700558 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800559 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
560 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800561 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700562 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800563 vis: vis,
564 extern_token: extern_,
565 crate_token: crate_,
566 ident: ident,
567 rename: rename,
568 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800569 })
David Tolnayedf2b992016-09-23 20:43:45 -0700570 ));
571
David Tolnay4c614be2017-11-10 00:02:38 -0800572 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500573 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700574 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800575 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500576 leading_colon: option!(punct!(::)) >>
577 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
578 tree: switch!(value!(leading_colon.is_none() && prefix.is_empty()),
579 true => syn!(UseTree)
580 |
581 false => switch!(value!(prefix.empty_or_trailing()),
582 true => syn!(UseTree)
583 |
584 false => alt!(
585 tuple!(keyword!(as), syn!(Ident)) => {
586 |rename| UseTree::Path(UsePath {
587 ident: prefix.pop().unwrap().into_item(),
588 rename: Some(rename),
589 })
590 }
591 |
592 epsilon!() => {
593 |_| UseTree::Path(UsePath {
594 ident: prefix.pop().unwrap().into_item(),
595 rename: None,
596 })
597 }
598 )
599 )
600 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800601 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800602 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700603 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800604 vis: vis,
605 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500606 leading_colon: leading_colon,
607 prefix: prefix,
608 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800609 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800610 })
David Tolnay4a057422016-10-08 00:02:31 -0700611 ));
612
David Tolnay5f332a92017-12-26 00:42:45 -0500613 named!(use_prefix -> Ident, alt!(
614 syn!(Ident)
615 |
616 keyword!(self) => { Into::into }
617 |
618 keyword!(super) => { Into::into }
619 |
620 keyword!(crate) => { Into::into }
621 ));
622
623 impl_synom!(UseTree "use tree" alt!(
624 syn!(UsePath) => { UseTree::Path }
625 |
626 syn!(UseGlob) => { UseTree::Glob }
627 |
628 syn!(UseList) => { UseTree::List }
629 ));
630
631 impl_synom!(UsePath "use path" do_parse!(
632 ident: alt!(
633 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400634 |
David Tolnay5f332a92017-12-26 00:42:45 -0500635 keyword!(self) => { Into::into }
636 ) >>
637 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
638 (UsePath {
639 ident: ident,
640 rename: rename,
641 })
642 ));
David Tolnay4a057422016-10-08 00:02:31 -0700643
David Tolnay5f332a92017-12-26 00:42:45 -0500644 impl_synom!(UseGlob "use glob" do_parse!(
645 star: punct!(*) >>
646 (UseGlob {
647 star_token: star,
648 })
649 ));
David Tolnay4a057422016-10-08 00:02:31 -0700650
David Tolnay5f332a92017-12-26 00:42:45 -0500651 impl_synom!(UseList "use list" do_parse!(
652 list: braces!(Delimited::parse_terminated) >>
653 (UseList {
654 brace_token: list.1,
655 items: list.0,
656 })
657 ));
David Tolnay4a057422016-10-08 00:02:31 -0700658
David Tolnay4c614be2017-11-10 00:02:38 -0800659 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500660 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700661 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700663 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700664 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800666 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800667 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700668 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800669 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800670 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700671 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800672 vis: vis,
673 static_token: static_,
674 mutbl: mutability,
675 ident: ident,
676 colon_token: colon,
677 ty: Box::new(ty),
678 eq_token: eq,
679 expr: Box::new(value),
680 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800681 })
David Tolnay47a877c2016-10-01 16:50:55 -0700682 ));
683
David Tolnay4c614be2017-11-10 00:02:38 -0800684 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500685 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700686 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800687 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700688 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800689 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800690 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800691 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700692 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800693 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800694 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700695 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800696 vis: vis,
697 const_token: const_,
698 ident: ident,
699 colon_token: colon,
700 ty: Box::new(ty),
701 eq_token: eq,
702 expr: Box::new(value),
703 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800704 })
David Tolnay47a877c2016-10-01 16:50:55 -0700705 ));
706
David Tolnay4c614be2017-11-10 00:02:38 -0800707 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500708 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700709 vis: syn!(Visibility) >>
710 constness: syn!(Constness) >>
711 unsafety: syn!(Unsafety) >>
712 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800713 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700714 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700715 generics: syn!(Generics) >>
716 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800717 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700718 where_clause: syn!(WhereClause) >>
719 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500720 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700721 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400722 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800723 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700724 attrs: {
725 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700726 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700727 attrs
728 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800729 vis: vis,
730 constness: constness,
731 unsafety: unsafety,
732 abi: abi,
733 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800734 fn_token: fn_,
735 paren_token: inputs.1,
736 inputs: inputs.0,
737 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500738 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800739 generics: Generics {
740 where_clause: where_clause,
741 .. generics
742 },
743 }),
744 ident: ident,
745 block: Box::new(Block {
746 brace_token: inner_attrs_stmts.1,
747 stmts: (inner_attrs_stmts.0).1,
748 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800749 })
David Tolnay42602292016-10-01 22:25:45 -0700750 ));
751
Alex Crichton954046c2017-05-30 21:49:42 -0700752 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400753 named!(parse -> Self, alt!(
754 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800755 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400756 lt: option!(syn!(Lifetime)) >>
757 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800758 self_: keyword!(self) >>
759 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400760 (ArgSelfRef {
761 lifetime: lt,
762 mutbl: mutability,
763 and_token: and,
764 self_token: self_,
765 }.into())
766 )
767 |
768 do_parse!(
769 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800770 self_: keyword!(self) >>
771 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400772 (ArgSelf {
773 mutbl: mutability,
774 self_token: self_,
775 }.into())
776 )
777 |
778 do_parse!(
779 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800780 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800781 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400782 (ArgCaptured {
783 pat: pat,
784 ty: ty,
785 colon_token: colon,
786 }.into())
787 )
788 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800789 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400790 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700791 }
David Tolnay62f374c2016-10-02 13:37:00 -0700792
David Tolnay4c614be2017-11-10 00:02:38 -0800793 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500794 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700795 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800796 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700797 ident: syn!(Ident) >>
798 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800799 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700800 Vec::new(),
801 None,
802 Some(semi),
803 )}
David Tolnay37d10332016-10-13 20:51:04 -0700804 |
Alex Crichton954046c2017-05-30 21:49:42 -0700805 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700806 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500807 many0!(Attribute::parse_inner),
808 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400809 )
David Tolnay570695e2017-06-03 16:15:13 -0700810 ) => {|((inner_attrs, items), brace)| (
811 inner_attrs,
812 Some((brace, items)),
813 None,
814 )}
David Tolnay37d10332016-10-13 20:51:04 -0700815 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800816 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700817 attrs: {
818 let mut attrs = outer_attrs;
819 attrs.extend(content_semi.0);
820 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700821 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800822 vis: vis,
823 mod_token: mod_,
824 ident: ident,
825 content: content_semi.1,
826 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800827 })
David Tolnay35902302016-10-06 01:11:08 -0700828 ));
829
David Tolnay4c614be2017-11-10 00:02:38 -0800830 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500831 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700832 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500833 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800834 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700835 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800836 abi: abi,
837 brace_token: items.1,
838 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800839 })
David Tolnay35902302016-10-06 01:11:08 -0700840 ));
841
David Tolnay8894f602017-11-11 12:11:04 -0800842 impl_synom!(ForeignItem "foreign item" alt!(
843 syn!(ForeignItemFn) => { ForeignItem::Fn }
844 |
845 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800846 |
847 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800848 ));
David Tolnay35902302016-10-06 01:11:08 -0700849
David Tolnay8894f602017-11-11 12:11:04 -0800850 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500851 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700852 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800853 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700854 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700855 generics: syn!(Generics) >>
856 inputs: parens!(do_parse!(
857 args: call!(Delimited::parse_terminated) >>
858 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800859 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700860 (args, variadic)
861 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800862 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700863 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800864 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700865 ({
866 let ((inputs, variadic), parens) = inputs;
867 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800868 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700869 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700870 attrs: attrs,
871 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800872 decl: Box::new(FnDecl {
873 fn_token: fn_,
874 paren_token: parens,
875 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500876 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800877 output: ret,
878 generics: Generics {
879 where_clause: where_clause,
880 .. generics
881 },
882 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700883 vis: vis,
884 }
David Tolnay35902302016-10-06 01:11:08 -0700885 })
886 ));
887
David Tolnay8894f602017-11-11 12:11:04 -0800888 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500889 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700890 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800891 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700892 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700893 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800894 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800895 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800896 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800897 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700898 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700899 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700900 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800901 ty: Box::new(ty),
902 mutbl: mutability,
903 static_token: static_,
904 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700905 vis: vis,
906 })
907 ));
908
David Tolnay199bcbb2017-11-12 10:33:52 -0800909 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500910 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800911 vis: syn!(Visibility) >>
912 type_: keyword!(type) >>
913 ident: syn!(Ident) >>
914 semi: punct!(;) >>
915 (ForeignItemType {
916 attrs: attrs,
917 vis: vis,
918 type_token: type_,
919 ident: ident,
920 semi_token: semi,
921 })
922 ));
923
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800924 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500925 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700926 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800927 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700928 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700929 generics: syn!(Generics) >>
930 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800931 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800932 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800934 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700935 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800936 vis: vis,
937 type_token: type_,
938 ident: ident,
939 generics: Generics {
940 where_clause: where_clause,
941 ..generics
942 },
943 eq_token: eq,
944 ty: Box::new(ty),
945 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800946 })
David Tolnay3cf52982016-10-01 17:11:37 -0700947 ));
948
David Tolnay4c614be2017-11-10 00:02:38 -0800949 impl_synom!(ItemStruct "struct item" switch!(
950 map!(syn!(DeriveInput), Into::into),
951 Item::Struct(item) => value!(item)
952 |
953 _ => reject!()
954 ));
David Tolnay42602292016-10-01 22:25:45 -0700955
David Tolnay4c614be2017-11-10 00:02:38 -0800956 impl_synom!(ItemEnum "enum item" switch!(
957 map!(syn!(DeriveInput), Into::into),
958 Item::Enum(item) => value!(item)
959 |
960 _ => reject!()
961 ));
962
963 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500964 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700965 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800966 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700967 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700968 generics: syn!(Generics) >>
969 where_clause: syn!(WhereClause) >>
970 fields: braces!(call!(Delimited::parse_terminated_with,
971 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800972 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700973 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800974 vis: vis,
975 union_token: union_,
976 ident: ident,
977 generics: Generics {
978 where_clause: where_clause,
979 .. generics
980 },
981 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800982 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700983 ));
984
David Tolnay4c614be2017-11-10 00:02:38 -0800985 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500986 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700987 vis: syn!(Visibility) >>
988 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500989 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700991 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700992 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800993 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700994 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700995 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700996 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700997 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -0500998 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800999 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001000 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001001 vis: vis,
1002 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001003 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001004 trait_token: trait_,
1005 ident: ident,
1006 generics: Generics {
1007 where_clause: where_clause,
1008 .. generics
1009 },
1010 colon_token: colon,
1011 supertraits: bounds.unwrap_or_default(),
1012 brace_token: body.1,
1013 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001014 })
David Tolnay0aecb732016-10-03 23:03:50 -07001015 ));
1016
David Tolnay4c614be2017-11-10 00:02:38 -08001017 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001018 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001019 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001021 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 for_: keyword!(for) >>
1023 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001024 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001025 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001026 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001027 unsafety: unsafety,
1028 impl_token: impl_,
1029 path: path,
1030 for_token: for_,
1031 dot2_token: dot2,
1032 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001033 })
David Tolnayf94e2362016-10-04 00:29:51 -07001034 ));
1035
David Tolnayda705bd2017-11-10 21:58:05 -08001036 impl_synom!(TraitItem "trait item" alt!(
1037 syn!(TraitItemConst) => { TraitItem::Const }
1038 |
1039 syn!(TraitItemMethod) => { TraitItem::Method }
1040 |
1041 syn!(TraitItemType) => { TraitItem::Type }
1042 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001043 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001044 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001045
David Tolnayda705bd2017-11-10 21:58:05 -08001046 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001047 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001048 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001049 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001051 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001052 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1053 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001054 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001055 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001056 const_token: const_,
1057 ident: ident,
1058 colon_token: colon,
1059 ty: ty,
1060 default: default,
1061 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001062 })
1063 ));
1064
David Tolnayda705bd2017-11-10 21:58:05 -08001065 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001066 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001067 constness: syn!(Constness) >>
1068 unsafety: syn!(Unsafety) >>
1069 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001070 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001071 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001072 generics: syn!(Generics) >>
1073 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001074 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001075 where_clause: syn!(WhereClause) >>
1076 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001077 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001078 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001079 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001080 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001081 ({
1082 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001083 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001084 None => (Vec::new(), None),
1085 };
David Tolnayda705bd2017-11-10 21:58:05 -08001086 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001087 attrs: {
1088 let mut attrs = outer_attrs;
1089 attrs.extend(inner_attrs);
1090 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001091 },
David Tolnayda705bd2017-11-10 21:58:05 -08001092 sig: MethodSig {
1093 constness: constness,
1094 unsafety: unsafety,
1095 abi: abi,
1096 ident: ident,
1097 decl: FnDecl {
1098 inputs: inputs.0,
1099 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001100 fn_token: fn_,
1101 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001102 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001103 generics: Generics {
1104 where_clause: where_clause,
1105 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001106 },
1107 },
David Tolnayda705bd2017-11-10 21:58:05 -08001108 },
1109 default: stmts.map(|stmts| {
1110 Block {
1111 stmts: stmts.0,
1112 brace_token: stmts.1,
1113 }
1114 }),
1115 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001116 }
David Tolnay0aecb732016-10-03 23:03:50 -07001117 })
1118 ));
1119
David Tolnayda705bd2017-11-10 21:58:05 -08001120 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001121 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001122 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001123 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001124 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001125 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001126 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001127 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001128 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001129 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001130 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001131 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001132 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001133 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001134 type_token: type_,
1135 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001136 generics: Generics {
1137 where_clause: where_clause,
1138 .. generics
1139 },
David Tolnayda705bd2017-11-10 21:58:05 -08001140 colon_token: colon,
1141 bounds: bounds.unwrap_or_default(),
1142 default: default,
1143 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001144 })
1145 ));
1146
David Tolnaydecf28d2017-11-11 11:56:45 -08001147 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001148 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001149 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001150 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001151 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001152 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001153 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001154 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001155 })
1156 ));
1157
David Tolnay4c614be2017-11-10 00:02:38 -08001158 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001159 attrs: many0!(Attribute::parse_outer) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001160 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001161 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001162 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001163 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001164 polarity_path: alt!(
1165 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001166 polarity: syn!(ImplPolarity) >>
1167 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001168 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001169 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001170 )
1171 |
David Tolnay570695e2017-06-03 16:15:13 -07001172 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001173 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001174 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001175 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -05001176 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001177 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001178 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001179 defaultness: defaultness,
1180 unsafety: unsafety,
1181 impl_token: impl_,
1182 generics: Generics {
1183 where_clause: where_clause,
1184 .. generics
1185 },
1186 trait_: polarity_path,
1187 self_ty: Box::new(self_ty),
1188 brace_token: body.1,
1189 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001190 })
David Tolnay4c9be372016-10-06 00:47:37 -07001191 ));
1192
David Tolnay857628c2017-11-11 12:25:31 -08001193 impl_synom!(ImplItem "item in impl block" alt!(
1194 syn!(ImplItemConst) => { ImplItem::Const }
1195 |
1196 syn!(ImplItemMethod) => { ImplItem::Method }
1197 |
1198 syn!(ImplItemType) => { ImplItem::Type }
1199 |
1200 syn!(ImplItemMacro) => { ImplItem::Macro }
1201 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001202
David Tolnay857628c2017-11-11 12:25:31 -08001203 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001204 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001205 vis: syn!(Visibility) >>
1206 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001207 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001208 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001209 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001210 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001211 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001212 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001213 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001214 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001215 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001216 vis: vis,
1217 defaultness: defaultness,
1218 const_token: const_,
1219 ident: ident,
1220 colon_token: colon,
1221 ty: ty,
1222 eq_token: eq,
1223 expr: value,
1224 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001225 })
1226 ));
1227
David Tolnay857628c2017-11-11 12:25:31 -08001228 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001229 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001230 vis: syn!(Visibility) >>
1231 defaultness: syn!(Defaultness) >>
1232 constness: syn!(Constness) >>
1233 unsafety: syn!(Unsafety) >>
1234 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001235 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001236 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001237 generics: syn!(Generics) >>
1238 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001239 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001240 where_clause: syn!(WhereClause) >>
1241 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001242 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001243 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001244 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001245 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001246 attrs: {
1247 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001248 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001249 attrs
1250 },
David Tolnay857628c2017-11-11 12:25:31 -08001251 vis: vis,
1252 defaultness: defaultness,
1253 sig: MethodSig {
1254 constness: constness,
1255 unsafety: unsafety,
1256 abi: abi,
1257 ident: ident,
1258 decl: FnDecl {
1259 fn_token: fn_,
1260 paren_token: inputs.1,
1261 inputs: inputs.0,
1262 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001263 generics: Generics {
1264 where_clause: where_clause,
1265 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001266 },
David Tolnayd2836e22017-12-27 23:13:00 -05001267 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001268 },
David Tolnay857628c2017-11-11 12:25:31 -08001269 },
1270 block: Block {
1271 brace_token: inner_attrs_stmts.1,
1272 stmts: (inner_attrs_stmts.0).1,
1273 },
David Tolnay4c9be372016-10-06 00:47:37 -07001274 })
1275 ));
1276
David Tolnay857628c2017-11-11 12:25:31 -08001277 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001278 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001279 vis: syn!(Visibility) >>
1280 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001281 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001282 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001283 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001284 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001285 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001286 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001287 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001288 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001289 vis: vis,
1290 defaultness: defaultness,
1291 type_token: type_,
1292 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001293 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001294 eq_token: eq,
1295 ty: ty,
1296 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001297 })
1298 ));
1299
David Tolnay857628c2017-11-11 12:25:31 -08001300 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001301 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001302 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001303 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001304 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001305 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001306 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001307 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001308 })
1309 ));
1310
Alex Crichton954046c2017-05-30 21:49:42 -07001311 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001312 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001313 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001314 |
1315 epsilon!() => { |_| ImplPolarity::Positive }
1316 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001317 }
David Tolnay4c9be372016-10-06 00:47:37 -07001318
Alex Crichton954046c2017-05-30 21:49:42 -07001319 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001320 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001321 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001322 |
1323 epsilon!() => { |_| Constness::NotConst }
1324 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001325 }
David Tolnay42602292016-10-01 22:25:45 -07001326
Alex Crichton954046c2017-05-30 21:49:42 -07001327 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001328 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001329 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001330 |
1331 epsilon!() => { |_| Defaultness::Final }
1332 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001333 }
David Tolnay57292da2017-12-27 21:03:33 -05001334
1335 fn is_braced(tt: &TokenTree) -> bool {
1336 match tt.kind {
1337 TokenNode::Group(Delimiter::Brace, _) => true,
1338 _ => false,
1339 }
1340 }
David Tolnayedf2b992016-09-23 20:43:45 -07001341}
David Tolnay4a51dc72016-10-01 00:40:31 -07001342
1343#[cfg(feature = "printing")]
1344mod printing {
1345 use super::*;
1346 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001347 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001348 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001349
David Tolnay1bfa7332017-11-11 12:41:20 -08001350 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001351 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001352 tokens.append_all(self.attrs.outer());
1353 self.vis.to_tokens(tokens);
1354 self.extern_token.to_tokens(tokens);
1355 self.crate_token.to_tokens(tokens);
1356 self.ident.to_tokens(tokens);
1357 if let Some((ref as_token, ref rename)) = self.rename {
1358 as_token.to_tokens(tokens);
1359 rename.to_tokens(tokens);
1360 }
1361 self.semi_token.to_tokens(tokens);
1362 }
1363 }
1364
1365 impl ToTokens for ItemUse {
1366 fn to_tokens(&self, tokens: &mut Tokens) {
1367 tokens.append_all(self.attrs.outer());
1368 self.vis.to_tokens(tokens);
1369 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001370 self.leading_colon.to_tokens(tokens);
1371 self.prefix.to_tokens(tokens);
1372 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001373 self.semi_token.to_tokens(tokens);
1374 }
1375 }
1376
1377 impl ToTokens for ItemStatic {
1378 fn to_tokens(&self, tokens: &mut Tokens) {
1379 tokens.append_all(self.attrs.outer());
1380 self.vis.to_tokens(tokens);
1381 self.static_token.to_tokens(tokens);
1382 self.mutbl.to_tokens(tokens);
1383 self.ident.to_tokens(tokens);
1384 self.colon_token.to_tokens(tokens);
1385 self.ty.to_tokens(tokens);
1386 self.eq_token.to_tokens(tokens);
1387 self.expr.to_tokens(tokens);
1388 self.semi_token.to_tokens(tokens);
1389 }
1390 }
1391
1392 impl ToTokens for ItemConst {
1393 fn to_tokens(&self, tokens: &mut Tokens) {
1394 tokens.append_all(self.attrs.outer());
1395 self.vis.to_tokens(tokens);
1396 self.const_token.to_tokens(tokens);
1397 self.ident.to_tokens(tokens);
1398 self.colon_token.to_tokens(tokens);
1399 self.ty.to_tokens(tokens);
1400 self.eq_token.to_tokens(tokens);
1401 self.expr.to_tokens(tokens);
1402 self.semi_token.to_tokens(tokens);
1403 }
1404 }
1405
1406 impl ToTokens for ItemFn {
1407 fn to_tokens(&self, tokens: &mut Tokens) {
1408 tokens.append_all(self.attrs.outer());
1409 self.vis.to_tokens(tokens);
1410 self.constness.to_tokens(tokens);
1411 self.unsafety.to_tokens(tokens);
1412 self.abi.to_tokens(tokens);
1413 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1414 self.block.brace_token.surround(tokens, |tokens| {
1415 tokens.append_all(self.attrs.inner());
1416 tokens.append_all(&self.block.stmts);
1417 });
1418 }
1419 }
1420
1421 impl ToTokens for ItemMod {
1422 fn to_tokens(&self, tokens: &mut Tokens) {
1423 tokens.append_all(self.attrs.outer());
1424 self.vis.to_tokens(tokens);
1425 self.mod_token.to_tokens(tokens);
1426 self.ident.to_tokens(tokens);
1427 if let Some((ref brace, ref items)) = self.content {
1428 brace.surround(tokens, |tokens| {
1429 tokens.append_all(self.attrs.inner());
1430 tokens.append_all(items);
1431 });
1432 } else {
1433 TokensOrDefault(&self.semi).to_tokens(tokens);
1434 }
1435 }
1436 }
1437
1438 impl ToTokens for ItemForeignMod {
1439 fn to_tokens(&self, tokens: &mut Tokens) {
1440 tokens.append_all(self.attrs.outer());
1441 self.abi.to_tokens(tokens);
1442 self.brace_token.surround(tokens, |tokens| {
1443 tokens.append_all(&self.items);
1444 });
1445 }
1446 }
1447
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001448 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001449 fn to_tokens(&self, tokens: &mut Tokens) {
1450 tokens.append_all(self.attrs.outer());
1451 self.vis.to_tokens(tokens);
1452 self.type_token.to_tokens(tokens);
1453 self.ident.to_tokens(tokens);
1454 self.generics.to_tokens(tokens);
1455 self.generics.where_clause.to_tokens(tokens);
1456 self.eq_token.to_tokens(tokens);
1457 self.ty.to_tokens(tokens);
1458 self.semi_token.to_tokens(tokens);
1459 }
1460 }
1461
1462 impl ToTokens for ItemEnum {
1463 fn to_tokens(&self, tokens: &mut Tokens) {
1464 tokens.append_all(self.attrs.outer());
1465 self.vis.to_tokens(tokens);
1466 self.enum_token.to_tokens(tokens);
1467 self.ident.to_tokens(tokens);
1468 self.generics.to_tokens(tokens);
1469 self.generics.where_clause.to_tokens(tokens);
1470 self.brace_token.surround(tokens, |tokens| {
1471 self.variants.to_tokens(tokens);
1472 });
1473 }
1474 }
1475
1476 impl ToTokens for ItemStruct {
1477 fn to_tokens(&self, tokens: &mut Tokens) {
1478 tokens.append_all(self.attrs.outer());
1479 self.vis.to_tokens(tokens);
1480 self.struct_token.to_tokens(tokens);
1481 self.ident.to_tokens(tokens);
1482 self.generics.to_tokens(tokens);
1483 match self.data {
1484 VariantData::Struct(..) => {
1485 self.generics.where_clause.to_tokens(tokens);
1486 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001487 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001488 VariantData::Tuple(..) => {
1489 self.data.to_tokens(tokens);
1490 self.generics.where_clause.to_tokens(tokens);
1491 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001492 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001493 VariantData::Unit => {
1494 self.generics.where_clause.to_tokens(tokens);
1495 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001496 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001497 }
1498 }
1499 }
1500
1501 impl ToTokens for ItemUnion {
1502 fn to_tokens(&self, tokens: &mut Tokens) {
1503 tokens.append_all(self.attrs.outer());
1504 self.vis.to_tokens(tokens);
1505 self.union_token.to_tokens(tokens);
1506 self.ident.to_tokens(tokens);
1507 self.generics.to_tokens(tokens);
1508 self.generics.where_clause.to_tokens(tokens);
1509 // XXX: Should we handle / complain when using a
1510 // non-VariantData::Struct Variant in Union?
1511 self.data.to_tokens(tokens);
1512 }
1513 }
1514
1515 impl ToTokens for ItemTrait {
1516 fn to_tokens(&self, tokens: &mut Tokens) {
1517 tokens.append_all(self.attrs.outer());
1518 self.vis.to_tokens(tokens);
1519 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001520 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001521 self.trait_token.to_tokens(tokens);
1522 self.ident.to_tokens(tokens);
1523 self.generics.to_tokens(tokens);
1524 if !self.supertraits.is_empty() {
1525 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1526 self.supertraits.to_tokens(tokens);
1527 }
1528 self.generics.where_clause.to_tokens(tokens);
1529 self.brace_token.surround(tokens, |tokens| {
1530 tokens.append_all(&self.items);
1531 });
1532 }
1533 }
1534
1535 impl ToTokens for ItemDefaultImpl {
1536 fn to_tokens(&self, tokens: &mut Tokens) {
1537 tokens.append_all(self.attrs.outer());
1538 self.unsafety.to_tokens(tokens);
1539 self.impl_token.to_tokens(tokens);
1540 self.path.to_tokens(tokens);
1541 self.for_token.to_tokens(tokens);
1542 self.dot2_token.to_tokens(tokens);
1543 self.brace_token.surround(tokens, |_tokens| {});
1544 }
1545 }
1546
1547 impl ToTokens for ItemImpl {
1548 fn to_tokens(&self, tokens: &mut Tokens) {
1549 tokens.append_all(self.attrs.outer());
1550 self.defaultness.to_tokens(tokens);
1551 self.unsafety.to_tokens(tokens);
1552 self.impl_token.to_tokens(tokens);
1553 self.generics.to_tokens(tokens);
1554 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1555 polarity.to_tokens(tokens);
1556 path.to_tokens(tokens);
1557 for_token.to_tokens(tokens);
1558 }
1559 self.self_ty.to_tokens(tokens);
1560 self.generics.where_clause.to_tokens(tokens);
1561 self.brace_token.surround(tokens, |tokens| {
1562 tokens.append_all(&self.items);
1563 });
1564 }
1565 }
1566
1567 impl ToTokens for ItemMacro {
1568 fn to_tokens(&self, tokens: &mut Tokens) {
1569 tokens.append_all(self.attrs.outer());
1570 self.mac.path.to_tokens(tokens);
1571 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001572 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001573 self.mac.tokens.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001574 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001575 }
1576 }
David Tolnay42602292016-10-01 22:25:45 -07001577
David Tolnay500d8322017-12-18 00:32:51 -08001578 impl ToTokens for ItemMacro2 {
1579 fn to_tokens(&self, tokens: &mut Tokens) {
1580 tokens.append_all(self.attrs.outer());
1581 self.vis.to_tokens(tokens);
1582 self.macro_token.to_tokens(tokens);
1583 self.ident.to_tokens(tokens);
1584 self.args.to_tokens(tokens);
1585 self.body.to_tokens(tokens);
1586 }
1587 }
1588
David Tolnay5f332a92017-12-26 00:42:45 -05001589 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001590 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001591 self.ident.to_tokens(tokens);
1592 if let Some((ref as_token, ref rename)) = self.rename {
1593 as_token.to_tokens(tokens);
1594 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001595 }
David Tolnay4a057422016-10-08 00:02:31 -07001596 }
1597 }
1598
David Tolnay5f332a92017-12-26 00:42:45 -05001599 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001600 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001601 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001602 }
1603 }
1604
David Tolnay5f332a92017-12-26 00:42:45 -05001605 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001606 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001607 self.brace_token.surround(tokens, |tokens| {
1608 self.items.to_tokens(tokens);
1609 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001610 }
1611 }
1612
David Tolnay1bfa7332017-11-11 12:41:20 -08001613 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001614 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001615 tokens.append_all(self.attrs.outer());
1616 self.const_token.to_tokens(tokens);
1617 self.ident.to_tokens(tokens);
1618 self.colon_token.to_tokens(tokens);
1619 self.ty.to_tokens(tokens);
1620 if let Some((ref eq_token, ref default)) = self.default {
1621 eq_token.to_tokens(tokens);
1622 default.to_tokens(tokens);
1623 }
1624 self.semi_token.to_tokens(tokens);
1625 }
1626 }
1627
1628 impl ToTokens for TraitItemMethod {
1629 fn to_tokens(&self, tokens: &mut Tokens) {
1630 tokens.append_all(self.attrs.outer());
1631 self.sig.to_tokens(tokens);
1632 match self.default {
1633 Some(ref block) => {
1634 block.brace_token.surround(tokens, |tokens| {
1635 tokens.append_all(self.attrs.inner());
1636 tokens.append_all(&block.stmts);
1637 });
David Tolnayca085422016-10-04 00:12:38 -07001638 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001639 None => {
1640 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001641 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001642 }
1643 }
1644 }
1645
1646 impl ToTokens for TraitItemType {
1647 fn to_tokens(&self, tokens: &mut Tokens) {
1648 tokens.append_all(self.attrs.outer());
1649 self.type_token.to_tokens(tokens);
1650 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001651 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001652 if !self.bounds.is_empty() {
1653 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1654 self.bounds.to_tokens(tokens);
1655 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001656 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001657 if let Some((ref eq_token, ref default)) = self.default {
1658 eq_token.to_tokens(tokens);
1659 default.to_tokens(tokens);
1660 }
1661 self.semi_token.to_tokens(tokens);
1662 }
1663 }
1664
1665 impl ToTokens for TraitItemMacro {
1666 fn to_tokens(&self, tokens: &mut Tokens) {
1667 tokens.append_all(self.attrs.outer());
1668 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001669 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001670 }
1671 }
1672
David Tolnay857628c2017-11-11 12:25:31 -08001673 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001674 fn to_tokens(&self, tokens: &mut Tokens) {
1675 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001676 self.vis.to_tokens(tokens);
1677 self.defaultness.to_tokens(tokens);
1678 self.const_token.to_tokens(tokens);
1679 self.ident.to_tokens(tokens);
1680 self.colon_token.to_tokens(tokens);
1681 self.ty.to_tokens(tokens);
1682 self.eq_token.to_tokens(tokens);
1683 self.expr.to_tokens(tokens);
1684 self.semi_token.to_tokens(tokens);
1685 }
1686 }
1687
1688 impl ToTokens for ImplItemMethod {
1689 fn to_tokens(&self, tokens: &mut Tokens) {
1690 tokens.append_all(self.attrs.outer());
1691 self.vis.to_tokens(tokens);
1692 self.defaultness.to_tokens(tokens);
1693 self.sig.to_tokens(tokens);
1694 self.block.brace_token.surround(tokens, |tokens| {
1695 tokens.append_all(self.attrs.inner());
1696 tokens.append_all(&self.block.stmts);
1697 });
1698 }
1699 }
1700
1701 impl ToTokens for ImplItemType {
1702 fn to_tokens(&self, tokens: &mut Tokens) {
1703 tokens.append_all(self.attrs.outer());
1704 self.vis.to_tokens(tokens);
1705 self.defaultness.to_tokens(tokens);
1706 self.type_token.to_tokens(tokens);
1707 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001708 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001709 self.eq_token.to_tokens(tokens);
1710 self.ty.to_tokens(tokens);
1711 self.semi_token.to_tokens(tokens);
1712 }
1713 }
1714
1715 impl ToTokens for ImplItemMacro {
1716 fn to_tokens(&self, tokens: &mut Tokens) {
1717 tokens.append_all(self.attrs.outer());
1718 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001719 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001720 }
1721 }
1722
David Tolnay8894f602017-11-11 12:11:04 -08001723 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001724 fn to_tokens(&self, tokens: &mut Tokens) {
1725 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001726 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001727 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1728 self.semi_token.to_tokens(tokens);
1729 }
1730 }
1731
1732 impl ToTokens for ForeignItemStatic {
1733 fn to_tokens(&self, tokens: &mut Tokens) {
1734 tokens.append_all(self.attrs.outer());
1735 self.vis.to_tokens(tokens);
1736 self.static_token.to_tokens(tokens);
1737 self.mutbl.to_tokens(tokens);
1738 self.ident.to_tokens(tokens);
1739 self.colon_token.to_tokens(tokens);
1740 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001741 self.semi_token.to_tokens(tokens);
1742 }
1743 }
1744
David Tolnay199bcbb2017-11-12 10:33:52 -08001745 impl ToTokens for ForeignItemType {
1746 fn to_tokens(&self, tokens: &mut Tokens) {
1747 tokens.append_all(self.attrs.outer());
1748 self.vis.to_tokens(tokens);
1749 self.type_token.to_tokens(tokens);
1750 self.ident.to_tokens(tokens);
1751 self.semi_token.to_tokens(tokens);
1752 }
1753 }
1754
David Tolnay570695e2017-06-03 16:15:13 -07001755 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001756 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001757 self.constness.to_tokens(tokens);
1758 self.unsafety.to_tokens(tokens);
1759 self.abi.to_tokens(tokens);
1760 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001761 }
1762 }
1763
David Tolnay570695e2017-06-03 16:15:13 -07001764 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765
1766 impl<'a> ToTokens for NamedDecl<'a> {
1767 fn to_tokens(&self, tokens: &mut Tokens) {
1768 self.0.fn_token.to_tokens(tokens);
1769 self.1.to_tokens(tokens);
1770 self.0.generics.to_tokens(tokens);
1771 self.0.paren_token.surround(tokens, |tokens| {
1772 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001773 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1774 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001775 }
David Tolnayd2836e22017-12-27 23:13:00 -05001776 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001777 });
1778 self.0.output.to_tokens(tokens);
1779 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001780 }
1781 }
1782
Alex Crichton62a0a592017-05-22 13:58:53 -07001783 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001784 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001785 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001786 self.lifetime.to_tokens(tokens);
1787 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001788 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001789 }
1790 }
1791
1792 impl ToTokens for ArgSelf {
1793 fn to_tokens(&self, tokens: &mut Tokens) {
1794 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001795 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001796 }
1797 }
1798
1799 impl ToTokens for ArgCaptured {
1800 fn to_tokens(&self, tokens: &mut Tokens) {
1801 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001803 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001804 }
1805 }
1806
David Tolnay42602292016-10-01 22:25:45 -07001807 impl ToTokens for Constness {
1808 fn to_tokens(&self, tokens: &mut Tokens) {
1809 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001811 Constness::NotConst => {
1812 // nothing
1813 }
David Tolnay42602292016-10-01 22:25:45 -07001814 }
1815 }
1816 }
1817
David Tolnay4c9be372016-10-06 00:47:37 -07001818 impl ToTokens for Defaultness {
1819 fn to_tokens(&self, tokens: &mut Tokens) {
1820 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001822 Defaultness::Final => {
1823 // nothing
1824 }
1825 }
1826 }
1827 }
1828
1829 impl ToTokens for ImplPolarity {
1830 fn to_tokens(&self, tokens: &mut Tokens) {
1831 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001832 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001833 ImplPolarity::Positive => {
1834 // nothing
1835 }
1836 }
1837 }
1838 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001839}