blob: dca8543e1af829630c253e393652f6dcac64c94e [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_struct! {
David Tolnay570695e2017-06-03 16:15:13 -07005 /// Things that can appear directly inside of a module.
Alex Crichton62a0a592017-05-22 13:58:53 -07006 pub struct Item {
Alex Crichton62a0a592017-05-22 13:58:53 -07007 pub attrs: Vec<Attribute>,
8 pub node: ItemKind,
9 }
David Tolnayb79ee962016-09-04 09:39:20 -070010}
11
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_enum_of_structs! {
13 pub enum ItemKind {
David Tolnay570695e2017-06-03 16:15:13 -070014 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070015 ///
16 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
17 pub ExternCrate(ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -070018 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070019 pub extern_token: tokens::Extern,
20 pub crate_token: tokens::Crate,
David Tolnay570695e2017-06-03 16:15:13 -070021 pub ident: Ident,
22 pub rename: Option<(tokens::As, Ident)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070023 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }),
25 /// A use declaration (`use` or `pub use`) item.
26 ///
27 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
28 pub Use(ItemUse {
David Tolnay570695e2017-06-03 16:15:13 -070029 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070030 pub use_token: tokens::Use,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 pub path: Box<ViewPath>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070032 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070033 }),
34 /// A static item (`static` or `pub static`).
35 ///
36 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
37 pub Static(ItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -070038 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070039 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -070040 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070041 pub ident: Ident,
42 pub colon_token: tokens::Colon,
43 pub ty: Box<Ty>,
44 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070045 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070046 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }),
48 /// A constant item (`const` or `pub const`).
49 ///
50 /// E.g. `const FOO: i32 = 42;`
51 pub Const(ItemConst {
David Tolnay570695e2017-06-03 16:15:13 -070052 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070053 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -070054 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070055 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -070056 pub ty: Box<Ty>,
David Tolnay570695e2017-06-03 16:15:13 -070057 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -070058 pub expr: Box<Expr>,
David Tolnay570695e2017-06-03 16:15:13 -070059 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070060 }),
61 /// A function declaration (`fn` or `pub fn`).
62 ///
63 /// E.g. `fn foo(bar: usize) -> usize { .. }`
64 pub Fn(ItemFn {
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 Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070078 pub mod_token: tokens::Mod,
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
80 pub content: Option<(tokens::Brace, Vec<Item>)>,
81 pub semi: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -070082 }),
83 /// An external module (`extern` or `pub extern`).
84 ///
85 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086 pub ForeignMod(ItemForeignMod {
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 Tolnay570695e2017-06-03 16:15:13 -070095 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070096 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -070097 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -070099 pub eq_token: tokens::Eq,
100 pub ty: Box<Ty>,
101 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 }),
103 /// An enum definition (`enum` or `pub enum`).
104 ///
105 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
106 pub Enum(ItemEnum {
David Tolnay570695e2017-06-03 16:15:13 -0700107 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108 pub enum_token: tokens::Enum,
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub ident: Ident,
110 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111 pub brace_token: tokens::Brace,
112 pub variants: Delimited<Variant, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
114 /// A struct definition (`struct` or `pub struct`).
115 ///
116 /// E.g. `struct Foo<A> { x: A }`
117 pub Struct(ItemStruct {
David Tolnay570695e2017-06-03 16:15:13 -0700118 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700119 pub struct_token: tokens::Struct,
David Tolnay570695e2017-06-03 16:15:13 -0700120 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700122 pub data: VariantData,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700123 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 }),
125 /// A union definition (`union` or `pub union`).
126 ///
127 /// E.g. `union Foo<A, B> { x: A, y: B }`
128 pub Union(ItemUnion {
David Tolnay570695e2017-06-03 16:15:13 -0700129 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700130 pub union_token: tokens::Union,
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 }),
135 /// A Trait declaration (`trait` or `pub trait`).
136 ///
137 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
138 pub Trait(ItemTrait {
David Tolnay570695e2017-06-03 16:15:13 -0700139 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700141 pub trait_token: tokens::Trait,
142 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700143 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700144 pub colon_token: Option<tokens::Colon>,
145 pub supertraits: Delimited<TyParamBound, tokens::Add>,
146 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 pub items: Vec<TraitItem>,
148 }),
149 /// Default trait implementation.
150 ///
151 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
152 pub DefaultImpl(ItemDefaultImpl {
153 pub unsafety: Unsafety,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700154 pub impl_token: tokens::Impl,
David Tolnay570695e2017-06-03 16:15:13 -0700155 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 pub for_token: tokens::For,
157 pub dot2_token: tokens::Dot2,
158 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 }),
160 /// An implementation.
161 ///
162 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
163 pub Impl(ItemImpl {
164 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700165 pub impl_token: tokens::Impl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700167 /// Trait this impl implements.
168 pub trait_: Option<(ImplPolarity, Path, tokens::For)>,
169 /// The Self type of the impl.
170 pub self_ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700171 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub items: Vec<ImplItem>,
173 }),
174 /// A macro invocation (which includes macro definition).
175 ///
176 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
177 pub Mac(Mac),
178 }
179
180 do_not_generate_to_tokens
David Tolnayb79ee962016-09-04 09:39:20 -0700181}
182
David Tolnay0e837402016-12-22 17:25:55 -0500183impl From<DeriveInput> for Item {
184 fn from(input: DeriveInput) -> Item {
David Tolnay453cfd12016-10-23 11:00:14 -0700185 Item {
David Tolnay453cfd12016-10-23 11:00:14 -0700186 attrs: input.attrs,
187 node: match input.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700188 Body::Enum(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 ItemEnum {
David Tolnay570695e2017-06-03 16:15:13 -0700190 vis: input.vis,
191 enum_token: data.enum_token,
192 ident: input.ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700193 generics: input.generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700194 brace_token: data.brace_token,
David Tolnay570695e2017-06-03 16:15:13 -0700195 variants: data.variants,
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 }.into()
197 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700198 Body::Struct(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700199 ItemStruct {
David Tolnay570695e2017-06-03 16:15:13 -0700200 vis: input.vis,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700201 struct_token: data.struct_token,
David Tolnay570695e2017-06-03 16:15:13 -0700202 ident: input.ident,
203 generics: input.generics,
204 data: data.data,
205 semi_token: data.semi_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 }.into()
207 }
David Tolnay453cfd12016-10-23 11:00:14 -0700208 },
209 }
210 }
211}
212
Alex Crichton62a0a592017-05-22 13:58:53 -0700213ast_enum_of_structs! {
214 pub enum ViewPath {
215 /// `foo::bar::baz as quux`
216 ///
217 /// or just
218 ///
219 /// `foo::bar::baz` (with `as baz` implicitly on the right)
220 pub Simple(PathSimple {
221 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700222 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 pub rename: Option<Ident>,
224 }),
225
226 /// `foo::bar::*`
227 pub Glob(PathGlob {
228 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700229 pub colon2_token: tokens::Colon2,
230 pub star_token: tokens::Star,
Alex Crichton62a0a592017-05-22 13:58:53 -0700231 }),
232
233 /// `foo::bar::{a, b, c}`
234 pub List(PathList {
235 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700236 pub colon2_token: tokens::Colon2,
237 pub brace_token: tokens::Brace,
238 pub items: Delimited<PathListItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700239 }),
240 }
241}
242
243ast_struct! {
244 pub struct PathListItem {
245 pub name: Ident,
246 /// renamed in list, e.g. `use foo::{bar as baz};`
247 pub rename: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700248 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 }
250}
251
252ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700253 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 pub enum Constness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700255 Const(tokens::Const),
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 NotConst,
257 }
258}
259
260ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700261 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 pub enum Defaultness {
Alex Crichton954046c2017-05-30 21:49:42 -0700263 Default(tokens::Default_),
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 Final,
265 }
266}
267
268ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 pub struct ForeignItem {
270 pub ident: Ident,
271 pub attrs: Vec<Attribute>,
272 pub node: ForeignItemKind,
273 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700274 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 }
276}
277
278ast_enum_of_structs! {
279 /// An item within an `extern` block
280 pub enum ForeignItemKind {
281 /// A foreign function
282 pub Fn(ForeignItemFn {
283 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 }),
285 /// A foreign static item (`static ext: u8`)
286 pub Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700287 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700289 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 pub mutbl: Mutability,
291 }),
292 }
293
294 do_not_generate_to_tokens
295}
296
297ast_struct! {
298 /// Represents an item declaration within a trait declaration,
299 /// possibly including a default implementation. A trait item is
300 /// either required (meaning it doesn't have an implementation, just a
301 /// signature) or provided (meaning it has a default implementation).
302 pub struct TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 pub attrs: Vec<Attribute>,
304 pub node: TraitItemKind,
305 }
306}
307
308ast_enum_of_structs! {
309 pub enum TraitItemKind {
310 pub Const(TraitItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700311 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700312 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700313 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700315 pub default: Option<(tokens::Eq, Expr)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700316 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 }),
318 pub Method(TraitItemMethod {
319 pub sig: MethodSig,
320 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700321 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
323 pub Type(TraitItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700324 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700325 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700326 pub colon_token: Option<tokens::Colon>,
327 pub bounds: Delimited<TyParamBound, tokens::Add>,
David Tolnay570695e2017-06-03 16:15:13 -0700328 pub default: Option<(tokens::Eq, Ty)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700329 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 }),
331 pub Macro(Mac),
332 }
333
334 do_not_generate_to_tokens
335}
336
337ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700338 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 pub enum ImplPolarity {
340 /// `impl Trait for Type`
341 Positive,
342 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700343 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700344 }
345}
346
347ast_struct! {
348 pub struct ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub attrs: Vec<Attribute>,
350 pub node: ImplItemKind,
351 }
352}
353
354ast_enum_of_structs! {
355 pub enum ImplItemKind {
356 pub Const(ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700357 pub vis: Visibility,
358 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700359 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700360 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700361 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700363 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700364 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700365 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 }),
367 pub Method(ImplItemMethod {
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 Tolnay570695e2017-06-03 16:15:13 -0700374 pub vis: Visibility,
375 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700376 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700377 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700378 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700380 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
382 pub Macro(Mac),
383 }
384
385 do_not_generate_to_tokens
386}
387
388ast_struct! {
389 /// Represents a method's signature in a trait declaration,
390 /// or in an implementation.
391 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700393 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700395 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }
398}
399
400ast_struct! {
401 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700402 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 /// E.g. `fn foo(bar: baz)`
404 pub struct FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -0700405 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700406 pub paren_token: tokens::Paren,
407 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700408 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700409 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700410 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700411 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700413}
414
Alex Crichton62a0a592017-05-22 13:58:53 -0700415ast_enum_of_structs! {
416 /// An argument in a function header.
417 ///
418 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
419 pub enum FnArg {
420 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700421 pub and_token: tokens::And,
422 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 pub lifetime: Option<Lifetime>,
424 pub mutbl: Mutability,
425 }),
426 pub SelfValue(ArgSelf {
427 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700428 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 }),
430 pub Captured(ArgCaptured {
431 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700432 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 pub ty: Ty,
434 }),
435 pub Ignored(Ty),
436 }
David Tolnay62f374c2016-10-02 13:37:00 -0700437}
438
David Tolnayedf2b992016-09-23 20:43:45 -0700439#[cfg(feature = "parsing")]
440pub mod parsing {
441 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700442
Michael Layzell92639a52017-06-01 00:07:44 -0400443 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700444 use synom::tokens::*;
445 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700446
Alex Crichton954046c2017-05-30 21:49:42 -0700447 impl Synom for Item {
Michael Layzell92639a52017-06-01 00:07:44 -0400448 named!(parse -> Self, alt!(
449 item_extern_crate
450 |
451 item_use
452 |
453 item_static
454 |
455 item_const
456 |
457 item_fn
458 |
459 item_mod
460 |
461 item_foreign_mod
462 |
463 item_ty
464 |
465 item_struct_or_enum
466 |
467 item_union
468 |
469 item_trait
470 |
471 item_default_impl
472 |
473 item_impl
474 |
475 item_mac
476 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700477
478 fn description() -> Option<&'static str> {
479 Some("item")
480 }
481 }
David Tolnay453cfd12016-10-23 11:00:14 -0700482
David Tolnay84aa0752016-10-02 23:01:13 -0700483 named!(item_mac -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700484 attrs: many0!(call!(Attribute::parse_outer)) >>
485 what: syn!(Path) >>
486 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700487 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700488 body: call!(::TokenTree::parse_delimited) >>
489 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700490 (Item {
David Tolnay84aa0752016-10-02 23:01:13 -0700491 attrs: attrs,
492 node: ItemKind::Mac(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -0500493 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700494 bang_token: bang,
495 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700496 tokens: vec![body],
David Tolnay84aa0752016-10-02 23:01:13 -0700497 }),
498 })
David Tolnayedf2b992016-09-23 20:43:45 -0700499 ));
500
David Tolnaya96a3fa2016-09-24 07:17:42 -0700501 named!(item_extern_crate -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700502 attrs: many0!(call!(Attribute::parse_outer)) >>
503 vis: syn!(Visibility) >>
504 extern_: syn!(Extern) >>
505 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700506 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700507 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
508 semi: syn!(Semi) >>
David Tolnay570695e2017-06-03 16:15:13 -0700509 (Item {
510 attrs: attrs,
511 node: ItemExternCrate {
David Tolnayedf2b992016-09-23 20:43:45 -0700512 vis: vis,
David Tolnay570695e2017-06-03 16:15:13 -0700513 extern_token: extern_,
514 crate_token: crate_,
515 ident: ident,
516 rename: rename,
517 semi_token: semi,
518 }.into(),
David Tolnayedf2b992016-09-23 20:43:45 -0700519 })
520 ));
521
David Tolnay4a057422016-10-08 00:02:31 -0700522 named!(item_use -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700523 attrs: many0!(call!(Attribute::parse_outer)) >>
524 vis: syn!(Visibility) >>
525 use_: syn!(Use) >>
526 what: syn!(ViewPath) >>
527 semi: syn!(Semi) >>
David Tolnay4a057422016-10-08 00:02:31 -0700528 (Item {
David Tolnay4a057422016-10-08 00:02:31 -0700529 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700530 node: ItemUse {
David Tolnay570695e2017-06-03 16:15:13 -0700531 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700532 use_token: use_,
David Tolnay570695e2017-06-03 16:15:13 -0700533 path: Box::new(what),
Alex Crichton954046c2017-05-30 21:49:42 -0700534 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700535 }.into(),
David Tolnay4a057422016-10-08 00:02:31 -0700536 })
537 ));
538
Alex Crichton954046c2017-05-30 21:49:42 -0700539 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400540 named!(parse -> Self, alt!(
541 syn!(PathGlob) => { ViewPath::Glob }
542 |
543 syn!(PathList) => { ViewPath::List }
544 |
545 syn!(PathSimple) => { ViewPath::Simple } // must be last
546 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700547 }
David Tolnay4a057422016-10-08 00:02:31 -0700548
Alex Crichton954046c2017-05-30 21:49:42 -0700549 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400550 named!(parse -> Self, do_parse!(
551 path: syn!(Path) >>
552 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
553 (PathSimple {
554 path: path,
555 as_token: rename.as_ref().map(|p| As((p.0).0)),
556 rename: rename.map(|p| p.1),
557 })
558 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700559 }
David Tolnay4a057422016-10-08 00:02:31 -0700560
Alex Crichton954046c2017-05-30 21:49:42 -0700561 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400562 named!(parse -> Self, do_parse!(
563 path: syn!(Path) >>
564 colon2: syn!(Colon2) >>
565 star: syn!(Star) >>
566 (PathGlob {
567 path: path,
568 colon2_token: colon2,
569 star_token: star,
570 })
571 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700572 }
David Tolnay4a057422016-10-08 00:02:31 -0700573
Alex Crichton954046c2017-05-30 21:49:42 -0700574 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400575 named!(parse -> Self, alt!(
576 do_parse!(
577 path: syn!(Path) >>
578 colon2: syn!(Colon2) >>
579 items: braces!(call!(Delimited::parse_terminated)) >>
580 (PathList {
581 path: path,
582 items: items.0,
583 brace_token: items.1,
584 colon2_token: colon2,
585 })
586 )
587 |
588 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700589 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400590 items: braces!(call!(Delimited::parse_terminated)) >>
591 (PathList {
592 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400593 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700594 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400595 },
David Tolnay570695e2017-06-03 16:15:13 -0700596 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400597 brace_token: items.1,
598 items: items.0,
599 })
600 )
601 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700602 }
David Tolnay4a057422016-10-08 00:02:31 -0700603
Alex Crichton954046c2017-05-30 21:49:42 -0700604 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400605 named!(parse -> Self, do_parse!(
606 name: alt!(
607 syn!(Ident)
608 |
609 map!(syn!(Self_), Into::into)
610 ) >>
611 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
612 (PathListItem {
613 name: name,
614 as_token: rename.as_ref().map(|p| As((p.0).0)),
615 rename: rename.map(|p| p.1),
616 })
617 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700618 }
David Tolnay4a057422016-10-08 00:02:31 -0700619
David Tolnay47a877c2016-10-01 16:50:55 -0700620 named!(item_static -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700621 attrs: many0!(call!(Attribute::parse_outer)) >>
622 vis: syn!(Visibility) >>
623 static_: syn!(Static) >>
624 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700625 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700626 colon: syn!(Colon) >>
627 ty: syn!(Ty) >>
628 eq: syn!(Eq) >>
629 value: syn!(Expr) >>
630 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700631 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700632 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 node: ItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700634 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700635 static_token: static_,
David Tolnay570695e2017-06-03 16:15:13 -0700636 mutbl: mutability,
637 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700638 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700639 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700640 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700641 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700642 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700643 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700644 })
645 ));
646
647 named!(item_const -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700648 attrs: many0!(call!(Attribute::parse_outer)) >>
649 vis: syn!(Visibility) >>
650 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700651 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700652 colon: syn!(Colon) >>
653 ty: syn!(Ty) >>
654 eq: syn!(Eq) >>
655 value: syn!(Expr) >>
656 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700657 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700658 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700659 node: ItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700660 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700661 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -0700662 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700663 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700664 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700665 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700666 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700667 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700668 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700669 })
670 ));
671
David Tolnay42602292016-10-01 22:25:45 -0700672 named!(item_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)) >>
678 fn_: syn!(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) >>
682 ret: syn!(FunctionRetTy) >>
683 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 Tolnay42602292016-10-01 22:25:45 -0700688 (Item {
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 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 node: ItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700695 vis: vis,
696 constness: constness,
697 unsafety: unsafety,
698 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700699 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700700 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700701 fn_token: fn_,
702 paren_token: inputs.1,
703 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700704 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700705 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700706 generics: Generics {
707 where_clause: where_clause,
708 .. generics
709 },
David Tolnay42602292016-10-01 22:25:45 -0700710 }),
David Tolnay570695e2017-06-03 16:15:13 -0700711 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 block: Box::new(Block {
Alex Crichton954046c2017-05-30 21:49:42 -0700713 brace_token: inner_attrs_stmts.1,
714 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -0700715 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700717 })
718 ));
719
Alex Crichton954046c2017-05-30 21:49:42 -0700720 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400721 named!(parse -> Self, alt!(
722 do_parse!(
723 and: syn!(And) >>
724 lt: option!(syn!(Lifetime)) >>
725 mutability: syn!(Mutability) >>
726 self_: syn!(Self_) >>
727 not!(syn!(Colon)) >>
728 (ArgSelfRef {
729 lifetime: lt,
730 mutbl: mutability,
731 and_token: and,
732 self_token: self_,
733 }.into())
734 )
735 |
736 do_parse!(
737 mutability: syn!(Mutability) >>
738 self_: syn!(Self_) >>
739 not!(syn!(Colon)) >>
740 (ArgSelf {
741 mutbl: mutability,
742 self_token: self_,
743 }.into())
744 )
745 |
746 do_parse!(
747 pat: syn!(Pat) >>
748 colon: syn!(Colon) >>
749 ty: syn!(Ty) >>
750 (ArgCaptured {
751 pat: pat,
752 ty: ty,
753 colon_token: colon,
754 }.into())
755 )
756 |
757 syn!(Ty) => { FnArg::Ignored }
758 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700759 }
David Tolnay62f374c2016-10-02 13:37:00 -0700760
David Tolnay35902302016-10-06 01:11:08 -0700761 named!(item_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700762 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
763 vis: syn!(Visibility) >>
764 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700765 ident: syn!(Ident) >>
766 content_semi: alt!(
767 syn!(Semi) => {|semi| (
768 Vec::new(),
769 None,
770 Some(semi),
771 )}
David Tolnay37d10332016-10-13 20:51:04 -0700772 |
Alex Crichton954046c2017-05-30 21:49:42 -0700773 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700774 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700775 many0!(call!(Attribute::parse_inner)),
776 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400777 )
David Tolnay570695e2017-06-03 16:15:13 -0700778 ) => {|((inner_attrs, items), brace)| (
779 inner_attrs,
780 Some((brace, items)),
781 None,
782 )}
David Tolnay37d10332016-10-13 20:51:04 -0700783 ) >>
David Tolnay570695e2017-06-03 16:15:13 -0700784 (Item {
785 attrs: {
786 let mut attrs = outer_attrs;
787 attrs.extend(content_semi.0);
788 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700789 },
David Tolnay570695e2017-06-03 16:15:13 -0700790 node: ItemMod {
David Tolnay7b8009b2016-10-25 22:36:00 -0700791 vis: vis,
David Tolnay570695e2017-06-03 16:15:13 -0700792 mod_token: mod_,
793 ident: ident,
794 content: content_semi.1,
795 semi: content_semi.2,
796 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700797 })
798 ));
799
800 named!(item_foreign_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700801 attrs: many0!(call!(Attribute::parse_outer)) >>
802 abi: syn!(Abi) >>
803 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay35902302016-10-06 01:11:08 -0700804 (Item {
David Tolnay35902302016-10-06 01:11:08 -0700805 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700806 node: ItemForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700807 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -0700808 brace_token: items.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700809 items: items.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700810 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700811 })
812 ));
813
Alex Crichton954046c2017-05-30 21:49:42 -0700814 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400815 named!(parse -> Self, alt!(
816 foreign_fn
817 |
818 foreign_static
819 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700820 }
David Tolnay35902302016-10-06 01:11:08 -0700821
822 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700823 attrs: many0!(call!(Attribute::parse_outer)) >>
824 vis: syn!(Visibility) >>
825 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700826 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700827 generics: syn!(Generics) >>
828 inputs: parens!(do_parse!(
829 args: call!(Delimited::parse_terminated) >>
830 variadic: cond!(args.is_empty() || args.trailing_delim(),
831 option!(syn!(Dot3))) >>
832 (args, variadic)
833 )) >>
834 ret: syn!(FunctionRetTy) >>
835 where_clause: syn!(WhereClause) >>
836 semi: syn!(Semi) >>
837 ({
838 let ((inputs, variadic), parens) = inputs;
839 let variadic = variadic.and_then(|v| v);
840 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700841 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700842 attrs: attrs,
843 semi_token: semi,
844 node: ForeignItemFn {
845 decl: Box::new(FnDecl {
846 fn_token: fn_,
847 paren_token: parens,
848 inputs: inputs,
849 variadic: variadic.is_some(),
850 dot_tokens: variadic,
851 output: ret,
852 generics: Generics {
853 where_clause: where_clause,
854 .. generics
855 },
856 }),
857 }.into(),
858 vis: vis,
859 }
David Tolnay35902302016-10-06 01:11:08 -0700860 })
861 ));
862
863 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700864 attrs: many0!(call!(Attribute::parse_outer)) >>
865 vis: syn!(Visibility) >>
866 static_: syn!(Static) >>
867 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700868 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700869 colon: syn!(Colon) >>
870 ty: syn!(Ty) >>
871 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700872 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700873 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700874 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700875 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700876 node: ForeignItemStatic {
877 ty: Box::new(ty),
878 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700879 static_token: static_,
880 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700881 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700882 vis: vis,
883 })
884 ));
885
David Tolnay3cf52982016-10-01 17:11:37 -0700886 named!(item_ty -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700887 attrs: many0!(call!(Attribute::parse_outer)) >>
888 vis: syn!(Visibility) >>
889 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700890 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700891 generics: syn!(Generics) >>
892 where_clause: syn!(WhereClause) >>
893 eq: syn!(Eq) >>
894 ty: syn!(Ty) >>
895 semi: syn!(Semi) >>
David Tolnay3cf52982016-10-01 17:11:37 -0700896 (Item {
David Tolnay3cf52982016-10-01 17:11:37 -0700897 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700898 node: ItemTy {
David Tolnay570695e2017-06-03 16:15:13 -0700899 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700900 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -0700901 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700902 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700903 where_clause: where_clause,
904 ..generics
905 },
David Tolnay570695e2017-06-03 16:15:13 -0700906 eq_token: eq,
907 ty: Box::new(ty),
908 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700909 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700910 })
911 ));
912
David Tolnay570695e2017-06-03 16:15:13 -0700913 named!(item_struct_or_enum -> Item, map!(syn!(DeriveInput), Into::into));
David Tolnay42602292016-10-01 22:25:45 -0700914
David Tolnay2f9fa632016-10-03 22:08:48 -0700915 named!(item_union -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700916 attrs: many0!(call!(Attribute::parse_outer)) >>
917 vis: syn!(Visibility) >>
918 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700919 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700920 generics: syn!(Generics) >>
921 where_clause: syn!(WhereClause) >>
922 fields: braces!(call!(Delimited::parse_terminated_with,
923 Field::parse_struct)) >>
David Tolnay2f9fa632016-10-03 22:08:48 -0700924 (Item {
David Tolnay2f9fa632016-10-03 22:08:48 -0700925 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700926 node: ItemUnion {
David Tolnay570695e2017-06-03 16:15:13 -0700927 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700928 union_token: union_,
David Tolnay570695e2017-06-03 16:15:13 -0700929 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700930 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700931 where_clause: where_clause,
932 .. generics
933 },
David Tolnay570695e2017-06-03 16:15:13 -0700934 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700935 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700936 })
937 ));
938
David Tolnay0aecb732016-10-03 23:03:50 -0700939 named!(item_trait -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700940 attrs: many0!(call!(Attribute::parse_outer)) >>
941 vis: syn!(Visibility) >>
942 unsafety: syn!(Unsafety) >>
943 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700944 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700945 generics: syn!(Generics) >>
946 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700947 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700948 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700949 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700950 where_clause: syn!(WhereClause) >>
951 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700952 (Item {
David Tolnay0aecb732016-10-03 23:03:50 -0700953 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 node: ItemTrait {
David Tolnay570695e2017-06-03 16:15:13 -0700955 vis: vis,
Alex Crichton62a0a592017-05-22 13:58:53 -0700956 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700957 trait_token: trait_,
958 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700959 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -0700960 where_clause: where_clause,
961 .. generics
962 },
David Tolnay570695e2017-06-03 16:15:13 -0700963 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 supertraits: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -0700965 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700966 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -0700968 })
969 ));
970
David Tolnayf94e2362016-10-04 00:29:51 -0700971 named!(item_default_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700972 attrs: many0!(call!(Attribute::parse_outer)) >>
973 unsafety: syn!(Unsafety) >>
974 impl_: syn!(Impl) >>
975 path: syn!(Path) >>
976 for_: syn!(For) >>
977 dot2: syn!(Dot2) >>
978 braces: braces!(epsilon!()) >>
David Tolnayf94e2362016-10-04 00:29:51 -0700979 (Item {
David Tolnayf94e2362016-10-04 00:29:51 -0700980 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700981 node: ItemDefaultImpl {
982 unsafety: unsafety,
Alex Crichton954046c2017-05-30 21:49:42 -0700983 impl_token: impl_,
David Tolnay570695e2017-06-03 16:15:13 -0700984 path: path,
Alex Crichton954046c2017-05-30 21:49:42 -0700985 for_token: for_,
986 dot2_token: dot2,
987 brace_token: braces.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700988 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -0700989 })
990 ));
991
Alex Crichton954046c2017-05-30 21:49:42 -0700992 impl Synom for TraitItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400993 named!(parse -> Self, alt!(
994 trait_item_const
995 |
996 trait_item_method
997 |
998 trait_item_type
999 |
1000 trait_item_mac
1001 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001002 }
David Tolnay0aecb732016-10-03 23:03:50 -07001003
1004 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001005 attrs: many0!(call!(Attribute::parse_outer)) >>
1006 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001007 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001008 colon: syn!(Colon) >>
1009 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001010 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001011 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001012 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001013 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001014 node: TraitItemConst {
Alex Crichton954046c2017-05-30 21:49:42 -07001015 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001016 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001017 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001018 ty: ty,
1019 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001020 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001022 })
1023 ));
1024
1025 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001026 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1027 constness: syn!(Constness) >>
1028 unsafety: syn!(Unsafety) >>
1029 abi: option!(syn!(Abi)) >>
1030 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001031 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001032 generics: syn!(Generics) >>
1033 inputs: parens!(call!(Delimited::parse_terminated)) >>
1034 ret: syn!(FunctionRetTy) >>
1035 where_clause: syn!(WhereClause) >>
1036 body: option!(braces!(
1037 tuple!(many0!(call!(Attribute::parse_inner)),
1038 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001039 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001040 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001041 ({
1042 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001043 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001044 None => (Vec::new(), None),
1045 };
1046 TraitItem {
David Tolnay5859df12016-10-29 22:49:54 -07001047 attrs: {
1048 let mut attrs = outer_attrs;
1049 attrs.extend(inner_attrs);
1050 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001051 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001052 node: TraitItemMethod {
1053 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001054 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001055 unsafety: unsafety,
David Tolnay5859df12016-10-29 22:49:54 -07001056 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001057 ident: ident,
David Tolnay5859df12016-10-29 22:49:54 -07001058 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001059 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001060 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001061 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001062 fn_token: fn_,
1063 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001064 dot_tokens: None,
1065 generics: Generics {
1066 where_clause: where_clause,
1067 .. generics
1068 },
David Tolnay5859df12016-10-29 22:49:54 -07001069 },
1070 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 default: stmts.map(|stmts| {
1072 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001073 stmts: stmts.0,
1074 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001075 }
1076 }),
David Tolnay570695e2017-06-03 16:15:13 -07001077 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001078 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001079 }
David Tolnay0aecb732016-10-03 23:03:50 -07001080 })
1081 ));
1082
1083 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001084 attrs: many0!(call!(Attribute::parse_outer)) >>
1085 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001086 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001087 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001088 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001089 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001091 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1092 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001093 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001094 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001095 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001096 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001097 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001098 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001099 bounds: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -07001100 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001101 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001102 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001103 })
1104 ));
1105
1106 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001107 attrs: many0!(call!(Attribute::parse_outer)) >>
1108 mac: syn!(Mac) >>
1109 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001110 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001111 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001112 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001113 })
1114 ));
1115
David Tolnay4c9be372016-10-06 00:47:37 -07001116 named!(item_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001117 attrs: many0!(call!(Attribute::parse_outer)) >>
1118 unsafety: syn!(Unsafety) >>
1119 impl_: syn!(Impl) >>
1120 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001121 polarity_path: alt!(
1122 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001123 polarity: syn!(ImplPolarity) >>
1124 path: syn!(Path) >>
1125 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001126 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001127 )
1128 |
David Tolnay570695e2017-06-03 16:15:13 -07001129 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001130 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001131 self_ty: syn!(Ty) >>
1132 where_clause: syn!(WhereClause) >>
1133 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001134 (Item {
David Tolnay4c9be372016-10-06 00:47:37 -07001135 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001136 node: ItemImpl {
1137 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -07001138 impl_token: impl_,
Alex Crichton62a0a592017-05-22 13:58:53 -07001139 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001140 where_clause: where_clause,
1141 .. generics
1142 },
David Tolnay570695e2017-06-03 16:15:13 -07001143 trait_: polarity_path,
Alex Crichton62a0a592017-05-22 13:58:53 -07001144 self_ty: Box::new(self_ty),
David Tolnay570695e2017-06-03 16:15:13 -07001145 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001146 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001147 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001148 })
1149 ));
1150
Alex Crichton954046c2017-05-30 21:49:42 -07001151 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001152 named!(parse -> Self, alt!(
1153 impl_item_const
1154 |
1155 impl_item_method
1156 |
1157 impl_item_type
1158 |
David Tolnay570695e2017-06-03 16:15:13 -07001159 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001160 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001161 }
David Tolnay4c9be372016-10-06 00:47:37 -07001162
1163 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001164 attrs: many0!(call!(Attribute::parse_outer)) >>
1165 vis: syn!(Visibility) >>
1166 defaultness: syn!(Defaultness) >>
1167 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001168 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001169 colon: syn!(Colon) >>
1170 ty: syn!(Ty) >>
1171 eq: syn!(Eq) >>
1172 value: syn!(Expr) >>
1173 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001174 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001175 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001176 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001177 vis: vis,
1178 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001179 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001180 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001181 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001182 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001183 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001184 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001185 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001186 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001187 })
1188 ));
1189
1190 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001191 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1192 vis: syn!(Visibility) >>
1193 defaultness: syn!(Defaultness) >>
1194 constness: syn!(Constness) >>
1195 unsafety: syn!(Unsafety) >>
1196 abi: option!(syn!(Abi)) >>
1197 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001198 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001199 generics: syn!(Generics) >>
1200 inputs: parens!(call!(Delimited::parse_terminated)) >>
1201 ret: syn!(FunctionRetTy) >>
1202 where_clause: syn!(WhereClause) >>
1203 inner_attrs_stmts: braces!(tuple!(
1204 many0!(call!(Attribute::parse_inner)),
1205 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001206 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001207 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001208 attrs: {
1209 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001210 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001211 attrs
1212 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001213 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001214 vis: vis,
1215 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001216 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001217 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001218 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001219 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001220 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001221 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001222 fn_token: fn_,
1223 paren_token: inputs.1,
1224 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001225 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001226 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001227 generics: Generics {
1228 where_clause: where_clause,
1229 .. generics
1230 },
1231 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001232 },
1233 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001234 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001235 brace_token: inner_attrs_stmts.1,
1236 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001237 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001238 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001239 })
1240 ));
1241
1242 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001243 attrs: many0!(call!(Attribute::parse_outer)) >>
1244 vis: syn!(Visibility) >>
1245 defaultness: syn!(Defaultness) >>
1246 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001247 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001248 eq: syn!(Eq) >>
1249 ty: syn!(Ty) >>
1250 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001251 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001252 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001253 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001254 vis: vis,
1255 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001256 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001257 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001258 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001259 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001260 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001261 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001262 })
1263 ));
1264
David Tolnay570695e2017-06-03 16:15:13 -07001265 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001266 attrs: many0!(call!(Attribute::parse_outer)) >>
1267 mac: syn!(Mac) >>
1268 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001269 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001270 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001271 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001272 })
1273 ));
1274
Alex Crichton954046c2017-05-30 21:49:42 -07001275 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001276 named!(parse -> Self, alt!(
1277 syn!(Bang) => { ImplPolarity::Negative }
1278 |
1279 epsilon!() => { |_| ImplPolarity::Positive }
1280 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001281 }
David Tolnay4c9be372016-10-06 00:47:37 -07001282
Alex Crichton954046c2017-05-30 21:49:42 -07001283 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001284 named!(parse -> Self, alt!(
1285 syn!(Const) => { Constness::Const }
1286 |
1287 epsilon!() => { |_| Constness::NotConst }
1288 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001289 }
David Tolnay42602292016-10-01 22:25:45 -07001290
Alex Crichton954046c2017-05-30 21:49:42 -07001291 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001292 named!(parse -> Self, alt!(
1293 syn!(Default_) => { Defaultness::Default }
1294 |
1295 epsilon!() => { |_| Defaultness::Final }
1296 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001297 }
David Tolnayedf2b992016-09-23 20:43:45 -07001298}
David Tolnay4a51dc72016-10-01 00:40:31 -07001299
1300#[cfg(feature = "printing")]
1301mod printing {
1302 use super::*;
1303 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001304 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001305 use quote::{Tokens, ToTokens};
1306
1307 impl ToTokens for Item {
1308 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001309 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001310 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001311 ItemKind::ExternCrate(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001312 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001313 item.extern_token.to_tokens(tokens);
1314 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001315 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001316 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001317 as_token.to_tokens(tokens);
1318 rename.to_tokens(tokens);
1319 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001320 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001321 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001322 ItemKind::Use(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001323 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001324 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001325 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001326 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001327 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001328 ItemKind::Static(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001329 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001330 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001331 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001332 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001333 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001334 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001335 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001336 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001337 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001338 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001339 ItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001340 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001341 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001342 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001343 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001344 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001345 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001346 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001347 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001348 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001349 ItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001350 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001351 item.constness.to_tokens(tokens);
1352 item.unsafety.to_tokens(tokens);
1353 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001354 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001355 item.block.brace_token.surround(tokens, |tokens| {
1356 tokens.append_all(self.attrs.inner());
1357 tokens.append_all(&item.block.stmts);
1358 });
David Tolnay42602292016-10-01 22:25:45 -07001359 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001360 ItemKind::Mod(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001361 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001362 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001363 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001364 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001366 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001367 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001368 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001369 } else {
1370 item.semi.unwrap_or_default().to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001371 }
David Tolnay35902302016-10-06 01:11:08 -07001372 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001373 ItemKind::ForeignMod(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.abi.to_tokens(tokens);
1375 item.brace_token.surround(tokens, |tokens| {
1376 tokens.append_all(&item.items);
1377 });
David Tolnay35902302016-10-06 01:11:08 -07001378 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001379 ItemKind::Ty(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001380 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001381 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001382 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001383 item.generics.to_tokens(tokens);
1384 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001385 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001386 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001387 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001388 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001389 ItemKind::Enum(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001390 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001391 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001392 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001393 item.generics.to_tokens(tokens);
1394 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 item.brace_token.surround(tokens, |tokens| {
1396 item.variants.to_tokens(tokens);
1397 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001398 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001399 ItemKind::Struct(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001400 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001401 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001402 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001403 item.generics.to_tokens(tokens);
1404 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001405 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001406 item.generics.where_clause.to_tokens(tokens);
1407 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001408 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001409 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001410 item.data.to_tokens(tokens);
1411 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001412 item.semi_token.unwrap_or_default().to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001413 }
1414 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001415 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001416 item.semi_token.unwrap_or_default().to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001417 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001418 }
1419 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001420 ItemKind::Union(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001421 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001422 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001423 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001424 item.generics.to_tokens(tokens);
1425 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001426 // XXX: Should we handle / complain when using a
1427 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001428 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001429 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001430 ItemKind::Trait(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001431 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001432 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001433 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001434 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001435 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001436 if !item.supertraits.is_empty() {
1437 item.colon_token.unwrap_or_default().to_tokens(tokens);
1438 item.supertraits.to_tokens(tokens);
1439 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001440 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001441 item.brace_token.surround(tokens, |tokens| {
1442 tokens.append_all(&item.items);
1443 });
David Tolnayca085422016-10-04 00:12:38 -07001444 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001445 ItemKind::DefaultImpl(ref item) => {
1446 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001447 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001448 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001449 item.for_token.to_tokens(tokens);
1450 item.dot2_token.to_tokens(tokens);
1451 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001452 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001453 ItemKind::Impl(ref item) => {
1454 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001455 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001456 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001457 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001458 polarity.to_tokens(tokens);
1459 path.to_tokens(tokens);
1460 for_token.to_tokens(tokens);
1461 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001462 item.self_ty.to_tokens(tokens);
1463 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001464 item.brace_token.surround(tokens, |tokens| {
1465 tokens.append_all(&item.items);
1466 });
David Tolnay4c9be372016-10-06 00:47:37 -07001467 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001468 ItemKind::Mac(ref mac) => {
1469 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001470 mac.bang_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001471 mac.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001472 tokens.append_all(&mac.tokens);
1473 if !mac.is_braced() {
1474 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001475 }
1476 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001477 }
1478 }
1479 }
David Tolnay42602292016-10-01 22:25:45 -07001480
Alex Crichton62a0a592017-05-22 13:58:53 -07001481 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001482 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001484 if self.rename.is_some() {
1485 self.as_token.unwrap_or_default().to_tokens(tokens);
1486 self.rename.to_tokens(tokens);
1487 }
David Tolnay4a057422016-10-08 00:02:31 -07001488 }
1489 }
1490
Alex Crichton62a0a592017-05-22 13:58:53 -07001491 impl ToTokens for PathGlob {
1492 fn to_tokens(&self, tokens: &mut Tokens) {
1493 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001494 self.colon2_token.to_tokens(tokens);
1495 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001496 }
1497 }
1498
1499 impl ToTokens for PathList {
1500 fn to_tokens(&self, tokens: &mut Tokens) {
1501 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001502 self.colon2_token.to_tokens(tokens);
1503 self.brace_token.surround(tokens, |tokens| {
1504 self.items.to_tokens(tokens);
1505 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001506 }
1507 }
1508
David Tolnay4a057422016-10-08 00:02:31 -07001509 impl ToTokens for PathListItem {
1510 fn to_tokens(&self, tokens: &mut Tokens) {
1511 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001512 if self.rename.is_some() {
1513 self.as_token.unwrap_or_default().to_tokens(tokens);
1514 self.rename.to_tokens(tokens);
1515 }
David Tolnay4a057422016-10-08 00:02:31 -07001516 }
1517 }
1518
David Tolnayca085422016-10-04 00:12:38 -07001519 impl ToTokens for TraitItem {
1520 fn to_tokens(&self, tokens: &mut Tokens) {
1521 tokens.append_all(self.attrs.outer());
1522 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001523 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001524 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001525 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001526 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001527 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001528 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001529 eq_token.to_tokens(tokens);
1530 default.to_tokens(tokens);
1531 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001532 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001533 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001534 TraitItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001535 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001536 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001537 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001538 block.brace_token.surround(tokens, |tokens| {
1539 tokens.append_all(self.attrs.inner());
1540 tokens.append_all(&block.stmts);
1541 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001542 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001543 None => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04001544 item.semi_token.unwrap_or_default().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001545 }
David Tolnayca085422016-10-04 00:12:38 -07001546 }
1547 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001548 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001549 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001550 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001551 if !item.bounds.is_empty() {
1552 item.colon_token.unwrap_or_default().to_tokens(tokens);
1553 item.bounds.to_tokens(tokens);
1554 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001555 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001556 eq_token.to_tokens(tokens);
1557 default.to_tokens(tokens);
1558 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001560 }
1561 TraitItemKind::Macro(ref mac) => {
1562 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 if !mac.is_braced() {
1564 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001565 }
David Tolnayca085422016-10-04 00:12:38 -07001566 }
1567 }
1568 }
1569 }
1570
David Tolnay4c9be372016-10-06 00:47:37 -07001571 impl ToTokens for ImplItem {
1572 fn to_tokens(&self, tokens: &mut Tokens) {
1573 tokens.append_all(self.attrs.outer());
1574 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001575 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001576 item.vis.to_tokens(tokens);
1577 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001578 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001579 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001580 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001581 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001582 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001583 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001585 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001586 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001587 item.vis.to_tokens(tokens);
1588 item.defaultness.to_tokens(tokens);
1589 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001590 item.block.brace_token.surround(tokens, |tokens| {
1591 tokens.append_all(self.attrs.inner());
1592 tokens.append_all(&item.block.stmts);
1593 });
David Tolnay4c9be372016-10-06 00:47:37 -07001594 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001595 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001596 item.vis.to_tokens(tokens);
1597 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001598 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001599 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001600 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001601 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001602 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001603 }
1604 ImplItemKind::Macro(ref mac) => {
1605 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001606 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001607 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001608 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001609 }
1610 }
1611 }
1612 }
1613 }
1614
David Tolnay35902302016-10-06 01:11:08 -07001615 impl ToTokens for ForeignItem {
1616 fn to_tokens(&self, tokens: &mut Tokens) {
1617 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001618 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001619 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001620 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001621 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001622 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001623 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001624 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001625 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001626 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001627 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001628 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001629 }
1630 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001631 self.semi_token.to_tokens(tokens);
1632 }
1633 }
1634
David Tolnay570695e2017-06-03 16:15:13 -07001635 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001636 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001637 self.constness.to_tokens(tokens);
1638 self.unsafety.to_tokens(tokens);
1639 self.abi.to_tokens(tokens);
1640 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001641 }
1642 }
1643
David Tolnay570695e2017-06-03 16:15:13 -07001644 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001645
1646 impl<'a> ToTokens for NamedDecl<'a> {
1647 fn to_tokens(&self, tokens: &mut Tokens) {
1648 self.0.fn_token.to_tokens(tokens);
1649 self.1.to_tokens(tokens);
1650 self.0.generics.to_tokens(tokens);
1651 self.0.paren_token.surround(tokens, |tokens| {
1652 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001653
1654 if self.0.variadic {
1655 if !self.0.inputs.empty_or_trailing() {
1656 tokens::Comma::default().to_tokens(tokens);
1657 }
1658 self.0.dot_tokens.unwrap_or_default().to_tokens(tokens);
1659 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001660 });
1661 self.0.output.to_tokens(tokens);
1662 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001663 }
1664 }
1665
Alex Crichton62a0a592017-05-22 13:58:53 -07001666 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001667 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001668 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001669 self.lifetime.to_tokens(tokens);
1670 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001671 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001672 }
1673 }
1674
1675 impl ToTokens for ArgSelf {
1676 fn to_tokens(&self, tokens: &mut Tokens) {
1677 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001678 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001679 }
1680 }
1681
1682 impl ToTokens for ArgCaptured {
1683 fn to_tokens(&self, tokens: &mut Tokens) {
1684 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001685 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001686 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001687 }
1688 }
1689
David Tolnay42602292016-10-01 22:25:45 -07001690 impl ToTokens for Constness {
1691 fn to_tokens(&self, tokens: &mut Tokens) {
1692 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001693 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001694 Constness::NotConst => {
1695 // nothing
1696 }
David Tolnay42602292016-10-01 22:25:45 -07001697 }
1698 }
1699 }
1700
David Tolnay4c9be372016-10-06 00:47:37 -07001701 impl ToTokens for Defaultness {
1702 fn to_tokens(&self, tokens: &mut Tokens) {
1703 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001704 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001705 Defaultness::Final => {
1706 // nothing
1707 }
1708 }
1709 }
1710 }
1711
1712 impl ToTokens for ImplPolarity {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001715 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001716 ImplPolarity::Positive => {
1717 // nothing
1718 }
1719 }
1720 }
1721 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001722}