blob: 840465ed0eef97265bc12704fef34d42da0c5f21 [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) >>
370 punct!("}") >>
371 (ViewPath::List(path, items))
372 ));
373
374 named!(view_path_list_root -> ViewPath, do_parse!(
375 global: option!(punct!("::")) >>
376 punct!("{") >>
377 items: separated_nonempty_list!(punct!(","), path_list_item) >>
378 punct!("}") >>
379 (ViewPath::List(Path {
380 global: global.is_some(),
381 segments: Vec::new(),
382 }, items))
383 ));
384
385 named!(path_list_item -> PathListItem, do_parse!(
David Tolnaye6e42542016-10-24 22:37:11 -0700386 name: alt!(
387 ident
388 |
389 map!(keyword!("self"), Into::into)
390 ) >>
David Tolnay4a057422016-10-08 00:02:31 -0700391 rename: option!(preceded!(keyword!("as"), ident)) >>
392 (PathListItem {
393 name: name,
394 rename: rename,
395 })
396 ));
397
David Tolnay47a877c2016-10-01 16:50:55 -0700398 named!(item_static -> Item, do_parse!(
399 attrs: many0!(outer_attr) >>
400 vis: visibility >>
401 keyword!("static") >>
402 mutability: mutability >>
403 id: ident >>
404 punct!(":") >>
405 ty: ty >>
406 punct!("=") >>
407 value: expr >>
408 punct!(";") >>
409 (Item {
410 ident: id,
411 vis: vis,
412 attrs: attrs,
413 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
414 })
415 ));
416
417 named!(item_const -> Item, do_parse!(
418 attrs: many0!(outer_attr) >>
419 vis: visibility >>
420 keyword!("const") >>
421 id: ident >>
422 punct!(":") >>
423 ty: ty >>
424 punct!("=") >>
425 value: expr >>
426 punct!(";") >>
427 (Item {
428 ident: id,
429 vis: vis,
430 attrs: attrs,
431 node: ItemKind::Const(Box::new(ty), Box::new(value)),
432 })
433 ));
434
David Tolnay42602292016-10-01 22:25:45 -0700435 named!(item_fn -> Item, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700436 outer_attrs: many0!(outer_attr) >>
David Tolnay42602292016-10-01 22:25:45 -0700437 vis: visibility >>
438 constness: constness >>
439 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700440 abi: option!(abi) >>
David Tolnay42602292016-10-01 22:25:45 -0700441 keyword!("fn") >>
442 name: ident >>
443 generics: generics >>
444 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700445 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700446 punct!(")") >>
447 ret: option!(preceded!(punct!("->"), ty)) >>
448 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700449 punct!("{") >>
450 inner_attrs: many0!(inner_attr) >>
451 stmts: within_block >>
452 punct!("}") >>
David Tolnay42602292016-10-01 22:25:45 -0700453 (Item {
454 ident: name,
455 vis: vis,
David Tolnay3b9783a2016-10-29 22:37:09 -0700456 attrs: {
457 let mut attrs = outer_attrs;
458 attrs.extend(inner_attrs);
459 attrs
460 },
David Tolnay42602292016-10-01 22:25:45 -0700461 node: ItemKind::Fn(
462 Box::new(FnDecl {
463 inputs: inputs,
464 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700465 variadic: false,
David Tolnay42602292016-10-01 22:25:45 -0700466 }),
467 unsafety,
468 constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700469 abi,
David Tolnay42602292016-10-01 22:25:45 -0700470 Generics {
471 where_clause: where_clause,
472 .. generics
473 },
David Tolnay3b9783a2016-10-29 22:37:09 -0700474 Box::new(Block {
475 stmts: stmts,
476 }),
David Tolnay42602292016-10-01 22:25:45 -0700477 ),
478 })
479 ));
480
David Tolnayca085422016-10-04 00:12:38 -0700481 named!(fn_arg -> FnArg, alt!(
482 do_parse!(
483 punct!("&") >>
484 lt: option!(lifetime) >>
485 mutability: mutability >>
486 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700487 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700488 (FnArg::SelfRef(lt, mutability))
489 )
490 |
491 do_parse!(
492 mutability: mutability >>
493 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700494 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700495 (FnArg::SelfValue(mutability))
496 )
497 |
498 do_parse!(
499 pat: pat >>
500 punct!(":") >>
501 ty: ty >>
502 (FnArg::Captured(pat, ty))
503 )
504 |
505 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700506 ));
507
David Tolnay35902302016-10-06 01:11:08 -0700508 named!(item_mod -> Item, do_parse!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700509 outer_attrs: many0!(outer_attr) >>
David Tolnay35902302016-10-06 01:11:08 -0700510 vis: visibility >>
511 keyword!("mod") >>
512 id: ident >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700513 content: alt!(
David Tolnay37d10332016-10-13 20:51:04 -0700514 punct!(";") => { |_| None }
515 |
David Tolnay7b8009b2016-10-25 22:36:00 -0700516 delimited!(
517 punct!("{"),
518 tuple!(
519 many0!(inner_attr),
520 items
521 ),
522 punct!("}")
523 ) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700524 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700525 (match content {
526 Some((inner_attrs, items)) => Item {
527 ident: id,
528 vis: vis,
529 attrs: {
530 let mut attrs = outer_attrs;
531 attrs.extend(inner_attrs);
532 attrs
533 },
534 node: ItemKind::Mod(Some(items)),
535 },
536 None => Item {
537 ident: id,
538 vis: vis,
539 attrs: outer_attrs,
540 node: ItemKind::Mod(None),
541 },
David Tolnay35902302016-10-06 01:11:08 -0700542 })
543 ));
544
545 named!(item_foreign_mod -> Item, do_parse!(
546 attrs: many0!(outer_attr) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700547 abi: abi >>
David Tolnay35902302016-10-06 01:11:08 -0700548 punct!("{") >>
549 items: many0!(foreign_item) >>
550 punct!("}") >>
551 (Item {
552 ident: "".into(),
553 vis: Visibility::Inherited,
554 attrs: attrs,
555 node: ItemKind::ForeignMod(ForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700556 abi: abi,
David Tolnay35902302016-10-06 01:11:08 -0700557 items: items,
558 }),
559 })
560 ));
561
562 named!(foreign_item -> ForeignItem, alt!(
563 foreign_fn
564 |
565 foreign_static
566 ));
567
568 named!(foreign_fn -> ForeignItem, do_parse!(
569 attrs: many0!(outer_attr) >>
570 vis: visibility >>
571 keyword!("fn") >>
572 name: ident >>
573 generics: generics >>
574 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700575 inputs: separated_list!(punct!(","), fn_arg) >>
576 trailing_comma: option!(punct!(",")) >>
577 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay35902302016-10-06 01:11:08 -0700578 punct!(")") >>
579 ret: option!(preceded!(punct!("->"), ty)) >>
580 where_clause: where_clause >>
581 punct!(";") >>
582 (ForeignItem {
583 ident: name,
584 attrs: attrs,
585 node: ForeignItemKind::Fn(
586 Box::new(FnDecl {
587 inputs: inputs,
588 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700589 variadic: variadic.is_some(),
David Tolnay35902302016-10-06 01:11:08 -0700590 }),
591 Generics {
592 where_clause: where_clause,
593 .. generics
594 },
595 ),
596 vis: vis,
597 })
598 ));
599
600 named!(foreign_static -> ForeignItem, do_parse!(
601 attrs: many0!(outer_attr) >>
602 vis: visibility >>
603 keyword!("static") >>
604 mutability: mutability >>
605 id: ident >>
606 punct!(":") >>
607 ty: ty >>
608 punct!(";") >>
609 (ForeignItem {
610 ident: id,
611 attrs: attrs,
612 node: ForeignItemKind::Static(Box::new(ty), mutability),
613 vis: vis,
614 })
615 ));
616
David Tolnay3cf52982016-10-01 17:11:37 -0700617 named!(item_ty -> Item, do_parse!(
618 attrs: many0!(outer_attr) >>
619 vis: visibility >>
620 keyword!("type") >>
621 id: ident >>
622 generics: generics >>
623 punct!("=") >>
624 ty: ty >>
625 punct!(";") >>
626 (Item {
627 ident: id,
628 vis: vis,
629 attrs: attrs,
630 node: ItemKind::Ty(Box::new(ty), generics),
631 })
632 ));
633
David Tolnaya96a3fa2016-09-24 07:17:42 -0700634 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700635 macro_input,
636 |def: MacroInput| Item {
637 ident: def.ident,
638 vis: def.vis,
639 attrs: def.attrs,
640 node: match def.body {
641 Body::Enum(variants) => {
642 ItemKind::Enum(variants, def.generics)
643 }
644 Body::Struct(variant_data) => {
645 ItemKind::Struct(variant_data, def.generics)
646 }
647 }
648 }
649 ));
David Tolnay42602292016-10-01 22:25:45 -0700650
David Tolnay2f9fa632016-10-03 22:08:48 -0700651 named!(item_union -> Item, do_parse!(
652 attrs: many0!(outer_attr) >>
653 vis: visibility >>
654 keyword!("union") >>
655 id: ident >>
656 generics: generics >>
657 where_clause: where_clause >>
658 fields: struct_like_body >>
659 (Item {
660 ident: id,
661 vis: vis,
662 attrs: attrs,
663 node: ItemKind::Union(
664 VariantData::Struct(fields),
665 Generics {
666 where_clause: where_clause,
667 .. generics
668 },
669 ),
670 })
671 ));
672
David Tolnay0aecb732016-10-03 23:03:50 -0700673 named!(item_trait -> Item, do_parse!(
674 attrs: many0!(outer_attr) >>
675 vis: visibility >>
676 unsafety: unsafety >>
677 keyword!("trait") >>
678 id: ident >>
679 generics: generics >>
680 bounds: opt_vec!(preceded!(
681 punct!(":"),
682 separated_nonempty_list!(punct!("+"), ty_param_bound)
683 )) >>
684 where_clause: where_clause >>
685 punct!("{") >>
686 body: many0!(trait_item) >>
687 punct!("}") >>
688 (Item {
689 ident: id,
690 vis: vis,
691 attrs: attrs,
692 node: ItemKind::Trait(
693 unsafety,
694 Generics {
695 where_clause: where_clause,
696 .. generics
697 },
698 bounds,
699 body,
700 ),
701 })
702 ));
703
David Tolnayf94e2362016-10-04 00:29:51 -0700704 named!(item_default_impl -> Item, do_parse!(
705 attrs: many0!(outer_attr) >>
706 unsafety: unsafety >>
707 keyword!("impl") >>
708 path: path >>
709 keyword!("for") >>
710 punct!("..") >>
711 punct!("{") >>
712 punct!("}") >>
713 (Item {
714 ident: "".into(),
715 vis: Visibility::Inherited,
716 attrs: attrs,
717 node: ItemKind::DefaultImpl(unsafety, path),
718 })
719 ));
720
David Tolnay0aecb732016-10-03 23:03:50 -0700721 named!(trait_item -> TraitItem, alt!(
722 trait_item_const
723 |
724 trait_item_method
725 |
726 trait_item_type
727 |
728 trait_item_mac
729 ));
730
731 named!(trait_item_const -> TraitItem, do_parse!(
732 attrs: many0!(outer_attr) >>
733 keyword!("const") >>
734 id: ident >>
735 punct!(":") >>
736 ty: ty >>
737 value: option!(preceded!(punct!("="), expr)) >>
738 punct!(";") >>
739 (TraitItem {
740 ident: id,
741 attrs: attrs,
742 node: TraitItemKind::Const(ty, value),
743 })
744 ));
745
746 named!(trait_item_method -> TraitItem, do_parse!(
David Tolnay5859df12016-10-29 22:49:54 -0700747 outer_attrs: many0!(outer_attr) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700748 constness: constness >>
749 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700750 abi: option!(abi) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700751 keyword!("fn") >>
752 name: ident >>
753 generics: generics >>
754 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700755 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700756 punct!(")") >>
757 ret: option!(preceded!(punct!("->"), ty)) >>
758 where_clause: where_clause >>
David Tolnay5859df12016-10-29 22:49:54 -0700759 body: option!(delimited!(
760 punct!("{"),
761 tuple!(many0!(inner_attr), within_block),
762 punct!("}")
763 )) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700764 cond!(body.is_none(), punct!(";")) >>
David Tolnay5859df12016-10-29 22:49:54 -0700765 ({
766 let (inner_attrs, stmts) = match body {
767 Some((inner_attrs, stmts)) => (inner_attrs, Some(stmts)),
768 None => (Vec::new(), None),
769 };
770 TraitItem {
771 ident: name,
772 attrs: {
773 let mut attrs = outer_attrs;
774 attrs.extend(inner_attrs);
775 attrs
David Tolnay0aecb732016-10-03 23:03:50 -0700776 },
David Tolnay5859df12016-10-29 22:49:54 -0700777 node: TraitItemKind::Method(
778 MethodSig {
779 unsafety: unsafety,
780 constness: constness,
781 abi: abi,
782 decl: FnDecl {
783 inputs: inputs,
784 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
785 variadic: false,
786 },
787 generics: Generics {
788 where_clause: where_clause,
789 .. generics
790 },
791 },
792 stmts.map(|stmts| Block { stmts: stmts }),
793 ),
794 }
David Tolnay0aecb732016-10-03 23:03:50 -0700795 })
796 ));
797
798 named!(trait_item_type -> TraitItem, do_parse!(
799 attrs: many0!(outer_attr) >>
800 keyword!("type") >>
801 id: ident >>
802 bounds: opt_vec!(preceded!(
803 punct!(":"),
804 separated_nonempty_list!(punct!("+"), ty_param_bound)
805 )) >>
806 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700807 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700808 (TraitItem {
809 ident: id,
810 attrs: attrs,
811 node: TraitItemKind::Type(bounds, default),
812 })
813 ));
814
815 named!(trait_item_mac -> TraitItem, do_parse!(
816 attrs: many0!(outer_attr) >>
817 id: ident >>
818 punct!("!") >>
819 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700820 cond!(match body.delim {
821 DelimToken::Paren | DelimToken::Bracket => true,
822 DelimToken::Brace => false,
823 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700824 (TraitItem {
825 ident: id.clone(),
826 attrs: attrs,
827 node: TraitItemKind::Macro(Mac {
828 path: id.into(),
829 tts: vec![TokenTree::Delimited(body)],
830 }),
831 })
832 ));
833
David Tolnay4c9be372016-10-06 00:47:37 -0700834 named!(item_impl -> Item, do_parse!(
835 attrs: many0!(outer_attr) >>
836 unsafety: unsafety >>
837 keyword!("impl") >>
838 generics: generics >>
839 polarity_path: alt!(
840 do_parse!(
841 polarity: impl_polarity >>
842 path: path >>
843 keyword!("for") >>
844 ((polarity, Some(path)))
845 )
846 |
847 epsilon!() => { |_| (ImplPolarity::Positive, None) }
848 ) >>
849 self_ty: ty >>
850 where_clause: where_clause >>
851 punct!("{") >>
852 body: many0!(impl_item) >>
853 punct!("}") >>
854 (Item {
855 ident: "".into(),
856 vis: Visibility::Inherited,
857 attrs: attrs,
858 node: ItemKind::Impl(
859 unsafety,
860 polarity_path.0,
861 Generics {
862 where_clause: where_clause,
863 .. generics
864 },
865 polarity_path.1,
866 Box::new(self_ty),
867 body,
868 ),
869 })
870 ));
871
872 named!(impl_item -> ImplItem, alt!(
873 impl_item_const
874 |
875 impl_item_method
876 |
877 impl_item_type
878 |
879 impl_item_macro
880 ));
881
882 named!(impl_item_const -> ImplItem, do_parse!(
883 attrs: many0!(outer_attr) >>
884 vis: visibility >>
885 defaultness: defaultness >>
886 keyword!("const") >>
887 id: ident >>
888 punct!(":") >>
889 ty: ty >>
890 punct!("=") >>
891 value: expr >>
892 punct!(";") >>
893 (ImplItem {
894 ident: id,
895 vis: vis,
896 defaultness: defaultness,
897 attrs: attrs,
898 node: ImplItemKind::Const(ty, value),
899 })
900 ));
901
902 named!(impl_item_method -> ImplItem, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700903 outer_attrs: many0!(outer_attr) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700904 vis: visibility >>
905 defaultness: defaultness >>
906 constness: constness >>
907 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700908 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700909 keyword!("fn") >>
910 name: ident >>
911 generics: generics >>
912 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700913 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700914 punct!(")") >>
915 ret: option!(preceded!(punct!("->"), ty)) >>
916 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700917 punct!("{") >>
918 inner_attrs: many0!(inner_attr) >>
919 stmts: within_block >>
920 punct!("}") >>
David Tolnay4c9be372016-10-06 00:47:37 -0700921 (ImplItem {
922 ident: name,
923 vis: vis,
924 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -0700925 attrs: {
926 let mut attrs = outer_attrs;
927 attrs.extend(inner_attrs);
928 attrs
929 },
David Tolnay4c9be372016-10-06 00:47:37 -0700930 node: ImplItemKind::Method(
931 MethodSig {
932 unsafety: unsafety,
933 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700934 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -0700935 decl: FnDecl {
936 inputs: inputs,
937 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700938 variadic: false,
David Tolnay4c9be372016-10-06 00:47:37 -0700939 },
940 generics: Generics {
941 where_clause: where_clause,
942 .. generics
943 },
944 },
David Tolnay3b9783a2016-10-29 22:37:09 -0700945 Block {
946 stmts: stmts,
947 },
David Tolnay4c9be372016-10-06 00:47:37 -0700948 ),
949 })
950 ));
951
952 named!(impl_item_type -> ImplItem, do_parse!(
953 attrs: many0!(outer_attr) >>
954 vis: visibility >>
955 defaultness: defaultness >>
956 keyword!("type") >>
957 id: ident >>
958 punct!("=") >>
959 ty: ty >>
960 punct!(";") >>
961 (ImplItem {
962 ident: id,
963 vis: vis,
964 defaultness: defaultness,
965 attrs: attrs,
966 node: ImplItemKind::Type(ty),
967 })
968 ));
969
970 named!(impl_item_macro -> ImplItem, do_parse!(
971 attrs: many0!(outer_attr) >>
972 id: ident >>
973 punct!("!") >>
974 body: delimited >>
975 cond!(match body.delim {
976 DelimToken::Paren | DelimToken::Bracket => true,
977 DelimToken::Brace => false,
978 }, punct!(";")) >>
979 (ImplItem {
980 ident: id.clone(),
981 vis: Visibility::Inherited,
982 defaultness: Defaultness::Final,
983 attrs: attrs,
984 node: ImplItemKind::Macro(Mac {
985 path: id.into(),
986 tts: vec![TokenTree::Delimited(body)],
987 }),
988 })
989 ));
990
991 named!(impl_polarity -> ImplPolarity, alt!(
992 punct!("!") => { |_| ImplPolarity::Negative }
993 |
994 epsilon!() => { |_| ImplPolarity::Positive }
995 ));
996
David Tolnay42602292016-10-01 22:25:45 -0700997 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700998 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700999 |
1000 epsilon!() => { |_| Constness::NotConst }
1001 ));
1002
David Tolnay4c9be372016-10-06 00:47:37 -07001003 named!(defaultness -> Defaultness, alt!(
1004 keyword!("default") => { |_| Defaultness::Default }
1005 |
1006 epsilon!() => { |_| Defaultness::Final }
1007 ));
David Tolnayedf2b992016-09-23 20:43:45 -07001008}
David Tolnay4a51dc72016-10-01 00:40:31 -07001009
1010#[cfg(feature = "printing")]
1011mod printing {
1012 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -07001013 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -07001014 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001015 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001016 use quote::{Tokens, ToTokens};
1017
1018 impl ToTokens for Item {
1019 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001020 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001021 match self.node {
1022 ItemKind::ExternCrate(ref original) => {
1023 tokens.append("extern");
1024 tokens.append("crate");
1025 if let Some(ref original) = *original {
1026 original.to_tokens(tokens);
1027 tokens.append("as");
1028 }
1029 self.ident.to_tokens(tokens);
1030 tokens.append(";");
1031 }
David Tolnay4a057422016-10-08 00:02:31 -07001032 ItemKind::Use(ref view_path) => {
1033 self.vis.to_tokens(tokens);
1034 tokens.append("use");
1035 view_path.to_tokens(tokens);
1036 tokens.append(";");
1037 }
David Tolnay47a877c2016-10-01 16:50:55 -07001038 ItemKind::Static(ref ty, ref mutability, ref expr) => {
1039 self.vis.to_tokens(tokens);
1040 tokens.append("static");
1041 mutability.to_tokens(tokens);
1042 self.ident.to_tokens(tokens);
1043 tokens.append(":");
1044 ty.to_tokens(tokens);
1045 tokens.append("=");
1046 expr.to_tokens(tokens);
1047 tokens.append(";");
1048 }
1049 ItemKind::Const(ref ty, ref expr) => {
1050 self.vis.to_tokens(tokens);
1051 tokens.append("const");
1052 self.ident.to_tokens(tokens);
1053 tokens.append(":");
1054 ty.to_tokens(tokens);
1055 tokens.append("=");
1056 expr.to_tokens(tokens);
1057 tokens.append(";");
1058 }
David Tolnay42602292016-10-01 22:25:45 -07001059 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
1060 self.vis.to_tokens(tokens);
1061 constness.to_tokens(tokens);
1062 unsafety.to_tokens(tokens);
1063 abi.to_tokens(tokens);
1064 tokens.append("fn");
1065 self.ident.to_tokens(tokens);
1066 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001067 tokens.append("(");
1068 tokens.append_separated(&decl.inputs, ",");
1069 tokens.append(")");
1070 if let FunctionRetTy::Ty(ref ty) = decl.output {
1071 tokens.append("->");
1072 ty.to_tokens(tokens);
1073 }
David Tolnay42602292016-10-01 22:25:45 -07001074 generics.where_clause.to_tokens(tokens);
David Tolnay3b9783a2016-10-29 22:37:09 -07001075 tokens.append("{");
1076 tokens.append_all(self.attrs.inner());
1077 tokens.append_all(&block.stmts);
1078 tokens.append("}");
David Tolnay42602292016-10-01 22:25:45 -07001079 }
David Tolnay35902302016-10-06 01:11:08 -07001080 ItemKind::Mod(ref items) => {
1081 self.vis.to_tokens(tokens);
1082 tokens.append("mod");
1083 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001084 match *items {
1085 Some(ref items) => {
1086 tokens.append("{");
David Tolnay7b8009b2016-10-25 22:36:00 -07001087 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001088 tokens.append_all(items);
1089 tokens.append("}");
1090 }
1091 None => tokens.append(";"),
1092 }
David Tolnay35902302016-10-06 01:11:08 -07001093 }
1094 ItemKind::ForeignMod(ref foreign_mod) => {
1095 self.vis.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001096 foreign_mod.abi.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001097 tokens.append("{");
1098 tokens.append_all(&foreign_mod.items);
1099 tokens.append("}");
1100 }
David Tolnay3cf52982016-10-01 17:11:37 -07001101 ItemKind::Ty(ref ty, ref generics) => {
1102 self.vis.to_tokens(tokens);
1103 tokens.append("type");
1104 self.ident.to_tokens(tokens);
1105 generics.to_tokens(tokens);
1106 tokens.append("=");
1107 ty.to_tokens(tokens);
1108 tokens.append(";");
1109 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001110 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001111 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001112 tokens.append("enum");
1113 self.ident.to_tokens(tokens);
1114 generics.to_tokens(tokens);
1115 generics.where_clause.to_tokens(tokens);
1116 tokens.append("{");
1117 for variant in variants {
1118 variant.to_tokens(tokens);
1119 tokens.append(",");
1120 }
1121 tokens.append("}");
1122 }
1123 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001124 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001125 tokens.append("struct");
1126 self.ident.to_tokens(tokens);
1127 generics.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001128 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001129 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -07001130 generics.where_clause.to_tokens(tokens);
1131 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001132 // no semicolon
1133 }
David Tolnay28c1db62016-10-27 22:48:18 -07001134 VariantData::Tuple(_) => {
1135 variant_data.to_tokens(tokens);
1136 generics.where_clause.to_tokens(tokens);
1137 tokens.append(";");
1138 }
1139 VariantData::Unit => {
1140 generics.where_clause.to_tokens(tokens);
1141 tokens.append(";");
1142 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001143 }
1144 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001145 ItemKind::Union(ref variant_data, ref generics) => {
1146 self.vis.to_tokens(tokens);
1147 tokens.append("union");
1148 self.ident.to_tokens(tokens);
1149 generics.to_tokens(tokens);
1150 generics.where_clause.to_tokens(tokens);
1151 variant_data.to_tokens(tokens);
1152 }
David Tolnayca085422016-10-04 00:12:38 -07001153 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1154 self.vis.to_tokens(tokens);
1155 unsafety.to_tokens(tokens);
1156 tokens.append("trait");
1157 self.ident.to_tokens(tokens);
David Tolnaye4606332016-10-25 21:57:41 -07001158 generics.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001159 if !bound.is_empty() {
1160 tokens.append(":");
1161 tokens.append_separated(bound, "+");
1162 }
David Tolnayca085422016-10-04 00:12:38 -07001163 generics.where_clause.to_tokens(tokens);
1164 tokens.append("{");
1165 tokens.append_all(items);
1166 tokens.append("}");
1167 }
David Tolnayf94e2362016-10-04 00:29:51 -07001168 ItemKind::DefaultImpl(unsafety, ref path) => {
1169 unsafety.to_tokens(tokens);
1170 tokens.append("impl");
1171 path.to_tokens(tokens);
1172 tokens.append("for");
1173 tokens.append("..");
1174 tokens.append("{");
1175 tokens.append("}");
1176 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001177 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001178 unsafety.to_tokens(tokens);
1179 tokens.append("impl");
1180 generics.to_tokens(tokens);
1181 if let Some(ref path) = *path {
1182 polarity.to_tokens(tokens);
1183 path.to_tokens(tokens);
1184 tokens.append("for");
1185 }
1186 ty.to_tokens(tokens);
1187 generics.where_clause.to_tokens(tokens);
1188 tokens.append("{");
1189 tokens.append_all(items);
1190 tokens.append("}");
1191 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001192 ItemKind::Mac(ref mac) => {
1193 mac.path.to_tokens(tokens);
1194 tokens.append("!");
1195 self.ident.to_tokens(tokens);
1196 for tt in &mac.tts {
1197 tt.to_tokens(tokens);
1198 }
1199 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001200 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1201 // no semicolon
1202 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001203 _ => tokens.append(";"),
1204 }
1205 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001206 }
1207 }
1208 }
David Tolnay42602292016-10-01 22:25:45 -07001209
David Tolnay4a057422016-10-08 00:02:31 -07001210 impl ToTokens for ViewPath {
1211 fn to_tokens(&self, tokens: &mut Tokens) {
1212 match *self {
1213 ViewPath::Simple(ref path, ref rename) => {
1214 path.to_tokens(tokens);
1215 if let Some(ref rename) = *rename {
1216 tokens.append("as");
1217 rename.to_tokens(tokens);
1218 }
1219 }
1220 ViewPath::Glob(ref path) => {
1221 path.to_tokens(tokens);
1222 tokens.append("::");
1223 tokens.append("*");
1224 }
1225 ViewPath::List(ref path, ref items) => {
1226 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001227 if path.global || !path.segments.is_empty() {
1228 tokens.append("::");
1229 }
David Tolnay4a057422016-10-08 00:02:31 -07001230 tokens.append("{");
1231 tokens.append_separated(items, ",");
1232 tokens.append("}");
1233 }
1234 }
1235 }
1236 }
1237
1238 impl ToTokens for PathListItem {
1239 fn to_tokens(&self, tokens: &mut Tokens) {
1240 self.name.to_tokens(tokens);
1241 if let Some(ref rename) = self.rename {
1242 tokens.append("as");
1243 rename.to_tokens(tokens);
1244 }
1245 }
1246 }
1247
David Tolnayca085422016-10-04 00:12:38 -07001248 impl ToTokens for TraitItem {
1249 fn to_tokens(&self, tokens: &mut Tokens) {
1250 tokens.append_all(self.attrs.outer());
1251 match self.node {
1252 TraitItemKind::Const(ref ty, ref expr) => {
1253 tokens.append("const");
1254 self.ident.to_tokens(tokens);
1255 tokens.append(":");
1256 ty.to_tokens(tokens);
1257 if let Some(ref expr) = *expr {
1258 tokens.append("=");
1259 expr.to_tokens(tokens);
1260 }
1261 tokens.append(";");
1262 }
1263 TraitItemKind::Method(ref sig, ref block) => {
David Tolnayb31d3f02016-10-25 21:15:13 -07001264 sig.constness.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001265 sig.unsafety.to_tokens(tokens);
1266 sig.abi.to_tokens(tokens);
1267 tokens.append("fn");
1268 self.ident.to_tokens(tokens);
1269 sig.generics.to_tokens(tokens);
1270 tokens.append("(");
1271 tokens.append_separated(&sig.decl.inputs, ",");
1272 tokens.append(")");
1273 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1274 tokens.append("->");
1275 ty.to_tokens(tokens);
1276 }
1277 sig.generics.where_clause.to_tokens(tokens);
1278 match *block {
David Tolnay3b9783a2016-10-29 22:37:09 -07001279 Some(ref block) => {
1280 tokens.append("{");
1281 tokens.append_all(self.attrs.inner());
1282 tokens.append_all(&block.stmts);
1283 tokens.append("}");
1284 }
David Tolnayca085422016-10-04 00:12:38 -07001285 None => tokens.append(";"),
1286 }
1287 }
1288 TraitItemKind::Type(ref bound, ref default) => {
1289 tokens.append("type");
1290 self.ident.to_tokens(tokens);
1291 if !bound.is_empty() {
1292 tokens.append(":");
1293 tokens.append_separated(bound, "+");
1294 }
1295 if let Some(ref default) = *default {
1296 tokens.append("=");
1297 default.to_tokens(tokens);
1298 }
1299 tokens.append(";");
1300 }
1301 TraitItemKind::Macro(ref mac) => {
1302 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001303 match mac.tts.last() {
1304 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1305 // no semicolon
1306 }
1307 _ => tokens.append(";"),
1308 }
David Tolnayca085422016-10-04 00:12:38 -07001309 }
1310 }
1311 }
1312 }
1313
David Tolnay4c9be372016-10-06 00:47:37 -07001314 impl ToTokens for ImplItem {
1315 fn to_tokens(&self, tokens: &mut Tokens) {
1316 tokens.append_all(self.attrs.outer());
1317 match self.node {
1318 ImplItemKind::Const(ref ty, ref expr) => {
1319 self.vis.to_tokens(tokens);
1320 self.defaultness.to_tokens(tokens);
1321 tokens.append("const");
1322 self.ident.to_tokens(tokens);
1323 tokens.append(":");
1324 ty.to_tokens(tokens);
1325 tokens.append("=");
1326 expr.to_tokens(tokens);
1327 tokens.append(";");
1328 }
1329 ImplItemKind::Method(ref sig, ref block) => {
1330 self.vis.to_tokens(tokens);
1331 self.defaultness.to_tokens(tokens);
David Tolnayb31d3f02016-10-25 21:15:13 -07001332 sig.constness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001333 sig.unsafety.to_tokens(tokens);
1334 sig.abi.to_tokens(tokens);
1335 tokens.append("fn");
1336 self.ident.to_tokens(tokens);
1337 sig.generics.to_tokens(tokens);
1338 tokens.append("(");
1339 tokens.append_separated(&sig.decl.inputs, ",");
1340 tokens.append(")");
1341 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1342 tokens.append("->");
1343 ty.to_tokens(tokens);
1344 }
1345 sig.generics.where_clause.to_tokens(tokens);
David Tolnay5859df12016-10-29 22:49:54 -07001346 tokens.append("{");
1347 tokens.append_all(self.attrs.inner());
1348 tokens.append_all(&block.stmts);
1349 tokens.append("}");
David Tolnay4c9be372016-10-06 00:47:37 -07001350 }
1351 ImplItemKind::Type(ref ty) => {
1352 self.vis.to_tokens(tokens);
1353 self.defaultness.to_tokens(tokens);
1354 tokens.append("type");
1355 self.ident.to_tokens(tokens);
1356 tokens.append("=");
1357 ty.to_tokens(tokens);
1358 tokens.append(";");
1359 }
1360 ImplItemKind::Macro(ref mac) => {
1361 mac.to_tokens(tokens);
1362 match mac.tts.last() {
1363 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1364 // no semicolon
1365 }
1366 _ => tokens.append(";"),
1367 }
1368 }
1369 }
1370 }
1371 }
1372
David Tolnay35902302016-10-06 01:11:08 -07001373 impl ToTokens for ForeignItem {
1374 fn to_tokens(&self, tokens: &mut Tokens) {
1375 tokens.append_all(self.attrs.outer());
1376 match self.node {
1377 ForeignItemKind::Fn(ref decl, ref generics) => {
1378 self.vis.to_tokens(tokens);
1379 tokens.append("fn");
1380 self.ident.to_tokens(tokens);
1381 generics.to_tokens(tokens);
1382 tokens.append("(");
1383 tokens.append_separated(&decl.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -07001384 if decl.variadic {
1385 if !decl.inputs.is_empty() {
1386 tokens.append(",");
1387 }
1388 tokens.append("...");
1389 }
David Tolnay35902302016-10-06 01:11:08 -07001390 tokens.append(")");
1391 if let FunctionRetTy::Ty(ref ty) = decl.output {
1392 tokens.append("->");
1393 ty.to_tokens(tokens);
1394 }
1395 generics.where_clause.to_tokens(tokens);
1396 tokens.append(";");
1397 }
1398 ForeignItemKind::Static(ref ty, mutability) => {
1399 self.vis.to_tokens(tokens);
1400 tokens.append("static");
1401 mutability.to_tokens(tokens);
1402 self.ident.to_tokens(tokens);
1403 tokens.append(":");
1404 ty.to_tokens(tokens);
1405 tokens.append(";");
1406 }
1407 }
1408 }
1409 }
1410
David Tolnay62f374c2016-10-02 13:37:00 -07001411 impl ToTokens for FnArg {
1412 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001413 match *self {
1414 FnArg::SelfRef(ref lifetime, mutability) => {
1415 tokens.append("&");
1416 lifetime.to_tokens(tokens);
1417 mutability.to_tokens(tokens);
1418 tokens.append("self");
1419 }
1420 FnArg::SelfValue(mutability) => {
1421 mutability.to_tokens(tokens);
1422 tokens.append("self");
1423 }
1424 FnArg::Captured(ref pat, ref ty) => {
1425 pat.to_tokens(tokens);
1426 tokens.append(":");
1427 ty.to_tokens(tokens);
1428 }
1429 FnArg::Ignored(ref ty) => {
1430 ty.to_tokens(tokens);
1431 }
1432 }
David Tolnay62f374c2016-10-02 13:37:00 -07001433 }
1434 }
1435
David Tolnay42602292016-10-01 22:25:45 -07001436 impl ToTokens for Constness {
1437 fn to_tokens(&self, tokens: &mut Tokens) {
1438 match *self {
1439 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001440 Constness::NotConst => {
1441 // nothing
1442 }
David Tolnay42602292016-10-01 22:25:45 -07001443 }
1444 }
1445 }
1446
David Tolnay4c9be372016-10-06 00:47:37 -07001447 impl ToTokens for Defaultness {
1448 fn to_tokens(&self, tokens: &mut Tokens) {
1449 match *self {
1450 Defaultness::Default => tokens.append("default"),
1451 Defaultness::Final => {
1452 // nothing
1453 }
1454 }
1455 }
1456 }
1457
1458 impl ToTokens for ImplPolarity {
1459 fn to_tokens(&self, tokens: &mut Tokens) {
1460 match *self {
1461 ImplPolarity::Negative => tokens.append("!"),
1462 ImplPolarity::Positive => {
1463 // nothing
1464 }
1465 }
1466 }
1467 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001468}