blob: a8fb948598ef4a5e319a6028263b4763f012d0b6 [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 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 >>
489 punct!("{") >>
490 items: many0!(item) >>
491 punct!("}") >>
492 (Item {
493 ident: id,
494 vis: vis,
495 attrs: attrs,
496 node: ItemKind::Mod(items),
497 })
498 ));
499
500 named!(item_foreign_mod -> Item, do_parse!(
501 attrs: many0!(outer_attr) >>
502 keyword!("extern") >>
503 abi: option!(quoted_string) >>
504 punct!("{") >>
505 items: many0!(foreign_item) >>
506 punct!("}") >>
507 (Item {
508 ident: "".into(),
509 vis: Visibility::Inherited,
510 attrs: attrs,
511 node: ItemKind::ForeignMod(ForeignMod {
512 abi: abi.map(Abi),
513 items: items,
514 }),
515 })
516 ));
517
518 named!(foreign_item -> ForeignItem, alt!(
519 foreign_fn
520 |
521 foreign_static
522 ));
523
524 named!(foreign_fn -> ForeignItem, do_parse!(
525 attrs: many0!(outer_attr) >>
526 vis: visibility >>
527 keyword!("fn") >>
528 name: ident >>
529 generics: generics >>
530 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700531 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay35902302016-10-06 01:11:08 -0700532 punct!(")") >>
533 ret: option!(preceded!(punct!("->"), ty)) >>
534 where_clause: where_clause >>
535 punct!(";") >>
536 (ForeignItem {
537 ident: name,
538 attrs: attrs,
539 node: ForeignItemKind::Fn(
540 Box::new(FnDecl {
541 inputs: inputs,
542 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
543 }),
544 Generics {
545 where_clause: where_clause,
546 .. generics
547 },
548 ),
549 vis: vis,
550 })
551 ));
552
553 named!(foreign_static -> ForeignItem, do_parse!(
554 attrs: many0!(outer_attr) >>
555 vis: visibility >>
556 keyword!("static") >>
557 mutability: mutability >>
558 id: ident >>
559 punct!(":") >>
560 ty: ty >>
561 punct!(";") >>
562 (ForeignItem {
563 ident: id,
564 attrs: attrs,
565 node: ForeignItemKind::Static(Box::new(ty), mutability),
566 vis: vis,
567 })
568 ));
569
David Tolnay3cf52982016-10-01 17:11:37 -0700570 named!(item_ty -> Item, do_parse!(
571 attrs: many0!(outer_attr) >>
572 vis: visibility >>
573 keyword!("type") >>
574 id: ident >>
575 generics: generics >>
576 punct!("=") >>
577 ty: ty >>
578 punct!(";") >>
579 (Item {
580 ident: id,
581 vis: vis,
582 attrs: attrs,
583 node: ItemKind::Ty(Box::new(ty), generics),
584 })
585 ));
586
David Tolnaya96a3fa2016-09-24 07:17:42 -0700587 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700588 macro_input,
589 |def: MacroInput| Item {
590 ident: def.ident,
591 vis: def.vis,
592 attrs: def.attrs,
593 node: match def.body {
594 Body::Enum(variants) => {
595 ItemKind::Enum(variants, def.generics)
596 }
597 Body::Struct(variant_data) => {
598 ItemKind::Struct(variant_data, def.generics)
599 }
600 }
601 }
602 ));
David Tolnay42602292016-10-01 22:25:45 -0700603
David Tolnay2f9fa632016-10-03 22:08:48 -0700604 named!(item_union -> Item, do_parse!(
605 attrs: many0!(outer_attr) >>
606 vis: visibility >>
607 keyword!("union") >>
608 id: ident >>
609 generics: generics >>
610 where_clause: where_clause >>
611 fields: struct_like_body >>
612 (Item {
613 ident: id,
614 vis: vis,
615 attrs: attrs,
616 node: ItemKind::Union(
617 VariantData::Struct(fields),
618 Generics {
619 where_clause: where_clause,
620 .. generics
621 },
622 ),
623 })
624 ));
625
David Tolnay0aecb732016-10-03 23:03:50 -0700626 named!(item_trait -> Item, do_parse!(
627 attrs: many0!(outer_attr) >>
628 vis: visibility >>
629 unsafety: unsafety >>
630 keyword!("trait") >>
631 id: ident >>
632 generics: generics >>
633 bounds: opt_vec!(preceded!(
634 punct!(":"),
635 separated_nonempty_list!(punct!("+"), ty_param_bound)
636 )) >>
637 where_clause: where_clause >>
638 punct!("{") >>
639 body: many0!(trait_item) >>
640 punct!("}") >>
641 (Item {
642 ident: id,
643 vis: vis,
644 attrs: attrs,
645 node: ItemKind::Trait(
646 unsafety,
647 Generics {
648 where_clause: where_clause,
649 .. generics
650 },
651 bounds,
652 body,
653 ),
654 })
655 ));
656
David Tolnayf94e2362016-10-04 00:29:51 -0700657 named!(item_default_impl -> Item, do_parse!(
658 attrs: many0!(outer_attr) >>
659 unsafety: unsafety >>
660 keyword!("impl") >>
661 path: path >>
662 keyword!("for") >>
663 punct!("..") >>
664 punct!("{") >>
665 punct!("}") >>
666 (Item {
667 ident: "".into(),
668 vis: Visibility::Inherited,
669 attrs: attrs,
670 node: ItemKind::DefaultImpl(unsafety, path),
671 })
672 ));
673
David Tolnay0aecb732016-10-03 23:03:50 -0700674 named!(trait_item -> TraitItem, alt!(
675 trait_item_const
676 |
677 trait_item_method
678 |
679 trait_item_type
680 |
681 trait_item_mac
682 ));
683
684 named!(trait_item_const -> TraitItem, do_parse!(
685 attrs: many0!(outer_attr) >>
686 keyword!("const") >>
687 id: ident >>
688 punct!(":") >>
689 ty: ty >>
690 value: option!(preceded!(punct!("="), expr)) >>
691 punct!(";") >>
692 (TraitItem {
693 ident: id,
694 attrs: attrs,
695 node: TraitItemKind::Const(ty, value),
696 })
697 ));
698
699 named!(trait_item_method -> TraitItem, do_parse!(
700 attrs: many0!(outer_attr) >>
701 constness: constness >>
702 unsafety: unsafety >>
703 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
704 keyword!("fn") >>
705 name: ident >>
706 generics: generics >>
707 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700708 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700709 punct!(")") >>
710 ret: option!(preceded!(punct!("->"), ty)) >>
711 where_clause: where_clause >>
712 body: option!(block) >>
713 cond!(body.is_none(), punct!(";")) >>
714 (TraitItem {
715 ident: name,
716 attrs: attrs,
717 node: TraitItemKind::Method(
718 MethodSig {
719 unsafety: unsafety,
720 constness: constness,
721 abi: abi.map(Abi),
722 decl: FnDecl {
723 inputs: inputs,
724 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
725 },
726 generics: Generics {
727 where_clause: where_clause,
728 .. generics
729 },
730 },
731 body,
732 ),
733 })
734 ));
735
736 named!(trait_item_type -> TraitItem, do_parse!(
737 attrs: many0!(outer_attr) >>
738 keyword!("type") >>
739 id: ident >>
740 bounds: opt_vec!(preceded!(
741 punct!(":"),
742 separated_nonempty_list!(punct!("+"), ty_param_bound)
743 )) >>
744 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700745 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700746 (TraitItem {
747 ident: id,
748 attrs: attrs,
749 node: TraitItemKind::Type(bounds, default),
750 })
751 ));
752
753 named!(trait_item_mac -> TraitItem, do_parse!(
754 attrs: many0!(outer_attr) >>
755 id: ident >>
756 punct!("!") >>
757 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700758 cond!(match body.delim {
759 DelimToken::Paren | DelimToken::Bracket => true,
760 DelimToken::Brace => false,
761 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700762 (TraitItem {
763 ident: id.clone(),
764 attrs: attrs,
765 node: TraitItemKind::Macro(Mac {
766 path: id.into(),
767 tts: vec![TokenTree::Delimited(body)],
768 }),
769 })
770 ));
771
David Tolnay4c9be372016-10-06 00:47:37 -0700772 named!(item_impl -> Item, do_parse!(
773 attrs: many0!(outer_attr) >>
774 unsafety: unsafety >>
775 keyword!("impl") >>
776 generics: generics >>
777 polarity_path: alt!(
778 do_parse!(
779 polarity: impl_polarity >>
780 path: path >>
781 keyword!("for") >>
782 ((polarity, Some(path)))
783 )
784 |
785 epsilon!() => { |_| (ImplPolarity::Positive, None) }
786 ) >>
787 self_ty: ty >>
788 where_clause: where_clause >>
789 punct!("{") >>
790 body: many0!(impl_item) >>
791 punct!("}") >>
792 (Item {
793 ident: "".into(),
794 vis: Visibility::Inherited,
795 attrs: attrs,
796 node: ItemKind::Impl(
797 unsafety,
798 polarity_path.0,
799 Generics {
800 where_clause: where_clause,
801 .. generics
802 },
803 polarity_path.1,
804 Box::new(self_ty),
805 body,
806 ),
807 })
808 ));
809
810 named!(impl_item -> ImplItem, alt!(
811 impl_item_const
812 |
813 impl_item_method
814 |
815 impl_item_type
816 |
817 impl_item_macro
818 ));
819
820 named!(impl_item_const -> ImplItem, do_parse!(
821 attrs: many0!(outer_attr) >>
822 vis: visibility >>
823 defaultness: defaultness >>
824 keyword!("const") >>
825 id: ident >>
826 punct!(":") >>
827 ty: ty >>
828 punct!("=") >>
829 value: expr >>
830 punct!(";") >>
831 (ImplItem {
832 ident: id,
833 vis: vis,
834 defaultness: defaultness,
835 attrs: attrs,
836 node: ImplItemKind::Const(ty, value),
837 })
838 ));
839
840 named!(impl_item_method -> ImplItem, do_parse!(
841 attrs: many0!(outer_attr) >>
842 vis: visibility >>
843 defaultness: defaultness >>
844 constness: constness >>
845 unsafety: unsafety >>
846 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
847 keyword!("fn") >>
848 name: ident >>
849 generics: generics >>
850 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700851 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700852 punct!(")") >>
853 ret: option!(preceded!(punct!("->"), ty)) >>
854 where_clause: where_clause >>
855 body: block >>
856 (ImplItem {
857 ident: name,
858 vis: vis,
859 defaultness: defaultness,
860 attrs: attrs,
861 node: ImplItemKind::Method(
862 MethodSig {
863 unsafety: unsafety,
864 constness: constness,
865 abi: abi.map(Abi),
866 decl: FnDecl {
867 inputs: inputs,
868 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
869 },
870 generics: Generics {
871 where_clause: where_clause,
872 .. generics
873 },
874 },
875 body,
876 ),
877 })
878 ));
879
880 named!(impl_item_type -> ImplItem, do_parse!(
881 attrs: many0!(outer_attr) >>
882 vis: visibility >>
883 defaultness: defaultness >>
884 keyword!("type") >>
885 id: ident >>
886 punct!("=") >>
887 ty: ty >>
888 punct!(";") >>
889 (ImplItem {
890 ident: id,
891 vis: vis,
892 defaultness: defaultness,
893 attrs: attrs,
894 node: ImplItemKind::Type(ty),
895 })
896 ));
897
898 named!(impl_item_macro -> ImplItem, do_parse!(
899 attrs: many0!(outer_attr) >>
900 id: ident >>
901 punct!("!") >>
902 body: delimited >>
903 cond!(match body.delim {
904 DelimToken::Paren | DelimToken::Bracket => true,
905 DelimToken::Brace => false,
906 }, punct!(";")) >>
907 (ImplItem {
908 ident: id.clone(),
909 vis: Visibility::Inherited,
910 defaultness: Defaultness::Final,
911 attrs: attrs,
912 node: ImplItemKind::Macro(Mac {
913 path: id.into(),
914 tts: vec![TokenTree::Delimited(body)],
915 }),
916 })
917 ));
918
919 named!(impl_polarity -> ImplPolarity, alt!(
920 punct!("!") => { |_| ImplPolarity::Negative }
921 |
922 epsilon!() => { |_| ImplPolarity::Positive }
923 ));
924
David Tolnay42602292016-10-01 22:25:45 -0700925 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700926 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700927 |
928 epsilon!() => { |_| Constness::NotConst }
929 ));
930
931 named!(unsafety -> Unsafety, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700932 keyword!("unsafe") => { |_| Unsafety::Unsafe }
David Tolnay42602292016-10-01 22:25:45 -0700933 |
934 epsilon!() => { |_| Unsafety::Normal }
935 ));
David Tolnay4c9be372016-10-06 00:47:37 -0700936
937 named!(defaultness -> Defaultness, alt!(
938 keyword!("default") => { |_| Defaultness::Default }
939 |
940 epsilon!() => { |_| Defaultness::Final }
941 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700942}
David Tolnay4a51dc72016-10-01 00:40:31 -0700943
944#[cfg(feature = "printing")]
945mod printing {
946 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700947 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700948 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700949 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700950 use quote::{Tokens, ToTokens};
951
952 impl ToTokens for Item {
953 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -0700954 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -0700955 match self.node {
956 ItemKind::ExternCrate(ref original) => {
957 tokens.append("extern");
958 tokens.append("crate");
959 if let Some(ref original) = *original {
960 original.to_tokens(tokens);
961 tokens.append("as");
962 }
963 self.ident.to_tokens(tokens);
964 tokens.append(";");
965 }
David Tolnay4a057422016-10-08 00:02:31 -0700966 ItemKind::Use(ref view_path) => {
967 self.vis.to_tokens(tokens);
968 tokens.append("use");
969 view_path.to_tokens(tokens);
970 tokens.append(";");
971 }
David Tolnay47a877c2016-10-01 16:50:55 -0700972 ItemKind::Static(ref ty, ref mutability, ref expr) => {
973 self.vis.to_tokens(tokens);
974 tokens.append("static");
975 mutability.to_tokens(tokens);
976 self.ident.to_tokens(tokens);
977 tokens.append(":");
978 ty.to_tokens(tokens);
979 tokens.append("=");
980 expr.to_tokens(tokens);
981 tokens.append(";");
982 }
983 ItemKind::Const(ref ty, ref expr) => {
984 self.vis.to_tokens(tokens);
985 tokens.append("const");
986 self.ident.to_tokens(tokens);
987 tokens.append(":");
988 ty.to_tokens(tokens);
989 tokens.append("=");
990 expr.to_tokens(tokens);
991 tokens.append(";");
992 }
David Tolnay42602292016-10-01 22:25:45 -0700993 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
994 self.vis.to_tokens(tokens);
995 constness.to_tokens(tokens);
996 unsafety.to_tokens(tokens);
997 abi.to_tokens(tokens);
998 tokens.append("fn");
999 self.ident.to_tokens(tokens);
1000 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001001 tokens.append("(");
1002 tokens.append_separated(&decl.inputs, ",");
1003 tokens.append(")");
1004 if let FunctionRetTy::Ty(ref ty) = decl.output {
1005 tokens.append("->");
1006 ty.to_tokens(tokens);
1007 }
David Tolnay42602292016-10-01 22:25:45 -07001008 generics.where_clause.to_tokens(tokens);
1009 block.to_tokens(tokens);
1010 }
David Tolnay35902302016-10-06 01:11:08 -07001011 ItemKind::Mod(ref items) => {
1012 self.vis.to_tokens(tokens);
1013 tokens.append("mod");
1014 self.ident.to_tokens(tokens);
1015 tokens.append("{");
1016 tokens.append_all(items);
1017 tokens.append("}");
1018 }
1019 ItemKind::ForeignMod(ref foreign_mod) => {
1020 self.vis.to_tokens(tokens);
1021 match foreign_mod.abi {
1022 Some(ref abi) => abi.to_tokens(tokens),
1023 None => tokens.append("extern"),
1024 }
1025 tokens.append("{");
1026 tokens.append_all(&foreign_mod.items);
1027 tokens.append("}");
1028 }
David Tolnay3cf52982016-10-01 17:11:37 -07001029 ItemKind::Ty(ref ty, ref generics) => {
1030 self.vis.to_tokens(tokens);
1031 tokens.append("type");
1032 self.ident.to_tokens(tokens);
1033 generics.to_tokens(tokens);
1034 tokens.append("=");
1035 ty.to_tokens(tokens);
1036 tokens.append(";");
1037 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001038 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001039 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001040 tokens.append("enum");
1041 self.ident.to_tokens(tokens);
1042 generics.to_tokens(tokens);
1043 generics.where_clause.to_tokens(tokens);
1044 tokens.append("{");
1045 for variant in variants {
1046 variant.to_tokens(tokens);
1047 tokens.append(",");
1048 }
1049 tokens.append("}");
1050 }
1051 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001052 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001053 tokens.append("struct");
1054 self.ident.to_tokens(tokens);
1055 generics.to_tokens(tokens);
1056 generics.where_clause.to_tokens(tokens);
1057 variant_data.to_tokens(tokens);
1058 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001059 VariantData::Struct(_) => {
1060 // no semicolon
1061 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001062 VariantData::Tuple(_) |
1063 VariantData::Unit => tokens.append(";"),
1064 }
1065 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001066 ItemKind::Union(ref variant_data, ref generics) => {
1067 self.vis.to_tokens(tokens);
1068 tokens.append("union");
1069 self.ident.to_tokens(tokens);
1070 generics.to_tokens(tokens);
1071 generics.where_clause.to_tokens(tokens);
1072 variant_data.to_tokens(tokens);
1073 }
David Tolnayca085422016-10-04 00:12:38 -07001074 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1075 self.vis.to_tokens(tokens);
1076 unsafety.to_tokens(tokens);
1077 tokens.append("trait");
1078 self.ident.to_tokens(tokens);
1079 if !bound.is_empty() {
1080 tokens.append(":");
1081 tokens.append_separated(bound, "+");
1082 }
1083 generics.to_tokens(tokens);
1084 generics.where_clause.to_tokens(tokens);
1085 tokens.append("{");
1086 tokens.append_all(items);
1087 tokens.append("}");
1088 }
David Tolnayf94e2362016-10-04 00:29:51 -07001089 ItemKind::DefaultImpl(unsafety, ref path) => {
1090 unsafety.to_tokens(tokens);
1091 tokens.append("impl");
1092 path.to_tokens(tokens);
1093 tokens.append("for");
1094 tokens.append("..");
1095 tokens.append("{");
1096 tokens.append("}");
1097 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001098 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001099 unsafety.to_tokens(tokens);
1100 tokens.append("impl");
1101 generics.to_tokens(tokens);
1102 if let Some(ref path) = *path {
1103 polarity.to_tokens(tokens);
1104 path.to_tokens(tokens);
1105 tokens.append("for");
1106 }
1107 ty.to_tokens(tokens);
1108 generics.where_clause.to_tokens(tokens);
1109 tokens.append("{");
1110 tokens.append_all(items);
1111 tokens.append("}");
1112 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001113 ItemKind::Mac(ref mac) => {
1114 mac.path.to_tokens(tokens);
1115 tokens.append("!");
1116 self.ident.to_tokens(tokens);
1117 for tt in &mac.tts {
1118 tt.to_tokens(tokens);
1119 }
1120 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001121 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1122 // no semicolon
1123 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001124 _ => tokens.append(";"),
1125 }
1126 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001127 }
1128 }
1129 }
David Tolnay42602292016-10-01 22:25:45 -07001130
David Tolnay4a057422016-10-08 00:02:31 -07001131 impl ToTokens for ViewPath {
1132 fn to_tokens(&self, tokens: &mut Tokens) {
1133 match *self {
1134 ViewPath::Simple(ref path, ref rename) => {
1135 path.to_tokens(tokens);
1136 if let Some(ref rename) = *rename {
1137 tokens.append("as");
1138 rename.to_tokens(tokens);
1139 }
1140 }
1141 ViewPath::Glob(ref path) => {
1142 path.to_tokens(tokens);
1143 tokens.append("::");
1144 tokens.append("*");
1145 }
1146 ViewPath::List(ref path, ref items) => {
1147 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001148 if path.global || !path.segments.is_empty() {
1149 tokens.append("::");
1150 }
David Tolnay4a057422016-10-08 00:02:31 -07001151 tokens.append("{");
1152 tokens.append_separated(items, ",");
1153 tokens.append("}");
1154 }
1155 }
1156 }
1157 }
1158
1159 impl ToTokens for PathListItem {
1160 fn to_tokens(&self, tokens: &mut Tokens) {
1161 self.name.to_tokens(tokens);
1162 if let Some(ref rename) = self.rename {
1163 tokens.append("as");
1164 rename.to_tokens(tokens);
1165 }
1166 }
1167 }
1168
David Tolnayca085422016-10-04 00:12:38 -07001169 impl ToTokens for TraitItem {
1170 fn to_tokens(&self, tokens: &mut Tokens) {
1171 tokens.append_all(self.attrs.outer());
1172 match self.node {
1173 TraitItemKind::Const(ref ty, ref expr) => {
1174 tokens.append("const");
1175 self.ident.to_tokens(tokens);
1176 tokens.append(":");
1177 ty.to_tokens(tokens);
1178 if let Some(ref expr) = *expr {
1179 tokens.append("=");
1180 expr.to_tokens(tokens);
1181 }
1182 tokens.append(";");
1183 }
1184 TraitItemKind::Method(ref sig, ref block) => {
1185 sig.unsafety.to_tokens(tokens);
1186 sig.abi.to_tokens(tokens);
1187 tokens.append("fn");
1188 self.ident.to_tokens(tokens);
1189 sig.generics.to_tokens(tokens);
1190 tokens.append("(");
1191 tokens.append_separated(&sig.decl.inputs, ",");
1192 tokens.append(")");
1193 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1194 tokens.append("->");
1195 ty.to_tokens(tokens);
1196 }
1197 sig.generics.where_clause.to_tokens(tokens);
1198 match *block {
1199 Some(ref block) => block.to_tokens(tokens),
1200 None => tokens.append(";"),
1201 }
1202 }
1203 TraitItemKind::Type(ref bound, ref default) => {
1204 tokens.append("type");
1205 self.ident.to_tokens(tokens);
1206 if !bound.is_empty() {
1207 tokens.append(":");
1208 tokens.append_separated(bound, "+");
1209 }
1210 if let Some(ref default) = *default {
1211 tokens.append("=");
1212 default.to_tokens(tokens);
1213 }
1214 tokens.append(";");
1215 }
1216 TraitItemKind::Macro(ref mac) => {
1217 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001218 match mac.tts.last() {
1219 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1220 // no semicolon
1221 }
1222 _ => tokens.append(";"),
1223 }
David Tolnayca085422016-10-04 00:12:38 -07001224 }
1225 }
1226 }
1227 }
1228
David Tolnay4c9be372016-10-06 00:47:37 -07001229 impl ToTokens for ImplItem {
1230 fn to_tokens(&self, tokens: &mut Tokens) {
1231 tokens.append_all(self.attrs.outer());
1232 match self.node {
1233 ImplItemKind::Const(ref ty, ref expr) => {
1234 self.vis.to_tokens(tokens);
1235 self.defaultness.to_tokens(tokens);
1236 tokens.append("const");
1237 self.ident.to_tokens(tokens);
1238 tokens.append(":");
1239 ty.to_tokens(tokens);
1240 tokens.append("=");
1241 expr.to_tokens(tokens);
1242 tokens.append(";");
1243 }
1244 ImplItemKind::Method(ref sig, ref block) => {
1245 self.vis.to_tokens(tokens);
1246 self.defaultness.to_tokens(tokens);
1247 sig.unsafety.to_tokens(tokens);
1248 sig.abi.to_tokens(tokens);
1249 tokens.append("fn");
1250 self.ident.to_tokens(tokens);
1251 sig.generics.to_tokens(tokens);
1252 tokens.append("(");
1253 tokens.append_separated(&sig.decl.inputs, ",");
1254 tokens.append(")");
1255 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1256 tokens.append("->");
1257 ty.to_tokens(tokens);
1258 }
1259 sig.generics.where_clause.to_tokens(tokens);
1260 block.to_tokens(tokens);
1261 }
1262 ImplItemKind::Type(ref ty) => {
1263 self.vis.to_tokens(tokens);
1264 self.defaultness.to_tokens(tokens);
1265 tokens.append("type");
1266 self.ident.to_tokens(tokens);
1267 tokens.append("=");
1268 ty.to_tokens(tokens);
1269 tokens.append(";");
1270 }
1271 ImplItemKind::Macro(ref mac) => {
1272 mac.to_tokens(tokens);
1273 match mac.tts.last() {
1274 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1275 // no semicolon
1276 }
1277 _ => tokens.append(";"),
1278 }
1279 }
1280 }
1281 }
1282 }
1283
David Tolnay35902302016-10-06 01:11:08 -07001284 impl ToTokens for ForeignItem {
1285 fn to_tokens(&self, tokens: &mut Tokens) {
1286 tokens.append_all(self.attrs.outer());
1287 match self.node {
1288 ForeignItemKind::Fn(ref decl, ref generics) => {
1289 self.vis.to_tokens(tokens);
1290 tokens.append("fn");
1291 self.ident.to_tokens(tokens);
1292 generics.to_tokens(tokens);
1293 tokens.append("(");
1294 tokens.append_separated(&decl.inputs, ",");
1295 tokens.append(")");
1296 if let FunctionRetTy::Ty(ref ty) = decl.output {
1297 tokens.append("->");
1298 ty.to_tokens(tokens);
1299 }
1300 generics.where_clause.to_tokens(tokens);
1301 tokens.append(";");
1302 }
1303 ForeignItemKind::Static(ref ty, mutability) => {
1304 self.vis.to_tokens(tokens);
1305 tokens.append("static");
1306 mutability.to_tokens(tokens);
1307 self.ident.to_tokens(tokens);
1308 tokens.append(":");
1309 ty.to_tokens(tokens);
1310 tokens.append(";");
1311 }
1312 }
1313 }
1314 }
1315
David Tolnay62f374c2016-10-02 13:37:00 -07001316 impl ToTokens for FnArg {
1317 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001318 match *self {
1319 FnArg::SelfRef(ref lifetime, mutability) => {
1320 tokens.append("&");
1321 lifetime.to_tokens(tokens);
1322 mutability.to_tokens(tokens);
1323 tokens.append("self");
1324 }
1325 FnArg::SelfValue(mutability) => {
1326 mutability.to_tokens(tokens);
1327 tokens.append("self");
1328 }
1329 FnArg::Captured(ref pat, ref ty) => {
1330 pat.to_tokens(tokens);
1331 tokens.append(":");
1332 ty.to_tokens(tokens);
1333 }
1334 FnArg::Ignored(ref ty) => {
1335 ty.to_tokens(tokens);
1336 }
1337 }
David Tolnay62f374c2016-10-02 13:37:00 -07001338 }
1339 }
1340
David Tolnay42602292016-10-01 22:25:45 -07001341 impl ToTokens for Unsafety {
1342 fn to_tokens(&self, tokens: &mut Tokens) {
1343 match *self {
1344 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001345 Unsafety::Normal => {
1346 // nothing
1347 }
David Tolnay42602292016-10-01 22:25:45 -07001348 }
1349 }
1350 }
1351
1352 impl ToTokens for Constness {
1353 fn to_tokens(&self, tokens: &mut Tokens) {
1354 match *self {
1355 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001356 Constness::NotConst => {
1357 // nothing
1358 }
David Tolnay42602292016-10-01 22:25:45 -07001359 }
1360 }
1361 }
1362
David Tolnay4c9be372016-10-06 00:47:37 -07001363 impl ToTokens for Defaultness {
1364 fn to_tokens(&self, tokens: &mut Tokens) {
1365 match *self {
1366 Defaultness::Default => tokens.append("default"),
1367 Defaultness::Final => {
1368 // nothing
1369 }
1370 }
1371 }
1372 }
1373
1374 impl ToTokens for ImplPolarity {
1375 fn to_tokens(&self, tokens: &mut Tokens) {
1376 match *self {
1377 ImplPolarity::Negative => tokens.append("!"),
1378 ImplPolarity::Positive => {
1379 // nothing
1380 }
1381 }
1382 }
1383 }
1384
David Tolnay42602292016-10-01 22:25:45 -07001385 impl ToTokens for Abi {
1386 fn to_tokens(&self, tokens: &mut Tokens) {
1387 tokens.append("extern");
1388 self.0.to_tokens(tokens);
1389 }
1390 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001391}