blob: 5467f26df1d3b0f6c70e7ccfd5721d098b3b3d34 [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],
David Tolnay5f332a92017-12-26 00:42:45 -050026 pub leading_colon: Option<Token![::]>,
27 pub prefix: Delimited<Ident, Token![::]>,
28 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
31 /// A static item (`static` or `pub static`).
32 ///
33 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
34 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -070038 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080041 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070043 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080044 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070045 }),
46 /// A constant item (`const` or `pub const`).
47 ///
48 /// E.g. `const FOO: i32 = 42;`
49 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080050 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070051 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070053 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080055 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070057 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080058 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070059 }),
60 /// A function declaration (`fn` or `pub fn`).
61 ///
62 /// E.g. `fn foo(bar: usize) -> usize { .. }`
63 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080064 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070065 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -070067 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070069 pub decl: Box<FnDecl>,
70 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub block: Box<Block>,
72 }),
73 /// A module declaration (`mod` or `pub mod`).
74 ///
75 /// E.g. `mod foo;` or `mod foo { .. }`
76 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080077 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070078 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080079 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070080 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050081 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070083 }),
84 /// An external module (`extern` or `pub extern`).
85 ///
86 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070087 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080088 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070089 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050090 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091 pub items: Vec<ForeignItem>,
92 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070093 /// A type alias (`type` or `pub type`).
94 ///
95 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -080096 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -080097 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700100 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800103 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800104 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 }),
106 /// An enum definition (`enum` or `pub enum`).
107 ///
108 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
109 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800110 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700111 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700113 pub ident: Ident,
114 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500115 pub brace_token: token::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800116 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700117 }),
118 /// A struct definition (`struct` or `pub struct`).
119 ///
120 /// E.g. `struct Foo<A> { x: A }`
121 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800122 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700123 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 pub struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700125 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700127 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 }),
130 /// A union definition (`union` or `pub union`).
131 ///
132 /// E.g. `union Foo<A, B> { x: A, y: B }`
133 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800134 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700135 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700137 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700139 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 }),
141 /// A Trait declaration (`trait` or `pub trait`).
142 ///
143 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
144 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800145 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700146 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 pub unsafety: Unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500148 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700150 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700151 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800153 pub supertraits: Delimited<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500154 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 pub items: Vec<TraitItem>,
156 }),
157 /// Default trait implementation.
158 ///
159 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
160 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800161 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800163 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700164 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800165 pub for_token: Token![for],
166 pub dot2_token: Token![..],
David Tolnay32954ef2017-12-26 22:43:16 -0500167 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 }),
169 /// An implementation.
170 ///
171 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
172 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800173 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700174 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800176 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700178 /// Trait this impl implements.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800179 pub trait_: Option<(ImplPolarity, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700180 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800181 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500182 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub items: Vec<ImplItem>,
184 }),
185 /// A macro invocation (which includes macro definition).
186 ///
187 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800188 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800189 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800190 /// The `example` in `macro_rules! example { ... }`.
191 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800192 pub mac: Macro,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800193 }),
David Tolnay500d8322017-12-18 00:32:51 -0800194 /// FIXME will need to revisit what this looks like in the AST.
195 pub Macro2(ItemMacro2 {
196 pub attrs: Vec<Attribute>,
197 pub vis: Visibility,
198 pub macro_token: Token![macro],
199 pub ident: Ident,
200 pub args: TokenTree,
201 pub body: TokenTree,
202 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 }
David Tolnayb79ee962016-09-04 09:39:20 -0700204}
205
David Tolnay0e837402016-12-22 17:25:55 -0500206impl From<DeriveInput> for Item {
207 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800208 match input.body {
209 Body::Enum(data) => {
210 Item::Enum(ItemEnum {
211 attrs: input.attrs,
212 vis: input.vis,
213 enum_token: data.enum_token,
214 ident: input.ident,
215 generics: input.generics,
216 brace_token: data.brace_token,
217 variants: data.variants,
218 })
219 }
220 Body::Struct(data) => {
221 Item::Struct(ItemStruct {
222 attrs: input.attrs,
223 vis: input.vis,
224 struct_token: data.struct_token,
225 ident: input.ident,
226 generics: input.generics,
227 data: data.data,
228 semi_token: data.semi_token,
229 })
230 }
David Tolnay453cfd12016-10-23 11:00:14 -0700231 }
232 }
233}
234
Alex Crichton62a0a592017-05-22 13:58:53 -0700235ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500236 /// Things that can appear directly inside of a module.
237 pub enum UseTree {
238 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
239 pub Path(UsePath {
240 pub ident: Ident,
241 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500243 /// `use prefix::*`
244 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800245 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500247 /// `use prefix::{a, b, c}`
248 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500249 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500250 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 }),
252 }
253}
254
Alex Crichton62a0a592017-05-22 13:58:53 -0700255ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700256 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700257 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 NotConst,
260 }
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 Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800266 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 Final,
268 }
269}
270
Alex Crichton62a0a592017-05-22 13:58:53 -0700271ast_enum_of_structs! {
272 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800273 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 /// A foreign function
275 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800276 pub attrs: Vec<Attribute>,
277 pub vis: Visibility,
278 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800280 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 }),
282 /// A foreign static item (`static ext: u8`)
283 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800284 pub attrs: Vec<Attribute>,
285 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800286 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700287 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800288 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800289 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800290 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800291 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800293 /// A foreign type
294 pub Type(ForeignItemType {
295 pub attrs: Vec<Attribute>,
296 pub vis: Visibility,
297 pub type_token: Token![type],
298 pub ident: Ident,
299 pub semi_token: Token![;],
300 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700301 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700302}
303
David Tolnayda705bd2017-11-10 21:58:05 -0800304ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// Represents an item declaration within a trait declaration,
306 /// possibly including a default implementation. A trait item is
307 /// either required (meaning it doesn't have an implementation, just a
308 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800309 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700310 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800311 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800312 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700313 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800314 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800315 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800316 pub default: Option<(Token![=], Expr)>,
317 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 }),
319 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800320 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700321 pub sig: MethodSig,
322 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800323 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
325 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800326 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800327 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700328 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500329 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800330 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800331 pub bounds: Delimited<TypeParamBound, Token![+]>,
332 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800333 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700334 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800335 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800336 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800337 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800338 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700340}
341
342ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700343 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700344 pub enum ImplPolarity {
345 /// `impl Trait for Type`
346 Positive,
347 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800348 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 }
350}
351
Alex Crichton62a0a592017-05-22 13:58:53 -0700352ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800353 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800355 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700356 pub vis: Visibility,
357 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700359 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800361 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800362 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700365 }),
366 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800367 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700368 pub vis: Visibility,
369 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 pub sig: MethodSig,
371 pub block: Block,
372 }),
373 pub Type(ImplItemType {
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,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700378 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500379 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800380 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800381 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800382 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 }),
David Tolnay857628c2017-11-11 12:25:31 -0800384 pub Macro(ImplItemMacro {
385 pub attrs: Vec<Attribute>,
386 pub mac: Macro,
387 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700389}
390
391ast_struct! {
392 /// Represents a method's signature in a trait declaration,
393 /// or in an implementation.
394 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700396 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700398 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 }
401}
402
403ast_struct! {
404 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700405 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 /// E.g. `fn foo(bar: baz)`
407 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800408 pub fn_token: Token![fn],
David Tolnay32954ef2017-12-26 22:43:16 -0500409 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800410 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800411 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700412 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700416}
417
Alex Crichton62a0a592017-05-22 13:58:53 -0700418ast_enum_of_structs! {
419 /// An argument in a function header.
420 ///
421 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
422 pub enum FnArg {
423 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800424 pub and_token: Token![&],
425 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub lifetime: Option<Lifetime>,
427 pub mutbl: Mutability,
428 }),
429 pub SelfValue(ArgSelf {
430 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800431 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 }),
433 pub Captured(ArgCaptured {
434 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800436 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700437 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800438 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 }
David Tolnay62f374c2016-10-02 13:37:00 -0700440}
441
David Tolnayedf2b992016-09-23 20:43:45 -0700442#[cfg(feature = "parsing")]
443pub mod parsing {
444 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700445
Michael Layzell92639a52017-06-01 00:07:44 -0400446 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700447
David Tolnay4c614be2017-11-10 00:02:38 -0800448 impl_synom!(Item "item" alt!(
449 syn!(ItemExternCrate) => { Item::ExternCrate }
450 |
451 syn!(ItemUse) => { Item::Use }
452 |
453 syn!(ItemStatic) => { Item::Static }
454 |
455 syn!(ItemConst) => { Item::Const }
456 |
457 syn!(ItemFn) => { Item::Fn }
458 |
459 syn!(ItemMod) => { Item::Mod }
460 |
461 syn!(ItemForeignMod) => { Item::ForeignMod }
462 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800463 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800464 |
465 syn!(ItemStruct) => { Item::Struct }
466 |
467 syn!(ItemEnum) => { Item::Enum }
468 |
469 syn!(ItemUnion) => { Item::Union }
470 |
471 syn!(ItemTrait) => { Item::Trait }
472 |
473 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
474 |
475 syn!(ItemImpl) => { Item::Impl }
476 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800477 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800478 |
479 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800480 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700481
David Tolnaydecf28d2017-11-11 11:56:45 -0800482 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700483 attrs: many0!(call!(Attribute::parse_outer)) >>
484 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800485 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700486 ident: option!(syn!(Ident)) >>
David Tolnay500d8322017-12-18 00:32:51 -0800487 body: call!(TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800488 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800489 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700490 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800491 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800492 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500493 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700494 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700495 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800496 },
David Tolnay4c614be2017-11-10 00:02:38 -0800497 })
David Tolnayedf2b992016-09-23 20:43:45 -0700498 ));
499
David Tolnay500d8322017-12-18 00:32:51 -0800500 // TODO: figure out the actual grammar; is body required to be braced?
501 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
502 attrs: many0!(call!(Attribute::parse_outer)) >>
503 vis: syn!(Visibility) >>
504 macro_: keyword!(macro) >>
505 ident: syn!(Ident) >>
506 args: call!(TokenTree::parse_delimited) >>
507 body: call!(TokenTree::parse_delimited) >>
508 (ItemMacro2 {
509 attrs: attrs,
510 vis: vis,
511 macro_token: macro_,
512 ident: ident,
513 args: args,
514 body: body,
515 })
516 ));
517
David Tolnay4c614be2017-11-10 00:02:38 -0800518 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700519 attrs: many0!(call!(Attribute::parse_outer)) >>
520 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800521 extern_: keyword!(extern) >>
522 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700523 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800524 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
525 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800526 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700527 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800528 vis: vis,
529 extern_token: extern_,
530 crate_token: crate_,
531 ident: ident,
532 rename: rename,
533 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800534 })
David Tolnayedf2b992016-09-23 20:43:45 -0700535 ));
536
David Tolnay4c614be2017-11-10 00:02:38 -0800537 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700538 attrs: many0!(call!(Attribute::parse_outer)) >>
539 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800540 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500541 leading_colon: option!(punct!(::)) >>
542 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
543 tree: switch!(value!(leading_colon.is_none() && prefix.is_empty()),
544 true => syn!(UseTree)
545 |
546 false => switch!(value!(prefix.empty_or_trailing()),
547 true => syn!(UseTree)
548 |
549 false => alt!(
550 tuple!(keyword!(as), syn!(Ident)) => {
551 |rename| UseTree::Path(UsePath {
552 ident: prefix.pop().unwrap().into_item(),
553 rename: Some(rename),
554 })
555 }
556 |
557 epsilon!() => {
558 |_| UseTree::Path(UsePath {
559 ident: prefix.pop().unwrap().into_item(),
560 rename: None,
561 })
562 }
563 )
564 )
565 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800566 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800567 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700568 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800569 vis: vis,
570 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500571 leading_colon: leading_colon,
572 prefix: prefix,
573 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800574 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800575 })
David Tolnay4a057422016-10-08 00:02:31 -0700576 ));
577
David Tolnay5f332a92017-12-26 00:42:45 -0500578 named!(use_prefix -> Ident, alt!(
579 syn!(Ident)
580 |
581 keyword!(self) => { Into::into }
582 |
583 keyword!(super) => { Into::into }
584 |
585 keyword!(crate) => { Into::into }
586 ));
587
588 impl_synom!(UseTree "use tree" alt!(
589 syn!(UsePath) => { UseTree::Path }
590 |
591 syn!(UseGlob) => { UseTree::Glob }
592 |
593 syn!(UseList) => { UseTree::List }
594 ));
595
596 impl_synom!(UsePath "use path" do_parse!(
597 ident: alt!(
598 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400599 |
David Tolnay5f332a92017-12-26 00:42:45 -0500600 keyword!(self) => { Into::into }
601 ) >>
602 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
603 (UsePath {
604 ident: ident,
605 rename: rename,
606 })
607 ));
David Tolnay4a057422016-10-08 00:02:31 -0700608
David Tolnay5f332a92017-12-26 00:42:45 -0500609 impl_synom!(UseGlob "use glob" do_parse!(
610 star: punct!(*) >>
611 (UseGlob {
612 star_token: star,
613 })
614 ));
David Tolnay4a057422016-10-08 00:02:31 -0700615
David Tolnay5f332a92017-12-26 00:42:45 -0500616 impl_synom!(UseList "use list" do_parse!(
617 list: braces!(Delimited::parse_terminated) >>
618 (UseList {
619 brace_token: list.1,
620 items: list.0,
621 })
622 ));
David Tolnay4a057422016-10-08 00:02:31 -0700623
David Tolnay4c614be2017-11-10 00:02:38 -0800624 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700625 attrs: many0!(call!(Attribute::parse_outer)) >>
626 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700628 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700629 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800630 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800631 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800632 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700633 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800634 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800635 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700636 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800637 vis: vis,
638 static_token: static_,
639 mutbl: mutability,
640 ident: ident,
641 colon_token: colon,
642 ty: Box::new(ty),
643 eq_token: eq,
644 expr: Box::new(value),
645 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800646 })
David Tolnay47a877c2016-10-01 16:50:55 -0700647 ));
648
David Tolnay4c614be2017-11-10 00:02:38 -0800649 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700650 attrs: many0!(call!(Attribute::parse_outer)) >>
651 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800652 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700653 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800654 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800655 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800656 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700657 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800658 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800659 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700660 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800661 vis: vis,
662 const_token: const_,
663 ident: ident,
664 colon_token: colon,
665 ty: Box::new(ty),
666 eq_token: eq,
667 expr: Box::new(value),
668 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800669 })
David Tolnay47a877c2016-10-01 16:50:55 -0700670 ));
671
David Tolnay4c614be2017-11-10 00:02:38 -0800672 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700673 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
674 vis: syn!(Visibility) >>
675 constness: syn!(Constness) >>
676 unsafety: syn!(Unsafety) >>
677 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800678 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700679 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700680 generics: syn!(Generics) >>
681 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800682 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700683 where_clause: syn!(WhereClause) >>
684 inner_attrs_stmts: braces!(tuple!(
685 many0!(call!(Attribute::parse_inner)),
686 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400687 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800688 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700689 attrs: {
690 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700691 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700692 attrs
693 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800694 vis: vis,
695 constness: constness,
696 unsafety: unsafety,
697 abi: abi,
698 decl: Box::new(FnDecl {
699 dot_tokens: None,
700 fn_token: fn_,
701 paren_token: inputs.1,
702 inputs: inputs.0,
703 output: ret,
704 variadic: false,
705 generics: Generics {
706 where_clause: where_clause,
707 .. generics
708 },
709 }),
710 ident: ident,
711 block: Box::new(Block {
712 brace_token: inner_attrs_stmts.1,
713 stmts: (inner_attrs_stmts.0).1,
714 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800715 })
David Tolnay42602292016-10-01 22:25:45 -0700716 ));
717
Alex Crichton954046c2017-05-30 21:49:42 -0700718 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400719 named!(parse -> Self, alt!(
720 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800721 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400722 lt: option!(syn!(Lifetime)) >>
723 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800724 self_: keyword!(self) >>
725 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400726 (ArgSelfRef {
727 lifetime: lt,
728 mutbl: mutability,
729 and_token: and,
730 self_token: self_,
731 }.into())
732 )
733 |
734 do_parse!(
735 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800736 self_: keyword!(self) >>
737 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400738 (ArgSelf {
739 mutbl: mutability,
740 self_token: self_,
741 }.into())
742 )
743 |
744 do_parse!(
745 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800746 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800747 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400748 (ArgCaptured {
749 pat: pat,
750 ty: ty,
751 colon_token: colon,
752 }.into())
753 )
754 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800755 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400756 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700757 }
David Tolnay62f374c2016-10-02 13:37:00 -0700758
David Tolnay4c614be2017-11-10 00:02:38 -0800759 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700760 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
761 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800762 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700763 ident: syn!(Ident) >>
764 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800765 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700766 Vec::new(),
767 None,
768 Some(semi),
769 )}
David Tolnay37d10332016-10-13 20:51:04 -0700770 |
Alex Crichton954046c2017-05-30 21:49:42 -0700771 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700772 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700773 many0!(call!(Attribute::parse_inner)),
774 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400775 )
David Tolnay570695e2017-06-03 16:15:13 -0700776 ) => {|((inner_attrs, items), brace)| (
777 inner_attrs,
778 Some((brace, items)),
779 None,
780 )}
David Tolnay37d10332016-10-13 20:51:04 -0700781 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800782 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700783 attrs: {
784 let mut attrs = outer_attrs;
785 attrs.extend(content_semi.0);
786 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700787 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800788 vis: vis,
789 mod_token: mod_,
790 ident: ident,
791 content: content_semi.1,
792 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800793 })
David Tolnay35902302016-10-06 01:11:08 -0700794 ));
795
David Tolnay4c614be2017-11-10 00:02:38 -0800796 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700797 attrs: many0!(call!(Attribute::parse_outer)) >>
798 abi: syn!(Abi) >>
799 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800800 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700801 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800802 abi: abi,
803 brace_token: items.1,
804 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800805 })
David Tolnay35902302016-10-06 01:11:08 -0700806 ));
807
David Tolnay8894f602017-11-11 12:11:04 -0800808 impl_synom!(ForeignItem "foreign item" alt!(
809 syn!(ForeignItemFn) => { ForeignItem::Fn }
810 |
811 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800812 |
813 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800814 ));
David Tolnay35902302016-10-06 01:11:08 -0700815
David Tolnay8894f602017-11-11 12:11:04 -0800816 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700817 attrs: many0!(call!(Attribute::parse_outer)) >>
818 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800819 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700820 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700821 generics: syn!(Generics) >>
822 inputs: parens!(do_parse!(
823 args: call!(Delimited::parse_terminated) >>
824 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800825 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700826 (args, variadic)
827 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800828 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700829 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800830 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700831 ({
832 let ((inputs, variadic), parens) = inputs;
833 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800834 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700835 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700836 attrs: attrs,
837 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800838 decl: Box::new(FnDecl {
839 fn_token: fn_,
840 paren_token: parens,
841 inputs: inputs,
842 variadic: variadic.is_some(),
843 dot_tokens: variadic,
844 output: ret,
845 generics: Generics {
846 where_clause: where_clause,
847 .. generics
848 },
849 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700850 vis: vis,
851 }
David Tolnay35902302016-10-06 01:11:08 -0700852 })
853 ));
854
David Tolnay8894f602017-11-11 12:11:04 -0800855 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700856 attrs: many0!(call!(Attribute::parse_outer)) >>
857 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800858 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700859 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700860 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800862 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800863 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800864 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700865 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700866 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700867 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800868 ty: Box::new(ty),
869 mutbl: mutability,
870 static_token: static_,
871 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700872 vis: vis,
873 })
874 ));
875
David Tolnay199bcbb2017-11-12 10:33:52 -0800876 impl_synom!(ForeignItemType "foreign type" do_parse!(
877 attrs: many0!(call!(Attribute::parse_outer)) >>
878 vis: syn!(Visibility) >>
879 type_: keyword!(type) >>
880 ident: syn!(Ident) >>
881 semi: punct!(;) >>
882 (ForeignItemType {
883 attrs: attrs,
884 vis: vis,
885 type_token: type_,
886 ident: ident,
887 semi_token: semi,
888 })
889 ));
890
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800891 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700892 attrs: many0!(call!(Attribute::parse_outer)) >>
893 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800894 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700895 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700896 generics: syn!(Generics) >>
897 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800898 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800899 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800900 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800901 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700902 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800903 vis: vis,
904 type_token: type_,
905 ident: ident,
906 generics: Generics {
907 where_clause: where_clause,
908 ..generics
909 },
910 eq_token: eq,
911 ty: Box::new(ty),
912 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800913 })
David Tolnay3cf52982016-10-01 17:11:37 -0700914 ));
915
David Tolnay4c614be2017-11-10 00:02:38 -0800916 impl_synom!(ItemStruct "struct item" switch!(
917 map!(syn!(DeriveInput), Into::into),
918 Item::Struct(item) => value!(item)
919 |
920 _ => reject!()
921 ));
David Tolnay42602292016-10-01 22:25:45 -0700922
David Tolnay4c614be2017-11-10 00:02:38 -0800923 impl_synom!(ItemEnum "enum item" switch!(
924 map!(syn!(DeriveInput), Into::into),
925 Item::Enum(item) => value!(item)
926 |
927 _ => reject!()
928 ));
929
930 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700931 attrs: many0!(call!(Attribute::parse_outer)) >>
932 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700934 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700935 generics: syn!(Generics) >>
936 where_clause: syn!(WhereClause) >>
937 fields: braces!(call!(Delimited::parse_terminated_with,
938 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800939 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700940 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800941 vis: vis,
942 union_token: union_,
943 ident: ident,
944 generics: Generics {
945 where_clause: where_clause,
946 .. generics
947 },
948 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800949 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700950 ));
951
David Tolnay4c614be2017-11-10 00:02:38 -0800952 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700953 attrs: many0!(call!(Attribute::parse_outer)) >>
954 vis: syn!(Visibility) >>
955 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500956 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700958 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700959 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800960 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700961 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700962 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700963 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700964 where_clause: syn!(WhereClause) >>
965 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800966 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700967 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800968 vis: vis,
969 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500970 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800971 trait_token: trait_,
972 ident: ident,
973 generics: Generics {
974 where_clause: where_clause,
975 .. generics
976 },
977 colon_token: colon,
978 supertraits: bounds.unwrap_or_default(),
979 brace_token: body.1,
980 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800981 })
David Tolnay0aecb732016-10-03 23:03:50 -0700982 ));
983
David Tolnay4c614be2017-11-10 00:02:38 -0800984 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700985 attrs: many0!(call!(Attribute::parse_outer)) >>
986 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800987 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700988 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800989 for_: keyword!(for) >>
990 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700991 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800992 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700993 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800994 unsafety: unsafety,
995 impl_token: impl_,
996 path: path,
997 for_token: for_,
998 dot2_token: dot2,
999 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001000 })
David Tolnayf94e2362016-10-04 00:29:51 -07001001 ));
1002
David Tolnayda705bd2017-11-10 21:58:05 -08001003 impl_synom!(TraitItem "trait item" alt!(
1004 syn!(TraitItemConst) => { TraitItem::Const }
1005 |
1006 syn!(TraitItemMethod) => { TraitItem::Method }
1007 |
1008 syn!(TraitItemType) => { TraitItem::Type }
1009 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001010 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001011 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001012
David Tolnayda705bd2017-11-10 21:58:05 -08001013 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001014 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001016 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001018 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001019 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1020 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001021 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001022 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001023 const_token: const_,
1024 ident: ident,
1025 colon_token: colon,
1026 ty: ty,
1027 default: default,
1028 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001029 })
1030 ));
1031
David Tolnayda705bd2017-11-10 21:58:05 -08001032 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001033 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1034 constness: syn!(Constness) >>
1035 unsafety: syn!(Unsafety) >>
1036 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001037 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001038 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001039 generics: syn!(Generics) >>
1040 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001041 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001042 where_clause: syn!(WhereClause) >>
1043 body: option!(braces!(
1044 tuple!(many0!(call!(Attribute::parse_inner)),
1045 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001046 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001047 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001048 ({
1049 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001050 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001051 None => (Vec::new(), None),
1052 };
David Tolnayda705bd2017-11-10 21:58:05 -08001053 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001054 attrs: {
1055 let mut attrs = outer_attrs;
1056 attrs.extend(inner_attrs);
1057 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001058 },
David Tolnayda705bd2017-11-10 21:58:05 -08001059 sig: MethodSig {
1060 constness: constness,
1061 unsafety: unsafety,
1062 abi: abi,
1063 ident: ident,
1064 decl: FnDecl {
1065 inputs: inputs.0,
1066 output: ret,
1067 variadic: false,
1068 fn_token: fn_,
1069 paren_token: inputs.1,
1070 dot_tokens: None,
1071 generics: Generics {
1072 where_clause: where_clause,
1073 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001074 },
1075 },
David Tolnayda705bd2017-11-10 21:58:05 -08001076 },
1077 default: stmts.map(|stmts| {
1078 Block {
1079 stmts: stmts.0,
1080 brace_token: stmts.1,
1081 }
1082 }),
1083 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001084 }
David Tolnay0aecb732016-10-03 23:03:50 -07001085 })
1086 ));
1087
David Tolnayda705bd2017-11-10 21:58:05 -08001088 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001089 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001090 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001091 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001092 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001093 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001094 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001095 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001096 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001097 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001098 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001099 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001100 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001101 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001102 type_token: type_,
1103 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001104 generics: Generics {
1105 where_clause: where_clause,
1106 .. generics
1107 },
David Tolnayda705bd2017-11-10 21:58:05 -08001108 colon_token: colon,
1109 bounds: bounds.unwrap_or_default(),
1110 default: default,
1111 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001112 })
1113 ));
1114
David Tolnaydecf28d2017-11-11 11:56:45 -08001115 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001116 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001117 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001118 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001119 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001120 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001121 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001122 })
1123 ));
1124
David Tolnay4c614be2017-11-10 00:02:38 -08001125 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001126 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001127 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001128 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001129 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001130 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001131 polarity_path: alt!(
1132 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001133 polarity: syn!(ImplPolarity) >>
1134 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001135 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001136 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001137 )
1138 |
David Tolnay570695e2017-06-03 16:15:13 -07001139 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001140 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001141 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001142 where_clause: syn!(WhereClause) >>
1143 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001144 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001145 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001146 defaultness: defaultness,
1147 unsafety: unsafety,
1148 impl_token: impl_,
1149 generics: Generics {
1150 where_clause: where_clause,
1151 .. generics
1152 },
1153 trait_: polarity_path,
1154 self_ty: Box::new(self_ty),
1155 brace_token: body.1,
1156 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001157 })
David Tolnay4c9be372016-10-06 00:47:37 -07001158 ));
1159
David Tolnay857628c2017-11-11 12:25:31 -08001160 impl_synom!(ImplItem "item in impl block" alt!(
1161 syn!(ImplItemConst) => { ImplItem::Const }
1162 |
1163 syn!(ImplItemMethod) => { ImplItem::Method }
1164 |
1165 syn!(ImplItemType) => { ImplItem::Type }
1166 |
1167 syn!(ImplItemMacro) => { ImplItem::Macro }
1168 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001169
David Tolnay857628c2017-11-11 12:25:31 -08001170 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001171 attrs: many0!(call!(Attribute::parse_outer)) >>
1172 vis: syn!(Visibility) >>
1173 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001174 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001175 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001176 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001177 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001178 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001179 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001180 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001181 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001182 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001183 vis: vis,
1184 defaultness: defaultness,
1185 const_token: const_,
1186 ident: ident,
1187 colon_token: colon,
1188 ty: ty,
1189 eq_token: eq,
1190 expr: value,
1191 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001192 })
1193 ));
1194
David Tolnay857628c2017-11-11 12:25:31 -08001195 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001196 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1197 vis: syn!(Visibility) >>
1198 defaultness: syn!(Defaultness) >>
1199 constness: syn!(Constness) >>
1200 unsafety: syn!(Unsafety) >>
1201 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001202 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001203 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001204 generics: syn!(Generics) >>
1205 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001206 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001207 where_clause: syn!(WhereClause) >>
1208 inner_attrs_stmts: braces!(tuple!(
1209 many0!(call!(Attribute::parse_inner)),
1210 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001211 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001212 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001213 attrs: {
1214 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001215 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001216 attrs
1217 },
David Tolnay857628c2017-11-11 12:25:31 -08001218 vis: vis,
1219 defaultness: defaultness,
1220 sig: MethodSig {
1221 constness: constness,
1222 unsafety: unsafety,
1223 abi: abi,
1224 ident: ident,
1225 decl: FnDecl {
1226 fn_token: fn_,
1227 paren_token: inputs.1,
1228 inputs: inputs.0,
1229 output: ret,
1230 variadic: false,
1231 generics: Generics {
1232 where_clause: where_clause,
1233 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001234 },
David Tolnay857628c2017-11-11 12:25:31 -08001235 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001236 },
David Tolnay857628c2017-11-11 12:25:31 -08001237 },
1238 block: Block {
1239 brace_token: inner_attrs_stmts.1,
1240 stmts: (inner_attrs_stmts.0).1,
1241 },
David Tolnay4c9be372016-10-06 00:47:37 -07001242 })
1243 ));
1244
David Tolnay857628c2017-11-11 12:25:31 -08001245 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001246 attrs: many0!(call!(Attribute::parse_outer)) >>
1247 vis: syn!(Visibility) >>
1248 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001249 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001250 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001251 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001252 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001253 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001254 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001255 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001256 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001257 vis: vis,
1258 defaultness: defaultness,
1259 type_token: type_,
1260 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001261 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001262 eq_token: eq,
1263 ty: ty,
1264 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001265 })
1266 ));
1267
David Tolnay857628c2017-11-11 12:25:31 -08001268 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001269 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001270 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001271 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001272 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001273 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001274 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001275 })
1276 ));
1277
Alex Crichton954046c2017-05-30 21:49:42 -07001278 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001279 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001280 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001281 |
1282 epsilon!() => { |_| ImplPolarity::Positive }
1283 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001284 }
David Tolnay4c9be372016-10-06 00:47:37 -07001285
Alex Crichton954046c2017-05-30 21:49:42 -07001286 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001287 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001288 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001289 |
1290 epsilon!() => { |_| Constness::NotConst }
1291 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001292 }
David Tolnay42602292016-10-01 22:25:45 -07001293
Alex Crichton954046c2017-05-30 21:49:42 -07001294 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001295 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001297 |
1298 epsilon!() => { |_| Defaultness::Final }
1299 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001300 }
David Tolnayedf2b992016-09-23 20:43:45 -07001301}
David Tolnay4a51dc72016-10-01 00:40:31 -07001302
1303#[cfg(feature = "printing")]
1304mod printing {
1305 use super::*;
1306 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001307 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001308 use quote::{Tokens, ToTokens};
1309
David Tolnay1bfa7332017-11-11 12:41:20 -08001310 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001311 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001312 tokens.append_all(self.attrs.outer());
1313 self.vis.to_tokens(tokens);
1314 self.extern_token.to_tokens(tokens);
1315 self.crate_token.to_tokens(tokens);
1316 self.ident.to_tokens(tokens);
1317 if let Some((ref as_token, ref rename)) = self.rename {
1318 as_token.to_tokens(tokens);
1319 rename.to_tokens(tokens);
1320 }
1321 self.semi_token.to_tokens(tokens);
1322 }
1323 }
1324
1325 impl ToTokens for ItemUse {
1326 fn to_tokens(&self, tokens: &mut Tokens) {
1327 tokens.append_all(self.attrs.outer());
1328 self.vis.to_tokens(tokens);
1329 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001330 self.leading_colon.to_tokens(tokens);
1331 self.prefix.to_tokens(tokens);
1332 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001333 self.semi_token.to_tokens(tokens);
1334 }
1335 }
1336
1337 impl ToTokens for ItemStatic {
1338 fn to_tokens(&self, tokens: &mut Tokens) {
1339 tokens.append_all(self.attrs.outer());
1340 self.vis.to_tokens(tokens);
1341 self.static_token.to_tokens(tokens);
1342 self.mutbl.to_tokens(tokens);
1343 self.ident.to_tokens(tokens);
1344 self.colon_token.to_tokens(tokens);
1345 self.ty.to_tokens(tokens);
1346 self.eq_token.to_tokens(tokens);
1347 self.expr.to_tokens(tokens);
1348 self.semi_token.to_tokens(tokens);
1349 }
1350 }
1351
1352 impl ToTokens for ItemConst {
1353 fn to_tokens(&self, tokens: &mut Tokens) {
1354 tokens.append_all(self.attrs.outer());
1355 self.vis.to_tokens(tokens);
1356 self.const_token.to_tokens(tokens);
1357 self.ident.to_tokens(tokens);
1358 self.colon_token.to_tokens(tokens);
1359 self.ty.to_tokens(tokens);
1360 self.eq_token.to_tokens(tokens);
1361 self.expr.to_tokens(tokens);
1362 self.semi_token.to_tokens(tokens);
1363 }
1364 }
1365
1366 impl ToTokens for ItemFn {
1367 fn to_tokens(&self, tokens: &mut Tokens) {
1368 tokens.append_all(self.attrs.outer());
1369 self.vis.to_tokens(tokens);
1370 self.constness.to_tokens(tokens);
1371 self.unsafety.to_tokens(tokens);
1372 self.abi.to_tokens(tokens);
1373 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1374 self.block.brace_token.surround(tokens, |tokens| {
1375 tokens.append_all(self.attrs.inner());
1376 tokens.append_all(&self.block.stmts);
1377 });
1378 }
1379 }
1380
1381 impl ToTokens for ItemMod {
1382 fn to_tokens(&self, tokens: &mut Tokens) {
1383 tokens.append_all(self.attrs.outer());
1384 self.vis.to_tokens(tokens);
1385 self.mod_token.to_tokens(tokens);
1386 self.ident.to_tokens(tokens);
1387 if let Some((ref brace, ref items)) = self.content {
1388 brace.surround(tokens, |tokens| {
1389 tokens.append_all(self.attrs.inner());
1390 tokens.append_all(items);
1391 });
1392 } else {
1393 TokensOrDefault(&self.semi).to_tokens(tokens);
1394 }
1395 }
1396 }
1397
1398 impl ToTokens for ItemForeignMod {
1399 fn to_tokens(&self, tokens: &mut Tokens) {
1400 tokens.append_all(self.attrs.outer());
1401 self.abi.to_tokens(tokens);
1402 self.brace_token.surround(tokens, |tokens| {
1403 tokens.append_all(&self.items);
1404 });
1405 }
1406 }
1407
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001408 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001409 fn to_tokens(&self, tokens: &mut Tokens) {
1410 tokens.append_all(self.attrs.outer());
1411 self.vis.to_tokens(tokens);
1412 self.type_token.to_tokens(tokens);
1413 self.ident.to_tokens(tokens);
1414 self.generics.to_tokens(tokens);
1415 self.generics.where_clause.to_tokens(tokens);
1416 self.eq_token.to_tokens(tokens);
1417 self.ty.to_tokens(tokens);
1418 self.semi_token.to_tokens(tokens);
1419 }
1420 }
1421
1422 impl ToTokens for ItemEnum {
1423 fn to_tokens(&self, tokens: &mut Tokens) {
1424 tokens.append_all(self.attrs.outer());
1425 self.vis.to_tokens(tokens);
1426 self.enum_token.to_tokens(tokens);
1427 self.ident.to_tokens(tokens);
1428 self.generics.to_tokens(tokens);
1429 self.generics.where_clause.to_tokens(tokens);
1430 self.brace_token.surround(tokens, |tokens| {
1431 self.variants.to_tokens(tokens);
1432 });
1433 }
1434 }
1435
1436 impl ToTokens for ItemStruct {
1437 fn to_tokens(&self, tokens: &mut Tokens) {
1438 tokens.append_all(self.attrs.outer());
1439 self.vis.to_tokens(tokens);
1440 self.struct_token.to_tokens(tokens);
1441 self.ident.to_tokens(tokens);
1442 self.generics.to_tokens(tokens);
1443 match self.data {
1444 VariantData::Struct(..) => {
1445 self.generics.where_clause.to_tokens(tokens);
1446 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001447 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001448 VariantData::Tuple(..) => {
1449 self.data.to_tokens(tokens);
1450 self.generics.where_clause.to_tokens(tokens);
1451 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001452 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001453 VariantData::Unit => {
1454 self.generics.where_clause.to_tokens(tokens);
1455 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001456 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001457 }
1458 }
1459 }
1460
1461 impl ToTokens for ItemUnion {
1462 fn to_tokens(&self, tokens: &mut Tokens) {
1463 tokens.append_all(self.attrs.outer());
1464 self.vis.to_tokens(tokens);
1465 self.union_token.to_tokens(tokens);
1466 self.ident.to_tokens(tokens);
1467 self.generics.to_tokens(tokens);
1468 self.generics.where_clause.to_tokens(tokens);
1469 // XXX: Should we handle / complain when using a
1470 // non-VariantData::Struct Variant in Union?
1471 self.data.to_tokens(tokens);
1472 }
1473 }
1474
1475 impl ToTokens for ItemTrait {
1476 fn to_tokens(&self, tokens: &mut Tokens) {
1477 tokens.append_all(self.attrs.outer());
1478 self.vis.to_tokens(tokens);
1479 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001480 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001481 self.trait_token.to_tokens(tokens);
1482 self.ident.to_tokens(tokens);
1483 self.generics.to_tokens(tokens);
1484 if !self.supertraits.is_empty() {
1485 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1486 self.supertraits.to_tokens(tokens);
1487 }
1488 self.generics.where_clause.to_tokens(tokens);
1489 self.brace_token.surround(tokens, |tokens| {
1490 tokens.append_all(&self.items);
1491 });
1492 }
1493 }
1494
1495 impl ToTokens for ItemDefaultImpl {
1496 fn to_tokens(&self, tokens: &mut Tokens) {
1497 tokens.append_all(self.attrs.outer());
1498 self.unsafety.to_tokens(tokens);
1499 self.impl_token.to_tokens(tokens);
1500 self.path.to_tokens(tokens);
1501 self.for_token.to_tokens(tokens);
1502 self.dot2_token.to_tokens(tokens);
1503 self.brace_token.surround(tokens, |_tokens| {});
1504 }
1505 }
1506
1507 impl ToTokens for ItemImpl {
1508 fn to_tokens(&self, tokens: &mut Tokens) {
1509 tokens.append_all(self.attrs.outer());
1510 self.defaultness.to_tokens(tokens);
1511 self.unsafety.to_tokens(tokens);
1512 self.impl_token.to_tokens(tokens);
1513 self.generics.to_tokens(tokens);
1514 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1515 polarity.to_tokens(tokens);
1516 path.to_tokens(tokens);
1517 for_token.to_tokens(tokens);
1518 }
1519 self.self_ty.to_tokens(tokens);
1520 self.generics.where_clause.to_tokens(tokens);
1521 self.brace_token.surround(tokens, |tokens| {
1522 tokens.append_all(&self.items);
1523 });
1524 }
1525 }
1526
1527 impl ToTokens for ItemMacro {
1528 fn to_tokens(&self, tokens: &mut Tokens) {
1529 tokens.append_all(self.attrs.outer());
1530 self.mac.path.to_tokens(tokens);
1531 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001532 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001533 tokens.append_all(&self.mac.tokens);
1534 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001535 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001536 }
1537 }
1538 }
David Tolnay42602292016-10-01 22:25:45 -07001539
David Tolnay500d8322017-12-18 00:32:51 -08001540 impl ToTokens for ItemMacro2 {
1541 fn to_tokens(&self, tokens: &mut Tokens) {
1542 tokens.append_all(self.attrs.outer());
1543 self.vis.to_tokens(tokens);
1544 self.macro_token.to_tokens(tokens);
1545 self.ident.to_tokens(tokens);
1546 self.args.to_tokens(tokens);
1547 self.body.to_tokens(tokens);
1548 }
1549 }
1550
David Tolnay5f332a92017-12-26 00:42:45 -05001551 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001552 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001553 self.ident.to_tokens(tokens);
1554 if let Some((ref as_token, ref rename)) = self.rename {
1555 as_token.to_tokens(tokens);
1556 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001557 }
David Tolnay4a057422016-10-08 00:02:31 -07001558 }
1559 }
1560
David Tolnay5f332a92017-12-26 00:42:45 -05001561 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001562 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001564 }
1565 }
1566
David Tolnay5f332a92017-12-26 00:42:45 -05001567 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001568 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 self.brace_token.surround(tokens, |tokens| {
1570 self.items.to_tokens(tokens);
1571 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001572 }
1573 }
1574
David Tolnay1bfa7332017-11-11 12:41:20 -08001575 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001576 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001577 tokens.append_all(self.attrs.outer());
1578 self.const_token.to_tokens(tokens);
1579 self.ident.to_tokens(tokens);
1580 self.colon_token.to_tokens(tokens);
1581 self.ty.to_tokens(tokens);
1582 if let Some((ref eq_token, ref default)) = self.default {
1583 eq_token.to_tokens(tokens);
1584 default.to_tokens(tokens);
1585 }
1586 self.semi_token.to_tokens(tokens);
1587 }
1588 }
1589
1590 impl ToTokens for TraitItemMethod {
1591 fn to_tokens(&self, tokens: &mut Tokens) {
1592 tokens.append_all(self.attrs.outer());
1593 self.sig.to_tokens(tokens);
1594 match self.default {
1595 Some(ref block) => {
1596 block.brace_token.surround(tokens, |tokens| {
1597 tokens.append_all(self.attrs.inner());
1598 tokens.append_all(&block.stmts);
1599 });
David Tolnayca085422016-10-04 00:12:38 -07001600 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001601 None => {
1602 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001603 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001604 }
1605 }
1606 }
1607
1608 impl ToTokens for TraitItemType {
1609 fn to_tokens(&self, tokens: &mut Tokens) {
1610 tokens.append_all(self.attrs.outer());
1611 self.type_token.to_tokens(tokens);
1612 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001613 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001614 if !self.bounds.is_empty() {
1615 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1616 self.bounds.to_tokens(tokens);
1617 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001618 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001619 if let Some((ref eq_token, ref default)) = self.default {
1620 eq_token.to_tokens(tokens);
1621 default.to_tokens(tokens);
1622 }
1623 self.semi_token.to_tokens(tokens);
1624 }
1625 }
1626
1627 impl ToTokens for TraitItemMacro {
1628 fn to_tokens(&self, tokens: &mut Tokens) {
1629 tokens.append_all(self.attrs.outer());
1630 self.mac.to_tokens(tokens);
1631 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001632 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001633 }
1634 }
1635 }
1636
David Tolnay857628c2017-11-11 12:25:31 -08001637 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001638 fn to_tokens(&self, tokens: &mut Tokens) {
1639 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001640 self.vis.to_tokens(tokens);
1641 self.defaultness.to_tokens(tokens);
1642 self.const_token.to_tokens(tokens);
1643 self.ident.to_tokens(tokens);
1644 self.colon_token.to_tokens(tokens);
1645 self.ty.to_tokens(tokens);
1646 self.eq_token.to_tokens(tokens);
1647 self.expr.to_tokens(tokens);
1648 self.semi_token.to_tokens(tokens);
1649 }
1650 }
1651
1652 impl ToTokens for ImplItemMethod {
1653 fn to_tokens(&self, tokens: &mut Tokens) {
1654 tokens.append_all(self.attrs.outer());
1655 self.vis.to_tokens(tokens);
1656 self.defaultness.to_tokens(tokens);
1657 self.sig.to_tokens(tokens);
1658 self.block.brace_token.surround(tokens, |tokens| {
1659 tokens.append_all(self.attrs.inner());
1660 tokens.append_all(&self.block.stmts);
1661 });
1662 }
1663 }
1664
1665 impl ToTokens for ImplItemType {
1666 fn to_tokens(&self, tokens: &mut Tokens) {
1667 tokens.append_all(self.attrs.outer());
1668 self.vis.to_tokens(tokens);
1669 self.defaultness.to_tokens(tokens);
1670 self.type_token.to_tokens(tokens);
1671 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001672 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001673 self.eq_token.to_tokens(tokens);
1674 self.ty.to_tokens(tokens);
1675 self.semi_token.to_tokens(tokens);
1676 }
1677 }
1678
1679 impl ToTokens for ImplItemMacro {
1680 fn to_tokens(&self, tokens: &mut Tokens) {
1681 tokens.append_all(self.attrs.outer());
1682 self.mac.to_tokens(tokens);
1683 if !self.mac.is_braced() {
1684 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001685 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001686 }
1687 }
1688 }
1689
David Tolnay8894f602017-11-11 12:11:04 -08001690 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001691 fn to_tokens(&self, tokens: &mut Tokens) {
1692 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001693 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001694 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1695 self.semi_token.to_tokens(tokens);
1696 }
1697 }
1698
1699 impl ToTokens for ForeignItemStatic {
1700 fn to_tokens(&self, tokens: &mut Tokens) {
1701 tokens.append_all(self.attrs.outer());
1702 self.vis.to_tokens(tokens);
1703 self.static_token.to_tokens(tokens);
1704 self.mutbl.to_tokens(tokens);
1705 self.ident.to_tokens(tokens);
1706 self.colon_token.to_tokens(tokens);
1707 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001708 self.semi_token.to_tokens(tokens);
1709 }
1710 }
1711
David Tolnay199bcbb2017-11-12 10:33:52 -08001712 impl ToTokens for ForeignItemType {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 tokens.append_all(self.attrs.outer());
1715 self.vis.to_tokens(tokens);
1716 self.type_token.to_tokens(tokens);
1717 self.ident.to_tokens(tokens);
1718 self.semi_token.to_tokens(tokens);
1719 }
1720 }
1721
David Tolnay570695e2017-06-03 16:15:13 -07001722 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001723 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001724 self.constness.to_tokens(tokens);
1725 self.unsafety.to_tokens(tokens);
1726 self.abi.to_tokens(tokens);
1727 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001728 }
1729 }
1730
David Tolnay570695e2017-06-03 16:15:13 -07001731 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001732
1733 impl<'a> ToTokens for NamedDecl<'a> {
1734 fn to_tokens(&self, tokens: &mut Tokens) {
1735 self.0.fn_token.to_tokens(tokens);
1736 self.1.to_tokens(tokens);
1737 self.0.generics.to_tokens(tokens);
1738 self.0.paren_token.surround(tokens, |tokens| {
1739 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001740
1741 if self.0.variadic {
1742 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001743 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001744 }
Alex Crichton259ee532017-07-14 06:51:02 -07001745 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001746 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 });
1748 self.0.output.to_tokens(tokens);
1749 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001750 }
1751 }
1752
Alex Crichton62a0a592017-05-22 13:58:53 -07001753 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001754 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001755 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001756 self.lifetime.to_tokens(tokens);
1757 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001759 }
1760 }
1761
1762 impl ToTokens for ArgSelf {
1763 fn to_tokens(&self, tokens: &mut Tokens) {
1764 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001766 }
1767 }
1768
1769 impl ToTokens for ArgCaptured {
1770 fn to_tokens(&self, tokens: &mut Tokens) {
1771 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001772 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001773 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001774 }
1775 }
1776
David Tolnay42602292016-10-01 22:25:45 -07001777 impl ToTokens for Constness {
1778 fn to_tokens(&self, tokens: &mut Tokens) {
1779 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001780 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001781 Constness::NotConst => {
1782 // nothing
1783 }
David Tolnay42602292016-10-01 22:25:45 -07001784 }
1785 }
1786 }
1787
David Tolnay4c9be372016-10-06 00:47:37 -07001788 impl ToTokens for Defaultness {
1789 fn to_tokens(&self, tokens: &mut Tokens) {
1790 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001791 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001792 Defaultness::Final => {
1793 // nothing
1794 }
1795 }
1796 }
1797 }
1798
1799 impl ToTokens for ImplPolarity {
1800 fn to_tokens(&self, tokens: &mut Tokens) {
1801 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001803 ImplPolarity::Positive => {
1804 // nothing
1805 }
1806 }
1807 }
1808 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001809}