blob: 6027065578e3d22f4b06b359c20083c22405106e [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 Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700472 }
David Tolnay62f374c2016-10-02 13:37:00 -0700473}
474
David Tolnayedf2b992016-09-23 20:43:45 -0700475#[cfg(feature = "parsing")]
476pub mod parsing {
477 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700478
Michael Layzell92639a52017-06-01 00:07:44 -0400479 use synom::Synom;
David Tolnay57292da2017-12-27 21:03:33 -0500480 use proc_macro2::{TokenNode, Delimiter};
David Tolnay84aa0752016-10-02 23:01:13 -0700481
David Tolnay4c614be2017-11-10 00:02:38 -0800482 impl_synom!(Item "item" alt!(
483 syn!(ItemExternCrate) => { Item::ExternCrate }
484 |
485 syn!(ItemUse) => { Item::Use }
486 |
487 syn!(ItemStatic) => { Item::Static }
488 |
489 syn!(ItemConst) => { Item::Const }
490 |
491 syn!(ItemFn) => { Item::Fn }
492 |
493 syn!(ItemMod) => { Item::Mod }
494 |
495 syn!(ItemForeignMod) => { Item::ForeignMod }
496 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800497 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800498 |
499 syn!(ItemStruct) => { Item::Struct }
500 |
501 syn!(ItemEnum) => { Item::Enum }
502 |
503 syn!(ItemUnion) => { Item::Union }
504 |
505 syn!(ItemTrait) => { Item::Trait }
506 |
507 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
508 |
509 syn!(ItemImpl) => { Item::Impl }
510 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800511 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800512 |
513 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800514 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700515
David Tolnaydecf28d2017-11-11 11:56:45 -0800516 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500517 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700518 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800519 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700520 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500521 body: call!(tt::delimited) >>
David Tolnay57292da2017-12-27 21:03:33 -0500522 semi: cond!(!is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800523 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700524 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800525 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800526 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500527 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700528 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500529 tokens: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800530 },
David Tolnay57292da2017-12-27 21:03:33 -0500531 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800532 })
David Tolnayedf2b992016-09-23 20:43:45 -0700533 ));
534
David Tolnay500d8322017-12-18 00:32:51 -0800535 // TODO: figure out the actual grammar; is body required to be braced?
536 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500537 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800538 vis: syn!(Visibility) >>
539 macro_: keyword!(macro) >>
540 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500541 args: call!(tt::parenthesized) >>
542 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800543 (ItemMacro2 {
544 attrs: attrs,
545 vis: vis,
546 macro_token: macro_,
547 ident: ident,
548 args: args,
549 body: body,
550 })
551 ));
552
David Tolnay4c614be2017-11-10 00:02:38 -0800553 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500554 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700555 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800556 extern_: keyword!(extern) >>
557 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700558 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800559 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
560 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800561 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700562 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800563 vis: vis,
564 extern_token: extern_,
565 crate_token: crate_,
566 ident: ident,
567 rename: rename,
568 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800569 })
David Tolnayedf2b992016-09-23 20:43:45 -0700570 ));
571
David Tolnay4c614be2017-11-10 00:02:38 -0800572 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500573 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700574 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800575 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500576 leading_colon: option!(punct!(::)) >>
577 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
578 tree: switch!(value!(leading_colon.is_none() && prefix.is_empty()),
579 true => syn!(UseTree)
580 |
581 false => switch!(value!(prefix.empty_or_trailing()),
582 true => syn!(UseTree)
583 |
584 false => alt!(
585 tuple!(keyword!(as), syn!(Ident)) => {
586 |rename| UseTree::Path(UsePath {
587 ident: prefix.pop().unwrap().into_item(),
588 rename: Some(rename),
589 })
590 }
591 |
592 epsilon!() => {
593 |_| UseTree::Path(UsePath {
594 ident: prefix.pop().unwrap().into_item(),
595 rename: None,
596 })
597 }
598 )
599 )
600 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800601 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800602 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700603 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800604 vis: vis,
605 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500606 leading_colon: leading_colon,
607 prefix: prefix,
608 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800609 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800610 })
David Tolnay4a057422016-10-08 00:02:31 -0700611 ));
612
David Tolnay5f332a92017-12-26 00:42:45 -0500613 named!(use_prefix -> Ident, alt!(
614 syn!(Ident)
615 |
616 keyword!(self) => { Into::into }
617 |
618 keyword!(super) => { Into::into }
619 |
620 keyword!(crate) => { Into::into }
621 ));
622
623 impl_synom!(UseTree "use tree" alt!(
624 syn!(UsePath) => { UseTree::Path }
625 |
626 syn!(UseGlob) => { UseTree::Glob }
627 |
628 syn!(UseList) => { UseTree::List }
629 ));
630
631 impl_synom!(UsePath "use path" do_parse!(
632 ident: alt!(
633 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400634 |
David Tolnay5f332a92017-12-26 00:42:45 -0500635 keyword!(self) => { Into::into }
636 ) >>
637 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
638 (UsePath {
639 ident: ident,
640 rename: rename,
641 })
642 ));
David Tolnay4a057422016-10-08 00:02:31 -0700643
David Tolnay5f332a92017-12-26 00:42:45 -0500644 impl_synom!(UseGlob "use glob" do_parse!(
645 star: punct!(*) >>
646 (UseGlob {
647 star_token: star,
648 })
649 ));
David Tolnay4a057422016-10-08 00:02:31 -0700650
David Tolnay5f332a92017-12-26 00:42:45 -0500651 impl_synom!(UseList "use list" do_parse!(
652 list: braces!(Delimited::parse_terminated) >>
653 (UseList {
654 brace_token: list.1,
655 items: list.0,
656 })
657 ));
David Tolnay4a057422016-10-08 00:02:31 -0700658
David Tolnay4c614be2017-11-10 00:02:38 -0800659 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500660 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700661 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700663 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700664 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800666 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800667 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700668 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800669 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800670 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700671 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800672 vis: vis,
673 static_token: static_,
674 mutbl: mutability,
675 ident: ident,
676 colon_token: colon,
677 ty: Box::new(ty),
678 eq_token: eq,
679 expr: Box::new(value),
680 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800681 })
David Tolnay47a877c2016-10-01 16:50:55 -0700682 ));
683
David Tolnay4c614be2017-11-10 00:02:38 -0800684 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500685 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700686 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800687 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700688 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800689 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800690 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800691 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700692 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800693 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800694 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700695 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800696 vis: vis,
697 const_token: const_,
698 ident: ident,
699 colon_token: colon,
700 ty: Box::new(ty),
701 eq_token: eq,
702 expr: Box::new(value),
703 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800704 })
David Tolnay47a877c2016-10-01 16:50:55 -0700705 ));
706
David Tolnay4c614be2017-11-10 00:02:38 -0800707 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500708 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700709 vis: syn!(Visibility) >>
710 constness: syn!(Constness) >>
711 unsafety: syn!(Unsafety) >>
712 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800713 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700714 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700715 generics: syn!(Generics) >>
716 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800717 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700718 where_clause: syn!(WhereClause) >>
719 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500720 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700721 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400722 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800723 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700724 attrs: {
725 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700726 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700727 attrs
728 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800729 vis: vis,
730 constness: constness,
731 unsafety: unsafety,
732 abi: abi,
733 decl: Box::new(FnDecl {
734 dot_tokens: None,
735 fn_token: fn_,
736 paren_token: inputs.1,
737 inputs: inputs.0,
738 output: ret,
739 variadic: false,
740 generics: Generics {
741 where_clause: where_clause,
742 .. generics
743 },
744 }),
745 ident: ident,
746 block: Box::new(Block {
747 brace_token: inner_attrs_stmts.1,
748 stmts: (inner_attrs_stmts.0).1,
749 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800750 })
David Tolnay42602292016-10-01 22:25:45 -0700751 ));
752
Alex Crichton954046c2017-05-30 21:49:42 -0700753 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400754 named!(parse -> Self, alt!(
755 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800756 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400757 lt: option!(syn!(Lifetime)) >>
758 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800759 self_: keyword!(self) >>
760 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400761 (ArgSelfRef {
762 lifetime: lt,
763 mutbl: mutability,
764 and_token: and,
765 self_token: self_,
766 }.into())
767 )
768 |
769 do_parse!(
770 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800771 self_: keyword!(self) >>
772 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400773 (ArgSelf {
774 mutbl: mutability,
775 self_token: self_,
776 }.into())
777 )
778 |
779 do_parse!(
780 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800781 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800782 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400783 (ArgCaptured {
784 pat: pat,
785 ty: ty,
786 colon_token: colon,
787 }.into())
788 )
789 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800790 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400791 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700792 }
David Tolnay62f374c2016-10-02 13:37:00 -0700793
David Tolnay4c614be2017-11-10 00:02:38 -0800794 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500795 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700796 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800797 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700798 ident: syn!(Ident) >>
799 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800800 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700801 Vec::new(),
802 None,
803 Some(semi),
804 )}
David Tolnay37d10332016-10-13 20:51:04 -0700805 |
Alex Crichton954046c2017-05-30 21:49:42 -0700806 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700807 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500808 many0!(Attribute::parse_inner),
809 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400810 )
David Tolnay570695e2017-06-03 16:15:13 -0700811 ) => {|((inner_attrs, items), brace)| (
812 inner_attrs,
813 Some((brace, items)),
814 None,
815 )}
David Tolnay37d10332016-10-13 20:51:04 -0700816 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800817 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700818 attrs: {
819 let mut attrs = outer_attrs;
820 attrs.extend(content_semi.0);
821 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700822 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800823 vis: vis,
824 mod_token: mod_,
825 ident: ident,
826 content: content_semi.1,
827 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800828 })
David Tolnay35902302016-10-06 01:11:08 -0700829 ));
830
David Tolnay4c614be2017-11-10 00:02:38 -0800831 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500832 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700833 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500834 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800835 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700836 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800837 abi: abi,
838 brace_token: items.1,
839 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800840 })
David Tolnay35902302016-10-06 01:11:08 -0700841 ));
842
David Tolnay8894f602017-11-11 12:11:04 -0800843 impl_synom!(ForeignItem "foreign item" alt!(
844 syn!(ForeignItemFn) => { ForeignItem::Fn }
845 |
846 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800847 |
848 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800849 ));
David Tolnay35902302016-10-06 01:11:08 -0700850
David Tolnay8894f602017-11-11 12:11:04 -0800851 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500852 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700853 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800854 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700855 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700856 generics: syn!(Generics) >>
857 inputs: parens!(do_parse!(
858 args: call!(Delimited::parse_terminated) >>
859 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800860 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700861 (args, variadic)
862 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800863 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700864 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800865 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700866 ({
867 let ((inputs, variadic), parens) = inputs;
868 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800869 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700870 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700871 attrs: attrs,
872 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800873 decl: Box::new(FnDecl {
874 fn_token: fn_,
875 paren_token: parens,
876 inputs: inputs,
877 variadic: variadic.is_some(),
878 dot_tokens: variadic,
879 output: ret,
880 generics: Generics {
881 where_clause: where_clause,
882 .. generics
883 },
884 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700885 vis: vis,
886 }
David Tolnay35902302016-10-06 01:11:08 -0700887 })
888 ));
889
David Tolnay8894f602017-11-11 12:11:04 -0800890 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500891 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700892 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800893 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700894 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700895 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800896 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800897 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800898 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800899 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700900 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700901 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700902 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800903 ty: Box::new(ty),
904 mutbl: mutability,
905 static_token: static_,
906 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700907 vis: vis,
908 })
909 ));
910
David Tolnay199bcbb2017-11-12 10:33:52 -0800911 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500912 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800913 vis: syn!(Visibility) >>
914 type_: keyword!(type) >>
915 ident: syn!(Ident) >>
916 semi: punct!(;) >>
917 (ForeignItemType {
918 attrs: attrs,
919 vis: vis,
920 type_token: type_,
921 ident: ident,
922 semi_token: semi,
923 })
924 ));
925
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800926 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500927 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700928 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800929 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700930 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700931 generics: syn!(Generics) >>
932 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800934 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800935 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800936 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700937 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800938 vis: vis,
939 type_token: type_,
940 ident: ident,
941 generics: Generics {
942 where_clause: where_clause,
943 ..generics
944 },
945 eq_token: eq,
946 ty: Box::new(ty),
947 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800948 })
David Tolnay3cf52982016-10-01 17:11:37 -0700949 ));
950
David Tolnay4c614be2017-11-10 00:02:38 -0800951 impl_synom!(ItemStruct "struct item" switch!(
952 map!(syn!(DeriveInput), Into::into),
953 Item::Struct(item) => value!(item)
954 |
955 _ => reject!()
956 ));
David Tolnay42602292016-10-01 22:25:45 -0700957
David Tolnay4c614be2017-11-10 00:02:38 -0800958 impl_synom!(ItemEnum "enum item" switch!(
959 map!(syn!(DeriveInput), Into::into),
960 Item::Enum(item) => value!(item)
961 |
962 _ => reject!()
963 ));
964
965 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500966 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700967 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800968 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700969 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700970 generics: syn!(Generics) >>
971 where_clause: syn!(WhereClause) >>
972 fields: braces!(call!(Delimited::parse_terminated_with,
973 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800974 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700975 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800976 vis: vis,
977 union_token: union_,
978 ident: ident,
979 generics: Generics {
980 where_clause: where_clause,
981 .. generics
982 },
983 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800984 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700985 ));
986
David Tolnay4c614be2017-11-10 00:02:38 -0800987 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500988 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700989 vis: syn!(Visibility) >>
990 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500991 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800992 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700993 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700994 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800995 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700996 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700997 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700998 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700999 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -05001000 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001001 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001002 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001003 vis: vis,
1004 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001005 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001006 trait_token: trait_,
1007 ident: ident,
1008 generics: Generics {
1009 where_clause: where_clause,
1010 .. generics
1011 },
1012 colon_token: colon,
1013 supertraits: bounds.unwrap_or_default(),
1014 brace_token: body.1,
1015 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001016 })
David Tolnay0aecb732016-10-03 23:03:50 -07001017 ));
1018
David Tolnay4c614be2017-11-10 00:02:38 -08001019 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001020 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001021 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001023 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001024 for_: keyword!(for) >>
1025 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001026 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001027 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001028 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001029 unsafety: unsafety,
1030 impl_token: impl_,
1031 path: path,
1032 for_token: for_,
1033 dot2_token: dot2,
1034 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001035 })
David Tolnayf94e2362016-10-04 00:29:51 -07001036 ));
1037
David Tolnayda705bd2017-11-10 21:58:05 -08001038 impl_synom!(TraitItem "trait item" alt!(
1039 syn!(TraitItemConst) => { TraitItem::Const }
1040 |
1041 syn!(TraitItemMethod) => { TraitItem::Method }
1042 |
1043 syn!(TraitItemType) => { TraitItem::Type }
1044 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001045 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001046 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001047
David Tolnayda705bd2017-11-10 21:58:05 -08001048 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001049 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001051 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001052 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001053 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001054 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1055 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001056 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001057 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001058 const_token: const_,
1059 ident: ident,
1060 colon_token: colon,
1061 ty: ty,
1062 default: default,
1063 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001064 })
1065 ));
1066
David Tolnayda705bd2017-11-10 21:58:05 -08001067 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001068 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001069 constness: syn!(Constness) >>
1070 unsafety: syn!(Unsafety) >>
1071 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001072 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001073 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001074 generics: syn!(Generics) >>
1075 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001076 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001077 where_clause: syn!(WhereClause) >>
1078 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001079 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001080 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001081 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001082 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001083 ({
1084 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001085 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001086 None => (Vec::new(), None),
1087 };
David Tolnayda705bd2017-11-10 21:58:05 -08001088 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001089 attrs: {
1090 let mut attrs = outer_attrs;
1091 attrs.extend(inner_attrs);
1092 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001093 },
David Tolnayda705bd2017-11-10 21:58:05 -08001094 sig: MethodSig {
1095 constness: constness,
1096 unsafety: unsafety,
1097 abi: abi,
1098 ident: ident,
1099 decl: FnDecl {
1100 inputs: inputs.0,
1101 output: ret,
1102 variadic: false,
1103 fn_token: fn_,
1104 paren_token: inputs.1,
1105 dot_tokens: None,
1106 generics: Generics {
1107 where_clause: where_clause,
1108 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001109 },
1110 },
David Tolnayda705bd2017-11-10 21:58:05 -08001111 },
1112 default: stmts.map(|stmts| {
1113 Block {
1114 stmts: stmts.0,
1115 brace_token: stmts.1,
1116 }
1117 }),
1118 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001119 }
David Tolnay0aecb732016-10-03 23:03:50 -07001120 })
1121 ));
1122
David Tolnayda705bd2017-11-10 21:58:05 -08001123 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001124 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001125 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001126 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001127 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001128 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001129 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001130 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001131 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001132 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001133 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001134 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001135 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001136 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001137 type_token: type_,
1138 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001139 generics: Generics {
1140 where_clause: where_clause,
1141 .. generics
1142 },
David Tolnayda705bd2017-11-10 21:58:05 -08001143 colon_token: colon,
1144 bounds: bounds.unwrap_or_default(),
1145 default: default,
1146 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001147 })
1148 ));
1149
David Tolnaydecf28d2017-11-11 11:56:45 -08001150 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001151 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001152 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001153 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001154 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001155 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001156 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001157 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001158 })
1159 ));
1160
David Tolnay4c614be2017-11-10 00:02:38 -08001161 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001162 attrs: many0!(Attribute::parse_outer) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001163 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001164 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001165 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001166 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001167 polarity_path: alt!(
1168 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001169 polarity: syn!(ImplPolarity) >>
1170 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001171 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001172 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001173 )
1174 |
David Tolnay570695e2017-06-03 16:15:13 -07001175 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001176 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001177 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001178 where_clause: syn!(WhereClause) >>
David Tolnay2c136452017-12-27 14:13:32 -05001179 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001180 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001181 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001182 defaultness: defaultness,
1183 unsafety: unsafety,
1184 impl_token: impl_,
1185 generics: Generics {
1186 where_clause: where_clause,
1187 .. generics
1188 },
1189 trait_: polarity_path,
1190 self_ty: Box::new(self_ty),
1191 brace_token: body.1,
1192 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001193 })
David Tolnay4c9be372016-10-06 00:47:37 -07001194 ));
1195
David Tolnay857628c2017-11-11 12:25:31 -08001196 impl_synom!(ImplItem "item in impl block" alt!(
1197 syn!(ImplItemConst) => { ImplItem::Const }
1198 |
1199 syn!(ImplItemMethod) => { ImplItem::Method }
1200 |
1201 syn!(ImplItemType) => { ImplItem::Type }
1202 |
1203 syn!(ImplItemMacro) => { ImplItem::Macro }
1204 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001205
David Tolnay857628c2017-11-11 12:25:31 -08001206 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001207 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001208 vis: syn!(Visibility) >>
1209 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001211 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001212 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001213 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001214 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001215 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001216 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001217 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001218 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001219 vis: vis,
1220 defaultness: defaultness,
1221 const_token: const_,
1222 ident: ident,
1223 colon_token: colon,
1224 ty: ty,
1225 eq_token: eq,
1226 expr: value,
1227 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001228 })
1229 ));
1230
David Tolnay857628c2017-11-11 12:25:31 -08001231 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001232 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001233 vis: syn!(Visibility) >>
1234 defaultness: syn!(Defaultness) >>
1235 constness: syn!(Constness) >>
1236 unsafety: syn!(Unsafety) >>
1237 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001238 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001239 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001240 generics: syn!(Generics) >>
1241 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001242 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001243 where_clause: syn!(WhereClause) >>
1244 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001245 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001246 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001247 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001248 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001249 attrs: {
1250 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001251 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001252 attrs
1253 },
David Tolnay857628c2017-11-11 12:25:31 -08001254 vis: vis,
1255 defaultness: defaultness,
1256 sig: MethodSig {
1257 constness: constness,
1258 unsafety: unsafety,
1259 abi: abi,
1260 ident: ident,
1261 decl: FnDecl {
1262 fn_token: fn_,
1263 paren_token: inputs.1,
1264 inputs: inputs.0,
1265 output: ret,
1266 variadic: false,
1267 generics: Generics {
1268 where_clause: where_clause,
1269 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001270 },
David Tolnay857628c2017-11-11 12:25:31 -08001271 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001272 },
David Tolnay857628c2017-11-11 12:25:31 -08001273 },
1274 block: Block {
1275 brace_token: inner_attrs_stmts.1,
1276 stmts: (inner_attrs_stmts.0).1,
1277 },
David Tolnay4c9be372016-10-06 00:47:37 -07001278 })
1279 ));
1280
David Tolnay857628c2017-11-11 12:25:31 -08001281 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001282 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001283 vis: syn!(Visibility) >>
1284 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001285 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001286 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001287 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001288 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001289 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001290 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001291 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001292 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001293 vis: vis,
1294 defaultness: defaultness,
1295 type_token: type_,
1296 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001297 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001298 eq_token: eq,
1299 ty: ty,
1300 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001301 })
1302 ));
1303
David Tolnay857628c2017-11-11 12:25:31 -08001304 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001305 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001306 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001307 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001308 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001309 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001310 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001311 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001312 })
1313 ));
1314
Alex Crichton954046c2017-05-30 21:49:42 -07001315 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001316 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001317 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001318 |
1319 epsilon!() => { |_| ImplPolarity::Positive }
1320 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001321 }
David Tolnay4c9be372016-10-06 00:47:37 -07001322
Alex Crichton954046c2017-05-30 21:49:42 -07001323 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001324 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001325 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001326 |
1327 epsilon!() => { |_| Constness::NotConst }
1328 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001329 }
David Tolnay42602292016-10-01 22:25:45 -07001330
Alex Crichton954046c2017-05-30 21:49:42 -07001331 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001332 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001333 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001334 |
1335 epsilon!() => { |_| Defaultness::Final }
1336 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001337 }
David Tolnay57292da2017-12-27 21:03:33 -05001338
1339 fn is_braced(tt: &TokenTree) -> bool {
1340 match tt.kind {
1341 TokenNode::Group(Delimiter::Brace, _) => true,
1342 _ => false,
1343 }
1344 }
David Tolnayedf2b992016-09-23 20:43:45 -07001345}
David Tolnay4a51dc72016-10-01 00:40:31 -07001346
1347#[cfg(feature = "printing")]
1348mod printing {
1349 use super::*;
1350 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001351 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001352 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001353
David Tolnay1bfa7332017-11-11 12:41:20 -08001354 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001355 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001356 tokens.append_all(self.attrs.outer());
1357 self.vis.to_tokens(tokens);
1358 self.extern_token.to_tokens(tokens);
1359 self.crate_token.to_tokens(tokens);
1360 self.ident.to_tokens(tokens);
1361 if let Some((ref as_token, ref rename)) = self.rename {
1362 as_token.to_tokens(tokens);
1363 rename.to_tokens(tokens);
1364 }
1365 self.semi_token.to_tokens(tokens);
1366 }
1367 }
1368
1369 impl ToTokens for ItemUse {
1370 fn to_tokens(&self, tokens: &mut Tokens) {
1371 tokens.append_all(self.attrs.outer());
1372 self.vis.to_tokens(tokens);
1373 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001374 self.leading_colon.to_tokens(tokens);
1375 self.prefix.to_tokens(tokens);
1376 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001377 self.semi_token.to_tokens(tokens);
1378 }
1379 }
1380
1381 impl ToTokens for ItemStatic {
1382 fn to_tokens(&self, tokens: &mut Tokens) {
1383 tokens.append_all(self.attrs.outer());
1384 self.vis.to_tokens(tokens);
1385 self.static_token.to_tokens(tokens);
1386 self.mutbl.to_tokens(tokens);
1387 self.ident.to_tokens(tokens);
1388 self.colon_token.to_tokens(tokens);
1389 self.ty.to_tokens(tokens);
1390 self.eq_token.to_tokens(tokens);
1391 self.expr.to_tokens(tokens);
1392 self.semi_token.to_tokens(tokens);
1393 }
1394 }
1395
1396 impl ToTokens for ItemConst {
1397 fn to_tokens(&self, tokens: &mut Tokens) {
1398 tokens.append_all(self.attrs.outer());
1399 self.vis.to_tokens(tokens);
1400 self.const_token.to_tokens(tokens);
1401 self.ident.to_tokens(tokens);
1402 self.colon_token.to_tokens(tokens);
1403 self.ty.to_tokens(tokens);
1404 self.eq_token.to_tokens(tokens);
1405 self.expr.to_tokens(tokens);
1406 self.semi_token.to_tokens(tokens);
1407 }
1408 }
1409
1410 impl ToTokens for ItemFn {
1411 fn to_tokens(&self, tokens: &mut Tokens) {
1412 tokens.append_all(self.attrs.outer());
1413 self.vis.to_tokens(tokens);
1414 self.constness.to_tokens(tokens);
1415 self.unsafety.to_tokens(tokens);
1416 self.abi.to_tokens(tokens);
1417 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1418 self.block.brace_token.surround(tokens, |tokens| {
1419 tokens.append_all(self.attrs.inner());
1420 tokens.append_all(&self.block.stmts);
1421 });
1422 }
1423 }
1424
1425 impl ToTokens for ItemMod {
1426 fn to_tokens(&self, tokens: &mut Tokens) {
1427 tokens.append_all(self.attrs.outer());
1428 self.vis.to_tokens(tokens);
1429 self.mod_token.to_tokens(tokens);
1430 self.ident.to_tokens(tokens);
1431 if let Some((ref brace, ref items)) = self.content {
1432 brace.surround(tokens, |tokens| {
1433 tokens.append_all(self.attrs.inner());
1434 tokens.append_all(items);
1435 });
1436 } else {
1437 TokensOrDefault(&self.semi).to_tokens(tokens);
1438 }
1439 }
1440 }
1441
1442 impl ToTokens for ItemForeignMod {
1443 fn to_tokens(&self, tokens: &mut Tokens) {
1444 tokens.append_all(self.attrs.outer());
1445 self.abi.to_tokens(tokens);
1446 self.brace_token.surround(tokens, |tokens| {
1447 tokens.append_all(&self.items);
1448 });
1449 }
1450 }
1451
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001452 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001453 fn to_tokens(&self, tokens: &mut Tokens) {
1454 tokens.append_all(self.attrs.outer());
1455 self.vis.to_tokens(tokens);
1456 self.type_token.to_tokens(tokens);
1457 self.ident.to_tokens(tokens);
1458 self.generics.to_tokens(tokens);
1459 self.generics.where_clause.to_tokens(tokens);
1460 self.eq_token.to_tokens(tokens);
1461 self.ty.to_tokens(tokens);
1462 self.semi_token.to_tokens(tokens);
1463 }
1464 }
1465
1466 impl ToTokens for ItemEnum {
1467 fn to_tokens(&self, tokens: &mut Tokens) {
1468 tokens.append_all(self.attrs.outer());
1469 self.vis.to_tokens(tokens);
1470 self.enum_token.to_tokens(tokens);
1471 self.ident.to_tokens(tokens);
1472 self.generics.to_tokens(tokens);
1473 self.generics.where_clause.to_tokens(tokens);
1474 self.brace_token.surround(tokens, |tokens| {
1475 self.variants.to_tokens(tokens);
1476 });
1477 }
1478 }
1479
1480 impl ToTokens for ItemStruct {
1481 fn to_tokens(&self, tokens: &mut Tokens) {
1482 tokens.append_all(self.attrs.outer());
1483 self.vis.to_tokens(tokens);
1484 self.struct_token.to_tokens(tokens);
1485 self.ident.to_tokens(tokens);
1486 self.generics.to_tokens(tokens);
1487 match self.data {
1488 VariantData::Struct(..) => {
1489 self.generics.where_clause.to_tokens(tokens);
1490 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001491 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001492 VariantData::Tuple(..) => {
1493 self.data.to_tokens(tokens);
1494 self.generics.where_clause.to_tokens(tokens);
1495 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001496 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001497 VariantData::Unit => {
1498 self.generics.where_clause.to_tokens(tokens);
1499 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001500 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001501 }
1502 }
1503 }
1504
1505 impl ToTokens for ItemUnion {
1506 fn to_tokens(&self, tokens: &mut Tokens) {
1507 tokens.append_all(self.attrs.outer());
1508 self.vis.to_tokens(tokens);
1509 self.union_token.to_tokens(tokens);
1510 self.ident.to_tokens(tokens);
1511 self.generics.to_tokens(tokens);
1512 self.generics.where_clause.to_tokens(tokens);
1513 // XXX: Should we handle / complain when using a
1514 // non-VariantData::Struct Variant in Union?
1515 self.data.to_tokens(tokens);
1516 }
1517 }
1518
1519 impl ToTokens for ItemTrait {
1520 fn to_tokens(&self, tokens: &mut Tokens) {
1521 tokens.append_all(self.attrs.outer());
1522 self.vis.to_tokens(tokens);
1523 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001524 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001525 self.trait_token.to_tokens(tokens);
1526 self.ident.to_tokens(tokens);
1527 self.generics.to_tokens(tokens);
1528 if !self.supertraits.is_empty() {
1529 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1530 self.supertraits.to_tokens(tokens);
1531 }
1532 self.generics.where_clause.to_tokens(tokens);
1533 self.brace_token.surround(tokens, |tokens| {
1534 tokens.append_all(&self.items);
1535 });
1536 }
1537 }
1538
1539 impl ToTokens for ItemDefaultImpl {
1540 fn to_tokens(&self, tokens: &mut Tokens) {
1541 tokens.append_all(self.attrs.outer());
1542 self.unsafety.to_tokens(tokens);
1543 self.impl_token.to_tokens(tokens);
1544 self.path.to_tokens(tokens);
1545 self.for_token.to_tokens(tokens);
1546 self.dot2_token.to_tokens(tokens);
1547 self.brace_token.surround(tokens, |_tokens| {});
1548 }
1549 }
1550
1551 impl ToTokens for ItemImpl {
1552 fn to_tokens(&self, tokens: &mut Tokens) {
1553 tokens.append_all(self.attrs.outer());
1554 self.defaultness.to_tokens(tokens);
1555 self.unsafety.to_tokens(tokens);
1556 self.impl_token.to_tokens(tokens);
1557 self.generics.to_tokens(tokens);
1558 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1559 polarity.to_tokens(tokens);
1560 path.to_tokens(tokens);
1561 for_token.to_tokens(tokens);
1562 }
1563 self.self_ty.to_tokens(tokens);
1564 self.generics.where_clause.to_tokens(tokens);
1565 self.brace_token.surround(tokens, |tokens| {
1566 tokens.append_all(&self.items);
1567 });
1568 }
1569 }
1570
1571 impl ToTokens for ItemMacro {
1572 fn to_tokens(&self, tokens: &mut Tokens) {
1573 tokens.append_all(self.attrs.outer());
1574 self.mac.path.to_tokens(tokens);
1575 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001576 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001577 self.mac.tokens.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001578 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001579 }
1580 }
David Tolnay42602292016-10-01 22:25:45 -07001581
David Tolnay500d8322017-12-18 00:32:51 -08001582 impl ToTokens for ItemMacro2 {
1583 fn to_tokens(&self, tokens: &mut Tokens) {
1584 tokens.append_all(self.attrs.outer());
1585 self.vis.to_tokens(tokens);
1586 self.macro_token.to_tokens(tokens);
1587 self.ident.to_tokens(tokens);
1588 self.args.to_tokens(tokens);
1589 self.body.to_tokens(tokens);
1590 }
1591 }
1592
David Tolnay5f332a92017-12-26 00:42:45 -05001593 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001595 self.ident.to_tokens(tokens);
1596 if let Some((ref as_token, ref rename)) = self.rename {
1597 as_token.to_tokens(tokens);
1598 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001599 }
David Tolnay4a057422016-10-08 00:02:31 -07001600 }
1601 }
1602
David Tolnay5f332a92017-12-26 00:42:45 -05001603 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001604 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001605 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001606 }
1607 }
1608
David Tolnay5f332a92017-12-26 00:42:45 -05001609 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001610 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001611 self.brace_token.surround(tokens, |tokens| {
1612 self.items.to_tokens(tokens);
1613 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001614 }
1615 }
1616
David Tolnay1bfa7332017-11-11 12:41:20 -08001617 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001618 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001619 tokens.append_all(self.attrs.outer());
1620 self.const_token.to_tokens(tokens);
1621 self.ident.to_tokens(tokens);
1622 self.colon_token.to_tokens(tokens);
1623 self.ty.to_tokens(tokens);
1624 if let Some((ref eq_token, ref default)) = self.default {
1625 eq_token.to_tokens(tokens);
1626 default.to_tokens(tokens);
1627 }
1628 self.semi_token.to_tokens(tokens);
1629 }
1630 }
1631
1632 impl ToTokens for TraitItemMethod {
1633 fn to_tokens(&self, tokens: &mut Tokens) {
1634 tokens.append_all(self.attrs.outer());
1635 self.sig.to_tokens(tokens);
1636 match self.default {
1637 Some(ref block) => {
1638 block.brace_token.surround(tokens, |tokens| {
1639 tokens.append_all(self.attrs.inner());
1640 tokens.append_all(&block.stmts);
1641 });
David Tolnayca085422016-10-04 00:12:38 -07001642 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001643 None => {
1644 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001645 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001646 }
1647 }
1648 }
1649
1650 impl ToTokens for TraitItemType {
1651 fn to_tokens(&self, tokens: &mut Tokens) {
1652 tokens.append_all(self.attrs.outer());
1653 self.type_token.to_tokens(tokens);
1654 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001655 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001656 if !self.bounds.is_empty() {
1657 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1658 self.bounds.to_tokens(tokens);
1659 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001660 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001661 if let Some((ref eq_token, ref default)) = self.default {
1662 eq_token.to_tokens(tokens);
1663 default.to_tokens(tokens);
1664 }
1665 self.semi_token.to_tokens(tokens);
1666 }
1667 }
1668
1669 impl ToTokens for TraitItemMacro {
1670 fn to_tokens(&self, tokens: &mut Tokens) {
1671 tokens.append_all(self.attrs.outer());
1672 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001673 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001674 }
1675 }
1676
David Tolnay857628c2017-11-11 12:25:31 -08001677 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001678 fn to_tokens(&self, tokens: &mut Tokens) {
1679 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001680 self.vis.to_tokens(tokens);
1681 self.defaultness.to_tokens(tokens);
1682 self.const_token.to_tokens(tokens);
1683 self.ident.to_tokens(tokens);
1684 self.colon_token.to_tokens(tokens);
1685 self.ty.to_tokens(tokens);
1686 self.eq_token.to_tokens(tokens);
1687 self.expr.to_tokens(tokens);
1688 self.semi_token.to_tokens(tokens);
1689 }
1690 }
1691
1692 impl ToTokens for ImplItemMethod {
1693 fn to_tokens(&self, tokens: &mut Tokens) {
1694 tokens.append_all(self.attrs.outer());
1695 self.vis.to_tokens(tokens);
1696 self.defaultness.to_tokens(tokens);
1697 self.sig.to_tokens(tokens);
1698 self.block.brace_token.surround(tokens, |tokens| {
1699 tokens.append_all(self.attrs.inner());
1700 tokens.append_all(&self.block.stmts);
1701 });
1702 }
1703 }
1704
1705 impl ToTokens for ImplItemType {
1706 fn to_tokens(&self, tokens: &mut Tokens) {
1707 tokens.append_all(self.attrs.outer());
1708 self.vis.to_tokens(tokens);
1709 self.defaultness.to_tokens(tokens);
1710 self.type_token.to_tokens(tokens);
1711 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001712 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001713 self.eq_token.to_tokens(tokens);
1714 self.ty.to_tokens(tokens);
1715 self.semi_token.to_tokens(tokens);
1716 }
1717 }
1718
1719 impl ToTokens for ImplItemMacro {
1720 fn to_tokens(&self, tokens: &mut Tokens) {
1721 tokens.append_all(self.attrs.outer());
1722 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001723 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001724 }
1725 }
1726
David Tolnay8894f602017-11-11 12:11:04 -08001727 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001728 fn to_tokens(&self, tokens: &mut Tokens) {
1729 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001730 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001731 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1732 self.semi_token.to_tokens(tokens);
1733 }
1734 }
1735
1736 impl ToTokens for ForeignItemStatic {
1737 fn to_tokens(&self, tokens: &mut Tokens) {
1738 tokens.append_all(self.attrs.outer());
1739 self.vis.to_tokens(tokens);
1740 self.static_token.to_tokens(tokens);
1741 self.mutbl.to_tokens(tokens);
1742 self.ident.to_tokens(tokens);
1743 self.colon_token.to_tokens(tokens);
1744 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001745 self.semi_token.to_tokens(tokens);
1746 }
1747 }
1748
David Tolnay199bcbb2017-11-12 10:33:52 -08001749 impl ToTokens for ForeignItemType {
1750 fn to_tokens(&self, tokens: &mut Tokens) {
1751 tokens.append_all(self.attrs.outer());
1752 self.vis.to_tokens(tokens);
1753 self.type_token.to_tokens(tokens);
1754 self.ident.to_tokens(tokens);
1755 self.semi_token.to_tokens(tokens);
1756 }
1757 }
1758
David Tolnay570695e2017-06-03 16:15:13 -07001759 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001760 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001761 self.constness.to_tokens(tokens);
1762 self.unsafety.to_tokens(tokens);
1763 self.abi.to_tokens(tokens);
1764 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765 }
1766 }
1767
David Tolnay570695e2017-06-03 16:15:13 -07001768 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001769
1770 impl<'a> ToTokens for NamedDecl<'a> {
1771 fn to_tokens(&self, tokens: &mut Tokens) {
1772 self.0.fn_token.to_tokens(tokens);
1773 self.1.to_tokens(tokens);
1774 self.0.generics.to_tokens(tokens);
1775 self.0.paren_token.surround(tokens, |tokens| {
1776 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001777
1778 if self.0.variadic {
1779 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001780 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001781 }
Alex Crichton259ee532017-07-14 06:51:02 -07001782 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001783 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001784 });
1785 self.0.output.to_tokens(tokens);
1786 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001787 }
1788 }
1789
Alex Crichton62a0a592017-05-22 13:58:53 -07001790 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001791 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001792 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001793 self.lifetime.to_tokens(tokens);
1794 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001795 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001796 }
1797 }
1798
1799 impl ToTokens for ArgSelf {
1800 fn to_tokens(&self, tokens: &mut Tokens) {
1801 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001803 }
1804 }
1805
1806 impl ToTokens for ArgCaptured {
1807 fn to_tokens(&self, tokens: &mut Tokens) {
1808 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001809 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001810 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001811 }
1812 }
1813
David Tolnay42602292016-10-01 22:25:45 -07001814 impl ToTokens for Constness {
1815 fn to_tokens(&self, tokens: &mut Tokens) {
1816 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001818 Constness::NotConst => {
1819 // nothing
1820 }
David Tolnay42602292016-10-01 22:25:45 -07001821 }
1822 }
1823 }
1824
David Tolnay4c9be372016-10-06 00:47:37 -07001825 impl ToTokens for Defaultness {
1826 fn to_tokens(&self, tokens: &mut Tokens) {
1827 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001828 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001829 Defaultness::Final => {
1830 // nothing
1831 }
1832 }
1833 }
1834 }
1835
1836 impl ToTokens for ImplPolarity {
1837 fn to_tokens(&self, tokens: &mut Tokens) {
1838 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001839 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001840 ImplPolarity::Positive => {
1841 // nothing
1842 }
1843 }
1844 }
1845 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001846}