blob: 3684fad7f3878222c959f8edeaa16b92504fcabf [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay771ecf42016-09-23 19:26:37 -07003/// An item
4///
5/// The name might be a dummy name in case of anonymous items
David Tolnayb79ee962016-09-04 09:39:20 -07006#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Item {
8 pub ident: Ident,
9 pub vis: Visibility,
10 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -070011 pub node: ItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -070012}
13
14#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070015pub enum ItemKind {
16 /// An`extern crate` item, with optional original crate name.
17 ///
18 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
19 ExternCrate(Option<Ident>),
20 /// A use declaration (`use` or `pub use`) item.
21 ///
22 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
23 Use(Box<ViewPath>),
24 /// A static item (`static` or `pub static`).
25 ///
26 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
27 Static(Box<Ty>, Mutability, Box<Expr>),
28 /// A constant item (`const` or `pub const`).
29 ///
30 /// E.g. `const FOO: i32 = 42;`
31 Const(Box<Ty>, Box<Expr>),
32 /// A function declaration (`fn` or `pub fn`).
33 ///
34 /// E.g. `fn foo(bar: usize) -> usize { .. }`
David Tolnay42602292016-10-01 22:25:45 -070035 Fn(Box<FnDecl>, Unsafety, Constness, Option<Abi>, Generics, Box<Block>),
David Tolnayf38cdf62016-09-23 19:07:09 -070036 /// A module declaration (`mod` or `pub mod`).
37 ///
38 /// E.g. `mod foo;` or `mod foo { .. }`
David Tolnay37d10332016-10-13 20:51:04 -070039 Mod(Option<Vec<Item>>),
David Tolnayf38cdf62016-09-23 19:07:09 -070040 /// An external module (`extern` or `pub extern`).
41 ///
42 /// E.g. `extern {}` or `extern "C" {}`
43 ForeignMod(ForeignMod),
44 /// A type alias (`type` or `pub type`).
45 ///
46 /// E.g. `type Foo = Bar<u8>;`
47 Ty(Box<Ty>, Generics),
48 /// An enum definition (`enum` or `pub enum`).
49 ///
50 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
51 Enum(Vec<Variant>, Generics),
52 /// A struct definition (`struct` or `pub struct`).
53 ///
54 /// E.g. `struct Foo<A> { x: A }`
55 Struct(VariantData, Generics),
56 /// A union definition (`union` or `pub union`).
57 ///
58 /// E.g. `union Foo<A, B> { x: A, y: B }`
59 Union(VariantData, Generics),
60 /// A Trait declaration (`trait` or `pub trait`).
61 ///
62 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
63 Trait(Unsafety, Generics, Vec<TyParamBound>, Vec<TraitItem>),
David Tolnay0aecb732016-10-03 23:03:50 -070064 /// Default trait implementation.
David Tolnayf38cdf62016-09-23 19:07:09 -070065 ///
66 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
67 DefaultImpl(Unsafety, Path),
68 /// An implementation.
69 ///
70 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
71 Impl(Unsafety,
David Tolnaydaaf7742016-10-03 11:11:43 -070072 ImplPolarity,
73 Generics,
74 Option<Path>, // (optional) trait this impl implements
75 Box<Ty>, // self
76 Vec<ImplItem>),
David Tolnayf38cdf62016-09-23 19:07:09 -070077 /// A macro invocation (which includes macro definition).
78 ///
79 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
80 Mac(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -070081}
82
David Tolnay453cfd12016-10-23 11:00:14 -070083impl From<MacroInput> for Item {
84 fn from(input: MacroInput) -> Item {
85 Item {
86 ident: input.ident,
87 vis: input.vis,
88 attrs: input.attrs,
89 node: match input.body {
90 Body::Enum(variants) => ItemKind::Enum(variants, input.generics),
91 Body::Struct(variant_data) => ItemKind::Struct(variant_data, input.generics),
92 },
93 }
94 }
95}
96
David Tolnayb79ee962016-09-04 09:39:20 -070097#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070098pub enum ViewPath {
99 /// `foo::bar::baz as quux`
100 ///
101 /// or just
102 ///
103 /// `foo::bar::baz` (with `as baz` implicitly on the right)
David Tolnay4a057422016-10-08 00:02:31 -0700104 Simple(Path, Option<Ident>),
David Tolnayf38cdf62016-09-23 19:07:09 -0700105
106 /// `foo::bar::*`
David Tolnayaed77b02016-09-23 20:50:31 -0700107 Glob(Path),
David Tolnayf38cdf62016-09-23 19:07:09 -0700108
David Tolnayaed77b02016-09-23 20:50:31 -0700109 /// `foo::bar::{a, b, c}`
David Tolnaydaaf7742016-10-03 11:11:43 -0700110 List(Path, Vec<PathListItem>),
David Tolnayf38cdf62016-09-23 19:07:09 -0700111}
112
113#[derive(Debug, Clone, Eq, PartialEq)]
114pub struct PathListItem {
115 pub name: Ident,
116 /// renamed in list, e.g. `use foo::{bar as baz};`
117 pub rename: Option<Ident>,
118}
119
120#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700121pub enum Constness {
122 Const,
123 NotConst,
124}
125
126#[derive(Debug, Copy, Clone, Eq, PartialEq)]
127pub enum Defaultness {
128 Default,
129 Final,
130}
131
David Tolnayf38cdf62016-09-23 19:07:09 -0700132/// Foreign module declaration.
133///
David Tolnay35902302016-10-06 01:11:08 -0700134/// E.g. `extern { .. }` or `extern "C" { .. }`
David Tolnayf38cdf62016-09-23 19:07:09 -0700135#[derive(Debug, Clone, Eq, PartialEq)]
136pub struct ForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700137 pub abi: Abi,
David Tolnayf38cdf62016-09-23 19:07:09 -0700138 pub items: Vec<ForeignItem>,
139}
140
141#[derive(Debug, Clone, Eq, PartialEq)]
142pub struct ForeignItem {
David Tolnayb79ee962016-09-04 09:39:20 -0700143 pub ident: Ident,
144 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700145 pub node: ForeignItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -0700146 pub vis: Visibility,
David Tolnayf38cdf62016-09-23 19:07:09 -0700147}
148
David Tolnay771ecf42016-09-23 19:26:37 -0700149/// An item within an `extern` block
David Tolnayf38cdf62016-09-23 19:07:09 -0700150#[derive(Debug, Clone, Eq, PartialEq)]
151pub enum ForeignItemKind {
152 /// A foreign function
153 Fn(Box<FnDecl>, Generics),
David Tolnay35902302016-10-06 01:11:08 -0700154 /// A foreign static item (`static ext: u8`)
155 Static(Box<Ty>, Mutability),
David Tolnayf38cdf62016-09-23 19:07:09 -0700156}
157
158/// Represents an item declaration within a trait declaration,
159/// possibly including a default implementation. A trait item is
160/// either required (meaning it doesn't have an implementation, just a
161/// signature) or provided (meaning it has a default implementation).
162#[derive(Debug, Clone, Eq, PartialEq)]
163pub struct TraitItem {
164 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700165 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700166 pub node: TraitItemKind,
167}
168
169#[derive(Debug, Clone, Eq, PartialEq)]
170pub enum TraitItemKind {
171 Const(Ty, Option<Expr>),
172 Method(MethodSig, Option<Block>),
173 Type(Vec<TyParamBound>, Option<Ty>),
174 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700175}
176
David Tolnay55337722016-09-11 12:58:56 -0700177#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700178pub enum ImplPolarity {
179 /// `impl Trait for Type`
180 Positive,
181 /// `impl !Trait for Type`
182 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700183}
184
David Tolnayf38cdf62016-09-23 19:07:09 -0700185#[derive(Debug, Clone, Eq, PartialEq)]
186pub struct ImplItem {
187 pub ident: Ident,
188 pub vis: Visibility,
189 pub defaultness: Defaultness,
190 pub attrs: Vec<Attribute>,
191 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700192}
193
David Tolnayf38cdf62016-09-23 19:07:09 -0700194#[derive(Debug, Clone, Eq, PartialEq)]
195pub enum ImplItemKind {
196 Const(Ty, Expr),
197 Method(MethodSig, Block),
198 Type(Ty),
199 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700200}
David Tolnayd5025812016-09-04 14:21:46 -0700201
David Tolnayf38cdf62016-09-23 19:07:09 -0700202/// Represents a method's signature in a trait declaration,
203/// or in an implementation.
204#[derive(Debug, Clone, Eq, PartialEq)]
205pub struct MethodSig {
206 pub unsafety: Unsafety,
207 pub constness: Constness,
David Tolnay0aecb732016-10-03 23:03:50 -0700208 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700209 pub decl: FnDecl,
210 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700211}
David Tolnayedf2b992016-09-23 20:43:45 -0700212
David Tolnay62f374c2016-10-02 13:37:00 -0700213/// Header (not the body) of a function declaration.
214///
215/// E.g. `fn foo(bar: baz)`
216#[derive(Debug, Clone, Eq, PartialEq)]
217pub struct FnDecl {
218 pub inputs: Vec<FnArg>,
219 pub output: FunctionRetTy,
David Tolnay292e6002016-10-29 22:03:51 -0700220 pub variadic: bool,
David Tolnay62f374c2016-10-02 13:37:00 -0700221}
222
223/// An argument in a function header.
224///
225/// E.g. `bar: usize` as in `fn foo(bar: usize)`
226#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayca085422016-10-04 00:12:38 -0700227pub enum FnArg {
228 SelfRef(Option<Lifetime>, Mutability),
229 SelfValue(Mutability),
230 Captured(Pat, Ty),
231 Ignored(Ty),
David Tolnay62f374c2016-10-02 13:37:00 -0700232}
233
David Tolnayedf2b992016-09-23 20:43:45 -0700234#[cfg(feature = "parsing")]
235pub mod parsing {
236 use super::*;
David Tolnay3b9783a2016-10-29 22:37:09 -0700237 use {Block, DelimToken, FunctionRetTy, Generics, Ident, Mac, Path, TokenTree, VariantData,
238 Visibility};
David Tolnay7b8009b2016-10-25 22:36:00 -0700239 use attr::parsing::{inner_attr, outer_attr};
David Tolnay2f9fa632016-10-03 22:08:48 -0700240 use data::parsing::{struct_like_body, visibility};
David Tolnay5859df12016-10-29 22:49:54 -0700241 use expr::parsing::{expr, pat, within_block};
David Tolnayca085422016-10-04 00:12:38 -0700242 use generics::parsing::{generics, lifetime, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700243 use ident::parsing::ident;
David Tolnay84aa0752016-10-02 23:01:13 -0700244 use mac::parsing::delimited;
David Tolnayedf2b992016-09-23 20:43:45 -0700245 use macro_input::{Body, MacroInput};
246 use macro_input::parsing::macro_input;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700247 use ty::parsing::{abi, mutability, path, ty, unsafety};
David Tolnayedf2b992016-09-23 20:43:45 -0700248
249 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700250 item_extern_crate
David Tolnay4a057422016-10-08 00:02:31 -0700251 |
252 item_use
David Tolnay47a877c2016-10-01 16:50:55 -0700253 |
254 item_static
255 |
256 item_const
David Tolnay42602292016-10-01 22:25:45 -0700257 |
258 item_fn
David Tolnay35902302016-10-06 01:11:08 -0700259 |
260 item_mod
261 |
262 item_foreign_mod
David Tolnay3cf52982016-10-01 17:11:37 -0700263 |
264 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700265 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700266 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700267 |
268 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700269 |
270 item_trait
David Tolnayf94e2362016-10-04 00:29:51 -0700271 |
272 item_default_impl
David Tolnay4c9be372016-10-06 00:47:37 -0700273 |
274 item_impl
David Tolnay84aa0752016-10-02 23:01:13 -0700275 |
276 item_mac
277 ));
278
David Tolnay453cfd12016-10-23 11:00:14 -0700279 named!(pub items -> Vec<Item>, many0!(item));
280
David Tolnay84aa0752016-10-02 23:01:13 -0700281 named!(item_mac -> Item, do_parse!(
282 attrs: many0!(outer_attr) >>
283 path: ident >>
284 punct!("!") >>
285 name: option!(ident) >>
286 body: delimited >>
David Tolnay1a8b3522016-10-08 22:27:00 -0700287 cond!(match body.delim {
288 DelimToken::Paren | DelimToken::Bracket => true,
289 DelimToken::Brace => false,
290 }, punct!(";")) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700291 (Item {
292 ident: name.unwrap_or_else(|| Ident::new("")),
293 vis: Visibility::Inherited,
294 attrs: attrs,
295 node: ItemKind::Mac(Mac {
296 path: path.into(),
297 tts: vec![TokenTree::Delimited(body)],
298 }),
299 })
David Tolnayedf2b992016-09-23 20:43:45 -0700300 ));
301
David Tolnaya96a3fa2016-09-24 07:17:42 -0700302 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700303 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700304 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700305 keyword!("extern") >>
306 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700307 id: ident >>
308 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700309 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700310 ident
311 )) >>
312 punct!(";") >>
313 ({
314 let (name, original_name) = match rename {
315 Some(rename) => (rename, Some(id)),
316 None => (id, None),
317 };
318 Item {
319 ident: name,
320 vis: vis,
321 attrs: attrs,
322 node: ItemKind::ExternCrate(original_name),
323 }
324 })
325 ));
326
David Tolnay4a057422016-10-08 00:02:31 -0700327 named!(item_use -> Item, do_parse!(
328 attrs: many0!(outer_attr) >>
329 vis: visibility >>
330 keyword!("use") >>
331 what: view_path >>
332 punct!(";") >>
333 (Item {
334 ident: "".into(),
335 vis: vis,
336 attrs: attrs,
337 node: ItemKind::Use(Box::new(what)),
338 })
339 ));
340
341 named!(view_path -> ViewPath, alt!(
342 view_path_glob
343 |
344 view_path_list
345 |
346 view_path_list_root
347 |
348 view_path_simple // must be last
349 ));
350
351
352 named!(view_path_simple -> ViewPath, do_parse!(
353 path: path >>
354 rename: option!(preceded!(keyword!("as"), ident)) >>
355 (ViewPath::Simple(path, rename))
356 ));
357
358 named!(view_path_glob -> ViewPath, do_parse!(
359 path: path >>
360 punct!("::") >>
361 punct!("*") >>
362 (ViewPath::Glob(path))
363 ));
364
365 named!(view_path_list -> ViewPath, do_parse!(
366 path: path >>
367 punct!("::") >>
368 punct!("{") >>
369 items: separated_nonempty_list!(punct!(","), path_list_item) >>
David Tolnaybcd4e362016-10-29 22:58:48 -0700370 option!(punct!(",")) >>
David Tolnay4a057422016-10-08 00:02:31 -0700371 punct!("}") >>
372 (ViewPath::List(path, items))
373 ));
374
375 named!(view_path_list_root -> ViewPath, do_parse!(
376 global: option!(punct!("::")) >>
377 punct!("{") >>
378 items: separated_nonempty_list!(punct!(","), path_list_item) >>
David Tolnaybcd4e362016-10-29 22:58:48 -0700379 option!(punct!(",")) >>
David Tolnay4a057422016-10-08 00:02:31 -0700380 punct!("}") >>
381 (ViewPath::List(Path {
382 global: global.is_some(),
383 segments: Vec::new(),
384 }, items))
385 ));
386
387 named!(path_list_item -> PathListItem, do_parse!(
David Tolnaye6e42542016-10-24 22:37:11 -0700388 name: alt!(
389 ident
390 |
391 map!(keyword!("self"), Into::into)
392 ) >>
David Tolnay4a057422016-10-08 00:02:31 -0700393 rename: option!(preceded!(keyword!("as"), ident)) >>
394 (PathListItem {
395 name: name,
396 rename: rename,
397 })
398 ));
399
David Tolnay47a877c2016-10-01 16:50:55 -0700400 named!(item_static -> Item, do_parse!(
401 attrs: many0!(outer_attr) >>
402 vis: visibility >>
403 keyword!("static") >>
404 mutability: mutability >>
405 id: ident >>
406 punct!(":") >>
407 ty: ty >>
408 punct!("=") >>
409 value: expr >>
410 punct!(";") >>
411 (Item {
412 ident: id,
413 vis: vis,
414 attrs: attrs,
415 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
416 })
417 ));
418
419 named!(item_const -> Item, do_parse!(
420 attrs: many0!(outer_attr) >>
421 vis: visibility >>
422 keyword!("const") >>
423 id: ident >>
424 punct!(":") >>
425 ty: ty >>
426 punct!("=") >>
427 value: expr >>
428 punct!(";") >>
429 (Item {
430 ident: id,
431 vis: vis,
432 attrs: attrs,
433 node: ItemKind::Const(Box::new(ty), Box::new(value)),
434 })
435 ));
436
David Tolnay42602292016-10-01 22:25:45 -0700437 named!(item_fn -> Item, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700438 outer_attrs: many0!(outer_attr) >>
David Tolnay42602292016-10-01 22:25:45 -0700439 vis: visibility >>
440 constness: constness >>
441 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700442 abi: option!(abi) >>
David Tolnay42602292016-10-01 22:25:45 -0700443 keyword!("fn") >>
444 name: ident >>
445 generics: generics >>
446 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700447 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700448 punct!(")") >>
449 ret: option!(preceded!(punct!("->"), ty)) >>
450 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700451 punct!("{") >>
452 inner_attrs: many0!(inner_attr) >>
453 stmts: within_block >>
454 punct!("}") >>
David Tolnay42602292016-10-01 22:25:45 -0700455 (Item {
456 ident: name,
457 vis: vis,
David Tolnay3b9783a2016-10-29 22:37:09 -0700458 attrs: {
459 let mut attrs = outer_attrs;
460 attrs.extend(inner_attrs);
461 attrs
462 },
David Tolnay42602292016-10-01 22:25:45 -0700463 node: ItemKind::Fn(
464 Box::new(FnDecl {
465 inputs: inputs,
466 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700467 variadic: false,
David Tolnay42602292016-10-01 22:25:45 -0700468 }),
469 unsafety,
470 constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700471 abi,
David Tolnay42602292016-10-01 22:25:45 -0700472 Generics {
473 where_clause: where_clause,
474 .. generics
475 },
David Tolnay3b9783a2016-10-29 22:37:09 -0700476 Box::new(Block {
477 stmts: stmts,
478 }),
David Tolnay42602292016-10-01 22:25:45 -0700479 ),
480 })
481 ));
482
David Tolnayca085422016-10-04 00:12:38 -0700483 named!(fn_arg -> FnArg, alt!(
484 do_parse!(
485 punct!("&") >>
486 lt: option!(lifetime) >>
487 mutability: mutability >>
488 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700489 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700490 (FnArg::SelfRef(lt, mutability))
491 )
492 |
493 do_parse!(
494 mutability: mutability >>
495 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700496 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700497 (FnArg::SelfValue(mutability))
498 )
499 |
500 do_parse!(
501 pat: pat >>
502 punct!(":") >>
503 ty: ty >>
504 (FnArg::Captured(pat, ty))
505 )
506 |
507 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700508 ));
509
David Tolnay35902302016-10-06 01:11:08 -0700510 named!(item_mod -> Item, do_parse!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700511 outer_attrs: many0!(outer_attr) >>
David Tolnay35902302016-10-06 01:11:08 -0700512 vis: visibility >>
513 keyword!("mod") >>
514 id: ident >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700515 content: alt!(
David Tolnay37d10332016-10-13 20:51:04 -0700516 punct!(";") => { |_| None }
517 |
David Tolnay7b8009b2016-10-25 22:36:00 -0700518 delimited!(
519 punct!("{"),
520 tuple!(
521 many0!(inner_attr),
522 items
523 ),
524 punct!("}")
525 ) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700526 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700527 (match content {
528 Some((inner_attrs, items)) => Item {
529 ident: id,
530 vis: vis,
531 attrs: {
532 let mut attrs = outer_attrs;
533 attrs.extend(inner_attrs);
534 attrs
535 },
536 node: ItemKind::Mod(Some(items)),
537 },
538 None => Item {
539 ident: id,
540 vis: vis,
541 attrs: outer_attrs,
542 node: ItemKind::Mod(None),
543 },
David Tolnay35902302016-10-06 01:11:08 -0700544 })
545 ));
546
547 named!(item_foreign_mod -> Item, do_parse!(
548 attrs: many0!(outer_attr) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700549 abi: abi >>
David Tolnay35902302016-10-06 01:11:08 -0700550 punct!("{") >>
551 items: many0!(foreign_item) >>
552 punct!("}") >>
553 (Item {
554 ident: "".into(),
555 vis: Visibility::Inherited,
556 attrs: attrs,
557 node: ItemKind::ForeignMod(ForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700558 abi: abi,
David Tolnay35902302016-10-06 01:11:08 -0700559 items: items,
560 }),
561 })
562 ));
563
564 named!(foreign_item -> ForeignItem, alt!(
565 foreign_fn
566 |
567 foreign_static
568 ));
569
570 named!(foreign_fn -> ForeignItem, do_parse!(
571 attrs: many0!(outer_attr) >>
572 vis: visibility >>
573 keyword!("fn") >>
574 name: ident >>
575 generics: generics >>
576 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700577 inputs: separated_list!(punct!(","), fn_arg) >>
578 trailing_comma: option!(punct!(",")) >>
579 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay35902302016-10-06 01:11:08 -0700580 punct!(")") >>
581 ret: option!(preceded!(punct!("->"), ty)) >>
582 where_clause: where_clause >>
583 punct!(";") >>
584 (ForeignItem {
585 ident: name,
586 attrs: attrs,
587 node: ForeignItemKind::Fn(
588 Box::new(FnDecl {
589 inputs: inputs,
590 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700591 variadic: variadic.is_some(),
David Tolnay35902302016-10-06 01:11:08 -0700592 }),
593 Generics {
594 where_clause: where_clause,
595 .. generics
596 },
597 ),
598 vis: vis,
599 })
600 ));
601
602 named!(foreign_static -> ForeignItem, do_parse!(
603 attrs: many0!(outer_attr) >>
604 vis: visibility >>
605 keyword!("static") >>
606 mutability: mutability >>
607 id: ident >>
608 punct!(":") >>
609 ty: ty >>
610 punct!(";") >>
611 (ForeignItem {
612 ident: id,
613 attrs: attrs,
614 node: ForeignItemKind::Static(Box::new(ty), mutability),
615 vis: vis,
616 })
617 ));
618
David Tolnay3cf52982016-10-01 17:11:37 -0700619 named!(item_ty -> Item, do_parse!(
620 attrs: many0!(outer_attr) >>
621 vis: visibility >>
622 keyword!("type") >>
623 id: ident >>
624 generics: generics >>
625 punct!("=") >>
626 ty: ty >>
627 punct!(";") >>
628 (Item {
629 ident: id,
630 vis: vis,
631 attrs: attrs,
632 node: ItemKind::Ty(Box::new(ty), generics),
633 })
634 ));
635
David Tolnaya96a3fa2016-09-24 07:17:42 -0700636 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700637 macro_input,
638 |def: MacroInput| Item {
639 ident: def.ident,
640 vis: def.vis,
641 attrs: def.attrs,
642 node: match def.body {
643 Body::Enum(variants) => {
644 ItemKind::Enum(variants, def.generics)
645 }
646 Body::Struct(variant_data) => {
647 ItemKind::Struct(variant_data, def.generics)
648 }
649 }
650 }
651 ));
David Tolnay42602292016-10-01 22:25:45 -0700652
David Tolnay2f9fa632016-10-03 22:08:48 -0700653 named!(item_union -> Item, do_parse!(
654 attrs: many0!(outer_attr) >>
655 vis: visibility >>
656 keyword!("union") >>
657 id: ident >>
658 generics: generics >>
659 where_clause: where_clause >>
660 fields: struct_like_body >>
661 (Item {
662 ident: id,
663 vis: vis,
664 attrs: attrs,
665 node: ItemKind::Union(
666 VariantData::Struct(fields),
667 Generics {
668 where_clause: where_clause,
669 .. generics
670 },
671 ),
672 })
673 ));
674
David Tolnay0aecb732016-10-03 23:03:50 -0700675 named!(item_trait -> Item, do_parse!(
676 attrs: many0!(outer_attr) >>
677 vis: visibility >>
678 unsafety: unsafety >>
679 keyword!("trait") >>
680 id: ident >>
681 generics: generics >>
682 bounds: opt_vec!(preceded!(
683 punct!(":"),
684 separated_nonempty_list!(punct!("+"), ty_param_bound)
685 )) >>
686 where_clause: where_clause >>
687 punct!("{") >>
688 body: many0!(trait_item) >>
689 punct!("}") >>
690 (Item {
691 ident: id,
692 vis: vis,
693 attrs: attrs,
694 node: ItemKind::Trait(
695 unsafety,
696 Generics {
697 where_clause: where_clause,
698 .. generics
699 },
700 bounds,
701 body,
702 ),
703 })
704 ));
705
David Tolnayf94e2362016-10-04 00:29:51 -0700706 named!(item_default_impl -> Item, do_parse!(
707 attrs: many0!(outer_attr) >>
708 unsafety: unsafety >>
709 keyword!("impl") >>
710 path: path >>
711 keyword!("for") >>
712 punct!("..") >>
713 punct!("{") >>
714 punct!("}") >>
715 (Item {
716 ident: "".into(),
717 vis: Visibility::Inherited,
718 attrs: attrs,
719 node: ItemKind::DefaultImpl(unsafety, path),
720 })
721 ));
722
David Tolnay0aecb732016-10-03 23:03:50 -0700723 named!(trait_item -> TraitItem, alt!(
724 trait_item_const
725 |
726 trait_item_method
727 |
728 trait_item_type
729 |
730 trait_item_mac
731 ));
732
733 named!(trait_item_const -> TraitItem, do_parse!(
734 attrs: many0!(outer_attr) >>
735 keyword!("const") >>
736 id: ident >>
737 punct!(":") >>
738 ty: ty >>
739 value: option!(preceded!(punct!("="), expr)) >>
740 punct!(";") >>
741 (TraitItem {
742 ident: id,
743 attrs: attrs,
744 node: TraitItemKind::Const(ty, value),
745 })
746 ));
747
748 named!(trait_item_method -> TraitItem, do_parse!(
David Tolnay5859df12016-10-29 22:49:54 -0700749 outer_attrs: many0!(outer_attr) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700750 constness: constness >>
751 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700752 abi: option!(abi) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700753 keyword!("fn") >>
754 name: ident >>
755 generics: generics >>
756 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700757 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700758 punct!(")") >>
759 ret: option!(preceded!(punct!("->"), ty)) >>
760 where_clause: where_clause >>
David Tolnay5859df12016-10-29 22:49:54 -0700761 body: option!(delimited!(
762 punct!("{"),
763 tuple!(many0!(inner_attr), within_block),
764 punct!("}")
765 )) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700766 cond!(body.is_none(), punct!(";")) >>
David Tolnay5859df12016-10-29 22:49:54 -0700767 ({
768 let (inner_attrs, stmts) = match body {
769 Some((inner_attrs, stmts)) => (inner_attrs, Some(stmts)),
770 None => (Vec::new(), None),
771 };
772 TraitItem {
773 ident: name,
774 attrs: {
775 let mut attrs = outer_attrs;
776 attrs.extend(inner_attrs);
777 attrs
David Tolnay0aecb732016-10-03 23:03:50 -0700778 },
David Tolnay5859df12016-10-29 22:49:54 -0700779 node: TraitItemKind::Method(
780 MethodSig {
781 unsafety: unsafety,
782 constness: constness,
783 abi: abi,
784 decl: FnDecl {
785 inputs: inputs,
786 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
787 variadic: false,
788 },
789 generics: Generics {
790 where_clause: where_clause,
791 .. generics
792 },
793 },
794 stmts.map(|stmts| Block { stmts: stmts }),
795 ),
796 }
David Tolnay0aecb732016-10-03 23:03:50 -0700797 })
798 ));
799
800 named!(trait_item_type -> TraitItem, do_parse!(
801 attrs: many0!(outer_attr) >>
802 keyword!("type") >>
803 id: ident >>
804 bounds: opt_vec!(preceded!(
805 punct!(":"),
806 separated_nonempty_list!(punct!("+"), ty_param_bound)
807 )) >>
808 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700809 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700810 (TraitItem {
811 ident: id,
812 attrs: attrs,
813 node: TraitItemKind::Type(bounds, default),
814 })
815 ));
816
817 named!(trait_item_mac -> TraitItem, do_parse!(
818 attrs: many0!(outer_attr) >>
819 id: ident >>
820 punct!("!") >>
821 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700822 cond!(match body.delim {
823 DelimToken::Paren | DelimToken::Bracket => true,
824 DelimToken::Brace => false,
825 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700826 (TraitItem {
827 ident: id.clone(),
828 attrs: attrs,
829 node: TraitItemKind::Macro(Mac {
830 path: id.into(),
831 tts: vec![TokenTree::Delimited(body)],
832 }),
833 })
834 ));
835
David Tolnay4c9be372016-10-06 00:47:37 -0700836 named!(item_impl -> Item, do_parse!(
837 attrs: many0!(outer_attr) >>
838 unsafety: unsafety >>
839 keyword!("impl") >>
840 generics: generics >>
841 polarity_path: alt!(
842 do_parse!(
843 polarity: impl_polarity >>
844 path: path >>
845 keyword!("for") >>
846 ((polarity, Some(path)))
847 )
848 |
849 epsilon!() => { |_| (ImplPolarity::Positive, None) }
850 ) >>
851 self_ty: ty >>
852 where_clause: where_clause >>
853 punct!("{") >>
854 body: many0!(impl_item) >>
855 punct!("}") >>
856 (Item {
857 ident: "".into(),
858 vis: Visibility::Inherited,
859 attrs: attrs,
860 node: ItemKind::Impl(
861 unsafety,
862 polarity_path.0,
863 Generics {
864 where_clause: where_clause,
865 .. generics
866 },
867 polarity_path.1,
868 Box::new(self_ty),
869 body,
870 ),
871 })
872 ));
873
874 named!(impl_item -> ImplItem, alt!(
875 impl_item_const
876 |
877 impl_item_method
878 |
879 impl_item_type
880 |
881 impl_item_macro
882 ));
883
884 named!(impl_item_const -> ImplItem, do_parse!(
885 attrs: many0!(outer_attr) >>
886 vis: visibility >>
887 defaultness: defaultness >>
888 keyword!("const") >>
889 id: ident >>
890 punct!(":") >>
891 ty: ty >>
892 punct!("=") >>
893 value: expr >>
894 punct!(";") >>
895 (ImplItem {
896 ident: id,
897 vis: vis,
898 defaultness: defaultness,
899 attrs: attrs,
900 node: ImplItemKind::Const(ty, value),
901 })
902 ));
903
904 named!(impl_item_method -> ImplItem, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700905 outer_attrs: many0!(outer_attr) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700906 vis: visibility >>
907 defaultness: defaultness >>
908 constness: constness >>
909 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700910 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700911 keyword!("fn") >>
912 name: ident >>
913 generics: generics >>
914 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700915 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700916 punct!(")") >>
917 ret: option!(preceded!(punct!("->"), ty)) >>
918 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700919 punct!("{") >>
920 inner_attrs: many0!(inner_attr) >>
921 stmts: within_block >>
922 punct!("}") >>
David Tolnay4c9be372016-10-06 00:47:37 -0700923 (ImplItem {
924 ident: name,
925 vis: vis,
926 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -0700927 attrs: {
928 let mut attrs = outer_attrs;
929 attrs.extend(inner_attrs);
930 attrs
931 },
David Tolnay4c9be372016-10-06 00:47:37 -0700932 node: ImplItemKind::Method(
933 MethodSig {
934 unsafety: unsafety,
935 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700936 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -0700937 decl: FnDecl {
938 inputs: inputs,
939 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700940 variadic: false,
David Tolnay4c9be372016-10-06 00:47:37 -0700941 },
942 generics: Generics {
943 where_clause: where_clause,
944 .. generics
945 },
946 },
David Tolnay3b9783a2016-10-29 22:37:09 -0700947 Block {
948 stmts: stmts,
949 },
David Tolnay4c9be372016-10-06 00:47:37 -0700950 ),
951 })
952 ));
953
954 named!(impl_item_type -> ImplItem, do_parse!(
955 attrs: many0!(outer_attr) >>
956 vis: visibility >>
957 defaultness: defaultness >>
958 keyword!("type") >>
959 id: ident >>
960 punct!("=") >>
961 ty: ty >>
962 punct!(";") >>
963 (ImplItem {
964 ident: id,
965 vis: vis,
966 defaultness: defaultness,
967 attrs: attrs,
968 node: ImplItemKind::Type(ty),
969 })
970 ));
971
972 named!(impl_item_macro -> ImplItem, do_parse!(
973 attrs: many0!(outer_attr) >>
974 id: ident >>
975 punct!("!") >>
976 body: delimited >>
977 cond!(match body.delim {
978 DelimToken::Paren | DelimToken::Bracket => true,
979 DelimToken::Brace => false,
980 }, punct!(";")) >>
981 (ImplItem {
982 ident: id.clone(),
983 vis: Visibility::Inherited,
984 defaultness: Defaultness::Final,
985 attrs: attrs,
986 node: ImplItemKind::Macro(Mac {
987 path: id.into(),
988 tts: vec![TokenTree::Delimited(body)],
989 }),
990 })
991 ));
992
993 named!(impl_polarity -> ImplPolarity, alt!(
994 punct!("!") => { |_| ImplPolarity::Negative }
995 |
996 epsilon!() => { |_| ImplPolarity::Positive }
997 ));
998
David Tolnay42602292016-10-01 22:25:45 -0700999 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -07001000 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -07001001 |
1002 epsilon!() => { |_| Constness::NotConst }
1003 ));
1004
David Tolnay4c9be372016-10-06 00:47:37 -07001005 named!(defaultness -> Defaultness, alt!(
1006 keyword!("default") => { |_| Defaultness::Default }
1007 |
1008 epsilon!() => { |_| Defaultness::Final }
1009 ));
David Tolnayedf2b992016-09-23 20:43:45 -07001010}
David Tolnay4a51dc72016-10-01 00:40:31 -07001011
1012#[cfg(feature = "printing")]
1013mod printing {
1014 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -07001015 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -07001016 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001017 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001018 use quote::{Tokens, ToTokens};
1019
1020 impl ToTokens for Item {
1021 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001022 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001023 match self.node {
1024 ItemKind::ExternCrate(ref original) => {
David Tolnaycbd9f7d2016-10-30 00:26:29 -07001025 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001026 tokens.append("extern");
1027 tokens.append("crate");
1028 if let Some(ref original) = *original {
1029 original.to_tokens(tokens);
1030 tokens.append("as");
1031 }
1032 self.ident.to_tokens(tokens);
1033 tokens.append(";");
1034 }
David Tolnay4a057422016-10-08 00:02:31 -07001035 ItemKind::Use(ref view_path) => {
1036 self.vis.to_tokens(tokens);
1037 tokens.append("use");
1038 view_path.to_tokens(tokens);
1039 tokens.append(";");
1040 }
David Tolnay47a877c2016-10-01 16:50:55 -07001041 ItemKind::Static(ref ty, ref mutability, ref expr) => {
1042 self.vis.to_tokens(tokens);
1043 tokens.append("static");
1044 mutability.to_tokens(tokens);
1045 self.ident.to_tokens(tokens);
1046 tokens.append(":");
1047 ty.to_tokens(tokens);
1048 tokens.append("=");
1049 expr.to_tokens(tokens);
1050 tokens.append(";");
1051 }
1052 ItemKind::Const(ref ty, ref expr) => {
1053 self.vis.to_tokens(tokens);
1054 tokens.append("const");
1055 self.ident.to_tokens(tokens);
1056 tokens.append(":");
1057 ty.to_tokens(tokens);
1058 tokens.append("=");
1059 expr.to_tokens(tokens);
1060 tokens.append(";");
1061 }
David Tolnay42602292016-10-01 22:25:45 -07001062 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
1063 self.vis.to_tokens(tokens);
1064 constness.to_tokens(tokens);
1065 unsafety.to_tokens(tokens);
1066 abi.to_tokens(tokens);
1067 tokens.append("fn");
1068 self.ident.to_tokens(tokens);
1069 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001070 tokens.append("(");
1071 tokens.append_separated(&decl.inputs, ",");
1072 tokens.append(")");
1073 if let FunctionRetTy::Ty(ref ty) = decl.output {
1074 tokens.append("->");
1075 ty.to_tokens(tokens);
1076 }
David Tolnay42602292016-10-01 22:25:45 -07001077 generics.where_clause.to_tokens(tokens);
David Tolnay3b9783a2016-10-29 22:37:09 -07001078 tokens.append("{");
1079 tokens.append_all(self.attrs.inner());
1080 tokens.append_all(&block.stmts);
1081 tokens.append("}");
David Tolnay42602292016-10-01 22:25:45 -07001082 }
David Tolnay35902302016-10-06 01:11:08 -07001083 ItemKind::Mod(ref items) => {
1084 self.vis.to_tokens(tokens);
1085 tokens.append("mod");
1086 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001087 match *items {
1088 Some(ref items) => {
1089 tokens.append("{");
David Tolnay7b8009b2016-10-25 22:36:00 -07001090 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001091 tokens.append_all(items);
1092 tokens.append("}");
1093 }
1094 None => tokens.append(";"),
1095 }
David Tolnay35902302016-10-06 01:11:08 -07001096 }
1097 ItemKind::ForeignMod(ref foreign_mod) => {
1098 self.vis.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001099 foreign_mod.abi.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001100 tokens.append("{");
1101 tokens.append_all(&foreign_mod.items);
1102 tokens.append("}");
1103 }
David Tolnay3cf52982016-10-01 17:11:37 -07001104 ItemKind::Ty(ref ty, ref generics) => {
1105 self.vis.to_tokens(tokens);
1106 tokens.append("type");
1107 self.ident.to_tokens(tokens);
1108 generics.to_tokens(tokens);
1109 tokens.append("=");
1110 ty.to_tokens(tokens);
1111 tokens.append(";");
1112 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001113 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001114 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001115 tokens.append("enum");
1116 self.ident.to_tokens(tokens);
1117 generics.to_tokens(tokens);
1118 generics.where_clause.to_tokens(tokens);
1119 tokens.append("{");
1120 for variant in variants {
1121 variant.to_tokens(tokens);
1122 tokens.append(",");
1123 }
1124 tokens.append("}");
1125 }
1126 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001127 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001128 tokens.append("struct");
1129 self.ident.to_tokens(tokens);
1130 generics.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001131 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001132 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -07001133 generics.where_clause.to_tokens(tokens);
1134 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001135 // no semicolon
1136 }
David Tolnay28c1db62016-10-27 22:48:18 -07001137 VariantData::Tuple(_) => {
1138 variant_data.to_tokens(tokens);
1139 generics.where_clause.to_tokens(tokens);
1140 tokens.append(";");
1141 }
1142 VariantData::Unit => {
1143 generics.where_clause.to_tokens(tokens);
1144 tokens.append(";");
1145 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001146 }
1147 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001148 ItemKind::Union(ref variant_data, ref generics) => {
1149 self.vis.to_tokens(tokens);
1150 tokens.append("union");
1151 self.ident.to_tokens(tokens);
1152 generics.to_tokens(tokens);
1153 generics.where_clause.to_tokens(tokens);
1154 variant_data.to_tokens(tokens);
1155 }
David Tolnayca085422016-10-04 00:12:38 -07001156 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1157 self.vis.to_tokens(tokens);
1158 unsafety.to_tokens(tokens);
1159 tokens.append("trait");
1160 self.ident.to_tokens(tokens);
David Tolnaye4606332016-10-25 21:57:41 -07001161 generics.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001162 if !bound.is_empty() {
1163 tokens.append(":");
1164 tokens.append_separated(bound, "+");
1165 }
David Tolnayca085422016-10-04 00:12:38 -07001166 generics.where_clause.to_tokens(tokens);
1167 tokens.append("{");
1168 tokens.append_all(items);
1169 tokens.append("}");
1170 }
David Tolnayf94e2362016-10-04 00:29:51 -07001171 ItemKind::DefaultImpl(unsafety, ref path) => {
1172 unsafety.to_tokens(tokens);
1173 tokens.append("impl");
1174 path.to_tokens(tokens);
1175 tokens.append("for");
1176 tokens.append("..");
1177 tokens.append("{");
1178 tokens.append("}");
1179 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001180 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001181 unsafety.to_tokens(tokens);
1182 tokens.append("impl");
1183 generics.to_tokens(tokens);
1184 if let Some(ref path) = *path {
1185 polarity.to_tokens(tokens);
1186 path.to_tokens(tokens);
1187 tokens.append("for");
1188 }
1189 ty.to_tokens(tokens);
1190 generics.where_clause.to_tokens(tokens);
1191 tokens.append("{");
1192 tokens.append_all(items);
1193 tokens.append("}");
1194 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001195 ItemKind::Mac(ref mac) => {
1196 mac.path.to_tokens(tokens);
1197 tokens.append("!");
1198 self.ident.to_tokens(tokens);
1199 for tt in &mac.tts {
1200 tt.to_tokens(tokens);
1201 }
1202 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001203 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1204 // no semicolon
1205 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001206 _ => tokens.append(";"),
1207 }
1208 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001209 }
1210 }
1211 }
David Tolnay42602292016-10-01 22:25:45 -07001212
David Tolnay4a057422016-10-08 00:02:31 -07001213 impl ToTokens for ViewPath {
1214 fn to_tokens(&self, tokens: &mut Tokens) {
1215 match *self {
1216 ViewPath::Simple(ref path, ref rename) => {
1217 path.to_tokens(tokens);
1218 if let Some(ref rename) = *rename {
1219 tokens.append("as");
1220 rename.to_tokens(tokens);
1221 }
1222 }
1223 ViewPath::Glob(ref path) => {
1224 path.to_tokens(tokens);
1225 tokens.append("::");
1226 tokens.append("*");
1227 }
1228 ViewPath::List(ref path, ref items) => {
1229 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001230 if path.global || !path.segments.is_empty() {
1231 tokens.append("::");
1232 }
David Tolnay4a057422016-10-08 00:02:31 -07001233 tokens.append("{");
1234 tokens.append_separated(items, ",");
1235 tokens.append("}");
1236 }
1237 }
1238 }
1239 }
1240
1241 impl ToTokens for PathListItem {
1242 fn to_tokens(&self, tokens: &mut Tokens) {
1243 self.name.to_tokens(tokens);
1244 if let Some(ref rename) = self.rename {
1245 tokens.append("as");
1246 rename.to_tokens(tokens);
1247 }
1248 }
1249 }
1250
David Tolnayca085422016-10-04 00:12:38 -07001251 impl ToTokens for TraitItem {
1252 fn to_tokens(&self, tokens: &mut Tokens) {
1253 tokens.append_all(self.attrs.outer());
1254 match self.node {
1255 TraitItemKind::Const(ref ty, ref expr) => {
1256 tokens.append("const");
1257 self.ident.to_tokens(tokens);
1258 tokens.append(":");
1259 ty.to_tokens(tokens);
1260 if let Some(ref expr) = *expr {
1261 tokens.append("=");
1262 expr.to_tokens(tokens);
1263 }
1264 tokens.append(";");
1265 }
1266 TraitItemKind::Method(ref sig, ref block) => {
David Tolnayb31d3f02016-10-25 21:15:13 -07001267 sig.constness.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001268 sig.unsafety.to_tokens(tokens);
1269 sig.abi.to_tokens(tokens);
1270 tokens.append("fn");
1271 self.ident.to_tokens(tokens);
1272 sig.generics.to_tokens(tokens);
1273 tokens.append("(");
1274 tokens.append_separated(&sig.decl.inputs, ",");
1275 tokens.append(")");
1276 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1277 tokens.append("->");
1278 ty.to_tokens(tokens);
1279 }
1280 sig.generics.where_clause.to_tokens(tokens);
1281 match *block {
David Tolnay3b9783a2016-10-29 22:37:09 -07001282 Some(ref block) => {
1283 tokens.append("{");
1284 tokens.append_all(self.attrs.inner());
1285 tokens.append_all(&block.stmts);
1286 tokens.append("}");
1287 }
David Tolnayca085422016-10-04 00:12:38 -07001288 None => tokens.append(";"),
1289 }
1290 }
1291 TraitItemKind::Type(ref bound, ref default) => {
1292 tokens.append("type");
1293 self.ident.to_tokens(tokens);
1294 if !bound.is_empty() {
1295 tokens.append(":");
1296 tokens.append_separated(bound, "+");
1297 }
1298 if let Some(ref default) = *default {
1299 tokens.append("=");
1300 default.to_tokens(tokens);
1301 }
1302 tokens.append(";");
1303 }
1304 TraitItemKind::Macro(ref mac) => {
1305 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001306 match mac.tts.last() {
1307 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1308 // no semicolon
1309 }
1310 _ => tokens.append(";"),
1311 }
David Tolnayca085422016-10-04 00:12:38 -07001312 }
1313 }
1314 }
1315 }
1316
David Tolnay4c9be372016-10-06 00:47:37 -07001317 impl ToTokens for ImplItem {
1318 fn to_tokens(&self, tokens: &mut Tokens) {
1319 tokens.append_all(self.attrs.outer());
1320 match self.node {
1321 ImplItemKind::Const(ref ty, ref expr) => {
1322 self.vis.to_tokens(tokens);
1323 self.defaultness.to_tokens(tokens);
1324 tokens.append("const");
1325 self.ident.to_tokens(tokens);
1326 tokens.append(":");
1327 ty.to_tokens(tokens);
1328 tokens.append("=");
1329 expr.to_tokens(tokens);
1330 tokens.append(";");
1331 }
1332 ImplItemKind::Method(ref sig, ref block) => {
1333 self.vis.to_tokens(tokens);
1334 self.defaultness.to_tokens(tokens);
David Tolnayb31d3f02016-10-25 21:15:13 -07001335 sig.constness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001336 sig.unsafety.to_tokens(tokens);
1337 sig.abi.to_tokens(tokens);
1338 tokens.append("fn");
1339 self.ident.to_tokens(tokens);
1340 sig.generics.to_tokens(tokens);
1341 tokens.append("(");
1342 tokens.append_separated(&sig.decl.inputs, ",");
1343 tokens.append(")");
1344 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1345 tokens.append("->");
1346 ty.to_tokens(tokens);
1347 }
1348 sig.generics.where_clause.to_tokens(tokens);
David Tolnay5859df12016-10-29 22:49:54 -07001349 tokens.append("{");
1350 tokens.append_all(self.attrs.inner());
1351 tokens.append_all(&block.stmts);
1352 tokens.append("}");
David Tolnay4c9be372016-10-06 00:47:37 -07001353 }
1354 ImplItemKind::Type(ref ty) => {
1355 self.vis.to_tokens(tokens);
1356 self.defaultness.to_tokens(tokens);
1357 tokens.append("type");
1358 self.ident.to_tokens(tokens);
1359 tokens.append("=");
1360 ty.to_tokens(tokens);
1361 tokens.append(";");
1362 }
1363 ImplItemKind::Macro(ref mac) => {
1364 mac.to_tokens(tokens);
1365 match mac.tts.last() {
1366 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1367 // no semicolon
1368 }
1369 _ => tokens.append(";"),
1370 }
1371 }
1372 }
1373 }
1374 }
1375
David Tolnay35902302016-10-06 01:11:08 -07001376 impl ToTokens for ForeignItem {
1377 fn to_tokens(&self, tokens: &mut Tokens) {
1378 tokens.append_all(self.attrs.outer());
1379 match self.node {
1380 ForeignItemKind::Fn(ref decl, ref generics) => {
1381 self.vis.to_tokens(tokens);
1382 tokens.append("fn");
1383 self.ident.to_tokens(tokens);
1384 generics.to_tokens(tokens);
1385 tokens.append("(");
1386 tokens.append_separated(&decl.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -07001387 if decl.variadic {
1388 if !decl.inputs.is_empty() {
1389 tokens.append(",");
1390 }
1391 tokens.append("...");
1392 }
David Tolnay35902302016-10-06 01:11:08 -07001393 tokens.append(")");
1394 if let FunctionRetTy::Ty(ref ty) = decl.output {
1395 tokens.append("->");
1396 ty.to_tokens(tokens);
1397 }
1398 generics.where_clause.to_tokens(tokens);
1399 tokens.append(";");
1400 }
1401 ForeignItemKind::Static(ref ty, mutability) => {
1402 self.vis.to_tokens(tokens);
1403 tokens.append("static");
1404 mutability.to_tokens(tokens);
1405 self.ident.to_tokens(tokens);
1406 tokens.append(":");
1407 ty.to_tokens(tokens);
1408 tokens.append(";");
1409 }
1410 }
1411 }
1412 }
1413
David Tolnay62f374c2016-10-02 13:37:00 -07001414 impl ToTokens for FnArg {
1415 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001416 match *self {
1417 FnArg::SelfRef(ref lifetime, mutability) => {
1418 tokens.append("&");
1419 lifetime.to_tokens(tokens);
1420 mutability.to_tokens(tokens);
1421 tokens.append("self");
1422 }
1423 FnArg::SelfValue(mutability) => {
1424 mutability.to_tokens(tokens);
1425 tokens.append("self");
1426 }
1427 FnArg::Captured(ref pat, ref ty) => {
1428 pat.to_tokens(tokens);
1429 tokens.append(":");
1430 ty.to_tokens(tokens);
1431 }
1432 FnArg::Ignored(ref ty) => {
1433 ty.to_tokens(tokens);
1434 }
1435 }
David Tolnay62f374c2016-10-02 13:37:00 -07001436 }
1437 }
1438
David Tolnay42602292016-10-01 22:25:45 -07001439 impl ToTokens for Constness {
1440 fn to_tokens(&self, tokens: &mut Tokens) {
1441 match *self {
1442 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001443 Constness::NotConst => {
1444 // nothing
1445 }
David Tolnay42602292016-10-01 22:25:45 -07001446 }
1447 }
1448 }
1449
David Tolnay4c9be372016-10-06 00:47:37 -07001450 impl ToTokens for Defaultness {
1451 fn to_tokens(&self, tokens: &mut Tokens) {
1452 match *self {
1453 Defaultness::Default => tokens.append("default"),
1454 Defaultness::Final => {
1455 // nothing
1456 }
1457 }
1458 }
1459 }
1460
1461 impl ToTokens for ImplPolarity {
1462 fn to_tokens(&self, tokens: &mut Tokens) {
1463 match *self {
1464 ImplPolarity::Negative => tokens.append("!"),
1465 ImplPolarity::Positive => {
1466 // nothing
1467 }
1468 }
1469 }
1470 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001471}