blob: d38d25fafecedcd7f3b7676e2cf548aeea950f1f [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 {
David Tolnay51382052017-12-27 13:46:21 -0500218 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnay9c76bcb2017-12-26 23:14:59 -0500219 && self.ident == other.ident
220 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
221 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
222 }
223}
224
225#[cfg(feature = "extra-traits")]
226impl Hash for ItemMacro2 {
227 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500228 where
229 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500230 {
231 self.attrs.hash(state);
232 self.vis.hash(state);
233 self.macro_token.hash(state);
234 self.ident.hash(state);
235 TokenTreeHelper(&self.args).hash(state);
236 TokenTreeHelper(&self.body).hash(state);
237 }
238}
239
David Tolnay0e837402016-12-22 17:25:55 -0500240impl From<DeriveInput> for Item {
241 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800242 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500243 Body::Enum(data) => Item::Enum(ItemEnum {
244 attrs: input.attrs,
245 vis: input.vis,
246 enum_token: data.enum_token,
247 ident: input.ident,
248 generics: input.generics,
249 brace_token: data.brace_token,
250 variants: data.variants,
251 }),
252 Body::Struct(data) => Item::Struct(ItemStruct {
253 attrs: input.attrs,
254 vis: input.vis,
255 struct_token: data.struct_token,
256 ident: input.ident,
257 generics: input.generics,
258 data: data.data,
259 semi_token: data.semi_token,
260 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700261 }
262 }
263}
264
Alex Crichton62a0a592017-05-22 13:58:53 -0700265ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500266 /// Things that can appear directly inside of a module.
267 pub enum UseTree {
268 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
269 pub Path(UsePath {
270 pub ident: Ident,
271 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500273 /// `use prefix::*`
274 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800275 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500277 /// `use prefix::{a, b, c}`
278 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500279 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500280 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 }),
282 }
283}
284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700286 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700287 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 NotConst,
290 }
291}
292
293ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700294 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700295 pub enum Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800296 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 Final,
298 }
299}
300
Alex Crichton62a0a592017-05-22 13:58:53 -0700301ast_enum_of_structs! {
302 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800303 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 /// A foreign function
305 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800306 pub attrs: Vec<Attribute>,
307 pub vis: Visibility,
308 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800310 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 }),
312 /// A foreign static item (`static ext: u8`)
313 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800314 pub attrs: Vec<Attribute>,
315 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800316 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800318 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800320 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800323 /// A foreign type
324 pub Type(ForeignItemType {
325 pub attrs: Vec<Attribute>,
326 pub vis: Visibility,
327 pub type_token: Token![type],
328 pub ident: Ident,
329 pub semi_token: Token![;],
330 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700332}
333
David Tolnayda705bd2017-11-10 21:58:05 -0800334ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 /// Represents an item declaration within a trait declaration,
336 /// possibly including a default implementation. A trait item is
337 /// either required (meaning it doesn't have an implementation, just a
338 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800339 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800341 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800342 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700343 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800344 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800345 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800346 pub default: Option<(Token![=], Expr)>,
347 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 }),
349 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800350 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub sig: MethodSig,
352 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800353 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 }),
355 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800356 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800357 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700358 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500359 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800361 pub bounds: Delimited<TypeParamBound, Token![+]>,
362 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800363 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700364 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800365 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800366 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800367 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800368 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700370}
371
372ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700373 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700374 pub enum ImplPolarity {
375 /// `impl Trait for Type`
376 Positive,
377 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800378 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 }
380}
381
Alex Crichton62a0a592017-05-22 13:58:53 -0700382ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800383 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800385 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700386 pub vis: Visibility,
387 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700389 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800390 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800391 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800392 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700393 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 }),
396 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800397 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700398 pub vis: Visibility,
399 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 pub sig: MethodSig,
401 pub block: Block,
402 }),
403 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800404 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700405 pub vis: Visibility,
406 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800407 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700408 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500409 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800410 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800411 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 }),
David Tolnay857628c2017-11-11 12:25:31 -0800414 pub Macro(ImplItemMacro {
415 pub attrs: Vec<Attribute>,
416 pub mac: Macro,
417 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700418 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700419}
420
421ast_struct! {
422 /// Represents a method's signature in a trait declaration,
423 /// or in an implementation.
424 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700425 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700426 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700428 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 }
431}
432
433ast_struct! {
434 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700435 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700436 /// E.g. `fn foo(bar: baz)`
437 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800438 pub fn_token: Token![fn],
David Tolnay32954ef2017-12-26 22:43:16 -0500439 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800441 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700442 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700446}
447
Alex Crichton62a0a592017-05-22 13:58:53 -0700448ast_enum_of_structs! {
449 /// An argument in a function header.
450 ///
451 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
452 pub enum FnArg {
453 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800454 pub and_token: Token![&],
455 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700456 pub lifetime: Option<Lifetime>,
457 pub mutbl: Mutability,
458 }),
459 pub SelfValue(ArgSelf {
460 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800461 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700462 }),
463 pub Captured(ArgCaptured {
464 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800465 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800466 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800468 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 }
David Tolnay62f374c2016-10-02 13:37:00 -0700470}
471
David Tolnayedf2b992016-09-23 20:43:45 -0700472#[cfg(feature = "parsing")]
473pub mod parsing {
474 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700475
Michael Layzell92639a52017-06-01 00:07:44 -0400476 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700477
David Tolnay4c614be2017-11-10 00:02:38 -0800478 impl_synom!(Item "item" alt!(
479 syn!(ItemExternCrate) => { Item::ExternCrate }
480 |
481 syn!(ItemUse) => { Item::Use }
482 |
483 syn!(ItemStatic) => { Item::Static }
484 |
485 syn!(ItemConst) => { Item::Const }
486 |
487 syn!(ItemFn) => { Item::Fn }
488 |
489 syn!(ItemMod) => { Item::Mod }
490 |
491 syn!(ItemForeignMod) => { Item::ForeignMod }
492 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800493 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800494 |
495 syn!(ItemStruct) => { Item::Struct }
496 |
497 syn!(ItemEnum) => { Item::Enum }
498 |
499 syn!(ItemUnion) => { Item::Union }
500 |
501 syn!(ItemTrait) => { Item::Trait }
502 |
503 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
504 |
505 syn!(ItemImpl) => { Item::Impl }
506 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800507 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800508 |
509 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800510 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700511
David Tolnaydecf28d2017-11-11 11:56:45 -0800512 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700513 attrs: many0!(call!(Attribute::parse_outer)) >>
514 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700516 ident: option!(syn!(Ident)) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500517 body: call!(mac::parsing::parse_tt_delimited) >>
518 cond!(!mac::is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800519 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700520 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800521 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800522 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500523 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700524 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500525 tokens: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800526 },
David Tolnay4c614be2017-11-10 00:02:38 -0800527 })
David Tolnayedf2b992016-09-23 20:43:45 -0700528 ));
529
David Tolnay500d8322017-12-18 00:32:51 -0800530 // TODO: figure out the actual grammar; is body required to be braced?
531 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
532 attrs: many0!(call!(Attribute::parse_outer)) >>
533 vis: syn!(Visibility) >>
534 macro_: keyword!(macro) >>
535 ident: syn!(Ident) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500536 args: call!(mac::parsing::parse_tt_delimited) >>
537 body: call!(mac::parsing::parse_tt_delimited) >>
David Tolnay500d8322017-12-18 00:32:51 -0800538 (ItemMacro2 {
539 attrs: attrs,
540 vis: vis,
541 macro_token: macro_,
542 ident: ident,
543 args: args,
544 body: body,
545 })
546 ));
547
David Tolnay4c614be2017-11-10 00:02:38 -0800548 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700549 attrs: many0!(call!(Attribute::parse_outer)) >>
550 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 extern_: keyword!(extern) >>
552 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700553 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800554 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
555 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800556 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700557 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800558 vis: vis,
559 extern_token: extern_,
560 crate_token: crate_,
561 ident: ident,
562 rename: rename,
563 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800564 })
David Tolnayedf2b992016-09-23 20:43:45 -0700565 ));
566
David Tolnay4c614be2017-11-10 00:02:38 -0800567 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700568 attrs: many0!(call!(Attribute::parse_outer)) >>
569 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800570 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500571 leading_colon: option!(punct!(::)) >>
572 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
573 tree: switch!(value!(leading_colon.is_none() && prefix.is_empty()),
574 true => syn!(UseTree)
575 |
576 false => switch!(value!(prefix.empty_or_trailing()),
577 true => syn!(UseTree)
578 |
579 false => alt!(
580 tuple!(keyword!(as), syn!(Ident)) => {
581 |rename| UseTree::Path(UsePath {
582 ident: prefix.pop().unwrap().into_item(),
583 rename: Some(rename),
584 })
585 }
586 |
587 epsilon!() => {
588 |_| UseTree::Path(UsePath {
589 ident: prefix.pop().unwrap().into_item(),
590 rename: None,
591 })
592 }
593 )
594 )
595 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800597 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700598 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800599 vis: vis,
600 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500601 leading_colon: leading_colon,
602 prefix: prefix,
603 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800604 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800605 })
David Tolnay4a057422016-10-08 00:02:31 -0700606 ));
607
David Tolnay5f332a92017-12-26 00:42:45 -0500608 named!(use_prefix -> Ident, alt!(
609 syn!(Ident)
610 |
611 keyword!(self) => { Into::into }
612 |
613 keyword!(super) => { Into::into }
614 |
615 keyword!(crate) => { Into::into }
616 ));
617
618 impl_synom!(UseTree "use tree" alt!(
619 syn!(UsePath) => { UseTree::Path }
620 |
621 syn!(UseGlob) => { UseTree::Glob }
622 |
623 syn!(UseList) => { UseTree::List }
624 ));
625
626 impl_synom!(UsePath "use path" do_parse!(
627 ident: alt!(
628 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400629 |
David Tolnay5f332a92017-12-26 00:42:45 -0500630 keyword!(self) => { Into::into }
631 ) >>
632 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
633 (UsePath {
634 ident: ident,
635 rename: rename,
636 })
637 ));
David Tolnay4a057422016-10-08 00:02:31 -0700638
David Tolnay5f332a92017-12-26 00:42:45 -0500639 impl_synom!(UseGlob "use glob" do_parse!(
640 star: punct!(*) >>
641 (UseGlob {
642 star_token: star,
643 })
644 ));
David Tolnay4a057422016-10-08 00:02:31 -0700645
David Tolnay5f332a92017-12-26 00:42:45 -0500646 impl_synom!(UseList "use list" do_parse!(
647 list: braces!(Delimited::parse_terminated) >>
648 (UseList {
649 brace_token: list.1,
650 items: list.0,
651 })
652 ));
David Tolnay4a057422016-10-08 00:02:31 -0700653
David Tolnay4c614be2017-11-10 00:02:38 -0800654 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700655 attrs: many0!(call!(Attribute::parse_outer)) >>
656 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800657 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700658 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700659 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800660 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800661 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700663 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800664 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800665 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700666 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800667 vis: vis,
668 static_token: static_,
669 mutbl: mutability,
670 ident: ident,
671 colon_token: colon,
672 ty: Box::new(ty),
673 eq_token: eq,
674 expr: Box::new(value),
675 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800676 })
David Tolnay47a877c2016-10-01 16:50:55 -0700677 ));
678
David Tolnay4c614be2017-11-10 00:02:38 -0800679 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700680 attrs: many0!(call!(Attribute::parse_outer)) >>
681 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800682 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700683 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800684 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800685 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800686 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700687 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800688 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800689 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700690 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800691 vis: vis,
692 const_token: const_,
693 ident: ident,
694 colon_token: colon,
695 ty: Box::new(ty),
696 eq_token: eq,
697 expr: Box::new(value),
698 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800699 })
David Tolnay47a877c2016-10-01 16:50:55 -0700700 ));
701
David Tolnay4c614be2017-11-10 00:02:38 -0800702 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700703 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
704 vis: syn!(Visibility) >>
705 constness: syn!(Constness) >>
706 unsafety: syn!(Unsafety) >>
707 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800708 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700709 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700710 generics: syn!(Generics) >>
711 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800712 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700713 where_clause: syn!(WhereClause) >>
714 inner_attrs_stmts: braces!(tuple!(
715 many0!(call!(Attribute::parse_inner)),
716 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400717 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800718 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700719 attrs: {
720 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700721 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700722 attrs
723 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800724 vis: vis,
725 constness: constness,
726 unsafety: unsafety,
727 abi: abi,
728 decl: Box::new(FnDecl {
729 dot_tokens: None,
730 fn_token: fn_,
731 paren_token: inputs.1,
732 inputs: inputs.0,
733 output: ret,
734 variadic: false,
735 generics: Generics {
736 where_clause: where_clause,
737 .. generics
738 },
739 }),
740 ident: ident,
741 block: Box::new(Block {
742 brace_token: inner_attrs_stmts.1,
743 stmts: (inner_attrs_stmts.0).1,
744 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800745 })
David Tolnay42602292016-10-01 22:25:45 -0700746 ));
747
Alex Crichton954046c2017-05-30 21:49:42 -0700748 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400749 named!(parse -> Self, alt!(
750 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800751 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400752 lt: option!(syn!(Lifetime)) >>
753 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800754 self_: keyword!(self) >>
755 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400756 (ArgSelfRef {
757 lifetime: lt,
758 mutbl: mutability,
759 and_token: and,
760 self_token: self_,
761 }.into())
762 )
763 |
764 do_parse!(
765 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 self_: keyword!(self) >>
767 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400768 (ArgSelf {
769 mutbl: mutability,
770 self_token: self_,
771 }.into())
772 )
773 |
774 do_parse!(
775 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800776 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800777 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400778 (ArgCaptured {
779 pat: pat,
780 ty: ty,
781 colon_token: colon,
782 }.into())
783 )
784 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800785 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400786 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700787 }
David Tolnay62f374c2016-10-02 13:37:00 -0700788
David Tolnay4c614be2017-11-10 00:02:38 -0800789 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700790 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
791 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800792 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700793 ident: syn!(Ident) >>
794 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800795 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700796 Vec::new(),
797 None,
798 Some(semi),
799 )}
David Tolnay37d10332016-10-13 20:51:04 -0700800 |
Alex Crichton954046c2017-05-30 21:49:42 -0700801 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700802 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700803 many0!(call!(Attribute::parse_inner)),
804 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400805 )
David Tolnay570695e2017-06-03 16:15:13 -0700806 ) => {|((inner_attrs, items), brace)| (
807 inner_attrs,
808 Some((brace, items)),
809 None,
810 )}
David Tolnay37d10332016-10-13 20:51:04 -0700811 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800812 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700813 attrs: {
814 let mut attrs = outer_attrs;
815 attrs.extend(content_semi.0);
816 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700817 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800818 vis: vis,
819 mod_token: mod_,
820 ident: ident,
821 content: content_semi.1,
822 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800823 })
David Tolnay35902302016-10-06 01:11:08 -0700824 ));
825
David Tolnay4c614be2017-11-10 00:02:38 -0800826 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700827 attrs: many0!(call!(Attribute::parse_outer)) >>
828 abi: syn!(Abi) >>
829 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800830 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700831 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800832 abi: abi,
833 brace_token: items.1,
834 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800835 })
David Tolnay35902302016-10-06 01:11:08 -0700836 ));
837
David Tolnay8894f602017-11-11 12:11:04 -0800838 impl_synom!(ForeignItem "foreign item" alt!(
839 syn!(ForeignItemFn) => { ForeignItem::Fn }
840 |
841 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800842 |
843 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800844 ));
David Tolnay35902302016-10-06 01:11:08 -0700845
David Tolnay8894f602017-11-11 12:11:04 -0800846 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700847 attrs: many0!(call!(Attribute::parse_outer)) >>
848 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800849 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700850 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700851 generics: syn!(Generics) >>
852 inputs: parens!(do_parse!(
853 args: call!(Delimited::parse_terminated) >>
854 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800855 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700856 (args, variadic)
857 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800858 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700859 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800860 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700861 ({
862 let ((inputs, variadic), parens) = inputs;
863 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800864 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700865 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700866 attrs: attrs,
867 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800868 decl: Box::new(FnDecl {
869 fn_token: fn_,
870 paren_token: parens,
871 inputs: inputs,
872 variadic: variadic.is_some(),
873 dot_tokens: variadic,
874 output: ret,
875 generics: Generics {
876 where_clause: where_clause,
877 .. generics
878 },
879 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700880 vis: vis,
881 }
David Tolnay35902302016-10-06 01:11:08 -0700882 })
883 ));
884
David Tolnay8894f602017-11-11 12:11:04 -0800885 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700886 attrs: many0!(call!(Attribute::parse_outer)) >>
887 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800888 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700889 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700890 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800891 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800892 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800893 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800894 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700895 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700896 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700897 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800898 ty: Box::new(ty),
899 mutbl: mutability,
900 static_token: static_,
901 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700902 vis: vis,
903 })
904 ));
905
David Tolnay199bcbb2017-11-12 10:33:52 -0800906 impl_synom!(ForeignItemType "foreign type" do_parse!(
907 attrs: many0!(call!(Attribute::parse_outer)) >>
908 vis: syn!(Visibility) >>
909 type_: keyword!(type) >>
910 ident: syn!(Ident) >>
911 semi: punct!(;) >>
912 (ForeignItemType {
913 attrs: attrs,
914 vis: vis,
915 type_token: type_,
916 ident: ident,
917 semi_token: semi,
918 })
919 ));
920
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800921 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700922 attrs: many0!(call!(Attribute::parse_outer)) >>
923 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800924 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700925 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700926 generics: syn!(Generics) >>
927 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800928 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800929 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800930 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800931 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700932 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800933 vis: vis,
934 type_token: type_,
935 ident: ident,
936 generics: Generics {
937 where_clause: where_clause,
938 ..generics
939 },
940 eq_token: eq,
941 ty: Box::new(ty),
942 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800943 })
David Tolnay3cf52982016-10-01 17:11:37 -0700944 ));
945
David Tolnay4c614be2017-11-10 00:02:38 -0800946 impl_synom!(ItemStruct "struct item" switch!(
947 map!(syn!(DeriveInput), Into::into),
948 Item::Struct(item) => value!(item)
949 |
950 _ => reject!()
951 ));
David Tolnay42602292016-10-01 22:25:45 -0700952
David Tolnay4c614be2017-11-10 00:02:38 -0800953 impl_synom!(ItemEnum "enum item" switch!(
954 map!(syn!(DeriveInput), Into::into),
955 Item::Enum(item) => value!(item)
956 |
957 _ => reject!()
958 ));
959
960 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700961 attrs: many0!(call!(Attribute::parse_outer)) >>
962 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700964 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700965 generics: syn!(Generics) >>
966 where_clause: syn!(WhereClause) >>
967 fields: braces!(call!(Delimited::parse_terminated_with,
968 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800969 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700970 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800971 vis: vis,
972 union_token: union_,
973 ident: ident,
974 generics: Generics {
975 where_clause: where_clause,
976 .. generics
977 },
978 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800979 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700980 ));
981
David Tolnay4c614be2017-11-10 00:02:38 -0800982 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700983 attrs: many0!(call!(Attribute::parse_outer)) >>
984 vis: syn!(Visibility) >>
985 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500986 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800987 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700988 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700989 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700992 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700993 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700994 where_clause: syn!(WhereClause) >>
995 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800996 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700997 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800998 vis: vis,
999 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001000 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001001 trait_token: trait_,
1002 ident: ident,
1003 generics: Generics {
1004 where_clause: where_clause,
1005 .. generics
1006 },
1007 colon_token: colon,
1008 supertraits: bounds.unwrap_or_default(),
1009 brace_token: body.1,
1010 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001011 })
David Tolnay0aecb732016-10-03 23:03:50 -07001012 ));
1013
David Tolnay4c614be2017-11-10 00:02:38 -08001014 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001015 attrs: many0!(call!(Attribute::parse_outer)) >>
1016 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001018 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001019 for_: keyword!(for) >>
1020 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001021 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001022 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001023 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001024 unsafety: unsafety,
1025 impl_token: impl_,
1026 path: path,
1027 for_token: for_,
1028 dot2_token: dot2,
1029 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001030 })
David Tolnayf94e2362016-10-04 00:29:51 -07001031 ));
1032
David Tolnayda705bd2017-11-10 21:58:05 -08001033 impl_synom!(TraitItem "trait item" alt!(
1034 syn!(TraitItemConst) => { TraitItem::Const }
1035 |
1036 syn!(TraitItemMethod) => { TraitItem::Method }
1037 |
1038 syn!(TraitItemType) => { TraitItem::Type }
1039 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001040 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001041 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001042
David Tolnayda705bd2017-11-10 21:58:05 -08001043 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001044 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001045 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001046 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001047 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001048 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001049 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1050 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001051 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001052 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001053 const_token: const_,
1054 ident: ident,
1055 colon_token: colon,
1056 ty: ty,
1057 default: default,
1058 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001059 })
1060 ));
1061
David Tolnayda705bd2017-11-10 21:58:05 -08001062 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001063 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1064 constness: syn!(Constness) >>
1065 unsafety: syn!(Unsafety) >>
1066 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001067 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001068 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001069 generics: syn!(Generics) >>
1070 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001071 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001072 where_clause: syn!(WhereClause) >>
1073 body: option!(braces!(
1074 tuple!(many0!(call!(Attribute::parse_inner)),
1075 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001076 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001077 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001078 ({
1079 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001080 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001081 None => (Vec::new(), None),
1082 };
David Tolnayda705bd2017-11-10 21:58:05 -08001083 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001084 attrs: {
1085 let mut attrs = outer_attrs;
1086 attrs.extend(inner_attrs);
1087 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001088 },
David Tolnayda705bd2017-11-10 21:58:05 -08001089 sig: MethodSig {
1090 constness: constness,
1091 unsafety: unsafety,
1092 abi: abi,
1093 ident: ident,
1094 decl: FnDecl {
1095 inputs: inputs.0,
1096 output: ret,
1097 variadic: false,
1098 fn_token: fn_,
1099 paren_token: inputs.1,
1100 dot_tokens: None,
1101 generics: Generics {
1102 where_clause: where_clause,
1103 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001104 },
1105 },
David Tolnayda705bd2017-11-10 21:58:05 -08001106 },
1107 default: stmts.map(|stmts| {
1108 Block {
1109 stmts: stmts.0,
1110 brace_token: stmts.1,
1111 }
1112 }),
1113 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001114 }
David Tolnay0aecb732016-10-03 23:03:50 -07001115 })
1116 ));
1117
David Tolnayda705bd2017-11-10 21:58:05 -08001118 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001119 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001120 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001121 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001122 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001123 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001124 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001125 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001126 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001127 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001128 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001129 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001130 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001131 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001132 type_token: type_,
1133 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001134 generics: Generics {
1135 where_clause: where_clause,
1136 .. generics
1137 },
David Tolnayda705bd2017-11-10 21:58:05 -08001138 colon_token: colon,
1139 bounds: bounds.unwrap_or_default(),
1140 default: default,
1141 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001142 })
1143 ));
1144
David Tolnaydecf28d2017-11-11 11:56:45 -08001145 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001146 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001147 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001148 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001149 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001150 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001151 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001152 })
1153 ));
1154
David Tolnay4c614be2017-11-10 00:02:38 -08001155 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001156 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001157 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001158 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001159 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001160 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001161 polarity_path: alt!(
1162 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001163 polarity: syn!(ImplPolarity) >>
1164 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001165 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001166 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001167 )
1168 |
David Tolnay570695e2017-06-03 16:15:13 -07001169 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001170 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001171 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001172 where_clause: syn!(WhereClause) >>
1173 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001174 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001175 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001176 defaultness: defaultness,
1177 unsafety: unsafety,
1178 impl_token: impl_,
1179 generics: Generics {
1180 where_clause: where_clause,
1181 .. generics
1182 },
1183 trait_: polarity_path,
1184 self_ty: Box::new(self_ty),
1185 brace_token: body.1,
1186 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001187 })
David Tolnay4c9be372016-10-06 00:47:37 -07001188 ));
1189
David Tolnay857628c2017-11-11 12:25:31 -08001190 impl_synom!(ImplItem "item in impl block" alt!(
1191 syn!(ImplItemConst) => { ImplItem::Const }
1192 |
1193 syn!(ImplItemMethod) => { ImplItem::Method }
1194 |
1195 syn!(ImplItemType) => { ImplItem::Type }
1196 |
1197 syn!(ImplItemMacro) => { ImplItem::Macro }
1198 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001199
David Tolnay857628c2017-11-11 12:25:31 -08001200 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001201 attrs: many0!(call!(Attribute::parse_outer)) >>
1202 vis: syn!(Visibility) >>
1203 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001204 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001205 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001206 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001207 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001208 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001209 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001211 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001212 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001213 vis: vis,
1214 defaultness: defaultness,
1215 const_token: const_,
1216 ident: ident,
1217 colon_token: colon,
1218 ty: ty,
1219 eq_token: eq,
1220 expr: value,
1221 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001222 })
1223 ));
1224
David Tolnay857628c2017-11-11 12:25:31 -08001225 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001226 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1227 vis: syn!(Visibility) >>
1228 defaultness: syn!(Defaultness) >>
1229 constness: syn!(Constness) >>
1230 unsafety: syn!(Unsafety) >>
1231 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001232 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001233 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001234 generics: syn!(Generics) >>
1235 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001236 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001237 where_clause: syn!(WhereClause) >>
1238 inner_attrs_stmts: braces!(tuple!(
1239 many0!(call!(Attribute::parse_inner)),
1240 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001241 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001242 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001243 attrs: {
1244 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001245 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001246 attrs
1247 },
David Tolnay857628c2017-11-11 12:25:31 -08001248 vis: vis,
1249 defaultness: defaultness,
1250 sig: MethodSig {
1251 constness: constness,
1252 unsafety: unsafety,
1253 abi: abi,
1254 ident: ident,
1255 decl: FnDecl {
1256 fn_token: fn_,
1257 paren_token: inputs.1,
1258 inputs: inputs.0,
1259 output: ret,
1260 variadic: false,
1261 generics: Generics {
1262 where_clause: where_clause,
1263 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001264 },
David Tolnay857628c2017-11-11 12:25:31 -08001265 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001266 },
David Tolnay857628c2017-11-11 12:25:31 -08001267 },
1268 block: Block {
1269 brace_token: inner_attrs_stmts.1,
1270 stmts: (inner_attrs_stmts.0).1,
1271 },
David Tolnay4c9be372016-10-06 00:47:37 -07001272 })
1273 ));
1274
David Tolnay857628c2017-11-11 12:25:31 -08001275 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001276 attrs: many0!(call!(Attribute::parse_outer)) >>
1277 vis: syn!(Visibility) >>
1278 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001279 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001280 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001281 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001282 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001283 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001284 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001285 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001286 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001287 vis: vis,
1288 defaultness: defaultness,
1289 type_token: type_,
1290 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001291 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001292 eq_token: eq,
1293 ty: ty,
1294 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001295 })
1296 ));
1297
David Tolnay857628c2017-11-11 12:25:31 -08001298 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001299 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001300 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001301 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001302 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001303 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001304 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001305 })
1306 ));
1307
Alex Crichton954046c2017-05-30 21:49:42 -07001308 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001309 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001310 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001311 |
1312 epsilon!() => { |_| ImplPolarity::Positive }
1313 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001314 }
David Tolnay4c9be372016-10-06 00:47:37 -07001315
Alex Crichton954046c2017-05-30 21:49:42 -07001316 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001317 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001318 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001319 |
1320 epsilon!() => { |_| Constness::NotConst }
1321 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001322 }
David Tolnay42602292016-10-01 22:25:45 -07001323
Alex Crichton954046c2017-05-30 21:49:42 -07001324 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001325 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001326 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001327 |
1328 epsilon!() => { |_| Defaultness::Final }
1329 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001330 }
David Tolnayedf2b992016-09-23 20:43:45 -07001331}
David Tolnay4a51dc72016-10-01 00:40:31 -07001332
1333#[cfg(feature = "printing")]
1334mod printing {
1335 use super::*;
1336 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001337 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001338 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001339
David Tolnay1bfa7332017-11-11 12:41:20 -08001340 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001341 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001342 tokens.append_all(self.attrs.outer());
1343 self.vis.to_tokens(tokens);
1344 self.extern_token.to_tokens(tokens);
1345 self.crate_token.to_tokens(tokens);
1346 self.ident.to_tokens(tokens);
1347 if let Some((ref as_token, ref rename)) = self.rename {
1348 as_token.to_tokens(tokens);
1349 rename.to_tokens(tokens);
1350 }
1351 self.semi_token.to_tokens(tokens);
1352 }
1353 }
1354
1355 impl ToTokens for ItemUse {
1356 fn to_tokens(&self, tokens: &mut Tokens) {
1357 tokens.append_all(self.attrs.outer());
1358 self.vis.to_tokens(tokens);
1359 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001360 self.leading_colon.to_tokens(tokens);
1361 self.prefix.to_tokens(tokens);
1362 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001363 self.semi_token.to_tokens(tokens);
1364 }
1365 }
1366
1367 impl ToTokens for ItemStatic {
1368 fn to_tokens(&self, tokens: &mut Tokens) {
1369 tokens.append_all(self.attrs.outer());
1370 self.vis.to_tokens(tokens);
1371 self.static_token.to_tokens(tokens);
1372 self.mutbl.to_tokens(tokens);
1373 self.ident.to_tokens(tokens);
1374 self.colon_token.to_tokens(tokens);
1375 self.ty.to_tokens(tokens);
1376 self.eq_token.to_tokens(tokens);
1377 self.expr.to_tokens(tokens);
1378 self.semi_token.to_tokens(tokens);
1379 }
1380 }
1381
1382 impl ToTokens for ItemConst {
1383 fn to_tokens(&self, tokens: &mut Tokens) {
1384 tokens.append_all(self.attrs.outer());
1385 self.vis.to_tokens(tokens);
1386 self.const_token.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 ItemFn {
1397 fn to_tokens(&self, tokens: &mut Tokens) {
1398 tokens.append_all(self.attrs.outer());
1399 self.vis.to_tokens(tokens);
1400 self.constness.to_tokens(tokens);
1401 self.unsafety.to_tokens(tokens);
1402 self.abi.to_tokens(tokens);
1403 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1404 self.block.brace_token.surround(tokens, |tokens| {
1405 tokens.append_all(self.attrs.inner());
1406 tokens.append_all(&self.block.stmts);
1407 });
1408 }
1409 }
1410
1411 impl ToTokens for ItemMod {
1412 fn to_tokens(&self, tokens: &mut Tokens) {
1413 tokens.append_all(self.attrs.outer());
1414 self.vis.to_tokens(tokens);
1415 self.mod_token.to_tokens(tokens);
1416 self.ident.to_tokens(tokens);
1417 if let Some((ref brace, ref items)) = self.content {
1418 brace.surround(tokens, |tokens| {
1419 tokens.append_all(self.attrs.inner());
1420 tokens.append_all(items);
1421 });
1422 } else {
1423 TokensOrDefault(&self.semi).to_tokens(tokens);
1424 }
1425 }
1426 }
1427
1428 impl ToTokens for ItemForeignMod {
1429 fn to_tokens(&self, tokens: &mut Tokens) {
1430 tokens.append_all(self.attrs.outer());
1431 self.abi.to_tokens(tokens);
1432 self.brace_token.surround(tokens, |tokens| {
1433 tokens.append_all(&self.items);
1434 });
1435 }
1436 }
1437
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001438 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001439 fn to_tokens(&self, tokens: &mut Tokens) {
1440 tokens.append_all(self.attrs.outer());
1441 self.vis.to_tokens(tokens);
1442 self.type_token.to_tokens(tokens);
1443 self.ident.to_tokens(tokens);
1444 self.generics.to_tokens(tokens);
1445 self.generics.where_clause.to_tokens(tokens);
1446 self.eq_token.to_tokens(tokens);
1447 self.ty.to_tokens(tokens);
1448 self.semi_token.to_tokens(tokens);
1449 }
1450 }
1451
1452 impl ToTokens for ItemEnum {
1453 fn to_tokens(&self, tokens: &mut Tokens) {
1454 tokens.append_all(self.attrs.outer());
1455 self.vis.to_tokens(tokens);
1456 self.enum_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.brace_token.surround(tokens, |tokens| {
1461 self.variants.to_tokens(tokens);
1462 });
1463 }
1464 }
1465
1466 impl ToTokens for ItemStruct {
1467 fn to_tokens(&self, tokens: &mut Tokens) {
1468 tokens.append_all(self.attrs.outer());
1469 self.vis.to_tokens(tokens);
1470 self.struct_token.to_tokens(tokens);
1471 self.ident.to_tokens(tokens);
1472 self.generics.to_tokens(tokens);
1473 match self.data {
1474 VariantData::Struct(..) => {
1475 self.generics.where_clause.to_tokens(tokens);
1476 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001477 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001478 VariantData::Tuple(..) => {
1479 self.data.to_tokens(tokens);
1480 self.generics.where_clause.to_tokens(tokens);
1481 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001482 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001483 VariantData::Unit => {
1484 self.generics.where_clause.to_tokens(tokens);
1485 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001486 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001487 }
1488 }
1489 }
1490
1491 impl ToTokens for ItemUnion {
1492 fn to_tokens(&self, tokens: &mut Tokens) {
1493 tokens.append_all(self.attrs.outer());
1494 self.vis.to_tokens(tokens);
1495 self.union_token.to_tokens(tokens);
1496 self.ident.to_tokens(tokens);
1497 self.generics.to_tokens(tokens);
1498 self.generics.where_clause.to_tokens(tokens);
1499 // XXX: Should we handle / complain when using a
1500 // non-VariantData::Struct Variant in Union?
1501 self.data.to_tokens(tokens);
1502 }
1503 }
1504
1505 impl ToTokens for ItemTrait {
1506 fn to_tokens(&self, tokens: &mut Tokens) {
1507 tokens.append_all(self.attrs.outer());
1508 self.vis.to_tokens(tokens);
1509 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001510 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001511 self.trait_token.to_tokens(tokens);
1512 self.ident.to_tokens(tokens);
1513 self.generics.to_tokens(tokens);
1514 if !self.supertraits.is_empty() {
1515 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1516 self.supertraits.to_tokens(tokens);
1517 }
1518 self.generics.where_clause.to_tokens(tokens);
1519 self.brace_token.surround(tokens, |tokens| {
1520 tokens.append_all(&self.items);
1521 });
1522 }
1523 }
1524
1525 impl ToTokens for ItemDefaultImpl {
1526 fn to_tokens(&self, tokens: &mut Tokens) {
1527 tokens.append_all(self.attrs.outer());
1528 self.unsafety.to_tokens(tokens);
1529 self.impl_token.to_tokens(tokens);
1530 self.path.to_tokens(tokens);
1531 self.for_token.to_tokens(tokens);
1532 self.dot2_token.to_tokens(tokens);
1533 self.brace_token.surround(tokens, |_tokens| {});
1534 }
1535 }
1536
1537 impl ToTokens for ItemImpl {
1538 fn to_tokens(&self, tokens: &mut Tokens) {
1539 tokens.append_all(self.attrs.outer());
1540 self.defaultness.to_tokens(tokens);
1541 self.unsafety.to_tokens(tokens);
1542 self.impl_token.to_tokens(tokens);
1543 self.generics.to_tokens(tokens);
1544 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1545 polarity.to_tokens(tokens);
1546 path.to_tokens(tokens);
1547 for_token.to_tokens(tokens);
1548 }
1549 self.self_ty.to_tokens(tokens);
1550 self.generics.where_clause.to_tokens(tokens);
1551 self.brace_token.surround(tokens, |tokens| {
1552 tokens.append_all(&self.items);
1553 });
1554 }
1555 }
1556
1557 impl ToTokens for ItemMacro {
1558 fn to_tokens(&self, tokens: &mut Tokens) {
1559 tokens.append_all(self.attrs.outer());
1560 self.mac.path.to_tokens(tokens);
1561 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001562 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001563 self.mac.tokens.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001564 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001565 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001566 }
1567 }
1568 }
David Tolnay42602292016-10-01 22:25:45 -07001569
David Tolnay500d8322017-12-18 00:32:51 -08001570 impl ToTokens for ItemMacro2 {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 tokens.append_all(self.attrs.outer());
1573 self.vis.to_tokens(tokens);
1574 self.macro_token.to_tokens(tokens);
1575 self.ident.to_tokens(tokens);
1576 self.args.to_tokens(tokens);
1577 self.body.to_tokens(tokens);
1578 }
1579 }
1580
David Tolnay5f332a92017-12-26 00:42:45 -05001581 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001582 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001583 self.ident.to_tokens(tokens);
1584 if let Some((ref as_token, ref rename)) = self.rename {
1585 as_token.to_tokens(tokens);
1586 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001587 }
David Tolnay4a057422016-10-08 00:02:31 -07001588 }
1589 }
1590
David Tolnay5f332a92017-12-26 00:42:45 -05001591 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001592 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001593 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001594 }
1595 }
1596
David Tolnay5f332a92017-12-26 00:42:45 -05001597 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001598 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001599 self.brace_token.surround(tokens, |tokens| {
1600 self.items.to_tokens(tokens);
1601 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001602 }
1603 }
1604
David Tolnay1bfa7332017-11-11 12:41:20 -08001605 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001606 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001607 tokens.append_all(self.attrs.outer());
1608 self.const_token.to_tokens(tokens);
1609 self.ident.to_tokens(tokens);
1610 self.colon_token.to_tokens(tokens);
1611 self.ty.to_tokens(tokens);
1612 if let Some((ref eq_token, ref default)) = self.default {
1613 eq_token.to_tokens(tokens);
1614 default.to_tokens(tokens);
1615 }
1616 self.semi_token.to_tokens(tokens);
1617 }
1618 }
1619
1620 impl ToTokens for TraitItemMethod {
1621 fn to_tokens(&self, tokens: &mut Tokens) {
1622 tokens.append_all(self.attrs.outer());
1623 self.sig.to_tokens(tokens);
1624 match self.default {
1625 Some(ref block) => {
1626 block.brace_token.surround(tokens, |tokens| {
1627 tokens.append_all(self.attrs.inner());
1628 tokens.append_all(&block.stmts);
1629 });
David Tolnayca085422016-10-04 00:12:38 -07001630 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001631 None => {
1632 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001633 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001634 }
1635 }
1636 }
1637
1638 impl ToTokens for TraitItemType {
1639 fn to_tokens(&self, tokens: &mut Tokens) {
1640 tokens.append_all(self.attrs.outer());
1641 self.type_token.to_tokens(tokens);
1642 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001643 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001644 if !self.bounds.is_empty() {
1645 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1646 self.bounds.to_tokens(tokens);
1647 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001648 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001649 if let Some((ref eq_token, ref default)) = self.default {
1650 eq_token.to_tokens(tokens);
1651 default.to_tokens(tokens);
1652 }
1653 self.semi_token.to_tokens(tokens);
1654 }
1655 }
1656
1657 impl ToTokens for TraitItemMacro {
1658 fn to_tokens(&self, tokens: &mut Tokens) {
1659 tokens.append_all(self.attrs.outer());
1660 self.mac.to_tokens(tokens);
1661 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001662 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001663 }
1664 }
1665 }
1666
David Tolnay857628c2017-11-11 12:25:31 -08001667 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001670 self.vis.to_tokens(tokens);
1671 self.defaultness.to_tokens(tokens);
1672 self.const_token.to_tokens(tokens);
1673 self.ident.to_tokens(tokens);
1674 self.colon_token.to_tokens(tokens);
1675 self.ty.to_tokens(tokens);
1676 self.eq_token.to_tokens(tokens);
1677 self.expr.to_tokens(tokens);
1678 self.semi_token.to_tokens(tokens);
1679 }
1680 }
1681
1682 impl ToTokens for ImplItemMethod {
1683 fn to_tokens(&self, tokens: &mut Tokens) {
1684 tokens.append_all(self.attrs.outer());
1685 self.vis.to_tokens(tokens);
1686 self.defaultness.to_tokens(tokens);
1687 self.sig.to_tokens(tokens);
1688 self.block.brace_token.surround(tokens, |tokens| {
1689 tokens.append_all(self.attrs.inner());
1690 tokens.append_all(&self.block.stmts);
1691 });
1692 }
1693 }
1694
1695 impl ToTokens for ImplItemType {
1696 fn to_tokens(&self, tokens: &mut Tokens) {
1697 tokens.append_all(self.attrs.outer());
1698 self.vis.to_tokens(tokens);
1699 self.defaultness.to_tokens(tokens);
1700 self.type_token.to_tokens(tokens);
1701 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001702 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001703 self.eq_token.to_tokens(tokens);
1704 self.ty.to_tokens(tokens);
1705 self.semi_token.to_tokens(tokens);
1706 }
1707 }
1708
1709 impl ToTokens for ImplItemMacro {
1710 fn to_tokens(&self, tokens: &mut Tokens) {
1711 tokens.append_all(self.attrs.outer());
1712 self.mac.to_tokens(tokens);
1713 if !self.mac.is_braced() {
1714 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001715 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001716 }
1717 }
1718 }
1719
David Tolnay8894f602017-11-11 12:11:04 -08001720 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001721 fn to_tokens(&self, tokens: &mut Tokens) {
1722 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001723 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001724 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1725 self.semi_token.to_tokens(tokens);
1726 }
1727 }
1728
1729 impl ToTokens for ForeignItemStatic {
1730 fn to_tokens(&self, tokens: &mut Tokens) {
1731 tokens.append_all(self.attrs.outer());
1732 self.vis.to_tokens(tokens);
1733 self.static_token.to_tokens(tokens);
1734 self.mutbl.to_tokens(tokens);
1735 self.ident.to_tokens(tokens);
1736 self.colon_token.to_tokens(tokens);
1737 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001738 self.semi_token.to_tokens(tokens);
1739 }
1740 }
1741
David Tolnay199bcbb2017-11-12 10:33:52 -08001742 impl ToTokens for ForeignItemType {
1743 fn to_tokens(&self, tokens: &mut Tokens) {
1744 tokens.append_all(self.attrs.outer());
1745 self.vis.to_tokens(tokens);
1746 self.type_token.to_tokens(tokens);
1747 self.ident.to_tokens(tokens);
1748 self.semi_token.to_tokens(tokens);
1749 }
1750 }
1751
David Tolnay570695e2017-06-03 16:15:13 -07001752 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001753 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001754 self.constness.to_tokens(tokens);
1755 self.unsafety.to_tokens(tokens);
1756 self.abi.to_tokens(tokens);
1757 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 }
1759 }
1760
David Tolnay570695e2017-06-03 16:15:13 -07001761 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001762
1763 impl<'a> ToTokens for NamedDecl<'a> {
1764 fn to_tokens(&self, tokens: &mut Tokens) {
1765 self.0.fn_token.to_tokens(tokens);
1766 self.1.to_tokens(tokens);
1767 self.0.generics.to_tokens(tokens);
1768 self.0.paren_token.surround(tokens, |tokens| {
1769 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001770
1771 if self.0.variadic {
1772 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001773 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001774 }
Alex Crichton259ee532017-07-14 06:51:02 -07001775 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001776 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001777 });
1778 self.0.output.to_tokens(tokens);
1779 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001780 }
1781 }
1782
Alex Crichton62a0a592017-05-22 13:58:53 -07001783 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001784 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001785 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001786 self.lifetime.to_tokens(tokens);
1787 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001788 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001789 }
1790 }
1791
1792 impl ToTokens for ArgSelf {
1793 fn to_tokens(&self, tokens: &mut Tokens) {
1794 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001795 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001796 }
1797 }
1798
1799 impl ToTokens for ArgCaptured {
1800 fn to_tokens(&self, tokens: &mut Tokens) {
1801 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001803 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001804 }
1805 }
1806
David Tolnay42602292016-10-01 22:25:45 -07001807 impl ToTokens for Constness {
1808 fn to_tokens(&self, tokens: &mut Tokens) {
1809 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001811 Constness::NotConst => {
1812 // nothing
1813 }
David Tolnay42602292016-10-01 22:25:45 -07001814 }
1815 }
1816 }
1817
David Tolnay4c9be372016-10-06 00:47:37 -07001818 impl ToTokens for Defaultness {
1819 fn to_tokens(&self, tokens: &mut Tokens) {
1820 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001822 Defaultness::Final => {
1823 // nothing
1824 }
1825 }
1826 }
1827 }
1828
1829 impl ToTokens for ImplPolarity {
1830 fn to_tokens(&self, tokens: &mut Tokens) {
1831 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001832 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001833 ImplPolarity::Positive => {
1834 // nothing
1835 }
1836 }
1837 }
1838 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001839}