blob: 9549823ed5ee12d5b50398cbca90b905cb21208b [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 Tolnayc6b55bc2017-11-09 22:48:38 -0800199 }),
David Tolnay500d8322017-12-18 00:32:51 -0800200 /// FIXME will need to revisit what this looks like in the AST.
David Tolnay9c76bcb2017-12-26 23:14:59 -0500201 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800202 pub attrs: Vec<Attribute>,
203 pub vis: Visibility,
204 pub macro_token: Token![macro],
205 pub ident: Ident,
206 pub args: TokenTree,
207 pub body: TokenTree,
208 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700209 }
David Tolnayb79ee962016-09-04 09:39:20 -0700210}
211
David Tolnay9c76bcb2017-12-26 23:14:59 -0500212#[cfg(feature = "extra-traits")]
213impl Eq for ItemMacro2 {}
214
215#[cfg(feature = "extra-traits")]
216impl PartialEq for ItemMacro2 {
217 fn eq(&self, other: &Self) -> bool {
218 self.attrs == other.attrs
219 && self.vis == other.vis
220 && self.macro_token == other.macro_token
221 && self.ident == other.ident
222 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
223 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
224 }
225}
226
227#[cfg(feature = "extra-traits")]
228impl Hash for ItemMacro2 {
229 fn hash<H>(&self, state: &mut H)
230 where H: Hasher
231 {
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 {
244 Body::Enum(data) => {
245 Item::Enum(ItemEnum {
246 attrs: input.attrs,
247 vis: input.vis,
248 enum_token: data.enum_token,
249 ident: input.ident,
250 generics: input.generics,
251 brace_token: data.brace_token,
252 variants: data.variants,
253 })
254 }
255 Body::Struct(data) => {
256 Item::Struct(ItemStruct {
257 attrs: input.attrs,
258 vis: input.vis,
259 struct_token: data.struct_token,
260 ident: input.ident,
261 generics: input.generics,
262 data: data.data,
263 semi_token: data.semi_token,
264 })
265 }
David Tolnay453cfd12016-10-23 11:00:14 -0700266 }
267 }
268}
269
Alex Crichton62a0a592017-05-22 13:58:53 -0700270ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500271 /// Things that can appear directly inside of a module.
272 pub enum UseTree {
273 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
274 pub Path(UsePath {
275 pub ident: Ident,
276 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500278 /// `use prefix::*`
279 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800280 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500282 /// `use prefix::{a, b, c}`
283 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500284 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500285 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 }),
287 }
288}
289
Alex Crichton62a0a592017-05-22 13:58:53 -0700290ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700291 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800293 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 NotConst,
295 }
296}
297
298ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700299 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 pub enum Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 Final,
303 }
304}
305
Alex Crichton62a0a592017-05-22 13:58:53 -0700306ast_enum_of_structs! {
307 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800308 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 /// A foreign function
310 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800311 pub attrs: Vec<Attribute>,
312 pub vis: Visibility,
313 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800315 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 }),
317 /// A foreign static item (`static ext: u8`)
318 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800319 pub attrs: Vec<Attribute>,
320 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800323 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800324 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800325 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800326 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800328 /// A foreign type
329 pub Type(ForeignItemType {
330 pub attrs: Vec<Attribute>,
331 pub vis: Visibility,
332 pub type_token: Token![type],
333 pub ident: Ident,
334 pub semi_token: Token![;],
335 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700337}
338
David Tolnayda705bd2017-11-10 21:58:05 -0800339ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 /// Represents an item declaration within a trait declaration,
341 /// possibly including a default implementation. A trait item is
342 /// either required (meaning it doesn't have an implementation, just a
343 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800344 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700345 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800346 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700348 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800349 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800350 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800351 pub default: Option<(Token![=], Expr)>,
352 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 }),
354 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800355 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 pub sig: MethodSig,
357 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 }),
360 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800361 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800362 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700363 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500364 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800365 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800366 pub bounds: Delimited<TypeParamBound, Token![+]>,
367 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800370 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800371 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800372 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800373 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700374 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700375}
376
377ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700378 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub enum ImplPolarity {
380 /// `impl Trait for Type`
381 Positive,
382 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800383 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 }
385}
386
Alex Crichton62a0a592017-05-22 13:58:53 -0700387ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800388 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800390 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700391 pub vis: Visibility,
392 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800393 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700394 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800395 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800396 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800397 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800399 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 }),
401 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800402 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700403 pub vis: Visibility,
404 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub sig: MethodSig,
406 pub block: Block,
407 }),
408 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800409 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700410 pub vis: Visibility,
411 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700413 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500414 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800415 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800416 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700418 }),
David Tolnay857628c2017-11-11 12:25:31 -0800419 pub Macro(ImplItemMacro {
420 pub attrs: Vec<Attribute>,
421 pub mac: Macro,
422 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700424}
425
426ast_struct! {
427 /// Represents a method's signature in a trait declaration,
428 /// or in an implementation.
429 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700431 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700433 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }
436}
437
438ast_struct! {
439 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700440 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700441 /// E.g. `fn foo(bar: baz)`
442 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub fn_token: Token![fn],
David Tolnay32954ef2017-12-26 22:43:16 -0500444 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800445 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800446 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700447 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800449 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700451}
452
Alex Crichton62a0a592017-05-22 13:58:53 -0700453ast_enum_of_structs! {
454 /// An argument in a function header.
455 ///
456 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
457 pub enum FnArg {
458 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800459 pub and_token: Token![&],
460 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700461 pub lifetime: Option<Lifetime>,
462 pub mutbl: Mutability,
463 }),
464 pub SelfValue(ArgSelf {
465 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800466 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 }),
468 pub Captured(ArgCaptured {
469 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800470 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700472 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800473 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700474 }
David Tolnay62f374c2016-10-02 13:37:00 -0700475}
476
David Tolnayedf2b992016-09-23 20:43:45 -0700477#[cfg(feature = "parsing")]
478pub mod parsing {
479 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700480
Michael Layzell92639a52017-06-01 00:07:44 -0400481 use synom::Synom;
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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700518 attrs: many0!(call!(Attribute::parse_outer)) >>
519 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 Tolnay9c76bcb2017-12-26 23:14:59 -0500522 body: call!(mac::parsing::parse_tt_delimited) >>
523 cond!(!mac::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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700530 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800531 },
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!(
537 attrs: many0!(call!(Attribute::parse_outer)) >>
538 vis: syn!(Visibility) >>
539 macro_: keyword!(macro) >>
540 ident: syn!(Ident) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500541 args: call!(mac::parsing::parse_tt_delimited) >>
542 body: call!(mac::parsing::parse_tt_delimited) >>
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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700554 attrs: many0!(call!(Attribute::parse_outer)) >>
555 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700573 attrs: many0!(call!(Attribute::parse_outer)) >>
574 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700660 attrs: many0!(call!(Attribute::parse_outer)) >>
661 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700685 attrs: many0!(call!(Attribute::parse_outer)) >>
686 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700708 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
709 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!(
720 many0!(call!(Attribute::parse_inner)),
721 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700795 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
796 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700808 many0!(call!(Attribute::parse_inner)),
809 many0!(syn!(Item))
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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700832 attrs: many0!(call!(Attribute::parse_outer)) >>
833 abi: syn!(Abi) >>
834 items: braces!(many0!(syn!(ForeignItem))) >>
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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700852 attrs: many0!(call!(Attribute::parse_outer)) >>
853 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700891 attrs: many0!(call!(Attribute::parse_outer)) >>
892 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!(
912 attrs: many0!(call!(Attribute::parse_outer)) >>
913 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700927 attrs: many0!(call!(Attribute::parse_outer)) >>
928 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700966 attrs: many0!(call!(Attribute::parse_outer)) >>
967 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!(
Alex Crichton954046c2017-05-30 21:49:42 -0700988 attrs: many0!(call!(Attribute::parse_outer)) >>
989 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) >>
1000 body: braces!(many0!(syn!(TraitItem))) >>
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!(
Alex Crichton954046c2017-05-30 21:49:42 -07001020 attrs: many0!(call!(Attribute::parse_outer)) >>
1021 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!(
Alex Crichton954046c2017-05-30 21:49:42 -07001049 attrs: many0!(call!(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!(
Alex Crichton954046c2017-05-30 21:49:42 -07001068 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1069 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!(
1079 tuple!(many0!(call!(Attribute::parse_inner)),
1080 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!(
Alex Crichton954046c2017-05-30 21:49:42 -07001124 attrs: many0!(call!(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!(
Alex Crichton954046c2017-05-30 21:49:42 -07001151 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001152 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001153 cond!(!mac.is_braced(), 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 Tolnay0aecb732016-10-03 23:03:50 -07001157 })
1158 ));
1159
David Tolnay4c614be2017-11-10 00:02:38 -08001160 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001161 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001162 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001163 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001164 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001165 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001166 polarity_path: alt!(
1167 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001168 polarity: syn!(ImplPolarity) >>
1169 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001170 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001171 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001172 )
1173 |
David Tolnay570695e2017-06-03 16:15:13 -07001174 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001175 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001176 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001177 where_clause: syn!(WhereClause) >>
1178 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001179 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001180 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001181 defaultness: defaultness,
1182 unsafety: unsafety,
1183 impl_token: impl_,
1184 generics: Generics {
1185 where_clause: where_clause,
1186 .. generics
1187 },
1188 trait_: polarity_path,
1189 self_ty: Box::new(self_ty),
1190 brace_token: body.1,
1191 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001192 })
David Tolnay4c9be372016-10-06 00:47:37 -07001193 ));
1194
David Tolnay857628c2017-11-11 12:25:31 -08001195 impl_synom!(ImplItem "item in impl block" alt!(
1196 syn!(ImplItemConst) => { ImplItem::Const }
1197 |
1198 syn!(ImplItemMethod) => { ImplItem::Method }
1199 |
1200 syn!(ImplItemType) => { ImplItem::Type }
1201 |
1202 syn!(ImplItemMacro) => { ImplItem::Macro }
1203 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001204
David Tolnay857628c2017-11-11 12:25:31 -08001205 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001206 attrs: many0!(call!(Attribute::parse_outer)) >>
1207 vis: syn!(Visibility) >>
1208 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001209 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001210 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001211 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001212 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001213 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001214 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001215 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001216 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001217 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001218 vis: vis,
1219 defaultness: defaultness,
1220 const_token: const_,
1221 ident: ident,
1222 colon_token: colon,
1223 ty: ty,
1224 eq_token: eq,
1225 expr: value,
1226 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001227 })
1228 ));
1229
David Tolnay857628c2017-11-11 12:25:31 -08001230 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001231 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1232 vis: syn!(Visibility) >>
1233 defaultness: syn!(Defaultness) >>
1234 constness: syn!(Constness) >>
1235 unsafety: syn!(Unsafety) >>
1236 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001237 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001238 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001239 generics: syn!(Generics) >>
1240 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001241 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001242 where_clause: syn!(WhereClause) >>
1243 inner_attrs_stmts: braces!(tuple!(
1244 many0!(call!(Attribute::parse_inner)),
1245 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001246 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001247 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001248 attrs: {
1249 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001250 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001251 attrs
1252 },
David Tolnay857628c2017-11-11 12:25:31 -08001253 vis: vis,
1254 defaultness: defaultness,
1255 sig: MethodSig {
1256 constness: constness,
1257 unsafety: unsafety,
1258 abi: abi,
1259 ident: ident,
1260 decl: FnDecl {
1261 fn_token: fn_,
1262 paren_token: inputs.1,
1263 inputs: inputs.0,
1264 output: ret,
1265 variadic: false,
1266 generics: Generics {
1267 where_clause: where_clause,
1268 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001269 },
David Tolnay857628c2017-11-11 12:25:31 -08001270 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001271 },
David Tolnay857628c2017-11-11 12:25:31 -08001272 },
1273 block: Block {
1274 brace_token: inner_attrs_stmts.1,
1275 stmts: (inner_attrs_stmts.0).1,
1276 },
David Tolnay4c9be372016-10-06 00:47:37 -07001277 })
1278 ));
1279
David Tolnay857628c2017-11-11 12:25:31 -08001280 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001281 attrs: many0!(call!(Attribute::parse_outer)) >>
1282 vis: syn!(Visibility) >>
1283 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001284 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001285 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001286 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001287 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001288 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001289 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001290 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001291 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001292 vis: vis,
1293 defaultness: defaultness,
1294 type_token: type_,
1295 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001296 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001297 eq_token: eq,
1298 ty: ty,
1299 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001300 })
1301 ));
1302
David Tolnay857628c2017-11-11 12:25:31 -08001303 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001304 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001305 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001306 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001307 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001308 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001309 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001310 })
1311 ));
1312
Alex Crichton954046c2017-05-30 21:49:42 -07001313 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001314 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001315 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001316 |
1317 epsilon!() => { |_| ImplPolarity::Positive }
1318 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001319 }
David Tolnay4c9be372016-10-06 00:47:37 -07001320
Alex Crichton954046c2017-05-30 21:49:42 -07001321 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001322 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001323 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001324 |
1325 epsilon!() => { |_| Constness::NotConst }
1326 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001327 }
David Tolnay42602292016-10-01 22:25:45 -07001328
Alex Crichton954046c2017-05-30 21:49:42 -07001329 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001330 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001331 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001332 |
1333 epsilon!() => { |_| Defaultness::Final }
1334 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001335 }
David Tolnayedf2b992016-09-23 20:43:45 -07001336}
David Tolnay4a51dc72016-10-01 00:40:31 -07001337
1338#[cfg(feature = "printing")]
1339mod printing {
1340 use super::*;
1341 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001342 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001343 use quote::{Tokens, ToTokens};
1344
David Tolnay1bfa7332017-11-11 12:41:20 -08001345 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001346 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001347 tokens.append_all(self.attrs.outer());
1348 self.vis.to_tokens(tokens);
1349 self.extern_token.to_tokens(tokens);
1350 self.crate_token.to_tokens(tokens);
1351 self.ident.to_tokens(tokens);
1352 if let Some((ref as_token, ref rename)) = self.rename {
1353 as_token.to_tokens(tokens);
1354 rename.to_tokens(tokens);
1355 }
1356 self.semi_token.to_tokens(tokens);
1357 }
1358 }
1359
1360 impl ToTokens for ItemUse {
1361 fn to_tokens(&self, tokens: &mut Tokens) {
1362 tokens.append_all(self.attrs.outer());
1363 self.vis.to_tokens(tokens);
1364 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001365 self.leading_colon.to_tokens(tokens);
1366 self.prefix.to_tokens(tokens);
1367 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001368 self.semi_token.to_tokens(tokens);
1369 }
1370 }
1371
1372 impl ToTokens for ItemStatic {
1373 fn to_tokens(&self, tokens: &mut Tokens) {
1374 tokens.append_all(self.attrs.outer());
1375 self.vis.to_tokens(tokens);
1376 self.static_token.to_tokens(tokens);
1377 self.mutbl.to_tokens(tokens);
1378 self.ident.to_tokens(tokens);
1379 self.colon_token.to_tokens(tokens);
1380 self.ty.to_tokens(tokens);
1381 self.eq_token.to_tokens(tokens);
1382 self.expr.to_tokens(tokens);
1383 self.semi_token.to_tokens(tokens);
1384 }
1385 }
1386
1387 impl ToTokens for ItemConst {
1388 fn to_tokens(&self, tokens: &mut Tokens) {
1389 tokens.append_all(self.attrs.outer());
1390 self.vis.to_tokens(tokens);
1391 self.const_token.to_tokens(tokens);
1392 self.ident.to_tokens(tokens);
1393 self.colon_token.to_tokens(tokens);
1394 self.ty.to_tokens(tokens);
1395 self.eq_token.to_tokens(tokens);
1396 self.expr.to_tokens(tokens);
1397 self.semi_token.to_tokens(tokens);
1398 }
1399 }
1400
1401 impl ToTokens for ItemFn {
1402 fn to_tokens(&self, tokens: &mut Tokens) {
1403 tokens.append_all(self.attrs.outer());
1404 self.vis.to_tokens(tokens);
1405 self.constness.to_tokens(tokens);
1406 self.unsafety.to_tokens(tokens);
1407 self.abi.to_tokens(tokens);
1408 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1409 self.block.brace_token.surround(tokens, |tokens| {
1410 tokens.append_all(self.attrs.inner());
1411 tokens.append_all(&self.block.stmts);
1412 });
1413 }
1414 }
1415
1416 impl ToTokens for ItemMod {
1417 fn to_tokens(&self, tokens: &mut Tokens) {
1418 tokens.append_all(self.attrs.outer());
1419 self.vis.to_tokens(tokens);
1420 self.mod_token.to_tokens(tokens);
1421 self.ident.to_tokens(tokens);
1422 if let Some((ref brace, ref items)) = self.content {
1423 brace.surround(tokens, |tokens| {
1424 tokens.append_all(self.attrs.inner());
1425 tokens.append_all(items);
1426 });
1427 } else {
1428 TokensOrDefault(&self.semi).to_tokens(tokens);
1429 }
1430 }
1431 }
1432
1433 impl ToTokens for ItemForeignMod {
1434 fn to_tokens(&self, tokens: &mut Tokens) {
1435 tokens.append_all(self.attrs.outer());
1436 self.abi.to_tokens(tokens);
1437 self.brace_token.surround(tokens, |tokens| {
1438 tokens.append_all(&self.items);
1439 });
1440 }
1441 }
1442
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001443 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001444 fn to_tokens(&self, tokens: &mut Tokens) {
1445 tokens.append_all(self.attrs.outer());
1446 self.vis.to_tokens(tokens);
1447 self.type_token.to_tokens(tokens);
1448 self.ident.to_tokens(tokens);
1449 self.generics.to_tokens(tokens);
1450 self.generics.where_clause.to_tokens(tokens);
1451 self.eq_token.to_tokens(tokens);
1452 self.ty.to_tokens(tokens);
1453 self.semi_token.to_tokens(tokens);
1454 }
1455 }
1456
1457 impl ToTokens for ItemEnum {
1458 fn to_tokens(&self, tokens: &mut Tokens) {
1459 tokens.append_all(self.attrs.outer());
1460 self.vis.to_tokens(tokens);
1461 self.enum_token.to_tokens(tokens);
1462 self.ident.to_tokens(tokens);
1463 self.generics.to_tokens(tokens);
1464 self.generics.where_clause.to_tokens(tokens);
1465 self.brace_token.surround(tokens, |tokens| {
1466 self.variants.to_tokens(tokens);
1467 });
1468 }
1469 }
1470
1471 impl ToTokens for ItemStruct {
1472 fn to_tokens(&self, tokens: &mut Tokens) {
1473 tokens.append_all(self.attrs.outer());
1474 self.vis.to_tokens(tokens);
1475 self.struct_token.to_tokens(tokens);
1476 self.ident.to_tokens(tokens);
1477 self.generics.to_tokens(tokens);
1478 match self.data {
1479 VariantData::Struct(..) => {
1480 self.generics.where_clause.to_tokens(tokens);
1481 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001482 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001483 VariantData::Tuple(..) => {
1484 self.data.to_tokens(tokens);
1485 self.generics.where_clause.to_tokens(tokens);
1486 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001487 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001488 VariantData::Unit => {
1489 self.generics.where_clause.to_tokens(tokens);
1490 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001491 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001492 }
1493 }
1494 }
1495
1496 impl ToTokens for ItemUnion {
1497 fn to_tokens(&self, tokens: &mut Tokens) {
1498 tokens.append_all(self.attrs.outer());
1499 self.vis.to_tokens(tokens);
1500 self.union_token.to_tokens(tokens);
1501 self.ident.to_tokens(tokens);
1502 self.generics.to_tokens(tokens);
1503 self.generics.where_clause.to_tokens(tokens);
1504 // XXX: Should we handle / complain when using a
1505 // non-VariantData::Struct Variant in Union?
1506 self.data.to_tokens(tokens);
1507 }
1508 }
1509
1510 impl ToTokens for ItemTrait {
1511 fn to_tokens(&self, tokens: &mut Tokens) {
1512 tokens.append_all(self.attrs.outer());
1513 self.vis.to_tokens(tokens);
1514 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001515 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001516 self.trait_token.to_tokens(tokens);
1517 self.ident.to_tokens(tokens);
1518 self.generics.to_tokens(tokens);
1519 if !self.supertraits.is_empty() {
1520 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1521 self.supertraits.to_tokens(tokens);
1522 }
1523 self.generics.where_clause.to_tokens(tokens);
1524 self.brace_token.surround(tokens, |tokens| {
1525 tokens.append_all(&self.items);
1526 });
1527 }
1528 }
1529
1530 impl ToTokens for ItemDefaultImpl {
1531 fn to_tokens(&self, tokens: &mut Tokens) {
1532 tokens.append_all(self.attrs.outer());
1533 self.unsafety.to_tokens(tokens);
1534 self.impl_token.to_tokens(tokens);
1535 self.path.to_tokens(tokens);
1536 self.for_token.to_tokens(tokens);
1537 self.dot2_token.to_tokens(tokens);
1538 self.brace_token.surround(tokens, |_tokens| {});
1539 }
1540 }
1541
1542 impl ToTokens for ItemImpl {
1543 fn to_tokens(&self, tokens: &mut Tokens) {
1544 tokens.append_all(self.attrs.outer());
1545 self.defaultness.to_tokens(tokens);
1546 self.unsafety.to_tokens(tokens);
1547 self.impl_token.to_tokens(tokens);
1548 self.generics.to_tokens(tokens);
1549 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1550 polarity.to_tokens(tokens);
1551 path.to_tokens(tokens);
1552 for_token.to_tokens(tokens);
1553 }
1554 self.self_ty.to_tokens(tokens);
1555 self.generics.where_clause.to_tokens(tokens);
1556 self.brace_token.surround(tokens, |tokens| {
1557 tokens.append_all(&self.items);
1558 });
1559 }
1560 }
1561
1562 impl ToTokens for ItemMacro {
1563 fn to_tokens(&self, tokens: &mut Tokens) {
1564 tokens.append_all(self.attrs.outer());
1565 self.mac.path.to_tokens(tokens);
1566 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001567 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001568 tokens.append_all(&self.mac.tokens);
1569 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001570 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001571 }
1572 }
1573 }
David Tolnay42602292016-10-01 22:25:45 -07001574
David Tolnay500d8322017-12-18 00:32:51 -08001575 impl ToTokens for ItemMacro2 {
1576 fn to_tokens(&self, tokens: &mut Tokens) {
1577 tokens.append_all(self.attrs.outer());
1578 self.vis.to_tokens(tokens);
1579 self.macro_token.to_tokens(tokens);
1580 self.ident.to_tokens(tokens);
1581 self.args.to_tokens(tokens);
1582 self.body.to_tokens(tokens);
1583 }
1584 }
1585
David Tolnay5f332a92017-12-26 00:42:45 -05001586 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001587 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001588 self.ident.to_tokens(tokens);
1589 if let Some((ref as_token, ref rename)) = self.rename {
1590 as_token.to_tokens(tokens);
1591 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001592 }
David Tolnay4a057422016-10-08 00:02:31 -07001593 }
1594 }
1595
David Tolnay5f332a92017-12-26 00:42:45 -05001596 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001597 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001598 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001599 }
1600 }
1601
David Tolnay5f332a92017-12-26 00:42:45 -05001602 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001603 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001604 self.brace_token.surround(tokens, |tokens| {
1605 self.items.to_tokens(tokens);
1606 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 }
1608 }
1609
David Tolnay1bfa7332017-11-11 12:41:20 -08001610 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001611 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001612 tokens.append_all(self.attrs.outer());
1613 self.const_token.to_tokens(tokens);
1614 self.ident.to_tokens(tokens);
1615 self.colon_token.to_tokens(tokens);
1616 self.ty.to_tokens(tokens);
1617 if let Some((ref eq_token, ref default)) = self.default {
1618 eq_token.to_tokens(tokens);
1619 default.to_tokens(tokens);
1620 }
1621 self.semi_token.to_tokens(tokens);
1622 }
1623 }
1624
1625 impl ToTokens for TraitItemMethod {
1626 fn to_tokens(&self, tokens: &mut Tokens) {
1627 tokens.append_all(self.attrs.outer());
1628 self.sig.to_tokens(tokens);
1629 match self.default {
1630 Some(ref block) => {
1631 block.brace_token.surround(tokens, |tokens| {
1632 tokens.append_all(self.attrs.inner());
1633 tokens.append_all(&block.stmts);
1634 });
David Tolnayca085422016-10-04 00:12:38 -07001635 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001636 None => {
1637 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001638 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001639 }
1640 }
1641 }
1642
1643 impl ToTokens for TraitItemType {
1644 fn to_tokens(&self, tokens: &mut Tokens) {
1645 tokens.append_all(self.attrs.outer());
1646 self.type_token.to_tokens(tokens);
1647 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001648 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001649 if !self.bounds.is_empty() {
1650 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1651 self.bounds.to_tokens(tokens);
1652 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001653 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001654 if let Some((ref eq_token, ref default)) = self.default {
1655 eq_token.to_tokens(tokens);
1656 default.to_tokens(tokens);
1657 }
1658 self.semi_token.to_tokens(tokens);
1659 }
1660 }
1661
1662 impl ToTokens for TraitItemMacro {
1663 fn to_tokens(&self, tokens: &mut Tokens) {
1664 tokens.append_all(self.attrs.outer());
1665 self.mac.to_tokens(tokens);
1666 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001667 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001668 }
1669 }
1670 }
1671
David Tolnay857628c2017-11-11 12:25:31 -08001672 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001673 fn to_tokens(&self, tokens: &mut Tokens) {
1674 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001675 self.vis.to_tokens(tokens);
1676 self.defaultness.to_tokens(tokens);
1677 self.const_token.to_tokens(tokens);
1678 self.ident.to_tokens(tokens);
1679 self.colon_token.to_tokens(tokens);
1680 self.ty.to_tokens(tokens);
1681 self.eq_token.to_tokens(tokens);
1682 self.expr.to_tokens(tokens);
1683 self.semi_token.to_tokens(tokens);
1684 }
1685 }
1686
1687 impl ToTokens for ImplItemMethod {
1688 fn to_tokens(&self, tokens: &mut Tokens) {
1689 tokens.append_all(self.attrs.outer());
1690 self.vis.to_tokens(tokens);
1691 self.defaultness.to_tokens(tokens);
1692 self.sig.to_tokens(tokens);
1693 self.block.brace_token.surround(tokens, |tokens| {
1694 tokens.append_all(self.attrs.inner());
1695 tokens.append_all(&self.block.stmts);
1696 });
1697 }
1698 }
1699
1700 impl ToTokens for ImplItemType {
1701 fn to_tokens(&self, tokens: &mut Tokens) {
1702 tokens.append_all(self.attrs.outer());
1703 self.vis.to_tokens(tokens);
1704 self.defaultness.to_tokens(tokens);
1705 self.type_token.to_tokens(tokens);
1706 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001707 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001708 self.eq_token.to_tokens(tokens);
1709 self.ty.to_tokens(tokens);
1710 self.semi_token.to_tokens(tokens);
1711 }
1712 }
1713
1714 impl ToTokens for ImplItemMacro {
1715 fn to_tokens(&self, tokens: &mut Tokens) {
1716 tokens.append_all(self.attrs.outer());
1717 self.mac.to_tokens(tokens);
1718 if !self.mac.is_braced() {
1719 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001720 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001721 }
1722 }
1723 }
1724
David Tolnay8894f602017-11-11 12:11:04 -08001725 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001726 fn to_tokens(&self, tokens: &mut Tokens) {
1727 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001728 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001729 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1730 self.semi_token.to_tokens(tokens);
1731 }
1732 }
1733
1734 impl ToTokens for ForeignItemStatic {
1735 fn to_tokens(&self, tokens: &mut Tokens) {
1736 tokens.append_all(self.attrs.outer());
1737 self.vis.to_tokens(tokens);
1738 self.static_token.to_tokens(tokens);
1739 self.mutbl.to_tokens(tokens);
1740 self.ident.to_tokens(tokens);
1741 self.colon_token.to_tokens(tokens);
1742 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001743 self.semi_token.to_tokens(tokens);
1744 }
1745 }
1746
David Tolnay199bcbb2017-11-12 10:33:52 -08001747 impl ToTokens for ForeignItemType {
1748 fn to_tokens(&self, tokens: &mut Tokens) {
1749 tokens.append_all(self.attrs.outer());
1750 self.vis.to_tokens(tokens);
1751 self.type_token.to_tokens(tokens);
1752 self.ident.to_tokens(tokens);
1753 self.semi_token.to_tokens(tokens);
1754 }
1755 }
1756
David Tolnay570695e2017-06-03 16:15:13 -07001757 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001759 self.constness.to_tokens(tokens);
1760 self.unsafety.to_tokens(tokens);
1761 self.abi.to_tokens(tokens);
1762 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001763 }
1764 }
1765
David Tolnay570695e2017-06-03 16:15:13 -07001766 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001767
1768 impl<'a> ToTokens for NamedDecl<'a> {
1769 fn to_tokens(&self, tokens: &mut Tokens) {
1770 self.0.fn_token.to_tokens(tokens);
1771 self.1.to_tokens(tokens);
1772 self.0.generics.to_tokens(tokens);
1773 self.0.paren_token.surround(tokens, |tokens| {
1774 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001775
1776 if self.0.variadic {
1777 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001778 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001779 }
Alex Crichton259ee532017-07-14 06:51:02 -07001780 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001781 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001782 });
1783 self.0.output.to_tokens(tokens);
1784 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001785 }
1786 }
1787
Alex Crichton62a0a592017-05-22 13:58:53 -07001788 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001789 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001790 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001791 self.lifetime.to_tokens(tokens);
1792 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001793 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001794 }
1795 }
1796
1797 impl ToTokens for ArgSelf {
1798 fn to_tokens(&self, tokens: &mut Tokens) {
1799 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001800 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001801 }
1802 }
1803
1804 impl ToTokens for ArgCaptured {
1805 fn to_tokens(&self, tokens: &mut Tokens) {
1806 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001807 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001808 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001809 }
1810 }
1811
David Tolnay42602292016-10-01 22:25:45 -07001812 impl ToTokens for Constness {
1813 fn to_tokens(&self, tokens: &mut Tokens) {
1814 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001815 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001816 Constness::NotConst => {
1817 // nothing
1818 }
David Tolnay42602292016-10-01 22:25:45 -07001819 }
1820 }
1821 }
1822
David Tolnay4c9be372016-10-06 00:47:37 -07001823 impl ToTokens for Defaultness {
1824 fn to_tokens(&self, tokens: &mut Tokens) {
1825 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001826 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001827 Defaultness::Final => {
1828 // nothing
1829 }
1830 }
1831 }
1832 }
1833
1834 impl ToTokens for ImplPolarity {
1835 fn to_tokens(&self, tokens: &mut Tokens) {
1836 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001837 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001838 ImplPolarity::Positive => {
1839 // nothing
1840 }
1841 }
1842 }
1843 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001844}