blob: 97ba25b7b556a5e0b6a14ca5f448973fefd3944b [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,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800447 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700449}
450
Alex Crichton62a0a592017-05-22 13:58:53 -0700451ast_enum_of_structs! {
452 /// An argument in a function header.
453 ///
454 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
455 pub enum FnArg {
456 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800457 pub and_token: Token![&],
458 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700459 pub lifetime: Option<Lifetime>,
460 pub mutbl: Mutability,
461 }),
462 pub SelfValue(ArgSelf {
463 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800464 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700465 }),
466 pub Captured(ArgCaptured {
467 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800468 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800469 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700470 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500471 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800472 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700473 }
David Tolnay62f374c2016-10-02 13:37:00 -0700474}
475
David Tolnayedf2b992016-09-23 20:43:45 -0700476#[cfg(feature = "parsing")]
477pub mod parsing {
478 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700479
Michael Layzell92639a52017-06-01 00:07:44 -0400480 use synom::Synom;
David Tolnay57292da2017-12-27 21:03:33 -0500481 use proc_macro2::{TokenNode, Delimiter};
David Tolnay84aa0752016-10-02 23:01:13 -0700482
David Tolnay4c614be2017-11-10 00:02:38 -0800483 impl_synom!(Item "item" alt!(
484 syn!(ItemExternCrate) => { Item::ExternCrate }
485 |
486 syn!(ItemUse) => { Item::Use }
487 |
488 syn!(ItemStatic) => { Item::Static }
489 |
490 syn!(ItemConst) => { Item::Const }
491 |
492 syn!(ItemFn) => { Item::Fn }
493 |
494 syn!(ItemMod) => { Item::Mod }
495 |
496 syn!(ItemForeignMod) => { Item::ForeignMod }
497 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800498 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800499 |
500 syn!(ItemStruct) => { Item::Struct }
501 |
502 syn!(ItemEnum) => { Item::Enum }
503 |
504 syn!(ItemUnion) => { Item::Union }
505 |
506 syn!(ItemTrait) => { Item::Trait }
507 |
508 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
509 |
510 syn!(ItemImpl) => { Item::Impl }
511 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800512 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800513 |
514 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800515 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700516
David Tolnaydecf28d2017-11-11 11:56:45 -0800517 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500518 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700519 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800520 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700521 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500522 body: call!(tt::delimited) >>
David Tolnay57292da2017-12-27 21:03:33 -0500523 semi: cond!(!is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800524 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700525 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800526 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800527 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500528 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700529 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500530 tokens: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800531 },
David Tolnay57292da2017-12-27 21:03:33 -0500532 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800533 })
David Tolnayedf2b992016-09-23 20:43:45 -0700534 ));
535
David Tolnay500d8322017-12-18 00:32:51 -0800536 // TODO: figure out the actual grammar; is body required to be braced?
537 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500538 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800539 vis: syn!(Visibility) >>
540 macro_: keyword!(macro) >>
541 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500542 args: call!(tt::parenthesized) >>
543 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800544 (ItemMacro2 {
545 attrs: attrs,
546 vis: vis,
547 macro_token: macro_,
548 ident: ident,
549 args: args,
550 body: body,
551 })
552 ));
553
David Tolnay4c614be2017-11-10 00:02:38 -0800554 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500555 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700556 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800557 extern_: keyword!(extern) >>
558 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700559 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800560 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
561 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800562 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700563 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800564 vis: vis,
565 extern_token: extern_,
566 crate_token: crate_,
567 ident: ident,
568 rename: rename,
569 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800570 })
David Tolnayedf2b992016-09-23 20:43:45 -0700571 ));
572
David Tolnay4c614be2017-11-10 00:02:38 -0800573 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500574 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700575 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500577 leading_colon: option!(punct!(::)) >>
578 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
579 tree: switch!(value!(leading_colon.is_none() && prefix.is_empty()),
580 true => syn!(UseTree)
581 |
582 false => switch!(value!(prefix.empty_or_trailing()),
583 true => syn!(UseTree)
584 |
585 false => alt!(
586 tuple!(keyword!(as), syn!(Ident)) => {
587 |rename| UseTree::Path(UsePath {
588 ident: prefix.pop().unwrap().into_item(),
589 rename: Some(rename),
590 })
591 }
592 |
593 epsilon!() => {
594 |_| UseTree::Path(UsePath {
595 ident: prefix.pop().unwrap().into_item(),
596 rename: None,
597 })
598 }
599 )
600 )
601 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800603 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700604 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800605 vis: vis,
606 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500607 leading_colon: leading_colon,
608 prefix: prefix,
609 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800610 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800611 })
David Tolnay4a057422016-10-08 00:02:31 -0700612 ));
613
David Tolnay5f332a92017-12-26 00:42:45 -0500614 named!(use_prefix -> Ident, alt!(
615 syn!(Ident)
616 |
617 keyword!(self) => { Into::into }
618 |
619 keyword!(super) => { Into::into }
620 |
621 keyword!(crate) => { Into::into }
622 ));
623
624 impl_synom!(UseTree "use tree" alt!(
625 syn!(UsePath) => { UseTree::Path }
626 |
627 syn!(UseGlob) => { UseTree::Glob }
628 |
629 syn!(UseList) => { UseTree::List }
630 ));
631
632 impl_synom!(UsePath "use path" do_parse!(
633 ident: alt!(
634 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400635 |
David Tolnay5f332a92017-12-26 00:42:45 -0500636 keyword!(self) => { Into::into }
637 ) >>
638 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
639 (UsePath {
640 ident: ident,
641 rename: rename,
642 })
643 ));
David Tolnay4a057422016-10-08 00:02:31 -0700644
David Tolnay5f332a92017-12-26 00:42:45 -0500645 impl_synom!(UseGlob "use glob" do_parse!(
646 star: punct!(*) >>
647 (UseGlob {
648 star_token: star,
649 })
650 ));
David Tolnay4a057422016-10-08 00:02:31 -0700651
David Tolnay5f332a92017-12-26 00:42:45 -0500652 impl_synom!(UseList "use list" do_parse!(
653 list: braces!(Delimited::parse_terminated) >>
654 (UseList {
655 brace_token: list.1,
656 items: list.0,
657 })
658 ));
David Tolnay4a057422016-10-08 00:02:31 -0700659
David Tolnay4c614be2017-11-10 00:02:38 -0800660 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500661 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700662 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700664 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700665 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800667 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800668 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700669 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800670 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800671 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700672 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800673 vis: vis,
674 static_token: static_,
675 mutbl: mutability,
676 ident: ident,
677 colon_token: colon,
678 ty: Box::new(ty),
679 eq_token: eq,
680 expr: Box::new(value),
681 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800682 })
David Tolnay47a877c2016-10-01 16:50:55 -0700683 ));
684
David Tolnay4c614be2017-11-10 00:02:38 -0800685 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500686 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700687 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800688 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700689 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800690 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800691 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800692 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700693 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800694 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800695 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700696 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800697 vis: vis,
698 const_token: const_,
699 ident: ident,
700 colon_token: colon,
701 ty: Box::new(ty),
702 eq_token: eq,
703 expr: Box::new(value),
704 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800705 })
David Tolnay47a877c2016-10-01 16:50:55 -0700706 ));
707
David Tolnay4c614be2017-11-10 00:02:38 -0800708 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500709 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700710 vis: syn!(Visibility) >>
711 constness: syn!(Constness) >>
712 unsafety: syn!(Unsafety) >>
713 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800714 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700715 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700716 generics: syn!(Generics) >>
717 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800718 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700719 where_clause: syn!(WhereClause) >>
720 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500721 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700722 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400723 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800724 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700725 attrs: {
726 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700727 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700728 attrs
729 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800730 vis: vis,
731 constness: constness,
732 unsafety: unsafety,
733 abi: abi,
734 decl: Box::new(FnDecl {
735 dot_tokens: None,
736 fn_token: fn_,
737 paren_token: inputs.1,
738 inputs: inputs.0,
739 output: ret,
740 variadic: false,
741 generics: Generics {
742 where_clause: where_clause,
743 .. generics
744 },
745 }),
746 ident: ident,
747 block: Box::new(Block {
748 brace_token: inner_attrs_stmts.1,
749 stmts: (inner_attrs_stmts.0).1,
750 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800751 })
David Tolnay42602292016-10-01 22:25:45 -0700752 ));
753
Alex Crichton954046c2017-05-30 21:49:42 -0700754 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400755 named!(parse -> Self, alt!(
756 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800757 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400758 lt: option!(syn!(Lifetime)) >>
759 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800760 self_: keyword!(self) >>
761 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400762 (ArgSelfRef {
763 lifetime: lt,
764 mutbl: mutability,
765 and_token: and,
766 self_token: self_,
767 }.into())
768 )
769 |
770 do_parse!(
771 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800772 self_: keyword!(self) >>
773 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400774 (ArgSelf {
775 mutbl: mutability,
776 self_token: self_,
777 }.into())
778 )
779 |
780 do_parse!(
781 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800782 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800783 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400784 (ArgCaptured {
785 pat: pat,
786 ty: ty,
787 colon_token: colon,
788 }.into())
789 )
790 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800791 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400792 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700793 }
David Tolnay62f374c2016-10-02 13:37:00 -0700794
David Tolnay4c614be2017-11-10 00:02:38 -0800795 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500796 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700797 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800798 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700799 ident: syn!(Ident) >>
800 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800801 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700802 Vec::new(),
803 None,
804 Some(semi),
805 )}
David Tolnay37d10332016-10-13 20:51:04 -0700806 |
Alex Crichton954046c2017-05-30 21:49:42 -0700807 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700808 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500809 many0!(Attribute::parse_inner),
810 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400811 )
David Tolnay570695e2017-06-03 16:15:13 -0700812 ) => {|((inner_attrs, items), brace)| (
813 inner_attrs,
814 Some((brace, items)),
815 None,
816 )}
David Tolnay37d10332016-10-13 20:51:04 -0700817 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800818 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700819 attrs: {
820 let mut attrs = outer_attrs;
821 attrs.extend(content_semi.0);
822 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700823 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800824 vis: vis,
825 mod_token: mod_,
826 ident: ident,
827 content: content_semi.1,
828 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800829 })
David Tolnay35902302016-10-06 01:11:08 -0700830 ));
831
David Tolnay4c614be2017-11-10 00:02:38 -0800832 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500833 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700834 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500835 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800836 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700837 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800838 abi: abi,
839 brace_token: items.1,
840 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800841 })
David Tolnay35902302016-10-06 01:11:08 -0700842 ));
843
David Tolnay8894f602017-11-11 12:11:04 -0800844 impl_synom!(ForeignItem "foreign item" alt!(
845 syn!(ForeignItemFn) => { ForeignItem::Fn }
846 |
847 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800848 |
849 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800850 ));
David Tolnay35902302016-10-06 01:11:08 -0700851
David Tolnay8894f602017-11-11 12:11:04 -0800852 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500853 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700854 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800855 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700856 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700857 generics: syn!(Generics) >>
858 inputs: parens!(do_parse!(
859 args: call!(Delimited::parse_terminated) >>
860 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700862 (args, variadic)
863 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800864 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700865 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700867 ({
868 let ((inputs, variadic), parens) = inputs;
869 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800870 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700871 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700872 attrs: attrs,
873 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800874 decl: Box::new(FnDecl {
875 fn_token: fn_,
876 paren_token: parens,
877 inputs: inputs,
878 variadic: variadic.is_some(),
879 dot_tokens: variadic,
880 output: ret,
881 generics: Generics {
882 where_clause: where_clause,
883 .. generics
884 },
885 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700886 vis: vis,
887 }
David Tolnay35902302016-10-06 01:11:08 -0700888 })
889 ));
890
David Tolnay8894f602017-11-11 12:11:04 -0800891 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500892 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700893 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800894 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700895 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700896 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800897 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800898 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800899 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800900 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700901 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700902 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700903 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800904 ty: Box::new(ty),
905 mutbl: mutability,
906 static_token: static_,
907 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700908 vis: vis,
909 })
910 ));
911
David Tolnay199bcbb2017-11-12 10:33:52 -0800912 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500913 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800914 vis: syn!(Visibility) >>
915 type_: keyword!(type) >>
916 ident: syn!(Ident) >>
917 semi: punct!(;) >>
918 (ForeignItemType {
919 attrs: attrs,
920 vis: vis,
921 type_token: type_,
922 ident: ident,
923 semi_token: semi,
924 })
925 ));
926
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800927 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500928 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700929 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800930 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700931 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700932 generics: syn!(Generics) >>
933 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800934 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800935 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800936 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800937 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700938 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800939 vis: vis,
940 type_token: type_,
941 ident: ident,
942 generics: Generics {
943 where_clause: where_clause,
944 ..generics
945 },
946 eq_token: eq,
947 ty: Box::new(ty),
948 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800949 })
David Tolnay3cf52982016-10-01 17:11:37 -0700950 ));
951
David Tolnay4c614be2017-11-10 00:02:38 -0800952 impl_synom!(ItemStruct "struct item" switch!(
953 map!(syn!(DeriveInput), Into::into),
954 Item::Struct(item) => value!(item)
955 |
956 _ => reject!()
957 ));
David Tolnay42602292016-10-01 22:25:45 -0700958
David Tolnay4c614be2017-11-10 00:02:38 -0800959 impl_synom!(ItemEnum "enum item" switch!(
960 map!(syn!(DeriveInput), Into::into),
961 Item::Enum(item) => value!(item)
962 |
963 _ => reject!()
964 ));
965
966 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500967 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700968 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700970 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700971 generics: syn!(Generics) >>
972 where_clause: syn!(WhereClause) >>
973 fields: braces!(call!(Delimited::parse_terminated_with,
974 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800975 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700976 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800977 vis: vis,
978 union_token: union_,
979 ident: ident,
980 generics: Generics {
981 where_clause: where_clause,
982 .. generics
983 },
984 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800985 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700986 ));
987
David Tolnay4c614be2017-11-10 00:02:38 -0800988 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500989 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700990 vis: syn!(Visibility) >>
991 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500992 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800993 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700994 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700995 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800996 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700997 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700998 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700999 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001000 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -05001001 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001002 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001003 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001004 vis: vis,
1005 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001006 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001007 trait_token: trait_,
1008 ident: ident,
1009 generics: Generics {
1010 where_clause: where_clause,
1011 .. generics
1012 },
1013 colon_token: colon,
1014 supertraits: bounds.unwrap_or_default(),
1015 brace_token: body.1,
1016 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001017 })
David Tolnay0aecb732016-10-03 23:03:50 -07001018 ));
1019
David Tolnay4c614be2017-11-10 00:02:38 -08001020 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001021 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001022 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001024 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 for_: keyword!(for) >>
1026 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001027 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001028 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001029 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001030 unsafety: unsafety,
1031 impl_token: impl_,
1032 path: path,
1033 for_token: for_,
1034 dot2_token: dot2,
1035 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001036 })
David Tolnayf94e2362016-10-04 00:29:51 -07001037 ));
1038
David Tolnayda705bd2017-11-10 21:58:05 -08001039 impl_synom!(TraitItem "trait item" alt!(
1040 syn!(TraitItemConst) => { TraitItem::Const }
1041 |
1042 syn!(TraitItemMethod) => { TraitItem::Method }
1043 |
1044 syn!(TraitItemType) => { TraitItem::Type }
1045 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001046 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001047 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001048
David Tolnayda705bd2017-11-10 21:58:05 -08001049 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001050 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001051 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001052 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001053 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001054 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001055 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1056 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001057 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001058 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001059 const_token: const_,
1060 ident: ident,
1061 colon_token: colon,
1062 ty: ty,
1063 default: default,
1064 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001065 })
1066 ));
1067
David Tolnayda705bd2017-11-10 21:58:05 -08001068 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001069 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001070 constness: syn!(Constness) >>
1071 unsafety: syn!(Unsafety) >>
1072 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001073 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001074 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001075 generics: syn!(Generics) >>
1076 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001077 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001078 where_clause: syn!(WhereClause) >>
1079 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001080 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001081 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001082 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001083 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001084 ({
1085 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001086 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001087 None => (Vec::new(), None),
1088 };
David Tolnayda705bd2017-11-10 21:58:05 -08001089 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001090 attrs: {
1091 let mut attrs = outer_attrs;
1092 attrs.extend(inner_attrs);
1093 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001094 },
David Tolnayda705bd2017-11-10 21:58:05 -08001095 sig: MethodSig {
1096 constness: constness,
1097 unsafety: unsafety,
1098 abi: abi,
1099 ident: ident,
1100 decl: FnDecl {
1101 inputs: inputs.0,
1102 output: ret,
1103 variadic: false,
1104 fn_token: fn_,
1105 paren_token: inputs.1,
1106 dot_tokens: None,
1107 generics: Generics {
1108 where_clause: where_clause,
1109 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001110 },
1111 },
David Tolnayda705bd2017-11-10 21:58:05 -08001112 },
1113 default: stmts.map(|stmts| {
1114 Block {
1115 stmts: stmts.0,
1116 brace_token: stmts.1,
1117 }
1118 }),
1119 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001120 }
David Tolnay0aecb732016-10-03 23:03:50 -07001121 })
1122 ));
1123
David Tolnayda705bd2017-11-10 21:58:05 -08001124 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001125 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001126 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001127 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001128 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001129 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001130 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001131 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001132 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001133 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001134 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001135 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001136 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001137 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001138 type_token: type_,
1139 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001140 generics: Generics {
1141 where_clause: where_clause,
1142 .. generics
1143 },
David Tolnayda705bd2017-11-10 21:58:05 -08001144 colon_token: colon,
1145 bounds: bounds.unwrap_or_default(),
1146 default: default,
1147 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001148 })
1149 ));
1150
David Tolnaydecf28d2017-11-11 11:56:45 -08001151 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001152 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001153 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001154 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001155 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001156 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001157 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001158 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001159 })
1160 ));
1161
David Tolnay4c614be2017-11-10 00:02:38 -08001162 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001163 attrs: many0!(Attribute::parse_outer) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001164 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001165 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001166 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001167 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001168 polarity_path: alt!(
1169 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001170 polarity: syn!(ImplPolarity) >>
1171 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001172 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001173 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001174 )
1175 |
David Tolnay570695e2017-06-03 16:15:13 -07001176 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001177 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001178 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001179 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -05001180 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001181 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001182 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001183 defaultness: defaultness,
1184 unsafety: unsafety,
1185 impl_token: impl_,
1186 generics: Generics {
1187 where_clause: where_clause,
1188 .. generics
1189 },
1190 trait_: polarity_path,
1191 self_ty: Box::new(self_ty),
1192 brace_token: body.1,
1193 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001194 })
David Tolnay4c9be372016-10-06 00:47:37 -07001195 ));
1196
David Tolnay857628c2017-11-11 12:25:31 -08001197 impl_synom!(ImplItem "item in impl block" alt!(
1198 syn!(ImplItemConst) => { ImplItem::Const }
1199 |
1200 syn!(ImplItemMethod) => { ImplItem::Method }
1201 |
1202 syn!(ImplItemType) => { ImplItem::Type }
1203 |
1204 syn!(ImplItemMacro) => { ImplItem::Macro }
1205 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001206
David Tolnay857628c2017-11-11 12:25:31 -08001207 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001208 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001209 vis: syn!(Visibility) >>
1210 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001211 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001212 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001213 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001214 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001215 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001216 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001217 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001218 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001219 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001220 vis: vis,
1221 defaultness: defaultness,
1222 const_token: const_,
1223 ident: ident,
1224 colon_token: colon,
1225 ty: ty,
1226 eq_token: eq,
1227 expr: value,
1228 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001229 })
1230 ));
1231
David Tolnay857628c2017-11-11 12:25:31 -08001232 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001233 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001234 vis: syn!(Visibility) >>
1235 defaultness: syn!(Defaultness) >>
1236 constness: syn!(Constness) >>
1237 unsafety: syn!(Unsafety) >>
1238 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001239 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001240 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001241 generics: syn!(Generics) >>
1242 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001243 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001244 where_clause: syn!(WhereClause) >>
1245 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001246 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001247 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001248 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001249 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001250 attrs: {
1251 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001252 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001253 attrs
1254 },
David Tolnay857628c2017-11-11 12:25:31 -08001255 vis: vis,
1256 defaultness: defaultness,
1257 sig: MethodSig {
1258 constness: constness,
1259 unsafety: unsafety,
1260 abi: abi,
1261 ident: ident,
1262 decl: FnDecl {
1263 fn_token: fn_,
1264 paren_token: inputs.1,
1265 inputs: inputs.0,
1266 output: ret,
1267 variadic: false,
1268 generics: Generics {
1269 where_clause: where_clause,
1270 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001271 },
David Tolnay857628c2017-11-11 12:25:31 -08001272 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001273 },
David Tolnay857628c2017-11-11 12:25:31 -08001274 },
1275 block: Block {
1276 brace_token: inner_attrs_stmts.1,
1277 stmts: (inner_attrs_stmts.0).1,
1278 },
David Tolnay4c9be372016-10-06 00:47:37 -07001279 })
1280 ));
1281
David Tolnay857628c2017-11-11 12:25:31 -08001282 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001283 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001284 vis: syn!(Visibility) >>
1285 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001286 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001287 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001288 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001289 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001290 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001291 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001292 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001293 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001294 vis: vis,
1295 defaultness: defaultness,
1296 type_token: type_,
1297 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001298 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001299 eq_token: eq,
1300 ty: ty,
1301 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001302 })
1303 ));
1304
David Tolnay857628c2017-11-11 12:25:31 -08001305 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001306 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001307 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001308 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001309 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001310 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001311 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001312 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001313 })
1314 ));
1315
Alex Crichton954046c2017-05-30 21:49:42 -07001316 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001317 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001318 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001319 |
1320 epsilon!() => { |_| ImplPolarity::Positive }
1321 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001322 }
David Tolnay4c9be372016-10-06 00:47:37 -07001323
Alex Crichton954046c2017-05-30 21:49:42 -07001324 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001325 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001326 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001327 |
1328 epsilon!() => { |_| Constness::NotConst }
1329 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001330 }
David Tolnay42602292016-10-01 22:25:45 -07001331
Alex Crichton954046c2017-05-30 21:49:42 -07001332 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001333 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001335 |
1336 epsilon!() => { |_| Defaultness::Final }
1337 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001338 }
David Tolnay57292da2017-12-27 21:03:33 -05001339
1340 fn is_braced(tt: &TokenTree) -> bool {
1341 match tt.kind {
1342 TokenNode::Group(Delimiter::Brace, _) => true,
1343 _ => false,
1344 }
1345 }
David Tolnayedf2b992016-09-23 20:43:45 -07001346}
David Tolnay4a51dc72016-10-01 00:40:31 -07001347
1348#[cfg(feature = "printing")]
1349mod printing {
1350 use super::*;
1351 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001352 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001353 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001354
David Tolnay1bfa7332017-11-11 12:41:20 -08001355 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001356 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001357 tokens.append_all(self.attrs.outer());
1358 self.vis.to_tokens(tokens);
1359 self.extern_token.to_tokens(tokens);
1360 self.crate_token.to_tokens(tokens);
1361 self.ident.to_tokens(tokens);
1362 if let Some((ref as_token, ref rename)) = self.rename {
1363 as_token.to_tokens(tokens);
1364 rename.to_tokens(tokens);
1365 }
1366 self.semi_token.to_tokens(tokens);
1367 }
1368 }
1369
1370 impl ToTokens for ItemUse {
1371 fn to_tokens(&self, tokens: &mut Tokens) {
1372 tokens.append_all(self.attrs.outer());
1373 self.vis.to_tokens(tokens);
1374 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001375 self.leading_colon.to_tokens(tokens);
1376 self.prefix.to_tokens(tokens);
1377 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001378 self.semi_token.to_tokens(tokens);
1379 }
1380 }
1381
1382 impl ToTokens for ItemStatic {
1383 fn to_tokens(&self, tokens: &mut Tokens) {
1384 tokens.append_all(self.attrs.outer());
1385 self.vis.to_tokens(tokens);
1386 self.static_token.to_tokens(tokens);
1387 self.mutbl.to_tokens(tokens);
1388 self.ident.to_tokens(tokens);
1389 self.colon_token.to_tokens(tokens);
1390 self.ty.to_tokens(tokens);
1391 self.eq_token.to_tokens(tokens);
1392 self.expr.to_tokens(tokens);
1393 self.semi_token.to_tokens(tokens);
1394 }
1395 }
1396
1397 impl ToTokens for ItemConst {
1398 fn to_tokens(&self, tokens: &mut Tokens) {
1399 tokens.append_all(self.attrs.outer());
1400 self.vis.to_tokens(tokens);
1401 self.const_token.to_tokens(tokens);
1402 self.ident.to_tokens(tokens);
1403 self.colon_token.to_tokens(tokens);
1404 self.ty.to_tokens(tokens);
1405 self.eq_token.to_tokens(tokens);
1406 self.expr.to_tokens(tokens);
1407 self.semi_token.to_tokens(tokens);
1408 }
1409 }
1410
1411 impl ToTokens for ItemFn {
1412 fn to_tokens(&self, tokens: &mut Tokens) {
1413 tokens.append_all(self.attrs.outer());
1414 self.vis.to_tokens(tokens);
1415 self.constness.to_tokens(tokens);
1416 self.unsafety.to_tokens(tokens);
1417 self.abi.to_tokens(tokens);
1418 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1419 self.block.brace_token.surround(tokens, |tokens| {
1420 tokens.append_all(self.attrs.inner());
1421 tokens.append_all(&self.block.stmts);
1422 });
1423 }
1424 }
1425
1426 impl ToTokens for ItemMod {
1427 fn to_tokens(&self, tokens: &mut Tokens) {
1428 tokens.append_all(self.attrs.outer());
1429 self.vis.to_tokens(tokens);
1430 self.mod_token.to_tokens(tokens);
1431 self.ident.to_tokens(tokens);
1432 if let Some((ref brace, ref items)) = self.content {
1433 brace.surround(tokens, |tokens| {
1434 tokens.append_all(self.attrs.inner());
1435 tokens.append_all(items);
1436 });
1437 } else {
1438 TokensOrDefault(&self.semi).to_tokens(tokens);
1439 }
1440 }
1441 }
1442
1443 impl ToTokens for ItemForeignMod {
1444 fn to_tokens(&self, tokens: &mut Tokens) {
1445 tokens.append_all(self.attrs.outer());
1446 self.abi.to_tokens(tokens);
1447 self.brace_token.surround(tokens, |tokens| {
1448 tokens.append_all(&self.items);
1449 });
1450 }
1451 }
1452
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001453 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001454 fn to_tokens(&self, tokens: &mut Tokens) {
1455 tokens.append_all(self.attrs.outer());
1456 self.vis.to_tokens(tokens);
1457 self.type_token.to_tokens(tokens);
1458 self.ident.to_tokens(tokens);
1459 self.generics.to_tokens(tokens);
1460 self.generics.where_clause.to_tokens(tokens);
1461 self.eq_token.to_tokens(tokens);
1462 self.ty.to_tokens(tokens);
1463 self.semi_token.to_tokens(tokens);
1464 }
1465 }
1466
1467 impl ToTokens for ItemEnum {
1468 fn to_tokens(&self, tokens: &mut Tokens) {
1469 tokens.append_all(self.attrs.outer());
1470 self.vis.to_tokens(tokens);
1471 self.enum_token.to_tokens(tokens);
1472 self.ident.to_tokens(tokens);
1473 self.generics.to_tokens(tokens);
1474 self.generics.where_clause.to_tokens(tokens);
1475 self.brace_token.surround(tokens, |tokens| {
1476 self.variants.to_tokens(tokens);
1477 });
1478 }
1479 }
1480
1481 impl ToTokens for ItemStruct {
1482 fn to_tokens(&self, tokens: &mut Tokens) {
1483 tokens.append_all(self.attrs.outer());
1484 self.vis.to_tokens(tokens);
1485 self.struct_token.to_tokens(tokens);
1486 self.ident.to_tokens(tokens);
1487 self.generics.to_tokens(tokens);
1488 match self.data {
1489 VariantData::Struct(..) => {
1490 self.generics.where_clause.to_tokens(tokens);
1491 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001492 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001493 VariantData::Tuple(..) => {
1494 self.data.to_tokens(tokens);
1495 self.generics.where_clause.to_tokens(tokens);
1496 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001497 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001498 VariantData::Unit => {
1499 self.generics.where_clause.to_tokens(tokens);
1500 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001501 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001502 }
1503 }
1504 }
1505
1506 impl ToTokens for ItemUnion {
1507 fn to_tokens(&self, tokens: &mut Tokens) {
1508 tokens.append_all(self.attrs.outer());
1509 self.vis.to_tokens(tokens);
1510 self.union_token.to_tokens(tokens);
1511 self.ident.to_tokens(tokens);
1512 self.generics.to_tokens(tokens);
1513 self.generics.where_clause.to_tokens(tokens);
1514 // XXX: Should we handle / complain when using a
1515 // non-VariantData::Struct Variant in Union?
1516 self.data.to_tokens(tokens);
1517 }
1518 }
1519
1520 impl ToTokens for ItemTrait {
1521 fn to_tokens(&self, tokens: &mut Tokens) {
1522 tokens.append_all(self.attrs.outer());
1523 self.vis.to_tokens(tokens);
1524 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001525 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001526 self.trait_token.to_tokens(tokens);
1527 self.ident.to_tokens(tokens);
1528 self.generics.to_tokens(tokens);
1529 if !self.supertraits.is_empty() {
1530 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1531 self.supertraits.to_tokens(tokens);
1532 }
1533 self.generics.where_clause.to_tokens(tokens);
1534 self.brace_token.surround(tokens, |tokens| {
1535 tokens.append_all(&self.items);
1536 });
1537 }
1538 }
1539
1540 impl ToTokens for ItemDefaultImpl {
1541 fn to_tokens(&self, tokens: &mut Tokens) {
1542 tokens.append_all(self.attrs.outer());
1543 self.unsafety.to_tokens(tokens);
1544 self.impl_token.to_tokens(tokens);
1545 self.path.to_tokens(tokens);
1546 self.for_token.to_tokens(tokens);
1547 self.dot2_token.to_tokens(tokens);
1548 self.brace_token.surround(tokens, |_tokens| {});
1549 }
1550 }
1551
1552 impl ToTokens for ItemImpl {
1553 fn to_tokens(&self, tokens: &mut Tokens) {
1554 tokens.append_all(self.attrs.outer());
1555 self.defaultness.to_tokens(tokens);
1556 self.unsafety.to_tokens(tokens);
1557 self.impl_token.to_tokens(tokens);
1558 self.generics.to_tokens(tokens);
1559 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1560 polarity.to_tokens(tokens);
1561 path.to_tokens(tokens);
1562 for_token.to_tokens(tokens);
1563 }
1564 self.self_ty.to_tokens(tokens);
1565 self.generics.where_clause.to_tokens(tokens);
1566 self.brace_token.surround(tokens, |tokens| {
1567 tokens.append_all(&self.items);
1568 });
1569 }
1570 }
1571
1572 impl ToTokens for ItemMacro {
1573 fn to_tokens(&self, tokens: &mut Tokens) {
1574 tokens.append_all(self.attrs.outer());
1575 self.mac.path.to_tokens(tokens);
1576 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001577 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001578 self.mac.tokens.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001579 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001580 }
1581 }
David Tolnay42602292016-10-01 22:25:45 -07001582
David Tolnay500d8322017-12-18 00:32:51 -08001583 impl ToTokens for ItemMacro2 {
1584 fn to_tokens(&self, tokens: &mut Tokens) {
1585 tokens.append_all(self.attrs.outer());
1586 self.vis.to_tokens(tokens);
1587 self.macro_token.to_tokens(tokens);
1588 self.ident.to_tokens(tokens);
1589 self.args.to_tokens(tokens);
1590 self.body.to_tokens(tokens);
1591 }
1592 }
1593
David Tolnay5f332a92017-12-26 00:42:45 -05001594 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001595 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001596 self.ident.to_tokens(tokens);
1597 if let Some((ref as_token, ref rename)) = self.rename {
1598 as_token.to_tokens(tokens);
1599 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001600 }
David Tolnay4a057422016-10-08 00:02:31 -07001601 }
1602 }
1603
David Tolnay5f332a92017-12-26 00:42:45 -05001604 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001605 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001606 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 }
1608 }
1609
David Tolnay5f332a92017-12-26 00:42:45 -05001610 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001611 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001612 self.brace_token.surround(tokens, |tokens| {
1613 self.items.to_tokens(tokens);
1614 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001615 }
1616 }
1617
David Tolnay1bfa7332017-11-11 12:41:20 -08001618 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001619 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001620 tokens.append_all(self.attrs.outer());
1621 self.const_token.to_tokens(tokens);
1622 self.ident.to_tokens(tokens);
1623 self.colon_token.to_tokens(tokens);
1624 self.ty.to_tokens(tokens);
1625 if let Some((ref eq_token, ref default)) = self.default {
1626 eq_token.to_tokens(tokens);
1627 default.to_tokens(tokens);
1628 }
1629 self.semi_token.to_tokens(tokens);
1630 }
1631 }
1632
1633 impl ToTokens for TraitItemMethod {
1634 fn to_tokens(&self, tokens: &mut Tokens) {
1635 tokens.append_all(self.attrs.outer());
1636 self.sig.to_tokens(tokens);
1637 match self.default {
1638 Some(ref block) => {
1639 block.brace_token.surround(tokens, |tokens| {
1640 tokens.append_all(self.attrs.inner());
1641 tokens.append_all(&block.stmts);
1642 });
David Tolnayca085422016-10-04 00:12:38 -07001643 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001644 None => {
1645 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001646 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001647 }
1648 }
1649 }
1650
1651 impl ToTokens for TraitItemType {
1652 fn to_tokens(&self, tokens: &mut Tokens) {
1653 tokens.append_all(self.attrs.outer());
1654 self.type_token.to_tokens(tokens);
1655 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001656 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001657 if !self.bounds.is_empty() {
1658 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1659 self.bounds.to_tokens(tokens);
1660 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001661 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001662 if let Some((ref eq_token, ref default)) = self.default {
1663 eq_token.to_tokens(tokens);
1664 default.to_tokens(tokens);
1665 }
1666 self.semi_token.to_tokens(tokens);
1667 }
1668 }
1669
1670 impl ToTokens for TraitItemMacro {
1671 fn to_tokens(&self, tokens: &mut Tokens) {
1672 tokens.append_all(self.attrs.outer());
1673 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001674 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001675 }
1676 }
1677
David Tolnay857628c2017-11-11 12:25:31 -08001678 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001679 fn to_tokens(&self, tokens: &mut Tokens) {
1680 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001681 self.vis.to_tokens(tokens);
1682 self.defaultness.to_tokens(tokens);
1683 self.const_token.to_tokens(tokens);
1684 self.ident.to_tokens(tokens);
1685 self.colon_token.to_tokens(tokens);
1686 self.ty.to_tokens(tokens);
1687 self.eq_token.to_tokens(tokens);
1688 self.expr.to_tokens(tokens);
1689 self.semi_token.to_tokens(tokens);
1690 }
1691 }
1692
1693 impl ToTokens for ImplItemMethod {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 tokens.append_all(self.attrs.outer());
1696 self.vis.to_tokens(tokens);
1697 self.defaultness.to_tokens(tokens);
1698 self.sig.to_tokens(tokens);
1699 self.block.brace_token.surround(tokens, |tokens| {
1700 tokens.append_all(self.attrs.inner());
1701 tokens.append_all(&self.block.stmts);
1702 });
1703 }
1704 }
1705
1706 impl ToTokens for ImplItemType {
1707 fn to_tokens(&self, tokens: &mut Tokens) {
1708 tokens.append_all(self.attrs.outer());
1709 self.vis.to_tokens(tokens);
1710 self.defaultness.to_tokens(tokens);
1711 self.type_token.to_tokens(tokens);
1712 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001713 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001714 self.eq_token.to_tokens(tokens);
1715 self.ty.to_tokens(tokens);
1716 self.semi_token.to_tokens(tokens);
1717 }
1718 }
1719
1720 impl ToTokens for ImplItemMacro {
1721 fn to_tokens(&self, tokens: &mut Tokens) {
1722 tokens.append_all(self.attrs.outer());
1723 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001724 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001725 }
1726 }
1727
David Tolnay8894f602017-11-11 12:11:04 -08001728 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001729 fn to_tokens(&self, tokens: &mut Tokens) {
1730 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001731 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001732 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1733 self.semi_token.to_tokens(tokens);
1734 }
1735 }
1736
1737 impl ToTokens for ForeignItemStatic {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
1739 tokens.append_all(self.attrs.outer());
1740 self.vis.to_tokens(tokens);
1741 self.static_token.to_tokens(tokens);
1742 self.mutbl.to_tokens(tokens);
1743 self.ident.to_tokens(tokens);
1744 self.colon_token.to_tokens(tokens);
1745 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001746 self.semi_token.to_tokens(tokens);
1747 }
1748 }
1749
David Tolnay199bcbb2017-11-12 10:33:52 -08001750 impl ToTokens for ForeignItemType {
1751 fn to_tokens(&self, tokens: &mut Tokens) {
1752 tokens.append_all(self.attrs.outer());
1753 self.vis.to_tokens(tokens);
1754 self.type_token.to_tokens(tokens);
1755 self.ident.to_tokens(tokens);
1756 self.semi_token.to_tokens(tokens);
1757 }
1758 }
1759
David Tolnay570695e2017-06-03 16:15:13 -07001760 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001761 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001762 self.constness.to_tokens(tokens);
1763 self.unsafety.to_tokens(tokens);
1764 self.abi.to_tokens(tokens);
1765 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001766 }
1767 }
1768
David Tolnay570695e2017-06-03 16:15:13 -07001769 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001770
1771 impl<'a> ToTokens for NamedDecl<'a> {
1772 fn to_tokens(&self, tokens: &mut Tokens) {
1773 self.0.fn_token.to_tokens(tokens);
1774 self.1.to_tokens(tokens);
1775 self.0.generics.to_tokens(tokens);
1776 self.0.paren_token.surround(tokens, |tokens| {
1777 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001778
1779 if self.0.variadic {
1780 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001781 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001782 }
Alex Crichton259ee532017-07-14 06:51:02 -07001783 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001784 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001785 });
1786 self.0.output.to_tokens(tokens);
1787 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001788 }
1789 }
1790
Alex Crichton62a0a592017-05-22 13:58:53 -07001791 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001792 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001793 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001794 self.lifetime.to_tokens(tokens);
1795 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001796 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001797 }
1798 }
1799
1800 impl ToTokens for ArgSelf {
1801 fn to_tokens(&self, tokens: &mut Tokens) {
1802 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001803 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001804 }
1805 }
1806
1807 impl ToTokens for ArgCaptured {
1808 fn to_tokens(&self, tokens: &mut Tokens) {
1809 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001811 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001812 }
1813 }
1814
David Tolnay42602292016-10-01 22:25:45 -07001815 impl ToTokens for Constness {
1816 fn to_tokens(&self, tokens: &mut Tokens) {
1817 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001818 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001819 Constness::NotConst => {
1820 // nothing
1821 }
David Tolnay42602292016-10-01 22:25:45 -07001822 }
1823 }
1824 }
1825
David Tolnay4c9be372016-10-06 00:47:37 -07001826 impl ToTokens for Defaultness {
1827 fn to_tokens(&self, tokens: &mut Tokens) {
1828 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001829 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001830 Defaultness::Final => {
1831 // nothing
1832 }
1833 }
1834 }
1835 }
1836
1837 impl ToTokens for ImplPolarity {
1838 fn to_tokens(&self, tokens: &mut Tokens) {
1839 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001840 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001841 ImplPolarity::Positive => {
1842 // nothing
1843 }
1844 }
1845 }
1846 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001847}