blob: 847ab8b03d54fd486a0bb3b48bf9a510f670f171 [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 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001113 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001114 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001115 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001116 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001117 type_token: type_,
1118 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001119 generics: generics,
David Tolnayda705bd2017-11-10 21:58:05 -08001120 colon_token: colon,
1121 bounds: bounds.unwrap_or_default(),
1122 default: default,
1123 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001124 })
1125 ));
1126
David Tolnaydecf28d2017-11-11 11:56:45 -08001127 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001128 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001129 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001130 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001131 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001132 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001133 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001134 })
1135 ));
1136
David Tolnay4c614be2017-11-10 00:02:38 -08001137 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001138 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001139 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001140 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001141 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001142 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001143 polarity_path: alt!(
1144 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001145 polarity: syn!(ImplPolarity) >>
1146 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001147 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001148 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001149 )
1150 |
David Tolnay570695e2017-06-03 16:15:13 -07001151 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001152 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001153 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001154 where_clause: syn!(WhereClause) >>
1155 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001156 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001157 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001158 defaultness: defaultness,
1159 unsafety: unsafety,
1160 impl_token: impl_,
1161 generics: Generics {
1162 where_clause: where_clause,
1163 .. generics
1164 },
1165 trait_: polarity_path,
1166 self_ty: Box::new(self_ty),
1167 brace_token: body.1,
1168 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001169 })
David Tolnay4c9be372016-10-06 00:47:37 -07001170 ));
1171
David Tolnay857628c2017-11-11 12:25:31 -08001172 impl_synom!(ImplItem "item in impl block" alt!(
1173 syn!(ImplItemConst) => { ImplItem::Const }
1174 |
1175 syn!(ImplItemMethod) => { ImplItem::Method }
1176 |
1177 syn!(ImplItemType) => { ImplItem::Type }
1178 |
1179 syn!(ImplItemMacro) => { ImplItem::Macro }
1180 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001181
David Tolnay857628c2017-11-11 12:25:31 -08001182 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001183 attrs: many0!(call!(Attribute::parse_outer)) >>
1184 vis: syn!(Visibility) >>
1185 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001186 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001187 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001188 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001189 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001190 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001191 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001192 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001193 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001194 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001195 vis: vis,
1196 defaultness: defaultness,
1197 const_token: const_,
1198 ident: ident,
1199 colon_token: colon,
1200 ty: ty,
1201 eq_token: eq,
1202 expr: value,
1203 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001204 })
1205 ));
1206
David Tolnay857628c2017-11-11 12:25:31 -08001207 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001208 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1209 vis: syn!(Visibility) >>
1210 defaultness: syn!(Defaultness) >>
1211 constness: syn!(Constness) >>
1212 unsafety: syn!(Unsafety) >>
1213 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001214 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001215 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001216 generics: syn!(Generics) >>
1217 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001218 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001219 where_clause: syn!(WhereClause) >>
1220 inner_attrs_stmts: braces!(tuple!(
1221 many0!(call!(Attribute::parse_inner)),
1222 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001223 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001224 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001225 attrs: {
1226 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001227 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001228 attrs
1229 },
David Tolnay857628c2017-11-11 12:25:31 -08001230 vis: vis,
1231 defaultness: defaultness,
1232 sig: MethodSig {
1233 constness: constness,
1234 unsafety: unsafety,
1235 abi: abi,
1236 ident: ident,
1237 decl: FnDecl {
1238 fn_token: fn_,
1239 paren_token: inputs.1,
1240 inputs: inputs.0,
1241 output: ret,
1242 variadic: false,
1243 generics: Generics {
1244 where_clause: where_clause,
1245 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001246 },
David Tolnay857628c2017-11-11 12:25:31 -08001247 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001248 },
David Tolnay857628c2017-11-11 12:25:31 -08001249 },
1250 block: Block {
1251 brace_token: inner_attrs_stmts.1,
1252 stmts: (inner_attrs_stmts.0).1,
1253 },
David Tolnay4c9be372016-10-06 00:47:37 -07001254 })
1255 ));
1256
David Tolnay857628c2017-11-11 12:25:31 -08001257 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001258 attrs: many0!(call!(Attribute::parse_outer)) >>
1259 vis: syn!(Visibility) >>
1260 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001262 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001263 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001264 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001265 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001266 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001267 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001268 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001269 vis: vis,
1270 defaultness: defaultness,
1271 type_token: type_,
1272 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001273 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001274 eq_token: eq,
1275 ty: ty,
1276 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001277 })
1278 ));
1279
David Tolnay857628c2017-11-11 12:25:31 -08001280 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001281 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001282 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001283 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001284 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001285 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001286 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001287 })
1288 ));
1289
Alex Crichton954046c2017-05-30 21:49:42 -07001290 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001291 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001292 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001293 |
1294 epsilon!() => { |_| ImplPolarity::Positive }
1295 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001296 }
David Tolnay4c9be372016-10-06 00:47:37 -07001297
Alex Crichton954046c2017-05-30 21:49:42 -07001298 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001299 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001300 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001301 |
1302 epsilon!() => { |_| Constness::NotConst }
1303 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001304 }
David Tolnay42602292016-10-01 22:25:45 -07001305
Alex Crichton954046c2017-05-30 21:49:42 -07001306 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001307 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001309 |
1310 epsilon!() => { |_| Defaultness::Final }
1311 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001312 }
David Tolnayedf2b992016-09-23 20:43:45 -07001313}
David Tolnay4a51dc72016-10-01 00:40:31 -07001314
1315#[cfg(feature = "printing")]
1316mod printing {
1317 use super::*;
1318 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001319 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001320 use quote::{Tokens, ToTokens};
1321
David Tolnay1bfa7332017-11-11 12:41:20 -08001322 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001323 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001324 tokens.append_all(self.attrs.outer());
1325 self.vis.to_tokens(tokens);
1326 self.extern_token.to_tokens(tokens);
1327 self.crate_token.to_tokens(tokens);
1328 self.ident.to_tokens(tokens);
1329 if let Some((ref as_token, ref rename)) = self.rename {
1330 as_token.to_tokens(tokens);
1331 rename.to_tokens(tokens);
1332 }
1333 self.semi_token.to_tokens(tokens);
1334 }
1335 }
1336
1337 impl ToTokens for ItemUse {
1338 fn to_tokens(&self, tokens: &mut Tokens) {
1339 tokens.append_all(self.attrs.outer());
1340 self.vis.to_tokens(tokens);
1341 self.use_token.to_tokens(tokens);
1342 self.path.to_tokens(tokens);
1343 self.semi_token.to_tokens(tokens);
1344 }
1345 }
1346
1347 impl ToTokens for ItemStatic {
1348 fn to_tokens(&self, tokens: &mut Tokens) {
1349 tokens.append_all(self.attrs.outer());
1350 self.vis.to_tokens(tokens);
1351 self.static_token.to_tokens(tokens);
1352 self.mutbl.to_tokens(tokens);
1353 self.ident.to_tokens(tokens);
1354 self.colon_token.to_tokens(tokens);
1355 self.ty.to_tokens(tokens);
1356 self.eq_token.to_tokens(tokens);
1357 self.expr.to_tokens(tokens);
1358 self.semi_token.to_tokens(tokens);
1359 }
1360 }
1361
1362 impl ToTokens for ItemConst {
1363 fn to_tokens(&self, tokens: &mut Tokens) {
1364 tokens.append_all(self.attrs.outer());
1365 self.vis.to_tokens(tokens);
1366 self.const_token.to_tokens(tokens);
1367 self.ident.to_tokens(tokens);
1368 self.colon_token.to_tokens(tokens);
1369 self.ty.to_tokens(tokens);
1370 self.eq_token.to_tokens(tokens);
1371 self.expr.to_tokens(tokens);
1372 self.semi_token.to_tokens(tokens);
1373 }
1374 }
1375
1376 impl ToTokens for ItemFn {
1377 fn to_tokens(&self, tokens: &mut Tokens) {
1378 tokens.append_all(self.attrs.outer());
1379 self.vis.to_tokens(tokens);
1380 self.constness.to_tokens(tokens);
1381 self.unsafety.to_tokens(tokens);
1382 self.abi.to_tokens(tokens);
1383 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1384 self.block.brace_token.surround(tokens, |tokens| {
1385 tokens.append_all(self.attrs.inner());
1386 tokens.append_all(&self.block.stmts);
1387 });
1388 }
1389 }
1390
1391 impl ToTokens for ItemMod {
1392 fn to_tokens(&self, tokens: &mut Tokens) {
1393 tokens.append_all(self.attrs.outer());
1394 self.vis.to_tokens(tokens);
1395 self.mod_token.to_tokens(tokens);
1396 self.ident.to_tokens(tokens);
1397 if let Some((ref brace, ref items)) = self.content {
1398 brace.surround(tokens, |tokens| {
1399 tokens.append_all(self.attrs.inner());
1400 tokens.append_all(items);
1401 });
1402 } else {
1403 TokensOrDefault(&self.semi).to_tokens(tokens);
1404 }
1405 }
1406 }
1407
1408 impl ToTokens for ItemForeignMod {
1409 fn to_tokens(&self, tokens: &mut Tokens) {
1410 tokens.append_all(self.attrs.outer());
1411 self.abi.to_tokens(tokens);
1412 self.brace_token.surround(tokens, |tokens| {
1413 tokens.append_all(&self.items);
1414 });
1415 }
1416 }
1417
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001418 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001419 fn to_tokens(&self, tokens: &mut Tokens) {
1420 tokens.append_all(self.attrs.outer());
1421 self.vis.to_tokens(tokens);
1422 self.type_token.to_tokens(tokens);
1423 self.ident.to_tokens(tokens);
1424 self.generics.to_tokens(tokens);
1425 self.generics.where_clause.to_tokens(tokens);
1426 self.eq_token.to_tokens(tokens);
1427 self.ty.to_tokens(tokens);
1428 self.semi_token.to_tokens(tokens);
1429 }
1430 }
1431
1432 impl ToTokens for ItemEnum {
1433 fn to_tokens(&self, tokens: &mut Tokens) {
1434 tokens.append_all(self.attrs.outer());
1435 self.vis.to_tokens(tokens);
1436 self.enum_token.to_tokens(tokens);
1437 self.ident.to_tokens(tokens);
1438 self.generics.to_tokens(tokens);
1439 self.generics.where_clause.to_tokens(tokens);
1440 self.brace_token.surround(tokens, |tokens| {
1441 self.variants.to_tokens(tokens);
1442 });
1443 }
1444 }
1445
1446 impl ToTokens for ItemStruct {
1447 fn to_tokens(&self, tokens: &mut Tokens) {
1448 tokens.append_all(self.attrs.outer());
1449 self.vis.to_tokens(tokens);
1450 self.struct_token.to_tokens(tokens);
1451 self.ident.to_tokens(tokens);
1452 self.generics.to_tokens(tokens);
1453 match self.data {
1454 VariantData::Struct(..) => {
1455 self.generics.where_clause.to_tokens(tokens);
1456 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001457 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001458 VariantData::Tuple(..) => {
1459 self.data.to_tokens(tokens);
1460 self.generics.where_clause.to_tokens(tokens);
1461 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001462 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001463 VariantData::Unit => {
1464 self.generics.where_clause.to_tokens(tokens);
1465 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001466 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001467 }
1468 }
1469 }
1470
1471 impl ToTokens for ItemUnion {
1472 fn to_tokens(&self, tokens: &mut Tokens) {
1473 tokens.append_all(self.attrs.outer());
1474 self.vis.to_tokens(tokens);
1475 self.union_token.to_tokens(tokens);
1476 self.ident.to_tokens(tokens);
1477 self.generics.to_tokens(tokens);
1478 self.generics.where_clause.to_tokens(tokens);
1479 // XXX: Should we handle / complain when using a
1480 // non-VariantData::Struct Variant in Union?
1481 self.data.to_tokens(tokens);
1482 }
1483 }
1484
1485 impl ToTokens for ItemTrait {
1486 fn to_tokens(&self, tokens: &mut Tokens) {
1487 tokens.append_all(self.attrs.outer());
1488 self.vis.to_tokens(tokens);
1489 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001490 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001491 self.trait_token.to_tokens(tokens);
1492 self.ident.to_tokens(tokens);
1493 self.generics.to_tokens(tokens);
1494 if !self.supertraits.is_empty() {
1495 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1496 self.supertraits.to_tokens(tokens);
1497 }
1498 self.generics.where_clause.to_tokens(tokens);
1499 self.brace_token.surround(tokens, |tokens| {
1500 tokens.append_all(&self.items);
1501 });
1502 }
1503 }
1504
1505 impl ToTokens for ItemDefaultImpl {
1506 fn to_tokens(&self, tokens: &mut Tokens) {
1507 tokens.append_all(self.attrs.outer());
1508 self.unsafety.to_tokens(tokens);
1509 self.impl_token.to_tokens(tokens);
1510 self.path.to_tokens(tokens);
1511 self.for_token.to_tokens(tokens);
1512 self.dot2_token.to_tokens(tokens);
1513 self.brace_token.surround(tokens, |_tokens| {});
1514 }
1515 }
1516
1517 impl ToTokens for ItemImpl {
1518 fn to_tokens(&self, tokens: &mut Tokens) {
1519 tokens.append_all(self.attrs.outer());
1520 self.defaultness.to_tokens(tokens);
1521 self.unsafety.to_tokens(tokens);
1522 self.impl_token.to_tokens(tokens);
1523 self.generics.to_tokens(tokens);
1524 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1525 polarity.to_tokens(tokens);
1526 path.to_tokens(tokens);
1527 for_token.to_tokens(tokens);
1528 }
1529 self.self_ty.to_tokens(tokens);
1530 self.generics.where_clause.to_tokens(tokens);
1531 self.brace_token.surround(tokens, |tokens| {
1532 tokens.append_all(&self.items);
1533 });
1534 }
1535 }
1536
1537 impl ToTokens for ItemMacro {
1538 fn to_tokens(&self, tokens: &mut Tokens) {
1539 tokens.append_all(self.attrs.outer());
1540 self.mac.path.to_tokens(tokens);
1541 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001542 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001543 tokens.append_all(&self.mac.tokens);
1544 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001545 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001546 }
1547 }
1548 }
David Tolnay42602292016-10-01 22:25:45 -07001549
Alex Crichton62a0a592017-05-22 13:58:53 -07001550 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001551 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001552 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001553 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001554 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001555 self.rename.to_tokens(tokens);
1556 }
David Tolnay4a057422016-10-08 00:02:31 -07001557 }
1558 }
1559
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 impl ToTokens for PathGlob {
1561 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001562 if self.path.segments.len() > 0 {
1563 self.path.to_tokens(tokens);
1564 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1565 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001566 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001567 }
1568 }
1569
1570 impl ToTokens for PathList {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001573 self.colon2_token.to_tokens(tokens);
1574 self.brace_token.surround(tokens, |tokens| {
1575 self.items.to_tokens(tokens);
1576 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001577 }
1578 }
1579
David Tolnay4a057422016-10-08 00:02:31 -07001580 impl ToTokens for PathListItem {
1581 fn to_tokens(&self, tokens: &mut Tokens) {
1582 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001583 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001584 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001585 self.rename.to_tokens(tokens);
1586 }
David Tolnay4a057422016-10-08 00:02:31 -07001587 }
1588 }
1589
David Tolnay1bfa7332017-11-11 12:41:20 -08001590 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001591 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001592 tokens.append_all(self.attrs.outer());
1593 self.const_token.to_tokens(tokens);
1594 self.ident.to_tokens(tokens);
1595 self.colon_token.to_tokens(tokens);
1596 self.ty.to_tokens(tokens);
1597 if let Some((ref eq_token, ref default)) = self.default {
1598 eq_token.to_tokens(tokens);
1599 default.to_tokens(tokens);
1600 }
1601 self.semi_token.to_tokens(tokens);
1602 }
1603 }
1604
1605 impl ToTokens for TraitItemMethod {
1606 fn to_tokens(&self, tokens: &mut Tokens) {
1607 tokens.append_all(self.attrs.outer());
1608 self.sig.to_tokens(tokens);
1609 match self.default {
1610 Some(ref block) => {
1611 block.brace_token.surround(tokens, |tokens| {
1612 tokens.append_all(self.attrs.inner());
1613 tokens.append_all(&block.stmts);
1614 });
David Tolnayca085422016-10-04 00:12:38 -07001615 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001616 None => {
1617 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001618 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001619 }
1620 }
1621 }
1622
1623 impl ToTokens for TraitItemType {
1624 fn to_tokens(&self, tokens: &mut Tokens) {
1625 tokens.append_all(self.attrs.outer());
1626 self.type_token.to_tokens(tokens);
1627 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001628 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001629 if !self.bounds.is_empty() {
1630 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1631 self.bounds.to_tokens(tokens);
1632 }
1633 if let Some((ref eq_token, ref default)) = self.default {
1634 eq_token.to_tokens(tokens);
1635 default.to_tokens(tokens);
1636 }
1637 self.semi_token.to_tokens(tokens);
1638 }
1639 }
1640
1641 impl ToTokens for TraitItemMacro {
1642 fn to_tokens(&self, tokens: &mut Tokens) {
1643 tokens.append_all(self.attrs.outer());
1644 self.mac.to_tokens(tokens);
1645 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001646 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001647 }
1648 }
1649 }
1650
David Tolnay857628c2017-11-11 12:25:31 -08001651 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001652 fn to_tokens(&self, tokens: &mut Tokens) {
1653 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001654 self.vis.to_tokens(tokens);
1655 self.defaultness.to_tokens(tokens);
1656 self.const_token.to_tokens(tokens);
1657 self.ident.to_tokens(tokens);
1658 self.colon_token.to_tokens(tokens);
1659 self.ty.to_tokens(tokens);
1660 self.eq_token.to_tokens(tokens);
1661 self.expr.to_tokens(tokens);
1662 self.semi_token.to_tokens(tokens);
1663 }
1664 }
1665
1666 impl ToTokens for ImplItemMethod {
1667 fn to_tokens(&self, tokens: &mut Tokens) {
1668 tokens.append_all(self.attrs.outer());
1669 self.vis.to_tokens(tokens);
1670 self.defaultness.to_tokens(tokens);
1671 self.sig.to_tokens(tokens);
1672 self.block.brace_token.surround(tokens, |tokens| {
1673 tokens.append_all(self.attrs.inner());
1674 tokens.append_all(&self.block.stmts);
1675 });
1676 }
1677 }
1678
1679 impl ToTokens for ImplItemType {
1680 fn to_tokens(&self, tokens: &mut Tokens) {
1681 tokens.append_all(self.attrs.outer());
1682 self.vis.to_tokens(tokens);
1683 self.defaultness.to_tokens(tokens);
1684 self.type_token.to_tokens(tokens);
1685 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001686 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001687 self.eq_token.to_tokens(tokens);
1688 self.ty.to_tokens(tokens);
1689 self.semi_token.to_tokens(tokens);
1690 }
1691 }
1692
1693 impl ToTokens for ImplItemMacro {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 tokens.append_all(self.attrs.outer());
1696 self.mac.to_tokens(tokens);
1697 if !self.mac.is_braced() {
1698 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001699 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001700 }
1701 }
1702 }
1703
David Tolnay8894f602017-11-11 12:11:04 -08001704 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001705 fn to_tokens(&self, tokens: &mut Tokens) {
1706 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001707 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001708 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1709 self.semi_token.to_tokens(tokens);
1710 }
1711 }
1712
1713 impl ToTokens for ForeignItemStatic {
1714 fn to_tokens(&self, tokens: &mut Tokens) {
1715 tokens.append_all(self.attrs.outer());
1716 self.vis.to_tokens(tokens);
1717 self.static_token.to_tokens(tokens);
1718 self.mutbl.to_tokens(tokens);
1719 self.ident.to_tokens(tokens);
1720 self.colon_token.to_tokens(tokens);
1721 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001722 self.semi_token.to_tokens(tokens);
1723 }
1724 }
1725
David Tolnay199bcbb2017-11-12 10:33:52 -08001726 impl ToTokens for ForeignItemType {
1727 fn to_tokens(&self, tokens: &mut Tokens) {
1728 tokens.append_all(self.attrs.outer());
1729 self.vis.to_tokens(tokens);
1730 self.type_token.to_tokens(tokens);
1731 self.ident.to_tokens(tokens);
1732 self.semi_token.to_tokens(tokens);
1733 }
1734 }
1735
David Tolnay570695e2017-06-03 16:15:13 -07001736 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001737 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001738 self.constness.to_tokens(tokens);
1739 self.unsafety.to_tokens(tokens);
1740 self.abi.to_tokens(tokens);
1741 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001742 }
1743 }
1744
David Tolnay570695e2017-06-03 16:15:13 -07001745 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001746
1747 impl<'a> ToTokens for NamedDecl<'a> {
1748 fn to_tokens(&self, tokens: &mut Tokens) {
1749 self.0.fn_token.to_tokens(tokens);
1750 self.1.to_tokens(tokens);
1751 self.0.generics.to_tokens(tokens);
1752 self.0.paren_token.surround(tokens, |tokens| {
1753 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001754
1755 if self.0.variadic {
1756 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001757 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001758 }
Alex Crichton259ee532017-07-14 06:51:02 -07001759 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001760 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001761 });
1762 self.0.output.to_tokens(tokens);
1763 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001764 }
1765 }
1766
Alex Crichton62a0a592017-05-22 13:58:53 -07001767 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001768 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001769 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001770 self.lifetime.to_tokens(tokens);
1771 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001772 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001773 }
1774 }
1775
1776 impl ToTokens for ArgSelf {
1777 fn to_tokens(&self, tokens: &mut Tokens) {
1778 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001779 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001780 }
1781 }
1782
1783 impl ToTokens for ArgCaptured {
1784 fn to_tokens(&self, tokens: &mut Tokens) {
1785 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001786 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001787 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001788 }
1789 }
1790
David Tolnay42602292016-10-01 22:25:45 -07001791 impl ToTokens for Constness {
1792 fn to_tokens(&self, tokens: &mut Tokens) {
1793 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001794 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001795 Constness::NotConst => {
1796 // nothing
1797 }
David Tolnay42602292016-10-01 22:25:45 -07001798 }
1799 }
1800 }
1801
David Tolnay4c9be372016-10-06 00:47:37 -07001802 impl ToTokens for Defaultness {
1803 fn to_tokens(&self, tokens: &mut Tokens) {
1804 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001805 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001806 Defaultness::Final => {
1807 // nothing
1808 }
1809 }
1810 }
1811 }
1812
1813 impl ToTokens for ImplPolarity {
1814 fn to_tokens(&self, tokens: &mut Tokens) {
1815 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001816 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001817 ImplPolarity::Positive => {
1818 // nothing
1819 }
1820 }
1821 }
1822 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001823}