blob: 0e1e6d6a69b569176600a268c35a634228ddc60e [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 { .. }`
39 Mod(Vec<Item>),
40 /// 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 Tolnayaed77b02016-09-23 20:50:31 -070090 Simple(Ident, Path),
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///
129/// E.g. `extern { .. }` or `extern C { .. }`
130#[derive(Debug, Clone, Eq, PartialEq)]
131pub struct ForeignMod {
132 pub abi: Abi,
133 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),
149 /// A foreign static item (`static ext: u8`), with optional mutability
150 /// (the boolean is true when mutable)
151 Static(Box<Ty>, bool),
152}
153
154/// Represents an item declaration within a trait declaration,
155/// possibly including a default implementation. A trait item is
156/// either required (meaning it doesn't have an implementation, just a
157/// signature) or provided (meaning it has a default implementation).
158#[derive(Debug, Clone, Eq, PartialEq)]
159pub struct TraitItem {
160 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700161 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700162 pub node: TraitItemKind,
163}
164
165#[derive(Debug, Clone, Eq, PartialEq)]
166pub enum TraitItemKind {
167 Const(Ty, Option<Expr>),
168 Method(MethodSig, Option<Block>),
169 Type(Vec<TyParamBound>, Option<Ty>),
170 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700171}
172
David Tolnay55337722016-09-11 12:58:56 -0700173#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700174pub enum ImplPolarity {
175 /// `impl Trait for Type`
176 Positive,
177 /// `impl !Trait for Type`
178 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700179}
180
David Tolnayf38cdf62016-09-23 19:07:09 -0700181#[derive(Debug, Clone, Eq, PartialEq)]
182pub struct ImplItem {
183 pub ident: Ident,
184 pub vis: Visibility,
185 pub defaultness: Defaultness,
186 pub attrs: Vec<Attribute>,
187 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700188}
189
David Tolnayf38cdf62016-09-23 19:07:09 -0700190#[derive(Debug, Clone, Eq, PartialEq)]
191pub enum ImplItemKind {
192 Const(Ty, Expr),
193 Method(MethodSig, Block),
194 Type(Ty),
195 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700196}
David Tolnayd5025812016-09-04 14:21:46 -0700197
David Tolnayf38cdf62016-09-23 19:07:09 -0700198/// Represents a method's signature in a trait declaration,
199/// or in an implementation.
200#[derive(Debug, Clone, Eq, PartialEq)]
201pub struct MethodSig {
202 pub unsafety: Unsafety,
203 pub constness: Constness,
David Tolnay0aecb732016-10-03 23:03:50 -0700204 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700205 pub decl: FnDecl,
206 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700207}
David Tolnayedf2b992016-09-23 20:43:45 -0700208
David Tolnay62f374c2016-10-02 13:37:00 -0700209/// Header (not the body) of a function declaration.
210///
211/// E.g. `fn foo(bar: baz)`
212#[derive(Debug, Clone, Eq, PartialEq)]
213pub struct FnDecl {
214 pub inputs: Vec<FnArg>,
215 pub output: FunctionRetTy,
216}
217
218/// An argument in a function header.
219///
220/// E.g. `bar: usize` as in `fn foo(bar: usize)`
221#[derive(Debug, Clone, Eq, PartialEq)]
222pub struct FnArg {
223 pub pat: Pat,
224 pub ty: Ty,
225}
226
David Tolnayedf2b992016-09-23 20:43:45 -0700227#[cfg(feature = "parsing")]
228pub mod parsing {
229 use super::*;
David Tolnay2f9fa632016-10-03 22:08:48 -0700230 use {FunctionRetTy, Generics, Ident, Mac, TokenTree, VariantData, Visibility};
David Tolnay4a51dc72016-10-01 00:40:31 -0700231 use attr::parsing::outer_attr;
David Tolnay2f9fa632016-10-03 22:08:48 -0700232 use data::parsing::{struct_like_body, visibility};
David Tolnay62f374c2016-10-02 13:37:00 -0700233 use expr::parsing::{block, expr, pat};
David Tolnay0aecb732016-10-03 23:03:50 -0700234 use generics::parsing::{generics, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700235 use ident::parsing::ident;
David Tolnay42602292016-10-01 22:25:45 -0700236 use lit::parsing::quoted_string;
David Tolnay84aa0752016-10-02 23:01:13 -0700237 use mac::parsing::delimited;
David Tolnayedf2b992016-09-23 20:43:45 -0700238 use macro_input::{Body, MacroInput};
239 use macro_input::parsing::macro_input;
David Tolnay62f374c2016-10-02 13:37:00 -0700240 use ty::parsing::{mutability, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700241
242 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700243 item_extern_crate
David Tolnaydaaf7742016-10-03 11:11:43 -0700244 // TODO: Use
David Tolnay47a877c2016-10-01 16:50:55 -0700245 |
246 item_static
247 |
248 item_const
David Tolnay42602292016-10-01 22:25:45 -0700249 |
250 item_fn
David Tolnaydaaf7742016-10-03 11:11:43 -0700251 // TODO: Mod
252 // TODO: ForeignMod
David Tolnay3cf52982016-10-01 17:11:37 -0700253 |
254 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700255 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700256 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700257 |
258 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700259 |
260 item_trait
David Tolnaydaaf7742016-10-03 11:11:43 -0700261 // TODO: DefaultImpl
262 // TODO: Impl
David Tolnay84aa0752016-10-02 23:01:13 -0700263 |
264 item_mac
265 ));
266
267 named!(item_mac -> Item, do_parse!(
268 attrs: many0!(outer_attr) >>
269 path: ident >>
270 punct!("!") >>
271 name: option!(ident) >>
272 body: delimited >>
273 (Item {
274 ident: name.unwrap_or_else(|| Ident::new("")),
275 vis: Visibility::Inherited,
276 attrs: attrs,
277 node: ItemKind::Mac(Mac {
278 path: path.into(),
279 tts: vec![TokenTree::Delimited(body)],
280 }),
281 })
David Tolnayedf2b992016-09-23 20:43:45 -0700282 ));
283
David Tolnaya96a3fa2016-09-24 07:17:42 -0700284 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700285 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700286 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700287 keyword!("extern") >>
288 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700289 id: ident >>
290 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700291 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700292 ident
293 )) >>
294 punct!(";") >>
295 ({
296 let (name, original_name) = match rename {
297 Some(rename) => (rename, Some(id)),
298 None => (id, None),
299 };
300 Item {
301 ident: name,
302 vis: vis,
303 attrs: attrs,
304 node: ItemKind::ExternCrate(original_name),
305 }
306 })
307 ));
308
David Tolnay47a877c2016-10-01 16:50:55 -0700309 named!(item_static -> Item, do_parse!(
310 attrs: many0!(outer_attr) >>
311 vis: visibility >>
312 keyword!("static") >>
313 mutability: mutability >>
314 id: ident >>
315 punct!(":") >>
316 ty: ty >>
317 punct!("=") >>
318 value: expr >>
319 punct!(";") >>
320 (Item {
321 ident: id,
322 vis: vis,
323 attrs: attrs,
324 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
325 })
326 ));
327
328 named!(item_const -> Item, do_parse!(
329 attrs: many0!(outer_attr) >>
330 vis: visibility >>
331 keyword!("const") >>
332 id: ident >>
333 punct!(":") >>
334 ty: ty >>
335 punct!("=") >>
336 value: expr >>
337 punct!(";") >>
338 (Item {
339 ident: id,
340 vis: vis,
341 attrs: attrs,
342 node: ItemKind::Const(Box::new(ty), Box::new(value)),
343 })
344 ));
345
David Tolnay42602292016-10-01 22:25:45 -0700346 named!(item_fn -> Item, do_parse!(
347 attrs: many0!(outer_attr) >>
348 vis: visibility >>
349 constness: constness >>
350 unsafety: unsafety >>
351 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
352 keyword!("fn") >>
353 name: ident >>
354 generics: generics >>
355 punct!("(") >>
356 inputs: separated_list!(punct!(","), fn_arg) >>
357 punct!(")") >>
358 ret: option!(preceded!(punct!("->"), ty)) >>
359 where_clause: where_clause >>
360 body: block >>
361 (Item {
362 ident: name,
363 vis: vis,
364 attrs: attrs,
365 node: ItemKind::Fn(
366 Box::new(FnDecl {
367 inputs: inputs,
368 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
369 }),
370 unsafety,
371 constness,
372 abi.map(Abi),
373 Generics {
374 where_clause: where_clause,
375 .. generics
376 },
377 Box::new(body),
378 ),
379 })
380 ));
381
David Tolnay62f374c2016-10-02 13:37:00 -0700382 named!(fn_arg -> FnArg, do_parse!(
383 pat: pat >>
384 punct!(":") >>
385 ty: ty >>
386 (FnArg {
387 pat: pat,
388 ty: ty,
389 })
390 ));
391
David Tolnay3cf52982016-10-01 17:11:37 -0700392 named!(item_ty -> Item, do_parse!(
393 attrs: many0!(outer_attr) >>
394 vis: visibility >>
395 keyword!("type") >>
396 id: ident >>
397 generics: generics >>
398 punct!("=") >>
399 ty: ty >>
400 punct!(";") >>
401 (Item {
402 ident: id,
403 vis: vis,
404 attrs: attrs,
405 node: ItemKind::Ty(Box::new(ty), generics),
406 })
407 ));
408
David Tolnaya96a3fa2016-09-24 07:17:42 -0700409 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700410 macro_input,
411 |def: MacroInput| Item {
412 ident: def.ident,
413 vis: def.vis,
414 attrs: def.attrs,
415 node: match def.body {
416 Body::Enum(variants) => {
417 ItemKind::Enum(variants, def.generics)
418 }
419 Body::Struct(variant_data) => {
420 ItemKind::Struct(variant_data, def.generics)
421 }
422 }
423 }
424 ));
David Tolnay42602292016-10-01 22:25:45 -0700425
David Tolnay2f9fa632016-10-03 22:08:48 -0700426 named!(item_union -> Item, do_parse!(
427 attrs: many0!(outer_attr) >>
428 vis: visibility >>
429 keyword!("union") >>
430 id: ident >>
431 generics: generics >>
432 where_clause: where_clause >>
433 fields: struct_like_body >>
434 (Item {
435 ident: id,
436 vis: vis,
437 attrs: attrs,
438 node: ItemKind::Union(
439 VariantData::Struct(fields),
440 Generics {
441 where_clause: where_clause,
442 .. generics
443 },
444 ),
445 })
446 ));
447
David Tolnay0aecb732016-10-03 23:03:50 -0700448 named!(item_trait -> Item, do_parse!(
449 attrs: many0!(outer_attr) >>
450 vis: visibility >>
451 unsafety: unsafety >>
452 keyword!("trait") >>
453 id: ident >>
454 generics: generics >>
455 bounds: opt_vec!(preceded!(
456 punct!(":"),
457 separated_nonempty_list!(punct!("+"), ty_param_bound)
458 )) >>
459 where_clause: where_clause >>
460 punct!("{") >>
461 body: many0!(trait_item) >>
462 punct!("}") >>
463 (Item {
464 ident: id,
465 vis: vis,
466 attrs: attrs,
467 node: ItemKind::Trait(
468 unsafety,
469 Generics {
470 where_clause: where_clause,
471 .. generics
472 },
473 bounds,
474 body,
475 ),
476 })
477 ));
478
479 named!(trait_item -> TraitItem, alt!(
480 trait_item_const
481 |
482 trait_item_method
483 |
484 trait_item_type
485 |
486 trait_item_mac
487 ));
488
489 named!(trait_item_const -> TraitItem, do_parse!(
490 attrs: many0!(outer_attr) >>
491 keyword!("const") >>
492 id: ident >>
493 punct!(":") >>
494 ty: ty >>
495 value: option!(preceded!(punct!("="), expr)) >>
496 punct!(";") >>
497 (TraitItem {
498 ident: id,
499 attrs: attrs,
500 node: TraitItemKind::Const(ty, value),
501 })
502 ));
503
504 named!(trait_item_method -> TraitItem, do_parse!(
505 attrs: many0!(outer_attr) >>
506 constness: constness >>
507 unsafety: unsafety >>
508 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
509 keyword!("fn") >>
510 name: ident >>
511 generics: generics >>
512 punct!("(") >>
513 inputs: separated_list!(punct!(","), fn_arg) >>
514 punct!(")") >>
515 ret: option!(preceded!(punct!("->"), ty)) >>
516 where_clause: where_clause >>
517 body: option!(block) >>
518 cond!(body.is_none(), punct!(";")) >>
519 (TraitItem {
520 ident: name,
521 attrs: attrs,
522 node: TraitItemKind::Method(
523 MethodSig {
524 unsafety: unsafety,
525 constness: constness,
526 abi: abi.map(Abi),
527 decl: FnDecl {
528 inputs: inputs,
529 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
530 },
531 generics: Generics {
532 where_clause: where_clause,
533 .. generics
534 },
535 },
536 body,
537 ),
538 })
539 ));
540
541 named!(trait_item_type -> TraitItem, do_parse!(
542 attrs: many0!(outer_attr) >>
543 keyword!("type") >>
544 id: ident >>
545 bounds: opt_vec!(preceded!(
546 punct!(":"),
547 separated_nonempty_list!(punct!("+"), ty_param_bound)
548 )) >>
549 default: option!(preceded!(punct!("="), ty)) >>
550 (TraitItem {
551 ident: id,
552 attrs: attrs,
553 node: TraitItemKind::Type(bounds, default),
554 })
555 ));
556
557 named!(trait_item_mac -> TraitItem, do_parse!(
558 attrs: many0!(outer_attr) >>
559 id: ident >>
560 punct!("!") >>
561 body: delimited >>
562 (TraitItem {
563 ident: id.clone(),
564 attrs: attrs,
565 node: TraitItemKind::Macro(Mac {
566 path: id.into(),
567 tts: vec![TokenTree::Delimited(body)],
568 }),
569 })
570 ));
571
David Tolnay42602292016-10-01 22:25:45 -0700572 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700573 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700574 |
575 epsilon!() => { |_| Constness::NotConst }
576 ));
577
578 named!(unsafety -> Unsafety, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700579 keyword!("unsafe") => { |_| Unsafety::Unsafe }
David Tolnay42602292016-10-01 22:25:45 -0700580 |
581 epsilon!() => { |_| Unsafety::Normal }
582 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700583}
David Tolnay4a51dc72016-10-01 00:40:31 -0700584
585#[cfg(feature = "printing")]
586mod printing {
587 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700588 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700589 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700590 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700591 use quote::{Tokens, ToTokens};
592
593 impl ToTokens for Item {
594 fn to_tokens(&self, tokens: &mut Tokens) {
595 for attr in self.attrs.outer() {
596 attr.to_tokens(tokens);
597 }
598 match self.node {
599 ItemKind::ExternCrate(ref original) => {
600 tokens.append("extern");
601 tokens.append("crate");
602 if let Some(ref original) = *original {
603 original.to_tokens(tokens);
604 tokens.append("as");
605 }
606 self.ident.to_tokens(tokens);
607 tokens.append(";");
608 }
609 ItemKind::Use(ref _view_path) => unimplemented!(),
David Tolnay47a877c2016-10-01 16:50:55 -0700610 ItemKind::Static(ref ty, ref mutability, ref expr) => {
611 self.vis.to_tokens(tokens);
612 tokens.append("static");
613 mutability.to_tokens(tokens);
614 self.ident.to_tokens(tokens);
615 tokens.append(":");
616 ty.to_tokens(tokens);
617 tokens.append("=");
618 expr.to_tokens(tokens);
619 tokens.append(";");
620 }
621 ItemKind::Const(ref ty, ref expr) => {
622 self.vis.to_tokens(tokens);
623 tokens.append("const");
624 self.ident.to_tokens(tokens);
625 tokens.append(":");
626 ty.to_tokens(tokens);
627 tokens.append("=");
628 expr.to_tokens(tokens);
629 tokens.append(";");
630 }
David Tolnay42602292016-10-01 22:25:45 -0700631 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
632 self.vis.to_tokens(tokens);
633 constness.to_tokens(tokens);
634 unsafety.to_tokens(tokens);
635 abi.to_tokens(tokens);
636 tokens.append("fn");
637 self.ident.to_tokens(tokens);
638 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -0700639 tokens.append("(");
640 tokens.append_separated(&decl.inputs, ",");
641 tokens.append(")");
642 if let FunctionRetTy::Ty(ref ty) = decl.output {
643 tokens.append("->");
644 ty.to_tokens(tokens);
645 }
David Tolnay42602292016-10-01 22:25:45 -0700646 generics.where_clause.to_tokens(tokens);
647 block.to_tokens(tokens);
648 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700649 ItemKind::Mod(ref _items) => unimplemented!(),
650 ItemKind::ForeignMod(ref _foreign_mod) => unimplemented!(),
David Tolnay3cf52982016-10-01 17:11:37 -0700651 ItemKind::Ty(ref ty, ref generics) => {
652 self.vis.to_tokens(tokens);
653 tokens.append("type");
654 self.ident.to_tokens(tokens);
655 generics.to_tokens(tokens);
656 tokens.append("=");
657 ty.to_tokens(tokens);
658 tokens.append(";");
659 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700660 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700661 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700662 tokens.append("enum");
663 self.ident.to_tokens(tokens);
664 generics.to_tokens(tokens);
665 generics.where_clause.to_tokens(tokens);
666 tokens.append("{");
667 for variant in variants {
668 variant.to_tokens(tokens);
669 tokens.append(",");
670 }
671 tokens.append("}");
672 }
673 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700674 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700675 tokens.append("struct");
676 self.ident.to_tokens(tokens);
677 generics.to_tokens(tokens);
678 generics.where_clause.to_tokens(tokens);
679 variant_data.to_tokens(tokens);
680 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -0700681 VariantData::Struct(_) => {
682 // no semicolon
683 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700684 VariantData::Tuple(_) |
685 VariantData::Unit => tokens.append(";"),
686 }
687 }
David Tolnay2f9fa632016-10-03 22:08:48 -0700688 ItemKind::Union(ref variant_data, ref generics) => {
689 self.vis.to_tokens(tokens);
690 tokens.append("union");
691 self.ident.to_tokens(tokens);
692 generics.to_tokens(tokens);
693 generics.where_clause.to_tokens(tokens);
694 variant_data.to_tokens(tokens);
695 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700696 ItemKind::Trait(_unsafety, ref _generics, ref _bound, ref _item) => unimplemented!(),
697 ItemKind::DefaultImpl(_unsafety, ref _path) => unimplemented!(),
David Tolnaydaaf7742016-10-03 11:11:43 -0700698 ItemKind::Impl(_unsafety,
699 _polarity,
700 ref _generics,
701 ref _path,
702 ref _ty,
703 ref _item) => unimplemented!(),
David Tolnaycc3d66e2016-10-02 23:36:05 -0700704 ItemKind::Mac(ref mac) => {
705 mac.path.to_tokens(tokens);
706 tokens.append("!");
707 self.ident.to_tokens(tokens);
708 for tt in &mac.tts {
709 tt.to_tokens(tokens);
710 }
711 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -0700712 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
713 // no semicolon
714 }
David Tolnaycc3d66e2016-10-02 23:36:05 -0700715 _ => tokens.append(";"),
716 }
717 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700718 }
719 }
720 }
David Tolnay42602292016-10-01 22:25:45 -0700721
David Tolnay62f374c2016-10-02 13:37:00 -0700722 impl ToTokens for FnArg {
723 fn to_tokens(&self, tokens: &mut Tokens) {
724 self.pat.to_tokens(tokens);
725 tokens.append(":");
726 self.ty.to_tokens(tokens);
727 }
728 }
729
David Tolnay42602292016-10-01 22:25:45 -0700730 impl ToTokens for Unsafety {
731 fn to_tokens(&self, tokens: &mut Tokens) {
732 match *self {
733 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -0700734 Unsafety::Normal => {
735 // nothing
736 }
David Tolnay42602292016-10-01 22:25:45 -0700737 }
738 }
739 }
740
741 impl ToTokens for Constness {
742 fn to_tokens(&self, tokens: &mut Tokens) {
743 match *self {
744 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -0700745 Constness::NotConst => {
746 // nothing
747 }
David Tolnay42602292016-10-01 22:25:45 -0700748 }
749 }
750 }
751
752 impl ToTokens for Abi {
753 fn to_tokens(&self, tokens: &mut Tokens) {
754 tokens.append("extern");
755 self.0.to_tokens(tokens);
756 }
757 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700758}