blob: bb3c892bd89c4cb9d2aceddbc585e9ce928cecaf [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 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,
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 Tolnay4a3f59a2017-12-28 21:21:12 -0500442 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500443 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500445 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500446 pub output: ReturnType,
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![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 pub lifetime: Option<Lifetime>,
458 pub mutbl: Mutability,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500459 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700460 }),
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) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500578 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500579 true => syn!(UseTree)
580 |
David Tolnay8edcef12017-12-28 12:06:52 -0500581 false => alt!(
582 tuple!(keyword!(as), syn!(Ident)) => {
583 |rename| UseTree::Path(UsePath {
584 ident: prefix.pop().unwrap().into_item(),
585 rename: Some(rename),
586 })
587 }
David Tolnay5f332a92017-12-26 00:42:45 -0500588 |
David Tolnay8edcef12017-12-28 12:06:52 -0500589 epsilon!() => {
590 |_| UseTree::Path(UsePath {
591 ident: prefix.pop().unwrap().into_item(),
592 rename: None,
593 })
594 }
David Tolnay5f332a92017-12-26 00:42:45 -0500595 )
596 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800597 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800598 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700599 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800600 vis: vis,
601 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500602 leading_colon: leading_colon,
603 prefix: prefix,
604 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800605 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800606 })
David Tolnay4a057422016-10-08 00:02:31 -0700607 ));
608
David Tolnay5f332a92017-12-26 00:42:45 -0500609 named!(use_prefix -> Ident, alt!(
610 syn!(Ident)
611 |
612 keyword!(self) => { Into::into }
613 |
614 keyword!(super) => { Into::into }
615 |
616 keyword!(crate) => { Into::into }
617 ));
618
619 impl_synom!(UseTree "use tree" alt!(
620 syn!(UsePath) => { UseTree::Path }
621 |
622 syn!(UseGlob) => { UseTree::Glob }
623 |
624 syn!(UseList) => { UseTree::List }
625 ));
626
627 impl_synom!(UsePath "use path" do_parse!(
628 ident: alt!(
629 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400630 |
David Tolnay5f332a92017-12-26 00:42:45 -0500631 keyword!(self) => { Into::into }
632 ) >>
633 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
634 (UsePath {
635 ident: ident,
636 rename: rename,
637 })
638 ));
David Tolnay4a057422016-10-08 00:02:31 -0700639
David Tolnay5f332a92017-12-26 00:42:45 -0500640 impl_synom!(UseGlob "use glob" do_parse!(
641 star: punct!(*) >>
642 (UseGlob {
643 star_token: star,
644 })
645 ));
David Tolnay4a057422016-10-08 00:02:31 -0700646
David Tolnay5f332a92017-12-26 00:42:45 -0500647 impl_synom!(UseList "use list" do_parse!(
648 list: braces!(Delimited::parse_terminated) >>
649 (UseList {
650 brace_token: list.1,
651 items: list.0,
652 })
653 ));
David Tolnay4a057422016-10-08 00:02:31 -0700654
David Tolnay4c614be2017-11-10 00:02:38 -0800655 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500656 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700657 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800658 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700659 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700660 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800661 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800662 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700664 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800666 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700667 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800668 vis: vis,
669 static_token: static_,
670 mutbl: mutability,
671 ident: ident,
672 colon_token: colon,
673 ty: Box::new(ty),
674 eq_token: eq,
675 expr: Box::new(value),
676 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800677 })
David Tolnay47a877c2016-10-01 16:50:55 -0700678 ));
679
David Tolnay4c614be2017-11-10 00:02:38 -0800680 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500681 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700682 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800683 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700684 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800685 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800686 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800687 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700688 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800689 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800690 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700691 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800692 vis: vis,
693 const_token: const_,
694 ident: ident,
695 colon_token: colon,
696 ty: Box::new(ty),
697 eq_token: eq,
698 expr: Box::new(value),
699 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800700 })
David Tolnay47a877c2016-10-01 16:50:55 -0700701 ));
702
David Tolnay4c614be2017-11-10 00:02:38 -0800703 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500704 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700705 vis: syn!(Visibility) >>
706 constness: syn!(Constness) >>
707 unsafety: syn!(Unsafety) >>
708 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800709 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700710 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700711 generics: syn!(Generics) >>
712 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800713 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500714 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700715 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500716 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700717 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400718 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800719 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700720 attrs: {
721 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700722 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700723 attrs
724 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800725 vis: vis,
726 constness: constness,
727 unsafety: unsafety,
728 abi: abi,
729 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800730 fn_token: fn_,
731 paren_token: inputs.1,
732 inputs: inputs.0,
733 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500734 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800735 generics: Generics {
736 where_clause: where_clause,
737 .. generics
738 },
739 }),
740 ident: ident,
741 block: Box::new(Block {
742 brace_token: inner_attrs_stmts.1,
743 stmts: (inner_attrs_stmts.0).1,
744 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800745 })
David Tolnay42602292016-10-01 22:25:45 -0700746 ));
747
Alex Crichton954046c2017-05-30 21:49:42 -0700748 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400749 named!(parse -> Self, alt!(
750 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800751 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400752 lt: option!(syn!(Lifetime)) >>
753 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800754 self_: keyword!(self) >>
755 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400756 (ArgSelfRef {
757 lifetime: lt,
758 mutbl: mutability,
759 and_token: and,
760 self_token: self_,
761 }.into())
762 )
763 |
764 do_parse!(
765 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 self_: keyword!(self) >>
767 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400768 (ArgSelf {
769 mutbl: mutability,
770 self_token: self_,
771 }.into())
772 )
773 |
774 do_parse!(
775 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800776 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800777 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400778 (ArgCaptured {
779 pat: pat,
780 ty: ty,
781 colon_token: colon,
782 }.into())
783 )
784 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800785 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400786 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700787 }
David Tolnay62f374c2016-10-02 13:37:00 -0700788
David Tolnay4c614be2017-11-10 00:02:38 -0800789 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500790 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700791 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800792 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700793 ident: syn!(Ident) >>
794 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800795 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700796 Vec::new(),
797 None,
798 Some(semi),
799 )}
David Tolnay37d10332016-10-13 20:51:04 -0700800 |
Alex Crichton954046c2017-05-30 21:49:42 -0700801 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700802 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500803 many0!(Attribute::parse_inner),
804 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400805 )
David Tolnay570695e2017-06-03 16:15:13 -0700806 ) => {|((inner_attrs, items), brace)| (
807 inner_attrs,
808 Some((brace, items)),
809 None,
810 )}
David Tolnay37d10332016-10-13 20:51:04 -0700811 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800812 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700813 attrs: {
814 let mut attrs = outer_attrs;
815 attrs.extend(content_semi.0);
816 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700817 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800818 vis: vis,
819 mod_token: mod_,
820 ident: ident,
821 content: content_semi.1,
822 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800823 })
David Tolnay35902302016-10-06 01:11:08 -0700824 ));
825
David Tolnay4c614be2017-11-10 00:02:38 -0800826 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500827 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700828 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500829 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800830 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700831 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800832 abi: abi,
833 brace_token: items.1,
834 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800835 })
David Tolnay35902302016-10-06 01:11:08 -0700836 ));
837
David Tolnay8894f602017-11-11 12:11:04 -0800838 impl_synom!(ForeignItem "foreign item" alt!(
839 syn!(ForeignItemFn) => { ForeignItem::Fn }
840 |
841 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800842 |
843 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800844 ));
David Tolnay35902302016-10-06 01:11:08 -0700845
David Tolnay8894f602017-11-11 12:11:04 -0800846 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500847 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700848 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800849 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700850 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700851 generics: syn!(Generics) >>
852 inputs: parens!(do_parse!(
853 args: call!(Delimited::parse_terminated) >>
854 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800855 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700856 (args, variadic)
857 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800858 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500859 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800860 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700861 ({
862 let ((inputs, variadic), parens) = inputs;
863 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800864 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700865 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700866 attrs: attrs,
867 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800868 decl: Box::new(FnDecl {
869 fn_token: fn_,
870 paren_token: parens,
871 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500872 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800873 output: ret,
874 generics: Generics {
875 where_clause: where_clause,
876 .. generics
877 },
878 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700879 vis: vis,
880 }
David Tolnay35902302016-10-06 01:11:08 -0700881 })
882 ));
883
David Tolnay8894f602017-11-11 12:11:04 -0800884 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500885 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700886 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800887 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700888 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700889 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800890 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800891 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800892 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800893 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700894 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700895 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700896 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800897 ty: Box::new(ty),
898 mutbl: mutability,
899 static_token: static_,
900 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700901 vis: vis,
902 })
903 ));
904
David Tolnay199bcbb2017-11-12 10:33:52 -0800905 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500906 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800907 vis: syn!(Visibility) >>
908 type_: keyword!(type) >>
909 ident: syn!(Ident) >>
910 semi: punct!(;) >>
911 (ForeignItemType {
912 attrs: attrs,
913 vis: vis,
914 type_token: type_,
915 ident: ident,
916 semi_token: semi,
917 })
918 ));
919
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800920 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500921 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700922 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800923 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700924 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700925 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500926 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800927 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800928 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800929 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800930 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700931 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800932 vis: vis,
933 type_token: type_,
934 ident: ident,
935 generics: Generics {
936 where_clause: where_clause,
937 ..generics
938 },
939 eq_token: eq,
940 ty: Box::new(ty),
941 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800942 })
David Tolnay3cf52982016-10-01 17:11:37 -0700943 ));
944
David Tolnay4c614be2017-11-10 00:02:38 -0800945 impl_synom!(ItemStruct "struct item" switch!(
946 map!(syn!(DeriveInput), Into::into),
947 Item::Struct(item) => value!(item)
948 |
949 _ => reject!()
950 ));
David Tolnay42602292016-10-01 22:25:45 -0700951
David Tolnay4c614be2017-11-10 00:02:38 -0800952 impl_synom!(ItemEnum "enum item" switch!(
953 map!(syn!(DeriveInput), Into::into),
954 Item::Enum(item) => value!(item)
955 |
956 _ => reject!()
957 ));
958
959 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500960 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700961 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700963 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700964 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500965 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700966 fields: braces!(call!(Delimited::parse_terminated_with,
967 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800968 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700969 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800970 vis: vis,
971 union_token: union_,
972 ident: ident,
973 generics: Generics {
974 where_clause: where_clause,
975 .. generics
976 },
977 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800978 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700979 ));
980
David Tolnay4c614be2017-11-10 00:02:38 -0800981 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500982 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700983 vis: syn!(Visibility) >>
984 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500985 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700987 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700988 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800989 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700990 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700991 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700992 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500993 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -0500994 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800995 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700996 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800997 vis: vis,
998 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500999 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001000 trait_token: trait_,
1001 ident: ident,
1002 generics: Generics {
1003 where_clause: where_clause,
1004 .. generics
1005 },
1006 colon_token: colon,
1007 supertraits: bounds.unwrap_or_default(),
1008 brace_token: body.1,
1009 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001010 })
David Tolnay0aecb732016-10-03 23:03:50 -07001011 ));
1012
David Tolnay4c614be2017-11-10 00:02:38 -08001013 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001014 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001015 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001016 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001017 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001018 for_: keyword!(for) >>
1019 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001020 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001021 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001022 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001023 unsafety: unsafety,
1024 impl_token: impl_,
1025 path: path,
1026 for_token: for_,
1027 dot2_token: dot2,
1028 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001029 })
David Tolnayf94e2362016-10-04 00:29:51 -07001030 ));
1031
David Tolnayda705bd2017-11-10 21:58:05 -08001032 impl_synom!(TraitItem "trait item" alt!(
1033 syn!(TraitItemConst) => { TraitItem::Const }
1034 |
1035 syn!(TraitItemMethod) => { TraitItem::Method }
1036 |
1037 syn!(TraitItemType) => { TraitItem::Type }
1038 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001039 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001040 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001041
David Tolnayda705bd2017-11-10 21:58:05 -08001042 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001043 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001044 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001045 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001046 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001047 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001048 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1049 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001050 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001051 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001052 const_token: const_,
1053 ident: ident,
1054 colon_token: colon,
1055 ty: ty,
1056 default: default,
1057 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001058 })
1059 ));
1060
David Tolnayda705bd2017-11-10 21:58:05 -08001061 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001062 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001063 constness: syn!(Constness) >>
1064 unsafety: syn!(Unsafety) >>
1065 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001066 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001067 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001068 generics: syn!(Generics) >>
1069 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001070 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001071 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001072 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001073 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001074 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001075 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001076 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001077 ({
1078 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001079 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001080 None => (Vec::new(), None),
1081 };
David Tolnayda705bd2017-11-10 21:58:05 -08001082 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001083 attrs: {
1084 let mut attrs = outer_attrs;
1085 attrs.extend(inner_attrs);
1086 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001087 },
David Tolnayda705bd2017-11-10 21:58:05 -08001088 sig: MethodSig {
1089 constness: constness,
1090 unsafety: unsafety,
1091 abi: abi,
1092 ident: ident,
1093 decl: FnDecl {
1094 inputs: inputs.0,
1095 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001096 fn_token: fn_,
1097 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001098 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001099 generics: Generics {
1100 where_clause: where_clause,
1101 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001102 },
1103 },
David Tolnayda705bd2017-11-10 21:58:05 -08001104 },
1105 default: stmts.map(|stmts| {
1106 Block {
1107 stmts: stmts.0,
1108 brace_token: stmts.1,
1109 }
1110 }),
1111 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001112 }
David Tolnay0aecb732016-10-03 23:03:50 -07001113 })
1114 ));
1115
David Tolnayda705bd2017-11-10 21:58:05 -08001116 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001117 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001118 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001119 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001120 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001121 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001122 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001123 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001124 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001125 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001126 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001127 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001128 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001129 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001130 type_token: type_,
1131 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001132 generics: Generics {
1133 where_clause: where_clause,
1134 .. generics
1135 },
David Tolnayda705bd2017-11-10 21:58:05 -08001136 colon_token: colon,
1137 bounds: bounds.unwrap_or_default(),
1138 default: default,
1139 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001140 })
1141 ));
1142
David Tolnaydecf28d2017-11-11 11:56:45 -08001143 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001144 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001145 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001146 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001147 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001148 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001149 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001150 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001151 })
1152 ));
1153
David Tolnay4c614be2017-11-10 00:02:38 -08001154 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001155 attrs: many0!(Attribute::parse_outer) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001156 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001157 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001158 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001159 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001160 polarity_path: alt!(
1161 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001162 polarity: syn!(ImplPolarity) >>
1163 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001164 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001165 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001166 )
1167 |
David Tolnay570695e2017-06-03 16:15:13 -07001168 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001169 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001170 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001171 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001172 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001173 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001174 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001175 defaultness: defaultness,
1176 unsafety: unsafety,
1177 impl_token: impl_,
1178 generics: Generics {
1179 where_clause: where_clause,
1180 .. generics
1181 },
1182 trait_: polarity_path,
1183 self_ty: Box::new(self_ty),
1184 brace_token: body.1,
1185 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001186 })
David Tolnay4c9be372016-10-06 00:47:37 -07001187 ));
1188
David Tolnay857628c2017-11-11 12:25:31 -08001189 impl_synom!(ImplItem "item in impl block" alt!(
1190 syn!(ImplItemConst) => { ImplItem::Const }
1191 |
1192 syn!(ImplItemMethod) => { ImplItem::Method }
1193 |
1194 syn!(ImplItemType) => { ImplItem::Type }
1195 |
1196 syn!(ImplItemMacro) => { ImplItem::Macro }
1197 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001198
David Tolnay857628c2017-11-11 12:25:31 -08001199 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001200 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001201 vis: syn!(Visibility) >>
1202 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001203 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001204 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001205 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001206 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001207 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001208 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001209 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001210 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001211 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001212 vis: vis,
1213 defaultness: defaultness,
1214 const_token: const_,
1215 ident: ident,
1216 colon_token: colon,
1217 ty: ty,
1218 eq_token: eq,
1219 expr: value,
1220 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001221 })
1222 ));
1223
David Tolnay857628c2017-11-11 12:25:31 -08001224 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001225 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001226 vis: syn!(Visibility) >>
1227 defaultness: syn!(Defaultness) >>
1228 constness: syn!(Constness) >>
1229 unsafety: syn!(Unsafety) >>
1230 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001231 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001232 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001233 generics: syn!(Generics) >>
1234 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001235 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001236 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001237 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001238 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001239 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001240 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001241 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001242 attrs: {
1243 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001244 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001245 attrs
1246 },
David Tolnay857628c2017-11-11 12:25:31 -08001247 vis: vis,
1248 defaultness: defaultness,
1249 sig: MethodSig {
1250 constness: constness,
1251 unsafety: unsafety,
1252 abi: abi,
1253 ident: ident,
1254 decl: FnDecl {
1255 fn_token: fn_,
1256 paren_token: inputs.1,
1257 inputs: inputs.0,
1258 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001259 generics: Generics {
1260 where_clause: where_clause,
1261 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001262 },
David Tolnayd2836e22017-12-27 23:13:00 -05001263 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001264 },
David Tolnay857628c2017-11-11 12:25:31 -08001265 },
1266 block: Block {
1267 brace_token: inner_attrs_stmts.1,
1268 stmts: (inner_attrs_stmts.0).1,
1269 },
David Tolnay4c9be372016-10-06 00:47:37 -07001270 })
1271 ));
1272
David Tolnay857628c2017-11-11 12:25:31 -08001273 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001274 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001275 vis: syn!(Visibility) >>
1276 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001277 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001278 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001279 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001280 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001281 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001282 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001283 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001284 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001285 vis: vis,
1286 defaultness: defaultness,
1287 type_token: type_,
1288 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001289 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001290 eq_token: eq,
1291 ty: ty,
1292 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001293 })
1294 ));
1295
David Tolnay857628c2017-11-11 12:25:31 -08001296 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001297 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001298 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001299 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001300 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001301 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001302 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001303 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001304 })
1305 ));
1306
Alex Crichton954046c2017-05-30 21:49:42 -07001307 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001308 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001309 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001310 |
1311 epsilon!() => { |_| ImplPolarity::Positive }
1312 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001313 }
David Tolnay4c9be372016-10-06 00:47:37 -07001314
Alex Crichton954046c2017-05-30 21:49:42 -07001315 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001316 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001317 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001318 |
1319 epsilon!() => { |_| Constness::NotConst }
1320 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001321 }
David Tolnay42602292016-10-01 22:25:45 -07001322
Alex Crichton954046c2017-05-30 21:49:42 -07001323 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001324 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001325 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001326 |
1327 epsilon!() => { |_| Defaultness::Final }
1328 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001329 }
David Tolnay57292da2017-12-27 21:03:33 -05001330
1331 fn is_braced(tt: &TokenTree) -> bool {
1332 match tt.kind {
1333 TokenNode::Group(Delimiter::Brace, _) => true,
1334 _ => false,
1335 }
1336 }
David Tolnayedf2b992016-09-23 20:43:45 -07001337}
David Tolnay4a51dc72016-10-01 00:40:31 -07001338
1339#[cfg(feature = "printing")]
1340mod printing {
1341 use super::*;
1342 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001343 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001344 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001345
David Tolnay1bfa7332017-11-11 12:41:20 -08001346 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001347 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001348 tokens.append_all(self.attrs.outer());
1349 self.vis.to_tokens(tokens);
1350 self.extern_token.to_tokens(tokens);
1351 self.crate_token.to_tokens(tokens);
1352 self.ident.to_tokens(tokens);
1353 if let Some((ref as_token, ref rename)) = self.rename {
1354 as_token.to_tokens(tokens);
1355 rename.to_tokens(tokens);
1356 }
1357 self.semi_token.to_tokens(tokens);
1358 }
1359 }
1360
1361 impl ToTokens for ItemUse {
1362 fn to_tokens(&self, tokens: &mut Tokens) {
1363 tokens.append_all(self.attrs.outer());
1364 self.vis.to_tokens(tokens);
1365 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001366 self.leading_colon.to_tokens(tokens);
1367 self.prefix.to_tokens(tokens);
1368 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001369 self.semi_token.to_tokens(tokens);
1370 }
1371 }
1372
1373 impl ToTokens for ItemStatic {
1374 fn to_tokens(&self, tokens: &mut Tokens) {
1375 tokens.append_all(self.attrs.outer());
1376 self.vis.to_tokens(tokens);
1377 self.static_token.to_tokens(tokens);
1378 self.mutbl.to_tokens(tokens);
1379 self.ident.to_tokens(tokens);
1380 self.colon_token.to_tokens(tokens);
1381 self.ty.to_tokens(tokens);
1382 self.eq_token.to_tokens(tokens);
1383 self.expr.to_tokens(tokens);
1384 self.semi_token.to_tokens(tokens);
1385 }
1386 }
1387
1388 impl ToTokens for ItemConst {
1389 fn to_tokens(&self, tokens: &mut Tokens) {
1390 tokens.append_all(self.attrs.outer());
1391 self.vis.to_tokens(tokens);
1392 self.const_token.to_tokens(tokens);
1393 self.ident.to_tokens(tokens);
1394 self.colon_token.to_tokens(tokens);
1395 self.ty.to_tokens(tokens);
1396 self.eq_token.to_tokens(tokens);
1397 self.expr.to_tokens(tokens);
1398 self.semi_token.to_tokens(tokens);
1399 }
1400 }
1401
1402 impl ToTokens for ItemFn {
1403 fn to_tokens(&self, tokens: &mut Tokens) {
1404 tokens.append_all(self.attrs.outer());
1405 self.vis.to_tokens(tokens);
1406 self.constness.to_tokens(tokens);
1407 self.unsafety.to_tokens(tokens);
1408 self.abi.to_tokens(tokens);
1409 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1410 self.block.brace_token.surround(tokens, |tokens| {
1411 tokens.append_all(self.attrs.inner());
1412 tokens.append_all(&self.block.stmts);
1413 });
1414 }
1415 }
1416
1417 impl ToTokens for ItemMod {
1418 fn to_tokens(&self, tokens: &mut Tokens) {
1419 tokens.append_all(self.attrs.outer());
1420 self.vis.to_tokens(tokens);
1421 self.mod_token.to_tokens(tokens);
1422 self.ident.to_tokens(tokens);
1423 if let Some((ref brace, ref items)) = self.content {
1424 brace.surround(tokens, |tokens| {
1425 tokens.append_all(self.attrs.inner());
1426 tokens.append_all(items);
1427 });
1428 } else {
1429 TokensOrDefault(&self.semi).to_tokens(tokens);
1430 }
1431 }
1432 }
1433
1434 impl ToTokens for ItemForeignMod {
1435 fn to_tokens(&self, tokens: &mut Tokens) {
1436 tokens.append_all(self.attrs.outer());
1437 self.abi.to_tokens(tokens);
1438 self.brace_token.surround(tokens, |tokens| {
1439 tokens.append_all(&self.items);
1440 });
1441 }
1442 }
1443
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001444 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001445 fn to_tokens(&self, tokens: &mut Tokens) {
1446 tokens.append_all(self.attrs.outer());
1447 self.vis.to_tokens(tokens);
1448 self.type_token.to_tokens(tokens);
1449 self.ident.to_tokens(tokens);
1450 self.generics.to_tokens(tokens);
1451 self.generics.where_clause.to_tokens(tokens);
1452 self.eq_token.to_tokens(tokens);
1453 self.ty.to_tokens(tokens);
1454 self.semi_token.to_tokens(tokens);
1455 }
1456 }
1457
1458 impl ToTokens for ItemEnum {
1459 fn to_tokens(&self, tokens: &mut Tokens) {
1460 tokens.append_all(self.attrs.outer());
1461 self.vis.to_tokens(tokens);
1462 self.enum_token.to_tokens(tokens);
1463 self.ident.to_tokens(tokens);
1464 self.generics.to_tokens(tokens);
1465 self.generics.where_clause.to_tokens(tokens);
1466 self.brace_token.surround(tokens, |tokens| {
1467 self.variants.to_tokens(tokens);
1468 });
1469 }
1470 }
1471
1472 impl ToTokens for ItemStruct {
1473 fn to_tokens(&self, tokens: &mut Tokens) {
1474 tokens.append_all(self.attrs.outer());
1475 self.vis.to_tokens(tokens);
1476 self.struct_token.to_tokens(tokens);
1477 self.ident.to_tokens(tokens);
1478 self.generics.to_tokens(tokens);
1479 match self.data {
1480 VariantData::Struct(..) => {
1481 self.generics.where_clause.to_tokens(tokens);
1482 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001483 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001484 VariantData::Tuple(..) => {
1485 self.data.to_tokens(tokens);
1486 self.generics.where_clause.to_tokens(tokens);
1487 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001488 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001489 VariantData::Unit => {
1490 self.generics.where_clause.to_tokens(tokens);
1491 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001492 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001493 }
1494 }
1495 }
1496
1497 impl ToTokens for ItemUnion {
1498 fn to_tokens(&self, tokens: &mut Tokens) {
1499 tokens.append_all(self.attrs.outer());
1500 self.vis.to_tokens(tokens);
1501 self.union_token.to_tokens(tokens);
1502 self.ident.to_tokens(tokens);
1503 self.generics.to_tokens(tokens);
1504 self.generics.where_clause.to_tokens(tokens);
1505 // XXX: Should we handle / complain when using a
1506 // non-VariantData::Struct Variant in Union?
1507 self.data.to_tokens(tokens);
1508 }
1509 }
1510
1511 impl ToTokens for ItemTrait {
1512 fn to_tokens(&self, tokens: &mut Tokens) {
1513 tokens.append_all(self.attrs.outer());
1514 self.vis.to_tokens(tokens);
1515 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001516 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001517 self.trait_token.to_tokens(tokens);
1518 self.ident.to_tokens(tokens);
1519 self.generics.to_tokens(tokens);
1520 if !self.supertraits.is_empty() {
1521 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1522 self.supertraits.to_tokens(tokens);
1523 }
1524 self.generics.where_clause.to_tokens(tokens);
1525 self.brace_token.surround(tokens, |tokens| {
1526 tokens.append_all(&self.items);
1527 });
1528 }
1529 }
1530
1531 impl ToTokens for ItemDefaultImpl {
1532 fn to_tokens(&self, tokens: &mut Tokens) {
1533 tokens.append_all(self.attrs.outer());
1534 self.unsafety.to_tokens(tokens);
1535 self.impl_token.to_tokens(tokens);
1536 self.path.to_tokens(tokens);
1537 self.for_token.to_tokens(tokens);
1538 self.dot2_token.to_tokens(tokens);
1539 self.brace_token.surround(tokens, |_tokens| {});
1540 }
1541 }
1542
1543 impl ToTokens for ItemImpl {
1544 fn to_tokens(&self, tokens: &mut Tokens) {
1545 tokens.append_all(self.attrs.outer());
1546 self.defaultness.to_tokens(tokens);
1547 self.unsafety.to_tokens(tokens);
1548 self.impl_token.to_tokens(tokens);
1549 self.generics.to_tokens(tokens);
1550 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1551 polarity.to_tokens(tokens);
1552 path.to_tokens(tokens);
1553 for_token.to_tokens(tokens);
1554 }
1555 self.self_ty.to_tokens(tokens);
1556 self.generics.where_clause.to_tokens(tokens);
1557 self.brace_token.surround(tokens, |tokens| {
1558 tokens.append_all(&self.items);
1559 });
1560 }
1561 }
1562
1563 impl ToTokens for ItemMacro {
1564 fn to_tokens(&self, tokens: &mut Tokens) {
1565 tokens.append_all(self.attrs.outer());
1566 self.mac.path.to_tokens(tokens);
1567 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001568 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001569 self.mac.tokens.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001570 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001571 }
1572 }
David Tolnay42602292016-10-01 22:25:45 -07001573
David Tolnay500d8322017-12-18 00:32:51 -08001574 impl ToTokens for ItemMacro2 {
1575 fn to_tokens(&self, tokens: &mut Tokens) {
1576 tokens.append_all(self.attrs.outer());
1577 self.vis.to_tokens(tokens);
1578 self.macro_token.to_tokens(tokens);
1579 self.ident.to_tokens(tokens);
1580 self.args.to_tokens(tokens);
1581 self.body.to_tokens(tokens);
1582 }
1583 }
1584
David Tolnay5f332a92017-12-26 00:42:45 -05001585 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001586 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001587 self.ident.to_tokens(tokens);
1588 if let Some((ref as_token, ref rename)) = self.rename {
1589 as_token.to_tokens(tokens);
1590 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001591 }
David Tolnay4a057422016-10-08 00:02:31 -07001592 }
1593 }
1594
David Tolnay5f332a92017-12-26 00:42:45 -05001595 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001596 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001597 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001598 }
1599 }
1600
David Tolnay5f332a92017-12-26 00:42:45 -05001601 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001602 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001603 self.brace_token.surround(tokens, |tokens| {
1604 self.items.to_tokens(tokens);
1605 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001606 }
1607 }
1608
David Tolnay1bfa7332017-11-11 12:41:20 -08001609 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001610 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001611 tokens.append_all(self.attrs.outer());
1612 self.const_token.to_tokens(tokens);
1613 self.ident.to_tokens(tokens);
1614 self.colon_token.to_tokens(tokens);
1615 self.ty.to_tokens(tokens);
1616 if let Some((ref eq_token, ref default)) = self.default {
1617 eq_token.to_tokens(tokens);
1618 default.to_tokens(tokens);
1619 }
1620 self.semi_token.to_tokens(tokens);
1621 }
1622 }
1623
1624 impl ToTokens for TraitItemMethod {
1625 fn to_tokens(&self, tokens: &mut Tokens) {
1626 tokens.append_all(self.attrs.outer());
1627 self.sig.to_tokens(tokens);
1628 match self.default {
1629 Some(ref block) => {
1630 block.brace_token.surround(tokens, |tokens| {
1631 tokens.append_all(self.attrs.inner());
1632 tokens.append_all(&block.stmts);
1633 });
David Tolnayca085422016-10-04 00:12:38 -07001634 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001635 None => {
1636 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001637 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001638 }
1639 }
1640 }
1641
1642 impl ToTokens for TraitItemType {
1643 fn to_tokens(&self, tokens: &mut Tokens) {
1644 tokens.append_all(self.attrs.outer());
1645 self.type_token.to_tokens(tokens);
1646 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001647 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001648 if !self.bounds.is_empty() {
1649 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1650 self.bounds.to_tokens(tokens);
1651 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001652 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001653 if let Some((ref eq_token, ref default)) = self.default {
1654 eq_token.to_tokens(tokens);
1655 default.to_tokens(tokens);
1656 }
1657 self.semi_token.to_tokens(tokens);
1658 }
1659 }
1660
1661 impl ToTokens for TraitItemMacro {
1662 fn to_tokens(&self, tokens: &mut Tokens) {
1663 tokens.append_all(self.attrs.outer());
1664 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001665 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001666 }
1667 }
1668
David Tolnay857628c2017-11-11 12:25:31 -08001669 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001670 fn to_tokens(&self, tokens: &mut Tokens) {
1671 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001672 self.vis.to_tokens(tokens);
1673 self.defaultness.to_tokens(tokens);
1674 self.const_token.to_tokens(tokens);
1675 self.ident.to_tokens(tokens);
1676 self.colon_token.to_tokens(tokens);
1677 self.ty.to_tokens(tokens);
1678 self.eq_token.to_tokens(tokens);
1679 self.expr.to_tokens(tokens);
1680 self.semi_token.to_tokens(tokens);
1681 }
1682 }
1683
1684 impl ToTokens for ImplItemMethod {
1685 fn to_tokens(&self, tokens: &mut Tokens) {
1686 tokens.append_all(self.attrs.outer());
1687 self.vis.to_tokens(tokens);
1688 self.defaultness.to_tokens(tokens);
1689 self.sig.to_tokens(tokens);
1690 self.block.brace_token.surround(tokens, |tokens| {
1691 tokens.append_all(self.attrs.inner());
1692 tokens.append_all(&self.block.stmts);
1693 });
1694 }
1695 }
1696
1697 impl ToTokens for ImplItemType {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 tokens.append_all(self.attrs.outer());
1700 self.vis.to_tokens(tokens);
1701 self.defaultness.to_tokens(tokens);
1702 self.type_token.to_tokens(tokens);
1703 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001704 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001705 self.eq_token.to_tokens(tokens);
1706 self.ty.to_tokens(tokens);
1707 self.semi_token.to_tokens(tokens);
1708 }
1709 }
1710
1711 impl ToTokens for ImplItemMacro {
1712 fn to_tokens(&self, tokens: &mut Tokens) {
1713 tokens.append_all(self.attrs.outer());
1714 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001715 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001716 }
1717 }
1718
David Tolnay8894f602017-11-11 12:11:04 -08001719 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001720 fn to_tokens(&self, tokens: &mut Tokens) {
1721 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001722 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001723 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1724 self.semi_token.to_tokens(tokens);
1725 }
1726 }
1727
1728 impl ToTokens for ForeignItemStatic {
1729 fn to_tokens(&self, tokens: &mut Tokens) {
1730 tokens.append_all(self.attrs.outer());
1731 self.vis.to_tokens(tokens);
1732 self.static_token.to_tokens(tokens);
1733 self.mutbl.to_tokens(tokens);
1734 self.ident.to_tokens(tokens);
1735 self.colon_token.to_tokens(tokens);
1736 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001737 self.semi_token.to_tokens(tokens);
1738 }
1739 }
1740
David Tolnay199bcbb2017-11-12 10:33:52 -08001741 impl ToTokens for ForeignItemType {
1742 fn to_tokens(&self, tokens: &mut Tokens) {
1743 tokens.append_all(self.attrs.outer());
1744 self.vis.to_tokens(tokens);
1745 self.type_token.to_tokens(tokens);
1746 self.ident.to_tokens(tokens);
1747 self.semi_token.to_tokens(tokens);
1748 }
1749 }
1750
David Tolnay570695e2017-06-03 16:15:13 -07001751 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001752 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001753 self.constness.to_tokens(tokens);
1754 self.unsafety.to_tokens(tokens);
1755 self.abi.to_tokens(tokens);
1756 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001757 }
1758 }
1759
David Tolnay570695e2017-06-03 16:15:13 -07001760 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001761
1762 impl<'a> ToTokens for NamedDecl<'a> {
1763 fn to_tokens(&self, tokens: &mut Tokens) {
1764 self.0.fn_token.to_tokens(tokens);
1765 self.1.to_tokens(tokens);
1766 self.0.generics.to_tokens(tokens);
1767 self.0.paren_token.surround(tokens, |tokens| {
1768 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001769 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1770 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001771 }
David Tolnayd2836e22017-12-27 23:13:00 -05001772 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001773 });
1774 self.0.output.to_tokens(tokens);
1775 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001776 }
1777 }
1778
Alex Crichton62a0a592017-05-22 13:58:53 -07001779 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001780 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001781 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001782 self.lifetime.to_tokens(tokens);
1783 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001784 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001785 }
1786 }
1787
1788 impl ToTokens for ArgSelf {
1789 fn to_tokens(&self, tokens: &mut Tokens) {
1790 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001791 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001792 }
1793 }
1794
1795 impl ToTokens for ArgCaptured {
1796 fn to_tokens(&self, tokens: &mut Tokens) {
1797 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001798 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001799 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001800 }
1801 }
1802
David Tolnay42602292016-10-01 22:25:45 -07001803 impl ToTokens for Constness {
1804 fn to_tokens(&self, tokens: &mut Tokens) {
1805 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001806 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001807 Constness::NotConst => {
1808 // nothing
1809 }
David Tolnay42602292016-10-01 22:25:45 -07001810 }
1811 }
1812 }
1813
David Tolnay4c9be372016-10-06 00:47:37 -07001814 impl ToTokens for Defaultness {
1815 fn to_tokens(&self, tokens: &mut Tokens) {
1816 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001818 Defaultness::Final => {
1819 // nothing
1820 }
1821 }
1822 }
1823 }
1824
1825 impl ToTokens for ImplPolarity {
1826 fn to_tokens(&self, tokens: &mut Tokens) {
1827 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001828 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001829 ImplPolarity::Positive => {
1830 // nothing
1831 }
1832 }
1833 }
1834 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001835}