blob: 476bfbb40cdb54aee8ac86dbcf11a36d7f20b9bc [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -08005 /// Things that can appear directly inside of a module.
6 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -07007 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -07008 ///
9 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
10 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080011 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070012 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 pub extern_token: Token![extern],
14 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070015 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub rename: Option<(Token![as], Ident)>,
17 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070018 }),
19 /// A use declaration (`use` or `pub use`) item.
20 ///
21 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
22 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080023 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070024 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 pub use_token: Token![use],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub path: Box<ViewPath>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070028 }),
29 /// A static item (`static` or `pub static`).
30 ///
31 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
32 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080033 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070034 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080038 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080039 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070041 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
44 /// A constant item (`const` or `pub const`).
45 ///
46 /// E.g. `const FOO: i32 = 42;`
47 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// A function declaration (`fn` or `pub fn`).
59 ///
60 /// E.g. `fn foo(bar: usize) -> usize { .. }`
61 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080062 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070063 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -070065 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070067 pub decl: Box<FnDecl>,
68 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub block: Box<Block>,
70 }),
71 /// A module declaration (`mod` or `pub mod`).
72 ///
73 /// E.g. `mod foo;` or `mod foo { .. }`
74 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080075 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070076 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070078 pub ident: Ident,
79 pub content: Option<(tokens::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070081 }),
82 /// An external module (`extern` or `pub extern`).
83 ///
84 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080086 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070087 pub abi: Abi,
David Tolnay570695e2017-06-03 16:15:13 -070088 pub brace_token: tokens::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070089 pub items: Vec<ForeignItem>,
90 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070091 /// A type alias (`type` or `pub type`).
92 ///
93 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -080094 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070096 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080097 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800101 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 }),
104 /// An enum definition (`enum` or `pub enum`).
105 ///
106 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
107 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800108 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700111 pub ident: Ident,
112 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700113 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 }),
116 /// A struct definition (`struct` or `pub struct`).
117 ///
118 /// E.g. `struct Foo<A> { x: A }`
119 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800120 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700121 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700123 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700125 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 }),
128 /// A union definition (`union` or `pub union`).
129 ///
130 /// E.g. `union Foo<A, B> { x: A, y: B }`
131 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800132 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700135 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700137 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 }),
139 /// A Trait declaration (`trait` or `pub trait`).
140 ///
141 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
142 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800143 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700144 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 pub unsafety: Unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500146 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800147 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700148 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800150 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800151 pub supertraits: Delimited<TypeParamBound, Token![+]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700152 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 pub items: Vec<TraitItem>,
154 }),
155 /// Default trait implementation.
156 ///
157 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
158 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800159 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800161 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700162 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800163 pub for_token: Token![for],
164 pub dot2_token: Token![..],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700165 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
167 /// An implementation.
168 ///
169 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
170 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700172 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800174 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700176 /// Trait this impl implements.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800177 pub trait_: Option<(ImplPolarity, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700178 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800179 pub self_ty: Box<Type>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700180 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 pub items: Vec<ImplItem>,
182 }),
183 /// A macro invocation (which includes macro definition).
184 ///
185 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800186 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800187 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800188 /// The `example` in `macro_rules! example { ... }`.
189 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800190 pub mac: Macro,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800191 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 }
David Tolnayb79ee962016-09-04 09:39:20 -0700193}
194
David Tolnay0e837402016-12-22 17:25:55 -0500195impl From<DeriveInput> for Item {
196 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800197 match input.body {
198 Body::Enum(data) => {
199 Item::Enum(ItemEnum {
200 attrs: input.attrs,
201 vis: input.vis,
202 enum_token: data.enum_token,
203 ident: input.ident,
204 generics: input.generics,
205 brace_token: data.brace_token,
206 variants: data.variants,
207 })
208 }
209 Body::Struct(data) => {
210 Item::Struct(ItemStruct {
211 attrs: input.attrs,
212 vis: input.vis,
213 struct_token: data.struct_token,
214 ident: input.ident,
215 generics: input.generics,
216 data: data.data,
217 semi_token: data.semi_token,
218 })
219 }
David Tolnay453cfd12016-10-23 11:00:14 -0700220 }
221 }
222}
223
Alex Crichton62a0a592017-05-22 13:58:53 -0700224ast_enum_of_structs! {
225 pub enum ViewPath {
226 /// `foo::bar::baz as quux`
227 ///
228 /// or just
229 ///
230 /// `foo::bar::baz` (with `as baz` implicitly on the right)
231 pub Simple(PathSimple {
232 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800233 pub as_token: Option<Token![as]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub rename: Option<Ident>,
235 }),
236
237 /// `foo::bar::*`
238 pub Glob(PathGlob {
239 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800240 pub colon2_token: Option<Token![::]>,
241 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 }),
243
244 /// `foo::bar::{a, b, c}`
245 pub List(PathList {
246 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800247 pub colon2_token: Token![::],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700248 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800249 pub items: Delimited<PathListItem, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }),
251 }
252}
253
254ast_struct! {
255 pub struct PathListItem {
256 pub name: Ident,
257 /// renamed in list, e.g. `use foo::{bar as baz};`
258 pub rename: Option<Ident>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800259 pub as_token: Option<Token![as]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700260 }
261}
262
263ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700264 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800266 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 NotConst,
268 }
269}
270
271ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700272 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 pub enum Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800274 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 Final,
276 }
277}
278
Alex Crichton62a0a592017-05-22 13:58:53 -0700279ast_enum_of_structs! {
280 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800281 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 /// A foreign function
283 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800284 pub attrs: Vec<Attribute>,
285 pub vis: Visibility,
286 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700287 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 }),
290 /// A foreign static item (`static ext: u8`)
291 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800292 pub attrs: Vec<Attribute>,
293 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800294 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700295 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800296 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800297 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800298 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800299 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800301 /// A foreign type
302 pub Type(ForeignItemType {
303 pub attrs: Vec<Attribute>,
304 pub vis: Visibility,
305 pub type_token: Token![type],
306 pub ident: Ident,
307 pub semi_token: Token![;],
308 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700310}
311
David Tolnayda705bd2017-11-10 21:58:05 -0800312ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 /// Represents an item declaration within a trait declaration,
314 /// possibly including a default implementation. A trait item is
315 /// either required (meaning it doesn't have an implementation, just a
316 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800317 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800319 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800320 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700321 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800322 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800323 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800324 pub default: Option<(Token![=], Expr)>,
325 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700326 }),
327 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800328 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700329 pub sig: MethodSig,
330 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800331 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 }),
333 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800334 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800335 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700336 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500337 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800338 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800339 pub bounds: Delimited<TypeParamBound, Token![+]>,
340 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800341 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800343 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800344 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800345 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800346 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700348}
349
350ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700351 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 pub enum ImplPolarity {
353 /// `impl Trait for Type`
354 Positive,
355 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800356 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 }
358}
359
Alex Crichton62a0a592017-05-22 13:58:53 -0700360ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800361 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800363 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700364 pub vis: Visibility,
365 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700367 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800369 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800372 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
374 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800375 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700376 pub vis: Visibility,
377 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700378 pub sig: MethodSig,
379 pub block: Block,
380 }),
381 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800382 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700383 pub vis: Visibility,
384 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700386 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500387 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800389 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800390 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 }),
David Tolnay857628c2017-11-11 12:25:31 -0800392 pub Macro(ImplItemMacro {
393 pub attrs: Vec<Attribute>,
394 pub mac: Macro,
395 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700397}
398
399ast_struct! {
400 /// Represents a method's signature in a trait declaration,
401 /// or in an implementation.
402 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700404 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700406 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700407 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700408 }
409}
410
411ast_struct! {
412 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700413 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 /// E.g. `fn foo(bar: baz)`
415 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800416 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700417 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800419 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700420 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800422 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700424}
425
Alex Crichton62a0a592017-05-22 13:58:53 -0700426ast_enum_of_structs! {
427 /// An argument in a function header.
428 ///
429 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
430 pub enum FnArg {
431 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub and_token: Token![&],
433 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 pub lifetime: Option<Lifetime>,
435 pub mutbl: Mutability,
436 }),
437 pub SelfValue(ArgSelf {
438 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800439 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700440 }),
441 pub Captured(ArgCaptured {
442 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800444 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800446 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700447 }
David Tolnay62f374c2016-10-02 13:37:00 -0700448}
449
David Tolnayedf2b992016-09-23 20:43:45 -0700450#[cfg(feature = "parsing")]
451pub mod parsing {
452 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700453
Michael Layzell92639a52017-06-01 00:07:44 -0400454 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700455
David Tolnay4c614be2017-11-10 00:02:38 -0800456 impl_synom!(Item "item" alt!(
457 syn!(ItemExternCrate) => { Item::ExternCrate }
458 |
459 syn!(ItemUse) => { Item::Use }
460 |
461 syn!(ItemStatic) => { Item::Static }
462 |
463 syn!(ItemConst) => { Item::Const }
464 |
465 syn!(ItemFn) => { Item::Fn }
466 |
467 syn!(ItemMod) => { Item::Mod }
468 |
469 syn!(ItemForeignMod) => { Item::ForeignMod }
470 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800472 |
473 syn!(ItemStruct) => { Item::Struct }
474 |
475 syn!(ItemEnum) => { Item::Enum }
476 |
477 syn!(ItemUnion) => { Item::Union }
478 |
479 syn!(ItemTrait) => { Item::Trait }
480 |
481 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
482 |
483 syn!(ItemImpl) => { Item::Impl }
484 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800485 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800486 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700487
David Tolnaydecf28d2017-11-11 11:56:45 -0800488 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700489 attrs: many0!(call!(Attribute::parse_outer)) >>
490 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800491 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700492 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700493 body: call!(::TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800494 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800495 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700496 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800497 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800498 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500499 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700500 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800502 },
David Tolnay4c614be2017-11-10 00:02:38 -0800503 })
David Tolnayedf2b992016-09-23 20:43:45 -0700504 ));
505
David Tolnay4c614be2017-11-10 00:02:38 -0800506 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700507 attrs: many0!(call!(Attribute::parse_outer)) >>
508 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 extern_: keyword!(extern) >>
510 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700511 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800512 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
513 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800514 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700515 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800516 vis: vis,
517 extern_token: extern_,
518 crate_token: crate_,
519 ident: ident,
520 rename: rename,
521 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800522 })
David Tolnayedf2b992016-09-23 20:43:45 -0700523 ));
524
David Tolnay4c614be2017-11-10 00:02:38 -0800525 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700526 attrs: many0!(call!(Attribute::parse_outer)) >>
527 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700529 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800530 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800531 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700532 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800533 vis: vis,
534 use_token: use_,
535 path: Box::new(what),
536 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800537 })
David Tolnay4a057422016-10-08 00:02:31 -0700538 ));
539
Alex Crichton954046c2017-05-30 21:49:42 -0700540 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400541 named!(parse -> Self, alt!(
542 syn!(PathGlob) => { ViewPath::Glob }
543 |
544 syn!(PathList) => { ViewPath::List }
545 |
546 syn!(PathSimple) => { ViewPath::Simple } // must be last
547 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700548 }
David Tolnay4a057422016-10-08 00:02:31 -0700549
Alex Crichton954046c2017-05-30 21:49:42 -0700550 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400551 named!(parse -> Self, do_parse!(
552 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800553 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400554 (PathSimple {
555 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800556 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400557 rename: rename.map(|p| p.1),
558 })
559 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700560 }
David Tolnay4a057422016-10-08 00:02:31 -0700561
Alex Crichton954046c2017-05-30 21:49:42 -0700562 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400563 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700564 path: option!(do_parse!(
565 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800566 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700567 (path, colon2)
568 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800569 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700570 ({
571 match path {
572 Some((path, colon2)) => {
573 PathGlob {
574 path: path,
575 colon2_token: Some(colon2),
576 star_token: star,
577 }
578 }
579 None => {
580 PathGlob {
581 path: Path {
582 leading_colon: None,
583 segments: Default::default(),
584 },
585 colon2_token: None,
586 star_token: star,
587 }
588 }
589 }
Michael Layzell92639a52017-06-01 00:07:44 -0400590 })
591 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700592 }
David Tolnay4a057422016-10-08 00:02:31 -0700593
Alex Crichton954046c2017-05-30 21:49:42 -0700594 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400595 named!(parse -> Self, alt!(
596 do_parse!(
597 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800598 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400599 items: braces!(call!(Delimited::parse_terminated)) >>
600 (PathList {
601 path: path,
602 items: items.0,
603 brace_token: items.1,
604 colon2_token: colon2,
605 })
606 )
607 |
608 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800609 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400610 items: braces!(call!(Delimited::parse_terminated)) >>
611 (PathList {
612 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400613 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700614 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400615 },
David Tolnay570695e2017-06-03 16:15:13 -0700616 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400617 brace_token: items.1,
618 items: items.0,
619 })
620 )
621 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700622 }
David Tolnay4a057422016-10-08 00:02:31 -0700623
Alex Crichton954046c2017-05-30 21:49:42 -0700624 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400625 named!(parse -> Self, do_parse!(
626 name: alt!(
627 syn!(Ident)
628 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800629 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400630 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800631 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400632 (PathListItem {
633 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800634 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400635 rename: rename.map(|p| p.1),
636 })
637 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
David Tolnay4a057422016-10-08 00:02:31 -0700639
David Tolnay4c614be2017-11-10 00:02:38 -0800640 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700641 attrs: many0!(call!(Attribute::parse_outer)) >>
642 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800643 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700644 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700645 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800646 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800647 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800648 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700649 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800650 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800651 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700652 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800653 vis: vis,
654 static_token: static_,
655 mutbl: mutability,
656 ident: ident,
657 colon_token: colon,
658 ty: Box::new(ty),
659 eq_token: eq,
660 expr: Box::new(value),
661 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800662 })
David Tolnay47a877c2016-10-01 16:50:55 -0700663 ));
664
David Tolnay4c614be2017-11-10 00:02:38 -0800665 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700666 attrs: many0!(call!(Attribute::parse_outer)) >>
667 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800668 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700669 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800670 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800671 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800672 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700673 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800674 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800675 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700676 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800677 vis: vis,
678 const_token: const_,
679 ident: ident,
680 colon_token: colon,
681 ty: Box::new(ty),
682 eq_token: eq,
683 expr: Box::new(value),
684 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800685 })
David Tolnay47a877c2016-10-01 16:50:55 -0700686 ));
687
David Tolnay4c614be2017-11-10 00:02:38 -0800688 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700689 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
690 vis: syn!(Visibility) >>
691 constness: syn!(Constness) >>
692 unsafety: syn!(Unsafety) >>
693 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800694 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700695 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700696 generics: syn!(Generics) >>
697 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800698 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700699 where_clause: syn!(WhereClause) >>
700 inner_attrs_stmts: braces!(tuple!(
701 many0!(call!(Attribute::parse_inner)),
702 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400703 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800704 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700705 attrs: {
706 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700707 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700708 attrs
709 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800710 vis: vis,
711 constness: constness,
712 unsafety: unsafety,
713 abi: abi,
714 decl: Box::new(FnDecl {
715 dot_tokens: None,
716 fn_token: fn_,
717 paren_token: inputs.1,
718 inputs: inputs.0,
719 output: ret,
720 variadic: false,
721 generics: Generics {
722 where_clause: where_clause,
723 .. generics
724 },
725 }),
726 ident: ident,
727 block: Box::new(Block {
728 brace_token: inner_attrs_stmts.1,
729 stmts: (inner_attrs_stmts.0).1,
730 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800731 })
David Tolnay42602292016-10-01 22:25:45 -0700732 ));
733
Alex Crichton954046c2017-05-30 21:49:42 -0700734 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400735 named!(parse -> Self, alt!(
736 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800737 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400738 lt: option!(syn!(Lifetime)) >>
739 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 self_: keyword!(self) >>
741 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400742 (ArgSelfRef {
743 lifetime: lt,
744 mutbl: mutability,
745 and_token: and,
746 self_token: self_,
747 }.into())
748 )
749 |
750 do_parse!(
751 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800752 self_: keyword!(self) >>
753 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400754 (ArgSelf {
755 mutbl: mutability,
756 self_token: self_,
757 }.into())
758 )
759 |
760 do_parse!(
761 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800762 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800763 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400764 (ArgCaptured {
765 pat: pat,
766 ty: ty,
767 colon_token: colon,
768 }.into())
769 )
770 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800771 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400772 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700773 }
David Tolnay62f374c2016-10-02 13:37:00 -0700774
David Tolnay4c614be2017-11-10 00:02:38 -0800775 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700776 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
777 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700779 ident: syn!(Ident) >>
780 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800781 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700782 Vec::new(),
783 None,
784 Some(semi),
785 )}
David Tolnay37d10332016-10-13 20:51:04 -0700786 |
Alex Crichton954046c2017-05-30 21:49:42 -0700787 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700788 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700789 many0!(call!(Attribute::parse_inner)),
790 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400791 )
David Tolnay570695e2017-06-03 16:15:13 -0700792 ) => {|((inner_attrs, items), brace)| (
793 inner_attrs,
794 Some((brace, items)),
795 None,
796 )}
David Tolnay37d10332016-10-13 20:51:04 -0700797 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800798 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700799 attrs: {
800 let mut attrs = outer_attrs;
801 attrs.extend(content_semi.0);
802 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700803 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800804 vis: vis,
805 mod_token: mod_,
806 ident: ident,
807 content: content_semi.1,
808 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800809 })
David Tolnay35902302016-10-06 01:11:08 -0700810 ));
811
David Tolnay4c614be2017-11-10 00:02:38 -0800812 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700813 attrs: many0!(call!(Attribute::parse_outer)) >>
814 abi: syn!(Abi) >>
815 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800816 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700817 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800818 abi: abi,
819 brace_token: items.1,
820 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800821 })
David Tolnay35902302016-10-06 01:11:08 -0700822 ));
823
David Tolnay8894f602017-11-11 12:11:04 -0800824 impl_synom!(ForeignItem "foreign item" alt!(
825 syn!(ForeignItemFn) => { ForeignItem::Fn }
826 |
827 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800828 |
829 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800830 ));
David Tolnay35902302016-10-06 01:11:08 -0700831
David Tolnay8894f602017-11-11 12:11:04 -0800832 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700833 attrs: many0!(call!(Attribute::parse_outer)) >>
834 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800835 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700836 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700837 generics: syn!(Generics) >>
838 inputs: parens!(do_parse!(
839 args: call!(Delimited::parse_terminated) >>
840 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800841 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700842 (args, variadic)
843 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800844 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700845 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800846 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700847 ({
848 let ((inputs, variadic), parens) = inputs;
849 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800850 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700851 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700852 attrs: attrs,
853 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800854 decl: Box::new(FnDecl {
855 fn_token: fn_,
856 paren_token: parens,
857 inputs: inputs,
858 variadic: variadic.is_some(),
859 dot_tokens: variadic,
860 output: ret,
861 generics: Generics {
862 where_clause: where_clause,
863 .. generics
864 },
865 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700866 vis: vis,
867 }
David Tolnay35902302016-10-06 01:11:08 -0700868 })
869 ));
870
David Tolnay8894f602017-11-11 12:11:04 -0800871 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700872 attrs: many0!(call!(Attribute::parse_outer)) >>
873 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700875 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700876 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800877 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800878 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800879 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800880 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700881 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700882 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700883 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800884 ty: Box::new(ty),
885 mutbl: mutability,
886 static_token: static_,
887 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700888 vis: vis,
889 })
890 ));
891
David Tolnay199bcbb2017-11-12 10:33:52 -0800892 impl_synom!(ForeignItemType "foreign type" do_parse!(
893 attrs: many0!(call!(Attribute::parse_outer)) >>
894 vis: syn!(Visibility) >>
895 type_: keyword!(type) >>
896 ident: syn!(Ident) >>
897 semi: punct!(;) >>
898 (ForeignItemType {
899 attrs: attrs,
900 vis: vis,
901 type_token: type_,
902 ident: ident,
903 semi_token: semi,
904 })
905 ));
906
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800907 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700908 attrs: many0!(call!(Attribute::parse_outer)) >>
909 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800910 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700911 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700912 generics: syn!(Generics) >>
913 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800915 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800916 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800917 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700918 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800919 vis: vis,
920 type_token: type_,
921 ident: ident,
922 generics: Generics {
923 where_clause: where_clause,
924 ..generics
925 },
926 eq_token: eq,
927 ty: Box::new(ty),
928 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800929 })
David Tolnay3cf52982016-10-01 17:11:37 -0700930 ));
931
David Tolnay4c614be2017-11-10 00:02:38 -0800932 impl_synom!(ItemStruct "struct item" switch!(
933 map!(syn!(DeriveInput), Into::into),
934 Item::Struct(item) => value!(item)
935 |
936 _ => reject!()
937 ));
David Tolnay42602292016-10-01 22:25:45 -0700938
David Tolnay4c614be2017-11-10 00:02:38 -0800939 impl_synom!(ItemEnum "enum item" switch!(
940 map!(syn!(DeriveInput), Into::into),
941 Item::Enum(item) => value!(item)
942 |
943 _ => reject!()
944 ));
945
946 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700947 attrs: many0!(call!(Attribute::parse_outer)) >>
948 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700950 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700951 generics: syn!(Generics) >>
952 where_clause: syn!(WhereClause) >>
953 fields: braces!(call!(Delimited::parse_terminated_with,
954 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800955 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700956 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800957 vis: vis,
958 union_token: union_,
959 ident: ident,
960 generics: Generics {
961 where_clause: where_clause,
962 .. generics
963 },
964 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800965 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700966 ));
967
David Tolnay4c614be2017-11-10 00:02:38 -0800968 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700969 attrs: many0!(call!(Attribute::parse_outer)) >>
970 vis: syn!(Visibility) >>
971 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500972 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800973 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700974 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700975 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800976 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700977 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700978 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700979 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700980 where_clause: syn!(WhereClause) >>
981 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800982 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700983 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800984 vis: vis,
985 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500986 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800987 trait_token: trait_,
988 ident: ident,
989 generics: Generics {
990 where_clause: where_clause,
991 .. generics
992 },
993 colon_token: colon,
994 supertraits: bounds.unwrap_or_default(),
995 brace_token: body.1,
996 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800997 })
David Tolnay0aecb732016-10-03 23:03:50 -0700998 ));
999
David Tolnay4c614be2017-11-10 00:02:38 -08001000 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001001 attrs: many0!(call!(Attribute::parse_outer)) >>
1002 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001004 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 for_: keyword!(for) >>
1006 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001007 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001008 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001009 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001010 unsafety: unsafety,
1011 impl_token: impl_,
1012 path: path,
1013 for_token: for_,
1014 dot2_token: dot2,
1015 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001016 })
David Tolnayf94e2362016-10-04 00:29:51 -07001017 ));
1018
David Tolnayda705bd2017-11-10 21:58:05 -08001019 impl_synom!(TraitItem "trait item" alt!(
1020 syn!(TraitItemConst) => { TraitItem::Const }
1021 |
1022 syn!(TraitItemMethod) => { TraitItem::Method }
1023 |
1024 syn!(TraitItemType) => { TraitItem::Type }
1025 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001026 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001027 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001028
David Tolnayda705bd2017-11-10 21:58:05 -08001029 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001030 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001032 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001034 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001035 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1036 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001037 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001038 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001039 const_token: const_,
1040 ident: ident,
1041 colon_token: colon,
1042 ty: ty,
1043 default: default,
1044 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001045 })
1046 ));
1047
David Tolnayda705bd2017-11-10 21:58:05 -08001048 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001049 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1050 constness: syn!(Constness) >>
1051 unsafety: syn!(Unsafety) >>
1052 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001053 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001054 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001055 generics: syn!(Generics) >>
1056 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001057 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001058 where_clause: syn!(WhereClause) >>
1059 body: option!(braces!(
1060 tuple!(many0!(call!(Attribute::parse_inner)),
1061 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001062 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001063 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001064 ({
1065 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001066 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001067 None => (Vec::new(), None),
1068 };
David Tolnayda705bd2017-11-10 21:58:05 -08001069 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001070 attrs: {
1071 let mut attrs = outer_attrs;
1072 attrs.extend(inner_attrs);
1073 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001074 },
David Tolnayda705bd2017-11-10 21:58:05 -08001075 sig: MethodSig {
1076 constness: constness,
1077 unsafety: unsafety,
1078 abi: abi,
1079 ident: ident,
1080 decl: FnDecl {
1081 inputs: inputs.0,
1082 output: ret,
1083 variadic: false,
1084 fn_token: fn_,
1085 paren_token: inputs.1,
1086 dot_tokens: None,
1087 generics: Generics {
1088 where_clause: where_clause,
1089 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001090 },
1091 },
David Tolnayda705bd2017-11-10 21:58:05 -08001092 },
1093 default: stmts.map(|stmts| {
1094 Block {
1095 stmts: stmts.0,
1096 brace_token: stmts.1,
1097 }
1098 }),
1099 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001100 }
David Tolnay0aecb732016-10-03 23:03:50 -07001101 })
1102 ));
1103
David Tolnayda705bd2017-11-10 21:58:05 -08001104 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001105 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001106 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001107 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001108 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001110 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001111 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001112 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001113 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001114 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001115 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001116 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001117 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001118 type_token: type_,
1119 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001120 generics: Generics {
1121 where_clause: where_clause,
1122 .. generics
1123 },
David Tolnayda705bd2017-11-10 21:58:05 -08001124 colon_token: colon,
1125 bounds: bounds.unwrap_or_default(),
1126 default: default,
1127 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001128 })
1129 ));
1130
David Tolnaydecf28d2017-11-11 11:56:45 -08001131 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001132 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001133 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001134 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001135 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001136 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001137 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001138 })
1139 ));
1140
David Tolnay4c614be2017-11-10 00:02:38 -08001141 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001142 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001143 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001144 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001145 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001146 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001147 polarity_path: alt!(
1148 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001149 polarity: syn!(ImplPolarity) >>
1150 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001151 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001152 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001153 )
1154 |
David Tolnay570695e2017-06-03 16:15:13 -07001155 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001156 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001157 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001158 where_clause: syn!(WhereClause) >>
1159 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001160 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001161 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001162 defaultness: defaultness,
1163 unsafety: unsafety,
1164 impl_token: impl_,
1165 generics: Generics {
1166 where_clause: where_clause,
1167 .. generics
1168 },
1169 trait_: polarity_path,
1170 self_ty: Box::new(self_ty),
1171 brace_token: body.1,
1172 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001173 })
David Tolnay4c9be372016-10-06 00:47:37 -07001174 ));
1175
David Tolnay857628c2017-11-11 12:25:31 -08001176 impl_synom!(ImplItem "item in impl block" alt!(
1177 syn!(ImplItemConst) => { ImplItem::Const }
1178 |
1179 syn!(ImplItemMethod) => { ImplItem::Method }
1180 |
1181 syn!(ImplItemType) => { ImplItem::Type }
1182 |
1183 syn!(ImplItemMacro) => { ImplItem::Macro }
1184 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001185
David Tolnay857628c2017-11-11 12:25:31 -08001186 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001187 attrs: many0!(call!(Attribute::parse_outer)) >>
1188 vis: syn!(Visibility) >>
1189 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001190 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001191 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001192 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001193 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001194 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001195 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001196 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001197 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001198 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001199 vis: vis,
1200 defaultness: defaultness,
1201 const_token: const_,
1202 ident: ident,
1203 colon_token: colon,
1204 ty: ty,
1205 eq_token: eq,
1206 expr: value,
1207 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001208 })
1209 ));
1210
David Tolnay857628c2017-11-11 12:25:31 -08001211 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001212 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1213 vis: syn!(Visibility) >>
1214 defaultness: syn!(Defaultness) >>
1215 constness: syn!(Constness) >>
1216 unsafety: syn!(Unsafety) >>
1217 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001218 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001219 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001220 generics: syn!(Generics) >>
1221 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001222 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001223 where_clause: syn!(WhereClause) >>
1224 inner_attrs_stmts: braces!(tuple!(
1225 many0!(call!(Attribute::parse_inner)),
1226 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001227 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001228 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001229 attrs: {
1230 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001231 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001232 attrs
1233 },
David Tolnay857628c2017-11-11 12:25:31 -08001234 vis: vis,
1235 defaultness: defaultness,
1236 sig: MethodSig {
1237 constness: constness,
1238 unsafety: unsafety,
1239 abi: abi,
1240 ident: ident,
1241 decl: FnDecl {
1242 fn_token: fn_,
1243 paren_token: inputs.1,
1244 inputs: inputs.0,
1245 output: ret,
1246 variadic: false,
1247 generics: Generics {
1248 where_clause: where_clause,
1249 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001250 },
David Tolnay857628c2017-11-11 12:25:31 -08001251 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001252 },
David Tolnay857628c2017-11-11 12:25:31 -08001253 },
1254 block: Block {
1255 brace_token: inner_attrs_stmts.1,
1256 stmts: (inner_attrs_stmts.0).1,
1257 },
David Tolnay4c9be372016-10-06 00:47:37 -07001258 })
1259 ));
1260
David Tolnay857628c2017-11-11 12:25:31 -08001261 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001262 attrs: many0!(call!(Attribute::parse_outer)) >>
1263 vis: syn!(Visibility) >>
1264 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001265 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001266 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001267 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001268 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001269 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001270 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001271 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001272 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001273 vis: vis,
1274 defaultness: defaultness,
1275 type_token: type_,
1276 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001277 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001278 eq_token: eq,
1279 ty: ty,
1280 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001281 })
1282 ));
1283
David Tolnay857628c2017-11-11 12:25:31 -08001284 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001285 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001286 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001287 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001288 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001289 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001290 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001291 })
1292 ));
1293
Alex Crichton954046c2017-05-30 21:49:42 -07001294 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001295 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001297 |
1298 epsilon!() => { |_| ImplPolarity::Positive }
1299 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001300 }
David Tolnay4c9be372016-10-06 00:47:37 -07001301
Alex Crichton954046c2017-05-30 21:49:42 -07001302 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001303 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001304 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001305 |
1306 epsilon!() => { |_| Constness::NotConst }
1307 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001308 }
David Tolnay42602292016-10-01 22:25:45 -07001309
Alex Crichton954046c2017-05-30 21:49:42 -07001310 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001311 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001312 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001313 |
1314 epsilon!() => { |_| Defaultness::Final }
1315 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001316 }
David Tolnayedf2b992016-09-23 20:43:45 -07001317}
David Tolnay4a51dc72016-10-01 00:40:31 -07001318
1319#[cfg(feature = "printing")]
1320mod printing {
1321 use super::*;
1322 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001323 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001324 use quote::{Tokens, ToTokens};
1325
David Tolnay1bfa7332017-11-11 12:41:20 -08001326 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001327 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001328 tokens.append_all(self.attrs.outer());
1329 self.vis.to_tokens(tokens);
1330 self.extern_token.to_tokens(tokens);
1331 self.crate_token.to_tokens(tokens);
1332 self.ident.to_tokens(tokens);
1333 if let Some((ref as_token, ref rename)) = self.rename {
1334 as_token.to_tokens(tokens);
1335 rename.to_tokens(tokens);
1336 }
1337 self.semi_token.to_tokens(tokens);
1338 }
1339 }
1340
1341 impl ToTokens for ItemUse {
1342 fn to_tokens(&self, tokens: &mut Tokens) {
1343 tokens.append_all(self.attrs.outer());
1344 self.vis.to_tokens(tokens);
1345 self.use_token.to_tokens(tokens);
1346 self.path.to_tokens(tokens);
1347 self.semi_token.to_tokens(tokens);
1348 }
1349 }
1350
1351 impl ToTokens for ItemStatic {
1352 fn to_tokens(&self, tokens: &mut Tokens) {
1353 tokens.append_all(self.attrs.outer());
1354 self.vis.to_tokens(tokens);
1355 self.static_token.to_tokens(tokens);
1356 self.mutbl.to_tokens(tokens);
1357 self.ident.to_tokens(tokens);
1358 self.colon_token.to_tokens(tokens);
1359 self.ty.to_tokens(tokens);
1360 self.eq_token.to_tokens(tokens);
1361 self.expr.to_tokens(tokens);
1362 self.semi_token.to_tokens(tokens);
1363 }
1364 }
1365
1366 impl ToTokens for ItemConst {
1367 fn to_tokens(&self, tokens: &mut Tokens) {
1368 tokens.append_all(self.attrs.outer());
1369 self.vis.to_tokens(tokens);
1370 self.const_token.to_tokens(tokens);
1371 self.ident.to_tokens(tokens);
1372 self.colon_token.to_tokens(tokens);
1373 self.ty.to_tokens(tokens);
1374 self.eq_token.to_tokens(tokens);
1375 self.expr.to_tokens(tokens);
1376 self.semi_token.to_tokens(tokens);
1377 }
1378 }
1379
1380 impl ToTokens for ItemFn {
1381 fn to_tokens(&self, tokens: &mut Tokens) {
1382 tokens.append_all(self.attrs.outer());
1383 self.vis.to_tokens(tokens);
1384 self.constness.to_tokens(tokens);
1385 self.unsafety.to_tokens(tokens);
1386 self.abi.to_tokens(tokens);
1387 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1388 self.block.brace_token.surround(tokens, |tokens| {
1389 tokens.append_all(self.attrs.inner());
1390 tokens.append_all(&self.block.stmts);
1391 });
1392 }
1393 }
1394
1395 impl ToTokens for ItemMod {
1396 fn to_tokens(&self, tokens: &mut Tokens) {
1397 tokens.append_all(self.attrs.outer());
1398 self.vis.to_tokens(tokens);
1399 self.mod_token.to_tokens(tokens);
1400 self.ident.to_tokens(tokens);
1401 if let Some((ref brace, ref items)) = self.content {
1402 brace.surround(tokens, |tokens| {
1403 tokens.append_all(self.attrs.inner());
1404 tokens.append_all(items);
1405 });
1406 } else {
1407 TokensOrDefault(&self.semi).to_tokens(tokens);
1408 }
1409 }
1410 }
1411
1412 impl ToTokens for ItemForeignMod {
1413 fn to_tokens(&self, tokens: &mut Tokens) {
1414 tokens.append_all(self.attrs.outer());
1415 self.abi.to_tokens(tokens);
1416 self.brace_token.surround(tokens, |tokens| {
1417 tokens.append_all(&self.items);
1418 });
1419 }
1420 }
1421
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001422 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001423 fn to_tokens(&self, tokens: &mut Tokens) {
1424 tokens.append_all(self.attrs.outer());
1425 self.vis.to_tokens(tokens);
1426 self.type_token.to_tokens(tokens);
1427 self.ident.to_tokens(tokens);
1428 self.generics.to_tokens(tokens);
1429 self.generics.where_clause.to_tokens(tokens);
1430 self.eq_token.to_tokens(tokens);
1431 self.ty.to_tokens(tokens);
1432 self.semi_token.to_tokens(tokens);
1433 }
1434 }
1435
1436 impl ToTokens for ItemEnum {
1437 fn to_tokens(&self, tokens: &mut Tokens) {
1438 tokens.append_all(self.attrs.outer());
1439 self.vis.to_tokens(tokens);
1440 self.enum_token.to_tokens(tokens);
1441 self.ident.to_tokens(tokens);
1442 self.generics.to_tokens(tokens);
1443 self.generics.where_clause.to_tokens(tokens);
1444 self.brace_token.surround(tokens, |tokens| {
1445 self.variants.to_tokens(tokens);
1446 });
1447 }
1448 }
1449
1450 impl ToTokens for ItemStruct {
1451 fn to_tokens(&self, tokens: &mut Tokens) {
1452 tokens.append_all(self.attrs.outer());
1453 self.vis.to_tokens(tokens);
1454 self.struct_token.to_tokens(tokens);
1455 self.ident.to_tokens(tokens);
1456 self.generics.to_tokens(tokens);
1457 match self.data {
1458 VariantData::Struct(..) => {
1459 self.generics.where_clause.to_tokens(tokens);
1460 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001461 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001462 VariantData::Tuple(..) => {
1463 self.data.to_tokens(tokens);
1464 self.generics.where_clause.to_tokens(tokens);
1465 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001466 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001467 VariantData::Unit => {
1468 self.generics.where_clause.to_tokens(tokens);
1469 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001470 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001471 }
1472 }
1473 }
1474
1475 impl ToTokens for ItemUnion {
1476 fn to_tokens(&self, tokens: &mut Tokens) {
1477 tokens.append_all(self.attrs.outer());
1478 self.vis.to_tokens(tokens);
1479 self.union_token.to_tokens(tokens);
1480 self.ident.to_tokens(tokens);
1481 self.generics.to_tokens(tokens);
1482 self.generics.where_clause.to_tokens(tokens);
1483 // XXX: Should we handle / complain when using a
1484 // non-VariantData::Struct Variant in Union?
1485 self.data.to_tokens(tokens);
1486 }
1487 }
1488
1489 impl ToTokens for ItemTrait {
1490 fn to_tokens(&self, tokens: &mut Tokens) {
1491 tokens.append_all(self.attrs.outer());
1492 self.vis.to_tokens(tokens);
1493 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001494 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001495 self.trait_token.to_tokens(tokens);
1496 self.ident.to_tokens(tokens);
1497 self.generics.to_tokens(tokens);
1498 if !self.supertraits.is_empty() {
1499 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1500 self.supertraits.to_tokens(tokens);
1501 }
1502 self.generics.where_clause.to_tokens(tokens);
1503 self.brace_token.surround(tokens, |tokens| {
1504 tokens.append_all(&self.items);
1505 });
1506 }
1507 }
1508
1509 impl ToTokens for ItemDefaultImpl {
1510 fn to_tokens(&self, tokens: &mut Tokens) {
1511 tokens.append_all(self.attrs.outer());
1512 self.unsafety.to_tokens(tokens);
1513 self.impl_token.to_tokens(tokens);
1514 self.path.to_tokens(tokens);
1515 self.for_token.to_tokens(tokens);
1516 self.dot2_token.to_tokens(tokens);
1517 self.brace_token.surround(tokens, |_tokens| {});
1518 }
1519 }
1520
1521 impl ToTokens for ItemImpl {
1522 fn to_tokens(&self, tokens: &mut Tokens) {
1523 tokens.append_all(self.attrs.outer());
1524 self.defaultness.to_tokens(tokens);
1525 self.unsafety.to_tokens(tokens);
1526 self.impl_token.to_tokens(tokens);
1527 self.generics.to_tokens(tokens);
1528 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1529 polarity.to_tokens(tokens);
1530 path.to_tokens(tokens);
1531 for_token.to_tokens(tokens);
1532 }
1533 self.self_ty.to_tokens(tokens);
1534 self.generics.where_clause.to_tokens(tokens);
1535 self.brace_token.surround(tokens, |tokens| {
1536 tokens.append_all(&self.items);
1537 });
1538 }
1539 }
1540
1541 impl ToTokens for ItemMacro {
1542 fn to_tokens(&self, tokens: &mut Tokens) {
1543 tokens.append_all(self.attrs.outer());
1544 self.mac.path.to_tokens(tokens);
1545 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001546 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001547 tokens.append_all(&self.mac.tokens);
1548 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001549 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001550 }
1551 }
1552 }
David Tolnay42602292016-10-01 22:25:45 -07001553
Alex Crichton62a0a592017-05-22 13:58:53 -07001554 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001555 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001556 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001557 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001558 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001559 self.rename.to_tokens(tokens);
1560 }
David Tolnay4a057422016-10-08 00:02:31 -07001561 }
1562 }
1563
Alex Crichton62a0a592017-05-22 13:58:53 -07001564 impl ToTokens for PathGlob {
1565 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001566 if self.path.segments.len() > 0 {
1567 self.path.to_tokens(tokens);
1568 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1569 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001570 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001571 }
1572 }
1573
1574 impl ToTokens for PathList {
1575 fn to_tokens(&self, tokens: &mut Tokens) {
1576 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001577 self.colon2_token.to_tokens(tokens);
1578 self.brace_token.surround(tokens, |tokens| {
1579 self.items.to_tokens(tokens);
1580 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001581 }
1582 }
1583
David Tolnay4a057422016-10-08 00:02:31 -07001584 impl ToTokens for PathListItem {
1585 fn to_tokens(&self, tokens: &mut Tokens) {
1586 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001587 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001588 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001589 self.rename.to_tokens(tokens);
1590 }
David Tolnay4a057422016-10-08 00:02:31 -07001591 }
1592 }
1593
David Tolnay1bfa7332017-11-11 12:41:20 -08001594 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001595 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001596 tokens.append_all(self.attrs.outer());
1597 self.const_token.to_tokens(tokens);
1598 self.ident.to_tokens(tokens);
1599 self.colon_token.to_tokens(tokens);
1600 self.ty.to_tokens(tokens);
1601 if let Some((ref eq_token, ref default)) = self.default {
1602 eq_token.to_tokens(tokens);
1603 default.to_tokens(tokens);
1604 }
1605 self.semi_token.to_tokens(tokens);
1606 }
1607 }
1608
1609 impl ToTokens for TraitItemMethod {
1610 fn to_tokens(&self, tokens: &mut Tokens) {
1611 tokens.append_all(self.attrs.outer());
1612 self.sig.to_tokens(tokens);
1613 match self.default {
1614 Some(ref block) => {
1615 block.brace_token.surround(tokens, |tokens| {
1616 tokens.append_all(self.attrs.inner());
1617 tokens.append_all(&block.stmts);
1618 });
David Tolnayca085422016-10-04 00:12:38 -07001619 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001620 None => {
1621 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001622 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001623 }
1624 }
1625 }
1626
1627 impl ToTokens for TraitItemType {
1628 fn to_tokens(&self, tokens: &mut Tokens) {
1629 tokens.append_all(self.attrs.outer());
1630 self.type_token.to_tokens(tokens);
1631 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001632 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001633 if !self.bounds.is_empty() {
1634 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1635 self.bounds.to_tokens(tokens);
1636 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001637 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001638 if let Some((ref eq_token, ref default)) = self.default {
1639 eq_token.to_tokens(tokens);
1640 default.to_tokens(tokens);
1641 }
1642 self.semi_token.to_tokens(tokens);
1643 }
1644 }
1645
1646 impl ToTokens for TraitItemMacro {
1647 fn to_tokens(&self, tokens: &mut Tokens) {
1648 tokens.append_all(self.attrs.outer());
1649 self.mac.to_tokens(tokens);
1650 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001651 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001652 }
1653 }
1654 }
1655
David Tolnay857628c2017-11-11 12:25:31 -08001656 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001657 fn to_tokens(&self, tokens: &mut Tokens) {
1658 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001659 self.vis.to_tokens(tokens);
1660 self.defaultness.to_tokens(tokens);
1661 self.const_token.to_tokens(tokens);
1662 self.ident.to_tokens(tokens);
1663 self.colon_token.to_tokens(tokens);
1664 self.ty.to_tokens(tokens);
1665 self.eq_token.to_tokens(tokens);
1666 self.expr.to_tokens(tokens);
1667 self.semi_token.to_tokens(tokens);
1668 }
1669 }
1670
1671 impl ToTokens for ImplItemMethod {
1672 fn to_tokens(&self, tokens: &mut Tokens) {
1673 tokens.append_all(self.attrs.outer());
1674 self.vis.to_tokens(tokens);
1675 self.defaultness.to_tokens(tokens);
1676 self.sig.to_tokens(tokens);
1677 self.block.brace_token.surround(tokens, |tokens| {
1678 tokens.append_all(self.attrs.inner());
1679 tokens.append_all(&self.block.stmts);
1680 });
1681 }
1682 }
1683
1684 impl ToTokens for ImplItemType {
1685 fn to_tokens(&self, tokens: &mut Tokens) {
1686 tokens.append_all(self.attrs.outer());
1687 self.vis.to_tokens(tokens);
1688 self.defaultness.to_tokens(tokens);
1689 self.type_token.to_tokens(tokens);
1690 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001691 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001692 self.eq_token.to_tokens(tokens);
1693 self.ty.to_tokens(tokens);
1694 self.semi_token.to_tokens(tokens);
1695 }
1696 }
1697
1698 impl ToTokens for ImplItemMacro {
1699 fn to_tokens(&self, tokens: &mut Tokens) {
1700 tokens.append_all(self.attrs.outer());
1701 self.mac.to_tokens(tokens);
1702 if !self.mac.is_braced() {
1703 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001704 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001705 }
1706 }
1707 }
1708
David Tolnay8894f602017-11-11 12:11:04 -08001709 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001710 fn to_tokens(&self, tokens: &mut Tokens) {
1711 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001712 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001713 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1714 self.semi_token.to_tokens(tokens);
1715 }
1716 }
1717
1718 impl ToTokens for ForeignItemStatic {
1719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 tokens.append_all(self.attrs.outer());
1721 self.vis.to_tokens(tokens);
1722 self.static_token.to_tokens(tokens);
1723 self.mutbl.to_tokens(tokens);
1724 self.ident.to_tokens(tokens);
1725 self.colon_token.to_tokens(tokens);
1726 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001727 self.semi_token.to_tokens(tokens);
1728 }
1729 }
1730
David Tolnay199bcbb2017-11-12 10:33:52 -08001731 impl ToTokens for ForeignItemType {
1732 fn to_tokens(&self, tokens: &mut Tokens) {
1733 tokens.append_all(self.attrs.outer());
1734 self.vis.to_tokens(tokens);
1735 self.type_token.to_tokens(tokens);
1736 self.ident.to_tokens(tokens);
1737 self.semi_token.to_tokens(tokens);
1738 }
1739 }
1740
David Tolnay570695e2017-06-03 16:15:13 -07001741 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001742 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001743 self.constness.to_tokens(tokens);
1744 self.unsafety.to_tokens(tokens);
1745 self.abi.to_tokens(tokens);
1746 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 }
1748 }
1749
David Tolnay570695e2017-06-03 16:15:13 -07001750 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001751
1752 impl<'a> ToTokens for NamedDecl<'a> {
1753 fn to_tokens(&self, tokens: &mut Tokens) {
1754 self.0.fn_token.to_tokens(tokens);
1755 self.1.to_tokens(tokens);
1756 self.0.generics.to_tokens(tokens);
1757 self.0.paren_token.surround(tokens, |tokens| {
1758 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001759
1760 if self.0.variadic {
1761 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001762 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001763 }
Alex Crichton259ee532017-07-14 06:51:02 -07001764 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001765 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001766 });
1767 self.0.output.to_tokens(tokens);
1768 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001769 }
1770 }
1771
Alex Crichton62a0a592017-05-22 13:58:53 -07001772 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001773 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001774 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001775 self.lifetime.to_tokens(tokens);
1776 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001777 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001778 }
1779 }
1780
1781 impl ToTokens for ArgSelf {
1782 fn to_tokens(&self, tokens: &mut Tokens) {
1783 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001784 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001785 }
1786 }
1787
1788 impl ToTokens for ArgCaptured {
1789 fn to_tokens(&self, tokens: &mut Tokens) {
1790 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001791 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001792 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001793 }
1794 }
1795
David Tolnay42602292016-10-01 22:25:45 -07001796 impl ToTokens for Constness {
1797 fn to_tokens(&self, tokens: &mut Tokens) {
1798 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001799 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001800 Constness::NotConst => {
1801 // nothing
1802 }
David Tolnay42602292016-10-01 22:25:45 -07001803 }
1804 }
1805 }
1806
David Tolnay4c9be372016-10-06 00:47:37 -07001807 impl ToTokens for Defaultness {
1808 fn to_tokens(&self, tokens: &mut Tokens) {
1809 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001811 Defaultness::Final => {
1812 // nothing
1813 }
1814 }
1815 }
1816 }
1817
1818 impl ToTokens for ImplPolarity {
1819 fn to_tokens(&self, tokens: &mut Tokens) {
1820 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001822 ImplPolarity::Positive => {
1823 // nothing
1824 }
1825 }
1826 }
1827 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001828}