blob: 16f7f309be8fa310a658fc043057b11f620f025c [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 >>
279 (Item {
280 ident: name.unwrap_or_else(|| Ident::new("")),
281 vis: Visibility::Inherited,
282 attrs: attrs,
283 node: ItemKind::Mac(Mac {
284 path: path.into(),
285 tts: vec![TokenTree::Delimited(body)],
286 }),
287 })
David Tolnayedf2b992016-09-23 20:43:45 -0700288 ));
289
David Tolnaya96a3fa2016-09-24 07:17:42 -0700290 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700291 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700292 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700293 keyword!("extern") >>
294 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700295 id: ident >>
296 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700297 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700298 ident
299 )) >>
300 punct!(";") >>
301 ({
302 let (name, original_name) = match rename {
303 Some(rename) => (rename, Some(id)),
304 None => (id, None),
305 };
306 Item {
307 ident: name,
308 vis: vis,
309 attrs: attrs,
310 node: ItemKind::ExternCrate(original_name),
311 }
312 })
313 ));
314
David Tolnay4a057422016-10-08 00:02:31 -0700315 named!(item_use -> Item, do_parse!(
316 attrs: many0!(outer_attr) >>
317 vis: visibility >>
318 keyword!("use") >>
319 what: view_path >>
320 punct!(";") >>
321 (Item {
322 ident: "".into(),
323 vis: vis,
324 attrs: attrs,
325 node: ItemKind::Use(Box::new(what)),
326 })
327 ));
328
329 named!(view_path -> ViewPath, alt!(
330 view_path_glob
331 |
332 view_path_list
333 |
334 view_path_list_root
335 |
336 view_path_simple // must be last
337 ));
338
339
340 named!(view_path_simple -> ViewPath, do_parse!(
341 path: path >>
342 rename: option!(preceded!(keyword!("as"), ident)) >>
343 (ViewPath::Simple(path, rename))
344 ));
345
346 named!(view_path_glob -> ViewPath, do_parse!(
347 path: path >>
348 punct!("::") >>
349 punct!("*") >>
350 (ViewPath::Glob(path))
351 ));
352
353 named!(view_path_list -> ViewPath, do_parse!(
354 path: path >>
355 punct!("::") >>
356 punct!("{") >>
357 items: separated_nonempty_list!(punct!(","), path_list_item) >>
358 punct!("}") >>
359 (ViewPath::List(path, items))
360 ));
361
362 named!(view_path_list_root -> ViewPath, do_parse!(
363 global: option!(punct!("::")) >>
364 punct!("{") >>
365 items: separated_nonempty_list!(punct!(","), path_list_item) >>
366 punct!("}") >>
367 (ViewPath::List(Path {
368 global: global.is_some(),
369 segments: Vec::new(),
370 }, items))
371 ));
372
373 named!(path_list_item -> PathListItem, do_parse!(
374 name: ident >>
375 rename: option!(preceded!(keyword!("as"), ident)) >>
376 (PathListItem {
377 name: name,
378 rename: rename,
379 })
380 ));
381
David Tolnay47a877c2016-10-01 16:50:55 -0700382 named!(item_static -> Item, do_parse!(
383 attrs: many0!(outer_attr) >>
384 vis: visibility >>
385 keyword!("static") >>
386 mutability: mutability >>
387 id: ident >>
388 punct!(":") >>
389 ty: ty >>
390 punct!("=") >>
391 value: expr >>
392 punct!(";") >>
393 (Item {
394 ident: id,
395 vis: vis,
396 attrs: attrs,
397 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
398 })
399 ));
400
401 named!(item_const -> Item, do_parse!(
402 attrs: many0!(outer_attr) >>
403 vis: visibility >>
404 keyword!("const") >>
405 id: ident >>
406 punct!(":") >>
407 ty: ty >>
408 punct!("=") >>
409 value: expr >>
410 punct!(";") >>
411 (Item {
412 ident: id,
413 vis: vis,
414 attrs: attrs,
415 node: ItemKind::Const(Box::new(ty), Box::new(value)),
416 })
417 ));
418
David Tolnay42602292016-10-01 22:25:45 -0700419 named!(item_fn -> Item, do_parse!(
420 attrs: many0!(outer_attr) >>
421 vis: visibility >>
422 constness: constness >>
423 unsafety: unsafety >>
424 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
425 keyword!("fn") >>
426 name: ident >>
427 generics: generics >>
428 punct!("(") >>
429 inputs: separated_list!(punct!(","), fn_arg) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700430 cond!(!inputs.is_empty(), option!(punct!(","))) >>
David Tolnay42602292016-10-01 22:25:45 -0700431 punct!(")") >>
432 ret: option!(preceded!(punct!("->"), ty)) >>
433 where_clause: where_clause >>
434 body: block >>
435 (Item {
436 ident: name,
437 vis: vis,
438 attrs: attrs,
439 node: ItemKind::Fn(
440 Box::new(FnDecl {
441 inputs: inputs,
442 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
443 }),
444 unsafety,
445 constness,
446 abi.map(Abi),
447 Generics {
448 where_clause: where_clause,
449 .. generics
450 },
451 Box::new(body),
452 ),
453 })
454 ));
455
David Tolnayca085422016-10-04 00:12:38 -0700456 named!(fn_arg -> FnArg, alt!(
457 do_parse!(
458 punct!("&") >>
459 lt: option!(lifetime) >>
460 mutability: mutability >>
461 keyword!("self") >>
462 (FnArg::SelfRef(lt, mutability))
463 )
464 |
465 do_parse!(
466 mutability: mutability >>
467 keyword!("self") >>
468 (FnArg::SelfValue(mutability))
469 )
470 |
471 do_parse!(
472 pat: pat >>
473 punct!(":") >>
474 ty: ty >>
475 (FnArg::Captured(pat, ty))
476 )
477 |
478 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700479 ));
480
David Tolnay35902302016-10-06 01:11:08 -0700481 named!(item_mod -> Item, do_parse!(
482 attrs: many0!(outer_attr) >>
483 vis: visibility >>
484 keyword!("mod") >>
485 id: ident >>
486 punct!("{") >>
487 items: many0!(item) >>
488 punct!("}") >>
489 (Item {
490 ident: id,
491 vis: vis,
492 attrs: attrs,
493 node: ItemKind::Mod(items),
494 })
495 ));
496
497 named!(item_foreign_mod -> Item, do_parse!(
498 attrs: many0!(outer_attr) >>
499 keyword!("extern") >>
500 abi: option!(quoted_string) >>
501 punct!("{") >>
502 items: many0!(foreign_item) >>
503 punct!("}") >>
504 (Item {
505 ident: "".into(),
506 vis: Visibility::Inherited,
507 attrs: attrs,
508 node: ItemKind::ForeignMod(ForeignMod {
509 abi: abi.map(Abi),
510 items: items,
511 }),
512 })
513 ));
514
515 named!(foreign_item -> ForeignItem, alt!(
516 foreign_fn
517 |
518 foreign_static
519 ));
520
521 named!(foreign_fn -> ForeignItem, do_parse!(
522 attrs: many0!(outer_attr) >>
523 vis: visibility >>
524 keyword!("fn") >>
525 name: ident >>
526 generics: generics >>
527 punct!("(") >>
528 inputs: separated_list!(punct!(","), fn_arg) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700529 cond!(!inputs.is_empty(), option!(punct!(","))) >>
David Tolnay35902302016-10-06 01:11:08 -0700530 punct!(")") >>
531 ret: option!(preceded!(punct!("->"), ty)) >>
532 where_clause: where_clause >>
533 punct!(";") >>
534 (ForeignItem {
535 ident: name,
536 attrs: attrs,
537 node: ForeignItemKind::Fn(
538 Box::new(FnDecl {
539 inputs: inputs,
540 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
541 }),
542 Generics {
543 where_clause: where_clause,
544 .. generics
545 },
546 ),
547 vis: vis,
548 })
549 ));
550
551 named!(foreign_static -> ForeignItem, do_parse!(
552 attrs: many0!(outer_attr) >>
553 vis: visibility >>
554 keyword!("static") >>
555 mutability: mutability >>
556 id: ident >>
557 punct!(":") >>
558 ty: ty >>
559 punct!(";") >>
560 (ForeignItem {
561 ident: id,
562 attrs: attrs,
563 node: ForeignItemKind::Static(Box::new(ty), mutability),
564 vis: vis,
565 })
566 ));
567
David Tolnay3cf52982016-10-01 17:11:37 -0700568 named!(item_ty -> Item, do_parse!(
569 attrs: many0!(outer_attr) >>
570 vis: visibility >>
571 keyword!("type") >>
572 id: ident >>
573 generics: generics >>
574 punct!("=") >>
575 ty: ty >>
576 punct!(";") >>
577 (Item {
578 ident: id,
579 vis: vis,
580 attrs: attrs,
581 node: ItemKind::Ty(Box::new(ty), generics),
582 })
583 ));
584
David Tolnaya96a3fa2016-09-24 07:17:42 -0700585 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700586 macro_input,
587 |def: MacroInput| Item {
588 ident: def.ident,
589 vis: def.vis,
590 attrs: def.attrs,
591 node: match def.body {
592 Body::Enum(variants) => {
593 ItemKind::Enum(variants, def.generics)
594 }
595 Body::Struct(variant_data) => {
596 ItemKind::Struct(variant_data, def.generics)
597 }
598 }
599 }
600 ));
David Tolnay42602292016-10-01 22:25:45 -0700601
David Tolnay2f9fa632016-10-03 22:08:48 -0700602 named!(item_union -> Item, do_parse!(
603 attrs: many0!(outer_attr) >>
604 vis: visibility >>
605 keyword!("union") >>
606 id: ident >>
607 generics: generics >>
608 where_clause: where_clause >>
609 fields: struct_like_body >>
610 (Item {
611 ident: id,
612 vis: vis,
613 attrs: attrs,
614 node: ItemKind::Union(
615 VariantData::Struct(fields),
616 Generics {
617 where_clause: where_clause,
618 .. generics
619 },
620 ),
621 })
622 ));
623
David Tolnay0aecb732016-10-03 23:03:50 -0700624 named!(item_trait -> Item, do_parse!(
625 attrs: many0!(outer_attr) >>
626 vis: visibility >>
627 unsafety: unsafety >>
628 keyword!("trait") >>
629 id: ident >>
630 generics: generics >>
631 bounds: opt_vec!(preceded!(
632 punct!(":"),
633 separated_nonempty_list!(punct!("+"), ty_param_bound)
634 )) >>
635 where_clause: where_clause >>
636 punct!("{") >>
637 body: many0!(trait_item) >>
638 punct!("}") >>
639 (Item {
640 ident: id,
641 vis: vis,
642 attrs: attrs,
643 node: ItemKind::Trait(
644 unsafety,
645 Generics {
646 where_clause: where_clause,
647 .. generics
648 },
649 bounds,
650 body,
651 ),
652 })
653 ));
654
David Tolnayf94e2362016-10-04 00:29:51 -0700655 named!(item_default_impl -> Item, do_parse!(
656 attrs: many0!(outer_attr) >>
657 unsafety: unsafety >>
658 keyword!("impl") >>
659 path: path >>
660 keyword!("for") >>
661 punct!("..") >>
662 punct!("{") >>
663 punct!("}") >>
664 (Item {
665 ident: "".into(),
666 vis: Visibility::Inherited,
667 attrs: attrs,
668 node: ItemKind::DefaultImpl(unsafety, path),
669 })
670 ));
671
David Tolnay0aecb732016-10-03 23:03:50 -0700672 named!(trait_item -> TraitItem, alt!(
673 trait_item_const
674 |
675 trait_item_method
676 |
677 trait_item_type
678 |
679 trait_item_mac
680 ));
681
682 named!(trait_item_const -> TraitItem, do_parse!(
683 attrs: many0!(outer_attr) >>
684 keyword!("const") >>
685 id: ident >>
686 punct!(":") >>
687 ty: ty >>
688 value: option!(preceded!(punct!("="), expr)) >>
689 punct!(";") >>
690 (TraitItem {
691 ident: id,
692 attrs: attrs,
693 node: TraitItemKind::Const(ty, value),
694 })
695 ));
696
697 named!(trait_item_method -> TraitItem, do_parse!(
698 attrs: many0!(outer_attr) >>
699 constness: constness >>
700 unsafety: unsafety >>
701 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
702 keyword!("fn") >>
703 name: ident >>
704 generics: generics >>
705 punct!("(") >>
706 inputs: separated_list!(punct!(","), fn_arg) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700707 cond!(!inputs.is_empty(), option!(punct!(","))) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700708 punct!(")") >>
709 ret: option!(preceded!(punct!("->"), ty)) >>
710 where_clause: where_clause >>
711 body: option!(block) >>
712 cond!(body.is_none(), punct!(";")) >>
713 (TraitItem {
714 ident: name,
715 attrs: attrs,
716 node: TraitItemKind::Method(
717 MethodSig {
718 unsafety: unsafety,
719 constness: constness,
720 abi: abi.map(Abi),
721 decl: FnDecl {
722 inputs: inputs,
723 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
724 },
725 generics: Generics {
726 where_clause: where_clause,
727 .. generics
728 },
729 },
730 body,
731 ),
732 })
733 ));
734
735 named!(trait_item_type -> TraitItem, do_parse!(
736 attrs: many0!(outer_attr) >>
737 keyword!("type") >>
738 id: ident >>
739 bounds: opt_vec!(preceded!(
740 punct!(":"),
741 separated_nonempty_list!(punct!("+"), ty_param_bound)
742 )) >>
743 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700744 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700745 (TraitItem {
746 ident: id,
747 attrs: attrs,
748 node: TraitItemKind::Type(bounds, default),
749 })
750 ));
751
752 named!(trait_item_mac -> TraitItem, do_parse!(
753 attrs: many0!(outer_attr) >>
754 id: ident >>
755 punct!("!") >>
756 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700757 cond!(match body.delim {
758 DelimToken::Paren | DelimToken::Bracket => true,
759 DelimToken::Brace => false,
760 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700761 (TraitItem {
762 ident: id.clone(),
763 attrs: attrs,
764 node: TraitItemKind::Macro(Mac {
765 path: id.into(),
766 tts: vec![TokenTree::Delimited(body)],
767 }),
768 })
769 ));
770
David Tolnay4c9be372016-10-06 00:47:37 -0700771 named!(item_impl -> Item, do_parse!(
772 attrs: many0!(outer_attr) >>
773 unsafety: unsafety >>
774 keyword!("impl") >>
775 generics: generics >>
776 polarity_path: alt!(
777 do_parse!(
778 polarity: impl_polarity >>
779 path: path >>
780 keyword!("for") >>
781 ((polarity, Some(path)))
782 )
783 |
784 epsilon!() => { |_| (ImplPolarity::Positive, None) }
785 ) >>
786 self_ty: ty >>
787 where_clause: where_clause >>
788 punct!("{") >>
789 body: many0!(impl_item) >>
790 punct!("}") >>
791 (Item {
792 ident: "".into(),
793 vis: Visibility::Inherited,
794 attrs: attrs,
795 node: ItemKind::Impl(
796 unsafety,
797 polarity_path.0,
798 Generics {
799 where_clause: where_clause,
800 .. generics
801 },
802 polarity_path.1,
803 Box::new(self_ty),
804 body,
805 ),
806 })
807 ));
808
809 named!(impl_item -> ImplItem, alt!(
810 impl_item_const
811 |
812 impl_item_method
813 |
814 impl_item_type
815 |
816 impl_item_macro
817 ));
818
819 named!(impl_item_const -> ImplItem, do_parse!(
820 attrs: many0!(outer_attr) >>
821 vis: visibility >>
822 defaultness: defaultness >>
823 keyword!("const") >>
824 id: ident >>
825 punct!(":") >>
826 ty: ty >>
827 punct!("=") >>
828 value: expr >>
829 punct!(";") >>
830 (ImplItem {
831 ident: id,
832 vis: vis,
833 defaultness: defaultness,
834 attrs: attrs,
835 node: ImplItemKind::Const(ty, value),
836 })
837 ));
838
839 named!(impl_item_method -> ImplItem, do_parse!(
840 attrs: many0!(outer_attr) >>
841 vis: visibility >>
842 defaultness: defaultness >>
843 constness: constness >>
844 unsafety: unsafety >>
845 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
846 keyword!("fn") >>
847 name: ident >>
848 generics: generics >>
849 punct!("(") >>
850 inputs: separated_list!(punct!(","), fn_arg) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700851 cond!(!inputs.is_empty(), option!(punct!(","))) >>
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 Tolnay4c9be372016-10-06 00:47:37 -07001098 ItemKind::Impl(unsafety,
1099 polarity,
1100 ref generics,
1101 ref path,
1102 ref ty,
1103 ref items) => {
1104 unsafety.to_tokens(tokens);
1105 tokens.append("impl");
1106 generics.to_tokens(tokens);
1107 if let Some(ref path) = *path {
1108 polarity.to_tokens(tokens);
1109 path.to_tokens(tokens);
1110 tokens.append("for");
1111 }
1112 ty.to_tokens(tokens);
1113 generics.where_clause.to_tokens(tokens);
1114 tokens.append("{");
1115 tokens.append_all(items);
1116 tokens.append("}");
1117 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001118 ItemKind::Mac(ref mac) => {
1119 mac.path.to_tokens(tokens);
1120 tokens.append("!");
1121 self.ident.to_tokens(tokens);
1122 for tt in &mac.tts {
1123 tt.to_tokens(tokens);
1124 }
1125 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001126 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1127 // no semicolon
1128 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001129 _ => tokens.append(";"),
1130 }
1131 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001132 }
1133 }
1134 }
David Tolnay42602292016-10-01 22:25:45 -07001135
David Tolnay4a057422016-10-08 00:02:31 -07001136 impl ToTokens for ViewPath {
1137 fn to_tokens(&self, tokens: &mut Tokens) {
1138 match *self {
1139 ViewPath::Simple(ref path, ref rename) => {
1140 path.to_tokens(tokens);
1141 if let Some(ref rename) = *rename {
1142 tokens.append("as");
1143 rename.to_tokens(tokens);
1144 }
1145 }
1146 ViewPath::Glob(ref path) => {
1147 path.to_tokens(tokens);
1148 tokens.append("::");
1149 tokens.append("*");
1150 }
1151 ViewPath::List(ref path, ref items) => {
1152 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001153 if path.global || !path.segments.is_empty() {
1154 tokens.append("::");
1155 }
David Tolnay4a057422016-10-08 00:02:31 -07001156 tokens.append("{");
1157 tokens.append_separated(items, ",");
1158 tokens.append("}");
1159 }
1160 }
1161 }
1162 }
1163
1164 impl ToTokens for PathListItem {
1165 fn to_tokens(&self, tokens: &mut Tokens) {
1166 self.name.to_tokens(tokens);
1167 if let Some(ref rename) = self.rename {
1168 tokens.append("as");
1169 rename.to_tokens(tokens);
1170 }
1171 }
1172 }
1173
David Tolnayca085422016-10-04 00:12:38 -07001174 impl ToTokens for TraitItem {
1175 fn to_tokens(&self, tokens: &mut Tokens) {
1176 tokens.append_all(self.attrs.outer());
1177 match self.node {
1178 TraitItemKind::Const(ref ty, ref expr) => {
1179 tokens.append("const");
1180 self.ident.to_tokens(tokens);
1181 tokens.append(":");
1182 ty.to_tokens(tokens);
1183 if let Some(ref expr) = *expr {
1184 tokens.append("=");
1185 expr.to_tokens(tokens);
1186 }
1187 tokens.append(";");
1188 }
1189 TraitItemKind::Method(ref sig, ref block) => {
1190 sig.unsafety.to_tokens(tokens);
1191 sig.abi.to_tokens(tokens);
1192 tokens.append("fn");
1193 self.ident.to_tokens(tokens);
1194 sig.generics.to_tokens(tokens);
1195 tokens.append("(");
1196 tokens.append_separated(&sig.decl.inputs, ",");
1197 tokens.append(")");
1198 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1199 tokens.append("->");
1200 ty.to_tokens(tokens);
1201 }
1202 sig.generics.where_clause.to_tokens(tokens);
1203 match *block {
1204 Some(ref block) => block.to_tokens(tokens),
1205 None => tokens.append(";"),
1206 }
1207 }
1208 TraitItemKind::Type(ref bound, ref default) => {
1209 tokens.append("type");
1210 self.ident.to_tokens(tokens);
1211 if !bound.is_empty() {
1212 tokens.append(":");
1213 tokens.append_separated(bound, "+");
1214 }
1215 if let Some(ref default) = *default {
1216 tokens.append("=");
1217 default.to_tokens(tokens);
1218 }
1219 tokens.append(";");
1220 }
1221 TraitItemKind::Macro(ref mac) => {
1222 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001223 match mac.tts.last() {
1224 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1225 // no semicolon
1226 }
1227 _ => tokens.append(";"),
1228 }
David Tolnayca085422016-10-04 00:12:38 -07001229 }
1230 }
1231 }
1232 }
1233
David Tolnay4c9be372016-10-06 00:47:37 -07001234 impl ToTokens for ImplItem {
1235 fn to_tokens(&self, tokens: &mut Tokens) {
1236 tokens.append_all(self.attrs.outer());
1237 match self.node {
1238 ImplItemKind::Const(ref ty, ref expr) => {
1239 self.vis.to_tokens(tokens);
1240 self.defaultness.to_tokens(tokens);
1241 tokens.append("const");
1242 self.ident.to_tokens(tokens);
1243 tokens.append(":");
1244 ty.to_tokens(tokens);
1245 tokens.append("=");
1246 expr.to_tokens(tokens);
1247 tokens.append(";");
1248 }
1249 ImplItemKind::Method(ref sig, ref block) => {
1250 self.vis.to_tokens(tokens);
1251 self.defaultness.to_tokens(tokens);
1252 sig.unsafety.to_tokens(tokens);
1253 sig.abi.to_tokens(tokens);
1254 tokens.append("fn");
1255 self.ident.to_tokens(tokens);
1256 sig.generics.to_tokens(tokens);
1257 tokens.append("(");
1258 tokens.append_separated(&sig.decl.inputs, ",");
1259 tokens.append(")");
1260 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1261 tokens.append("->");
1262 ty.to_tokens(tokens);
1263 }
1264 sig.generics.where_clause.to_tokens(tokens);
1265 block.to_tokens(tokens);
1266 }
1267 ImplItemKind::Type(ref ty) => {
1268 self.vis.to_tokens(tokens);
1269 self.defaultness.to_tokens(tokens);
1270 tokens.append("type");
1271 self.ident.to_tokens(tokens);
1272 tokens.append("=");
1273 ty.to_tokens(tokens);
1274 tokens.append(";");
1275 }
1276 ImplItemKind::Macro(ref mac) => {
1277 mac.to_tokens(tokens);
1278 match mac.tts.last() {
1279 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1280 // no semicolon
1281 }
1282 _ => tokens.append(";"),
1283 }
1284 }
1285 }
1286 }
1287 }
1288
David Tolnay35902302016-10-06 01:11:08 -07001289 impl ToTokens for ForeignItem {
1290 fn to_tokens(&self, tokens: &mut Tokens) {
1291 tokens.append_all(self.attrs.outer());
1292 match self.node {
1293 ForeignItemKind::Fn(ref decl, ref generics) => {
1294 self.vis.to_tokens(tokens);
1295 tokens.append("fn");
1296 self.ident.to_tokens(tokens);
1297 generics.to_tokens(tokens);
1298 tokens.append("(");
1299 tokens.append_separated(&decl.inputs, ",");
1300 tokens.append(")");
1301 if let FunctionRetTy::Ty(ref ty) = decl.output {
1302 tokens.append("->");
1303 ty.to_tokens(tokens);
1304 }
1305 generics.where_clause.to_tokens(tokens);
1306 tokens.append(";");
1307 }
1308 ForeignItemKind::Static(ref ty, mutability) => {
1309 self.vis.to_tokens(tokens);
1310 tokens.append("static");
1311 mutability.to_tokens(tokens);
1312 self.ident.to_tokens(tokens);
1313 tokens.append(":");
1314 ty.to_tokens(tokens);
1315 tokens.append(";");
1316 }
1317 }
1318 }
1319 }
1320
David Tolnay62f374c2016-10-02 13:37:00 -07001321 impl ToTokens for FnArg {
1322 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001323 match *self {
1324 FnArg::SelfRef(ref lifetime, mutability) => {
1325 tokens.append("&");
1326 lifetime.to_tokens(tokens);
1327 mutability.to_tokens(tokens);
1328 tokens.append("self");
1329 }
1330 FnArg::SelfValue(mutability) => {
1331 mutability.to_tokens(tokens);
1332 tokens.append("self");
1333 }
1334 FnArg::Captured(ref pat, ref ty) => {
1335 pat.to_tokens(tokens);
1336 tokens.append(":");
1337 ty.to_tokens(tokens);
1338 }
1339 FnArg::Ignored(ref ty) => {
1340 ty.to_tokens(tokens);
1341 }
1342 }
David Tolnay62f374c2016-10-02 13:37:00 -07001343 }
1344 }
1345
David Tolnay42602292016-10-01 22:25:45 -07001346 impl ToTokens for Unsafety {
1347 fn to_tokens(&self, tokens: &mut Tokens) {
1348 match *self {
1349 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001350 Unsafety::Normal => {
1351 // nothing
1352 }
David Tolnay42602292016-10-01 22:25:45 -07001353 }
1354 }
1355 }
1356
1357 impl ToTokens for Constness {
1358 fn to_tokens(&self, tokens: &mut Tokens) {
1359 match *self {
1360 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001361 Constness::NotConst => {
1362 // nothing
1363 }
David Tolnay42602292016-10-01 22:25:45 -07001364 }
1365 }
1366 }
1367
David Tolnay4c9be372016-10-06 00:47:37 -07001368 impl ToTokens for Defaultness {
1369 fn to_tokens(&self, tokens: &mut Tokens) {
1370 match *self {
1371 Defaultness::Default => tokens.append("default"),
1372 Defaultness::Final => {
1373 // nothing
1374 }
1375 }
1376 }
1377 }
1378
1379 impl ToTokens for ImplPolarity {
1380 fn to_tokens(&self, tokens: &mut Tokens) {
1381 match *self {
1382 ImplPolarity::Negative => tokens.append("!"),
1383 ImplPolarity::Positive => {
1384 // nothing
1385 }
1386 }
1387 }
1388 }
1389
David Tolnay42602292016-10-01 22:25:45 -07001390 impl ToTokens for Abi {
1391 fn to_tokens(&self, tokens: &mut Tokens) {
1392 tokens.append("extern");
1393 self.0.to_tokens(tokens);
1394 }
1395 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001396}