blob: 9ba8560ac92b70864e3a3c6fd3fb3081a55159d2 [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) >>
430 punct!(")") >>
431 ret: option!(preceded!(punct!("->"), ty)) >>
432 where_clause: where_clause >>
433 body: block >>
434 (Item {
435 ident: name,
436 vis: vis,
437 attrs: attrs,
438 node: ItemKind::Fn(
439 Box::new(FnDecl {
440 inputs: inputs,
441 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
442 }),
443 unsafety,
444 constness,
445 abi.map(Abi),
446 Generics {
447 where_clause: where_clause,
448 .. generics
449 },
450 Box::new(body),
451 ),
452 })
453 ));
454
David Tolnayca085422016-10-04 00:12:38 -0700455 named!(fn_arg -> FnArg, alt!(
456 do_parse!(
457 punct!("&") >>
458 lt: option!(lifetime) >>
459 mutability: mutability >>
460 keyword!("self") >>
461 (FnArg::SelfRef(lt, mutability))
462 )
463 |
464 do_parse!(
465 mutability: mutability >>
466 keyword!("self") >>
467 (FnArg::SelfValue(mutability))
468 )
469 |
470 do_parse!(
471 pat: pat >>
472 punct!(":") >>
473 ty: ty >>
474 (FnArg::Captured(pat, ty))
475 )
476 |
477 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700478 ));
479
David Tolnay35902302016-10-06 01:11:08 -0700480 named!(item_mod -> Item, do_parse!(
481 attrs: many0!(outer_attr) >>
482 vis: visibility >>
483 keyword!("mod") >>
484 id: ident >>
485 punct!("{") >>
486 items: many0!(item) >>
487 punct!("}") >>
488 (Item {
489 ident: id,
490 vis: vis,
491 attrs: attrs,
492 node: ItemKind::Mod(items),
493 })
494 ));
495
496 named!(item_foreign_mod -> Item, do_parse!(
497 attrs: many0!(outer_attr) >>
498 keyword!("extern") >>
499 abi: option!(quoted_string) >>
500 punct!("{") >>
501 items: many0!(foreign_item) >>
502 punct!("}") >>
503 (Item {
504 ident: "".into(),
505 vis: Visibility::Inherited,
506 attrs: attrs,
507 node: ItemKind::ForeignMod(ForeignMod {
508 abi: abi.map(Abi),
509 items: items,
510 }),
511 })
512 ));
513
514 named!(foreign_item -> ForeignItem, alt!(
515 foreign_fn
516 |
517 foreign_static
518 ));
519
520 named!(foreign_fn -> ForeignItem, do_parse!(
521 attrs: many0!(outer_attr) >>
522 vis: visibility >>
523 keyword!("fn") >>
524 name: ident >>
525 generics: generics >>
526 punct!("(") >>
527 inputs: separated_list!(punct!(","), fn_arg) >>
528 punct!(")") >>
529 ret: option!(preceded!(punct!("->"), ty)) >>
530 where_clause: where_clause >>
531 punct!(";") >>
532 (ForeignItem {
533 ident: name,
534 attrs: attrs,
535 node: ForeignItemKind::Fn(
536 Box::new(FnDecl {
537 inputs: inputs,
538 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
539 }),
540 Generics {
541 where_clause: where_clause,
542 .. generics
543 },
544 ),
545 vis: vis,
546 })
547 ));
548
549 named!(foreign_static -> ForeignItem, do_parse!(
550 attrs: many0!(outer_attr) >>
551 vis: visibility >>
552 keyword!("static") >>
553 mutability: mutability >>
554 id: ident >>
555 punct!(":") >>
556 ty: ty >>
557 punct!(";") >>
558 (ForeignItem {
559 ident: id,
560 attrs: attrs,
561 node: ForeignItemKind::Static(Box::new(ty), mutability),
562 vis: vis,
563 })
564 ));
565
David Tolnay3cf52982016-10-01 17:11:37 -0700566 named!(item_ty -> Item, do_parse!(
567 attrs: many0!(outer_attr) >>
568 vis: visibility >>
569 keyword!("type") >>
570 id: ident >>
571 generics: generics >>
572 punct!("=") >>
573 ty: ty >>
574 punct!(";") >>
575 (Item {
576 ident: id,
577 vis: vis,
578 attrs: attrs,
579 node: ItemKind::Ty(Box::new(ty), generics),
580 })
581 ));
582
David Tolnaya96a3fa2016-09-24 07:17:42 -0700583 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700584 macro_input,
585 |def: MacroInput| Item {
586 ident: def.ident,
587 vis: def.vis,
588 attrs: def.attrs,
589 node: match def.body {
590 Body::Enum(variants) => {
591 ItemKind::Enum(variants, def.generics)
592 }
593 Body::Struct(variant_data) => {
594 ItemKind::Struct(variant_data, def.generics)
595 }
596 }
597 }
598 ));
David Tolnay42602292016-10-01 22:25:45 -0700599
David Tolnay2f9fa632016-10-03 22:08:48 -0700600 named!(item_union -> Item, do_parse!(
601 attrs: many0!(outer_attr) >>
602 vis: visibility >>
603 keyword!("union") >>
604 id: ident >>
605 generics: generics >>
606 where_clause: where_clause >>
607 fields: struct_like_body >>
608 (Item {
609 ident: id,
610 vis: vis,
611 attrs: attrs,
612 node: ItemKind::Union(
613 VariantData::Struct(fields),
614 Generics {
615 where_clause: where_clause,
616 .. generics
617 },
618 ),
619 })
620 ));
621
David Tolnay0aecb732016-10-03 23:03:50 -0700622 named!(item_trait -> Item, do_parse!(
623 attrs: many0!(outer_attr) >>
624 vis: visibility >>
625 unsafety: unsafety >>
626 keyword!("trait") >>
627 id: ident >>
628 generics: generics >>
629 bounds: opt_vec!(preceded!(
630 punct!(":"),
631 separated_nonempty_list!(punct!("+"), ty_param_bound)
632 )) >>
633 where_clause: where_clause >>
634 punct!("{") >>
635 body: many0!(trait_item) >>
636 punct!("}") >>
637 (Item {
638 ident: id,
639 vis: vis,
640 attrs: attrs,
641 node: ItemKind::Trait(
642 unsafety,
643 Generics {
644 where_clause: where_clause,
645 .. generics
646 },
647 bounds,
648 body,
649 ),
650 })
651 ));
652
David Tolnayf94e2362016-10-04 00:29:51 -0700653 named!(item_default_impl -> Item, do_parse!(
654 attrs: many0!(outer_attr) >>
655 unsafety: unsafety >>
656 keyword!("impl") >>
657 path: path >>
658 keyword!("for") >>
659 punct!("..") >>
660 punct!("{") >>
661 punct!("}") >>
662 (Item {
663 ident: "".into(),
664 vis: Visibility::Inherited,
665 attrs: attrs,
666 node: ItemKind::DefaultImpl(unsafety, path),
667 })
668 ));
669
David Tolnay0aecb732016-10-03 23:03:50 -0700670 named!(trait_item -> TraitItem, alt!(
671 trait_item_const
672 |
673 trait_item_method
674 |
675 trait_item_type
676 |
677 trait_item_mac
678 ));
679
680 named!(trait_item_const -> TraitItem, do_parse!(
681 attrs: many0!(outer_attr) >>
682 keyword!("const") >>
683 id: ident >>
684 punct!(":") >>
685 ty: ty >>
686 value: option!(preceded!(punct!("="), expr)) >>
687 punct!(";") >>
688 (TraitItem {
689 ident: id,
690 attrs: attrs,
691 node: TraitItemKind::Const(ty, value),
692 })
693 ));
694
695 named!(trait_item_method -> TraitItem, do_parse!(
696 attrs: many0!(outer_attr) >>
697 constness: constness >>
698 unsafety: unsafety >>
699 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
700 keyword!("fn") >>
701 name: ident >>
702 generics: generics >>
703 punct!("(") >>
704 inputs: separated_list!(punct!(","), fn_arg) >>
705 punct!(")") >>
706 ret: option!(preceded!(punct!("->"), ty)) >>
707 where_clause: where_clause >>
708 body: option!(block) >>
709 cond!(body.is_none(), punct!(";")) >>
710 (TraitItem {
711 ident: name,
712 attrs: attrs,
713 node: TraitItemKind::Method(
714 MethodSig {
715 unsafety: unsafety,
716 constness: constness,
717 abi: abi.map(Abi),
718 decl: FnDecl {
719 inputs: inputs,
720 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
721 },
722 generics: Generics {
723 where_clause: where_clause,
724 .. generics
725 },
726 },
727 body,
728 ),
729 })
730 ));
731
732 named!(trait_item_type -> TraitItem, do_parse!(
733 attrs: many0!(outer_attr) >>
734 keyword!("type") >>
735 id: ident >>
736 bounds: opt_vec!(preceded!(
737 punct!(":"),
738 separated_nonempty_list!(punct!("+"), ty_param_bound)
739 )) >>
740 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700741 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700742 (TraitItem {
743 ident: id,
744 attrs: attrs,
745 node: TraitItemKind::Type(bounds, default),
746 })
747 ));
748
749 named!(trait_item_mac -> TraitItem, do_parse!(
750 attrs: many0!(outer_attr) >>
751 id: ident >>
752 punct!("!") >>
753 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700754 cond!(match body.delim {
755 DelimToken::Paren | DelimToken::Bracket => true,
756 DelimToken::Brace => false,
757 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700758 (TraitItem {
759 ident: id.clone(),
760 attrs: attrs,
761 node: TraitItemKind::Macro(Mac {
762 path: id.into(),
763 tts: vec![TokenTree::Delimited(body)],
764 }),
765 })
766 ));
767
David Tolnay4c9be372016-10-06 00:47:37 -0700768 named!(item_impl -> Item, do_parse!(
769 attrs: many0!(outer_attr) >>
770 unsafety: unsafety >>
771 keyword!("impl") >>
772 generics: generics >>
773 polarity_path: alt!(
774 do_parse!(
775 polarity: impl_polarity >>
776 path: path >>
777 keyword!("for") >>
778 ((polarity, Some(path)))
779 )
780 |
781 epsilon!() => { |_| (ImplPolarity::Positive, None) }
782 ) >>
783 self_ty: ty >>
784 where_clause: where_clause >>
785 punct!("{") >>
786 body: many0!(impl_item) >>
787 punct!("}") >>
788 (Item {
789 ident: "".into(),
790 vis: Visibility::Inherited,
791 attrs: attrs,
792 node: ItemKind::Impl(
793 unsafety,
794 polarity_path.0,
795 Generics {
796 where_clause: where_clause,
797 .. generics
798 },
799 polarity_path.1,
800 Box::new(self_ty),
801 body,
802 ),
803 })
804 ));
805
806 named!(impl_item -> ImplItem, alt!(
807 impl_item_const
808 |
809 impl_item_method
810 |
811 impl_item_type
812 |
813 impl_item_macro
814 ));
815
816 named!(impl_item_const -> ImplItem, do_parse!(
817 attrs: many0!(outer_attr) >>
818 vis: visibility >>
819 defaultness: defaultness >>
820 keyword!("const") >>
821 id: ident >>
822 punct!(":") >>
823 ty: ty >>
824 punct!("=") >>
825 value: expr >>
826 punct!(";") >>
827 (ImplItem {
828 ident: id,
829 vis: vis,
830 defaultness: defaultness,
831 attrs: attrs,
832 node: ImplItemKind::Const(ty, value),
833 })
834 ));
835
836 named!(impl_item_method -> ImplItem, do_parse!(
837 attrs: many0!(outer_attr) >>
838 vis: visibility >>
839 defaultness: defaultness >>
840 constness: constness >>
841 unsafety: unsafety >>
842 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
843 keyword!("fn") >>
844 name: ident >>
845 generics: generics >>
846 punct!("(") >>
847 inputs: separated_list!(punct!(","), fn_arg) >>
848 punct!(")") >>
849 ret: option!(preceded!(punct!("->"), ty)) >>
850 where_clause: where_clause >>
851 body: block >>
852 (ImplItem {
853 ident: name,
854 vis: vis,
855 defaultness: defaultness,
856 attrs: attrs,
857 node: ImplItemKind::Method(
858 MethodSig {
859 unsafety: unsafety,
860 constness: constness,
861 abi: abi.map(Abi),
862 decl: FnDecl {
863 inputs: inputs,
864 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
865 },
866 generics: Generics {
867 where_clause: where_clause,
868 .. generics
869 },
870 },
871 body,
872 ),
873 })
874 ));
875
876 named!(impl_item_type -> ImplItem, do_parse!(
877 attrs: many0!(outer_attr) >>
878 vis: visibility >>
879 defaultness: defaultness >>
880 keyword!("type") >>
881 id: ident >>
882 punct!("=") >>
883 ty: ty >>
884 punct!(";") >>
885 (ImplItem {
886 ident: id,
887 vis: vis,
888 defaultness: defaultness,
889 attrs: attrs,
890 node: ImplItemKind::Type(ty),
891 })
892 ));
893
894 named!(impl_item_macro -> ImplItem, do_parse!(
895 attrs: many0!(outer_attr) >>
896 id: ident >>
897 punct!("!") >>
898 body: delimited >>
899 cond!(match body.delim {
900 DelimToken::Paren | DelimToken::Bracket => true,
901 DelimToken::Brace => false,
902 }, punct!(";")) >>
903 (ImplItem {
904 ident: id.clone(),
905 vis: Visibility::Inherited,
906 defaultness: Defaultness::Final,
907 attrs: attrs,
908 node: ImplItemKind::Macro(Mac {
909 path: id.into(),
910 tts: vec![TokenTree::Delimited(body)],
911 }),
912 })
913 ));
914
915 named!(impl_polarity -> ImplPolarity, alt!(
916 punct!("!") => { |_| ImplPolarity::Negative }
917 |
918 epsilon!() => { |_| ImplPolarity::Positive }
919 ));
920
David Tolnay42602292016-10-01 22:25:45 -0700921 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700922 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700923 |
924 epsilon!() => { |_| Constness::NotConst }
925 ));
926
927 named!(unsafety -> Unsafety, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700928 keyword!("unsafe") => { |_| Unsafety::Unsafe }
David Tolnay42602292016-10-01 22:25:45 -0700929 |
930 epsilon!() => { |_| Unsafety::Normal }
931 ));
David Tolnay4c9be372016-10-06 00:47:37 -0700932
933 named!(defaultness -> Defaultness, alt!(
934 keyword!("default") => { |_| Defaultness::Default }
935 |
936 epsilon!() => { |_| Defaultness::Final }
937 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700938}
David Tolnay4a51dc72016-10-01 00:40:31 -0700939
940#[cfg(feature = "printing")]
941mod printing {
942 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700943 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700944 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700945 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700946 use quote::{Tokens, ToTokens};
947
948 impl ToTokens for Item {
949 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -0700950 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -0700951 match self.node {
952 ItemKind::ExternCrate(ref original) => {
953 tokens.append("extern");
954 tokens.append("crate");
955 if let Some(ref original) = *original {
956 original.to_tokens(tokens);
957 tokens.append("as");
958 }
959 self.ident.to_tokens(tokens);
960 tokens.append(";");
961 }
David Tolnay4a057422016-10-08 00:02:31 -0700962 ItemKind::Use(ref view_path) => {
963 self.vis.to_tokens(tokens);
964 tokens.append("use");
965 view_path.to_tokens(tokens);
966 tokens.append(";");
967 }
David Tolnay47a877c2016-10-01 16:50:55 -0700968 ItemKind::Static(ref ty, ref mutability, ref expr) => {
969 self.vis.to_tokens(tokens);
970 tokens.append("static");
971 mutability.to_tokens(tokens);
972 self.ident.to_tokens(tokens);
973 tokens.append(":");
974 ty.to_tokens(tokens);
975 tokens.append("=");
976 expr.to_tokens(tokens);
977 tokens.append(";");
978 }
979 ItemKind::Const(ref ty, ref expr) => {
980 self.vis.to_tokens(tokens);
981 tokens.append("const");
982 self.ident.to_tokens(tokens);
983 tokens.append(":");
984 ty.to_tokens(tokens);
985 tokens.append("=");
986 expr.to_tokens(tokens);
987 tokens.append(";");
988 }
David Tolnay42602292016-10-01 22:25:45 -0700989 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
990 self.vis.to_tokens(tokens);
991 constness.to_tokens(tokens);
992 unsafety.to_tokens(tokens);
993 abi.to_tokens(tokens);
994 tokens.append("fn");
995 self.ident.to_tokens(tokens);
996 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -0700997 tokens.append("(");
998 tokens.append_separated(&decl.inputs, ",");
999 tokens.append(")");
1000 if let FunctionRetTy::Ty(ref ty) = decl.output {
1001 tokens.append("->");
1002 ty.to_tokens(tokens);
1003 }
David Tolnay42602292016-10-01 22:25:45 -07001004 generics.where_clause.to_tokens(tokens);
1005 block.to_tokens(tokens);
1006 }
David Tolnay35902302016-10-06 01:11:08 -07001007 ItemKind::Mod(ref items) => {
1008 self.vis.to_tokens(tokens);
1009 tokens.append("mod");
1010 self.ident.to_tokens(tokens);
1011 tokens.append("{");
1012 tokens.append_all(items);
1013 tokens.append("}");
1014 }
1015 ItemKind::ForeignMod(ref foreign_mod) => {
1016 self.vis.to_tokens(tokens);
1017 match foreign_mod.abi {
1018 Some(ref abi) => abi.to_tokens(tokens),
1019 None => tokens.append("extern"),
1020 }
1021 tokens.append("{");
1022 tokens.append_all(&foreign_mod.items);
1023 tokens.append("}");
1024 }
David Tolnay3cf52982016-10-01 17:11:37 -07001025 ItemKind::Ty(ref ty, ref generics) => {
1026 self.vis.to_tokens(tokens);
1027 tokens.append("type");
1028 self.ident.to_tokens(tokens);
1029 generics.to_tokens(tokens);
1030 tokens.append("=");
1031 ty.to_tokens(tokens);
1032 tokens.append(";");
1033 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001034 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001035 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001036 tokens.append("enum");
1037 self.ident.to_tokens(tokens);
1038 generics.to_tokens(tokens);
1039 generics.where_clause.to_tokens(tokens);
1040 tokens.append("{");
1041 for variant in variants {
1042 variant.to_tokens(tokens);
1043 tokens.append(",");
1044 }
1045 tokens.append("}");
1046 }
1047 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001048 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001049 tokens.append("struct");
1050 self.ident.to_tokens(tokens);
1051 generics.to_tokens(tokens);
1052 generics.where_clause.to_tokens(tokens);
1053 variant_data.to_tokens(tokens);
1054 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001055 VariantData::Struct(_) => {
1056 // no semicolon
1057 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001058 VariantData::Tuple(_) |
1059 VariantData::Unit => tokens.append(";"),
1060 }
1061 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001062 ItemKind::Union(ref variant_data, ref generics) => {
1063 self.vis.to_tokens(tokens);
1064 tokens.append("union");
1065 self.ident.to_tokens(tokens);
1066 generics.to_tokens(tokens);
1067 generics.where_clause.to_tokens(tokens);
1068 variant_data.to_tokens(tokens);
1069 }
David Tolnayca085422016-10-04 00:12:38 -07001070 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1071 self.vis.to_tokens(tokens);
1072 unsafety.to_tokens(tokens);
1073 tokens.append("trait");
1074 self.ident.to_tokens(tokens);
1075 if !bound.is_empty() {
1076 tokens.append(":");
1077 tokens.append_separated(bound, "+");
1078 }
1079 generics.to_tokens(tokens);
1080 generics.where_clause.to_tokens(tokens);
1081 tokens.append("{");
1082 tokens.append_all(items);
1083 tokens.append("}");
1084 }
David Tolnayf94e2362016-10-04 00:29:51 -07001085 ItemKind::DefaultImpl(unsafety, ref path) => {
1086 unsafety.to_tokens(tokens);
1087 tokens.append("impl");
1088 path.to_tokens(tokens);
1089 tokens.append("for");
1090 tokens.append("..");
1091 tokens.append("{");
1092 tokens.append("}");
1093 }
David Tolnay4c9be372016-10-06 00:47:37 -07001094 ItemKind::Impl(unsafety,
1095 polarity,
1096 ref generics,
1097 ref path,
1098 ref ty,
1099 ref items) => {
1100 unsafety.to_tokens(tokens);
1101 tokens.append("impl");
1102 generics.to_tokens(tokens);
1103 if let Some(ref path) = *path {
1104 polarity.to_tokens(tokens);
1105 path.to_tokens(tokens);
1106 tokens.append("for");
1107 }
1108 ty.to_tokens(tokens);
1109 generics.where_clause.to_tokens(tokens);
1110 tokens.append("{");
1111 tokens.append_all(items);
1112 tokens.append("}");
1113 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001114 ItemKind::Mac(ref mac) => {
1115 mac.path.to_tokens(tokens);
1116 tokens.append("!");
1117 self.ident.to_tokens(tokens);
1118 for tt in &mac.tts {
1119 tt.to_tokens(tokens);
1120 }
1121 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001122 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1123 // no semicolon
1124 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001125 _ => tokens.append(";"),
1126 }
1127 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001128 }
1129 }
1130 }
David Tolnay42602292016-10-01 22:25:45 -07001131
David Tolnay4a057422016-10-08 00:02:31 -07001132 impl ToTokens for ViewPath {
1133 fn to_tokens(&self, tokens: &mut Tokens) {
1134 match *self {
1135 ViewPath::Simple(ref path, ref rename) => {
1136 path.to_tokens(tokens);
1137 if let Some(ref rename) = *rename {
1138 tokens.append("as");
1139 rename.to_tokens(tokens);
1140 }
1141 }
1142 ViewPath::Glob(ref path) => {
1143 path.to_tokens(tokens);
1144 tokens.append("::");
1145 tokens.append("*");
1146 }
1147 ViewPath::List(ref path, ref items) => {
1148 path.to_tokens(tokens);
1149 tokens.append("::");
1150 tokens.append("{");
1151 tokens.append_separated(items, ",");
1152 tokens.append("}");
1153 }
1154 }
1155 }
1156 }
1157
1158 impl ToTokens for PathListItem {
1159 fn to_tokens(&self, tokens: &mut Tokens) {
1160 self.name.to_tokens(tokens);
1161 if let Some(ref rename) = self.rename {
1162 tokens.append("as");
1163 rename.to_tokens(tokens);
1164 }
1165 }
1166 }
1167
David Tolnayca085422016-10-04 00:12:38 -07001168 impl ToTokens for TraitItem {
1169 fn to_tokens(&self, tokens: &mut Tokens) {
1170 tokens.append_all(self.attrs.outer());
1171 match self.node {
1172 TraitItemKind::Const(ref ty, ref expr) => {
1173 tokens.append("const");
1174 self.ident.to_tokens(tokens);
1175 tokens.append(":");
1176 ty.to_tokens(tokens);
1177 if let Some(ref expr) = *expr {
1178 tokens.append("=");
1179 expr.to_tokens(tokens);
1180 }
1181 tokens.append(";");
1182 }
1183 TraitItemKind::Method(ref sig, ref block) => {
1184 sig.unsafety.to_tokens(tokens);
1185 sig.abi.to_tokens(tokens);
1186 tokens.append("fn");
1187 self.ident.to_tokens(tokens);
1188 sig.generics.to_tokens(tokens);
1189 tokens.append("(");
1190 tokens.append_separated(&sig.decl.inputs, ",");
1191 tokens.append(")");
1192 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1193 tokens.append("->");
1194 ty.to_tokens(tokens);
1195 }
1196 sig.generics.where_clause.to_tokens(tokens);
1197 match *block {
1198 Some(ref block) => block.to_tokens(tokens),
1199 None => tokens.append(";"),
1200 }
1201 }
1202 TraitItemKind::Type(ref bound, ref default) => {
1203 tokens.append("type");
1204 self.ident.to_tokens(tokens);
1205 if !bound.is_empty() {
1206 tokens.append(":");
1207 tokens.append_separated(bound, "+");
1208 }
1209 if let Some(ref default) = *default {
1210 tokens.append("=");
1211 default.to_tokens(tokens);
1212 }
1213 tokens.append(";");
1214 }
1215 TraitItemKind::Macro(ref mac) => {
1216 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001217 match mac.tts.last() {
1218 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1219 // no semicolon
1220 }
1221 _ => tokens.append(";"),
1222 }
David Tolnayca085422016-10-04 00:12:38 -07001223 }
1224 }
1225 }
1226 }
1227
David Tolnay4c9be372016-10-06 00:47:37 -07001228 impl ToTokens for ImplItem {
1229 fn to_tokens(&self, tokens: &mut Tokens) {
1230 tokens.append_all(self.attrs.outer());
1231 match self.node {
1232 ImplItemKind::Const(ref ty, ref expr) => {
1233 self.vis.to_tokens(tokens);
1234 self.defaultness.to_tokens(tokens);
1235 tokens.append("const");
1236 self.ident.to_tokens(tokens);
1237 tokens.append(":");
1238 ty.to_tokens(tokens);
1239 tokens.append("=");
1240 expr.to_tokens(tokens);
1241 tokens.append(";");
1242 }
1243 ImplItemKind::Method(ref sig, ref block) => {
1244 self.vis.to_tokens(tokens);
1245 self.defaultness.to_tokens(tokens);
1246 sig.unsafety.to_tokens(tokens);
1247 sig.abi.to_tokens(tokens);
1248 tokens.append("fn");
1249 self.ident.to_tokens(tokens);
1250 sig.generics.to_tokens(tokens);
1251 tokens.append("(");
1252 tokens.append_separated(&sig.decl.inputs, ",");
1253 tokens.append(")");
1254 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1255 tokens.append("->");
1256 ty.to_tokens(tokens);
1257 }
1258 sig.generics.where_clause.to_tokens(tokens);
1259 block.to_tokens(tokens);
1260 }
1261 ImplItemKind::Type(ref ty) => {
1262 self.vis.to_tokens(tokens);
1263 self.defaultness.to_tokens(tokens);
1264 tokens.append("type");
1265 self.ident.to_tokens(tokens);
1266 tokens.append("=");
1267 ty.to_tokens(tokens);
1268 tokens.append(";");
1269 }
1270 ImplItemKind::Macro(ref mac) => {
1271 mac.to_tokens(tokens);
1272 match mac.tts.last() {
1273 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1274 // no semicolon
1275 }
1276 _ => tokens.append(";"),
1277 }
1278 }
1279 }
1280 }
1281 }
1282
David Tolnay35902302016-10-06 01:11:08 -07001283 impl ToTokens for ForeignItem {
1284 fn to_tokens(&self, tokens: &mut Tokens) {
1285 tokens.append_all(self.attrs.outer());
1286 match self.node {
1287 ForeignItemKind::Fn(ref decl, ref generics) => {
1288 self.vis.to_tokens(tokens);
1289 tokens.append("fn");
1290 self.ident.to_tokens(tokens);
1291 generics.to_tokens(tokens);
1292 tokens.append("(");
1293 tokens.append_separated(&decl.inputs, ",");
1294 tokens.append(")");
1295 if let FunctionRetTy::Ty(ref ty) = decl.output {
1296 tokens.append("->");
1297 ty.to_tokens(tokens);
1298 }
1299 generics.where_clause.to_tokens(tokens);
1300 tokens.append(";");
1301 }
1302 ForeignItemKind::Static(ref ty, mutability) => {
1303 self.vis.to_tokens(tokens);
1304 tokens.append("static");
1305 mutability.to_tokens(tokens);
1306 self.ident.to_tokens(tokens);
1307 tokens.append(":");
1308 ty.to_tokens(tokens);
1309 tokens.append(";");
1310 }
1311 }
1312 }
1313 }
1314
David Tolnay62f374c2016-10-02 13:37:00 -07001315 impl ToTokens for FnArg {
1316 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001317 match *self {
1318 FnArg::SelfRef(ref lifetime, mutability) => {
1319 tokens.append("&");
1320 lifetime.to_tokens(tokens);
1321 mutability.to_tokens(tokens);
1322 tokens.append("self");
1323 }
1324 FnArg::SelfValue(mutability) => {
1325 mutability.to_tokens(tokens);
1326 tokens.append("self");
1327 }
1328 FnArg::Captured(ref pat, ref ty) => {
1329 pat.to_tokens(tokens);
1330 tokens.append(":");
1331 ty.to_tokens(tokens);
1332 }
1333 FnArg::Ignored(ref ty) => {
1334 ty.to_tokens(tokens);
1335 }
1336 }
David Tolnay62f374c2016-10-02 13:37:00 -07001337 }
1338 }
1339
David Tolnay42602292016-10-01 22:25:45 -07001340 impl ToTokens for Unsafety {
1341 fn to_tokens(&self, tokens: &mut Tokens) {
1342 match *self {
1343 Unsafety::Unsafe => tokens.append("unsafe"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001344 Unsafety::Normal => {
1345 // nothing
1346 }
David Tolnay42602292016-10-01 22:25:45 -07001347 }
1348 }
1349 }
1350
1351 impl ToTokens for Constness {
1352 fn to_tokens(&self, tokens: &mut Tokens) {
1353 match *self {
1354 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001355 Constness::NotConst => {
1356 // nothing
1357 }
David Tolnay42602292016-10-01 22:25:45 -07001358 }
1359 }
1360 }
1361
David Tolnay4c9be372016-10-06 00:47:37 -07001362 impl ToTokens for Defaultness {
1363 fn to_tokens(&self, tokens: &mut Tokens) {
1364 match *self {
1365 Defaultness::Default => tokens.append("default"),
1366 Defaultness::Final => {
1367 // nothing
1368 }
1369 }
1370 }
1371 }
1372
1373 impl ToTokens for ImplPolarity {
1374 fn to_tokens(&self, tokens: &mut Tokens) {
1375 match *self {
1376 ImplPolarity::Negative => tokens.append("!"),
1377 ImplPolarity::Positive => {
1378 // nothing
1379 }
1380 }
1381 }
1382 }
1383
David Tolnay42602292016-10-01 22:25:45 -07001384 impl ToTokens for Abi {
1385 fn to_tokens(&self, tokens: &mut Tokens) {
1386 tokens.append("extern");
1387 self.0.to_tokens(tokens);
1388 }
1389 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001390}