blob: abe5f1d1b194817c65ff93abf97f0d72f16418d8 [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![+]>,
Nika Layzell6bd36812017-12-05 13:46:07 -0500340 pub where_clause: WhereClause,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800341 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800342 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700343 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800344 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800345 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800346 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800347 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700349}
350
351ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700352 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 pub enum ImplPolarity {
354 /// `impl Trait for Type`
355 Positive,
356 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800357 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 }
359}
360
Alex Crichton62a0a592017-05-22 13:58:53 -0700361ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800362 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800364 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700365 pub vis: Visibility,
366 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800367 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700368 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800369 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800370 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800373 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700374 }),
375 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800376 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700377 pub vis: Visibility,
378 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub sig: MethodSig,
380 pub block: Block,
381 }),
382 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800383 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700384 pub vis: Visibility,
385 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800386 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700387 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500388 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800389 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800390 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800391 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 }),
David Tolnay857628c2017-11-11 12:25:31 -0800393 pub Macro(ImplItemMacro {
394 pub attrs: Vec<Attribute>,
395 pub mac: Macro,
396 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700398}
399
400ast_struct! {
401 /// Represents a method's signature in a trait declaration,
402 /// or in an implementation.
403 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700405 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700407 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700408 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700409 }
410}
411
412ast_struct! {
413 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700414 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 /// E.g. `fn foo(bar: baz)`
416 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700418 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800419 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800420 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700421 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800423 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700425}
426
Alex Crichton62a0a592017-05-22 13:58:53 -0700427ast_enum_of_structs! {
428 /// An argument in a function header.
429 ///
430 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
431 pub enum FnArg {
432 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800433 pub and_token: Token![&],
434 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 pub lifetime: Option<Lifetime>,
436 pub mutbl: Mutability,
437 }),
438 pub SelfValue(ArgSelf {
439 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800440 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700441 }),
442 pub Captured(ArgCaptured {
443 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800445 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800447 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 }
David Tolnay62f374c2016-10-02 13:37:00 -0700449}
450
David Tolnayedf2b992016-09-23 20:43:45 -0700451#[cfg(feature = "parsing")]
452pub mod parsing {
453 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700454
Michael Layzell92639a52017-06-01 00:07:44 -0400455 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700456
David Tolnay4c614be2017-11-10 00:02:38 -0800457 impl_synom!(Item "item" alt!(
458 syn!(ItemExternCrate) => { Item::ExternCrate }
459 |
460 syn!(ItemUse) => { Item::Use }
461 |
462 syn!(ItemStatic) => { Item::Static }
463 |
464 syn!(ItemConst) => { Item::Const }
465 |
466 syn!(ItemFn) => { Item::Fn }
467 |
468 syn!(ItemMod) => { Item::Mod }
469 |
470 syn!(ItemForeignMod) => { Item::ForeignMod }
471 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800472 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800473 |
474 syn!(ItemStruct) => { Item::Struct }
475 |
476 syn!(ItemEnum) => { Item::Enum }
477 |
478 syn!(ItemUnion) => { Item::Union }
479 |
480 syn!(ItemTrait) => { Item::Trait }
481 |
482 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
483 |
484 syn!(ItemImpl) => { Item::Impl }
485 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800486 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800487 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700488
David Tolnaydecf28d2017-11-11 11:56:45 -0800489 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700490 attrs: many0!(call!(Attribute::parse_outer)) >>
491 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800492 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700493 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700494 body: call!(::TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800495 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800496 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700497 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800498 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800499 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500500 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700501 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700502 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800503 },
David Tolnay4c614be2017-11-10 00:02:38 -0800504 })
David Tolnayedf2b992016-09-23 20:43:45 -0700505 ));
506
David Tolnay4c614be2017-11-10 00:02:38 -0800507 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700508 attrs: many0!(call!(Attribute::parse_outer)) >>
509 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 extern_: keyword!(extern) >>
511 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700512 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800513 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
514 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800515 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700516 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800517 vis: vis,
518 extern_token: extern_,
519 crate_token: crate_,
520 ident: ident,
521 rename: rename,
522 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800523 })
David Tolnayedf2b992016-09-23 20:43:45 -0700524 ));
525
David Tolnay4c614be2017-11-10 00:02:38 -0800526 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700527 attrs: many0!(call!(Attribute::parse_outer)) >>
528 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800529 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700530 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800531 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800532 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700533 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800534 vis: vis,
535 use_token: use_,
536 path: Box::new(what),
537 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800538 })
David Tolnay4a057422016-10-08 00:02:31 -0700539 ));
540
Alex Crichton954046c2017-05-30 21:49:42 -0700541 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400542 named!(parse -> Self, alt!(
543 syn!(PathGlob) => { ViewPath::Glob }
544 |
545 syn!(PathList) => { ViewPath::List }
546 |
547 syn!(PathSimple) => { ViewPath::Simple } // must be last
548 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700549 }
David Tolnay4a057422016-10-08 00:02:31 -0700550
Alex Crichton954046c2017-05-30 21:49:42 -0700551 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400552 named!(parse -> Self, do_parse!(
553 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800554 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400555 (PathSimple {
556 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800557 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400558 rename: rename.map(|p| p.1),
559 })
560 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700561 }
David Tolnay4a057422016-10-08 00:02:31 -0700562
Alex Crichton954046c2017-05-30 21:49:42 -0700563 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400564 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700565 path: option!(do_parse!(
566 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800567 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700568 (path, colon2)
569 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800570 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700571 ({
572 match path {
573 Some((path, colon2)) => {
574 PathGlob {
575 path: path,
576 colon2_token: Some(colon2),
577 star_token: star,
578 }
579 }
580 None => {
581 PathGlob {
582 path: Path {
583 leading_colon: None,
584 segments: Default::default(),
585 },
586 colon2_token: None,
587 star_token: star,
588 }
589 }
590 }
Michael Layzell92639a52017-06-01 00:07:44 -0400591 })
592 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700593 }
David Tolnay4a057422016-10-08 00:02:31 -0700594
Alex Crichton954046c2017-05-30 21:49:42 -0700595 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400596 named!(parse -> Self, alt!(
597 do_parse!(
598 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800599 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400600 items: braces!(call!(Delimited::parse_terminated)) >>
601 (PathList {
602 path: path,
603 items: items.0,
604 brace_token: items.1,
605 colon2_token: colon2,
606 })
607 )
608 |
609 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800610 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400611 items: braces!(call!(Delimited::parse_terminated)) >>
612 (PathList {
613 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400614 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700615 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400616 },
David Tolnay570695e2017-06-03 16:15:13 -0700617 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400618 brace_token: items.1,
619 items: items.0,
620 })
621 )
622 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700623 }
David Tolnay4a057422016-10-08 00:02:31 -0700624
Alex Crichton954046c2017-05-30 21:49:42 -0700625 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400626 named!(parse -> Self, do_parse!(
627 name: alt!(
628 syn!(Ident)
629 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800630 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400631 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800632 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400633 (PathListItem {
634 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800635 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400636 rename: rename.map(|p| p.1),
637 })
638 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700639 }
David Tolnay4a057422016-10-08 00:02:31 -0700640
David Tolnay4c614be2017-11-10 00:02:38 -0800641 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700642 attrs: many0!(call!(Attribute::parse_outer)) >>
643 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700645 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700646 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800647 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800648 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800649 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700650 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800651 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800652 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700653 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800654 vis: vis,
655 static_token: static_,
656 mutbl: mutability,
657 ident: ident,
658 colon_token: colon,
659 ty: Box::new(ty),
660 eq_token: eq,
661 expr: Box::new(value),
662 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800663 })
David Tolnay47a877c2016-10-01 16:50:55 -0700664 ));
665
David Tolnay4c614be2017-11-10 00:02:38 -0800666 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700667 attrs: many0!(call!(Attribute::parse_outer)) >>
668 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800669 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700670 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800671 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800672 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800673 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700674 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800675 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800676 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700677 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800678 vis: vis,
679 const_token: const_,
680 ident: ident,
681 colon_token: colon,
682 ty: Box::new(ty),
683 eq_token: eq,
684 expr: Box::new(value),
685 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800686 })
David Tolnay47a877c2016-10-01 16:50:55 -0700687 ));
688
David Tolnay4c614be2017-11-10 00:02:38 -0800689 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700690 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
691 vis: syn!(Visibility) >>
692 constness: syn!(Constness) >>
693 unsafety: syn!(Unsafety) >>
694 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800695 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700696 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700697 generics: syn!(Generics) >>
698 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800699 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700700 where_clause: syn!(WhereClause) >>
701 inner_attrs_stmts: braces!(tuple!(
702 many0!(call!(Attribute::parse_inner)),
703 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400704 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800705 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700706 attrs: {
707 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700708 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700709 attrs
710 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800711 vis: vis,
712 constness: constness,
713 unsafety: unsafety,
714 abi: abi,
715 decl: Box::new(FnDecl {
716 dot_tokens: None,
717 fn_token: fn_,
718 paren_token: inputs.1,
719 inputs: inputs.0,
720 output: ret,
721 variadic: false,
722 generics: Generics {
723 where_clause: where_clause,
724 .. generics
725 },
726 }),
727 ident: ident,
728 block: Box::new(Block {
729 brace_token: inner_attrs_stmts.1,
730 stmts: (inner_attrs_stmts.0).1,
731 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800732 })
David Tolnay42602292016-10-01 22:25:45 -0700733 ));
734
Alex Crichton954046c2017-05-30 21:49:42 -0700735 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400736 named!(parse -> Self, alt!(
737 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800738 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400739 lt: option!(syn!(Lifetime)) >>
740 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800741 self_: keyword!(self) >>
742 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400743 (ArgSelfRef {
744 lifetime: lt,
745 mutbl: mutability,
746 and_token: and,
747 self_token: self_,
748 }.into())
749 )
750 |
751 do_parse!(
752 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800753 self_: keyword!(self) >>
754 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400755 (ArgSelf {
756 mutbl: mutability,
757 self_token: self_,
758 }.into())
759 )
760 |
761 do_parse!(
762 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800763 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800764 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400765 (ArgCaptured {
766 pat: pat,
767 ty: ty,
768 colon_token: colon,
769 }.into())
770 )
771 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800772 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400773 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700774 }
David Tolnay62f374c2016-10-02 13:37:00 -0700775
David Tolnay4c614be2017-11-10 00:02:38 -0800776 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700777 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
778 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800779 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700780 ident: syn!(Ident) >>
781 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800782 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700783 Vec::new(),
784 None,
785 Some(semi),
786 )}
David Tolnay37d10332016-10-13 20:51:04 -0700787 |
Alex Crichton954046c2017-05-30 21:49:42 -0700788 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700789 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700790 many0!(call!(Attribute::parse_inner)),
791 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400792 )
David Tolnay570695e2017-06-03 16:15:13 -0700793 ) => {|((inner_attrs, items), brace)| (
794 inner_attrs,
795 Some((brace, items)),
796 None,
797 )}
David Tolnay37d10332016-10-13 20:51:04 -0700798 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800799 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700800 attrs: {
801 let mut attrs = outer_attrs;
802 attrs.extend(content_semi.0);
803 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700804 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800805 vis: vis,
806 mod_token: mod_,
807 ident: ident,
808 content: content_semi.1,
809 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800810 })
David Tolnay35902302016-10-06 01:11:08 -0700811 ));
812
David Tolnay4c614be2017-11-10 00:02:38 -0800813 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700814 attrs: many0!(call!(Attribute::parse_outer)) >>
815 abi: syn!(Abi) >>
816 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800817 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700818 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800819 abi: abi,
820 brace_token: items.1,
821 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800822 })
David Tolnay35902302016-10-06 01:11:08 -0700823 ));
824
David Tolnay8894f602017-11-11 12:11:04 -0800825 impl_synom!(ForeignItem "foreign item" alt!(
826 syn!(ForeignItemFn) => { ForeignItem::Fn }
827 |
828 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800829 |
830 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800831 ));
David Tolnay35902302016-10-06 01:11:08 -0700832
David Tolnay8894f602017-11-11 12:11:04 -0800833 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700834 attrs: many0!(call!(Attribute::parse_outer)) >>
835 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800836 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700837 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700838 generics: syn!(Generics) >>
839 inputs: parens!(do_parse!(
840 args: call!(Delimited::parse_terminated) >>
841 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800842 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700843 (args, variadic)
844 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800845 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700846 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800847 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700848 ({
849 let ((inputs, variadic), parens) = inputs;
850 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800851 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700852 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700853 attrs: attrs,
854 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800855 decl: Box::new(FnDecl {
856 fn_token: fn_,
857 paren_token: parens,
858 inputs: inputs,
859 variadic: variadic.is_some(),
860 dot_tokens: variadic,
861 output: ret,
862 generics: Generics {
863 where_clause: where_clause,
864 .. generics
865 },
866 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700867 vis: vis,
868 }
David Tolnay35902302016-10-06 01:11:08 -0700869 })
870 ));
871
David Tolnay8894f602017-11-11 12:11:04 -0800872 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700873 attrs: many0!(call!(Attribute::parse_outer)) >>
874 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800875 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700876 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700877 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800878 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800879 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800880 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800881 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700882 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700883 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700884 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800885 ty: Box::new(ty),
886 mutbl: mutability,
887 static_token: static_,
888 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700889 vis: vis,
890 })
891 ));
892
David Tolnay199bcbb2017-11-12 10:33:52 -0800893 impl_synom!(ForeignItemType "foreign type" do_parse!(
894 attrs: many0!(call!(Attribute::parse_outer)) >>
895 vis: syn!(Visibility) >>
896 type_: keyword!(type) >>
897 ident: syn!(Ident) >>
898 semi: punct!(;) >>
899 (ForeignItemType {
900 attrs: attrs,
901 vis: vis,
902 type_token: type_,
903 ident: ident,
904 semi_token: semi,
905 })
906 ));
907
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800908 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700909 attrs: many0!(call!(Attribute::parse_outer)) >>
910 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800911 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700912 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700913 generics: syn!(Generics) >>
914 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800915 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800916 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800917 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800918 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700919 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800920 vis: vis,
921 type_token: type_,
922 ident: ident,
923 generics: Generics {
924 where_clause: where_clause,
925 ..generics
926 },
927 eq_token: eq,
928 ty: Box::new(ty),
929 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800930 })
David Tolnay3cf52982016-10-01 17:11:37 -0700931 ));
932
David Tolnay4c614be2017-11-10 00:02:38 -0800933 impl_synom!(ItemStruct "struct item" switch!(
934 map!(syn!(DeriveInput), Into::into),
935 Item::Struct(item) => value!(item)
936 |
937 _ => reject!()
938 ));
David Tolnay42602292016-10-01 22:25:45 -0700939
David Tolnay4c614be2017-11-10 00:02:38 -0800940 impl_synom!(ItemEnum "enum item" switch!(
941 map!(syn!(DeriveInput), Into::into),
942 Item::Enum(item) => value!(item)
943 |
944 _ => reject!()
945 ));
946
947 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700948 attrs: many0!(call!(Attribute::parse_outer)) >>
949 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800950 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700951 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700952 generics: syn!(Generics) >>
953 where_clause: syn!(WhereClause) >>
954 fields: braces!(call!(Delimited::parse_terminated_with,
955 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800956 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700957 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800958 vis: vis,
959 union_token: union_,
960 ident: ident,
961 generics: Generics {
962 where_clause: where_clause,
963 .. generics
964 },
965 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800966 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700967 ));
968
David Tolnay4c614be2017-11-10 00:02:38 -0800969 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700970 attrs: many0!(call!(Attribute::parse_outer)) >>
971 vis: syn!(Visibility) >>
972 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500973 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700975 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700976 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800977 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700978 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700979 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700980 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700981 where_clause: syn!(WhereClause) >>
982 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800983 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700984 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800985 vis: vis,
986 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500987 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800988 trait_token: trait_,
989 ident: ident,
990 generics: Generics {
991 where_clause: where_clause,
992 .. generics
993 },
994 colon_token: colon,
995 supertraits: bounds.unwrap_or_default(),
996 brace_token: body.1,
997 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800998 })
David Tolnay0aecb732016-10-03 23:03:50 -0700999 ));
1000
David Tolnay4c614be2017-11-10 00:02:38 -08001001 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001002 attrs: many0!(call!(Attribute::parse_outer)) >>
1003 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001004 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001005 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001006 for_: keyword!(for) >>
1007 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001008 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001009 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001010 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001011 unsafety: unsafety,
1012 impl_token: impl_,
1013 path: path,
1014 for_token: for_,
1015 dot2_token: dot2,
1016 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001017 })
David Tolnayf94e2362016-10-04 00:29:51 -07001018 ));
1019
David Tolnayda705bd2017-11-10 21:58:05 -08001020 impl_synom!(TraitItem "trait item" alt!(
1021 syn!(TraitItemConst) => { TraitItem::Const }
1022 |
1023 syn!(TraitItemMethod) => { TraitItem::Method }
1024 |
1025 syn!(TraitItemType) => { TraitItem::Type }
1026 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001027 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001028 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001029
David Tolnayda705bd2017-11-10 21:58:05 -08001030 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001031 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001033 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001034 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001035 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001036 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1037 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001038 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001039 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001040 const_token: const_,
1041 ident: ident,
1042 colon_token: colon,
1043 ty: ty,
1044 default: default,
1045 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001046 })
1047 ));
1048
David Tolnayda705bd2017-11-10 21:58:05 -08001049 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001050 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1051 constness: syn!(Constness) >>
1052 unsafety: syn!(Unsafety) >>
1053 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001054 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001055 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001056 generics: syn!(Generics) >>
1057 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001058 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001059 where_clause: syn!(WhereClause) >>
1060 body: option!(braces!(
1061 tuple!(many0!(call!(Attribute::parse_inner)),
1062 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001063 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001064 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001065 ({
1066 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001067 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001068 None => (Vec::new(), None),
1069 };
David Tolnayda705bd2017-11-10 21:58:05 -08001070 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001071 attrs: {
1072 let mut attrs = outer_attrs;
1073 attrs.extend(inner_attrs);
1074 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001075 },
David Tolnayda705bd2017-11-10 21:58:05 -08001076 sig: MethodSig {
1077 constness: constness,
1078 unsafety: unsafety,
1079 abi: abi,
1080 ident: ident,
1081 decl: FnDecl {
1082 inputs: inputs.0,
1083 output: ret,
1084 variadic: false,
1085 fn_token: fn_,
1086 paren_token: inputs.1,
1087 dot_tokens: None,
1088 generics: Generics {
1089 where_clause: where_clause,
1090 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001091 },
1092 },
David Tolnayda705bd2017-11-10 21:58:05 -08001093 },
1094 default: stmts.map(|stmts| {
1095 Block {
1096 stmts: stmts.0,
1097 brace_token: stmts.1,
1098 }
1099 }),
1100 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001101 }
David Tolnay0aecb732016-10-03 23:03:50 -07001102 })
1103 ));
1104
David Tolnayda705bd2017-11-10 21:58:05 -08001105 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001106 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001107 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001108 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001109 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001110 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001111 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001112 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001113 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001114 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001115 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001116 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001117 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001118 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001119 type_token: type_,
1120 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001121 generics: generics,
David Tolnayda705bd2017-11-10 21:58:05 -08001122 colon_token: colon,
1123 bounds: bounds.unwrap_or_default(),
Nika Layzell6bd36812017-12-05 13:46:07 -05001124 where_clause: where_clause,
David Tolnayda705bd2017-11-10 21:58:05 -08001125 default: default,
1126 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001127 })
1128 ));
1129
David Tolnaydecf28d2017-11-11 11:56:45 -08001130 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001131 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001132 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001133 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001134 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001135 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001136 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001137 })
1138 ));
1139
David Tolnay4c614be2017-11-10 00:02:38 -08001140 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001141 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001142 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001143 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001144 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001145 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001146 polarity_path: alt!(
1147 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001148 polarity: syn!(ImplPolarity) >>
1149 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001150 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001151 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001152 )
1153 |
David Tolnay570695e2017-06-03 16:15:13 -07001154 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001155 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001156 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001157 where_clause: syn!(WhereClause) >>
1158 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001159 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001160 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001161 defaultness: defaultness,
1162 unsafety: unsafety,
1163 impl_token: impl_,
1164 generics: Generics {
1165 where_clause: where_clause,
1166 .. generics
1167 },
1168 trait_: polarity_path,
1169 self_ty: Box::new(self_ty),
1170 brace_token: body.1,
1171 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001172 })
David Tolnay4c9be372016-10-06 00:47:37 -07001173 ));
1174
David Tolnay857628c2017-11-11 12:25:31 -08001175 impl_synom!(ImplItem "item in impl block" alt!(
1176 syn!(ImplItemConst) => { ImplItem::Const }
1177 |
1178 syn!(ImplItemMethod) => { ImplItem::Method }
1179 |
1180 syn!(ImplItemType) => { ImplItem::Type }
1181 |
1182 syn!(ImplItemMacro) => { ImplItem::Macro }
1183 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001184
David Tolnay857628c2017-11-11 12:25:31 -08001185 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001186 attrs: many0!(call!(Attribute::parse_outer)) >>
1187 vis: syn!(Visibility) >>
1188 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001189 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001190 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001191 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001192 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001193 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001194 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001195 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001196 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001197 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001198 vis: vis,
1199 defaultness: defaultness,
1200 const_token: const_,
1201 ident: ident,
1202 colon_token: colon,
1203 ty: ty,
1204 eq_token: eq,
1205 expr: value,
1206 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001207 })
1208 ));
1209
David Tolnay857628c2017-11-11 12:25:31 -08001210 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001211 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1212 vis: syn!(Visibility) >>
1213 defaultness: syn!(Defaultness) >>
1214 constness: syn!(Constness) >>
1215 unsafety: syn!(Unsafety) >>
1216 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001217 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001218 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001219 generics: syn!(Generics) >>
1220 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001221 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001222 where_clause: syn!(WhereClause) >>
1223 inner_attrs_stmts: braces!(tuple!(
1224 many0!(call!(Attribute::parse_inner)),
1225 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001226 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001227 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001228 attrs: {
1229 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001230 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001231 attrs
1232 },
David Tolnay857628c2017-11-11 12:25:31 -08001233 vis: vis,
1234 defaultness: defaultness,
1235 sig: MethodSig {
1236 constness: constness,
1237 unsafety: unsafety,
1238 abi: abi,
1239 ident: ident,
1240 decl: FnDecl {
1241 fn_token: fn_,
1242 paren_token: inputs.1,
1243 inputs: inputs.0,
1244 output: ret,
1245 variadic: false,
1246 generics: Generics {
1247 where_clause: where_clause,
1248 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001249 },
David Tolnay857628c2017-11-11 12:25:31 -08001250 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001251 },
David Tolnay857628c2017-11-11 12:25:31 -08001252 },
1253 block: Block {
1254 brace_token: inner_attrs_stmts.1,
1255 stmts: (inner_attrs_stmts.0).1,
1256 },
David Tolnay4c9be372016-10-06 00:47:37 -07001257 })
1258 ));
1259
David Tolnay857628c2017-11-11 12:25:31 -08001260 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001261 attrs: many0!(call!(Attribute::parse_outer)) >>
1262 vis: syn!(Visibility) >>
1263 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001264 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001265 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001266 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001267 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001268 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001270 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001271 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001272 vis: vis,
1273 defaultness: defaultness,
1274 type_token: type_,
1275 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001276 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001277 eq_token: eq,
1278 ty: ty,
1279 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001280 })
1281 ));
1282
David Tolnay857628c2017-11-11 12:25:31 -08001283 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001284 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001285 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001286 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001287 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001288 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001289 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001290 })
1291 ));
1292
Alex Crichton954046c2017-05-30 21:49:42 -07001293 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001294 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001295 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001296 |
1297 epsilon!() => { |_| ImplPolarity::Positive }
1298 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001299 }
David Tolnay4c9be372016-10-06 00:47:37 -07001300
Alex Crichton954046c2017-05-30 21:49:42 -07001301 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001302 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001303 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001304 |
1305 epsilon!() => { |_| Constness::NotConst }
1306 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001307 }
David Tolnay42602292016-10-01 22:25:45 -07001308
Alex Crichton954046c2017-05-30 21:49:42 -07001309 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001310 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001311 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001312 |
1313 epsilon!() => { |_| Defaultness::Final }
1314 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001315 }
David Tolnayedf2b992016-09-23 20:43:45 -07001316}
David Tolnay4a51dc72016-10-01 00:40:31 -07001317
1318#[cfg(feature = "printing")]
1319mod printing {
1320 use super::*;
1321 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001322 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001323 use quote::{Tokens, ToTokens};
1324
David Tolnay1bfa7332017-11-11 12:41:20 -08001325 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001326 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001327 tokens.append_all(self.attrs.outer());
1328 self.vis.to_tokens(tokens);
1329 self.extern_token.to_tokens(tokens);
1330 self.crate_token.to_tokens(tokens);
1331 self.ident.to_tokens(tokens);
1332 if let Some((ref as_token, ref rename)) = self.rename {
1333 as_token.to_tokens(tokens);
1334 rename.to_tokens(tokens);
1335 }
1336 self.semi_token.to_tokens(tokens);
1337 }
1338 }
1339
1340 impl ToTokens for ItemUse {
1341 fn to_tokens(&self, tokens: &mut Tokens) {
1342 tokens.append_all(self.attrs.outer());
1343 self.vis.to_tokens(tokens);
1344 self.use_token.to_tokens(tokens);
1345 self.path.to_tokens(tokens);
1346 self.semi_token.to_tokens(tokens);
1347 }
1348 }
1349
1350 impl ToTokens for ItemStatic {
1351 fn to_tokens(&self, tokens: &mut Tokens) {
1352 tokens.append_all(self.attrs.outer());
1353 self.vis.to_tokens(tokens);
1354 self.static_token.to_tokens(tokens);
1355 self.mutbl.to_tokens(tokens);
1356 self.ident.to_tokens(tokens);
1357 self.colon_token.to_tokens(tokens);
1358 self.ty.to_tokens(tokens);
1359 self.eq_token.to_tokens(tokens);
1360 self.expr.to_tokens(tokens);
1361 self.semi_token.to_tokens(tokens);
1362 }
1363 }
1364
1365 impl ToTokens for ItemConst {
1366 fn to_tokens(&self, tokens: &mut Tokens) {
1367 tokens.append_all(self.attrs.outer());
1368 self.vis.to_tokens(tokens);
1369 self.const_token.to_tokens(tokens);
1370 self.ident.to_tokens(tokens);
1371 self.colon_token.to_tokens(tokens);
1372 self.ty.to_tokens(tokens);
1373 self.eq_token.to_tokens(tokens);
1374 self.expr.to_tokens(tokens);
1375 self.semi_token.to_tokens(tokens);
1376 }
1377 }
1378
1379 impl ToTokens for ItemFn {
1380 fn to_tokens(&self, tokens: &mut Tokens) {
1381 tokens.append_all(self.attrs.outer());
1382 self.vis.to_tokens(tokens);
1383 self.constness.to_tokens(tokens);
1384 self.unsafety.to_tokens(tokens);
1385 self.abi.to_tokens(tokens);
1386 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1387 self.block.brace_token.surround(tokens, |tokens| {
1388 tokens.append_all(self.attrs.inner());
1389 tokens.append_all(&self.block.stmts);
1390 });
1391 }
1392 }
1393
1394 impl ToTokens for ItemMod {
1395 fn to_tokens(&self, tokens: &mut Tokens) {
1396 tokens.append_all(self.attrs.outer());
1397 self.vis.to_tokens(tokens);
1398 self.mod_token.to_tokens(tokens);
1399 self.ident.to_tokens(tokens);
1400 if let Some((ref brace, ref items)) = self.content {
1401 brace.surround(tokens, |tokens| {
1402 tokens.append_all(self.attrs.inner());
1403 tokens.append_all(items);
1404 });
1405 } else {
1406 TokensOrDefault(&self.semi).to_tokens(tokens);
1407 }
1408 }
1409 }
1410
1411 impl ToTokens for ItemForeignMod {
1412 fn to_tokens(&self, tokens: &mut Tokens) {
1413 tokens.append_all(self.attrs.outer());
1414 self.abi.to_tokens(tokens);
1415 self.brace_token.surround(tokens, |tokens| {
1416 tokens.append_all(&self.items);
1417 });
1418 }
1419 }
1420
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001421 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001422 fn to_tokens(&self, tokens: &mut Tokens) {
1423 tokens.append_all(self.attrs.outer());
1424 self.vis.to_tokens(tokens);
1425 self.type_token.to_tokens(tokens);
1426 self.ident.to_tokens(tokens);
1427 self.generics.to_tokens(tokens);
1428 self.generics.where_clause.to_tokens(tokens);
1429 self.eq_token.to_tokens(tokens);
1430 self.ty.to_tokens(tokens);
1431 self.semi_token.to_tokens(tokens);
1432 }
1433 }
1434
1435 impl ToTokens for ItemEnum {
1436 fn to_tokens(&self, tokens: &mut Tokens) {
1437 tokens.append_all(self.attrs.outer());
1438 self.vis.to_tokens(tokens);
1439 self.enum_token.to_tokens(tokens);
1440 self.ident.to_tokens(tokens);
1441 self.generics.to_tokens(tokens);
1442 self.generics.where_clause.to_tokens(tokens);
1443 self.brace_token.surround(tokens, |tokens| {
1444 self.variants.to_tokens(tokens);
1445 });
1446 }
1447 }
1448
1449 impl ToTokens for ItemStruct {
1450 fn to_tokens(&self, tokens: &mut Tokens) {
1451 tokens.append_all(self.attrs.outer());
1452 self.vis.to_tokens(tokens);
1453 self.struct_token.to_tokens(tokens);
1454 self.ident.to_tokens(tokens);
1455 self.generics.to_tokens(tokens);
1456 match self.data {
1457 VariantData::Struct(..) => {
1458 self.generics.where_clause.to_tokens(tokens);
1459 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001460 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001461 VariantData::Tuple(..) => {
1462 self.data.to_tokens(tokens);
1463 self.generics.where_clause.to_tokens(tokens);
1464 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001465 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001466 VariantData::Unit => {
1467 self.generics.where_clause.to_tokens(tokens);
1468 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001469 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001470 }
1471 }
1472 }
1473
1474 impl ToTokens for ItemUnion {
1475 fn to_tokens(&self, tokens: &mut Tokens) {
1476 tokens.append_all(self.attrs.outer());
1477 self.vis.to_tokens(tokens);
1478 self.union_token.to_tokens(tokens);
1479 self.ident.to_tokens(tokens);
1480 self.generics.to_tokens(tokens);
1481 self.generics.where_clause.to_tokens(tokens);
1482 // XXX: Should we handle / complain when using a
1483 // non-VariantData::Struct Variant in Union?
1484 self.data.to_tokens(tokens);
1485 }
1486 }
1487
1488 impl ToTokens for ItemTrait {
1489 fn to_tokens(&self, tokens: &mut Tokens) {
1490 tokens.append_all(self.attrs.outer());
1491 self.vis.to_tokens(tokens);
1492 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001493 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001494 self.trait_token.to_tokens(tokens);
1495 self.ident.to_tokens(tokens);
1496 self.generics.to_tokens(tokens);
1497 if !self.supertraits.is_empty() {
1498 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1499 self.supertraits.to_tokens(tokens);
1500 }
1501 self.generics.where_clause.to_tokens(tokens);
1502 self.brace_token.surround(tokens, |tokens| {
1503 tokens.append_all(&self.items);
1504 });
1505 }
1506 }
1507
1508 impl ToTokens for ItemDefaultImpl {
1509 fn to_tokens(&self, tokens: &mut Tokens) {
1510 tokens.append_all(self.attrs.outer());
1511 self.unsafety.to_tokens(tokens);
1512 self.impl_token.to_tokens(tokens);
1513 self.path.to_tokens(tokens);
1514 self.for_token.to_tokens(tokens);
1515 self.dot2_token.to_tokens(tokens);
1516 self.brace_token.surround(tokens, |_tokens| {});
1517 }
1518 }
1519
1520 impl ToTokens for ItemImpl {
1521 fn to_tokens(&self, tokens: &mut Tokens) {
1522 tokens.append_all(self.attrs.outer());
1523 self.defaultness.to_tokens(tokens);
1524 self.unsafety.to_tokens(tokens);
1525 self.impl_token.to_tokens(tokens);
1526 self.generics.to_tokens(tokens);
1527 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1528 polarity.to_tokens(tokens);
1529 path.to_tokens(tokens);
1530 for_token.to_tokens(tokens);
1531 }
1532 self.self_ty.to_tokens(tokens);
1533 self.generics.where_clause.to_tokens(tokens);
1534 self.brace_token.surround(tokens, |tokens| {
1535 tokens.append_all(&self.items);
1536 });
1537 }
1538 }
1539
1540 impl ToTokens for ItemMacro {
1541 fn to_tokens(&self, tokens: &mut Tokens) {
1542 tokens.append_all(self.attrs.outer());
1543 self.mac.path.to_tokens(tokens);
1544 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001545 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001546 tokens.append_all(&self.mac.tokens);
1547 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001548 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001549 }
1550 }
1551 }
David Tolnay42602292016-10-01 22:25:45 -07001552
Alex Crichton62a0a592017-05-22 13:58:53 -07001553 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001554 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001555 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001556 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001557 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001558 self.rename.to_tokens(tokens);
1559 }
David Tolnay4a057422016-10-08 00:02:31 -07001560 }
1561 }
1562
Alex Crichton62a0a592017-05-22 13:58:53 -07001563 impl ToTokens for PathGlob {
1564 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001565 if self.path.segments.len() > 0 {
1566 self.path.to_tokens(tokens);
1567 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1568 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001570 }
1571 }
1572
1573 impl ToTokens for PathList {
1574 fn to_tokens(&self, tokens: &mut Tokens) {
1575 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001576 self.colon2_token.to_tokens(tokens);
1577 self.brace_token.surround(tokens, |tokens| {
1578 self.items.to_tokens(tokens);
1579 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001580 }
1581 }
1582
David Tolnay4a057422016-10-08 00:02:31 -07001583 impl ToTokens for PathListItem {
1584 fn to_tokens(&self, tokens: &mut Tokens) {
1585 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001586 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001587 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001588 self.rename.to_tokens(tokens);
1589 }
David Tolnay4a057422016-10-08 00:02:31 -07001590 }
1591 }
1592
David Tolnay1bfa7332017-11-11 12:41:20 -08001593 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001595 tokens.append_all(self.attrs.outer());
1596 self.const_token.to_tokens(tokens);
1597 self.ident.to_tokens(tokens);
1598 self.colon_token.to_tokens(tokens);
1599 self.ty.to_tokens(tokens);
1600 if let Some((ref eq_token, ref default)) = self.default {
1601 eq_token.to_tokens(tokens);
1602 default.to_tokens(tokens);
1603 }
1604 self.semi_token.to_tokens(tokens);
1605 }
1606 }
1607
1608 impl ToTokens for TraitItemMethod {
1609 fn to_tokens(&self, tokens: &mut Tokens) {
1610 tokens.append_all(self.attrs.outer());
1611 self.sig.to_tokens(tokens);
1612 match self.default {
1613 Some(ref block) => {
1614 block.brace_token.surround(tokens, |tokens| {
1615 tokens.append_all(self.attrs.inner());
1616 tokens.append_all(&block.stmts);
1617 });
David Tolnayca085422016-10-04 00:12:38 -07001618 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001619 None => {
1620 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001621 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001622 }
1623 }
1624 }
1625
1626 impl ToTokens for TraitItemType {
1627 fn to_tokens(&self, tokens: &mut Tokens) {
1628 tokens.append_all(self.attrs.outer());
1629 self.type_token.to_tokens(tokens);
1630 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001631 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001632 if !self.bounds.is_empty() {
1633 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1634 self.bounds.to_tokens(tokens);
1635 }
Nika Layzell6bd36812017-12-05 13:46:07 -05001636 self.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001637 if let Some((ref eq_token, ref default)) = self.default {
1638 eq_token.to_tokens(tokens);
1639 default.to_tokens(tokens);
1640 }
1641 self.semi_token.to_tokens(tokens);
1642 }
1643 }
1644
1645 impl ToTokens for TraitItemMacro {
1646 fn to_tokens(&self, tokens: &mut Tokens) {
1647 tokens.append_all(self.attrs.outer());
1648 self.mac.to_tokens(tokens);
1649 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001650 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001651 }
1652 }
1653 }
1654
David Tolnay857628c2017-11-11 12:25:31 -08001655 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001656 fn to_tokens(&self, tokens: &mut Tokens) {
1657 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001658 self.vis.to_tokens(tokens);
1659 self.defaultness.to_tokens(tokens);
1660 self.const_token.to_tokens(tokens);
1661 self.ident.to_tokens(tokens);
1662 self.colon_token.to_tokens(tokens);
1663 self.ty.to_tokens(tokens);
1664 self.eq_token.to_tokens(tokens);
1665 self.expr.to_tokens(tokens);
1666 self.semi_token.to_tokens(tokens);
1667 }
1668 }
1669
1670 impl ToTokens for ImplItemMethod {
1671 fn to_tokens(&self, tokens: &mut Tokens) {
1672 tokens.append_all(self.attrs.outer());
1673 self.vis.to_tokens(tokens);
1674 self.defaultness.to_tokens(tokens);
1675 self.sig.to_tokens(tokens);
1676 self.block.brace_token.surround(tokens, |tokens| {
1677 tokens.append_all(self.attrs.inner());
1678 tokens.append_all(&self.block.stmts);
1679 });
1680 }
1681 }
1682
1683 impl ToTokens for ImplItemType {
1684 fn to_tokens(&self, tokens: &mut Tokens) {
1685 tokens.append_all(self.attrs.outer());
1686 self.vis.to_tokens(tokens);
1687 self.defaultness.to_tokens(tokens);
1688 self.type_token.to_tokens(tokens);
1689 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001690 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001691 self.eq_token.to_tokens(tokens);
1692 self.ty.to_tokens(tokens);
1693 self.semi_token.to_tokens(tokens);
1694 }
1695 }
1696
1697 impl ToTokens for ImplItemMacro {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 tokens.append_all(self.attrs.outer());
1700 self.mac.to_tokens(tokens);
1701 if !self.mac.is_braced() {
1702 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001703 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001704 }
1705 }
1706 }
1707
David Tolnay8894f602017-11-11 12:11:04 -08001708 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001709 fn to_tokens(&self, tokens: &mut Tokens) {
1710 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001712 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1713 self.semi_token.to_tokens(tokens);
1714 }
1715 }
1716
1717 impl ToTokens for ForeignItemStatic {
1718 fn to_tokens(&self, tokens: &mut Tokens) {
1719 tokens.append_all(self.attrs.outer());
1720 self.vis.to_tokens(tokens);
1721 self.static_token.to_tokens(tokens);
1722 self.mutbl.to_tokens(tokens);
1723 self.ident.to_tokens(tokens);
1724 self.colon_token.to_tokens(tokens);
1725 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001726 self.semi_token.to_tokens(tokens);
1727 }
1728 }
1729
David Tolnay199bcbb2017-11-12 10:33:52 -08001730 impl ToTokens for ForeignItemType {
1731 fn to_tokens(&self, tokens: &mut Tokens) {
1732 tokens.append_all(self.attrs.outer());
1733 self.vis.to_tokens(tokens);
1734 self.type_token.to_tokens(tokens);
1735 self.ident.to_tokens(tokens);
1736 self.semi_token.to_tokens(tokens);
1737 }
1738 }
1739
David Tolnay570695e2017-06-03 16:15:13 -07001740 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001741 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001742 self.constness.to_tokens(tokens);
1743 self.unsafety.to_tokens(tokens);
1744 self.abi.to_tokens(tokens);
1745 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001746 }
1747 }
1748
David Tolnay570695e2017-06-03 16:15:13 -07001749 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001750
1751 impl<'a> ToTokens for NamedDecl<'a> {
1752 fn to_tokens(&self, tokens: &mut Tokens) {
1753 self.0.fn_token.to_tokens(tokens);
1754 self.1.to_tokens(tokens);
1755 self.0.generics.to_tokens(tokens);
1756 self.0.paren_token.surround(tokens, |tokens| {
1757 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001758
1759 if self.0.variadic {
1760 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001761 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001762 }
Alex Crichton259ee532017-07-14 06:51:02 -07001763 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001764 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765 });
1766 self.0.output.to_tokens(tokens);
1767 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001768 }
1769 }
1770
Alex Crichton62a0a592017-05-22 13:58:53 -07001771 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001772 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001773 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001774 self.lifetime.to_tokens(tokens);
1775 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001776 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001777 }
1778 }
1779
1780 impl ToTokens for ArgSelf {
1781 fn to_tokens(&self, tokens: &mut Tokens) {
1782 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001783 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001784 }
1785 }
1786
1787 impl ToTokens for ArgCaptured {
1788 fn to_tokens(&self, tokens: &mut Tokens) {
1789 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001790 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001791 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001792 }
1793 }
1794
David Tolnay42602292016-10-01 22:25:45 -07001795 impl ToTokens for Constness {
1796 fn to_tokens(&self, tokens: &mut Tokens) {
1797 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001798 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001799 Constness::NotConst => {
1800 // nothing
1801 }
David Tolnay42602292016-10-01 22:25:45 -07001802 }
1803 }
1804 }
1805
David Tolnay4c9be372016-10-06 00:47:37 -07001806 impl ToTokens for Defaultness {
1807 fn to_tokens(&self, tokens: &mut Tokens) {
1808 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001809 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001810 Defaultness::Final => {
1811 // nothing
1812 }
1813 }
1814 }
1815 }
1816
1817 impl ToTokens for ImplPolarity {
1818 fn to_tokens(&self, tokens: &mut Tokens) {
1819 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001820 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001821 ImplPolarity::Positive => {
1822 // nothing
1823 }
1824 }
1825 }
1826 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001827}