blob: e9ccd329cf90a77088609fb3eec04bfc61f0a802 [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 {
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700164 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 pub unsafety: Unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700166 pub impl_token: tokens::Impl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700168 /// Trait this impl implements.
169 pub trait_: Option<(ImplPolarity, Path, tokens::For)>,
170 /// The Self type of the impl.
171 pub self_ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700172 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub items: Vec<ImplItem>,
174 }),
175 /// A macro invocation (which includes macro definition).
176 ///
177 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
178 pub Mac(Mac),
179 }
180
181 do_not_generate_to_tokens
David Tolnayb79ee962016-09-04 09:39:20 -0700182}
183
David Tolnay0e837402016-12-22 17:25:55 -0500184impl From<DeriveInput> for Item {
185 fn from(input: DeriveInput) -> Item {
David Tolnay453cfd12016-10-23 11:00:14 -0700186 Item {
David Tolnay453cfd12016-10-23 11:00:14 -0700187 attrs: input.attrs,
188 node: match input.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700189 Body::Enum(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 ItemEnum {
David Tolnay570695e2017-06-03 16:15:13 -0700191 vis: input.vis,
192 enum_token: data.enum_token,
193 ident: input.ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 generics: input.generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700195 brace_token: data.brace_token,
David Tolnay570695e2017-06-03 16:15:13 -0700196 variants: data.variants,
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 }.into()
198 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 Body::Struct(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 ItemStruct {
David Tolnay570695e2017-06-03 16:15:13 -0700201 vis: input.vis,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700202 struct_token: data.struct_token,
David Tolnay570695e2017-06-03 16:15:13 -0700203 ident: input.ident,
204 generics: input.generics,
205 data: data.data,
206 semi_token: data.semi_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }.into()
208 }
David Tolnay453cfd12016-10-23 11:00:14 -0700209 },
210 }
211 }
212}
213
Alex Crichton62a0a592017-05-22 13:58:53 -0700214ast_enum_of_structs! {
215 pub enum ViewPath {
216 /// `foo::bar::baz as quux`
217 ///
218 /// or just
219 ///
220 /// `foo::bar::baz` (with `as baz` implicitly on the right)
221 pub Simple(PathSimple {
222 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700223 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub rename: Option<Ident>,
225 }),
226
227 /// `foo::bar::*`
228 pub Glob(PathGlob {
229 pub path: Path,
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700230 pub colon2_token: Option<tokens::Colon2>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 pub star_token: tokens::Star,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
233
234 /// `foo::bar::{a, b, c}`
235 pub List(PathList {
236 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700237 pub colon2_token: tokens::Colon2,
238 pub brace_token: tokens::Brace,
239 pub items: Delimited<PathListItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }),
241 }
242}
243
244ast_struct! {
245 pub struct PathListItem {
246 pub name: Ident,
247 /// renamed in list, e.g. `use foo::{bar as baz};`
248 pub rename: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700249 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }
251}
252
253ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700254 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700255 pub enum Constness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700256 Const(tokens::Const),
Alex Crichton62a0a592017-05-22 13:58:53 -0700257 NotConst,
258 }
259}
260
261ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700262 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700263 pub enum Defaultness {
Alex Crichton954046c2017-05-30 21:49:42 -0700264 Default(tokens::Default_),
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 Final,
266 }
267}
268
269ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700270 pub struct ForeignItem {
271 pub ident: Ident,
272 pub attrs: Vec<Attribute>,
273 pub node: ForeignItemKind,
274 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700275 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 }
277}
278
279ast_enum_of_structs! {
280 /// An item within an `extern` block
281 pub enum ForeignItemKind {
282 /// A foreign function
283 pub Fn(ForeignItemFn {
284 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 }),
286 /// A foreign static item (`static ext: u8`)
287 pub Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700288 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700290 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 pub mutbl: Mutability,
292 }),
293 }
294
295 do_not_generate_to_tokens
296}
297
298ast_struct! {
299 /// Represents an item declaration within a trait declaration,
300 /// possibly including a default implementation. A trait item is
301 /// either required (meaning it doesn't have an implementation, just a
302 /// signature) or provided (meaning it has a default implementation).
303 pub struct TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 pub attrs: Vec<Attribute>,
305 pub node: TraitItemKind,
306 }
307}
308
309ast_enum_of_structs! {
310 pub enum TraitItemKind {
311 pub Const(TraitItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700312 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700313 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700314 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700315 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700316 pub default: Option<(tokens::Eq, Expr)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700317 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 }),
319 pub Method(TraitItemMethod {
320 pub sig: MethodSig,
321 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700322 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
324 pub Type(TraitItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700325 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700326 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700327 pub colon_token: Option<tokens::Colon>,
328 pub bounds: Delimited<TyParamBound, tokens::Add>,
David Tolnay570695e2017-06-03 16:15:13 -0700329 pub default: Option<(tokens::Eq, Ty)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700330 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 }),
332 pub Macro(Mac),
333 }
334
335 do_not_generate_to_tokens
336}
337
338ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700339 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 pub enum ImplPolarity {
341 /// `impl Trait for Type`
342 Positive,
343 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700344 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700345 }
346}
347
348ast_struct! {
349 pub struct ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 pub attrs: Vec<Attribute>,
351 pub node: ImplItemKind,
352 }
353}
354
355ast_enum_of_structs! {
356 pub enum ImplItemKind {
357 pub Const(ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700358 pub vis: Visibility,
359 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700360 pub const_token: tokens::Const,
David Tolnay570695e2017-06-03 16:15:13 -0700361 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700362 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700364 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700365 pub expr: Expr,
David Tolnay570695e2017-06-03 16:15:13 -0700366 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700367 }),
368 pub Method(ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -0700369 pub vis: Visibility,
370 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 pub sig: MethodSig,
372 pub block: Block,
373 }),
374 pub Type(ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -0700375 pub vis: Visibility,
376 pub defaultness: Defaultness,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700377 pub type_token: tokens::Type,
David Tolnay570695e2017-06-03 16:15:13 -0700378 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700379 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub ty: Ty,
David Tolnay570695e2017-06-03 16:15:13 -0700381 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
383 pub Macro(Mac),
384 }
385
386 do_not_generate_to_tokens
387}
388
389ast_struct! {
390 /// Represents a method's signature in a trait declaration,
391 /// or in an implementation.
392 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700393 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700394 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700396 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 }
399}
400
401ast_struct! {
402 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700403 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 /// E.g. `fn foo(bar: baz)`
405 pub struct FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -0700406 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700407 pub paren_token: tokens::Paren,
408 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700409 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700410 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700412 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700414}
415
Alex Crichton62a0a592017-05-22 13:58:53 -0700416ast_enum_of_structs! {
417 /// An argument in a function header.
418 ///
419 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
420 pub enum FnArg {
421 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700422 pub and_token: tokens::And,
423 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 pub lifetime: Option<Lifetime>,
425 pub mutbl: Mutability,
426 }),
427 pub SelfValue(ArgSelf {
428 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700429 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 }),
431 pub Captured(ArgCaptured {
432 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700433 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 pub ty: Ty,
435 }),
436 pub Ignored(Ty),
437 }
David Tolnay62f374c2016-10-02 13:37:00 -0700438}
439
David Tolnayedf2b992016-09-23 20:43:45 -0700440#[cfg(feature = "parsing")]
441pub mod parsing {
442 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700443
Michael Layzell92639a52017-06-01 00:07:44 -0400444 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700445 use synom::tokens::*;
446 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700447
Alex Crichton954046c2017-05-30 21:49:42 -0700448 impl Synom for Item {
Michael Layzell92639a52017-06-01 00:07:44 -0400449 named!(parse -> Self, alt!(
450 item_extern_crate
451 |
452 item_use
453 |
454 item_static
455 |
456 item_const
457 |
458 item_fn
459 |
460 item_mod
461 |
462 item_foreign_mod
463 |
464 item_ty
465 |
466 item_struct_or_enum
467 |
468 item_union
469 |
470 item_trait
471 |
472 item_default_impl
473 |
474 item_impl
475 |
476 item_mac
477 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700478
479 fn description() -> Option<&'static str> {
480 Some("item")
481 }
482 }
David Tolnay453cfd12016-10-23 11:00:14 -0700483
David Tolnay84aa0752016-10-02 23:01:13 -0700484 named!(item_mac -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700485 attrs: many0!(call!(Attribute::parse_outer)) >>
486 what: syn!(Path) >>
487 bang: syn!(Bang) >>
David Tolnay570695e2017-06-03 16:15:13 -0700488 ident: option!(syn!(Ident)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700489 body: call!(::TokenTree::parse_delimited) >>
490 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700491 (Item {
David Tolnay84aa0752016-10-02 23:01:13 -0700492 attrs: attrs,
493 node: ItemKind::Mac(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -0500494 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700495 bang_token: bang,
496 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700497 tokens: vec![body],
David Tolnay84aa0752016-10-02 23:01:13 -0700498 }),
499 })
David Tolnayedf2b992016-09-23 20:43:45 -0700500 ));
501
David Tolnaya96a3fa2016-09-24 07:17:42 -0700502 named!(item_extern_crate -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700503 attrs: many0!(call!(Attribute::parse_outer)) >>
504 vis: syn!(Visibility) >>
505 extern_: syn!(Extern) >>
506 crate_: syn!(tokens::Crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700507 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700508 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
509 semi: syn!(Semi) >>
David Tolnay570695e2017-06-03 16:15:13 -0700510 (Item {
511 attrs: attrs,
512 node: ItemExternCrate {
David Tolnayedf2b992016-09-23 20:43:45 -0700513 vis: vis,
David Tolnay570695e2017-06-03 16:15:13 -0700514 extern_token: extern_,
515 crate_token: crate_,
516 ident: ident,
517 rename: rename,
518 semi_token: semi,
519 }.into(),
David Tolnayedf2b992016-09-23 20:43:45 -0700520 })
521 ));
522
David Tolnay4a057422016-10-08 00:02:31 -0700523 named!(item_use -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700524 attrs: many0!(call!(Attribute::parse_outer)) >>
525 vis: syn!(Visibility) >>
526 use_: syn!(Use) >>
527 what: syn!(ViewPath) >>
528 semi: syn!(Semi) >>
David Tolnay4a057422016-10-08 00:02:31 -0700529 (Item {
David Tolnay4a057422016-10-08 00:02:31 -0700530 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700531 node: ItemUse {
David Tolnay570695e2017-06-03 16:15:13 -0700532 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700533 use_token: use_,
David Tolnay570695e2017-06-03 16:15:13 -0700534 path: Box::new(what),
Alex Crichton954046c2017-05-30 21:49:42 -0700535 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536 }.into(),
David Tolnay4a057422016-10-08 00:02:31 -0700537 })
538 ));
539
Alex Crichton954046c2017-05-30 21:49:42 -0700540 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400541 named!(parse -> Self, alt!(
542 syn!(PathGlob) => { ViewPath::Glob }
543 |
544 syn!(PathList) => { ViewPath::List }
545 |
546 syn!(PathSimple) => { ViewPath::Simple } // must be last
547 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700548 }
David Tolnay4a057422016-10-08 00:02:31 -0700549
Alex Crichton954046c2017-05-30 21:49:42 -0700550 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400551 named!(parse -> Self, do_parse!(
552 path: syn!(Path) >>
553 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
554 (PathSimple {
555 path: path,
556 as_token: rename.as_ref().map(|p| As((p.0).0)),
557 rename: rename.map(|p| p.1),
558 })
559 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700560 }
David Tolnay4a057422016-10-08 00:02:31 -0700561
Alex Crichton954046c2017-05-30 21:49:42 -0700562 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400563 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700564 path: option!(do_parse!(
565 path: syn!(Path) >>
566 colon2: syn!(Colon2) >>
567 (path, colon2)
568 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400569 star: syn!(Star) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700570 ({
571 match path {
572 Some((path, colon2)) => {
573 PathGlob {
574 path: path,
575 colon2_token: Some(colon2),
576 star_token: star,
577 }
578 }
579 None => {
580 PathGlob {
581 path: Path {
582 leading_colon: None,
583 segments: Default::default(),
584 },
585 colon2_token: None,
586 star_token: star,
587 }
588 }
589 }
Michael Layzell92639a52017-06-01 00:07:44 -0400590 })
591 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700592 }
David Tolnay4a057422016-10-08 00:02:31 -0700593
Alex Crichton954046c2017-05-30 21:49:42 -0700594 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400595 named!(parse -> Self, alt!(
596 do_parse!(
597 path: syn!(Path) >>
598 colon2: syn!(Colon2) >>
599 items: braces!(call!(Delimited::parse_terminated)) >>
600 (PathList {
601 path: path,
602 items: items.0,
603 brace_token: items.1,
604 colon2_token: colon2,
605 })
606 )
607 |
608 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -0700609 colon: option!(syn!(Colon2)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400610 items: braces!(call!(Delimited::parse_terminated)) >>
611 (PathList {
612 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400613 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700614 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400615 },
David Tolnay570695e2017-06-03 16:15:13 -0700616 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400617 brace_token: items.1,
618 items: items.0,
619 })
620 )
621 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700622 }
David Tolnay4a057422016-10-08 00:02:31 -0700623
Alex Crichton954046c2017-05-30 21:49:42 -0700624 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400625 named!(parse -> Self, do_parse!(
626 name: alt!(
627 syn!(Ident)
628 |
629 map!(syn!(Self_), Into::into)
630 ) >>
631 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
632 (PathListItem {
633 name: name,
634 as_token: rename.as_ref().map(|p| As((p.0).0)),
635 rename: rename.map(|p| p.1),
636 })
637 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
David Tolnay4a057422016-10-08 00:02:31 -0700639
David Tolnay47a877c2016-10-01 16:50:55 -0700640 named!(item_static -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700641 attrs: many0!(call!(Attribute::parse_outer)) >>
642 vis: syn!(Visibility) >>
643 static_: syn!(Static) >>
644 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700645 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700646 colon: syn!(Colon) >>
647 ty: syn!(Ty) >>
648 eq: syn!(Eq) >>
649 value: syn!(Expr) >>
650 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700651 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700652 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 node: ItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700654 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700655 static_token: static_,
David Tolnay570695e2017-06-03 16:15:13 -0700656 mutbl: mutability,
657 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700658 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700659 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700660 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700661 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700662 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700663 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700664 })
665 ));
666
667 named!(item_const -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700668 attrs: many0!(call!(Attribute::parse_outer)) >>
669 vis: syn!(Visibility) >>
670 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700671 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700672 colon: syn!(Colon) >>
673 ty: syn!(Ty) >>
674 eq: syn!(Eq) >>
675 value: syn!(Expr) >>
676 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700677 (Item {
David Tolnay47a877c2016-10-01 16:50:55 -0700678 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700679 node: ItemConst {
David Tolnay570695e2017-06-03 16:15:13 -0700680 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700681 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -0700682 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700683 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -0700684 ty: Box::new(ty),
Alex Crichton954046c2017-05-30 21:49:42 -0700685 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -0700686 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700687 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700688 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700689 })
690 ));
691
David Tolnay42602292016-10-01 22:25:45 -0700692 named!(item_fn -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700693 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
694 vis: syn!(Visibility) >>
695 constness: syn!(Constness) >>
696 unsafety: syn!(Unsafety) >>
697 abi: option!(syn!(Abi)) >>
698 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700699 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700700 generics: syn!(Generics) >>
701 inputs: parens!(Delimited::parse_terminated) >>
702 ret: syn!(FunctionRetTy) >>
703 where_clause: syn!(WhereClause) >>
704 inner_attrs_stmts: braces!(tuple!(
705 many0!(call!(Attribute::parse_inner)),
706 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400707 )) >>
David Tolnay42602292016-10-01 22:25:45 -0700708 (Item {
David Tolnay3b9783a2016-10-29 22:37:09 -0700709 attrs: {
710 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700711 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700712 attrs
713 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700714 node: ItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700715 vis: vis,
716 constness: constness,
717 unsafety: unsafety,
718 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700719 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700720 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700721 fn_token: fn_,
722 paren_token: inputs.1,
723 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700724 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700725 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700726 generics: Generics {
727 where_clause: where_clause,
728 .. generics
729 },
David Tolnay42602292016-10-01 22:25:45 -0700730 }),
David Tolnay570695e2017-06-03 16:15:13 -0700731 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 block: Box::new(Block {
Alex Crichton954046c2017-05-30 21:49:42 -0700733 brace_token: inner_attrs_stmts.1,
734 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -0700735 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700736 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700737 })
738 ));
739
Alex Crichton954046c2017-05-30 21:49:42 -0700740 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400741 named!(parse -> Self, alt!(
742 do_parse!(
743 and: syn!(And) >>
744 lt: option!(syn!(Lifetime)) >>
745 mutability: syn!(Mutability) >>
746 self_: syn!(Self_) >>
747 not!(syn!(Colon)) >>
748 (ArgSelfRef {
749 lifetime: lt,
750 mutbl: mutability,
751 and_token: and,
752 self_token: self_,
753 }.into())
754 )
755 |
756 do_parse!(
757 mutability: syn!(Mutability) >>
758 self_: syn!(Self_) >>
759 not!(syn!(Colon)) >>
760 (ArgSelf {
761 mutbl: mutability,
762 self_token: self_,
763 }.into())
764 )
765 |
766 do_parse!(
767 pat: syn!(Pat) >>
768 colon: syn!(Colon) >>
769 ty: syn!(Ty) >>
770 (ArgCaptured {
771 pat: pat,
772 ty: ty,
773 colon_token: colon,
774 }.into())
775 )
776 |
777 syn!(Ty) => { FnArg::Ignored }
778 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700779 }
David Tolnay62f374c2016-10-02 13:37:00 -0700780
David Tolnay35902302016-10-06 01:11:08 -0700781 named!(item_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700782 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
783 vis: syn!(Visibility) >>
784 mod_: syn!(Mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700785 ident: syn!(Ident) >>
786 content_semi: alt!(
787 syn!(Semi) => {|semi| (
788 Vec::new(),
789 None,
790 Some(semi),
791 )}
David Tolnay37d10332016-10-13 20:51:04 -0700792 |
Alex Crichton954046c2017-05-30 21:49:42 -0700793 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700794 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700795 many0!(call!(Attribute::parse_inner)),
796 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400797 )
David Tolnay570695e2017-06-03 16:15:13 -0700798 ) => {|((inner_attrs, items), brace)| (
799 inner_attrs,
800 Some((brace, items)),
801 None,
802 )}
David Tolnay37d10332016-10-13 20:51:04 -0700803 ) >>
David Tolnay570695e2017-06-03 16:15:13 -0700804 (Item {
805 attrs: {
806 let mut attrs = outer_attrs;
807 attrs.extend(content_semi.0);
808 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700809 },
David Tolnay570695e2017-06-03 16:15:13 -0700810 node: ItemMod {
David Tolnay7b8009b2016-10-25 22:36:00 -0700811 vis: vis,
David Tolnay570695e2017-06-03 16:15:13 -0700812 mod_token: mod_,
813 ident: ident,
814 content: content_semi.1,
815 semi: content_semi.2,
816 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700817 })
818 ));
819
820 named!(item_foreign_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700821 attrs: many0!(call!(Attribute::parse_outer)) >>
822 abi: syn!(Abi) >>
823 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay35902302016-10-06 01:11:08 -0700824 (Item {
David Tolnay35902302016-10-06 01:11:08 -0700825 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700826 node: ItemForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700827 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -0700828 brace_token: items.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700829 items: items.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700830 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700831 })
832 ));
833
Alex Crichton954046c2017-05-30 21:49:42 -0700834 impl Synom for ForeignItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400835 named!(parse -> Self, alt!(
836 foreign_fn
837 |
838 foreign_static
839 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700840 }
David Tolnay35902302016-10-06 01:11:08 -0700841
842 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700843 attrs: many0!(call!(Attribute::parse_outer)) >>
844 vis: syn!(Visibility) >>
845 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -0700846 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700847 generics: syn!(Generics) >>
848 inputs: parens!(do_parse!(
849 args: call!(Delimited::parse_terminated) >>
850 variadic: cond!(args.is_empty() || args.trailing_delim(),
851 option!(syn!(Dot3))) >>
852 (args, variadic)
853 )) >>
854 ret: syn!(FunctionRetTy) >>
855 where_clause: syn!(WhereClause) >>
856 semi: syn!(Semi) >>
857 ({
858 let ((inputs, variadic), parens) = inputs;
859 let variadic = variadic.and_then(|v| v);
860 ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700861 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700862 attrs: attrs,
863 semi_token: semi,
864 node: ForeignItemFn {
865 decl: Box::new(FnDecl {
866 fn_token: fn_,
867 paren_token: parens,
868 inputs: inputs,
869 variadic: variadic.is_some(),
870 dot_tokens: variadic,
871 output: ret,
872 generics: Generics {
873 where_clause: where_clause,
874 .. generics
875 },
876 }),
877 }.into(),
878 vis: vis,
879 }
David Tolnay35902302016-10-06 01:11:08 -0700880 })
881 ));
882
883 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700884 attrs: many0!(call!(Attribute::parse_outer)) >>
885 vis: syn!(Visibility) >>
886 static_: syn!(Static) >>
887 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700888 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700889 colon: syn!(Colon) >>
890 ty: syn!(Ty) >>
891 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700892 (ForeignItem {
David Tolnay570695e2017-06-03 16:15:13 -0700893 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700894 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700895 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 node: ForeignItemStatic {
897 ty: Box::new(ty),
898 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700899 static_token: static_,
900 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700901 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700902 vis: vis,
903 })
904 ));
905
David Tolnay3cf52982016-10-01 17:11:37 -0700906 named!(item_ty -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700907 attrs: many0!(call!(Attribute::parse_outer)) >>
908 vis: syn!(Visibility) >>
909 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700910 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700911 generics: syn!(Generics) >>
912 where_clause: syn!(WhereClause) >>
913 eq: syn!(Eq) >>
914 ty: syn!(Ty) >>
915 semi: syn!(Semi) >>
David Tolnay3cf52982016-10-01 17:11:37 -0700916 (Item {
David Tolnay3cf52982016-10-01 17:11:37 -0700917 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700918 node: ItemTy {
David Tolnay570695e2017-06-03 16:15:13 -0700919 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700920 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -0700921 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700922 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700923 where_clause: where_clause,
924 ..generics
925 },
David Tolnay570695e2017-06-03 16:15:13 -0700926 eq_token: eq,
927 ty: Box::new(ty),
928 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700929 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700930 })
931 ));
932
David Tolnay570695e2017-06-03 16:15:13 -0700933 named!(item_struct_or_enum -> Item, map!(syn!(DeriveInput), Into::into));
David Tolnay42602292016-10-01 22:25:45 -0700934
David Tolnay2f9fa632016-10-03 22:08:48 -0700935 named!(item_union -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700936 attrs: many0!(call!(Attribute::parse_outer)) >>
937 vis: syn!(Visibility) >>
938 union_: syn!(Union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700939 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700940 generics: syn!(Generics) >>
941 where_clause: syn!(WhereClause) >>
942 fields: braces!(call!(Delimited::parse_terminated_with,
943 Field::parse_struct)) >>
David Tolnay2f9fa632016-10-03 22:08:48 -0700944 (Item {
David Tolnay2f9fa632016-10-03 22:08:48 -0700945 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700946 node: ItemUnion {
David Tolnay570695e2017-06-03 16:15:13 -0700947 vis: vis,
Alex Crichton954046c2017-05-30 21:49:42 -0700948 union_token: union_,
David Tolnay570695e2017-06-03 16:15:13 -0700949 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700951 where_clause: where_clause,
952 .. generics
953 },
David Tolnay570695e2017-06-03 16:15:13 -0700954 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700956 })
957 ));
958
David Tolnay0aecb732016-10-03 23:03:50 -0700959 named!(item_trait -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700960 attrs: many0!(call!(Attribute::parse_outer)) >>
961 vis: syn!(Visibility) >>
962 unsafety: syn!(Unsafety) >>
963 trait_: syn!(Trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700964 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700965 generics: syn!(Generics) >>
966 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700967 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700968 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700969 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700970 where_clause: syn!(WhereClause) >>
971 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700972 (Item {
David Tolnay0aecb732016-10-03 23:03:50 -0700973 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700974 node: ItemTrait {
David Tolnay570695e2017-06-03 16:15:13 -0700975 vis: vis,
Alex Crichton62a0a592017-05-22 13:58:53 -0700976 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -0700977 trait_token: trait_,
978 ident: ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700979 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -0700980 where_clause: where_clause,
981 .. generics
982 },
David Tolnay570695e2017-06-03 16:15:13 -0700983 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700984 supertraits: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -0700985 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -0700986 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700987 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -0700988 })
989 ));
990
David Tolnayf94e2362016-10-04 00:29:51 -0700991 named!(item_default_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700992 attrs: many0!(call!(Attribute::parse_outer)) >>
993 unsafety: syn!(Unsafety) >>
994 impl_: syn!(Impl) >>
995 path: syn!(Path) >>
996 for_: syn!(For) >>
997 dot2: syn!(Dot2) >>
998 braces: braces!(epsilon!()) >>
David Tolnayf94e2362016-10-04 00:29:51 -0700999 (Item {
David Tolnayf94e2362016-10-04 00:29:51 -07001000 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001001 node: ItemDefaultImpl {
1002 unsafety: unsafety,
Alex Crichton954046c2017-05-30 21:49:42 -07001003 impl_token: impl_,
David Tolnay570695e2017-06-03 16:15:13 -07001004 path: path,
Alex Crichton954046c2017-05-30 21:49:42 -07001005 for_token: for_,
1006 dot2_token: dot2,
1007 brace_token: braces.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001009 })
1010 ));
1011
Alex Crichton954046c2017-05-30 21:49:42 -07001012 impl Synom for TraitItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001013 named!(parse -> Self, alt!(
1014 trait_item_const
1015 |
1016 trait_item_method
1017 |
1018 trait_item_type
1019 |
1020 trait_item_mac
1021 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001022 }
David Tolnay0aecb732016-10-03 23:03:50 -07001023
1024 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001025 attrs: many0!(call!(Attribute::parse_outer)) >>
1026 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001027 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001028 colon: syn!(Colon) >>
1029 ty: syn!(Ty) >>
David Tolnay570695e2017-06-03 16:15:13 -07001030 default: option!(tuple!(syn!(Eq), syn!(Expr))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001031 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001032 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001033 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001034 node: TraitItemConst {
Alex Crichton954046c2017-05-30 21:49:42 -07001035 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001036 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001037 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001038 ty: ty,
1039 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001040 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001041 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001042 })
1043 ));
1044
1045 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001046 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1047 constness: syn!(Constness) >>
1048 unsafety: syn!(Unsafety) >>
1049 abi: option!(syn!(Abi)) >>
1050 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001051 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001052 generics: syn!(Generics) >>
1053 inputs: parens!(call!(Delimited::parse_terminated)) >>
1054 ret: syn!(FunctionRetTy) >>
1055 where_clause: syn!(WhereClause) >>
1056 body: option!(braces!(
1057 tuple!(many0!(call!(Attribute::parse_inner)),
1058 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001059 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001060 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001061 ({
1062 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001063 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001064 None => (Vec::new(), None),
1065 };
1066 TraitItem {
David Tolnay5859df12016-10-29 22:49:54 -07001067 attrs: {
1068 let mut attrs = outer_attrs;
1069 attrs.extend(inner_attrs);
1070 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001071 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001072 node: TraitItemMethod {
1073 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001074 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001075 unsafety: unsafety,
David Tolnay5859df12016-10-29 22:49:54 -07001076 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001077 ident: ident,
David Tolnay5859df12016-10-29 22:49:54 -07001078 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001079 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001080 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001081 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001082 fn_token: fn_,
1083 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001084 dot_tokens: None,
1085 generics: Generics {
1086 where_clause: where_clause,
1087 .. generics
1088 },
David Tolnay5859df12016-10-29 22:49:54 -07001089 },
1090 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001091 default: stmts.map(|stmts| {
1092 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001093 stmts: stmts.0,
1094 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001095 }
1096 }),
David Tolnay570695e2017-06-03 16:15:13 -07001097 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001098 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001099 }
David Tolnay0aecb732016-10-03 23:03:50 -07001100 })
1101 ));
1102
1103 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001104 attrs: many0!(call!(Attribute::parse_outer)) >>
1105 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001106 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001107 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001108 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001109 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001110 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001111 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1112 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001113 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001114 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001115 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001116 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001117 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001118 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001119 bounds: bounds.unwrap_or_default(),
David Tolnay570695e2017-06-03 16:15:13 -07001120 default: default,
Alex Crichton954046c2017-05-30 21:49:42 -07001121 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001122 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001123 })
1124 ));
1125
1126 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001127 attrs: many0!(call!(Attribute::parse_outer)) >>
1128 mac: syn!(Mac) >>
1129 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001130 (TraitItem {
David Tolnay0aecb732016-10-03 23:03:50 -07001131 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001132 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001133 })
1134 ));
1135
David Tolnay4c9be372016-10-06 00:47:37 -07001136 named!(item_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001137 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001138 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001139 unsafety: syn!(Unsafety) >>
1140 impl_: syn!(Impl) >>
1141 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001142 polarity_path: alt!(
1143 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001144 polarity: syn!(ImplPolarity) >>
1145 path: syn!(Path) >>
1146 for_: syn!(For) >>
David Tolnay570695e2017-06-03 16:15:13 -07001147 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001148 )
1149 |
David Tolnay570695e2017-06-03 16:15:13 -07001150 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001151 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001152 self_ty: syn!(Ty) >>
1153 where_clause: syn!(WhereClause) >>
1154 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001155 (Item {
David Tolnay4c9be372016-10-06 00:47:37 -07001156 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001157 node: ItemImpl {
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001158 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001159 unsafety: unsafety,
David Tolnay570695e2017-06-03 16:15:13 -07001160 impl_token: impl_,
Alex Crichton62a0a592017-05-22 13:58:53 -07001161 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001162 where_clause: where_clause,
1163 .. generics
1164 },
David Tolnay570695e2017-06-03 16:15:13 -07001165 trait_: polarity_path,
Alex Crichton62a0a592017-05-22 13:58:53 -07001166 self_ty: Box::new(self_ty),
David Tolnay570695e2017-06-03 16:15:13 -07001167 brace_token: body.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001168 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001169 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001170 })
1171 ));
1172
Alex Crichton954046c2017-05-30 21:49:42 -07001173 impl Synom for ImplItem {
Michael Layzell92639a52017-06-01 00:07:44 -04001174 named!(parse -> Self, alt!(
1175 impl_item_const
1176 |
1177 impl_item_method
1178 |
1179 impl_item_type
1180 |
David Tolnay570695e2017-06-03 16:15:13 -07001181 impl_item_mac
Michael Layzell92639a52017-06-01 00:07:44 -04001182 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001183 }
David Tolnay4c9be372016-10-06 00:47:37 -07001184
1185 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001186 attrs: many0!(call!(Attribute::parse_outer)) >>
1187 vis: syn!(Visibility) >>
1188 defaultness: syn!(Defaultness) >>
1189 const_: syn!(Const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001190 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001191 colon: syn!(Colon) >>
1192 ty: syn!(Ty) >>
1193 eq: syn!(Eq) >>
1194 value: syn!(Expr) >>
1195 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001196 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001197 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001198 node: ImplItemConst {
David Tolnay570695e2017-06-03 16:15:13 -07001199 vis: vis,
1200 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001201 const_token: const_,
David Tolnay570695e2017-06-03 16:15:13 -07001202 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001203 colon_token: colon,
David Tolnay570695e2017-06-03 16:15:13 -07001204 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001205 eq_token: eq,
David Tolnay570695e2017-06-03 16:15:13 -07001206 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001207 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001208 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001209 })
1210 ));
1211
1212 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001213 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1214 vis: syn!(Visibility) >>
1215 defaultness: syn!(Defaultness) >>
1216 constness: syn!(Constness) >>
1217 unsafety: syn!(Unsafety) >>
1218 abi: option!(syn!(Abi)) >>
1219 fn_: syn!(Fn_) >>
David Tolnay570695e2017-06-03 16:15:13 -07001220 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001221 generics: syn!(Generics) >>
1222 inputs: parens!(call!(Delimited::parse_terminated)) >>
1223 ret: syn!(FunctionRetTy) >>
1224 where_clause: syn!(WhereClause) >>
1225 inner_attrs_stmts: braces!(tuple!(
1226 many0!(call!(Attribute::parse_inner)),
1227 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001228 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001229 (ImplItem {
David Tolnay3b9783a2016-10-29 22:37:09 -07001230 attrs: {
1231 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001232 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001233 attrs
1234 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001235 node: ImplItemMethod {
David Tolnay570695e2017-06-03 16:15:13 -07001236 vis: vis,
1237 defaultness: defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -07001238 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001239 constness: constness,
David Tolnay570695e2017-06-03 16:15:13 -07001240 unsafety: unsafety,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001241 abi: abi,
David Tolnay570695e2017-06-03 16:15:13 -07001242 ident: ident,
David Tolnay4c9be372016-10-06 00:47:37 -07001243 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001244 fn_token: fn_,
1245 paren_token: inputs.1,
1246 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001247 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001248 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001249 generics: Generics {
1250 where_clause: where_clause,
1251 .. generics
1252 },
1253 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001254 },
1255 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001256 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001257 brace_token: inner_attrs_stmts.1,
1258 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001259 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001260 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001261 })
1262 ));
1263
1264 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001265 attrs: many0!(call!(Attribute::parse_outer)) >>
1266 vis: syn!(Visibility) >>
1267 defaultness: syn!(Defaultness) >>
1268 type_: syn!(Type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001269 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001270 eq: syn!(Eq) >>
1271 ty: syn!(Ty) >>
1272 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001273 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001274 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001275 node: ImplItemType {
David Tolnay570695e2017-06-03 16:15:13 -07001276 vis: vis,
1277 defaultness: defaultness,
Alex Crichton954046c2017-05-30 21:49:42 -07001278 type_token: type_,
David Tolnay570695e2017-06-03 16:15:13 -07001279 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001280 eq_token: eq,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001281 ty: ty,
David Tolnay570695e2017-06-03 16:15:13 -07001282 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001283 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001284 })
1285 ));
1286
David Tolnay570695e2017-06-03 16:15:13 -07001287 named!(impl_item_mac -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001288 attrs: many0!(call!(Attribute::parse_outer)) >>
1289 mac: syn!(Mac) >>
1290 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001291 (ImplItem {
David Tolnay4c9be372016-10-06 00:47:37 -07001292 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001293 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001294 })
1295 ));
1296
Alex Crichton954046c2017-05-30 21:49:42 -07001297 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001298 named!(parse -> Self, alt!(
1299 syn!(Bang) => { ImplPolarity::Negative }
1300 |
1301 epsilon!() => { |_| ImplPolarity::Positive }
1302 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001303 }
David Tolnay4c9be372016-10-06 00:47:37 -07001304
Alex Crichton954046c2017-05-30 21:49:42 -07001305 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001306 named!(parse -> Self, alt!(
1307 syn!(Const) => { Constness::Const }
1308 |
1309 epsilon!() => { |_| Constness::NotConst }
1310 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001311 }
David Tolnay42602292016-10-01 22:25:45 -07001312
Alex Crichton954046c2017-05-30 21:49:42 -07001313 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001314 named!(parse -> Self, alt!(
1315 syn!(Default_) => { Defaultness::Default }
1316 |
1317 epsilon!() => { |_| Defaultness::Final }
1318 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001319 }
David Tolnayedf2b992016-09-23 20:43:45 -07001320}
David Tolnay4a51dc72016-10-01 00:40:31 -07001321
1322#[cfg(feature = "printing")]
1323mod printing {
1324 use super::*;
1325 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001326 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001327 use quote::{Tokens, ToTokens};
1328
1329 impl ToTokens for Item {
1330 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001331 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001332 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001333 ItemKind::ExternCrate(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001334 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001335 item.extern_token.to_tokens(tokens);
1336 item.crate_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001337 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001338 if let Some((ref as_token, ref rename)) = item.rename {
David Tolnay570695e2017-06-03 16:15:13 -07001339 as_token.to_tokens(tokens);
1340 rename.to_tokens(tokens);
1341 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001342 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001343 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001344 ItemKind::Use(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001345 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001346 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001347 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001348 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001349 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001350 ItemKind::Static(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001351 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001352 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001353 item.mutbl.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001354 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001355 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001356 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001357 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001358 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001359 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001360 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001361 ItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001362 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001363 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001364 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001366 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001367 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001368 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001369 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001370 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001371 ItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001372 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001373 item.constness.to_tokens(tokens);
1374 item.unsafety.to_tokens(tokens);
1375 item.abi.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001376 NamedDecl(&item.decl, item.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001377 item.block.brace_token.surround(tokens, |tokens| {
1378 tokens.append_all(self.attrs.inner());
1379 tokens.append_all(&item.block.stmts);
1380 });
David Tolnay42602292016-10-01 22:25:45 -07001381 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001382 ItemKind::Mod(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001383 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001384 item.mod_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001385 item.ident.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001386 if let Some((ref brace, ref items)) = item.content {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001387 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001388 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001389 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001390 });
Michael Layzell3936ceb2017-07-08 00:28:36 -04001391 } else {
Alex Crichton259ee532017-07-14 06:51:02 -07001392 TokensOrDefault(&item.semi).to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001393 }
David Tolnay35902302016-10-06 01:11:08 -07001394 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 ItemKind::ForeignMod(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001396 item.abi.to_tokens(tokens);
1397 item.brace_token.surround(tokens, |tokens| {
1398 tokens.append_all(&item.items);
1399 });
David Tolnay35902302016-10-06 01:11:08 -07001400 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001401 ItemKind::Ty(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001402 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001403 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001404 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001405 item.generics.to_tokens(tokens);
1406 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001407 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001409 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001410 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001411 ItemKind::Enum(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001412 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001413 item.enum_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001414 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001415 item.generics.to_tokens(tokens);
1416 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001417 item.brace_token.surround(tokens, |tokens| {
1418 item.variants.to_tokens(tokens);
1419 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001420 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001421 ItemKind::Struct(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001422 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001423 item.struct_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001424 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001425 item.generics.to_tokens(tokens);
1426 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001427 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001428 item.generics.where_clause.to_tokens(tokens);
1429 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001430 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001431 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001432 item.data.to_tokens(tokens);
1433 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001434 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001435 }
1436 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001437 item.generics.where_clause.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07001438 TokensOrDefault(&item.semi_token).to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001439 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001440 }
1441 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001442 ItemKind::Union(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001443 item.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001444 item.union_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001445 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001446 item.generics.to_tokens(tokens);
1447 item.generics.where_clause.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001448 // XXX: Should we handle / complain when using a
1449 // non-VariantData::Struct Variant in Union?
Alex Crichton62a0a592017-05-22 13:58:53 -07001450 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001451 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001452 ItemKind::Trait(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001453 item.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001454 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001455 item.trait_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001456 item.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001457 item.generics.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001458 if !item.supertraits.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001459 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001460 item.supertraits.to_tokens(tokens);
1461 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001462 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001463 item.brace_token.surround(tokens, |tokens| {
1464 tokens.append_all(&item.items);
1465 });
David Tolnayca085422016-10-04 00:12:38 -07001466 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001467 ItemKind::DefaultImpl(ref item) => {
1468 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001469 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001470 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001471 item.for_token.to_tokens(tokens);
1472 item.dot2_token.to_tokens(tokens);
1473 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001474 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 ItemKind::Impl(ref item) => {
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001476 item.defaultness.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001477 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001478 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001479 item.generics.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001480 if let Some((ref polarity, ref path, ref for_token)) = item.trait_ {
David Tolnay570695e2017-06-03 16:15:13 -07001481 polarity.to_tokens(tokens);
1482 path.to_tokens(tokens);
1483 for_token.to_tokens(tokens);
1484 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001485 item.self_ty.to_tokens(tokens);
1486 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001487 item.brace_token.surround(tokens, |tokens| {
1488 tokens.append_all(&item.items);
1489 });
David Tolnay4c9be372016-10-06 00:47:37 -07001490 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001491 ItemKind::Mac(ref mac) => {
1492 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001493 mac.bang_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001494 mac.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001495 tokens.append_all(&mac.tokens);
1496 if !mac.is_braced() {
1497 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001498 }
1499 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001500 }
1501 }
1502 }
David Tolnay42602292016-10-01 22:25:45 -07001503
Alex Crichton62a0a592017-05-22 13:58:53 -07001504 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001505 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001506 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001507 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001508 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001509 self.rename.to_tokens(tokens);
1510 }
David Tolnay4a057422016-10-08 00:02:31 -07001511 }
1512 }
1513
Alex Crichton62a0a592017-05-22 13:58:53 -07001514 impl ToTokens for PathGlob {
1515 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001516 if self.path.segments.len() > 0 {
1517 self.path.to_tokens(tokens);
1518 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1519 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001520 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001521 }
1522 }
1523
1524 impl ToTokens for PathList {
1525 fn to_tokens(&self, tokens: &mut Tokens) {
1526 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001527 self.colon2_token.to_tokens(tokens);
1528 self.brace_token.surround(tokens, |tokens| {
1529 self.items.to_tokens(tokens);
1530 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001531 }
1532 }
1533
David Tolnay4a057422016-10-08 00:02:31 -07001534 impl ToTokens for PathListItem {
1535 fn to_tokens(&self, tokens: &mut Tokens) {
1536 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001537 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001538 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001539 self.rename.to_tokens(tokens);
1540 }
David Tolnay4a057422016-10-08 00:02:31 -07001541 }
1542 }
1543
David Tolnayca085422016-10-04 00:12:38 -07001544 impl ToTokens for TraitItem {
1545 fn to_tokens(&self, tokens: &mut Tokens) {
1546 tokens.append_all(self.attrs.outer());
1547 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001548 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001549 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001550 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001551 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001552 item.ty.to_tokens(tokens);
David Tolnayb99e1b02017-06-03 19:00:55 -07001553 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001554 eq_token.to_tokens(tokens);
1555 default.to_tokens(tokens);
1556 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001557 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001558 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001559 TraitItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001560 item.sig.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001561 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001562 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 block.brace_token.surround(tokens, |tokens| {
1564 tokens.append_all(self.attrs.inner());
1565 tokens.append_all(&block.stmts);
1566 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001567 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001568 None => {
Alex Crichton259ee532017-07-14 06:51:02 -07001569 TokensOrDefault(&item.semi_token).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001570 }
David Tolnayca085422016-10-04 00:12:38 -07001571 }
1572 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001573 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001574 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001575 item.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001576 if !item.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001577 TokensOrDefault(&item.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001578 item.bounds.to_tokens(tokens);
1579 }
David Tolnayb99e1b02017-06-03 19:00:55 -07001580 if let Some((ref eq_token, ref default)) = item.default {
David Tolnay570695e2017-06-03 16:15:13 -07001581 eq_token.to_tokens(tokens);
1582 default.to_tokens(tokens);
1583 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001585 }
1586 TraitItemKind::Macro(ref mac) => {
1587 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001588 if !mac.is_braced() {
1589 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001590 }
David Tolnayca085422016-10-04 00:12:38 -07001591 }
1592 }
1593 }
1594 }
1595
David Tolnay4c9be372016-10-06 00:47:37 -07001596 impl ToTokens for ImplItem {
1597 fn to_tokens(&self, tokens: &mut Tokens) {
1598 tokens.append_all(self.attrs.outer());
1599 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001600 ImplItemKind::Const(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001601 item.vis.to_tokens(tokens);
1602 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001603 item.const_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001604 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001605 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001606 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001607 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001608 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001609 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001610 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001611 ImplItemKind::Method(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001612 item.vis.to_tokens(tokens);
1613 item.defaultness.to_tokens(tokens);
1614 item.sig.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001615 item.block.brace_token.surround(tokens, |tokens| {
1616 tokens.append_all(self.attrs.inner());
1617 tokens.append_all(&item.block.stmts);
1618 });
David Tolnay4c9be372016-10-06 00:47:37 -07001619 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001620 ImplItemKind::Type(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001621 item.vis.to_tokens(tokens);
1622 item.defaultness.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001623 item.type_token.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07001624 item.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001625 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001626 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001627 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001628 }
1629 ImplItemKind::Macro(ref mac) => {
1630 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001631 if !mac.is_braced() {
David Tolnay570695e2017-06-03 16:15:13 -07001632 // FIXME needs a span
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001633 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001634 }
1635 }
1636 }
1637 }
1638 }
1639
David Tolnay35902302016-10-06 01:11:08 -07001640 impl ToTokens for ForeignItem {
1641 fn to_tokens(&self, tokens: &mut Tokens) {
1642 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001643 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001644 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 ForeignItemKind::Fn(ref item) => {
David Tolnay570695e2017-06-03 16:15:13 -07001646 NamedDecl(&item.decl, self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001647 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001648 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001649 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001650 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001651 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001652 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001653 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001654 }
1655 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001656 self.semi_token.to_tokens(tokens);
1657 }
1658 }
1659
David Tolnay570695e2017-06-03 16:15:13 -07001660 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001661 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001662 self.constness.to_tokens(tokens);
1663 self.unsafety.to_tokens(tokens);
1664 self.abi.to_tokens(tokens);
1665 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001666 }
1667 }
1668
David Tolnay570695e2017-06-03 16:15:13 -07001669 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001670
1671 impl<'a> ToTokens for NamedDecl<'a> {
1672 fn to_tokens(&self, tokens: &mut Tokens) {
1673 self.0.fn_token.to_tokens(tokens);
1674 self.1.to_tokens(tokens);
1675 self.0.generics.to_tokens(tokens);
1676 self.0.paren_token.surround(tokens, |tokens| {
1677 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001678
1679 if self.0.variadic {
1680 if !self.0.inputs.empty_or_trailing() {
1681 tokens::Comma::default().to_tokens(tokens);
1682 }
Alex Crichton259ee532017-07-14 06:51:02 -07001683 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001684 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001685 });
1686 self.0.output.to_tokens(tokens);
1687 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001688 }
1689 }
1690
Alex Crichton62a0a592017-05-22 13:58:53 -07001691 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001692 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001693 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001694 self.lifetime.to_tokens(tokens);
1695 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001696 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001697 }
1698 }
1699
1700 impl ToTokens for ArgSelf {
1701 fn to_tokens(&self, tokens: &mut Tokens) {
1702 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001703 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001704 }
1705 }
1706
1707 impl ToTokens for ArgCaptured {
1708 fn to_tokens(&self, tokens: &mut Tokens) {
1709 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001710 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001711 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001712 }
1713 }
1714
David Tolnay42602292016-10-01 22:25:45 -07001715 impl ToTokens for Constness {
1716 fn to_tokens(&self, tokens: &mut Tokens) {
1717 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001718 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001719 Constness::NotConst => {
1720 // nothing
1721 }
David Tolnay42602292016-10-01 22:25:45 -07001722 }
1723 }
1724 }
1725
David Tolnay4c9be372016-10-06 00:47:37 -07001726 impl ToTokens for Defaultness {
1727 fn to_tokens(&self, tokens: &mut Tokens) {
1728 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001729 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001730 Defaultness::Final => {
1731 // nothing
1732 }
1733 }
1734 }
1735 }
1736
1737 impl ToTokens for ImplPolarity {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
1739 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001740 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001741 ImplPolarity::Positive => {
1742 // nothing
1743 }
1744 }
1745 }
1746 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001747}