blob: 3255ed9194f8406d20545f95b7900ae71983ac73 [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 Crichtonaf9d2952017-08-27 10:19:54 -0700229 pub colon2_token: Option<tokens::Colon2>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700230 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!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700563 path: option!(do_parse!(
564 path: syn!(Path) >>
565 colon2: syn!(Colon2) >>
566 (path, colon2)
567 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400568 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700569 ({
570 match path {
571 Some((path, colon2)) => {
572 PathGlob {
573 path: path,
574 colon2_token: Some(colon2),
575 star_token: star,
576 }
577 }
578 None => {
579 PathGlob {
580 path: Path {
581 leading_colon: None,
582 segments: Default::default(),
583 },
584 colon2_token: None,
585 star_token: star,
586 }
587 }
588 }
Michael Layzell92639a52017-06-01 00:07:44 -0400589 })
590 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700591 }
David Tolnay4a057422016-10-08 00:02:31 -0700592
Alex Crichton954046c2017-05-30 21:49:42 -0700593 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400594 named!(parse -> Self, alt!(
595 do_parse!(
596 path: syn!(Path) >>
597 colon2: syn!(Colon2) >>
598 items: braces!(call!(Delimited::parse_terminated)) >>
599 (PathList {
600 path: path,
601 items: items.0,
602 brace_token: items.1,
603 colon2_token: colon2,
604 })
605 )
606 |
607 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700608 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400609 items: braces!(call!(Delimited::parse_terminated)) >>
610 (PathList {
611 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400612 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700613 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400614 },
David Tolnay570695e2017-06-03 16:15:13 -0700615 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400616 brace_token: items.1,
617 items: items.0,
618 })
619 )
620 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700621 }
David Tolnay4a057422016-10-08 00:02:31 -0700622
Alex Crichton954046c2017-05-30 21:49:42 -0700623 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400624 named!(parse -> Self, do_parse!(
625 name: alt!(
626 syn!(Ident)
627 |
628 map!(syn!(Self_), Into::into)
629 ) >>
630 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
631 (PathListItem {
632 name: name,
633 as_token: rename.as_ref().map(|p| As((p.0).0)),
634 rename: rename.map(|p| p.1),
635 })
636 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700637 }
David Tolnay4a057422016-10-08 00:02:31 -0700638
David Tolnay47a877c2016-10-01 16:50:55 -0700639 named!(item_static -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700640 attrs: many0!(call!(Attribute::parse_outer)) >>
641 vis: syn!(Visibility) >>
642 static_: syn!(Static) >>
643 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700644 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700645 colon: syn!(Colon) >>
646 ty: syn!(Ty) >>
647 eq: syn!(Eq) >>
648 value: syn!(Expr) >>
649 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700650 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700651 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 node: ItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700653 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700654 static_token: static_,
David Tolnay570695e2017-06-03 16:15:13 -0700655 mutbl: mutability,
656 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700657 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700658 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700659 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700660 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700661 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700662 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700663 })
664 ));
665
666 named!(item_const -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700667 attrs: many0!(call!(Attribute::parse_outer)) >>
668 vis: syn!(Visibility) >>
669 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700670 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700671 colon: syn!(Colon) >>
672 ty: syn!(Ty) >>
673 eq: syn!(Eq) >>
674 value: syn!(Expr) >>
675 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700676 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700677 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700678 node: ItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700679 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700680 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -0700681 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700682 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700683 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700684 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700685 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700686 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700687 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700688 })
689 ));
690
David Tolnay42602292016-10-01 22:25:45 -0700691 named!(item_fn -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700692 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
693 vis: syn!(Visibility) >>
694 constness: syn!(Constness) >>
695 unsafety: syn!(Unsafety) >>
696 abi: option!(syn!(Abi)) >>
697 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700698 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700699 generics: syn!(Generics) >>
700 inputs: parens!(Delimited::parse_terminated) >>
701 ret: syn!(FunctionRetTy) >>
702 where_clause: syn!(WhereClause) >>
703 inner_attrs_stmts: braces!(tuple!(
704 many0!(call!(Attribute::parse_inner)),
705 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400706 )) >>
David Tolnay42602292016-10-01 22:25:45 -0700707 (Item {
David Tolnay3b9783a2016-10-29 22:37:09 -0700708 attrs: {
709 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700710 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700711 attrs
712 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 node: ItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700714 vis: vis,
715 constness: constness,
716 unsafety: unsafety,
717 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700719 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700720 fn_token: fn_,
721 paren_token: inputs.1,
722 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700723 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700724 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700725 generics: Generics {
726 where_clause: where_clause,
727 .. generics
728 },
David Tolnay42602292016-10-01 22:25:45 -0700729 }),
David Tolnay570695e2017-06-03 16:15:13 -0700730 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 block: Box::new(Block {
Alex Crichton954046c2017-05-30 21:49:42 -0700732 brace_token: inner_attrs_stmts.1,
733 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -0700734 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700736 })
737 ));
738
Alex Crichton954046c2017-05-30 21:49:42 -0700739 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400740 named!(parse -> Self, alt!(
741 do_parse!(
742 and: syn!(And) >>
743 lt: option!(syn!(Lifetime)) >>
744 mutability: syn!(Mutability) >>
745 self_: syn!(Self_) >>
746 not!(syn!(Colon)) >>
747 (ArgSelfRef {
748 lifetime: lt,
749 mutbl: mutability,
750 and_token: and,
751 self_token: self_,
752 }.into())
753 )
754 |
755 do_parse!(
756 mutability: syn!(Mutability) >>
757 self_: syn!(Self_) >>
758 not!(syn!(Colon)) >>
759 (ArgSelf {
760 mutbl: mutability,
761 self_token: self_,
762 }.into())
763 )
764 |
765 do_parse!(
766 pat: syn!(Pat) >>
767 colon: syn!(Colon) >>
768 ty: syn!(Ty) >>
769 (ArgCaptured {
770 pat: pat,
771 ty: ty,
772 colon_token: colon,
773 }.into())
774 )
775 |
776 syn!(Ty) => { FnArg::Ignored }
777 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700778 }
David Tolnay62f374c2016-10-02 13:37:00 -0700779
David Tolnay35902302016-10-06 01:11:08 -0700780 named!(item_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700781 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
782 vis: syn!(Visibility) >>
783 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700784 ident: syn!(Ident) >>
785 content_semi: alt!(
786 syn!(Semi) => {|semi| (
787 Vec::new(),
788 None,
789 Some(semi),
790 )}
David Tolnay37d10332016-10-13 20:51:04 -0700791 |
Alex Crichton954046c2017-05-30 21:49:42 -0700792 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700793 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700794 many0!(call!(Attribute::parse_inner)),
795 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400796 )
David Tolnay570695e2017-06-03 16:15:13 -0700797 ) => {|((inner_attrs, items), brace)| (
798 inner_attrs,
799 Some((brace, items)),
800 None,
801 )}
David Tolnay37d10332016-10-13 20:51:04 -0700802 ) >>
David Tolnay570695e2017-06-03 16:15:13 -0700803 (Item {
804 attrs: {
805 let mut attrs = outer_attrs;
806 attrs.extend(content_semi.0);
807 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700808 },
David Tolnay570695e2017-06-03 16:15:13 -0700809 node: ItemMod {
David Tolnay7b8009b2016-10-25 22:36:00 -0700810 vis: vis,
David Tolnay570695e2017-06-03 16:15:13 -0700811 mod_token: mod_,
812 ident: ident,
813 content: content_semi.1,
814 semi: content_semi.2,
815 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700816 })
817 ));
818
819 named!(item_foreign_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700820 attrs: many0!(call!(Attribute::parse_outer)) >>
821 abi: syn!(Abi) >>
822 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay35902302016-10-06 01:11:08 -0700823 (Item {
David Tolnay35902302016-10-06 01:11:08 -0700824 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700825 node: ItemForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700826 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -0700827 brace_token: items.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700828 items: items.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700829 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700830 })
831 ));
832
Alex Crichton954046c2017-05-30 21:49:42 -0700833 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400834 named!(parse -> Self, alt!(
835 foreign_fn
836 |
837 foreign_static
838 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700839 }
David Tolnay35902302016-10-06 01:11:08 -0700840
841 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700842 attrs: many0!(call!(Attribute::parse_outer)) >>
843 vis: syn!(Visibility) >>
844 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700845 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700846 generics: syn!(Generics) >>
847 inputs: parens!(do_parse!(
848 args: call!(Delimited::parse_terminated) >>
849 variadic: cond!(args.is_empty() || args.trailing_delim(),
850 option!(syn!(Dot3))) >>
851 (args, variadic)
852 )) >>
853 ret: syn!(FunctionRetTy) >>
854 where_clause: syn!(WhereClause) >>
855 semi: syn!(Semi) >>
856 ({
857 let ((inputs, variadic), parens) = inputs;
858 let variadic = variadic.and_then(|v| v);
859 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700860 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700861 attrs: attrs,
862 semi_token: semi,
863 node: ForeignItemFn {
864 decl: Box::new(FnDecl {
865 fn_token: fn_,
866 paren_token: parens,
867 inputs: inputs,
868 variadic: variadic.is_some(),
869 dot_tokens: variadic,
870 output: ret,
871 generics: Generics {
872 where_clause: where_clause,
873 .. generics
874 },
875 }),
876 }.into(),
877 vis: vis,
878 }
David Tolnay35902302016-10-06 01:11:08 -0700879 })
880 ));
881
882 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700883 attrs: many0!(call!(Attribute::parse_outer)) >>
884 vis: syn!(Visibility) >>
885 static_: syn!(Static) >>
886 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700887 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700888 colon: syn!(Colon) >>
889 ty: syn!(Ty) >>
890 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700891 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700892 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700893 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700894 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700895 node: ForeignItemStatic {
896 ty: Box::new(ty),
897 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700898 static_token: static_,
899 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700901 vis: vis,
902 })
903 ));
904
David Tolnay3cf52982016-10-01 17:11:37 -0700905 named!(item_ty -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700906 attrs: many0!(call!(Attribute::parse_outer)) >>
907 vis: syn!(Visibility) >>
908 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700909 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700910 generics: syn!(Generics) >>
911 where_clause: syn!(WhereClause) >>
912 eq: syn!(Eq) >>
913 ty: syn!(Ty) >>
914 semi: syn!(Semi) >>
David Tolnay3cf52982016-10-01 17:11:37 -0700915 (Item {
David Tolnay3cf52982016-10-01 17:11:37 -0700916 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700917 node: ItemTy {
David Tolnay570695e2017-06-03 16:15:13 -0700918 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700919 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -0700920 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700921 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700922 where_clause: where_clause,
923 ..generics
924 },
David Tolnay570695e2017-06-03 16:15:13 -0700925 eq_token: eq,
926 ty: Box::new(ty),
927 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700928 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700929 })
930 ));
931
David Tolnay570695e2017-06-03 16:15:13 -0700932 named!(item_struct_or_enum -> Item, map!(syn!(DeriveInput), Into::into));
David Tolnay42602292016-10-01 22:25:45 -0700933
David Tolnay2f9fa632016-10-03 22:08:48 -0700934 named!(item_union -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700935 attrs: many0!(call!(Attribute::parse_outer)) >>
936 vis: syn!(Visibility) >>
937 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700938 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700939 generics: syn!(Generics) >>
940 where_clause: syn!(WhereClause) >>
941 fields: braces!(call!(Delimited::parse_terminated_with,
942 Field::parse_struct)) >>
David Tolnay2f9fa632016-10-03 22:08:48 -0700943 (Item {
David Tolnay2f9fa632016-10-03 22:08:48 -0700944 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700945 node: ItemUnion {
David Tolnay570695e2017-06-03 16:15:13 -0700946 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700947 union_token: union_,
David Tolnay570695e2017-06-03 16:15:13 -0700948 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700950 where_clause: where_clause,
951 .. generics
952 },
David Tolnay570695e2017-06-03 16:15:13 -0700953 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700955 })
956 ));
957
David Tolnay0aecb732016-10-03 23:03:50 -0700958 named!(item_trait -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700959 attrs: many0!(call!(Attribute::parse_outer)) >>
960 vis: syn!(Visibility) >>
961 unsafety: syn!(Unsafety) >>
962 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700963 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700964 generics: syn!(Generics) >>
965 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700967 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700968 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700969 where_clause: syn!(WhereClause) >>
970 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700971 (Item {
David Tolnay0aecb732016-10-03 23:03:50 -0700972 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700973 node: ItemTrait {
David Tolnay570695e2017-06-03 16:15:13 -0700974 vis: vis,
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700976 trait_token: trait_,
977 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700978 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -0700979 where_clause: where_clause,
980 .. generics
981 },
David Tolnay570695e2017-06-03 16:15:13 -0700982 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700983 supertraits: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -0700984 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700985 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -0700987 })
988 ));
989
David Tolnayf94e2362016-10-04 00:29:51 -0700990 named!(item_default_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700991 attrs: many0!(call!(Attribute::parse_outer)) >>
992 unsafety: syn!(Unsafety) >>
993 impl_: syn!(Impl) >>
994 path: syn!(Path) >>
995 for_: syn!(For) >>
996 dot2: syn!(Dot2) >>
997 braces: braces!(epsilon!()) >>
David Tolnayf94e2362016-10-04 00:29:51 -0700998 (Item {
David Tolnayf94e2362016-10-04 00:29:51 -0700999 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001000 node: ItemDefaultImpl {
1001 unsafety: unsafety,
Alex Crichton954046c2017-05-30 21:49:42 -07001002 impl_token: impl_,
David Tolnay570695e2017-06-03 16:15:13 -07001003 path: path,
Alex Crichton954046c2017-05-30 21:49:42 -07001004 for_token: for_,
1005 dot2_token: dot2,
1006 brace_token: braces.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001007 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001008 })
1009 ));
1010
Alex Crichton954046c2017-05-30 21:49:42 -07001011 impl Synom for TraitItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001012 named!(parse -> Self, alt!(
1013 trait_item_const
1014 |
1015 trait_item_method
1016 |
1017 trait_item_type
1018 |
1019 trait_item_mac
1020 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001021 }
David Tolnay0aecb732016-10-03 23:03:50 -07001022
1023 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001024 attrs: many0!(call!(Attribute::parse_outer)) >>
1025 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001026 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001027 colon: syn!(Colon) >>
1028 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001029 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001030 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001031 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001032 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001033 node: TraitItemConst {
Alex Crichton954046c2017-05-30 21:49:42 -07001034 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001035 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001036 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001037 ty: ty,
1038 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001039 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001040 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001041 })
1042 ));
1043
1044 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001045 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1046 constness: syn!(Constness) >>
1047 unsafety: syn!(Unsafety) >>
1048 abi: option!(syn!(Abi)) >>
1049 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001050 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001051 generics: syn!(Generics) >>
1052 inputs: parens!(call!(Delimited::parse_terminated)) >>
1053 ret: syn!(FunctionRetTy) >>
1054 where_clause: syn!(WhereClause) >>
1055 body: option!(braces!(
1056 tuple!(many0!(call!(Attribute::parse_inner)),
1057 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001058 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001059 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001060 ({
1061 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001062 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001063 None => (Vec::new(), None),
1064 };
1065 TraitItem {
David Tolnay5859df12016-10-29 22:49:54 -07001066 attrs: {
1067 let mut attrs = outer_attrs;
1068 attrs.extend(inner_attrs);
1069 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001070 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001071 node: TraitItemMethod {
1072 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001073 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001074 unsafety: unsafety,
David Tolnay5859df12016-10-29 22:49:54 -07001075 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001076 ident: ident,
David Tolnay5859df12016-10-29 22:49:54 -07001077 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001078 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001079 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001080 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001081 fn_token: fn_,
1082 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001083 dot_tokens: None,
1084 generics: Generics {
1085 where_clause: where_clause,
1086 .. generics
1087 },
David Tolnay5859df12016-10-29 22:49:54 -07001088 },
1089 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 default: stmts.map(|stmts| {
1091 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001092 stmts: stmts.0,
1093 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001094 }
1095 }),
David Tolnay570695e2017-06-03 16:15:13 -07001096 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001097 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001098 }
David Tolnay0aecb732016-10-03 23:03:50 -07001099 })
1100 ));
1101
1102 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001103 attrs: many0!(call!(Attribute::parse_outer)) >>
1104 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001105 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001106 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001107 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001108 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001110 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1111 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001112 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001113 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001114 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001115 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001116 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001117 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001118 bounds: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -07001119 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001120 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001121 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001122 })
1123 ));
1124
1125 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001126 attrs: many0!(call!(Attribute::parse_outer)) >>
1127 mac: syn!(Mac) >>
1128 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001129 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001130 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001131 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001132 })
1133 ));
1134
David Tolnay4c9be372016-10-06 00:47:37 -07001135 named!(item_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001136 attrs: many0!(call!(Attribute::parse_outer)) >>
1137 unsafety: syn!(Unsafety) >>
1138 impl_: syn!(Impl) >>
1139 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001140 polarity_path: alt!(
1141 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001142 polarity: syn!(ImplPolarity) >>
1143 path: syn!(Path) >>
1144 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001145 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001146 )
1147 |
David Tolnay570695e2017-06-03 16:15:13 -07001148 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001149 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001150 self_ty: syn!(Ty) >>
1151 where_clause: syn!(WhereClause) >>
1152 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001153 (Item {
David Tolnay4c9be372016-10-06 00:47:37 -07001154 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001155 node: ItemImpl {
1156 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -07001157 impl_token: impl_,
Alex Crichton62a0a592017-05-22 13:58:53 -07001158 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001159 where_clause: where_clause,
1160 .. generics
1161 },
David Tolnay570695e2017-06-03 16:15:13 -07001162 trait_: polarity_path,
Alex Crichton62a0a592017-05-22 13:58:53 -07001163 self_ty: Box::new(self_ty),
David Tolnay570695e2017-06-03 16:15:13 -07001164 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001165 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001166 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001167 })
1168 ));
1169
Alex Crichton954046c2017-05-30 21:49:42 -07001170 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001171 named!(parse -> Self, alt!(
1172 impl_item_const
1173 |
1174 impl_item_method
1175 |
1176 impl_item_type
1177 |
David Tolnay570695e2017-06-03 16:15:13 -07001178 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001179 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001180 }
David Tolnay4c9be372016-10-06 00:47:37 -07001181
1182 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001183 attrs: many0!(call!(Attribute::parse_outer)) >>
1184 vis: syn!(Visibility) >>
1185 defaultness: syn!(Defaultness) >>
1186 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001187 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001188 colon: syn!(Colon) >>
1189 ty: syn!(Ty) >>
1190 eq: syn!(Eq) >>
1191 value: syn!(Expr) >>
1192 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001193 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001194 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001195 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001196 vis: vis,
1197 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001198 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001199 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001200 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001201 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001202 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001203 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001204 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001205 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001206 })
1207 ));
1208
1209 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001210 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1211 vis: syn!(Visibility) >>
1212 defaultness: syn!(Defaultness) >>
1213 constness: syn!(Constness) >>
1214 unsafety: syn!(Unsafety) >>
1215 abi: option!(syn!(Abi)) >>
1216 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001217 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001218 generics: syn!(Generics) >>
1219 inputs: parens!(call!(Delimited::parse_terminated)) >>
1220 ret: syn!(FunctionRetTy) >>
1221 where_clause: syn!(WhereClause) >>
1222 inner_attrs_stmts: braces!(tuple!(
1223 many0!(call!(Attribute::parse_inner)),
1224 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001225 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001226 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001227 attrs: {
1228 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001229 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001230 attrs
1231 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001232 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001233 vis: vis,
1234 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001235 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001236 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001237 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001238 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001239 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001240 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001241 fn_token: fn_,
1242 paren_token: inputs.1,
1243 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001244 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001245 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001246 generics: Generics {
1247 where_clause: where_clause,
1248 .. generics
1249 },
1250 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001251 },
1252 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001253 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001254 brace_token: inner_attrs_stmts.1,
1255 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001256 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001257 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001258 })
1259 ));
1260
1261 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001262 attrs: many0!(call!(Attribute::parse_outer)) >>
1263 vis: syn!(Visibility) >>
1264 defaultness: syn!(Defaultness) >>
1265 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001266 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001267 eq: syn!(Eq) >>
1268 ty: syn!(Ty) >>
1269 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001270 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001271 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001272 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001273 vis: vis,
1274 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001275 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001276 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001277 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001278 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001279 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001280 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001281 })
1282 ));
1283
David Tolnay570695e2017-06-03 16:15:13 -07001284 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001285 attrs: many0!(call!(Attribute::parse_outer)) >>
1286 mac: syn!(Mac) >>
1287 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001288 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001289 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001290 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001291 })
1292 ));
1293
Alex Crichton954046c2017-05-30 21:49:42 -07001294 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001295 named!(parse -> Self, alt!(
1296 syn!(Bang) => { ImplPolarity::Negative }
1297 |
1298 epsilon!() => { |_| ImplPolarity::Positive }
1299 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001300 }
David Tolnay4c9be372016-10-06 00:47:37 -07001301
Alex Crichton954046c2017-05-30 21:49:42 -07001302 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001303 named!(parse -> Self, alt!(
1304 syn!(Const) => { Constness::Const }
1305 |
1306 epsilon!() => { |_| Constness::NotConst }
1307 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001308 }
David Tolnay42602292016-10-01 22:25:45 -07001309
Alex Crichton954046c2017-05-30 21:49:42 -07001310 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001311 named!(parse -> Self, alt!(
1312 syn!(Default_) => { Defaultness::Default }
1313 |
1314 epsilon!() => { |_| Defaultness::Final }
1315 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001316 }
David Tolnayedf2b992016-09-23 20:43:45 -07001317}
David Tolnay4a51dc72016-10-01 00:40:31 -07001318
1319#[cfg(feature = "printing")]
1320mod printing {
1321 use super::*;
1322 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001323 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001324 use quote::{Tokens, ToTokens};
1325
1326 impl ToTokens for Item {
1327 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001328 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001329 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001330 ItemKind::ExternCrate(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001331 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001332 item.extern_token.to_tokens(tokens);
1333 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001334 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001335 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001336 as_token.to_tokens(tokens);
1337 rename.to_tokens(tokens);
1338 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001339 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001340 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001341 ItemKind::Use(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001342 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001343 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001344 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001345 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001346 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001347 ItemKind::Static(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001348 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001349 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001350 item.mutbl.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 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001358 ItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001359 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001360 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001361 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001362 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001363 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001364 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001365 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001366 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001367 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001368 ItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001369 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001370 item.constness.to_tokens(tokens);
1371 item.unsafety.to_tokens(tokens);
1372 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001373 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.block.brace_token.surround(tokens, |tokens| {
1375 tokens.append_all(self.attrs.inner());
1376 tokens.append_all(&item.block.stmts);
1377 });
David Tolnay42602292016-10-01 22:25:45 -07001378 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001379 ItemKind::Mod(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001380 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001381 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001382 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001383 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001384 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001385 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001386 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001387 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001388 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001389 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001390 }
David Tolnay35902302016-10-06 01:11:08 -07001391 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001392 ItemKind::ForeignMod(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001393 item.abi.to_tokens(tokens);
1394 item.brace_token.surround(tokens, |tokens| {
1395 tokens.append_all(&item.items);
1396 });
David Tolnay35902302016-10-06 01:11:08 -07001397 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001398 ItemKind::Ty(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001399 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001400 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001401 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001402 item.generics.to_tokens(tokens);
1403 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001404 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001405 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001406 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001407 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 ItemKind::Enum(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001409 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001410 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001411 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001412 item.generics.to_tokens(tokens);
1413 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414 item.brace_token.surround(tokens, |tokens| {
1415 item.variants.to_tokens(tokens);
1416 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001417 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001418 ItemKind::Struct(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001419 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001420 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001421 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001422 item.generics.to_tokens(tokens);
1423 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001425 item.generics.where_clause.to_tokens(tokens);
1426 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001427 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001428 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001429 item.data.to_tokens(tokens);
1430 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 }
1433 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001434 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001435 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001436 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001437 }
1438 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001439 ItemKind::Union(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001440 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001441 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001442 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001443 item.generics.to_tokens(tokens);
1444 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001445 // XXX: Should we handle / complain when using a
1446 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001447 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001448 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001449 ItemKind::Trait(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001450 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001451 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001452 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001453 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001454 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001455 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001456 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001457 item.supertraits.to_tokens(tokens);
1458 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001459 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001460 item.brace_token.surround(tokens, |tokens| {
1461 tokens.append_all(&item.items);
1462 });
David Tolnayca085422016-10-04 00:12:38 -07001463 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001464 ItemKind::DefaultImpl(ref item) => {
1465 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001466 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001467 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001468 item.for_token.to_tokens(tokens);
1469 item.dot2_token.to_tokens(tokens);
1470 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001471 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001472 ItemKind::Impl(ref item) => {
1473 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001474 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001476 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001477 polarity.to_tokens(tokens);
1478 path.to_tokens(tokens);
1479 for_token.to_tokens(tokens);
1480 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001481 item.self_ty.to_tokens(tokens);
1482 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001483 item.brace_token.surround(tokens, |tokens| {
1484 tokens.append_all(&item.items);
1485 });
David Tolnay4c9be372016-10-06 00:47:37 -07001486 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001487 ItemKind::Mac(ref mac) => {
1488 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001489 mac.bang_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001490 mac.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001491 tokens.append_all(&mac.tokens);
1492 if !mac.is_braced() {
1493 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001494 }
1495 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001496 }
1497 }
1498 }
David Tolnay42602292016-10-01 22:25:45 -07001499
Alex Crichton62a0a592017-05-22 13:58:53 -07001500 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001501 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001502 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001503 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001504 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001505 self.rename.to_tokens(tokens);
1506 }
David Tolnay4a057422016-10-08 00:02:31 -07001507 }
1508 }
1509
Alex Crichton62a0a592017-05-22 13:58:53 -07001510 impl ToTokens for PathGlob {
1511 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001512 if self.path.segments.len() > 0 {
1513 self.path.to_tokens(tokens);
1514 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1515 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001516 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001517 }
1518 }
1519
1520 impl ToTokens for PathList {
1521 fn to_tokens(&self, tokens: &mut Tokens) {
1522 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001523 self.colon2_token.to_tokens(tokens);
1524 self.brace_token.surround(tokens, |tokens| {
1525 self.items.to_tokens(tokens);
1526 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001527 }
1528 }
1529
David Tolnay4a057422016-10-08 00:02:31 -07001530 impl ToTokens for PathListItem {
1531 fn to_tokens(&self, tokens: &mut Tokens) {
1532 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001533 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001534 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001535 self.rename.to_tokens(tokens);
1536 }
David Tolnay4a057422016-10-08 00:02:31 -07001537 }
1538 }
1539
David Tolnayca085422016-10-04 00:12:38 -07001540 impl ToTokens for TraitItem {
1541 fn to_tokens(&self, tokens: &mut Tokens) {
1542 tokens.append_all(self.attrs.outer());
1543 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001544 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001545 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001546 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001547 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001548 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001549 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001550 eq_token.to_tokens(tokens);
1551 default.to_tokens(tokens);
1552 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001553 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001554 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001555 TraitItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001556 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001557 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001558 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 block.brace_token.surround(tokens, |tokens| {
1560 tokens.append_all(self.attrs.inner());
1561 tokens.append_all(&block.stmts);
1562 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001563 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001564 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001565 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001566 }
David Tolnayca085422016-10-04 00:12:38 -07001567 }
1568 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001569 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001570 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001571 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001572 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001573 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001574 item.bounds.to_tokens(tokens);
1575 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001576 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001577 eq_token.to_tokens(tokens);
1578 default.to_tokens(tokens);
1579 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001580 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001581 }
1582 TraitItemKind::Macro(ref mac) => {
1583 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 if !mac.is_braced() {
1585 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001586 }
David Tolnayca085422016-10-04 00:12:38 -07001587 }
1588 }
1589 }
1590 }
1591
David Tolnay4c9be372016-10-06 00:47:37 -07001592 impl ToTokens for ImplItem {
1593 fn to_tokens(&self, tokens: &mut Tokens) {
1594 tokens.append_all(self.attrs.outer());
1595 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001596 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001597 item.vis.to_tokens(tokens);
1598 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001599 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001600 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001601 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001602 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001603 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001604 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001605 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001606 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001607 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001608 item.vis.to_tokens(tokens);
1609 item.defaultness.to_tokens(tokens);
1610 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001611 item.block.brace_token.surround(tokens, |tokens| {
1612 tokens.append_all(self.attrs.inner());
1613 tokens.append_all(&item.block.stmts);
1614 });
David Tolnay4c9be372016-10-06 00:47:37 -07001615 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001616 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001617 item.vis.to_tokens(tokens);
1618 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001619 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001620 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001621 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001622 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001623 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001624 }
1625 ImplItemKind::Macro(ref mac) => {
1626 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001627 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001628 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001629 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001630 }
1631 }
1632 }
1633 }
1634 }
1635
David Tolnay35902302016-10-06 01:11:08 -07001636 impl ToTokens for ForeignItem {
1637 fn to_tokens(&self, tokens: &mut Tokens) {
1638 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001639 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001640 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001641 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001642 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001643 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001644 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001645 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001646 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001647 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001648 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001650 }
1651 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001652 self.semi_token.to_tokens(tokens);
1653 }
1654 }
1655
David Tolnay570695e2017-06-03 16:15:13 -07001656 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001657 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001658 self.constness.to_tokens(tokens);
1659 self.unsafety.to_tokens(tokens);
1660 self.abi.to_tokens(tokens);
1661 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001662 }
1663 }
1664
David Tolnay570695e2017-06-03 16:15:13 -07001665 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001666
1667 impl<'a> ToTokens for NamedDecl<'a> {
1668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 self.0.fn_token.to_tokens(tokens);
1670 self.1.to_tokens(tokens);
1671 self.0.generics.to_tokens(tokens);
1672 self.0.paren_token.surround(tokens, |tokens| {
1673 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001674
1675 if self.0.variadic {
1676 if !self.0.inputs.empty_or_trailing() {
1677 tokens::Comma::default().to_tokens(tokens);
1678 }
Alex Crichton259ee532017-07-14 06:51:02 -07001679 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001680 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001681 });
1682 self.0.output.to_tokens(tokens);
1683 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001684 }
1685 }
1686
Alex Crichton62a0a592017-05-22 13:58:53 -07001687 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001688 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001689 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001690 self.lifetime.to_tokens(tokens);
1691 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001692 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001693 }
1694 }
1695
1696 impl ToTokens for ArgSelf {
1697 fn to_tokens(&self, tokens: &mut Tokens) {
1698 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001699 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001700 }
1701 }
1702
1703 impl ToTokens for ArgCaptured {
1704 fn to_tokens(&self, tokens: &mut Tokens) {
1705 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001706 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001707 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001708 }
1709 }
1710
David Tolnay42602292016-10-01 22:25:45 -07001711 impl ToTokens for Constness {
1712 fn to_tokens(&self, tokens: &mut Tokens) {
1713 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001714 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001715 Constness::NotConst => {
1716 // nothing
1717 }
David Tolnay42602292016-10-01 22:25:45 -07001718 }
1719 }
1720 }
1721
David Tolnay4c9be372016-10-06 00:47:37 -07001722 impl ToTokens for Defaultness {
1723 fn to_tokens(&self, tokens: &mut Tokens) {
1724 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001726 Defaultness::Final => {
1727 // nothing
1728 }
1729 }
1730 }
1731 }
1732
1733 impl ToTokens for ImplPolarity {
1734 fn to_tokens(&self, tokens: &mut Tokens) {
1735 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001736 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001737 ImplPolarity::Positive => {
1738 // nothing
1739 }
1740 }
1741 }
1742 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001743}