blob: be58fbb5e99079db6e626ca00be810202550b350 [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
83#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070084pub enum ViewPath {
85 /// `foo::bar::baz as quux`
86 ///
87 /// or just
88 ///
89 /// `foo::bar::baz` (with `as baz` implicitly on the right)
David Tolnay4a057422016-10-08 00:02:31 -070090 Simple(Path, Option<Ident>),
David Tolnayf38cdf62016-09-23 19:07:09 -070091
92 /// `foo::bar::*`
David Tolnayaed77b02016-09-23 20:50:31 -070093 Glob(Path),
David Tolnayf38cdf62016-09-23 19:07:09 -070094
David Tolnayaed77b02016-09-23 20:50:31 -070095 /// `foo::bar::{a, b, c}`
David Tolnaydaaf7742016-10-03 11:11:43 -070096 List(Path, Vec<PathListItem>),
David Tolnayf38cdf62016-09-23 19:07:09 -070097}
98
99#[derive(Debug, Clone, Eq, PartialEq)]
100pub struct PathListItem {
101 pub name: Ident,
102 /// renamed in list, e.g. `use foo::{bar as baz};`
103 pub rename: Option<Ident>,
104}
105
106#[derive(Debug, Copy, Clone, Eq, PartialEq)]
107pub enum Unsafety {
108 Unsafe,
109 Normal,
110}
111
112#[derive(Debug, Copy, Clone, Eq, PartialEq)]
113pub enum Constness {
114 Const,
115 NotConst,
116}
117
118#[derive(Debug, Copy, Clone, Eq, PartialEq)]
119pub enum Defaultness {
120 Default,
121 Final,
122}
123
124#[derive(Debug, Clone, Eq, PartialEq)]
125pub struct Abi(pub String);
126
127/// Foreign module declaration.
128///
David Tolnay35902302016-10-06 01:11:08 -0700129/// E.g. `extern { .. }` or `extern "C" { .. }`
David Tolnayf38cdf62016-09-23 19:07:09 -0700130#[derive(Debug, Clone, Eq, PartialEq)]
131pub struct ForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700132 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700133 pub items: Vec<ForeignItem>,
134}
135
136#[derive(Debug, Clone, Eq, PartialEq)]
137pub struct ForeignItem {
David Tolnayb79ee962016-09-04 09:39:20 -0700138 pub ident: Ident,
139 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700140 pub node: ForeignItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -0700141 pub vis: Visibility,
David Tolnayf38cdf62016-09-23 19:07:09 -0700142}
143
David Tolnay771ecf42016-09-23 19:26:37 -0700144/// An item within an `extern` block
David Tolnayf38cdf62016-09-23 19:07:09 -0700145#[derive(Debug, Clone, Eq, PartialEq)]
146pub enum ForeignItemKind {
147 /// A foreign function
148 Fn(Box<FnDecl>, Generics),
David Tolnay35902302016-10-06 01:11:08 -0700149 /// A foreign static item (`static ext: u8`)
150 Static(Box<Ty>, Mutability),
David Tolnayf38cdf62016-09-23 19:07:09 -0700151}
152
153/// Represents an item declaration within a trait declaration,
154/// possibly including a default implementation. A trait item is
155/// either required (meaning it doesn't have an implementation, just a
156/// signature) or provided (meaning it has a default implementation).
157#[derive(Debug, Clone, Eq, PartialEq)]
158pub struct TraitItem {
159 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700160 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700161 pub node: TraitItemKind,
162}
163
164#[derive(Debug, Clone, Eq, PartialEq)]
165pub enum TraitItemKind {
166 Const(Ty, Option<Expr>),
167 Method(MethodSig, Option<Block>),
168 Type(Vec<TyParamBound>, Option<Ty>),
169 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700170}
171
David Tolnay55337722016-09-11 12:58:56 -0700172#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700173pub enum ImplPolarity {
174 /// `impl Trait for Type`
175 Positive,
176 /// `impl !Trait for Type`
177 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700178}
179
David Tolnayf38cdf62016-09-23 19:07:09 -0700180#[derive(Debug, Clone, Eq, PartialEq)]
181pub struct ImplItem {
182 pub ident: Ident,
183 pub vis: Visibility,
184 pub defaultness: Defaultness,
185 pub attrs: Vec<Attribute>,
186 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700187}
188
David Tolnayf38cdf62016-09-23 19:07:09 -0700189#[derive(Debug, Clone, Eq, PartialEq)]
190pub enum ImplItemKind {
191 Const(Ty, Expr),
192 Method(MethodSig, Block),
193 Type(Ty),
194 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700195}
David Tolnayd5025812016-09-04 14:21:46 -0700196
David Tolnayf38cdf62016-09-23 19:07:09 -0700197/// Represents a method's signature in a trait declaration,
198/// or in an implementation.
199#[derive(Debug, Clone, Eq, PartialEq)]
200pub struct MethodSig {
201 pub unsafety: Unsafety,
202 pub constness: Constness,
David Tolnay0aecb732016-10-03 23:03:50 -0700203 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700204 pub decl: FnDecl,
205 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700206}
David Tolnayedf2b992016-09-23 20:43:45 -0700207
David Tolnay62f374c2016-10-02 13:37:00 -0700208/// Header (not the body) of a function declaration.
209///
210/// E.g. `fn foo(bar: baz)`
211#[derive(Debug, Clone, Eq, PartialEq)]
212pub struct FnDecl {
213 pub inputs: Vec<FnArg>,
214 pub output: FunctionRetTy,
215}
216
217/// An argument in a function header.
218///
219/// E.g. `bar: usize` as in `fn foo(bar: usize)`
220#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayca085422016-10-04 00:12:38 -0700221pub enum FnArg {
222 SelfRef(Option<Lifetime>, Mutability),
223 SelfValue(Mutability),
224 Captured(Pat, Ty),
225 Ignored(Ty),
David Tolnay62f374c2016-10-02 13:37:00 -0700226}
227
David Tolnayedf2b992016-09-23 20:43:45 -0700228#[cfg(feature = "parsing")]
229pub mod parsing {
230 use super::*;
David Tolnay4a057422016-10-08 00:02:31 -0700231 use {DelimToken, FunctionRetTy, Generics, Ident, Mac, Path, TokenTree, VariantData, Visibility};
David Tolnay4a51dc72016-10-01 00:40:31 -0700232 use attr::parsing::outer_attr;
David Tolnay2f9fa632016-10-03 22:08:48 -0700233 use data::parsing::{struct_like_body, visibility};
David Tolnay62f374c2016-10-02 13:37:00 -0700234 use expr::parsing::{block, expr, pat};
David Tolnayca085422016-10-04 00:12:38 -0700235 use generics::parsing::{generics, lifetime, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700236 use ident::parsing::ident;
David Tolnay42602292016-10-01 22:25:45 -0700237 use lit::parsing::quoted_string;
David Tolnay84aa0752016-10-02 23:01:13 -0700238 use mac::parsing::delimited;
David Tolnayedf2b992016-09-23 20:43:45 -0700239 use macro_input::{Body, MacroInput};
240 use macro_input::parsing::macro_input;
David Tolnayf94e2362016-10-04 00:29:51 -0700241 use ty::parsing::{mutability, path, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700242
243 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700244 item_extern_crate
David Tolnay4a057422016-10-08 00:02:31 -0700245 |
246 item_use
David Tolnay47a877c2016-10-01 16:50:55 -0700247 |
248 item_static
249 |
250 item_const
David Tolnay42602292016-10-01 22:25:45 -0700251 |
252 item_fn
David Tolnay35902302016-10-06 01:11:08 -0700253 |
254 item_mod
255 |
256 item_foreign_mod
David Tolnay3cf52982016-10-01 17:11:37 -0700257 |
258 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700259 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700260 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700261 |
262 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700263 |
264 item_trait
David Tolnayf94e2362016-10-04 00:29:51 -0700265 |
266 item_default_impl
David Tolnay4c9be372016-10-06 00:47:37 -0700267 |
268 item_impl
David Tolnay84aa0752016-10-02 23:01:13 -0700269 |
270 item_mac
271 ));
272
273 named!(item_mac -> Item, do_parse!(
274 attrs: many0!(outer_attr) >>
275 path: ident >>
276 punct!("!") >>
277 name: option!(ident) >>
278 body: delimited >>
David Tolnay1a8b3522016-10-08 22:27:00 -0700279 cond!(match body.delim {
280 DelimToken::Paren | DelimToken::Bracket => true,
281 DelimToken::Brace => false,
282 }, punct!(";")) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700283 (Item {
284 ident: name.unwrap_or_else(|| Ident::new("")),
285 vis: Visibility::Inherited,
286 attrs: attrs,
287 node: ItemKind::Mac(Mac {
288 path: path.into(),
289 tts: vec![TokenTree::Delimited(body)],
290 }),
291 })
David Tolnayedf2b992016-09-23 20:43:45 -0700292 ));
293
David Tolnaya96a3fa2016-09-24 07:17:42 -0700294 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700295 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700296 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700297 keyword!("extern") >>
298 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700299 id: ident >>
300 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700301 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700302 ident
303 )) >>
304 punct!(";") >>
305 ({
306 let (name, original_name) = match rename {
307 Some(rename) => (rename, Some(id)),
308 None => (id, None),
309 };
310 Item {
311 ident: name,
312 vis: vis,
313 attrs: attrs,
314 node: ItemKind::ExternCrate(original_name),
315 }
316 })
317 ));
318
David Tolnay4a057422016-10-08 00:02:31 -0700319 named!(item_use -> Item, do_parse!(
320 attrs: many0!(outer_attr) >>
321 vis: visibility >>
322 keyword!("use") >>
323 what: view_path >>
324 punct!(";") >>
325 (Item {
326 ident: "".into(),
327 vis: vis,
328 attrs: attrs,
329 node: ItemKind::Use(Box::new(what)),
330 })
331 ));
332
333 named!(view_path -> ViewPath, alt!(
334 view_path_glob
335 |
336 view_path_list
337 |
338 view_path_list_root
339 |
340 view_path_simple // must be last
341 ));
342
343
344 named!(view_path_simple -> ViewPath, do_parse!(
345 path: path >>
346 rename: option!(preceded!(keyword!("as"), ident)) >>
347 (ViewPath::Simple(path, rename))
348 ));
349
350 named!(view_path_glob -> ViewPath, do_parse!(
351 path: path >>
352 punct!("::") >>
353 punct!("*") >>
354 (ViewPath::Glob(path))
355 ));
356
357 named!(view_path_list -> ViewPath, do_parse!(
358 path: path >>
359 punct!("::") >>
360 punct!("{") >>
361 items: separated_nonempty_list!(punct!(","), path_list_item) >>
362 punct!("}") >>
363 (ViewPath::List(path, items))
364 ));
365
366 named!(view_path_list_root -> ViewPath, do_parse!(
367 global: option!(punct!("::")) >>
368 punct!("{") >>
369 items: separated_nonempty_list!(punct!(","), path_list_item) >>
370 punct!("}") >>
371 (ViewPath::List(Path {
372 global: global.is_some(),
373 segments: Vec::new(),
374 }, items))
375 ));
376
377 named!(path_list_item -> PathListItem, do_parse!(
378 name: ident >>
379 rename: option!(preceded!(keyword!("as"), ident)) >>
380 (PathListItem {
381 name: name,
382 rename: rename,
383 })
384 ));
385
David Tolnay47a877c2016-10-01 16:50:55 -0700386 named!(item_static -> Item, do_parse!(
387 attrs: many0!(outer_attr) >>
388 vis: visibility >>
389 keyword!("static") >>
390 mutability: mutability >>
391 id: ident >>
392 punct!(":") >>
393 ty: ty >>
394 punct!("=") >>
395 value: expr >>
396 punct!(";") >>
397 (Item {
398 ident: id,
399 vis: vis,
400 attrs: attrs,
401 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
402 })
403 ));
404
405 named!(item_const -> Item, do_parse!(
406 attrs: many0!(outer_attr) >>
407 vis: visibility >>
408 keyword!("const") >>
409 id: ident >>
410 punct!(":") >>
411 ty: ty >>
412 punct!("=") >>
413 value: expr >>
414 punct!(";") >>
415 (Item {
416 ident: id,
417 vis: vis,
418 attrs: attrs,
419 node: ItemKind::Const(Box::new(ty), Box::new(value)),
420 })
421 ));
422
David Tolnay42602292016-10-01 22:25:45 -0700423 named!(item_fn -> Item, do_parse!(
424 attrs: many0!(outer_attr) >>
425 vis: visibility >>
426 constness: constness >>
427 unsafety: unsafety >>
428 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
429 keyword!("fn") >>
430 name: ident >>
431 generics: generics >>
432 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700433 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700434 punct!(")") >>
435 ret: option!(preceded!(punct!("->"), ty)) >>
436 where_clause: where_clause >>
437 body: block >>
438 (Item {
439 ident: name,
440 vis: vis,
441 attrs: attrs,
442 node: ItemKind::Fn(
443 Box::new(FnDecl {
444 inputs: inputs,
445 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
446 }),
447 unsafety,
448 constness,
449 abi.map(Abi),
450 Generics {
451 where_clause: where_clause,
452 .. generics
453 },
454 Box::new(body),
455 ),
456 })
457 ));
458
David Tolnayca085422016-10-04 00:12:38 -0700459 named!(fn_arg -> FnArg, alt!(
460 do_parse!(
461 punct!("&") >>
462 lt: option!(lifetime) >>
463 mutability: mutability >>
464 keyword!("self") >>
465 (FnArg::SelfRef(lt, mutability))
466 )
467 |
468 do_parse!(
469 mutability: mutability >>
470 keyword!("self") >>
471 (FnArg::SelfValue(mutability))
472 )
473 |
474 do_parse!(
475 pat: pat >>
476 punct!(":") >>
477 ty: ty >>
478 (FnArg::Captured(pat, ty))
479 )
480 |
481 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700482 ));
483
David Tolnay35902302016-10-06 01:11:08 -0700484 named!(item_mod -> Item, do_parse!(
485 attrs: many0!(outer_attr) >>
486 vis: visibility >>
487 keyword!("mod") >>
488 id: ident >>
David Tolnay37d10332016-10-13 20:51:04 -0700489 items: alt!(
490 punct!(";") => { |_| None }
491 |
492 delimited!(punct!("{"), many0!(item), punct!("}")) => { Some }
493 ) >>
David Tolnay35902302016-10-06 01:11:08 -0700494 (Item {
495 ident: id,
496 vis: vis,
497 attrs: attrs,
498 node: ItemKind::Mod(items),
499 })
500 ));
501
502 named!(item_foreign_mod -> Item, do_parse!(
503 attrs: many0!(outer_attr) >>
504 keyword!("extern") >>
505 abi: option!(quoted_string) >>
506 punct!("{") >>
507 items: many0!(foreign_item) >>
508 punct!("}") >>
509 (Item {
510 ident: "".into(),
511 vis: Visibility::Inherited,
512 attrs: attrs,
513 node: ItemKind::ForeignMod(ForeignMod {
514 abi: abi.map(Abi),
515 items: items,
516 }),
517 })
518 ));
519
520 named!(foreign_item -> ForeignItem, alt!(
521 foreign_fn
522 |
523 foreign_static
524 ));
525
526 named!(foreign_fn -> ForeignItem, do_parse!(
527 attrs: many0!(outer_attr) >>
528 vis: visibility >>
529 keyword!("fn") >>
530 name: ident >>
531 generics: generics >>
532 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700533 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay35902302016-10-06 01:11:08 -0700534 punct!(")") >>
535 ret: option!(preceded!(punct!("->"), ty)) >>
536 where_clause: where_clause >>
537 punct!(";") >>
538 (ForeignItem {
539 ident: name,
540 attrs: attrs,
541 node: ForeignItemKind::Fn(
542 Box::new(FnDecl {
543 inputs: inputs,
544 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
545 }),
546 Generics {
547 where_clause: where_clause,
548 .. generics
549 },
550 ),
551 vis: vis,
552 })
553 ));
554
555 named!(foreign_static -> ForeignItem, do_parse!(
556 attrs: many0!(outer_attr) >>
557 vis: visibility >>
558 keyword!("static") >>
559 mutability: mutability >>
560 id: ident >>
561 punct!(":") >>
562 ty: ty >>
563 punct!(";") >>
564 (ForeignItem {
565 ident: id,
566 attrs: attrs,
567 node: ForeignItemKind::Static(Box::new(ty), mutability),
568 vis: vis,
569 })
570 ));
571
David Tolnay3cf52982016-10-01 17:11:37 -0700572 named!(item_ty -> Item, do_parse!(
573 attrs: many0!(outer_attr) >>
574 vis: visibility >>
575 keyword!("type") >>
576 id: ident >>
577 generics: generics >>
578 punct!("=") >>
579 ty: ty >>
580 punct!(";") >>
581 (Item {
582 ident: id,
583 vis: vis,
584 attrs: attrs,
585 node: ItemKind::Ty(Box::new(ty), generics),
586 })
587 ));
588
David Tolnaya96a3fa2016-09-24 07:17:42 -0700589 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700590 macro_input,
591 |def: MacroInput| Item {
592 ident: def.ident,
593 vis: def.vis,
594 attrs: def.attrs,
595 node: match def.body {
596 Body::Enum(variants) => {
597 ItemKind::Enum(variants, def.generics)
598 }
599 Body::Struct(variant_data) => {
600 ItemKind::Struct(variant_data, def.generics)
601 }
602 }
603 }
604 ));
David Tolnay42602292016-10-01 22:25:45 -0700605
David Tolnay2f9fa632016-10-03 22:08:48 -0700606 named!(item_union -> Item, do_parse!(
607 attrs: many0!(outer_attr) >>
608 vis: visibility >>
609 keyword!("union") >>
610 id: ident >>
611 generics: generics >>
612 where_clause: where_clause >>
613 fields: struct_like_body >>
614 (Item {
615 ident: id,
616 vis: vis,
617 attrs: attrs,
618 node: ItemKind::Union(
619 VariantData::Struct(fields),
620 Generics {
621 where_clause: where_clause,
622 .. generics
623 },
624 ),
625 })
626 ));
627
David Tolnay0aecb732016-10-03 23:03:50 -0700628 named!(item_trait -> Item, do_parse!(
629 attrs: many0!(outer_attr) >>
630 vis: visibility >>
631 unsafety: unsafety >>
632 keyword!("trait") >>
633 id: ident >>
634 generics: generics >>
635 bounds: opt_vec!(preceded!(
636 punct!(":"),
637 separated_nonempty_list!(punct!("+"), ty_param_bound)
638 )) >>
639 where_clause: where_clause >>
640 punct!("{") >>
641 body: many0!(trait_item) >>
642 punct!("}") >>
643 (Item {
644 ident: id,
645 vis: vis,
646 attrs: attrs,
647 node: ItemKind::Trait(
648 unsafety,
649 Generics {
650 where_clause: where_clause,
651 .. generics
652 },
653 bounds,
654 body,
655 ),
656 })
657 ));
658
David Tolnayf94e2362016-10-04 00:29:51 -0700659 named!(item_default_impl -> Item, do_parse!(
660 attrs: many0!(outer_attr) >>
661 unsafety: unsafety >>
662 keyword!("impl") >>
663 path: path >>
664 keyword!("for") >>
665 punct!("..") >>
666 punct!("{") >>
667 punct!("}") >>
668 (Item {
669 ident: "".into(),
670 vis: Visibility::Inherited,
671 attrs: attrs,
672 node: ItemKind::DefaultImpl(unsafety, path),
673 })
674 ));
675
David Tolnay0aecb732016-10-03 23:03:50 -0700676 named!(trait_item -> TraitItem, alt!(
677 trait_item_const
678 |
679 trait_item_method
680 |
681 trait_item_type
682 |
683 trait_item_mac
684 ));
685
686 named!(trait_item_const -> TraitItem, do_parse!(
687 attrs: many0!(outer_attr) >>
688 keyword!("const") >>
689 id: ident >>
690 punct!(":") >>
691 ty: ty >>
692 value: option!(preceded!(punct!("="), expr)) >>
693 punct!(";") >>
694 (TraitItem {
695 ident: id,
696 attrs: attrs,
697 node: TraitItemKind::Const(ty, value),
698 })
699 ));
700
701 named!(trait_item_method -> TraitItem, do_parse!(
702 attrs: many0!(outer_attr) >>
703 constness: constness >>
704 unsafety: unsafety >>
705 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
706 keyword!("fn") >>
707 name: ident >>
708 generics: generics >>
709 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700710 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700711 punct!(")") >>
712 ret: option!(preceded!(punct!("->"), ty)) >>
713 where_clause: where_clause >>
714 body: option!(block) >>
715 cond!(body.is_none(), punct!(";")) >>
716 (TraitItem {
717 ident: name,
718 attrs: attrs,
719 node: TraitItemKind::Method(
720 MethodSig {
721 unsafety: unsafety,
722 constness: constness,
723 abi: abi.map(Abi),
724 decl: FnDecl {
725 inputs: inputs,
726 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
727 },
728 generics: Generics {
729 where_clause: where_clause,
730 .. generics
731 },
732 },
733 body,
734 ),
735 })
736 ));
737
738 named!(trait_item_type -> TraitItem, do_parse!(
739 attrs: many0!(outer_attr) >>
740 keyword!("type") >>
741 id: ident >>
742 bounds: opt_vec!(preceded!(
743 punct!(":"),
744 separated_nonempty_list!(punct!("+"), ty_param_bound)
745 )) >>
746 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700747 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700748 (TraitItem {
749 ident: id,
750 attrs: attrs,
751 node: TraitItemKind::Type(bounds, default),
752 })
753 ));
754
755 named!(trait_item_mac -> TraitItem, do_parse!(
756 attrs: many0!(outer_attr) >>
757 id: ident >>
758 punct!("!") >>
759 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700760 cond!(match body.delim {
761 DelimToken::Paren | DelimToken::Bracket => true,
762 DelimToken::Brace => false,
763 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700764 (TraitItem {
765 ident: id.clone(),
766 attrs: attrs,
767 node: TraitItemKind::Macro(Mac {
768 path: id.into(),
769 tts: vec![TokenTree::Delimited(body)],
770 }),
771 })
772 ));
773
David Tolnay4c9be372016-10-06 00:47:37 -0700774 named!(item_impl -> Item, do_parse!(
775 attrs: many0!(outer_attr) >>
776 unsafety: unsafety >>
777 keyword!("impl") >>
778 generics: generics >>
779 polarity_path: alt!(
780 do_parse!(
781 polarity: impl_polarity >>
782 path: path >>
783 keyword!("for") >>
784 ((polarity, Some(path)))
785 )
786 |
787 epsilon!() => { |_| (ImplPolarity::Positive, None) }
788 ) >>
789 self_ty: ty >>
790 where_clause: where_clause >>
791 punct!("{") >>
792 body: many0!(impl_item) >>
793 punct!("}") >>
794 (Item {
795 ident: "".into(),
796 vis: Visibility::Inherited,
797 attrs: attrs,
798 node: ItemKind::Impl(
799 unsafety,
800 polarity_path.0,
801 Generics {
802 where_clause: where_clause,
803 .. generics
804 },
805 polarity_path.1,
806 Box::new(self_ty),
807 body,
808 ),
809 })
810 ));
811
812 named!(impl_item -> ImplItem, alt!(
813 impl_item_const
814 |
815 impl_item_method
816 |
817 impl_item_type
818 |
819 impl_item_macro
820 ));
821
822 named!(impl_item_const -> ImplItem, do_parse!(
823 attrs: many0!(outer_attr) >>
824 vis: visibility >>
825 defaultness: defaultness >>
826 keyword!("const") >>
827 id: ident >>
828 punct!(":") >>
829 ty: ty >>
830 punct!("=") >>
831 value: expr >>
832 punct!(";") >>
833 (ImplItem {
834 ident: id,
835 vis: vis,
836 defaultness: defaultness,
837 attrs: attrs,
838 node: ImplItemKind::Const(ty, value),
839 })
840 ));
841
842 named!(impl_item_method -> ImplItem, do_parse!(
843 attrs: many0!(outer_attr) >>
844 vis: visibility >>
845 defaultness: defaultness >>
846 constness: constness >>
847 unsafety: unsafety >>
848 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
849 keyword!("fn") >>
850 name: ident >>
851 generics: generics >>
852 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700853 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700854 punct!(")") >>
855 ret: option!(preceded!(punct!("->"), ty)) >>
856 where_clause: where_clause >>
857 body: block >>
858 (ImplItem {
859 ident: name,
860 vis: vis,
861 defaultness: defaultness,
862 attrs: attrs,
863 node: ImplItemKind::Method(
864 MethodSig {
865 unsafety: unsafety,
866 constness: constness,
867 abi: abi.map(Abi),
868 decl: FnDecl {
869 inputs: inputs,
870 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
871 },
872 generics: Generics {
873 where_clause: where_clause,
874 .. generics
875 },
876 },
877 body,
878 ),
879 })
880 ));
881
882 named!(impl_item_type -> ImplItem, do_parse!(
883 attrs: many0!(outer_attr) >>
884 vis: visibility >>
885 defaultness: defaultness >>
886 keyword!("type") >>
887 id: ident >>
888 punct!("=") >>
889 ty: ty >>
890 punct!(";") >>
891 (ImplItem {
892 ident: id,
893 vis: vis,
894 defaultness: defaultness,
895 attrs: attrs,
896 node: ImplItemKind::Type(ty),
897 })
898 ));
899
900 named!(impl_item_macro -> ImplItem, do_parse!(
901 attrs: many0!(outer_attr) >>
902 id: ident >>
903 punct!("!") >>
904 body: delimited >>
905 cond!(match body.delim {
906 DelimToken::Paren | DelimToken::Bracket => true,
907 DelimToken::Brace => false,
908 }, punct!(";")) >>
909 (ImplItem {
910 ident: id.clone(),
911 vis: Visibility::Inherited,
912 defaultness: Defaultness::Final,
913 attrs: attrs,
914 node: ImplItemKind::Macro(Mac {
915 path: id.into(),
916 tts: vec![TokenTree::Delimited(body)],
917 }),
918 })
919 ));
920
921 named!(impl_polarity -> ImplPolarity, alt!(
922 punct!("!") => { |_| ImplPolarity::Negative }
923 |
924 epsilon!() => { |_| ImplPolarity::Positive }
925 ));
926
David Tolnay42602292016-10-01 22:25:45 -0700927 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700928 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700929 |
930 epsilon!() => { |_| Constness::NotConst }
931 ));
932
933 named!(unsafety -> Unsafety, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700934 keyword!("unsafe") => { |_| Unsafety::Unsafe }
David Tolnay42602292016-10-01 22:25:45 -0700935 |
936 epsilon!() => { |_| Unsafety::Normal }
937 ));
David Tolnay4c9be372016-10-06 00:47:37 -0700938
939 named!(defaultness -> Defaultness, alt!(
940 keyword!("default") => { |_| Defaultness::Default }
941 |
942 epsilon!() => { |_| Defaultness::Final }
943 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700944}
David Tolnay4a51dc72016-10-01 00:40:31 -0700945
946#[cfg(feature = "printing")]
947mod printing {
948 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700949 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700950 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700951 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700952 use quote::{Tokens, ToTokens};
953
954 impl ToTokens for Item {
955 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -0700956 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -0700957 match self.node {
958 ItemKind::ExternCrate(ref original) => {
959 tokens.append("extern");
960 tokens.append("crate");
961 if let Some(ref original) = *original {
962 original.to_tokens(tokens);
963 tokens.append("as");
964 }
965 self.ident.to_tokens(tokens);
966 tokens.append(";");
967 }
David Tolnay4a057422016-10-08 00:02:31 -0700968 ItemKind::Use(ref view_path) => {
969 self.vis.to_tokens(tokens);
970 tokens.append("use");
971 view_path.to_tokens(tokens);
972 tokens.append(";");
973 }
David Tolnay47a877c2016-10-01 16:50:55 -0700974 ItemKind::Static(ref ty, ref mutability, ref expr) => {
975 self.vis.to_tokens(tokens);
976 tokens.append("static");
977 mutability.to_tokens(tokens);
978 self.ident.to_tokens(tokens);
979 tokens.append(":");
980 ty.to_tokens(tokens);
981 tokens.append("=");
982 expr.to_tokens(tokens);
983 tokens.append(";");
984 }
985 ItemKind::Const(ref ty, ref expr) => {
986 self.vis.to_tokens(tokens);
987 tokens.append("const");
988 self.ident.to_tokens(tokens);
989 tokens.append(":");
990 ty.to_tokens(tokens);
991 tokens.append("=");
992 expr.to_tokens(tokens);
993 tokens.append(";");
994 }
David Tolnay42602292016-10-01 22:25:45 -0700995 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
996 self.vis.to_tokens(tokens);
997 constness.to_tokens(tokens);
998 unsafety.to_tokens(tokens);
999 abi.to_tokens(tokens);
1000 tokens.append("fn");
1001 self.ident.to_tokens(tokens);
1002 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001003 tokens.append("(");
1004 tokens.append_separated(&decl.inputs, ",");
1005 tokens.append(")");
1006 if let FunctionRetTy::Ty(ref ty) = decl.output {
1007 tokens.append("->");
1008 ty.to_tokens(tokens);
1009 }
David Tolnay42602292016-10-01 22:25:45 -07001010 generics.where_clause.to_tokens(tokens);
1011 block.to_tokens(tokens);
1012 }
David Tolnay35902302016-10-06 01:11:08 -07001013 ItemKind::Mod(ref items) => {
1014 self.vis.to_tokens(tokens);
1015 tokens.append("mod");
1016 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001017 match *items {
1018 Some(ref items) => {
1019 tokens.append("{");
1020 tokens.append_all(items);
1021 tokens.append("}");
1022 }
1023 None => tokens.append(";"),
1024 }
David Tolnay35902302016-10-06 01:11:08 -07001025 }
1026 ItemKind::ForeignMod(ref foreign_mod) => {
1027 self.vis.to_tokens(tokens);
1028 match foreign_mod.abi {
1029 Some(ref abi) => abi.to_tokens(tokens),
1030 None => tokens.append("extern"),
1031 }
1032 tokens.append("{");
1033 tokens.append_all(&foreign_mod.items);
1034 tokens.append("}");
1035 }
David Tolnay3cf52982016-10-01 17:11:37 -07001036 ItemKind::Ty(ref ty, ref generics) => {
1037 self.vis.to_tokens(tokens);
1038 tokens.append("type");
1039 self.ident.to_tokens(tokens);
1040 generics.to_tokens(tokens);
1041 tokens.append("=");
1042 ty.to_tokens(tokens);
1043 tokens.append(";");
1044 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001045 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001046 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001047 tokens.append("enum");
1048 self.ident.to_tokens(tokens);
1049 generics.to_tokens(tokens);
1050 generics.where_clause.to_tokens(tokens);
1051 tokens.append("{");
1052 for variant in variants {
1053 variant.to_tokens(tokens);
1054 tokens.append(",");
1055 }
1056 tokens.append("}");
1057 }
1058 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001059 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001060 tokens.append("struct");
1061 self.ident.to_tokens(tokens);
1062 generics.to_tokens(tokens);
1063 generics.where_clause.to_tokens(tokens);
1064 variant_data.to_tokens(tokens);
1065 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001066 VariantData::Struct(_) => {
1067 // no semicolon
1068 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001069 VariantData::Tuple(_) |
1070 VariantData::Unit => tokens.append(";"),
1071 }
1072 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001073 ItemKind::Union(ref variant_data, ref generics) => {
1074 self.vis.to_tokens(tokens);
1075 tokens.append("union");
1076 self.ident.to_tokens(tokens);
1077 generics.to_tokens(tokens);
1078 generics.where_clause.to_tokens(tokens);
1079 variant_data.to_tokens(tokens);
1080 }
David Tolnayca085422016-10-04 00:12:38 -07001081 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1082 self.vis.to_tokens(tokens);
1083 unsafety.to_tokens(tokens);
1084 tokens.append("trait");
1085 self.ident.to_tokens(tokens);
1086 if !bound.is_empty() {
1087 tokens.append(":");
1088 tokens.append_separated(bound, "+");
1089 }
1090 generics.to_tokens(tokens);
1091 generics.where_clause.to_tokens(tokens);
1092 tokens.append("{");
1093 tokens.append_all(items);
1094 tokens.append("}");
1095 }
David Tolnayf94e2362016-10-04 00:29:51 -07001096 ItemKind::DefaultImpl(unsafety, ref path) => {
1097 unsafety.to_tokens(tokens);
1098 tokens.append("impl");
1099 path.to_tokens(tokens);
1100 tokens.append("for");
1101 tokens.append("..");
1102 tokens.append("{");
1103 tokens.append("}");
1104 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001105 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001106 unsafety.to_tokens(tokens);
1107 tokens.append("impl");
1108 generics.to_tokens(tokens);
1109 if let Some(ref path) = *path {
1110 polarity.to_tokens(tokens);
1111 path.to_tokens(tokens);
1112 tokens.append("for");
1113 }
1114 ty.to_tokens(tokens);
1115 generics.where_clause.to_tokens(tokens);
1116 tokens.append("{");
1117 tokens.append_all(items);
1118 tokens.append("}");
1119 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001120 ItemKind::Mac(ref mac) => {
1121 mac.path.to_tokens(tokens);
1122 tokens.append("!");
1123 self.ident.to_tokens(tokens);
1124 for tt in &mac.tts {
1125 tt.to_tokens(tokens);
1126 }
1127 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001128 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1129 // no semicolon
1130 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001131 _ => tokens.append(";"),
1132 }
1133 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001134 }
1135 }
1136 }
David Tolnay42602292016-10-01 22:25:45 -07001137
David Tolnay4a057422016-10-08 00:02:31 -07001138 impl ToTokens for ViewPath {
1139 fn to_tokens(&self, tokens: &mut Tokens) {
1140 match *self {
1141 ViewPath::Simple(ref path, ref rename) => {
1142 path.to_tokens(tokens);
1143 if let Some(ref rename) = *rename {
1144 tokens.append("as");
1145 rename.to_tokens(tokens);
1146 }
1147 }
1148 ViewPath::Glob(ref path) => {
1149 path.to_tokens(tokens);
1150 tokens.append("::");
1151 tokens.append("*");
1152 }
1153 ViewPath::List(ref path, ref items) => {
1154 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001155 if path.global || !path.segments.is_empty() {
1156 tokens.append("::");
1157 }
David Tolnay4a057422016-10-08 00:02:31 -07001158 tokens.append("{");
1159 tokens.append_separated(items, ",");
1160 tokens.append("}");
1161 }
1162 }
1163 }
1164 }
1165
1166 impl ToTokens for PathListItem {
1167 fn to_tokens(&self, tokens: &mut Tokens) {
1168 self.name.to_tokens(tokens);
1169 if let Some(ref rename) = self.rename {
1170 tokens.append("as");
1171 rename.to_tokens(tokens);
1172 }
1173 }
1174 }
1175
David Tolnayca085422016-10-04 00:12:38 -07001176 impl ToTokens for TraitItem {
1177 fn to_tokens(&self, tokens: &mut Tokens) {
1178 tokens.append_all(self.attrs.outer());
1179 match self.node {
1180 TraitItemKind::Const(ref ty, ref expr) => {
1181 tokens.append("const");
1182 self.ident.to_tokens(tokens);
1183 tokens.append(":");
1184 ty.to_tokens(tokens);
1185 if let Some(ref expr) = *expr {
1186 tokens.append("=");
1187 expr.to_tokens(tokens);
1188 }
1189 tokens.append(";");
1190 }
1191 TraitItemKind::Method(ref sig, ref block) => {
1192 sig.unsafety.to_tokens(tokens);
1193 sig.abi.to_tokens(tokens);
1194 tokens.append("fn");
1195 self.ident.to_tokens(tokens);
1196 sig.generics.to_tokens(tokens);
1197 tokens.append("(");
1198 tokens.append_separated(&sig.decl.inputs, ",");
1199 tokens.append(")");
1200 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1201 tokens.append("->");
1202 ty.to_tokens(tokens);
1203 }
1204 sig.generics.where_clause.to_tokens(tokens);
1205 match *block {
1206 Some(ref block) => block.to_tokens(tokens),
1207 None => tokens.append(";"),
1208 }
1209 }
1210 TraitItemKind::Type(ref bound, ref default) => {
1211 tokens.append("type");
1212 self.ident.to_tokens(tokens);
1213 if !bound.is_empty() {
1214 tokens.append(":");
1215 tokens.append_separated(bound, "+");
1216 }
1217 if let Some(ref default) = *default {
1218 tokens.append("=");
1219 default.to_tokens(tokens);
1220 }
1221 tokens.append(";");
1222 }
1223 TraitItemKind::Macro(ref mac) => {
1224 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001225 match mac.tts.last() {
1226 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1227 // no semicolon
1228 }
1229 _ => tokens.append(";"),
1230 }
David Tolnayca085422016-10-04 00:12:38 -07001231 }
1232 }
1233 }
1234 }
1235
David Tolnay4c9be372016-10-06 00:47:37 -07001236 impl ToTokens for ImplItem {
1237 fn to_tokens(&self, tokens: &mut Tokens) {
1238 tokens.append_all(self.attrs.outer());
1239 match self.node {
1240 ImplItemKind::Const(ref ty, ref expr) => {
1241 self.vis.to_tokens(tokens);
1242 self.defaultness.to_tokens(tokens);
1243 tokens.append("const");
1244 self.ident.to_tokens(tokens);
1245 tokens.append(":");
1246 ty.to_tokens(tokens);
1247 tokens.append("=");
1248 expr.to_tokens(tokens);
1249 tokens.append(";");
1250 }
1251 ImplItemKind::Method(ref sig, ref block) => {
1252 self.vis.to_tokens(tokens);
1253 self.defaultness.to_tokens(tokens);
1254 sig.unsafety.to_tokens(tokens);
1255 sig.abi.to_tokens(tokens);
1256 tokens.append("fn");
1257 self.ident.to_tokens(tokens);
1258 sig.generics.to_tokens(tokens);
1259 tokens.append("(");
1260 tokens.append_separated(&sig.decl.inputs, ",");
1261 tokens.append(")");
1262 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1263 tokens.append("->");
1264 ty.to_tokens(tokens);
1265 }
1266 sig.generics.where_clause.to_tokens(tokens);
1267 block.to_tokens(tokens);
1268 }
1269 ImplItemKind::Type(ref ty) => {
1270 self.vis.to_tokens(tokens);
1271 self.defaultness.to_tokens(tokens);
1272 tokens.append("type");
1273 self.ident.to_tokens(tokens);
1274 tokens.append("=");
1275 ty.to_tokens(tokens);
1276 tokens.append(";");
1277 }
1278 ImplItemKind::Macro(ref mac) => {
1279 mac.to_tokens(tokens);
1280 match mac.tts.last() {
1281 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1282 // no semicolon
1283 }
1284 _ => tokens.append(";"),
1285 }
1286 }
1287 }
1288 }
1289 }
1290
David Tolnay35902302016-10-06 01:11:08 -07001291 impl ToTokens for ForeignItem {
1292 fn to_tokens(&self, tokens: &mut Tokens) {
1293 tokens.append_all(self.attrs.outer());
1294 match self.node {
1295 ForeignItemKind::Fn(ref decl, ref generics) => {
1296 self.vis.to_tokens(tokens);
1297 tokens.append("fn");
1298 self.ident.to_tokens(tokens);
1299 generics.to_tokens(tokens);
1300 tokens.append("(");
1301 tokens.append_separated(&decl.inputs, ",");
1302 tokens.append(")");
1303 if let FunctionRetTy::Ty(ref ty) = decl.output {
1304 tokens.append("->");
1305 ty.to_tokens(tokens);
1306 }
1307 generics.where_clause.to_tokens(tokens);
1308 tokens.append(";");
1309 }
1310 ForeignItemKind::Static(ref ty, mutability) => {
1311 self.vis.to_tokens(tokens);
1312 tokens.append("static");
1313 mutability.to_tokens(tokens);
1314 self.ident.to_tokens(tokens);
1315 tokens.append(":");
1316 ty.to_tokens(tokens);
1317 tokens.append(";");
1318 }
1319 }
1320 }
1321 }
1322
David Tolnay62f374c2016-10-02 13:37:00 -07001323 impl ToTokens for FnArg {
1324 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001325 match *self {
1326 FnArg::SelfRef(ref lifetime, mutability) => {
1327 tokens.append("&");
1328 lifetime.to_tokens(tokens);
1329 mutability.to_tokens(tokens);
1330 tokens.append("self");
1331 }
1332 FnArg::SelfValue(mutability) => {
1333 mutability.to_tokens(tokens);
1334 tokens.append("self");
1335 }
1336 FnArg::Captured(ref pat, ref ty) => {
1337 pat.to_tokens(tokens);
1338 tokens.append(":");
1339 ty.to_tokens(tokens);
1340 }
1341 FnArg::Ignored(ref ty) => {
1342 ty.to_tokens(tokens);
1343 }
1344 }
David Tolnay62f374c2016-10-02 13:37:00 -07001345 }
1346 }
1347
David Tolnay42602292016-10-01 22:25:45 -07001348 impl ToTokens for Unsafety {
1349 fn to_tokens(&self, tokens: &mut Tokens) {
1350 match *self {
1351 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001352 Unsafety::Normal => {
1353 // nothing
1354 }
David Tolnay42602292016-10-01 22:25:45 -07001355 }
1356 }
1357 }
1358
1359 impl ToTokens for Constness {
1360 fn to_tokens(&self, tokens: &mut Tokens) {
1361 match *self {
1362 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001363 Constness::NotConst => {
1364 // nothing
1365 }
David Tolnay42602292016-10-01 22:25:45 -07001366 }
1367 }
1368 }
1369
David Tolnay4c9be372016-10-06 00:47:37 -07001370 impl ToTokens for Defaultness {
1371 fn to_tokens(&self, tokens: &mut Tokens) {
1372 match *self {
1373 Defaultness::Default => tokens.append("default"),
1374 Defaultness::Final => {
1375 // nothing
1376 }
1377 }
1378 }
1379 }
1380
1381 impl ToTokens for ImplPolarity {
1382 fn to_tokens(&self, tokens: &mut Tokens) {
1383 match *self {
1384 ImplPolarity::Negative => tokens.append("!"),
1385 ImplPolarity::Positive => {
1386 // nothing
1387 }
1388 }
1389 }
1390 }
1391
David Tolnay42602292016-10-01 22:25:45 -07001392 impl ToTokens for Abi {
1393 fn to_tokens(&self, tokens: &mut Tokens) {
1394 tokens.append("extern");
1395 self.0.to_tokens(tokens);
1396 }
1397 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001398}