blob: 7af0b494eb27c4bc4b3044ad501475face38d28a [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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070013 pub extern_token: tokens::Extern,
14 pub crate_token: tokens::Crate,
David Tolnay570695e2017-06-03 16:15:13 -070015 pub ident: Ident,
16 pub rename: Option<(tokens::As, Ident)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070017 pub semi_token: tokens::Semi,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025 pub use_token: tokens::Use,
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub path: Box<ViewPath>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070027 pub semi_token: tokens::Semi,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub ident: Ident,
38 pub colon_token: tokens::Colon,
39 pub ty: Box<Ty>,
40 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070041 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070042 pub semi_token: tokens::Semi,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070050 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -070053 pub ty: Box<Ty>,
David Tolnay570695e2017-06-03 16:15:13 -070054 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070056 pub semi_token: tokens::Semi,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 pub mod_token: tokens::Mod,
David Tolnay570695e2017-06-03 16:15:13 -070078 pub ident: Ident,
79 pub content: Option<(tokens::Brace, Vec<Item>)>,
80 pub semi: Option<tokens::Semi>,
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>;`
94 pub Ty(ItemTy {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070096 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700100 pub eq_token: tokens::Eq,
101 pub ty: Box<Ty>,
102 pub semi_token: tokens::Semi,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700110 pub enum_token: tokens::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,
114 pub variants: Delimited<Variant, tokens::Comma>,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub struct_token: tokens::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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 pub semi_token: Option<tokens::Semi>,
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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700134 pub union_token: tokens::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,
David Tolnay570695e2017-06-03 16:15:13 -0700146 pub trait_token: tokens::Trait,
147 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700148 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700149 pub colon_token: Option<tokens::Colon>,
150 pub supertraits: Delimited<TyParamBound, tokens::Add>,
151 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 pub items: Vec<TraitItem>,
153 }),
154 /// Default trait implementation.
155 ///
156 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
157 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800158 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 pub unsafety: Unsafety,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700160 pub impl_token: tokens::Impl,
David Tolnay570695e2017-06-03 16:15:13 -0700161 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700162 pub for_token: tokens::For,
163 pub dot2_token: tokens::Dot2,
164 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 }),
166 /// An implementation.
167 ///
168 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
169 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800170 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700171 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700173 pub impl_token: tokens::Impl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700175 /// Trait this impl implements.
176 pub trait_: Option<(ImplPolarity, Path, tokens::For)>,
177 /// The Self type of the impl.
178 pub self_ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700179 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub items: Vec<ImplItem>,
181 }),
182 /// A macro invocation (which includes macro definition).
183 ///
184 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnayc6b55bc2017-11-09 22:48:38 -0800185 pub Mac(ItemMac {
186 pub attrs: Vec<Attribute>,
187 pub mac: Mac,
188 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }
190
191 do_not_generate_to_tokens
David Tolnayb79ee962016-09-04 09:39:20 -0700192}
193
David Tolnay0e837402016-12-22 17:25:55 -0500194impl From<DeriveInput> for Item {
195 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800196 match input.body {
197 Body::Enum(data) => {
198 Item::Enum(ItemEnum {
199 attrs: input.attrs,
200 vis: input.vis,
201 enum_token: data.enum_token,
202 ident: input.ident,
203 generics: input.generics,
204 brace_token: data.brace_token,
205 variants: data.variants,
206 })
207 }
208 Body::Struct(data) => {
209 Item::Struct(ItemStruct {
210 attrs: input.attrs,
211 vis: input.vis,
212 struct_token: data.struct_token,
213 ident: input.ident,
214 generics: input.generics,
215 data: data.data,
216 semi_token: data.semi_token,
217 })
218 }
David Tolnay453cfd12016-10-23 11:00:14 -0700219 }
220 }
221}
222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223ast_enum_of_structs! {
224 pub enum ViewPath {
225 /// `foo::bar::baz as quux`
226 ///
227 /// or just
228 ///
229 /// `foo::bar::baz` (with `as baz` implicitly on the right)
230 pub Simple(PathSimple {
231 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700232 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700233 pub rename: Option<Ident>,
234 }),
235
236 /// `foo::bar::*`
237 pub Glob(PathGlob {
238 pub path: Path,
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700239 pub colon2_token: Option<tokens::Colon2>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700240 pub star_token: tokens::Star,
Alex Crichton62a0a592017-05-22 13:58:53 -0700241 }),
242
243 /// `foo::bar::{a, b, c}`
244 pub List(PathList {
245 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700246 pub colon2_token: tokens::Colon2,
247 pub brace_token: tokens::Brace,
248 pub items: Delimited<PathListItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 }),
250 }
251}
252
253ast_struct! {
254 pub struct PathListItem {
255 pub name: Ident,
256 /// renamed in list, e.g. `use foo::{bar as baz};`
257 pub rename: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700258 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 }
260}
261
262ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700263 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 pub enum Constness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700265 Const(tokens::Const),
Alex Crichton62a0a592017-05-22 13:58:53 -0700266 NotConst,
267 }
268}
269
270ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700271 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 pub enum Defaultness {
Alex Crichton954046c2017-05-30 21:49:42 -0700273 Default(tokens::Default_),
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 Final,
275 }
276}
277
278ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub struct ForeignItem {
280 pub ident: Ident,
281 pub attrs: Vec<Attribute>,
282 pub node: ForeignItemKind,
283 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 }
286}
287
288ast_enum_of_structs! {
289 /// An item within an `extern` block
290 pub enum ForeignItemKind {
291 /// A foreign function
292 pub Fn(ForeignItemFn {
293 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 }),
295 /// A foreign static item (`static ext: u8`)
296 pub Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700297 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700299 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 pub mutbl: Mutability,
301 }),
302 }
303
304 do_not_generate_to_tokens
305}
306
David Tolnayda705bd2017-11-10 21:58:05 -0800307ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 /// Represents an item declaration within a trait declaration,
309 /// possibly including a default implementation. A trait item is
310 /// either required (meaning it doesn't have an implementation, just a
311 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800312 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800314 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700315 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700316 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700317 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700319 pub default: Option<(tokens::Eq, Expr)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700320 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700321 }),
322 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800323 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 pub sig: MethodSig,
325 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700326 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 }),
328 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800329 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700330 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700331 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700332 pub colon_token: Option<tokens::Colon>,
333 pub bounds: Delimited<TyParamBound, tokens::Add>,
David Tolnay570695e2017-06-03 16:15:13 -0700334 pub default: Option<(tokens::Eq, Ty)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700335 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 }),
David Tolnayda705bd2017-11-10 21:58:05 -0800337 pub Macro(TraitItemMac {
338 pub attrs: Vec<Attribute>,
339 pub mac: Mac,
340 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }
342
343 do_not_generate_to_tokens
344}
345
346ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700347 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 pub enum ImplPolarity {
349 /// `impl Trait for Type`
350 Positive,
351 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700352 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 }
354}
355
356ast_struct! {
357 pub struct ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 pub attrs: Vec<Attribute>,
359 pub node: ImplItemKind,
360 }
361}
362
363ast_enum_of_structs! {
364 pub enum ImplItemKind {
365 pub Const(ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700366 pub vis: Visibility,
367 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700368 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700369 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700370 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700372 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700374 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700375 }),
376 pub Method(ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -0700377 pub vis: Visibility,
378 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub sig: MethodSig,
380 pub block: Block,
381 }),
382 pub Type(ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -0700383 pub vis: Visibility,
384 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700385 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700386 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700387 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700389 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 }),
391 pub Macro(Mac),
392 }
393
394 do_not_generate_to_tokens
395}
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 {
Alex Crichton954046c2017-05-30 21:49:42 -0700414 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700415 pub paren_token: tokens::Paren,
416 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700418 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700420 pub dot_tokens: Option<tokens::Dot3>,
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 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700430 pub and_token: tokens::And,
431 pub self_token: tokens::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,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700437 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 }),
439 pub Captured(ArgCaptured {
440 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700441 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700442 pub ty: Ty,
443 }),
444 pub Ignored(Ty),
445 }
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;
Alex Crichton954046c2017-05-30 21:49:42 -0700453 use synom::tokens::*;
454 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700455
David Tolnay4c614be2017-11-10 00:02:38 -0800456 impl_synom!(Item "item" alt!(
457 syn!(ItemExternCrate) => { Item::ExternCrate }
458 |
459 syn!(ItemUse) => { Item::Use }
460 |
461 syn!(ItemStatic) => { Item::Static }
462 |
463 syn!(ItemConst) => { Item::Const }
464 |
465 syn!(ItemFn) => { Item::Fn }
466 |
467 syn!(ItemMod) => { Item::Mod }
468 |
469 syn!(ItemForeignMod) => { Item::ForeignMod }
470 |
471 syn!(ItemTy) => { Item::Ty }
472 |
473 syn!(ItemStruct) => { Item::Struct }
474 |
475 syn!(ItemEnum) => { Item::Enum }
476 |
477 syn!(ItemUnion) => { Item::Union }
478 |
479 syn!(ItemTrait) => { Item::Trait }
480 |
481 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
482 |
483 syn!(ItemImpl) => { Item::Impl }
484 |
485 syn!(ItemMac) => { Item::Mac }
486 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700487
David Tolnay4c614be2017-11-10 00:02:38 -0800488 impl_synom!(ItemMac "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700489 attrs: many0!(call!(Attribute::parse_outer)) >>
490 what: syn!(Path) >>
491 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700492 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700493 body: call!(::TokenTree::parse_delimited) >>
494 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800495 (ItemMac {
David Tolnay84aa0752016-10-02 23:01:13 -0700496 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800497 mac: Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -0500498 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700499 bang_token: bang,
500 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800502 },
David Tolnay4c614be2017-11-10 00:02:38 -0800503 })
David Tolnayedf2b992016-09-23 20:43:45 -0700504 ));
505
David Tolnay4c614be2017-11-10 00:02:38 -0800506 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700507 attrs: many0!(call!(Attribute::parse_outer)) >>
508 vis: syn!(Visibility) >>
509 extern_: syn!(Extern) >>
510 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700511 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700512 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
513 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800514 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700515 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800516 vis: vis,
517 extern_token: extern_,
518 crate_token: crate_,
519 ident: ident,
520 rename: rename,
521 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800522 })
David Tolnayedf2b992016-09-23 20:43:45 -0700523 ));
524
David Tolnay4c614be2017-11-10 00:02:38 -0800525 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700526 attrs: many0!(call!(Attribute::parse_outer)) >>
527 vis: syn!(Visibility) >>
528 use_: syn!(Use) >>
529 what: syn!(ViewPath) >>
530 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800531 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700532 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800533 vis: vis,
534 use_token: use_,
535 path: Box::new(what),
536 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800537 })
David Tolnay4a057422016-10-08 00:02:31 -0700538 ));
539
Alex Crichton954046c2017-05-30 21:49:42 -0700540 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400541 named!(parse -> Self, alt!(
542 syn!(PathGlob) => { ViewPath::Glob }
543 |
544 syn!(PathList) => { ViewPath::List }
545 |
546 syn!(PathSimple) => { ViewPath::Simple } // must be last
547 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700548 }
David Tolnay4a057422016-10-08 00:02:31 -0700549
Alex Crichton954046c2017-05-30 21:49:42 -0700550 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400551 named!(parse -> Self, do_parse!(
552 path: syn!(Path) >>
553 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
554 (PathSimple {
555 path: path,
556 as_token: rename.as_ref().map(|p| As((p.0).0)),
557 rename: rename.map(|p| p.1),
558 })
559 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700560 }
David Tolnay4a057422016-10-08 00:02:31 -0700561
Alex Crichton954046c2017-05-30 21:49:42 -0700562 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400563 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700564 path: option!(do_parse!(
565 path: syn!(Path) >>
566 colon2: syn!(Colon2) >>
567 (path, colon2)
568 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400569 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700570 ({
571 match path {
572 Some((path, colon2)) => {
573 PathGlob {
574 path: path,
575 colon2_token: Some(colon2),
576 star_token: star,
577 }
578 }
579 None => {
580 PathGlob {
581 path: Path {
582 leading_colon: None,
583 segments: Default::default(),
584 },
585 colon2_token: None,
586 star_token: star,
587 }
588 }
589 }
Michael Layzell92639a52017-06-01 00:07:44 -0400590 })
591 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700592 }
David Tolnay4a057422016-10-08 00:02:31 -0700593
Alex Crichton954046c2017-05-30 21:49:42 -0700594 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400595 named!(parse -> Self, alt!(
596 do_parse!(
597 path: syn!(Path) >>
598 colon2: syn!(Colon2) >>
599 items: braces!(call!(Delimited::parse_terminated)) >>
600 (PathList {
601 path: path,
602 items: items.0,
603 brace_token: items.1,
604 colon2_token: colon2,
605 })
606 )
607 |
608 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700609 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400610 items: braces!(call!(Delimited::parse_terminated)) >>
611 (PathList {
612 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400613 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700614 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400615 },
David Tolnay570695e2017-06-03 16:15:13 -0700616 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400617 brace_token: items.1,
618 items: items.0,
619 })
620 )
621 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700622 }
David Tolnay4a057422016-10-08 00:02:31 -0700623
Alex Crichton954046c2017-05-30 21:49:42 -0700624 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400625 named!(parse -> Self, do_parse!(
626 name: alt!(
627 syn!(Ident)
628 |
629 map!(syn!(Self_), Into::into)
630 ) >>
631 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
632 (PathListItem {
633 name: name,
634 as_token: rename.as_ref().map(|p| As((p.0).0)),
635 rename: rename.map(|p| p.1),
636 })
637 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
David Tolnay4a057422016-10-08 00:02:31 -0700639
David Tolnay4c614be2017-11-10 00:02:38 -0800640 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700641 attrs: many0!(call!(Attribute::parse_outer)) >>
642 vis: syn!(Visibility) >>
643 static_: syn!(Static) >>
644 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700645 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700646 colon: syn!(Colon) >>
647 ty: syn!(Ty) >>
648 eq: syn!(Eq) >>
649 value: syn!(Expr) >>
650 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800651 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700652 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800653 vis: vis,
654 static_token: static_,
655 mutbl: mutability,
656 ident: ident,
657 colon_token: colon,
658 ty: Box::new(ty),
659 eq_token: eq,
660 expr: Box::new(value),
661 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800662 })
David Tolnay47a877c2016-10-01 16:50:55 -0700663 ));
664
David Tolnay4c614be2017-11-10 00:02:38 -0800665 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700666 attrs: many0!(call!(Attribute::parse_outer)) >>
667 vis: syn!(Visibility) >>
668 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700669 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700670 colon: syn!(Colon) >>
671 ty: syn!(Ty) >>
672 eq: syn!(Eq) >>
673 value: syn!(Expr) >>
674 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800675 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700676 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800677 vis: vis,
678 const_token: const_,
679 ident: ident,
680 colon_token: colon,
681 ty: Box::new(ty),
682 eq_token: eq,
683 expr: Box::new(value),
684 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800685 })
David Tolnay47a877c2016-10-01 16:50:55 -0700686 ));
687
David Tolnay4c614be2017-11-10 00:02:38 -0800688 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700689 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
690 vis: syn!(Visibility) >>
691 constness: syn!(Constness) >>
692 unsafety: syn!(Unsafety) >>
693 abi: option!(syn!(Abi)) >>
694 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700695 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700696 generics: syn!(Generics) >>
697 inputs: parens!(Delimited::parse_terminated) >>
698 ret: syn!(FunctionRetTy) >>
699 where_clause: syn!(WhereClause) >>
700 inner_attrs_stmts: braces!(tuple!(
701 many0!(call!(Attribute::parse_inner)),
702 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400703 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800704 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700705 attrs: {
706 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700707 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700708 attrs
709 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800710 vis: vis,
711 constness: constness,
712 unsafety: unsafety,
713 abi: abi,
714 decl: Box::new(FnDecl {
715 dot_tokens: None,
716 fn_token: fn_,
717 paren_token: inputs.1,
718 inputs: inputs.0,
719 output: ret,
720 variadic: false,
721 generics: Generics {
722 where_clause: where_clause,
723 .. generics
724 },
725 }),
726 ident: ident,
727 block: Box::new(Block {
728 brace_token: inner_attrs_stmts.1,
729 stmts: (inner_attrs_stmts.0).1,
730 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800731 })
David Tolnay42602292016-10-01 22:25:45 -0700732 ));
733
Alex Crichton954046c2017-05-30 21:49:42 -0700734 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400735 named!(parse -> Self, alt!(
736 do_parse!(
737 and: syn!(And) >>
738 lt: option!(syn!(Lifetime)) >>
739 mutability: syn!(Mutability) >>
740 self_: syn!(Self_) >>
741 not!(syn!(Colon)) >>
742 (ArgSelfRef {
743 lifetime: lt,
744 mutbl: mutability,
745 and_token: and,
746 self_token: self_,
747 }.into())
748 )
749 |
750 do_parse!(
751 mutability: syn!(Mutability) >>
752 self_: syn!(Self_) >>
753 not!(syn!(Colon)) >>
754 (ArgSelf {
755 mutbl: mutability,
756 self_token: self_,
757 }.into())
758 )
759 |
760 do_parse!(
761 pat: syn!(Pat) >>
762 colon: syn!(Colon) >>
763 ty: syn!(Ty) >>
764 (ArgCaptured {
765 pat: pat,
766 ty: ty,
767 colon_token: colon,
768 }.into())
769 )
770 |
771 syn!(Ty) => { FnArg::Ignored }
772 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700773 }
David Tolnay62f374c2016-10-02 13:37:00 -0700774
David Tolnay4c614be2017-11-10 00:02:38 -0800775 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700776 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
777 vis: syn!(Visibility) >>
778 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700779 ident: syn!(Ident) >>
780 content_semi: alt!(
781 syn!(Semi) => {|semi| (
782 Vec::new(),
783 None,
784 Some(semi),
785 )}
David Tolnay37d10332016-10-13 20:51:04 -0700786 |
Alex Crichton954046c2017-05-30 21:49:42 -0700787 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700788 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700789 many0!(call!(Attribute::parse_inner)),
790 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400791 )
David Tolnay570695e2017-06-03 16:15:13 -0700792 ) => {|((inner_attrs, items), brace)| (
793 inner_attrs,
794 Some((brace, items)),
795 None,
796 )}
David Tolnay37d10332016-10-13 20:51:04 -0700797 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800798 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700799 attrs: {
800 let mut attrs = outer_attrs;
801 attrs.extend(content_semi.0);
802 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700803 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800804 vis: vis,
805 mod_token: mod_,
806 ident: ident,
807 content: content_semi.1,
808 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800809 })
David Tolnay35902302016-10-06 01:11:08 -0700810 ));
811
David Tolnay4c614be2017-11-10 00:02:38 -0800812 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700813 attrs: many0!(call!(Attribute::parse_outer)) >>
814 abi: syn!(Abi) >>
815 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800816 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700817 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800818 abi: abi,
819 brace_token: items.1,
820 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800821 })
David Tolnay35902302016-10-06 01:11:08 -0700822 ));
823
Alex Crichton954046c2017-05-30 21:49:42 -0700824 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400825 named!(parse -> Self, alt!(
826 foreign_fn
827 |
828 foreign_static
829 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700830 }
David Tolnay35902302016-10-06 01:11:08 -0700831
832 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700833 attrs: many0!(call!(Attribute::parse_outer)) >>
834 vis: syn!(Visibility) >>
835 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700836 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700837 generics: syn!(Generics) >>
838 inputs: parens!(do_parse!(
839 args: call!(Delimited::parse_terminated) >>
840 variadic: cond!(args.is_empty() || args.trailing_delim(),
841 option!(syn!(Dot3))) >>
842 (args, variadic)
843 )) >>
844 ret: syn!(FunctionRetTy) >>
845 where_clause: syn!(WhereClause) >>
846 semi: syn!(Semi) >>
847 ({
848 let ((inputs, variadic), parens) = inputs;
849 let variadic = variadic.and_then(|v| v);
850 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700851 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700852 attrs: attrs,
853 semi_token: semi,
854 node: ForeignItemFn {
855 decl: Box::new(FnDecl {
856 fn_token: fn_,
857 paren_token: parens,
858 inputs: inputs,
859 variadic: variadic.is_some(),
860 dot_tokens: variadic,
861 output: ret,
862 generics: Generics {
863 where_clause: where_clause,
864 .. generics
865 },
866 }),
867 }.into(),
868 vis: vis,
869 }
David Tolnay35902302016-10-06 01:11:08 -0700870 })
871 ));
872
873 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700874 attrs: many0!(call!(Attribute::parse_outer)) >>
875 vis: syn!(Visibility) >>
876 static_: syn!(Static) >>
877 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700878 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700879 colon: syn!(Colon) >>
880 ty: syn!(Ty) >>
881 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700882 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700883 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700884 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700885 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700886 node: ForeignItemStatic {
887 ty: Box::new(ty),
888 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700889 static_token: static_,
890 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700892 vis: vis,
893 })
894 ));
895
David Tolnay4c614be2017-11-10 00:02:38 -0800896 impl_synom!(ItemTy "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700897 attrs: many0!(call!(Attribute::parse_outer)) >>
898 vis: syn!(Visibility) >>
899 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700900 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700901 generics: syn!(Generics) >>
902 where_clause: syn!(WhereClause) >>
903 eq: syn!(Eq) >>
904 ty: syn!(Ty) >>
905 semi: syn!(Semi) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800906 (ItemTy {
David Tolnay3cf52982016-10-01 17:11:37 -0700907 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800908 vis: vis,
909 type_token: type_,
910 ident: ident,
911 generics: Generics {
912 where_clause: where_clause,
913 ..generics
914 },
915 eq_token: eq,
916 ty: Box::new(ty),
917 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800918 })
David Tolnay3cf52982016-10-01 17:11:37 -0700919 ));
920
David Tolnay4c614be2017-11-10 00:02:38 -0800921 impl_synom!(ItemStruct "struct item" switch!(
922 map!(syn!(DeriveInput), Into::into),
923 Item::Struct(item) => value!(item)
924 |
925 _ => reject!()
926 ));
David Tolnay42602292016-10-01 22:25:45 -0700927
David Tolnay4c614be2017-11-10 00:02:38 -0800928 impl_synom!(ItemEnum "enum item" switch!(
929 map!(syn!(DeriveInput), Into::into),
930 Item::Enum(item) => value!(item)
931 |
932 _ => reject!()
933 ));
934
935 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700936 attrs: many0!(call!(Attribute::parse_outer)) >>
937 vis: syn!(Visibility) >>
938 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700939 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700940 generics: syn!(Generics) >>
941 where_clause: syn!(WhereClause) >>
942 fields: braces!(call!(Delimited::parse_terminated_with,
943 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800944 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700945 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800946 vis: vis,
947 union_token: union_,
948 ident: ident,
949 generics: Generics {
950 where_clause: where_clause,
951 .. generics
952 },
953 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800954 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700955 ));
956
David Tolnay4c614be2017-11-10 00:02:38 -0800957 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700958 attrs: many0!(call!(Attribute::parse_outer)) >>
959 vis: syn!(Visibility) >>
960 unsafety: syn!(Unsafety) >>
961 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700962 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700963 generics: syn!(Generics) >>
964 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700965 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700966 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700967 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700968 where_clause: syn!(WhereClause) >>
969 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800970 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700971 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800972 vis: vis,
973 unsafety: unsafety,
974 trait_token: trait_,
975 ident: ident,
976 generics: Generics {
977 where_clause: where_clause,
978 .. generics
979 },
980 colon_token: colon,
981 supertraits: bounds.unwrap_or_default(),
982 brace_token: body.1,
983 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800984 })
David Tolnay0aecb732016-10-03 23:03:50 -0700985 ));
986
David Tolnay4c614be2017-11-10 00:02:38 -0800987 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700988 attrs: many0!(call!(Attribute::parse_outer)) >>
989 unsafety: syn!(Unsafety) >>
990 impl_: syn!(Impl) >>
991 path: syn!(Path) >>
992 for_: syn!(For) >>
993 dot2: syn!(Dot2) >>
994 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800995 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700996 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800997 unsafety: unsafety,
998 impl_token: impl_,
999 path: path,
1000 for_token: for_,
1001 dot2_token: dot2,
1002 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001003 })
David Tolnayf94e2362016-10-04 00:29:51 -07001004 ));
1005
David Tolnayda705bd2017-11-10 21:58:05 -08001006 impl_synom!(TraitItem "trait item" alt!(
1007 syn!(TraitItemConst) => { TraitItem::Const }
1008 |
1009 syn!(TraitItemMethod) => { TraitItem::Method }
1010 |
1011 syn!(TraitItemType) => { TraitItem::Type }
1012 |
1013 syn!(TraitItemMac) => { TraitItem::Macro }
1014 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001015
David Tolnayda705bd2017-11-10 21:58:05 -08001016 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001017 attrs: many0!(call!(Attribute::parse_outer)) >>
1018 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001019 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001020 colon: syn!(Colon) >>
1021 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001022 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001023 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001024 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001025 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001026 const_token: const_,
1027 ident: ident,
1028 colon_token: colon,
1029 ty: ty,
1030 default: default,
1031 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001032 })
1033 ));
1034
David Tolnayda705bd2017-11-10 21:58:05 -08001035 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001036 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1037 constness: syn!(Constness) >>
1038 unsafety: syn!(Unsafety) >>
1039 abi: option!(syn!(Abi)) >>
1040 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001041 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001042 generics: syn!(Generics) >>
1043 inputs: parens!(call!(Delimited::parse_terminated)) >>
1044 ret: syn!(FunctionRetTy) >>
1045 where_clause: syn!(WhereClause) >>
1046 body: option!(braces!(
1047 tuple!(many0!(call!(Attribute::parse_inner)),
1048 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001049 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001050 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001051 ({
1052 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001053 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001054 None => (Vec::new(), None),
1055 };
David Tolnayda705bd2017-11-10 21:58:05 -08001056 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001057 attrs: {
1058 let mut attrs = outer_attrs;
1059 attrs.extend(inner_attrs);
1060 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001061 },
David Tolnayda705bd2017-11-10 21:58:05 -08001062 sig: MethodSig {
1063 constness: constness,
1064 unsafety: unsafety,
1065 abi: abi,
1066 ident: ident,
1067 decl: FnDecl {
1068 inputs: inputs.0,
1069 output: ret,
1070 variadic: false,
1071 fn_token: fn_,
1072 paren_token: inputs.1,
1073 dot_tokens: None,
1074 generics: Generics {
1075 where_clause: where_clause,
1076 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001077 },
1078 },
David Tolnayda705bd2017-11-10 21:58:05 -08001079 },
1080 default: stmts.map(|stmts| {
1081 Block {
1082 stmts: stmts.0,
1083 brace_token: stmts.1,
1084 }
1085 }),
1086 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001087 }
David Tolnay0aecb732016-10-03 23:03:50 -07001088 })
1089 ));
1090
David Tolnayda705bd2017-11-10 21:58:05 -08001091 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001092 attrs: many0!(call!(Attribute::parse_outer)) >>
1093 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001094 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001095 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001096 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001097 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001098 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001099 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1100 semi: syn!(Semi) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001101 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001102 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001103 type_token: type_,
1104 ident: ident,
1105 colon_token: colon,
1106 bounds: bounds.unwrap_or_default(),
1107 default: default,
1108 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001109 })
1110 ));
1111
David Tolnayda705bd2017-11-10 21:58:05 -08001112 impl_synom!(TraitItemMac "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001113 attrs: many0!(call!(Attribute::parse_outer)) >>
1114 mac: syn!(Mac) >>
1115 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001116 (TraitItemMac {
David Tolnay0aecb732016-10-03 23:03:50 -07001117 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001118 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001119 })
1120 ));
1121
David Tolnay4c614be2017-11-10 00:02:38 -08001122 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001123 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001124 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001125 unsafety: syn!(Unsafety) >>
1126 impl_: syn!(Impl) >>
1127 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001128 polarity_path: alt!(
1129 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001130 polarity: syn!(ImplPolarity) >>
1131 path: syn!(Path) >>
1132 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001133 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001134 )
1135 |
David Tolnay570695e2017-06-03 16:15:13 -07001136 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001137 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001138 self_ty: syn!(Ty) >>
1139 where_clause: syn!(WhereClause) >>
1140 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001141 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001142 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001143 defaultness: defaultness,
1144 unsafety: unsafety,
1145 impl_token: impl_,
1146 generics: Generics {
1147 where_clause: where_clause,
1148 .. generics
1149 },
1150 trait_: polarity_path,
1151 self_ty: Box::new(self_ty),
1152 brace_token: body.1,
1153 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001154 })
David Tolnay4c9be372016-10-06 00:47:37 -07001155 ));
1156
Alex Crichton954046c2017-05-30 21:49:42 -07001157 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001158 named!(parse -> Self, alt!(
1159 impl_item_const
1160 |
1161 impl_item_method
1162 |
1163 impl_item_type
1164 |
David Tolnay570695e2017-06-03 16:15:13 -07001165 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001166 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001167 }
David Tolnay4c9be372016-10-06 00:47:37 -07001168
1169 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001170 attrs: many0!(call!(Attribute::parse_outer)) >>
1171 vis: syn!(Visibility) >>
1172 defaultness: syn!(Defaultness) >>
1173 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001174 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001175 colon: syn!(Colon) >>
1176 ty: syn!(Ty) >>
1177 eq: syn!(Eq) >>
1178 value: syn!(Expr) >>
1179 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001180 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001181 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001182 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001183 vis: vis,
1184 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001185 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001186 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001187 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001188 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001189 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001190 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001191 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001192 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001193 })
1194 ));
1195
1196 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001197 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1198 vis: syn!(Visibility) >>
1199 defaultness: syn!(Defaultness) >>
1200 constness: syn!(Constness) >>
1201 unsafety: syn!(Unsafety) >>
1202 abi: option!(syn!(Abi)) >>
1203 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001204 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001205 generics: syn!(Generics) >>
1206 inputs: parens!(call!(Delimited::parse_terminated)) >>
1207 ret: syn!(FunctionRetTy) >>
1208 where_clause: syn!(WhereClause) >>
1209 inner_attrs_stmts: braces!(tuple!(
1210 many0!(call!(Attribute::parse_inner)),
1211 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001212 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001213 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001214 attrs: {
1215 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001216 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001217 attrs
1218 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001219 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001220 vis: vis,
1221 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001222 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001223 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001224 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001225 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001226 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001227 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001228 fn_token: fn_,
1229 paren_token: inputs.1,
1230 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001231 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001232 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001233 generics: Generics {
1234 where_clause: where_clause,
1235 .. generics
1236 },
1237 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001238 },
1239 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001240 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001241 brace_token: inner_attrs_stmts.1,
1242 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001243 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001244 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001245 })
1246 ));
1247
1248 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001249 attrs: many0!(call!(Attribute::parse_outer)) >>
1250 vis: syn!(Visibility) >>
1251 defaultness: syn!(Defaultness) >>
1252 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001253 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001254 eq: syn!(Eq) >>
1255 ty: syn!(Ty) >>
1256 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001257 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001258 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001259 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001260 vis: vis,
1261 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001262 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001263 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001264 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001265 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001266 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001267 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001268 })
1269 ));
1270
David Tolnay570695e2017-06-03 16:15:13 -07001271 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001272 attrs: many0!(call!(Attribute::parse_outer)) >>
1273 mac: syn!(Mac) >>
1274 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001275 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001276 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001277 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001278 })
1279 ));
1280
Alex Crichton954046c2017-05-30 21:49:42 -07001281 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001282 named!(parse -> Self, alt!(
1283 syn!(Bang) => { ImplPolarity::Negative }
1284 |
1285 epsilon!() => { |_| ImplPolarity::Positive }
1286 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001287 }
David Tolnay4c9be372016-10-06 00:47:37 -07001288
Alex Crichton954046c2017-05-30 21:49:42 -07001289 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001290 named!(parse -> Self, alt!(
1291 syn!(Const) => { Constness::Const }
1292 |
1293 epsilon!() => { |_| Constness::NotConst }
1294 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001295 }
David Tolnay42602292016-10-01 22:25:45 -07001296
Alex Crichton954046c2017-05-30 21:49:42 -07001297 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001298 named!(parse -> Self, alt!(
1299 syn!(Default_) => { Defaultness::Default }
1300 |
1301 epsilon!() => { |_| Defaultness::Final }
1302 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001303 }
David Tolnayedf2b992016-09-23 20:43:45 -07001304}
David Tolnay4a51dc72016-10-01 00:40:31 -07001305
1306#[cfg(feature = "printing")]
1307mod printing {
1308 use super::*;
1309 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001310 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001311 use quote::{Tokens, ToTokens};
1312
1313 impl ToTokens for Item {
1314 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001315 match *self {
1316 Item::ExternCrate(ref item) => {
1317 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001318 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001319 item.extern_token.to_tokens(tokens);
1320 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001321 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001322 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001323 as_token.to_tokens(tokens);
1324 rename.to_tokens(tokens);
1325 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001326 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001327 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001328 Item::Use(ref item) => {
1329 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001330 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001331 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001332 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001333 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001334 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001335 Item::Static(ref item) => {
1336 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001337 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001338 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001339 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001340 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001341 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001342 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001343 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001344 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001345 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001346 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001347 Item::Const(ref item) => {
1348 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001349 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001350 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001351 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001352 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001353 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001354 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001355 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001356 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001357 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001358 Item::Fn(ref item) => {
1359 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001360 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001361 item.constness.to_tokens(tokens);
1362 item.unsafety.to_tokens(tokens);
1363 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001364 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 item.block.brace_token.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001366 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001367 tokens.append_all(&item.block.stmts);
1368 });
David Tolnay42602292016-10-01 22:25:45 -07001369 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001370 Item::Mod(ref item) => {
1371 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001372 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001373 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001374 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001375 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001376 brace.surround(tokens, |tokens| {
David Tolnayc6b55bc2017-11-09 22:48:38 -08001377 tokens.append_all(item.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001378 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001379 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001380 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001381 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001382 }
David Tolnay35902302016-10-06 01:11:08 -07001383 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001384 Item::ForeignMod(ref item) => {
1385 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001386 item.abi.to_tokens(tokens);
1387 item.brace_token.surround(tokens, |tokens| {
1388 tokens.append_all(&item.items);
1389 });
David Tolnay35902302016-10-06 01:11:08 -07001390 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001391 Item::Ty(ref item) => {
1392 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001393 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001394 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001395 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001396 item.generics.to_tokens(tokens);
1397 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001398 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001399 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001400 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001401 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001402 Item::Enum(ref item) => {
1403 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001404 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001405 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001406 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001407 item.generics.to_tokens(tokens);
1408 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001409 item.brace_token.surround(tokens, |tokens| {
1410 item.variants.to_tokens(tokens);
1411 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001412 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001413 Item::Struct(ref item) => {
1414 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001415 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001417 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001418 item.generics.to_tokens(tokens);
1419 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001420 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001421 item.generics.where_clause.to_tokens(tokens);
1422 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001423 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001425 item.data.to_tokens(tokens);
1426 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001427 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001428 }
1429 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001430 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001431 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001432 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001433 }
1434 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001435 Item::Union(ref item) => {
1436 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001437 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001438 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001439 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001440 item.generics.to_tokens(tokens);
1441 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001442 // XXX: Should we handle / complain when using a
1443 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001444 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001445 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001446 Item::Trait(ref item) => {
1447 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001448 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001449 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001450 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001451 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001452 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001453 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001454 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001455 item.supertraits.to_tokens(tokens);
1456 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001457 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001458 item.brace_token.surround(tokens, |tokens| {
1459 tokens.append_all(&item.items);
1460 });
David Tolnayca085422016-10-04 00:12:38 -07001461 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001462 Item::DefaultImpl(ref item) => {
1463 tokens.append_all(item.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001464 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001465 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001466 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001467 item.for_token.to_tokens(tokens);
1468 item.dot2_token.to_tokens(tokens);
1469 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001470 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001471 Item::Impl(ref item) => {
1472 tokens.append_all(item.attrs.outer());
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001473 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001474 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001475 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001476 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001477 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001478 polarity.to_tokens(tokens);
1479 path.to_tokens(tokens);
1480 for_token.to_tokens(tokens);
1481 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001482 item.self_ty.to_tokens(tokens);
1483 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001484 item.brace_token.surround(tokens, |tokens| {
1485 tokens.append_all(&item.items);
1486 });
David Tolnay4c9be372016-10-06 00:47:37 -07001487 }
David Tolnayc6b55bc2017-11-09 22:48:38 -08001488 Item::Mac(ref item) => {
1489 tokens.append_all(item.attrs.outer());
1490 item.mac.path.to_tokens(tokens);
1491 item.mac.bang_token.to_tokens(tokens);
1492 item.mac.ident.to_tokens(tokens);
1493 tokens.append_all(&item.mac.tokens);
1494 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001495 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001496 }
1497 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001498 }
1499 }
1500 }
David Tolnay42602292016-10-01 22:25:45 -07001501
Alex Crichton62a0a592017-05-22 13:58:53 -07001502 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001503 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001504 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001505 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001506 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001507 self.rename.to_tokens(tokens);
1508 }
David Tolnay4a057422016-10-08 00:02:31 -07001509 }
1510 }
1511
Alex Crichton62a0a592017-05-22 13:58:53 -07001512 impl ToTokens for PathGlob {
1513 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001514 if self.path.segments.len() > 0 {
1515 self.path.to_tokens(tokens);
1516 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1517 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001518 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001519 }
1520 }
1521
1522 impl ToTokens for PathList {
1523 fn to_tokens(&self, tokens: &mut Tokens) {
1524 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001525 self.colon2_token.to_tokens(tokens);
1526 self.brace_token.surround(tokens, |tokens| {
1527 self.items.to_tokens(tokens);
1528 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001529 }
1530 }
1531
David Tolnay4a057422016-10-08 00:02:31 -07001532 impl ToTokens for PathListItem {
1533 fn to_tokens(&self, tokens: &mut Tokens) {
1534 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001535 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001536 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001537 self.rename.to_tokens(tokens);
1538 }
David Tolnay4a057422016-10-08 00:02:31 -07001539 }
1540 }
1541
David Tolnayca085422016-10-04 00:12:38 -07001542 impl ToTokens for TraitItem {
1543 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayda705bd2017-11-10 21:58:05 -08001544 match *self {
1545 TraitItem::Const(ref item) => {
1546 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001547 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001548 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001549 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001550 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001551 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001552 eq_token.to_tokens(tokens);
1553 default.to_tokens(tokens);
1554 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001555 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001556 }
David Tolnayda705bd2017-11-10 21:58:05 -08001557 TraitItem::Method(ref item) => {
1558 tokens.append_all(item.attrs.outer());
David Tolnay570695e2017-06-03 16:15:13 -07001559 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001561 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001562 block.brace_token.surround(tokens, |tokens| {
David Tolnayda705bd2017-11-10 21:58:05 -08001563 tokens.append_all(item.attrs.inner());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001564 tokens.append_all(&block.stmts);
1565 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001566 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001567 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001568 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 }
David Tolnayca085422016-10-04 00:12:38 -07001570 }
1571 }
David Tolnayda705bd2017-11-10 21:58:05 -08001572 TraitItem::Type(ref item) => {
1573 tokens.append_all(item.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001574 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001575 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001576 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001577 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001578 item.bounds.to_tokens(tokens);
1579 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001580 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001581 eq_token.to_tokens(tokens);
1582 default.to_tokens(tokens);
1583 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001585 }
David Tolnayda705bd2017-11-10 21:58:05 -08001586 TraitItem::Macro(ref item) => {
1587 tokens.append_all(item.attrs.outer());
1588 item.mac.to_tokens(tokens);
1589 if !item.mac.is_braced() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001590 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001591 }
David Tolnayca085422016-10-04 00:12:38 -07001592 }
1593 }
1594 }
1595 }
1596
David Tolnay4c9be372016-10-06 00:47:37 -07001597 impl ToTokens for ImplItem {
1598 fn to_tokens(&self, tokens: &mut Tokens) {
1599 tokens.append_all(self.attrs.outer());
1600 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001601 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001602 item.vis.to_tokens(tokens);
1603 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001604 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001605 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001606 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001608 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001609 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001610 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001611 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001612 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001613 item.vis.to_tokens(tokens);
1614 item.defaultness.to_tokens(tokens);
1615 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001616 item.block.brace_token.surround(tokens, |tokens| {
1617 tokens.append_all(self.attrs.inner());
1618 tokens.append_all(&item.block.stmts);
1619 });
David Tolnay4c9be372016-10-06 00:47:37 -07001620 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001621 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001622 item.vis.to_tokens(tokens);
1623 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001624 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001625 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001626 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001627 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001628 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001629 }
1630 ImplItemKind::Macro(ref mac) => {
1631 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001632 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001633 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001634 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001635 }
1636 }
1637 }
1638 }
1639 }
1640
David Tolnay35902302016-10-06 01:11:08 -07001641 impl ToTokens for ForeignItem {
1642 fn to_tokens(&self, tokens: &mut Tokens) {
1643 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001644 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001645 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001646 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001647 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001648 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001650 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001651 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001652 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001653 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001655 }
1656 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001657 self.semi_token.to_tokens(tokens);
1658 }
1659 }
1660
David Tolnay570695e2017-06-03 16:15:13 -07001661 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001662 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001663 self.constness.to_tokens(tokens);
1664 self.unsafety.to_tokens(tokens);
1665 self.abi.to_tokens(tokens);
1666 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001667 }
1668 }
1669
David Tolnay570695e2017-06-03 16:15:13 -07001670 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001671
1672 impl<'a> ToTokens for NamedDecl<'a> {
1673 fn to_tokens(&self, tokens: &mut Tokens) {
1674 self.0.fn_token.to_tokens(tokens);
1675 self.1.to_tokens(tokens);
1676 self.0.generics.to_tokens(tokens);
1677 self.0.paren_token.surround(tokens, |tokens| {
1678 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001679
1680 if self.0.variadic {
1681 if !self.0.inputs.empty_or_trailing() {
1682 tokens::Comma::default().to_tokens(tokens);
1683 }
Alex Crichton259ee532017-07-14 06:51:02 -07001684 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001685 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001686 });
1687 self.0.output.to_tokens(tokens);
1688 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001689 }
1690 }
1691
Alex Crichton62a0a592017-05-22 13:58:53 -07001692 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001693 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001694 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001695 self.lifetime.to_tokens(tokens);
1696 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001697 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001698 }
1699 }
1700
1701 impl ToTokens for ArgSelf {
1702 fn to_tokens(&self, tokens: &mut Tokens) {
1703 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001704 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001705 }
1706 }
1707
1708 impl ToTokens for ArgCaptured {
1709 fn to_tokens(&self, tokens: &mut Tokens) {
1710 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001712 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001713 }
1714 }
1715
David Tolnay42602292016-10-01 22:25:45 -07001716 impl ToTokens for Constness {
1717 fn to_tokens(&self, tokens: &mut Tokens) {
1718 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001719 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001720 Constness::NotConst => {
1721 // nothing
1722 }
David Tolnay42602292016-10-01 22:25:45 -07001723 }
1724 }
1725 }
1726
David Tolnay4c9be372016-10-06 00:47:37 -07001727 impl ToTokens for Defaultness {
1728 fn to_tokens(&self, tokens: &mut Tokens) {
1729 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001730 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001731 Defaultness::Final => {
1732 // nothing
1733 }
1734 }
1735 }
1736 }
1737
1738 impl ToTokens for ImplPolarity {
1739 fn to_tokens(&self, tokens: &mut Tokens) {
1740 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001741 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001742 ImplPolarity::Positive => {
1743 // nothing
1744 }
1745 }
1746 }
1747 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001748}