blob: 194a1ece0d7de0c8301958b2313b07dc33e0b091 [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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800337 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800338 pub bounds: Delimited<TypeParamBound, Token![+]>,
339 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800340 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800342 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800343 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800344 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800345 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700346 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700347}
348
349ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700350 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub enum ImplPolarity {
352 /// `impl Trait for Type`
353 Positive,
354 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800355 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 }
357}
358
Alex Crichton62a0a592017-05-22 13:58:53 -0700359ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800360 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800362 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700363 pub vis: Visibility,
364 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800365 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700366 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800367 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800368 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800369 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 }),
373 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800374 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700375 pub vis: Visibility,
376 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700377 pub sig: MethodSig,
378 pub block: Block,
379 }),
380 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800381 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700382 pub vis: Visibility,
383 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800384 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700385 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800386 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800387 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay857628c2017-11-11 12:25:31 -0800390 pub Macro(ImplItemMacro {
391 pub attrs: Vec<Attribute>,
392 pub mac: Macro,
393 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700395}
396
397ast_struct! {
398 /// Represents a method's signature in a trait declaration,
399 /// or in an implementation.
400 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700402 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700404 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 }
407}
408
409ast_struct! {
410 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700411 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 /// E.g. `fn foo(bar: baz)`
413 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700415 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800416 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800417 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700418 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700422}
423
Alex Crichton62a0a592017-05-22 13:58:53 -0700424ast_enum_of_structs! {
425 /// An argument in a function header.
426 ///
427 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
428 pub enum FnArg {
429 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800430 pub and_token: Token![&],
431 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 pub lifetime: Option<Lifetime>,
433 pub mutbl: Mutability,
434 }),
435 pub SelfValue(ArgSelf {
436 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 }),
439 pub Captured(ArgCaptured {
440 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800442 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800444 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 }
David Tolnay62f374c2016-10-02 13:37:00 -0700446}
447
David Tolnayedf2b992016-09-23 20:43:45 -0700448#[cfg(feature = "parsing")]
449pub mod parsing {
450 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700451
Michael Layzell92639a52017-06-01 00:07:44 -0400452 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700453
David Tolnay4c614be2017-11-10 00:02:38 -0800454 impl_synom!(Item "item" alt!(
455 syn!(ItemExternCrate) => { Item::ExternCrate }
456 |
457 syn!(ItemUse) => { Item::Use }
458 |
459 syn!(ItemStatic) => { Item::Static }
460 |
461 syn!(ItemConst) => { Item::Const }
462 |
463 syn!(ItemFn) => { Item::Fn }
464 |
465 syn!(ItemMod) => { Item::Mod }
466 |
467 syn!(ItemForeignMod) => { Item::ForeignMod }
468 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800469 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800470 |
471 syn!(ItemStruct) => { Item::Struct }
472 |
473 syn!(ItemEnum) => { Item::Enum }
474 |
475 syn!(ItemUnion) => { Item::Union }
476 |
477 syn!(ItemTrait) => { Item::Trait }
478 |
479 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
480 |
481 syn!(ItemImpl) => { Item::Impl }
482 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800483 syn!(ItemMacro) => { Item::Macro }
David Tolnay4c614be2017-11-10 00:02:38 -0800484 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700485
David Tolnaydecf28d2017-11-11 11:56:45 -0800486 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700487 attrs: many0!(call!(Attribute::parse_outer)) >>
488 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800489 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700490 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700491 body: call!(::TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800492 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800493 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700494 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800495 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800496 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500497 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700498 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700499 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800500 },
David Tolnay4c614be2017-11-10 00:02:38 -0800501 })
David Tolnayedf2b992016-09-23 20:43:45 -0700502 ));
503
David Tolnay4c614be2017-11-10 00:02:38 -0800504 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700505 attrs: many0!(call!(Attribute::parse_outer)) >>
506 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800507 extern_: keyword!(extern) >>
508 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700509 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
511 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800512 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700513 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800514 vis: vis,
515 extern_token: extern_,
516 crate_token: crate_,
517 ident: ident,
518 rename: rename,
519 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800520 })
David Tolnayedf2b992016-09-23 20:43:45 -0700521 ));
522
David Tolnay4c614be2017-11-10 00:02:38 -0800523 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700524 attrs: many0!(call!(Attribute::parse_outer)) >>
525 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800526 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700527 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800528 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800529 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700530 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800531 vis: vis,
532 use_token: use_,
533 path: Box::new(what),
534 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800535 })
David Tolnay4a057422016-10-08 00:02:31 -0700536 ));
537
Alex Crichton954046c2017-05-30 21:49:42 -0700538 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400539 named!(parse -> Self, alt!(
540 syn!(PathGlob) => { ViewPath::Glob }
541 |
542 syn!(PathList) => { ViewPath::List }
543 |
544 syn!(PathSimple) => { ViewPath::Simple } // must be last
545 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700546 }
David Tolnay4a057422016-10-08 00:02:31 -0700547
Alex Crichton954046c2017-05-30 21:49:42 -0700548 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400549 named!(parse -> Self, do_parse!(
550 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400552 (PathSimple {
553 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800554 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400555 rename: rename.map(|p| p.1),
556 })
557 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700558 }
David Tolnay4a057422016-10-08 00:02:31 -0700559
Alex Crichton954046c2017-05-30 21:49:42 -0700560 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400561 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700562 path: option!(do_parse!(
563 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800564 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700565 (path, colon2)
566 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800567 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700568 ({
569 match path {
570 Some((path, colon2)) => {
571 PathGlob {
572 path: path,
573 colon2_token: Some(colon2),
574 star_token: star,
575 }
576 }
577 None => {
578 PathGlob {
579 path: Path {
580 leading_colon: None,
581 segments: Default::default(),
582 },
583 colon2_token: None,
584 star_token: star,
585 }
586 }
587 }
Michael Layzell92639a52017-06-01 00:07:44 -0400588 })
589 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700590 }
David Tolnay4a057422016-10-08 00:02:31 -0700591
Alex Crichton954046c2017-05-30 21:49:42 -0700592 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400593 named!(parse -> Self, alt!(
594 do_parse!(
595 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400597 items: braces!(call!(Delimited::parse_terminated)) >>
598 (PathList {
599 path: path,
600 items: items.0,
601 brace_token: items.1,
602 colon2_token: colon2,
603 })
604 )
605 |
606 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800607 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400608 items: braces!(call!(Delimited::parse_terminated)) >>
609 (PathList {
610 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400611 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700612 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400613 },
David Tolnay570695e2017-06-03 16:15:13 -0700614 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400615 brace_token: items.1,
616 items: items.0,
617 })
618 )
619 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700620 }
David Tolnay4a057422016-10-08 00:02:31 -0700621
Alex Crichton954046c2017-05-30 21:49:42 -0700622 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400623 named!(parse -> Self, do_parse!(
624 name: alt!(
625 syn!(Ident)
626 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400628 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800629 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400630 (PathListItem {
631 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800632 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400633 rename: rename.map(|p| p.1),
634 })
635 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700636 }
David Tolnay4a057422016-10-08 00:02:31 -0700637
David Tolnay4c614be2017-11-10 00:02:38 -0800638 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700639 attrs: many0!(call!(Attribute::parse_outer)) >>
640 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800641 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700642 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700643 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800645 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800646 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700647 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800648 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800649 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700650 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800651 vis: vis,
652 static_token: static_,
653 mutbl: mutability,
654 ident: ident,
655 colon_token: colon,
656 ty: Box::new(ty),
657 eq_token: eq,
658 expr: Box::new(value),
659 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800660 })
David Tolnay47a877c2016-10-01 16:50:55 -0700661 ));
662
David Tolnay4c614be2017-11-10 00:02:38 -0800663 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700664 attrs: many0!(call!(Attribute::parse_outer)) >>
665 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700667 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800668 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800669 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800670 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700671 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800672 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800673 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700674 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800675 vis: vis,
676 const_token: const_,
677 ident: ident,
678 colon_token: colon,
679 ty: Box::new(ty),
680 eq_token: eq,
681 expr: Box::new(value),
682 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800683 })
David Tolnay47a877c2016-10-01 16:50:55 -0700684 ));
685
David Tolnay4c614be2017-11-10 00:02:38 -0800686 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700687 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
688 vis: syn!(Visibility) >>
689 constness: syn!(Constness) >>
690 unsafety: syn!(Unsafety) >>
691 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800692 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700693 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700694 generics: syn!(Generics) >>
695 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800696 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700697 where_clause: syn!(WhereClause) >>
698 inner_attrs_stmts: braces!(tuple!(
699 many0!(call!(Attribute::parse_inner)),
700 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400701 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800702 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700703 attrs: {
704 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700705 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700706 attrs
707 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800708 vis: vis,
709 constness: constness,
710 unsafety: unsafety,
711 abi: abi,
712 decl: Box::new(FnDecl {
713 dot_tokens: None,
714 fn_token: fn_,
715 paren_token: inputs.1,
716 inputs: inputs.0,
717 output: ret,
718 variadic: false,
719 generics: Generics {
720 where_clause: where_clause,
721 .. generics
722 },
723 }),
724 ident: ident,
725 block: Box::new(Block {
726 brace_token: inner_attrs_stmts.1,
727 stmts: (inner_attrs_stmts.0).1,
728 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800729 })
David Tolnay42602292016-10-01 22:25:45 -0700730 ));
731
Alex Crichton954046c2017-05-30 21:49:42 -0700732 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400733 named!(parse -> Self, alt!(
734 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800735 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400736 lt: option!(syn!(Lifetime)) >>
737 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800738 self_: keyword!(self) >>
739 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400740 (ArgSelfRef {
741 lifetime: lt,
742 mutbl: mutability,
743 and_token: and,
744 self_token: self_,
745 }.into())
746 )
747 |
748 do_parse!(
749 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800750 self_: keyword!(self) >>
751 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400752 (ArgSelf {
753 mutbl: mutability,
754 self_token: self_,
755 }.into())
756 )
757 |
758 do_parse!(
759 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800760 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800761 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400762 (ArgCaptured {
763 pat: pat,
764 ty: ty,
765 colon_token: colon,
766 }.into())
767 )
768 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800769 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400770 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700771 }
David Tolnay62f374c2016-10-02 13:37:00 -0700772
David Tolnay4c614be2017-11-10 00:02:38 -0800773 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700774 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
775 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800776 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700777 ident: syn!(Ident) >>
778 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800779 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700780 Vec::new(),
781 None,
782 Some(semi),
783 )}
David Tolnay37d10332016-10-13 20:51:04 -0700784 |
Alex Crichton954046c2017-05-30 21:49:42 -0700785 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700786 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700787 many0!(call!(Attribute::parse_inner)),
788 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400789 )
David Tolnay570695e2017-06-03 16:15:13 -0700790 ) => {|((inner_attrs, items), brace)| (
791 inner_attrs,
792 Some((brace, items)),
793 None,
794 )}
David Tolnay37d10332016-10-13 20:51:04 -0700795 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800796 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700797 attrs: {
798 let mut attrs = outer_attrs;
799 attrs.extend(content_semi.0);
800 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700801 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800802 vis: vis,
803 mod_token: mod_,
804 ident: ident,
805 content: content_semi.1,
806 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800807 })
David Tolnay35902302016-10-06 01:11:08 -0700808 ));
809
David Tolnay4c614be2017-11-10 00:02:38 -0800810 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700811 attrs: many0!(call!(Attribute::parse_outer)) >>
812 abi: syn!(Abi) >>
813 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800814 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700815 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800816 abi: abi,
817 brace_token: items.1,
818 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800819 })
David Tolnay35902302016-10-06 01:11:08 -0700820 ));
821
David Tolnay8894f602017-11-11 12:11:04 -0800822 impl_synom!(ForeignItem "foreign item" alt!(
823 syn!(ForeignItemFn) => { ForeignItem::Fn }
824 |
825 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800826 |
827 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800828 ));
David Tolnay35902302016-10-06 01:11:08 -0700829
David Tolnay8894f602017-11-11 12:11:04 -0800830 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700831 attrs: many0!(call!(Attribute::parse_outer)) >>
832 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800833 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700834 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700835 generics: syn!(Generics) >>
836 inputs: parens!(do_parse!(
837 args: call!(Delimited::parse_terminated) >>
838 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800839 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700840 (args, variadic)
841 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800842 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700843 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800844 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700845 ({
846 let ((inputs, variadic), parens) = inputs;
847 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800848 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700849 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700850 attrs: attrs,
851 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800852 decl: Box::new(FnDecl {
853 fn_token: fn_,
854 paren_token: parens,
855 inputs: inputs,
856 variadic: variadic.is_some(),
857 dot_tokens: variadic,
858 output: ret,
859 generics: Generics {
860 where_clause: where_clause,
861 .. generics
862 },
863 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700864 vis: vis,
865 }
David Tolnay35902302016-10-06 01:11:08 -0700866 })
867 ));
868
David Tolnay8894f602017-11-11 12:11:04 -0800869 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700870 attrs: many0!(call!(Attribute::parse_outer)) >>
871 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800872 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700873 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700874 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800875 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800876 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800877 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800878 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700879 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700880 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700881 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800882 ty: Box::new(ty),
883 mutbl: mutability,
884 static_token: static_,
885 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700886 vis: vis,
887 })
888 ));
889
David Tolnay199bcbb2017-11-12 10:33:52 -0800890 impl_synom!(ForeignItemType "foreign type" do_parse!(
891 attrs: many0!(call!(Attribute::parse_outer)) >>
892 vis: syn!(Visibility) >>
893 type_: keyword!(type) >>
894 ident: syn!(Ident) >>
895 semi: punct!(;) >>
896 (ForeignItemType {
897 attrs: attrs,
898 vis: vis,
899 type_token: type_,
900 ident: ident,
901 semi_token: semi,
902 })
903 ));
904
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800905 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700906 attrs: many0!(call!(Attribute::parse_outer)) >>
907 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800908 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700909 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700910 generics: syn!(Generics) >>
911 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800912 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800913 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800915 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700916 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800917 vis: vis,
918 type_token: type_,
919 ident: ident,
920 generics: Generics {
921 where_clause: where_clause,
922 ..generics
923 },
924 eq_token: eq,
925 ty: Box::new(ty),
926 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800927 })
David Tolnay3cf52982016-10-01 17:11:37 -0700928 ));
929
David Tolnay4c614be2017-11-10 00:02:38 -0800930 impl_synom!(ItemStruct "struct item" switch!(
931 map!(syn!(DeriveInput), Into::into),
932 Item::Struct(item) => value!(item)
933 |
934 _ => reject!()
935 ));
David Tolnay42602292016-10-01 22:25:45 -0700936
David Tolnay4c614be2017-11-10 00:02:38 -0800937 impl_synom!(ItemEnum "enum item" switch!(
938 map!(syn!(DeriveInput), Into::into),
939 Item::Enum(item) => value!(item)
940 |
941 _ => reject!()
942 ));
943
944 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700945 attrs: many0!(call!(Attribute::parse_outer)) >>
946 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800947 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700948 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700949 generics: syn!(Generics) >>
950 where_clause: syn!(WhereClause) >>
951 fields: braces!(call!(Delimited::parse_terminated_with,
952 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800953 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700954 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800955 vis: vis,
956 union_token: union_,
957 ident: ident,
958 generics: Generics {
959 where_clause: where_clause,
960 .. generics
961 },
962 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800963 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700964 ));
965
David Tolnay4c614be2017-11-10 00:02:38 -0800966 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700967 attrs: many0!(call!(Attribute::parse_outer)) >>
968 vis: syn!(Visibility) >>
969 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500970 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800971 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700972 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700973 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700975 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700976 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700977 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700978 where_clause: syn!(WhereClause) >>
979 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800980 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700981 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800982 vis: vis,
983 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500984 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800985 trait_token: trait_,
986 ident: ident,
987 generics: Generics {
988 where_clause: where_clause,
989 .. generics
990 },
991 colon_token: colon,
992 supertraits: bounds.unwrap_or_default(),
993 brace_token: body.1,
994 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800995 })
David Tolnay0aecb732016-10-03 23:03:50 -0700996 ));
997
David Tolnay4c614be2017-11-10 00:02:38 -0800998 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700999 attrs: many0!(call!(Attribute::parse_outer)) >>
1000 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001001 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001002 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 for_: keyword!(for) >>
1004 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001005 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001006 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001007 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001008 unsafety: unsafety,
1009 impl_token: impl_,
1010 path: path,
1011 for_token: for_,
1012 dot2_token: dot2,
1013 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001014 })
David Tolnayf94e2362016-10-04 00:29:51 -07001015 ));
1016
David Tolnayda705bd2017-11-10 21:58:05 -08001017 impl_synom!(TraitItem "trait item" alt!(
1018 syn!(TraitItemConst) => { TraitItem::Const }
1019 |
1020 syn!(TraitItemMethod) => { TraitItem::Method }
1021 |
1022 syn!(TraitItemType) => { TraitItem::Type }
1023 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001024 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001025 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001026
David Tolnayda705bd2017-11-10 21:58:05 -08001027 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001028 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001029 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001030 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001032 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1034 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001035 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001036 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001037 const_token: const_,
1038 ident: ident,
1039 colon_token: colon,
1040 ty: ty,
1041 default: default,
1042 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001043 })
1044 ));
1045
David Tolnayda705bd2017-11-10 21:58:05 -08001046 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001047 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1048 constness: syn!(Constness) >>
1049 unsafety: syn!(Unsafety) >>
1050 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001051 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001052 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001053 generics: syn!(Generics) >>
1054 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001055 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001056 where_clause: syn!(WhereClause) >>
1057 body: option!(braces!(
1058 tuple!(many0!(call!(Attribute::parse_inner)),
1059 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001060 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001061 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001062 ({
1063 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001064 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001065 None => (Vec::new(), None),
1066 };
David Tolnayda705bd2017-11-10 21:58:05 -08001067 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001068 attrs: {
1069 let mut attrs = outer_attrs;
1070 attrs.extend(inner_attrs);
1071 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001072 },
David Tolnayda705bd2017-11-10 21:58:05 -08001073 sig: MethodSig {
1074 constness: constness,
1075 unsafety: unsafety,
1076 abi: abi,
1077 ident: ident,
1078 decl: FnDecl {
1079 inputs: inputs.0,
1080 output: ret,
1081 variadic: false,
1082 fn_token: fn_,
1083 paren_token: inputs.1,
1084 dot_tokens: None,
1085 generics: Generics {
1086 where_clause: where_clause,
1087 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001088 },
1089 },
David Tolnayda705bd2017-11-10 21:58:05 -08001090 },
1091 default: stmts.map(|stmts| {
1092 Block {
1093 stmts: stmts.0,
1094 brace_token: stmts.1,
1095 }
1096 }),
1097 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001098 }
David Tolnay0aecb732016-10-03 23:03:50 -07001099 })
1100 ));
1101
David Tolnayda705bd2017-11-10 21:58:05 -08001102 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001103 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001104 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001105 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001106 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001107 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001108 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001110 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001111 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001112 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001113 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001114 type_token: type_,
1115 ident: ident,
1116 colon_token: colon,
1117 bounds: bounds.unwrap_or_default(),
1118 default: default,
1119 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001120 })
1121 ));
1122
David Tolnaydecf28d2017-11-11 11:56:45 -08001123 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001124 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001125 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001126 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001127 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001128 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001129 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001130 })
1131 ));
1132
David Tolnay4c614be2017-11-10 00:02:38 -08001133 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001134 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001135 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001136 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001137 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001138 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001139 polarity_path: alt!(
1140 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001141 polarity: syn!(ImplPolarity) >>
1142 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001143 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001144 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001145 )
1146 |
David Tolnay570695e2017-06-03 16:15:13 -07001147 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001148 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001149 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001150 where_clause: syn!(WhereClause) >>
1151 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001152 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001153 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001154 defaultness: defaultness,
1155 unsafety: unsafety,
1156 impl_token: impl_,
1157 generics: Generics {
1158 where_clause: where_clause,
1159 .. generics
1160 },
1161 trait_: polarity_path,
1162 self_ty: Box::new(self_ty),
1163 brace_token: body.1,
1164 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001165 })
David Tolnay4c9be372016-10-06 00:47:37 -07001166 ));
1167
David Tolnay857628c2017-11-11 12:25:31 -08001168 impl_synom!(ImplItem "item in impl block" alt!(
1169 syn!(ImplItemConst) => { ImplItem::Const }
1170 |
1171 syn!(ImplItemMethod) => { ImplItem::Method }
1172 |
1173 syn!(ImplItemType) => { ImplItem::Type }
1174 |
1175 syn!(ImplItemMacro) => { ImplItem::Macro }
1176 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001177
David Tolnay857628c2017-11-11 12:25:31 -08001178 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001179 attrs: many0!(call!(Attribute::parse_outer)) >>
1180 vis: syn!(Visibility) >>
1181 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001182 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001183 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001184 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001185 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001186 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001187 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001188 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001189 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001190 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001191 vis: vis,
1192 defaultness: defaultness,
1193 const_token: const_,
1194 ident: ident,
1195 colon_token: colon,
1196 ty: ty,
1197 eq_token: eq,
1198 expr: value,
1199 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001200 })
1201 ));
1202
David Tolnay857628c2017-11-11 12:25:31 -08001203 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001204 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1205 vis: syn!(Visibility) >>
1206 defaultness: syn!(Defaultness) >>
1207 constness: syn!(Constness) >>
1208 unsafety: syn!(Unsafety) >>
1209 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001211 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001212 generics: syn!(Generics) >>
1213 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001214 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001215 where_clause: syn!(WhereClause) >>
1216 inner_attrs_stmts: braces!(tuple!(
1217 many0!(call!(Attribute::parse_inner)),
1218 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001219 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001220 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001221 attrs: {
1222 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001223 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001224 attrs
1225 },
David Tolnay857628c2017-11-11 12:25:31 -08001226 vis: vis,
1227 defaultness: defaultness,
1228 sig: MethodSig {
1229 constness: constness,
1230 unsafety: unsafety,
1231 abi: abi,
1232 ident: ident,
1233 decl: FnDecl {
1234 fn_token: fn_,
1235 paren_token: inputs.1,
1236 inputs: inputs.0,
1237 output: ret,
1238 variadic: false,
1239 generics: Generics {
1240 where_clause: where_clause,
1241 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001242 },
David Tolnay857628c2017-11-11 12:25:31 -08001243 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001244 },
David Tolnay857628c2017-11-11 12:25:31 -08001245 },
1246 block: Block {
1247 brace_token: inner_attrs_stmts.1,
1248 stmts: (inner_attrs_stmts.0).1,
1249 },
David Tolnay4c9be372016-10-06 00:47:37 -07001250 })
1251 ));
1252
David Tolnay857628c2017-11-11 12:25:31 -08001253 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001254 attrs: many0!(call!(Attribute::parse_outer)) >>
1255 vis: syn!(Visibility) >>
1256 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001258 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001259 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001260 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001262 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001263 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001264 vis: vis,
1265 defaultness: defaultness,
1266 type_token: type_,
1267 ident: ident,
1268 eq_token: eq,
1269 ty: ty,
1270 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001271 })
1272 ));
1273
David Tolnay857628c2017-11-11 12:25:31 -08001274 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001275 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001276 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001277 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001278 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001279 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001280 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001281 })
1282 ));
1283
Alex Crichton954046c2017-05-30 21:49:42 -07001284 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001285 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001286 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001287 |
1288 epsilon!() => { |_| ImplPolarity::Positive }
1289 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001290 }
David Tolnay4c9be372016-10-06 00:47:37 -07001291
Alex Crichton954046c2017-05-30 21:49:42 -07001292 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001293 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001295 |
1296 epsilon!() => { |_| Constness::NotConst }
1297 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001298 }
David Tolnay42602292016-10-01 22:25:45 -07001299
Alex Crichton954046c2017-05-30 21:49:42 -07001300 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001301 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001302 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001303 |
1304 epsilon!() => { |_| Defaultness::Final }
1305 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001306 }
David Tolnayedf2b992016-09-23 20:43:45 -07001307}
David Tolnay4a51dc72016-10-01 00:40:31 -07001308
1309#[cfg(feature = "printing")]
1310mod printing {
1311 use super::*;
1312 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001313 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001314 use quote::{Tokens, ToTokens};
1315
David Tolnay1bfa7332017-11-11 12:41:20 -08001316 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001317 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001318 tokens.append_all(self.attrs.outer());
1319 self.vis.to_tokens(tokens);
1320 self.extern_token.to_tokens(tokens);
1321 self.crate_token.to_tokens(tokens);
1322 self.ident.to_tokens(tokens);
1323 if let Some((ref as_token, ref rename)) = self.rename {
1324 as_token.to_tokens(tokens);
1325 rename.to_tokens(tokens);
1326 }
1327 self.semi_token.to_tokens(tokens);
1328 }
1329 }
1330
1331 impl ToTokens for ItemUse {
1332 fn to_tokens(&self, tokens: &mut Tokens) {
1333 tokens.append_all(self.attrs.outer());
1334 self.vis.to_tokens(tokens);
1335 self.use_token.to_tokens(tokens);
1336 self.path.to_tokens(tokens);
1337 self.semi_token.to_tokens(tokens);
1338 }
1339 }
1340
1341 impl ToTokens for ItemStatic {
1342 fn to_tokens(&self, tokens: &mut Tokens) {
1343 tokens.append_all(self.attrs.outer());
1344 self.vis.to_tokens(tokens);
1345 self.static_token.to_tokens(tokens);
1346 self.mutbl.to_tokens(tokens);
1347 self.ident.to_tokens(tokens);
1348 self.colon_token.to_tokens(tokens);
1349 self.ty.to_tokens(tokens);
1350 self.eq_token.to_tokens(tokens);
1351 self.expr.to_tokens(tokens);
1352 self.semi_token.to_tokens(tokens);
1353 }
1354 }
1355
1356 impl ToTokens for ItemConst {
1357 fn to_tokens(&self, tokens: &mut Tokens) {
1358 tokens.append_all(self.attrs.outer());
1359 self.vis.to_tokens(tokens);
1360 self.const_token.to_tokens(tokens);
1361 self.ident.to_tokens(tokens);
1362 self.colon_token.to_tokens(tokens);
1363 self.ty.to_tokens(tokens);
1364 self.eq_token.to_tokens(tokens);
1365 self.expr.to_tokens(tokens);
1366 self.semi_token.to_tokens(tokens);
1367 }
1368 }
1369
1370 impl ToTokens for ItemFn {
1371 fn to_tokens(&self, tokens: &mut Tokens) {
1372 tokens.append_all(self.attrs.outer());
1373 self.vis.to_tokens(tokens);
1374 self.constness.to_tokens(tokens);
1375 self.unsafety.to_tokens(tokens);
1376 self.abi.to_tokens(tokens);
1377 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1378 self.block.brace_token.surround(tokens, |tokens| {
1379 tokens.append_all(self.attrs.inner());
1380 tokens.append_all(&self.block.stmts);
1381 });
1382 }
1383 }
1384
1385 impl ToTokens for ItemMod {
1386 fn to_tokens(&self, tokens: &mut Tokens) {
1387 tokens.append_all(self.attrs.outer());
1388 self.vis.to_tokens(tokens);
1389 self.mod_token.to_tokens(tokens);
1390 self.ident.to_tokens(tokens);
1391 if let Some((ref brace, ref items)) = self.content {
1392 brace.surround(tokens, |tokens| {
1393 tokens.append_all(self.attrs.inner());
1394 tokens.append_all(items);
1395 });
1396 } else {
1397 TokensOrDefault(&self.semi).to_tokens(tokens);
1398 }
1399 }
1400 }
1401
1402 impl ToTokens for ItemForeignMod {
1403 fn to_tokens(&self, tokens: &mut Tokens) {
1404 tokens.append_all(self.attrs.outer());
1405 self.abi.to_tokens(tokens);
1406 self.brace_token.surround(tokens, |tokens| {
1407 tokens.append_all(&self.items);
1408 });
1409 }
1410 }
1411
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001412 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001413 fn to_tokens(&self, tokens: &mut Tokens) {
1414 tokens.append_all(self.attrs.outer());
1415 self.vis.to_tokens(tokens);
1416 self.type_token.to_tokens(tokens);
1417 self.ident.to_tokens(tokens);
1418 self.generics.to_tokens(tokens);
1419 self.generics.where_clause.to_tokens(tokens);
1420 self.eq_token.to_tokens(tokens);
1421 self.ty.to_tokens(tokens);
1422 self.semi_token.to_tokens(tokens);
1423 }
1424 }
1425
1426 impl ToTokens for ItemEnum {
1427 fn to_tokens(&self, tokens: &mut Tokens) {
1428 tokens.append_all(self.attrs.outer());
1429 self.vis.to_tokens(tokens);
1430 self.enum_token.to_tokens(tokens);
1431 self.ident.to_tokens(tokens);
1432 self.generics.to_tokens(tokens);
1433 self.generics.where_clause.to_tokens(tokens);
1434 self.brace_token.surround(tokens, |tokens| {
1435 self.variants.to_tokens(tokens);
1436 });
1437 }
1438 }
1439
1440 impl ToTokens for ItemStruct {
1441 fn to_tokens(&self, tokens: &mut Tokens) {
1442 tokens.append_all(self.attrs.outer());
1443 self.vis.to_tokens(tokens);
1444 self.struct_token.to_tokens(tokens);
1445 self.ident.to_tokens(tokens);
1446 self.generics.to_tokens(tokens);
1447 match self.data {
1448 VariantData::Struct(..) => {
1449 self.generics.where_clause.to_tokens(tokens);
1450 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001451 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001452 VariantData::Tuple(..) => {
1453 self.data.to_tokens(tokens);
1454 self.generics.where_clause.to_tokens(tokens);
1455 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001456 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001457 VariantData::Unit => {
1458 self.generics.where_clause.to_tokens(tokens);
1459 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001460 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001461 }
1462 }
1463 }
1464
1465 impl ToTokens for ItemUnion {
1466 fn to_tokens(&self, tokens: &mut Tokens) {
1467 tokens.append_all(self.attrs.outer());
1468 self.vis.to_tokens(tokens);
1469 self.union_token.to_tokens(tokens);
1470 self.ident.to_tokens(tokens);
1471 self.generics.to_tokens(tokens);
1472 self.generics.where_clause.to_tokens(tokens);
1473 // XXX: Should we handle / complain when using a
1474 // non-VariantData::Struct Variant in Union?
1475 self.data.to_tokens(tokens);
1476 }
1477 }
1478
1479 impl ToTokens for ItemTrait {
1480 fn to_tokens(&self, tokens: &mut Tokens) {
1481 tokens.append_all(self.attrs.outer());
1482 self.vis.to_tokens(tokens);
1483 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001484 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001485 self.trait_token.to_tokens(tokens);
1486 self.ident.to_tokens(tokens);
1487 self.generics.to_tokens(tokens);
1488 if !self.supertraits.is_empty() {
1489 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1490 self.supertraits.to_tokens(tokens);
1491 }
1492 self.generics.where_clause.to_tokens(tokens);
1493 self.brace_token.surround(tokens, |tokens| {
1494 tokens.append_all(&self.items);
1495 });
1496 }
1497 }
1498
1499 impl ToTokens for ItemDefaultImpl {
1500 fn to_tokens(&self, tokens: &mut Tokens) {
1501 tokens.append_all(self.attrs.outer());
1502 self.unsafety.to_tokens(tokens);
1503 self.impl_token.to_tokens(tokens);
1504 self.path.to_tokens(tokens);
1505 self.for_token.to_tokens(tokens);
1506 self.dot2_token.to_tokens(tokens);
1507 self.brace_token.surround(tokens, |_tokens| {});
1508 }
1509 }
1510
1511 impl ToTokens for ItemImpl {
1512 fn to_tokens(&self, tokens: &mut Tokens) {
1513 tokens.append_all(self.attrs.outer());
1514 self.defaultness.to_tokens(tokens);
1515 self.unsafety.to_tokens(tokens);
1516 self.impl_token.to_tokens(tokens);
1517 self.generics.to_tokens(tokens);
1518 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1519 polarity.to_tokens(tokens);
1520 path.to_tokens(tokens);
1521 for_token.to_tokens(tokens);
1522 }
1523 self.self_ty.to_tokens(tokens);
1524 self.generics.where_clause.to_tokens(tokens);
1525 self.brace_token.surround(tokens, |tokens| {
1526 tokens.append_all(&self.items);
1527 });
1528 }
1529 }
1530
1531 impl ToTokens for ItemMacro {
1532 fn to_tokens(&self, tokens: &mut Tokens) {
1533 tokens.append_all(self.attrs.outer());
1534 self.mac.path.to_tokens(tokens);
1535 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001536 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001537 tokens.append_all(&self.mac.tokens);
1538 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001539 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001540 }
1541 }
1542 }
David Tolnay42602292016-10-01 22:25:45 -07001543
Alex Crichton62a0a592017-05-22 13:58:53 -07001544 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001545 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001546 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001547 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001548 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001549 self.rename.to_tokens(tokens);
1550 }
David Tolnay4a057422016-10-08 00:02:31 -07001551 }
1552 }
1553
Alex Crichton62a0a592017-05-22 13:58:53 -07001554 impl ToTokens for PathGlob {
1555 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001556 if self.path.segments.len() > 0 {
1557 self.path.to_tokens(tokens);
1558 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1559 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001560 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001561 }
1562 }
1563
1564 impl ToTokens for PathList {
1565 fn to_tokens(&self, tokens: &mut Tokens) {
1566 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001567 self.colon2_token.to_tokens(tokens);
1568 self.brace_token.surround(tokens, |tokens| {
1569 self.items.to_tokens(tokens);
1570 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001571 }
1572 }
1573
David Tolnay4a057422016-10-08 00:02:31 -07001574 impl ToTokens for PathListItem {
1575 fn to_tokens(&self, tokens: &mut Tokens) {
1576 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001577 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001578 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001579 self.rename.to_tokens(tokens);
1580 }
David Tolnay4a057422016-10-08 00:02:31 -07001581 }
1582 }
1583
David Tolnay1bfa7332017-11-11 12:41:20 -08001584 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001585 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001586 tokens.append_all(self.attrs.outer());
1587 self.const_token.to_tokens(tokens);
1588 self.ident.to_tokens(tokens);
1589 self.colon_token.to_tokens(tokens);
1590 self.ty.to_tokens(tokens);
1591 if let Some((ref eq_token, ref default)) = self.default {
1592 eq_token.to_tokens(tokens);
1593 default.to_tokens(tokens);
1594 }
1595 self.semi_token.to_tokens(tokens);
1596 }
1597 }
1598
1599 impl ToTokens for TraitItemMethod {
1600 fn to_tokens(&self, tokens: &mut Tokens) {
1601 tokens.append_all(self.attrs.outer());
1602 self.sig.to_tokens(tokens);
1603 match self.default {
1604 Some(ref block) => {
1605 block.brace_token.surround(tokens, |tokens| {
1606 tokens.append_all(self.attrs.inner());
1607 tokens.append_all(&block.stmts);
1608 });
David Tolnayca085422016-10-04 00:12:38 -07001609 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001610 None => {
1611 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001612 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001613 }
1614 }
1615 }
1616
1617 impl ToTokens for TraitItemType {
1618 fn to_tokens(&self, tokens: &mut Tokens) {
1619 tokens.append_all(self.attrs.outer());
1620 self.type_token.to_tokens(tokens);
1621 self.ident.to_tokens(tokens);
1622 if !self.bounds.is_empty() {
1623 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1624 self.bounds.to_tokens(tokens);
1625 }
1626 if let Some((ref eq_token, ref default)) = self.default {
1627 eq_token.to_tokens(tokens);
1628 default.to_tokens(tokens);
1629 }
1630 self.semi_token.to_tokens(tokens);
1631 }
1632 }
1633
1634 impl ToTokens for TraitItemMacro {
1635 fn to_tokens(&self, tokens: &mut Tokens) {
1636 tokens.append_all(self.attrs.outer());
1637 self.mac.to_tokens(tokens);
1638 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001639 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001640 }
1641 }
1642 }
1643
David Tolnay857628c2017-11-11 12:25:31 -08001644 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001645 fn to_tokens(&self, tokens: &mut Tokens) {
1646 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001647 self.vis.to_tokens(tokens);
1648 self.defaultness.to_tokens(tokens);
1649 self.const_token.to_tokens(tokens);
1650 self.ident.to_tokens(tokens);
1651 self.colon_token.to_tokens(tokens);
1652 self.ty.to_tokens(tokens);
1653 self.eq_token.to_tokens(tokens);
1654 self.expr.to_tokens(tokens);
1655 self.semi_token.to_tokens(tokens);
1656 }
1657 }
1658
1659 impl ToTokens for ImplItemMethod {
1660 fn to_tokens(&self, tokens: &mut Tokens) {
1661 tokens.append_all(self.attrs.outer());
1662 self.vis.to_tokens(tokens);
1663 self.defaultness.to_tokens(tokens);
1664 self.sig.to_tokens(tokens);
1665 self.block.brace_token.surround(tokens, |tokens| {
1666 tokens.append_all(self.attrs.inner());
1667 tokens.append_all(&self.block.stmts);
1668 });
1669 }
1670 }
1671
1672 impl ToTokens for ImplItemType {
1673 fn to_tokens(&self, tokens: &mut Tokens) {
1674 tokens.append_all(self.attrs.outer());
1675 self.vis.to_tokens(tokens);
1676 self.defaultness.to_tokens(tokens);
1677 self.type_token.to_tokens(tokens);
1678 self.ident.to_tokens(tokens);
1679 self.eq_token.to_tokens(tokens);
1680 self.ty.to_tokens(tokens);
1681 self.semi_token.to_tokens(tokens);
1682 }
1683 }
1684
1685 impl ToTokens for ImplItemMacro {
1686 fn to_tokens(&self, tokens: &mut Tokens) {
1687 tokens.append_all(self.attrs.outer());
1688 self.mac.to_tokens(tokens);
1689 if !self.mac.is_braced() {
1690 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001691 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001692 }
1693 }
1694 }
1695
David Tolnay8894f602017-11-11 12:11:04 -08001696 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001697 fn to_tokens(&self, tokens: &mut Tokens) {
1698 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001699 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001700 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1701 self.semi_token.to_tokens(tokens);
1702 }
1703 }
1704
1705 impl ToTokens for ForeignItemStatic {
1706 fn to_tokens(&self, tokens: &mut Tokens) {
1707 tokens.append_all(self.attrs.outer());
1708 self.vis.to_tokens(tokens);
1709 self.static_token.to_tokens(tokens);
1710 self.mutbl.to_tokens(tokens);
1711 self.ident.to_tokens(tokens);
1712 self.colon_token.to_tokens(tokens);
1713 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001714 self.semi_token.to_tokens(tokens);
1715 }
1716 }
1717
David Tolnay199bcbb2017-11-12 10:33:52 -08001718 impl ToTokens for ForeignItemType {
1719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 tokens.append_all(self.attrs.outer());
1721 self.vis.to_tokens(tokens);
1722 self.type_token.to_tokens(tokens);
1723 self.ident.to_tokens(tokens);
1724 self.semi_token.to_tokens(tokens);
1725 }
1726 }
1727
David Tolnay570695e2017-06-03 16:15:13 -07001728 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001729 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001730 self.constness.to_tokens(tokens);
1731 self.unsafety.to_tokens(tokens);
1732 self.abi.to_tokens(tokens);
1733 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001734 }
1735 }
1736
David Tolnay570695e2017-06-03 16:15:13 -07001737 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001738
1739 impl<'a> ToTokens for NamedDecl<'a> {
1740 fn to_tokens(&self, tokens: &mut Tokens) {
1741 self.0.fn_token.to_tokens(tokens);
1742 self.1.to_tokens(tokens);
1743 self.0.generics.to_tokens(tokens);
1744 self.0.paren_token.surround(tokens, |tokens| {
1745 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001746
1747 if self.0.variadic {
1748 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001749 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001750 }
Alex Crichton259ee532017-07-14 06:51:02 -07001751 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001752 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001753 });
1754 self.0.output.to_tokens(tokens);
1755 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001756 }
1757 }
1758
Alex Crichton62a0a592017-05-22 13:58:53 -07001759 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001760 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001761 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001762 self.lifetime.to_tokens(tokens);
1763 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001764 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001765 }
1766 }
1767
1768 impl ToTokens for ArgSelf {
1769 fn to_tokens(&self, tokens: &mut Tokens) {
1770 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001771 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001772 }
1773 }
1774
1775 impl ToTokens for ArgCaptured {
1776 fn to_tokens(&self, tokens: &mut Tokens) {
1777 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001778 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001779 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001780 }
1781 }
1782
David Tolnay42602292016-10-01 22:25:45 -07001783 impl ToTokens for Constness {
1784 fn to_tokens(&self, tokens: &mut Tokens) {
1785 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001786 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001787 Constness::NotConst => {
1788 // nothing
1789 }
David Tolnay42602292016-10-01 22:25:45 -07001790 }
1791 }
1792 }
1793
David Tolnay4c9be372016-10-06 00:47:37 -07001794 impl ToTokens for Defaultness {
1795 fn to_tokens(&self, tokens: &mut Tokens) {
1796 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001797 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001798 Defaultness::Final => {
1799 // nothing
1800 }
1801 }
1802 }
1803 }
1804
1805 impl ToTokens for ImplPolarity {
1806 fn to_tokens(&self, tokens: &mut Tokens) {
1807 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001808 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001809 ImplPolarity::Positive => {
1810 // nothing
1811 }
1812 }
1813 }
1814 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001815}