blob: e75a76d5c14f12f50acb19067457e2210eebb31e [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay771ecf42016-09-23 19:26:37 -07003/// An item
4///
5/// The name might be a dummy name in case of anonymous items
David Tolnayb79ee962016-09-04 09:39:20 -07006#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Item {
8 pub ident: Ident,
9 pub vis: Visibility,
10 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -070011 pub node: ItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -070012}
13
14#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070015pub enum ItemKind {
16 /// An`extern crate` item, with optional original crate name.
17 ///
18 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
19 ExternCrate(Option<Ident>),
20 /// A use declaration (`use` or `pub use`) item.
21 ///
22 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
23 Use(Box<ViewPath>),
24 /// A static item (`static` or `pub static`).
25 ///
26 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
27 Static(Box<Ty>, Mutability, Box<Expr>),
28 /// A constant item (`const` or `pub const`).
29 ///
30 /// E.g. `const FOO: i32 = 42;`
31 Const(Box<Ty>, Box<Expr>),
32 /// A function declaration (`fn` or `pub fn`).
33 ///
34 /// E.g. `fn foo(bar: usize) -> usize { .. }`
David Tolnay42602292016-10-01 22:25:45 -070035 Fn(Box<FnDecl>, Unsafety, Constness, Option<Abi>, Generics, Box<Block>),
David Tolnayf38cdf62016-09-23 19:07:09 -070036 /// A module declaration (`mod` or `pub mod`).
37 ///
38 /// E.g. `mod foo;` or `mod foo { .. }`
David Tolnay37d10332016-10-13 20:51:04 -070039 Mod(Option<Vec<Item>>),
David Tolnayf38cdf62016-09-23 19:07:09 -070040 /// An external module (`extern` or `pub extern`).
41 ///
42 /// E.g. `extern {}` or `extern "C" {}`
43 ForeignMod(ForeignMod),
44 /// A type alias (`type` or `pub type`).
45 ///
46 /// E.g. `type Foo = Bar<u8>;`
47 Ty(Box<Ty>, Generics),
48 /// An enum definition (`enum` or `pub enum`).
49 ///
50 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
51 Enum(Vec<Variant>, Generics),
52 /// A struct definition (`struct` or `pub struct`).
53 ///
54 /// E.g. `struct Foo<A> { x: A }`
55 Struct(VariantData, Generics),
56 /// A union definition (`union` or `pub union`).
57 ///
58 /// E.g. `union Foo<A, B> { x: A, y: B }`
59 Union(VariantData, Generics),
60 /// A Trait declaration (`trait` or `pub trait`).
61 ///
62 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
63 Trait(Unsafety, Generics, Vec<TyParamBound>, Vec<TraitItem>),
David Tolnay0aecb732016-10-03 23:03:50 -070064 /// Default trait implementation.
David Tolnayf38cdf62016-09-23 19:07:09 -070065 ///
66 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
67 DefaultImpl(Unsafety, Path),
68 /// An implementation.
69 ///
70 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
71 Impl(Unsafety,
David Tolnaydaaf7742016-10-03 11:11:43 -070072 ImplPolarity,
73 Generics,
74 Option<Path>, // (optional) trait this impl implements
75 Box<Ty>, // self
76 Vec<ImplItem>),
David Tolnayf38cdf62016-09-23 19:07:09 -070077 /// A macro invocation (which includes macro definition).
78 ///
79 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
80 Mac(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -070081}
82
David Tolnay453cfd12016-10-23 11:00:14 -070083impl From<MacroInput> for Item {
84 fn from(input: MacroInput) -> Item {
85 Item {
86 ident: input.ident,
87 vis: input.vis,
88 attrs: input.attrs,
89 node: match input.body {
90 Body::Enum(variants) => ItemKind::Enum(variants, input.generics),
91 Body::Struct(variant_data) => ItemKind::Struct(variant_data, input.generics),
92 },
93 }
94 }
95}
96
David Tolnayb79ee962016-09-04 09:39:20 -070097#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070098pub enum ViewPath {
99 /// `foo::bar::baz as quux`
100 ///
101 /// or just
102 ///
103 /// `foo::bar::baz` (with `as baz` implicitly on the right)
David Tolnay4a057422016-10-08 00:02:31 -0700104 Simple(Path, Option<Ident>),
David Tolnayf38cdf62016-09-23 19:07:09 -0700105
106 /// `foo::bar::*`
David Tolnayaed77b02016-09-23 20:50:31 -0700107 Glob(Path),
David Tolnayf38cdf62016-09-23 19:07:09 -0700108
David Tolnayaed77b02016-09-23 20:50:31 -0700109 /// `foo::bar::{a, b, c}`
David Tolnaydaaf7742016-10-03 11:11:43 -0700110 List(Path, Vec<PathListItem>),
David Tolnayf38cdf62016-09-23 19:07:09 -0700111}
112
113#[derive(Debug, Clone, Eq, PartialEq)]
114pub struct PathListItem {
115 pub name: Ident,
116 /// renamed in list, e.g. `use foo::{bar as baz};`
117 pub rename: Option<Ident>,
118}
119
120#[derive(Debug, Copy, Clone, Eq, PartialEq)]
121pub enum Unsafety {
122 Unsafe,
123 Normal,
124}
125
126#[derive(Debug, Copy, Clone, Eq, PartialEq)]
127pub enum Constness {
128 Const,
129 NotConst,
130}
131
132#[derive(Debug, Copy, Clone, Eq, PartialEq)]
133pub enum Defaultness {
134 Default,
135 Final,
136}
137
138#[derive(Debug, Clone, Eq, PartialEq)]
139pub struct Abi(pub String);
140
141/// Foreign module declaration.
142///
David Tolnay35902302016-10-06 01:11:08 -0700143/// E.g. `extern { .. }` or `extern "C" { .. }`
David Tolnayf38cdf62016-09-23 19:07:09 -0700144#[derive(Debug, Clone, Eq, PartialEq)]
145pub struct ForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700146 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700147 pub items: Vec<ForeignItem>,
148}
149
150#[derive(Debug, Clone, Eq, PartialEq)]
151pub struct ForeignItem {
David Tolnayb79ee962016-09-04 09:39:20 -0700152 pub ident: Ident,
153 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700154 pub node: ForeignItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -0700155 pub vis: Visibility,
David Tolnayf38cdf62016-09-23 19:07:09 -0700156}
157
David Tolnay771ecf42016-09-23 19:26:37 -0700158/// An item within an `extern` block
David Tolnayf38cdf62016-09-23 19:07:09 -0700159#[derive(Debug, Clone, Eq, PartialEq)]
160pub enum ForeignItemKind {
161 /// A foreign function
162 Fn(Box<FnDecl>, Generics),
David Tolnay35902302016-10-06 01:11:08 -0700163 /// A foreign static item (`static ext: u8`)
164 Static(Box<Ty>, Mutability),
David Tolnayf38cdf62016-09-23 19:07:09 -0700165}
166
167/// Represents an item declaration within a trait declaration,
168/// possibly including a default implementation. A trait item is
169/// either required (meaning it doesn't have an implementation, just a
170/// signature) or provided (meaning it has a default implementation).
171#[derive(Debug, Clone, Eq, PartialEq)]
172pub struct TraitItem {
173 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700174 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700175 pub node: TraitItemKind,
176}
177
178#[derive(Debug, Clone, Eq, PartialEq)]
179pub enum TraitItemKind {
180 Const(Ty, Option<Expr>),
181 Method(MethodSig, Option<Block>),
182 Type(Vec<TyParamBound>, Option<Ty>),
183 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700184}
185
David Tolnay55337722016-09-11 12:58:56 -0700186#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700187pub enum ImplPolarity {
188 /// `impl Trait for Type`
189 Positive,
190 /// `impl !Trait for Type`
191 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700192}
193
David Tolnayf38cdf62016-09-23 19:07:09 -0700194#[derive(Debug, Clone, Eq, PartialEq)]
195pub struct ImplItem {
196 pub ident: Ident,
197 pub vis: Visibility,
198 pub defaultness: Defaultness,
199 pub attrs: Vec<Attribute>,
200 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700201}
202
David Tolnayf38cdf62016-09-23 19:07:09 -0700203#[derive(Debug, Clone, Eq, PartialEq)]
204pub enum ImplItemKind {
205 Const(Ty, Expr),
206 Method(MethodSig, Block),
207 Type(Ty),
208 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700209}
David Tolnayd5025812016-09-04 14:21:46 -0700210
David Tolnayf38cdf62016-09-23 19:07:09 -0700211/// Represents a method's signature in a trait declaration,
212/// or in an implementation.
213#[derive(Debug, Clone, Eq, PartialEq)]
214pub struct MethodSig {
215 pub unsafety: Unsafety,
216 pub constness: Constness,
David Tolnay0aecb732016-10-03 23:03:50 -0700217 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700218 pub decl: FnDecl,
219 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700220}
David Tolnayedf2b992016-09-23 20:43:45 -0700221
David Tolnay62f374c2016-10-02 13:37:00 -0700222/// Header (not the body) of a function declaration.
223///
224/// E.g. `fn foo(bar: baz)`
225#[derive(Debug, Clone, Eq, PartialEq)]
226pub struct FnDecl {
227 pub inputs: Vec<FnArg>,
228 pub output: FunctionRetTy,
229}
230
231/// An argument in a function header.
232///
233/// E.g. `bar: usize` as in `fn foo(bar: usize)`
234#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayca085422016-10-04 00:12:38 -0700235pub enum FnArg {
236 SelfRef(Option<Lifetime>, Mutability),
237 SelfValue(Mutability),
238 Captured(Pat, Ty),
239 Ignored(Ty),
David Tolnay62f374c2016-10-02 13:37:00 -0700240}
241
David Tolnayedf2b992016-09-23 20:43:45 -0700242#[cfg(feature = "parsing")]
243pub mod parsing {
244 use super::*;
David Tolnay4a057422016-10-08 00:02:31 -0700245 use {DelimToken, FunctionRetTy, Generics, Ident, Mac, Path, TokenTree, VariantData, Visibility};
David Tolnay4a51dc72016-10-01 00:40:31 -0700246 use attr::parsing::outer_attr;
David Tolnay2f9fa632016-10-03 22:08:48 -0700247 use data::parsing::{struct_like_body, visibility};
David Tolnay62f374c2016-10-02 13:37:00 -0700248 use expr::parsing::{block, expr, pat};
David Tolnayca085422016-10-04 00:12:38 -0700249 use generics::parsing::{generics, lifetime, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700250 use ident::parsing::ident;
David Tolnay42602292016-10-01 22:25:45 -0700251 use lit::parsing::quoted_string;
David Tolnay84aa0752016-10-02 23:01:13 -0700252 use mac::parsing::delimited;
David Tolnayedf2b992016-09-23 20:43:45 -0700253 use macro_input::{Body, MacroInput};
254 use macro_input::parsing::macro_input;
David Tolnayf94e2362016-10-04 00:29:51 -0700255 use ty::parsing::{mutability, path, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700256
257 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700258 item_extern_crate
David Tolnay4a057422016-10-08 00:02:31 -0700259 |
260 item_use
David Tolnay47a877c2016-10-01 16:50:55 -0700261 |
262 item_static
263 |
264 item_const
David Tolnay42602292016-10-01 22:25:45 -0700265 |
266 item_fn
David Tolnay35902302016-10-06 01:11:08 -0700267 |
268 item_mod
269 |
270 item_foreign_mod
David Tolnay3cf52982016-10-01 17:11:37 -0700271 |
272 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700273 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700274 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700275 |
276 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700277 |
278 item_trait
David Tolnayf94e2362016-10-04 00:29:51 -0700279 |
280 item_default_impl
David Tolnay4c9be372016-10-06 00:47:37 -0700281 |
282 item_impl
David Tolnay84aa0752016-10-02 23:01:13 -0700283 |
284 item_mac
285 ));
286
David Tolnay453cfd12016-10-23 11:00:14 -0700287 named!(pub items -> Vec<Item>, many0!(item));
288
David Tolnay84aa0752016-10-02 23:01:13 -0700289 named!(item_mac -> Item, do_parse!(
290 attrs: many0!(outer_attr) >>
291 path: ident >>
292 punct!("!") >>
293 name: option!(ident) >>
294 body: delimited >>
David Tolnay1a8b3522016-10-08 22:27:00 -0700295 cond!(match body.delim {
296 DelimToken::Paren | DelimToken::Bracket => true,
297 DelimToken::Brace => false,
298 }, punct!(";")) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700299 (Item {
300 ident: name.unwrap_or_else(|| Ident::new("")),
301 vis: Visibility::Inherited,
302 attrs: attrs,
303 node: ItemKind::Mac(Mac {
304 path: path.into(),
305 tts: vec![TokenTree::Delimited(body)],
306 }),
307 })
David Tolnayedf2b992016-09-23 20:43:45 -0700308 ));
309
David Tolnaya96a3fa2016-09-24 07:17:42 -0700310 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700311 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700312 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700313 keyword!("extern") >>
314 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700315 id: ident >>
316 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700317 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700318 ident
319 )) >>
320 punct!(";") >>
321 ({
322 let (name, original_name) = match rename {
323 Some(rename) => (rename, Some(id)),
324 None => (id, None),
325 };
326 Item {
327 ident: name,
328 vis: vis,
329 attrs: attrs,
330 node: ItemKind::ExternCrate(original_name),
331 }
332 })
333 ));
334
David Tolnay4a057422016-10-08 00:02:31 -0700335 named!(item_use -> Item, do_parse!(
336 attrs: many0!(outer_attr) >>
337 vis: visibility >>
338 keyword!("use") >>
339 what: view_path >>
340 punct!(";") >>
341 (Item {
342 ident: "".into(),
343 vis: vis,
344 attrs: attrs,
345 node: ItemKind::Use(Box::new(what)),
346 })
347 ));
348
349 named!(view_path -> ViewPath, alt!(
350 view_path_glob
351 |
352 view_path_list
353 |
354 view_path_list_root
355 |
356 view_path_simple // must be last
357 ));
358
359
360 named!(view_path_simple -> ViewPath, do_parse!(
361 path: path >>
362 rename: option!(preceded!(keyword!("as"), ident)) >>
363 (ViewPath::Simple(path, rename))
364 ));
365
366 named!(view_path_glob -> ViewPath, do_parse!(
367 path: path >>
368 punct!("::") >>
369 punct!("*") >>
370 (ViewPath::Glob(path))
371 ));
372
373 named!(view_path_list -> ViewPath, do_parse!(
374 path: path >>
375 punct!("::") >>
376 punct!("{") >>
377 items: separated_nonempty_list!(punct!(","), path_list_item) >>
378 punct!("}") >>
379 (ViewPath::List(path, items))
380 ));
381
382 named!(view_path_list_root -> ViewPath, do_parse!(
383 global: option!(punct!("::")) >>
384 punct!("{") >>
385 items: separated_nonempty_list!(punct!(","), path_list_item) >>
386 punct!("}") >>
387 (ViewPath::List(Path {
388 global: global.is_some(),
389 segments: Vec::new(),
390 }, items))
391 ));
392
393 named!(path_list_item -> PathListItem, do_parse!(
David Tolnaye6e42542016-10-24 22:37:11 -0700394 name: alt!(
395 ident
396 |
397 map!(keyword!("self"), Into::into)
398 ) >>
David Tolnay4a057422016-10-08 00:02:31 -0700399 rename: option!(preceded!(keyword!("as"), ident)) >>
400 (PathListItem {
401 name: name,
402 rename: rename,
403 })
404 ));
405
David Tolnay47a877c2016-10-01 16:50:55 -0700406 named!(item_static -> Item, do_parse!(
407 attrs: many0!(outer_attr) >>
408 vis: visibility >>
409 keyword!("static") >>
410 mutability: mutability >>
411 id: ident >>
412 punct!(":") >>
413 ty: ty >>
414 punct!("=") >>
415 value: expr >>
416 punct!(";") >>
417 (Item {
418 ident: id,
419 vis: vis,
420 attrs: attrs,
421 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
422 })
423 ));
424
425 named!(item_const -> Item, do_parse!(
426 attrs: many0!(outer_attr) >>
427 vis: visibility >>
428 keyword!("const") >>
429 id: ident >>
430 punct!(":") >>
431 ty: ty >>
432 punct!("=") >>
433 value: expr >>
434 punct!(";") >>
435 (Item {
436 ident: id,
437 vis: vis,
438 attrs: attrs,
439 node: ItemKind::Const(Box::new(ty), Box::new(value)),
440 })
441 ));
442
David Tolnay42602292016-10-01 22:25:45 -0700443 named!(item_fn -> Item, do_parse!(
444 attrs: many0!(outer_attr) >>
445 vis: visibility >>
446 constness: constness >>
447 unsafety: unsafety >>
448 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
449 keyword!("fn") >>
450 name: ident >>
451 generics: generics >>
452 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700453 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700454 punct!(")") >>
455 ret: option!(preceded!(punct!("->"), ty)) >>
456 where_clause: where_clause >>
457 body: block >>
458 (Item {
459 ident: name,
460 vis: vis,
461 attrs: attrs,
462 node: ItemKind::Fn(
463 Box::new(FnDecl {
464 inputs: inputs,
465 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
466 }),
467 unsafety,
468 constness,
469 abi.map(Abi),
470 Generics {
471 where_clause: where_clause,
472 .. generics
473 },
474 Box::new(body),
475 ),
476 })
477 ));
478
David Tolnayca085422016-10-04 00:12:38 -0700479 named!(fn_arg -> FnArg, alt!(
480 do_parse!(
481 punct!("&") >>
482 lt: option!(lifetime) >>
483 mutability: mutability >>
484 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700485 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700486 (FnArg::SelfRef(lt, mutability))
487 )
488 |
489 do_parse!(
490 mutability: mutability >>
491 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700492 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700493 (FnArg::SelfValue(mutability))
494 )
495 |
496 do_parse!(
497 pat: pat >>
498 punct!(":") >>
499 ty: ty >>
500 (FnArg::Captured(pat, ty))
501 )
502 |
503 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700504 ));
505
David Tolnay35902302016-10-06 01:11:08 -0700506 named!(item_mod -> Item, do_parse!(
507 attrs: many0!(outer_attr) >>
508 vis: visibility >>
509 keyword!("mod") >>
510 id: ident >>
David Tolnay37d10332016-10-13 20:51:04 -0700511 items: alt!(
512 punct!(";") => { |_| None }
513 |
David Tolnay453cfd12016-10-23 11:00:14 -0700514 delimited!(punct!("{"), items, punct!("}")) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700515 ) >>
David Tolnay35902302016-10-06 01:11:08 -0700516 (Item {
517 ident: id,
518 vis: vis,
519 attrs: attrs,
520 node: ItemKind::Mod(items),
521 })
522 ));
523
524 named!(item_foreign_mod -> Item, do_parse!(
525 attrs: many0!(outer_attr) >>
526 keyword!("extern") >>
527 abi: option!(quoted_string) >>
528 punct!("{") >>
529 items: many0!(foreign_item) >>
530 punct!("}") >>
531 (Item {
532 ident: "".into(),
533 vis: Visibility::Inherited,
534 attrs: attrs,
535 node: ItemKind::ForeignMod(ForeignMod {
536 abi: abi.map(Abi),
537 items: items,
538 }),
539 })
540 ));
541
542 named!(foreign_item -> ForeignItem, alt!(
543 foreign_fn
544 |
545 foreign_static
546 ));
547
548 named!(foreign_fn -> ForeignItem, do_parse!(
549 attrs: many0!(outer_attr) >>
550 vis: visibility >>
551 keyword!("fn") >>
552 name: ident >>
553 generics: generics >>
554 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700555 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay35902302016-10-06 01:11:08 -0700556 punct!(")") >>
557 ret: option!(preceded!(punct!("->"), ty)) >>
558 where_clause: where_clause >>
559 punct!(";") >>
560 (ForeignItem {
561 ident: name,
562 attrs: attrs,
563 node: ForeignItemKind::Fn(
564 Box::new(FnDecl {
565 inputs: inputs,
566 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
567 }),
568 Generics {
569 where_clause: where_clause,
570 .. generics
571 },
572 ),
573 vis: vis,
574 })
575 ));
576
577 named!(foreign_static -> ForeignItem, do_parse!(
578 attrs: many0!(outer_attr) >>
579 vis: visibility >>
580 keyword!("static") >>
581 mutability: mutability >>
582 id: ident >>
583 punct!(":") >>
584 ty: ty >>
585 punct!(";") >>
586 (ForeignItem {
587 ident: id,
588 attrs: attrs,
589 node: ForeignItemKind::Static(Box::new(ty), mutability),
590 vis: vis,
591 })
592 ));
593
David Tolnay3cf52982016-10-01 17:11:37 -0700594 named!(item_ty -> Item, do_parse!(
595 attrs: many0!(outer_attr) >>
596 vis: visibility >>
597 keyword!("type") >>
598 id: ident >>
599 generics: generics >>
600 punct!("=") >>
601 ty: ty >>
602 punct!(";") >>
603 (Item {
604 ident: id,
605 vis: vis,
606 attrs: attrs,
607 node: ItemKind::Ty(Box::new(ty), generics),
608 })
609 ));
610
David Tolnaya96a3fa2016-09-24 07:17:42 -0700611 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700612 macro_input,
613 |def: MacroInput| Item {
614 ident: def.ident,
615 vis: def.vis,
616 attrs: def.attrs,
617 node: match def.body {
618 Body::Enum(variants) => {
619 ItemKind::Enum(variants, def.generics)
620 }
621 Body::Struct(variant_data) => {
622 ItemKind::Struct(variant_data, def.generics)
623 }
624 }
625 }
626 ));
David Tolnay42602292016-10-01 22:25:45 -0700627
David Tolnay2f9fa632016-10-03 22:08:48 -0700628 named!(item_union -> Item, do_parse!(
629 attrs: many0!(outer_attr) >>
630 vis: visibility >>
631 keyword!("union") >>
632 id: ident >>
633 generics: generics >>
634 where_clause: where_clause >>
635 fields: struct_like_body >>
636 (Item {
637 ident: id,
638 vis: vis,
639 attrs: attrs,
640 node: ItemKind::Union(
641 VariantData::Struct(fields),
642 Generics {
643 where_clause: where_clause,
644 .. generics
645 },
646 ),
647 })
648 ));
649
David Tolnay0aecb732016-10-03 23:03:50 -0700650 named!(item_trait -> Item, do_parse!(
651 attrs: many0!(outer_attr) >>
652 vis: visibility >>
653 unsafety: unsafety >>
654 keyword!("trait") >>
655 id: ident >>
656 generics: generics >>
657 bounds: opt_vec!(preceded!(
658 punct!(":"),
659 separated_nonempty_list!(punct!("+"), ty_param_bound)
660 )) >>
661 where_clause: where_clause >>
662 punct!("{") >>
663 body: many0!(trait_item) >>
664 punct!("}") >>
665 (Item {
666 ident: id,
667 vis: vis,
668 attrs: attrs,
669 node: ItemKind::Trait(
670 unsafety,
671 Generics {
672 where_clause: where_clause,
673 .. generics
674 },
675 bounds,
676 body,
677 ),
678 })
679 ));
680
David Tolnayf94e2362016-10-04 00:29:51 -0700681 named!(item_default_impl -> Item, do_parse!(
682 attrs: many0!(outer_attr) >>
683 unsafety: unsafety >>
684 keyword!("impl") >>
685 path: path >>
686 keyword!("for") >>
687 punct!("..") >>
688 punct!("{") >>
689 punct!("}") >>
690 (Item {
691 ident: "".into(),
692 vis: Visibility::Inherited,
693 attrs: attrs,
694 node: ItemKind::DefaultImpl(unsafety, path),
695 })
696 ));
697
David Tolnay0aecb732016-10-03 23:03:50 -0700698 named!(trait_item -> TraitItem, alt!(
699 trait_item_const
700 |
701 trait_item_method
702 |
703 trait_item_type
704 |
705 trait_item_mac
706 ));
707
708 named!(trait_item_const -> TraitItem, do_parse!(
709 attrs: many0!(outer_attr) >>
710 keyword!("const") >>
711 id: ident >>
712 punct!(":") >>
713 ty: ty >>
714 value: option!(preceded!(punct!("="), expr)) >>
715 punct!(";") >>
716 (TraitItem {
717 ident: id,
718 attrs: attrs,
719 node: TraitItemKind::Const(ty, value),
720 })
721 ));
722
723 named!(trait_item_method -> TraitItem, do_parse!(
724 attrs: many0!(outer_attr) >>
725 constness: constness >>
726 unsafety: unsafety >>
727 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
728 keyword!("fn") >>
729 name: ident >>
730 generics: generics >>
731 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700732 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700733 punct!(")") >>
734 ret: option!(preceded!(punct!("->"), ty)) >>
735 where_clause: where_clause >>
736 body: option!(block) >>
737 cond!(body.is_none(), punct!(";")) >>
738 (TraitItem {
739 ident: name,
740 attrs: attrs,
741 node: TraitItemKind::Method(
742 MethodSig {
743 unsafety: unsafety,
744 constness: constness,
745 abi: abi.map(Abi),
746 decl: FnDecl {
747 inputs: inputs,
748 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
749 },
750 generics: Generics {
751 where_clause: where_clause,
752 .. generics
753 },
754 },
755 body,
756 ),
757 })
758 ));
759
760 named!(trait_item_type -> TraitItem, do_parse!(
761 attrs: many0!(outer_attr) >>
762 keyword!("type") >>
763 id: ident >>
764 bounds: opt_vec!(preceded!(
765 punct!(":"),
766 separated_nonempty_list!(punct!("+"), ty_param_bound)
767 )) >>
768 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700769 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700770 (TraitItem {
771 ident: id,
772 attrs: attrs,
773 node: TraitItemKind::Type(bounds, default),
774 })
775 ));
776
777 named!(trait_item_mac -> TraitItem, do_parse!(
778 attrs: many0!(outer_attr) >>
779 id: ident >>
780 punct!("!") >>
781 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700782 cond!(match body.delim {
783 DelimToken::Paren | DelimToken::Bracket => true,
784 DelimToken::Brace => false,
785 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700786 (TraitItem {
787 ident: id.clone(),
788 attrs: attrs,
789 node: TraitItemKind::Macro(Mac {
790 path: id.into(),
791 tts: vec![TokenTree::Delimited(body)],
792 }),
793 })
794 ));
795
David Tolnay4c9be372016-10-06 00:47:37 -0700796 named!(item_impl -> Item, do_parse!(
797 attrs: many0!(outer_attr) >>
798 unsafety: unsafety >>
799 keyword!("impl") >>
800 generics: generics >>
801 polarity_path: alt!(
802 do_parse!(
803 polarity: impl_polarity >>
804 path: path >>
805 keyword!("for") >>
806 ((polarity, Some(path)))
807 )
808 |
809 epsilon!() => { |_| (ImplPolarity::Positive, None) }
810 ) >>
811 self_ty: ty >>
812 where_clause: where_clause >>
813 punct!("{") >>
814 body: many0!(impl_item) >>
815 punct!("}") >>
816 (Item {
817 ident: "".into(),
818 vis: Visibility::Inherited,
819 attrs: attrs,
820 node: ItemKind::Impl(
821 unsafety,
822 polarity_path.0,
823 Generics {
824 where_clause: where_clause,
825 .. generics
826 },
827 polarity_path.1,
828 Box::new(self_ty),
829 body,
830 ),
831 })
832 ));
833
834 named!(impl_item -> ImplItem, alt!(
835 impl_item_const
836 |
837 impl_item_method
838 |
839 impl_item_type
840 |
841 impl_item_macro
842 ));
843
844 named!(impl_item_const -> ImplItem, do_parse!(
845 attrs: many0!(outer_attr) >>
846 vis: visibility >>
847 defaultness: defaultness >>
848 keyword!("const") >>
849 id: ident >>
850 punct!(":") >>
851 ty: ty >>
852 punct!("=") >>
853 value: expr >>
854 punct!(";") >>
855 (ImplItem {
856 ident: id,
857 vis: vis,
858 defaultness: defaultness,
859 attrs: attrs,
860 node: ImplItemKind::Const(ty, value),
861 })
862 ));
863
864 named!(impl_item_method -> ImplItem, do_parse!(
865 attrs: many0!(outer_attr) >>
866 vis: visibility >>
867 defaultness: defaultness >>
868 constness: constness >>
869 unsafety: unsafety >>
870 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
871 keyword!("fn") >>
872 name: ident >>
873 generics: generics >>
874 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700875 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700876 punct!(")") >>
877 ret: option!(preceded!(punct!("->"), ty)) >>
878 where_clause: where_clause >>
879 body: block >>
880 (ImplItem {
881 ident: name,
882 vis: vis,
883 defaultness: defaultness,
884 attrs: attrs,
885 node: ImplItemKind::Method(
886 MethodSig {
887 unsafety: unsafety,
888 constness: constness,
889 abi: abi.map(Abi),
890 decl: FnDecl {
891 inputs: inputs,
892 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
893 },
894 generics: Generics {
895 where_clause: where_clause,
896 .. generics
897 },
898 },
899 body,
900 ),
901 })
902 ));
903
904 named!(impl_item_type -> ImplItem, do_parse!(
905 attrs: many0!(outer_attr) >>
906 vis: visibility >>
907 defaultness: defaultness >>
908 keyword!("type") >>
909 id: ident >>
910 punct!("=") >>
911 ty: ty >>
912 punct!(";") >>
913 (ImplItem {
914 ident: id,
915 vis: vis,
916 defaultness: defaultness,
917 attrs: attrs,
918 node: ImplItemKind::Type(ty),
919 })
920 ));
921
922 named!(impl_item_macro -> ImplItem, do_parse!(
923 attrs: many0!(outer_attr) >>
924 id: ident >>
925 punct!("!") >>
926 body: delimited >>
927 cond!(match body.delim {
928 DelimToken::Paren | DelimToken::Bracket => true,
929 DelimToken::Brace => false,
930 }, punct!(";")) >>
931 (ImplItem {
932 ident: id.clone(),
933 vis: Visibility::Inherited,
934 defaultness: Defaultness::Final,
935 attrs: attrs,
936 node: ImplItemKind::Macro(Mac {
937 path: id.into(),
938 tts: vec![TokenTree::Delimited(body)],
939 }),
940 })
941 ));
942
943 named!(impl_polarity -> ImplPolarity, alt!(
944 punct!("!") => { |_| ImplPolarity::Negative }
945 |
946 epsilon!() => { |_| ImplPolarity::Positive }
947 ));
948
David Tolnay42602292016-10-01 22:25:45 -0700949 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700950 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700951 |
952 epsilon!() => { |_| Constness::NotConst }
953 ));
954
955 named!(unsafety -> Unsafety, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700956 keyword!("unsafe") => { |_| Unsafety::Unsafe }
David Tolnay42602292016-10-01 22:25:45 -0700957 |
958 epsilon!() => { |_| Unsafety::Normal }
959 ));
David Tolnay4c9be372016-10-06 00:47:37 -0700960
961 named!(defaultness -> Defaultness, alt!(
962 keyword!("default") => { |_| Defaultness::Default }
963 |
964 epsilon!() => { |_| Defaultness::Final }
965 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700966}
David Tolnay4a51dc72016-10-01 00:40:31 -0700967
968#[cfg(feature = "printing")]
969mod printing {
970 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700971 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700972 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700973 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700974 use quote::{Tokens, ToTokens};
975
976 impl ToTokens for Item {
977 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -0700978 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -0700979 match self.node {
980 ItemKind::ExternCrate(ref original) => {
981 tokens.append("extern");
982 tokens.append("crate");
983 if let Some(ref original) = *original {
984 original.to_tokens(tokens);
985 tokens.append("as");
986 }
987 self.ident.to_tokens(tokens);
988 tokens.append(";");
989 }
David Tolnay4a057422016-10-08 00:02:31 -0700990 ItemKind::Use(ref view_path) => {
991 self.vis.to_tokens(tokens);
992 tokens.append("use");
993 view_path.to_tokens(tokens);
994 tokens.append(";");
995 }
David Tolnay47a877c2016-10-01 16:50:55 -0700996 ItemKind::Static(ref ty, ref mutability, ref expr) => {
997 self.vis.to_tokens(tokens);
998 tokens.append("static");
999 mutability.to_tokens(tokens);
1000 self.ident.to_tokens(tokens);
1001 tokens.append(":");
1002 ty.to_tokens(tokens);
1003 tokens.append("=");
1004 expr.to_tokens(tokens);
1005 tokens.append(";");
1006 }
1007 ItemKind::Const(ref ty, ref expr) => {
1008 self.vis.to_tokens(tokens);
1009 tokens.append("const");
1010 self.ident.to_tokens(tokens);
1011 tokens.append(":");
1012 ty.to_tokens(tokens);
1013 tokens.append("=");
1014 expr.to_tokens(tokens);
1015 tokens.append(";");
1016 }
David Tolnay42602292016-10-01 22:25:45 -07001017 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
1018 self.vis.to_tokens(tokens);
1019 constness.to_tokens(tokens);
1020 unsafety.to_tokens(tokens);
1021 abi.to_tokens(tokens);
1022 tokens.append("fn");
1023 self.ident.to_tokens(tokens);
1024 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001025 tokens.append("(");
1026 tokens.append_separated(&decl.inputs, ",");
1027 tokens.append(")");
1028 if let FunctionRetTy::Ty(ref ty) = decl.output {
1029 tokens.append("->");
1030 ty.to_tokens(tokens);
1031 }
David Tolnay42602292016-10-01 22:25:45 -07001032 generics.where_clause.to_tokens(tokens);
1033 block.to_tokens(tokens);
1034 }
David Tolnay35902302016-10-06 01:11:08 -07001035 ItemKind::Mod(ref items) => {
1036 self.vis.to_tokens(tokens);
1037 tokens.append("mod");
1038 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001039 match *items {
1040 Some(ref items) => {
1041 tokens.append("{");
1042 tokens.append_all(items);
1043 tokens.append("}");
1044 }
1045 None => tokens.append(";"),
1046 }
David Tolnay35902302016-10-06 01:11:08 -07001047 }
1048 ItemKind::ForeignMod(ref foreign_mod) => {
1049 self.vis.to_tokens(tokens);
1050 match foreign_mod.abi {
1051 Some(ref abi) => abi.to_tokens(tokens),
1052 None => tokens.append("extern"),
1053 }
1054 tokens.append("{");
1055 tokens.append_all(&foreign_mod.items);
1056 tokens.append("}");
1057 }
David Tolnay3cf52982016-10-01 17:11:37 -07001058 ItemKind::Ty(ref ty, ref generics) => {
1059 self.vis.to_tokens(tokens);
1060 tokens.append("type");
1061 self.ident.to_tokens(tokens);
1062 generics.to_tokens(tokens);
1063 tokens.append("=");
1064 ty.to_tokens(tokens);
1065 tokens.append(";");
1066 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001067 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001068 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001069 tokens.append("enum");
1070 self.ident.to_tokens(tokens);
1071 generics.to_tokens(tokens);
1072 generics.where_clause.to_tokens(tokens);
1073 tokens.append("{");
1074 for variant in variants {
1075 variant.to_tokens(tokens);
1076 tokens.append(",");
1077 }
1078 tokens.append("}");
1079 }
1080 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001081 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001082 tokens.append("struct");
1083 self.ident.to_tokens(tokens);
1084 generics.to_tokens(tokens);
1085 generics.where_clause.to_tokens(tokens);
1086 variant_data.to_tokens(tokens);
1087 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001088 VariantData::Struct(_) => {
1089 // no semicolon
1090 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001091 VariantData::Tuple(_) |
1092 VariantData::Unit => tokens.append(";"),
1093 }
1094 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001095 ItemKind::Union(ref variant_data, ref generics) => {
1096 self.vis.to_tokens(tokens);
1097 tokens.append("union");
1098 self.ident.to_tokens(tokens);
1099 generics.to_tokens(tokens);
1100 generics.where_clause.to_tokens(tokens);
1101 variant_data.to_tokens(tokens);
1102 }
David Tolnayca085422016-10-04 00:12:38 -07001103 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1104 self.vis.to_tokens(tokens);
1105 unsafety.to_tokens(tokens);
1106 tokens.append("trait");
1107 self.ident.to_tokens(tokens);
David Tolnaye4606332016-10-25 21:57:41 -07001108 generics.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001109 if !bound.is_empty() {
1110 tokens.append(":");
1111 tokens.append_separated(bound, "+");
1112 }
David Tolnayca085422016-10-04 00:12:38 -07001113 generics.where_clause.to_tokens(tokens);
1114 tokens.append("{");
1115 tokens.append_all(items);
1116 tokens.append("}");
1117 }
David Tolnayf94e2362016-10-04 00:29:51 -07001118 ItemKind::DefaultImpl(unsafety, ref path) => {
1119 unsafety.to_tokens(tokens);
1120 tokens.append("impl");
1121 path.to_tokens(tokens);
1122 tokens.append("for");
1123 tokens.append("..");
1124 tokens.append("{");
1125 tokens.append("}");
1126 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001127 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001128 unsafety.to_tokens(tokens);
1129 tokens.append("impl");
1130 generics.to_tokens(tokens);
1131 if let Some(ref path) = *path {
1132 polarity.to_tokens(tokens);
1133 path.to_tokens(tokens);
1134 tokens.append("for");
1135 }
1136 ty.to_tokens(tokens);
1137 generics.where_clause.to_tokens(tokens);
1138 tokens.append("{");
1139 tokens.append_all(items);
1140 tokens.append("}");
1141 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001142 ItemKind::Mac(ref mac) => {
1143 mac.path.to_tokens(tokens);
1144 tokens.append("!");
1145 self.ident.to_tokens(tokens);
1146 for tt in &mac.tts {
1147 tt.to_tokens(tokens);
1148 }
1149 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001150 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1151 // no semicolon
1152 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001153 _ => tokens.append(";"),
1154 }
1155 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001156 }
1157 }
1158 }
David Tolnay42602292016-10-01 22:25:45 -07001159
David Tolnay4a057422016-10-08 00:02:31 -07001160 impl ToTokens for ViewPath {
1161 fn to_tokens(&self, tokens: &mut Tokens) {
1162 match *self {
1163 ViewPath::Simple(ref path, ref rename) => {
1164 path.to_tokens(tokens);
1165 if let Some(ref rename) = *rename {
1166 tokens.append("as");
1167 rename.to_tokens(tokens);
1168 }
1169 }
1170 ViewPath::Glob(ref path) => {
1171 path.to_tokens(tokens);
1172 tokens.append("::");
1173 tokens.append("*");
1174 }
1175 ViewPath::List(ref path, ref items) => {
1176 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001177 if path.global || !path.segments.is_empty() {
1178 tokens.append("::");
1179 }
David Tolnay4a057422016-10-08 00:02:31 -07001180 tokens.append("{");
1181 tokens.append_separated(items, ",");
1182 tokens.append("}");
1183 }
1184 }
1185 }
1186 }
1187
1188 impl ToTokens for PathListItem {
1189 fn to_tokens(&self, tokens: &mut Tokens) {
1190 self.name.to_tokens(tokens);
1191 if let Some(ref rename) = self.rename {
1192 tokens.append("as");
1193 rename.to_tokens(tokens);
1194 }
1195 }
1196 }
1197
David Tolnayca085422016-10-04 00:12:38 -07001198 impl ToTokens for TraitItem {
1199 fn to_tokens(&self, tokens: &mut Tokens) {
1200 tokens.append_all(self.attrs.outer());
1201 match self.node {
1202 TraitItemKind::Const(ref ty, ref expr) => {
1203 tokens.append("const");
1204 self.ident.to_tokens(tokens);
1205 tokens.append(":");
1206 ty.to_tokens(tokens);
1207 if let Some(ref expr) = *expr {
1208 tokens.append("=");
1209 expr.to_tokens(tokens);
1210 }
1211 tokens.append(";");
1212 }
1213 TraitItemKind::Method(ref sig, ref block) => {
David Tolnayb31d3f02016-10-25 21:15:13 -07001214 sig.constness.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001215 sig.unsafety.to_tokens(tokens);
1216 sig.abi.to_tokens(tokens);
1217 tokens.append("fn");
1218 self.ident.to_tokens(tokens);
1219 sig.generics.to_tokens(tokens);
1220 tokens.append("(");
1221 tokens.append_separated(&sig.decl.inputs, ",");
1222 tokens.append(")");
1223 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1224 tokens.append("->");
1225 ty.to_tokens(tokens);
1226 }
1227 sig.generics.where_clause.to_tokens(tokens);
1228 match *block {
1229 Some(ref block) => block.to_tokens(tokens),
1230 None => tokens.append(";"),
1231 }
1232 }
1233 TraitItemKind::Type(ref bound, ref default) => {
1234 tokens.append("type");
1235 self.ident.to_tokens(tokens);
1236 if !bound.is_empty() {
1237 tokens.append(":");
1238 tokens.append_separated(bound, "+");
1239 }
1240 if let Some(ref default) = *default {
1241 tokens.append("=");
1242 default.to_tokens(tokens);
1243 }
1244 tokens.append(";");
1245 }
1246 TraitItemKind::Macro(ref mac) => {
1247 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001248 match mac.tts.last() {
1249 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1250 // no semicolon
1251 }
1252 _ => tokens.append(";"),
1253 }
David Tolnayca085422016-10-04 00:12:38 -07001254 }
1255 }
1256 }
1257 }
1258
David Tolnay4c9be372016-10-06 00:47:37 -07001259 impl ToTokens for ImplItem {
1260 fn to_tokens(&self, tokens: &mut Tokens) {
1261 tokens.append_all(self.attrs.outer());
1262 match self.node {
1263 ImplItemKind::Const(ref ty, ref expr) => {
1264 self.vis.to_tokens(tokens);
1265 self.defaultness.to_tokens(tokens);
1266 tokens.append("const");
1267 self.ident.to_tokens(tokens);
1268 tokens.append(":");
1269 ty.to_tokens(tokens);
1270 tokens.append("=");
1271 expr.to_tokens(tokens);
1272 tokens.append(";");
1273 }
1274 ImplItemKind::Method(ref sig, ref block) => {
1275 self.vis.to_tokens(tokens);
1276 self.defaultness.to_tokens(tokens);
David Tolnayb31d3f02016-10-25 21:15:13 -07001277 sig.constness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001278 sig.unsafety.to_tokens(tokens);
1279 sig.abi.to_tokens(tokens);
1280 tokens.append("fn");
1281 self.ident.to_tokens(tokens);
1282 sig.generics.to_tokens(tokens);
1283 tokens.append("(");
1284 tokens.append_separated(&sig.decl.inputs, ",");
1285 tokens.append(")");
1286 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1287 tokens.append("->");
1288 ty.to_tokens(tokens);
1289 }
1290 sig.generics.where_clause.to_tokens(tokens);
1291 block.to_tokens(tokens);
1292 }
1293 ImplItemKind::Type(ref ty) => {
1294 self.vis.to_tokens(tokens);
1295 self.defaultness.to_tokens(tokens);
1296 tokens.append("type");
1297 self.ident.to_tokens(tokens);
1298 tokens.append("=");
1299 ty.to_tokens(tokens);
1300 tokens.append(";");
1301 }
1302 ImplItemKind::Macro(ref mac) => {
1303 mac.to_tokens(tokens);
1304 match mac.tts.last() {
1305 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1306 // no semicolon
1307 }
1308 _ => tokens.append(";"),
1309 }
1310 }
1311 }
1312 }
1313 }
1314
David Tolnay35902302016-10-06 01:11:08 -07001315 impl ToTokens for ForeignItem {
1316 fn to_tokens(&self, tokens: &mut Tokens) {
1317 tokens.append_all(self.attrs.outer());
1318 match self.node {
1319 ForeignItemKind::Fn(ref decl, ref generics) => {
1320 self.vis.to_tokens(tokens);
1321 tokens.append("fn");
1322 self.ident.to_tokens(tokens);
1323 generics.to_tokens(tokens);
1324 tokens.append("(");
1325 tokens.append_separated(&decl.inputs, ",");
1326 tokens.append(")");
1327 if let FunctionRetTy::Ty(ref ty) = decl.output {
1328 tokens.append("->");
1329 ty.to_tokens(tokens);
1330 }
1331 generics.where_clause.to_tokens(tokens);
1332 tokens.append(";");
1333 }
1334 ForeignItemKind::Static(ref ty, mutability) => {
1335 self.vis.to_tokens(tokens);
1336 tokens.append("static");
1337 mutability.to_tokens(tokens);
1338 self.ident.to_tokens(tokens);
1339 tokens.append(":");
1340 ty.to_tokens(tokens);
1341 tokens.append(";");
1342 }
1343 }
1344 }
1345 }
1346
David Tolnay62f374c2016-10-02 13:37:00 -07001347 impl ToTokens for FnArg {
1348 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001349 match *self {
1350 FnArg::SelfRef(ref lifetime, mutability) => {
1351 tokens.append("&");
1352 lifetime.to_tokens(tokens);
1353 mutability.to_tokens(tokens);
1354 tokens.append("self");
1355 }
1356 FnArg::SelfValue(mutability) => {
1357 mutability.to_tokens(tokens);
1358 tokens.append("self");
1359 }
1360 FnArg::Captured(ref pat, ref ty) => {
1361 pat.to_tokens(tokens);
1362 tokens.append(":");
1363 ty.to_tokens(tokens);
1364 }
1365 FnArg::Ignored(ref ty) => {
1366 ty.to_tokens(tokens);
1367 }
1368 }
David Tolnay62f374c2016-10-02 13:37:00 -07001369 }
1370 }
1371
David Tolnay42602292016-10-01 22:25:45 -07001372 impl ToTokens for Unsafety {
1373 fn to_tokens(&self, tokens: &mut Tokens) {
1374 match *self {
1375 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001376 Unsafety::Normal => {
1377 // nothing
1378 }
David Tolnay42602292016-10-01 22:25:45 -07001379 }
1380 }
1381 }
1382
1383 impl ToTokens for Constness {
1384 fn to_tokens(&self, tokens: &mut Tokens) {
1385 match *self {
1386 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001387 Constness::NotConst => {
1388 // nothing
1389 }
David Tolnay42602292016-10-01 22:25:45 -07001390 }
1391 }
1392 }
1393
David Tolnay4c9be372016-10-06 00:47:37 -07001394 impl ToTokens for Defaultness {
1395 fn to_tokens(&self, tokens: &mut Tokens) {
1396 match *self {
1397 Defaultness::Default => tokens.append("default"),
1398 Defaultness::Final => {
1399 // nothing
1400 }
1401 }
1402 }
1403 }
1404
1405 impl ToTokens for ImplPolarity {
1406 fn to_tokens(&self, tokens: &mut Tokens) {
1407 match *self {
1408 ImplPolarity::Negative => tokens.append("!"),
1409 ImplPolarity::Positive => {
1410 // nothing
1411 }
1412 }
1413 }
1414 }
1415
David Tolnay42602292016-10-01 22:25:45 -07001416 impl ToTokens for Abi {
1417 fn to_tokens(&self, tokens: &mut Tokens) {
1418 tokens.append("extern");
1419 self.0.to_tokens(tokens);
1420 }
1421 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001422}