blob: 56005076f376555542d9789aa38ff8dc290f8c02 [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 Tolnay3b9783a2016-10-29 22:37:09 -0700241 use expr::parsing::{block, 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!(
747 attrs: many0!(outer_attr) >>
748 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 >>
759 body: option!(block) >>
760 cond!(body.is_none(), punct!(";")) >>
761 (TraitItem {
762 ident: name,
763 attrs: attrs,
764 node: TraitItemKind::Method(
765 MethodSig {
766 unsafety: unsafety,
767 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700768 abi: abi,
David Tolnay0aecb732016-10-03 23:03:50 -0700769 decl: FnDecl {
770 inputs: inputs,
771 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700772 variadic: false,
David Tolnay0aecb732016-10-03 23:03:50 -0700773 },
774 generics: Generics {
775 where_clause: where_clause,
776 .. generics
777 },
778 },
779 body,
780 ),
781 })
782 ));
783
784 named!(trait_item_type -> TraitItem, do_parse!(
785 attrs: many0!(outer_attr) >>
786 keyword!("type") >>
787 id: ident >>
788 bounds: opt_vec!(preceded!(
789 punct!(":"),
790 separated_nonempty_list!(punct!("+"), ty_param_bound)
791 )) >>
792 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700793 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700794 (TraitItem {
795 ident: id,
796 attrs: attrs,
797 node: TraitItemKind::Type(bounds, default),
798 })
799 ));
800
801 named!(trait_item_mac -> TraitItem, do_parse!(
802 attrs: many0!(outer_attr) >>
803 id: ident >>
804 punct!("!") >>
805 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700806 cond!(match body.delim {
807 DelimToken::Paren | DelimToken::Bracket => true,
808 DelimToken::Brace => false,
809 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700810 (TraitItem {
811 ident: id.clone(),
812 attrs: attrs,
813 node: TraitItemKind::Macro(Mac {
814 path: id.into(),
815 tts: vec![TokenTree::Delimited(body)],
816 }),
817 })
818 ));
819
David Tolnay4c9be372016-10-06 00:47:37 -0700820 named!(item_impl -> Item, do_parse!(
821 attrs: many0!(outer_attr) >>
822 unsafety: unsafety >>
823 keyword!("impl") >>
824 generics: generics >>
825 polarity_path: alt!(
826 do_parse!(
827 polarity: impl_polarity >>
828 path: path >>
829 keyword!("for") >>
830 ((polarity, Some(path)))
831 )
832 |
833 epsilon!() => { |_| (ImplPolarity::Positive, None) }
834 ) >>
835 self_ty: ty >>
836 where_clause: where_clause >>
837 punct!("{") >>
838 body: many0!(impl_item) >>
839 punct!("}") >>
840 (Item {
841 ident: "".into(),
842 vis: Visibility::Inherited,
843 attrs: attrs,
844 node: ItemKind::Impl(
845 unsafety,
846 polarity_path.0,
847 Generics {
848 where_clause: where_clause,
849 .. generics
850 },
851 polarity_path.1,
852 Box::new(self_ty),
853 body,
854 ),
855 })
856 ));
857
858 named!(impl_item -> ImplItem, alt!(
859 impl_item_const
860 |
861 impl_item_method
862 |
863 impl_item_type
864 |
865 impl_item_macro
866 ));
867
868 named!(impl_item_const -> ImplItem, do_parse!(
869 attrs: many0!(outer_attr) >>
870 vis: visibility >>
871 defaultness: defaultness >>
872 keyword!("const") >>
873 id: ident >>
874 punct!(":") >>
875 ty: ty >>
876 punct!("=") >>
877 value: expr >>
878 punct!(";") >>
879 (ImplItem {
880 ident: id,
881 vis: vis,
882 defaultness: defaultness,
883 attrs: attrs,
884 node: ImplItemKind::Const(ty, value),
885 })
886 ));
887
888 named!(impl_item_method -> ImplItem, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700889 outer_attrs: many0!(outer_attr) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700890 vis: visibility >>
891 defaultness: defaultness >>
892 constness: constness >>
893 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700894 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700895 keyword!("fn") >>
896 name: ident >>
897 generics: generics >>
898 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700899 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700900 punct!(")") >>
901 ret: option!(preceded!(punct!("->"), ty)) >>
902 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700903 punct!("{") >>
904 inner_attrs: many0!(inner_attr) >>
905 stmts: within_block >>
906 punct!("}") >>
David Tolnay4c9be372016-10-06 00:47:37 -0700907 (ImplItem {
908 ident: name,
909 vis: vis,
910 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -0700911 attrs: {
912 let mut attrs = outer_attrs;
913 attrs.extend(inner_attrs);
914 attrs
915 },
David Tolnay4c9be372016-10-06 00:47:37 -0700916 node: ImplItemKind::Method(
917 MethodSig {
918 unsafety: unsafety,
919 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700920 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -0700921 decl: FnDecl {
922 inputs: inputs,
923 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700924 variadic: false,
David Tolnay4c9be372016-10-06 00:47:37 -0700925 },
926 generics: Generics {
927 where_clause: where_clause,
928 .. generics
929 },
930 },
David Tolnay3b9783a2016-10-29 22:37:09 -0700931 Block {
932 stmts: stmts,
933 },
David Tolnay4c9be372016-10-06 00:47:37 -0700934 ),
935 })
936 ));
937
938 named!(impl_item_type -> ImplItem, do_parse!(
939 attrs: many0!(outer_attr) >>
940 vis: visibility >>
941 defaultness: defaultness >>
942 keyword!("type") >>
943 id: ident >>
944 punct!("=") >>
945 ty: ty >>
946 punct!(";") >>
947 (ImplItem {
948 ident: id,
949 vis: vis,
950 defaultness: defaultness,
951 attrs: attrs,
952 node: ImplItemKind::Type(ty),
953 })
954 ));
955
956 named!(impl_item_macro -> ImplItem, do_parse!(
957 attrs: many0!(outer_attr) >>
958 id: ident >>
959 punct!("!") >>
960 body: delimited >>
961 cond!(match body.delim {
962 DelimToken::Paren | DelimToken::Bracket => true,
963 DelimToken::Brace => false,
964 }, punct!(";")) >>
965 (ImplItem {
966 ident: id.clone(),
967 vis: Visibility::Inherited,
968 defaultness: Defaultness::Final,
969 attrs: attrs,
970 node: ImplItemKind::Macro(Mac {
971 path: id.into(),
972 tts: vec![TokenTree::Delimited(body)],
973 }),
974 })
975 ));
976
977 named!(impl_polarity -> ImplPolarity, alt!(
978 punct!("!") => { |_| ImplPolarity::Negative }
979 |
980 epsilon!() => { |_| ImplPolarity::Positive }
981 ));
982
David Tolnay42602292016-10-01 22:25:45 -0700983 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700984 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700985 |
986 epsilon!() => { |_| Constness::NotConst }
987 ));
988
David Tolnay4c9be372016-10-06 00:47:37 -0700989 named!(defaultness -> Defaultness, alt!(
990 keyword!("default") => { |_| Defaultness::Default }
991 |
992 epsilon!() => { |_| Defaultness::Final }
993 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700994}
David Tolnay4a51dc72016-10-01 00:40:31 -0700995
996#[cfg(feature = "printing")]
997mod printing {
998 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700999 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -07001000 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001001 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001002 use quote::{Tokens, ToTokens};
1003
1004 impl ToTokens for Item {
1005 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001006 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001007 match self.node {
1008 ItemKind::ExternCrate(ref original) => {
1009 tokens.append("extern");
1010 tokens.append("crate");
1011 if let Some(ref original) = *original {
1012 original.to_tokens(tokens);
1013 tokens.append("as");
1014 }
1015 self.ident.to_tokens(tokens);
1016 tokens.append(";");
1017 }
David Tolnay4a057422016-10-08 00:02:31 -07001018 ItemKind::Use(ref view_path) => {
1019 self.vis.to_tokens(tokens);
1020 tokens.append("use");
1021 view_path.to_tokens(tokens);
1022 tokens.append(";");
1023 }
David Tolnay47a877c2016-10-01 16:50:55 -07001024 ItemKind::Static(ref ty, ref mutability, ref expr) => {
1025 self.vis.to_tokens(tokens);
1026 tokens.append("static");
1027 mutability.to_tokens(tokens);
1028 self.ident.to_tokens(tokens);
1029 tokens.append(":");
1030 ty.to_tokens(tokens);
1031 tokens.append("=");
1032 expr.to_tokens(tokens);
1033 tokens.append(";");
1034 }
1035 ItemKind::Const(ref ty, ref expr) => {
1036 self.vis.to_tokens(tokens);
1037 tokens.append("const");
1038 self.ident.to_tokens(tokens);
1039 tokens.append(":");
1040 ty.to_tokens(tokens);
1041 tokens.append("=");
1042 expr.to_tokens(tokens);
1043 tokens.append(";");
1044 }
David Tolnay42602292016-10-01 22:25:45 -07001045 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
1046 self.vis.to_tokens(tokens);
1047 constness.to_tokens(tokens);
1048 unsafety.to_tokens(tokens);
1049 abi.to_tokens(tokens);
1050 tokens.append("fn");
1051 self.ident.to_tokens(tokens);
1052 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001053 tokens.append("(");
1054 tokens.append_separated(&decl.inputs, ",");
1055 tokens.append(")");
1056 if let FunctionRetTy::Ty(ref ty) = decl.output {
1057 tokens.append("->");
1058 ty.to_tokens(tokens);
1059 }
David Tolnay42602292016-10-01 22:25:45 -07001060 generics.where_clause.to_tokens(tokens);
David Tolnay3b9783a2016-10-29 22:37:09 -07001061 tokens.append("{");
1062 tokens.append_all(self.attrs.inner());
1063 tokens.append_all(&block.stmts);
1064 tokens.append("}");
David Tolnay42602292016-10-01 22:25:45 -07001065 }
David Tolnay35902302016-10-06 01:11:08 -07001066 ItemKind::Mod(ref items) => {
1067 self.vis.to_tokens(tokens);
1068 tokens.append("mod");
1069 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001070 match *items {
1071 Some(ref items) => {
1072 tokens.append("{");
David Tolnay7b8009b2016-10-25 22:36:00 -07001073 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001074 tokens.append_all(items);
1075 tokens.append("}");
1076 }
1077 None => tokens.append(";"),
1078 }
David Tolnay35902302016-10-06 01:11:08 -07001079 }
1080 ItemKind::ForeignMod(ref foreign_mod) => {
1081 self.vis.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001082 foreign_mod.abi.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001083 tokens.append("{");
1084 tokens.append_all(&foreign_mod.items);
1085 tokens.append("}");
1086 }
David Tolnay3cf52982016-10-01 17:11:37 -07001087 ItemKind::Ty(ref ty, ref generics) => {
1088 self.vis.to_tokens(tokens);
1089 tokens.append("type");
1090 self.ident.to_tokens(tokens);
1091 generics.to_tokens(tokens);
1092 tokens.append("=");
1093 ty.to_tokens(tokens);
1094 tokens.append(";");
1095 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001096 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001097 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001098 tokens.append("enum");
1099 self.ident.to_tokens(tokens);
1100 generics.to_tokens(tokens);
1101 generics.where_clause.to_tokens(tokens);
1102 tokens.append("{");
1103 for variant in variants {
1104 variant.to_tokens(tokens);
1105 tokens.append(",");
1106 }
1107 tokens.append("}");
1108 }
1109 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001110 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001111 tokens.append("struct");
1112 self.ident.to_tokens(tokens);
1113 generics.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001114 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001115 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -07001116 generics.where_clause.to_tokens(tokens);
1117 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001118 // no semicolon
1119 }
David Tolnay28c1db62016-10-27 22:48:18 -07001120 VariantData::Tuple(_) => {
1121 variant_data.to_tokens(tokens);
1122 generics.where_clause.to_tokens(tokens);
1123 tokens.append(";");
1124 }
1125 VariantData::Unit => {
1126 generics.where_clause.to_tokens(tokens);
1127 tokens.append(";");
1128 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001129 }
1130 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001131 ItemKind::Union(ref variant_data, ref generics) => {
1132 self.vis.to_tokens(tokens);
1133 tokens.append("union");
1134 self.ident.to_tokens(tokens);
1135 generics.to_tokens(tokens);
1136 generics.where_clause.to_tokens(tokens);
1137 variant_data.to_tokens(tokens);
1138 }
David Tolnayca085422016-10-04 00:12:38 -07001139 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1140 self.vis.to_tokens(tokens);
1141 unsafety.to_tokens(tokens);
1142 tokens.append("trait");
1143 self.ident.to_tokens(tokens);
David Tolnaye4606332016-10-25 21:57:41 -07001144 generics.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001145 if !bound.is_empty() {
1146 tokens.append(":");
1147 tokens.append_separated(bound, "+");
1148 }
David Tolnayca085422016-10-04 00:12:38 -07001149 generics.where_clause.to_tokens(tokens);
1150 tokens.append("{");
1151 tokens.append_all(items);
1152 tokens.append("}");
1153 }
David Tolnayf94e2362016-10-04 00:29:51 -07001154 ItemKind::DefaultImpl(unsafety, ref path) => {
1155 unsafety.to_tokens(tokens);
1156 tokens.append("impl");
1157 path.to_tokens(tokens);
1158 tokens.append("for");
1159 tokens.append("..");
1160 tokens.append("{");
1161 tokens.append("}");
1162 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001163 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001164 unsafety.to_tokens(tokens);
1165 tokens.append("impl");
1166 generics.to_tokens(tokens);
1167 if let Some(ref path) = *path {
1168 polarity.to_tokens(tokens);
1169 path.to_tokens(tokens);
1170 tokens.append("for");
1171 }
1172 ty.to_tokens(tokens);
1173 generics.where_clause.to_tokens(tokens);
1174 tokens.append("{");
1175 tokens.append_all(items);
1176 tokens.append("}");
1177 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001178 ItemKind::Mac(ref mac) => {
1179 mac.path.to_tokens(tokens);
1180 tokens.append("!");
1181 self.ident.to_tokens(tokens);
1182 for tt in &mac.tts {
1183 tt.to_tokens(tokens);
1184 }
1185 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001186 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1187 // no semicolon
1188 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001189 _ => tokens.append(";"),
1190 }
1191 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001192 }
1193 }
1194 }
David Tolnay42602292016-10-01 22:25:45 -07001195
David Tolnay4a057422016-10-08 00:02:31 -07001196 impl ToTokens for ViewPath {
1197 fn to_tokens(&self, tokens: &mut Tokens) {
1198 match *self {
1199 ViewPath::Simple(ref path, ref rename) => {
1200 path.to_tokens(tokens);
1201 if let Some(ref rename) = *rename {
1202 tokens.append("as");
1203 rename.to_tokens(tokens);
1204 }
1205 }
1206 ViewPath::Glob(ref path) => {
1207 path.to_tokens(tokens);
1208 tokens.append("::");
1209 tokens.append("*");
1210 }
1211 ViewPath::List(ref path, ref items) => {
1212 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001213 if path.global || !path.segments.is_empty() {
1214 tokens.append("::");
1215 }
David Tolnay4a057422016-10-08 00:02:31 -07001216 tokens.append("{");
1217 tokens.append_separated(items, ",");
1218 tokens.append("}");
1219 }
1220 }
1221 }
1222 }
1223
1224 impl ToTokens for PathListItem {
1225 fn to_tokens(&self, tokens: &mut Tokens) {
1226 self.name.to_tokens(tokens);
1227 if let Some(ref rename) = self.rename {
1228 tokens.append("as");
1229 rename.to_tokens(tokens);
1230 }
1231 }
1232 }
1233
David Tolnayca085422016-10-04 00:12:38 -07001234 impl ToTokens for TraitItem {
1235 fn to_tokens(&self, tokens: &mut Tokens) {
1236 tokens.append_all(self.attrs.outer());
1237 match self.node {
1238 TraitItemKind::Const(ref ty, ref expr) => {
1239 tokens.append("const");
1240 self.ident.to_tokens(tokens);
1241 tokens.append(":");
1242 ty.to_tokens(tokens);
1243 if let Some(ref expr) = *expr {
1244 tokens.append("=");
1245 expr.to_tokens(tokens);
1246 }
1247 tokens.append(";");
1248 }
1249 TraitItemKind::Method(ref sig, ref block) => {
David Tolnayb31d3f02016-10-25 21:15:13 -07001250 sig.constness.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001251 sig.unsafety.to_tokens(tokens);
1252 sig.abi.to_tokens(tokens);
1253 tokens.append("fn");
1254 self.ident.to_tokens(tokens);
1255 sig.generics.to_tokens(tokens);
1256 tokens.append("(");
1257 tokens.append_separated(&sig.decl.inputs, ",");
1258 tokens.append(")");
1259 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1260 tokens.append("->");
1261 ty.to_tokens(tokens);
1262 }
1263 sig.generics.where_clause.to_tokens(tokens);
1264 match *block {
David Tolnay3b9783a2016-10-29 22:37:09 -07001265 Some(ref block) => {
1266 tokens.append("{");
1267 tokens.append_all(self.attrs.inner());
1268 tokens.append_all(&block.stmts);
1269 tokens.append("}");
1270 }
David Tolnayca085422016-10-04 00:12:38 -07001271 None => tokens.append(";"),
1272 }
1273 }
1274 TraitItemKind::Type(ref bound, ref default) => {
1275 tokens.append("type");
1276 self.ident.to_tokens(tokens);
1277 if !bound.is_empty() {
1278 tokens.append(":");
1279 tokens.append_separated(bound, "+");
1280 }
1281 if let Some(ref default) = *default {
1282 tokens.append("=");
1283 default.to_tokens(tokens);
1284 }
1285 tokens.append(";");
1286 }
1287 TraitItemKind::Macro(ref mac) => {
1288 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001289 match mac.tts.last() {
1290 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1291 // no semicolon
1292 }
1293 _ => tokens.append(";"),
1294 }
David Tolnayca085422016-10-04 00:12:38 -07001295 }
1296 }
1297 }
1298 }
1299
David Tolnay4c9be372016-10-06 00:47:37 -07001300 impl ToTokens for ImplItem {
1301 fn to_tokens(&self, tokens: &mut Tokens) {
1302 tokens.append_all(self.attrs.outer());
1303 match self.node {
1304 ImplItemKind::Const(ref ty, ref expr) => {
1305 self.vis.to_tokens(tokens);
1306 self.defaultness.to_tokens(tokens);
1307 tokens.append("const");
1308 self.ident.to_tokens(tokens);
1309 tokens.append(":");
1310 ty.to_tokens(tokens);
1311 tokens.append("=");
1312 expr.to_tokens(tokens);
1313 tokens.append(";");
1314 }
1315 ImplItemKind::Method(ref sig, ref block) => {
1316 self.vis.to_tokens(tokens);
1317 self.defaultness.to_tokens(tokens);
David Tolnayb31d3f02016-10-25 21:15:13 -07001318 sig.constness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001319 sig.unsafety.to_tokens(tokens);
1320 sig.abi.to_tokens(tokens);
1321 tokens.append("fn");
1322 self.ident.to_tokens(tokens);
1323 sig.generics.to_tokens(tokens);
1324 tokens.append("(");
1325 tokens.append_separated(&sig.decl.inputs, ",");
1326 tokens.append(")");
1327 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1328 tokens.append("->");
1329 ty.to_tokens(tokens);
1330 }
1331 sig.generics.where_clause.to_tokens(tokens);
1332 block.to_tokens(tokens);
1333 }
1334 ImplItemKind::Type(ref ty) => {
1335 self.vis.to_tokens(tokens);
1336 self.defaultness.to_tokens(tokens);
1337 tokens.append("type");
1338 self.ident.to_tokens(tokens);
1339 tokens.append("=");
1340 ty.to_tokens(tokens);
1341 tokens.append(";");
1342 }
1343 ImplItemKind::Macro(ref mac) => {
1344 mac.to_tokens(tokens);
1345 match mac.tts.last() {
1346 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1347 // no semicolon
1348 }
1349 _ => tokens.append(";"),
1350 }
1351 }
1352 }
1353 }
1354 }
1355
David Tolnay35902302016-10-06 01:11:08 -07001356 impl ToTokens for ForeignItem {
1357 fn to_tokens(&self, tokens: &mut Tokens) {
1358 tokens.append_all(self.attrs.outer());
1359 match self.node {
1360 ForeignItemKind::Fn(ref decl, ref generics) => {
1361 self.vis.to_tokens(tokens);
1362 tokens.append("fn");
1363 self.ident.to_tokens(tokens);
1364 generics.to_tokens(tokens);
1365 tokens.append("(");
1366 tokens.append_separated(&decl.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -07001367 if decl.variadic {
1368 if !decl.inputs.is_empty() {
1369 tokens.append(",");
1370 }
1371 tokens.append("...");
1372 }
David Tolnay35902302016-10-06 01:11:08 -07001373 tokens.append(")");
1374 if let FunctionRetTy::Ty(ref ty) = decl.output {
1375 tokens.append("->");
1376 ty.to_tokens(tokens);
1377 }
1378 generics.where_clause.to_tokens(tokens);
1379 tokens.append(";");
1380 }
1381 ForeignItemKind::Static(ref ty, mutability) => {
1382 self.vis.to_tokens(tokens);
1383 tokens.append("static");
1384 mutability.to_tokens(tokens);
1385 self.ident.to_tokens(tokens);
1386 tokens.append(":");
1387 ty.to_tokens(tokens);
1388 tokens.append(";");
1389 }
1390 }
1391 }
1392 }
1393
David Tolnay62f374c2016-10-02 13:37:00 -07001394 impl ToTokens for FnArg {
1395 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001396 match *self {
1397 FnArg::SelfRef(ref lifetime, mutability) => {
1398 tokens.append("&");
1399 lifetime.to_tokens(tokens);
1400 mutability.to_tokens(tokens);
1401 tokens.append("self");
1402 }
1403 FnArg::SelfValue(mutability) => {
1404 mutability.to_tokens(tokens);
1405 tokens.append("self");
1406 }
1407 FnArg::Captured(ref pat, ref ty) => {
1408 pat.to_tokens(tokens);
1409 tokens.append(":");
1410 ty.to_tokens(tokens);
1411 }
1412 FnArg::Ignored(ref ty) => {
1413 ty.to_tokens(tokens);
1414 }
1415 }
David Tolnay62f374c2016-10-02 13:37:00 -07001416 }
1417 }
1418
David Tolnay42602292016-10-01 22:25:45 -07001419 impl ToTokens for Constness {
1420 fn to_tokens(&self, tokens: &mut Tokens) {
1421 match *self {
1422 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001423 Constness::NotConst => {
1424 // nothing
1425 }
David Tolnay42602292016-10-01 22:25:45 -07001426 }
1427 }
1428 }
1429
David Tolnay4c9be372016-10-06 00:47:37 -07001430 impl ToTokens for Defaultness {
1431 fn to_tokens(&self, tokens: &mut Tokens) {
1432 match *self {
1433 Defaultness::Default => tokens.append("default"),
1434 Defaultness::Final => {
1435 // nothing
1436 }
1437 }
1438 }
1439 }
1440
1441 impl ToTokens for ImplPolarity {
1442 fn to_tokens(&self, tokens: &mut Tokens) {
1443 match *self {
1444 ImplPolarity::Negative => tokens.append("!"),
1445 ImplPolarity::Positive => {
1446 // nothing
1447 }
1448 }
1449 }
1450 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001451}